C W
2015-Jan-30 03:34 UTC
[R] How to use curve() function without using x as the variable name inside expression?
Hi Bill, You solved by problem. For some reason, I thought xname was only referring to name of the x-axis. I remember last time I fixed it, it was something about xname, couldn't get it right this time. Thanks! Saved me hours from frustration. Mike On Thu, Jan 29, 2015 at 9:04 PM, William Dunlap <wdunlap at tibco.com> wrote:> Does > help(curve) > talk about its 'xname' argument? > > Try > curve(10*foofoo, from=0, to=17, xname="foofoo") > > You will have to modify your function, since curve() will > call it once with a long vector for the independent variable > and func(rnorm(10), rnorm(10), mu=seq(0,5,len=501)) won't > work right. > > > Bill Dunlap > TIBCO Software > wdunlap tibco.com > > On Thu, Jan 29, 2015 at 5:43 PM, C W <tmrsg11 at gmail.com> wrote: > >> Hi Rui, >> >> Thank you for your help. That works for now, but eventually, I need to be >> pass in x and y. >> >> Is there a way to tell the curve() function, x is a fix vector, mu is a >> variable! >> >> Thanks, >> >> Mike >> >> On Thu, Jan 29, 2015 at 5:25 PM, Rui Barradas <ruipbarradas at sapo.pt> >> wrote: >> >> > Hello, >> > >> > The following will work, but I don't know if it's what you want. func2 >> > will get x and y from the global environment. >> > >> > func2 <- function(mu){ >> > x + y + mu ^ 2 >> > } >> > >> > curve(func2, from = 0, to = 10) >> > >> > >> > Hope this helps, >> > >> > Rui Barradas >> > >> > Em 29-01-2015 21:02, C W escreveu: >> > >> >> Hi all, >> >> >> >> I want to graph a curve as a function of mu, not x. >> >> >> >> Here's the R code: >> >> >> >> x <- rnorm(10) >> >> y <- rnorm(10) >> >> >> >> func <- function(x, y, mu){ >> >> x + y + mu ^ 2 >> >> } >> >> >> >> curve(f = func(x = x, y = y, mu), from = 0, to = 10) >> >> I know I can change variable mu to x, but is there a way to tell R >> that mu >> >> is the variable of interest, not x. >> >> >> >> Thanks in advance, >> >> >> >> Mike >> >> >> >> [[alternative HTML version deleted]] >> >> >> >> ______________________________________________ >> >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> >> https://stat.ethz.ch/mailman/listinfo/r-help >> >> PLEASE do read the posting guide http://www.R-project.org/ >> >> posting-guide.html >> >> and provide commented, minimal, self-contained, reproducible code. >> >> >> >> >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > >[[alternative HTML version deleted]]
C W
2015-Jan-31 08:10 UTC
[R] How to use curve() function without using x as the variable name inside expression?
Hi Bill,
One quick question. What if I wanted to use curve() for a uniform
distribution?
Say, unif(0.5, 1.3), 0 elsewhere.
My R code:
func <- function(min, max){
1 / (max - min)
}
curve(func(min = 0.5, max = 1.3), from = 0, to = 2)
curve() wants an expression, but I have a constant. And I want zero
everywhere else.
Thanks,
Mike
On Thu, Jan 29, 2015 at 10:34 PM, C W <tmrsg11 at gmail.com> wrote:
> Hi Bill,
>
> You solved by problem. For some reason, I thought xname was only
> referring to name of the x-axis.
>
> I remember last time I fixed it, it was something about xname, couldn't
> get it right this time.
>
> Thanks! Saved me hours from frustration.
>
> Mike
>
> On Thu, Jan 29, 2015 at 9:04 PM, William Dunlap <wdunlap at
tibco.com> wrote:
>
>> Does
>> help(curve)
>> talk about its 'xname' argument?
>>
>> Try
>> curve(10*foofoo, from=0, to=17, xname="foofoo")
>>
>> You will have to modify your function, since curve() will
>> call it once with a long vector for the independent variable
>> and func(rnorm(10), rnorm(10), mu=seq(0,5,len=501)) won't
>> work right.
>>
>>
>> Bill Dunlap
>> TIBCO Software
>> wdunlap tibco.com
>>
>> On Thu, Jan 29, 2015 at 5:43 PM, C W <tmrsg11 at gmail.com>
wrote:
>>
>>> Hi Rui,
>>>
>>> Thank you for your help. That works for now, but eventually, I
need to
>>> be
>>> pass in x and y.
>>>
>>> Is there a way to tell the curve() function, x is a fix vector, mu
is a
>>> variable!
>>>
>>> Thanks,
>>>
>>> Mike
>>>
>>> On Thu, Jan 29, 2015 at 5:25 PM, Rui Barradas <ruipbarradas at
sapo.pt>
>>> wrote:
>>>
>>> > Hello,
>>> >
>>> > The following will work, but I don't know if it's what
you want. func2
>>> > will get x and y from the global environment.
>>> >
>>> > func2 <- function(mu){
>>> > x + y + mu ^ 2
>>> > }
>>> >
>>> > curve(func2, from = 0, to = 10)
>>> >
>>> >
>>> > Hope this helps,
>>> >
>>> > Rui Barradas
>>> >
>>> > Em 29-01-2015 21:02, C W escreveu:
>>> >
>>> >> Hi all,
>>> >>
>>> >> I want to graph a curve as a function of mu, not x.
>>> >>
>>> >> Here's the R code:
>>> >>
>>> >> x <- rnorm(10)
>>> >> y <- rnorm(10)
>>> >>
>>> >> func <- function(x, y, mu){
>>> >> x + y + mu ^ 2
>>> >> }
>>> >>
>>> >> curve(f = func(x = x, y = y, mu), from = 0, to = 10)
>>> >> I know I can change variable mu to x, but is there a way
to tell R
>>> that mu
>>> >> is the variable of interest, not x.
>>> >>
>>> >> Thanks in advance,
>>> >>
>>> >> Mike
>>> >>
>>> >> [[alternative HTML version deleted]]
>>> >>
>>> >> ______________________________________________
>>> >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and
more, see
>>> >> https://stat.ethz.ch/mailman/listinfo/r-help
>>> >> PLEASE do read the posting guide http://www.R-project.org/
>>> >> posting-guide.html
>>> >> and provide commented, minimal, self-contained,
reproducible code.
>>> >>
>>> >>
>>>
>>> [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more,
see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>
>
[[alternative HTML version deleted]]
Rolf Turner
2015-Jan-31 08:39 UTC
[R] How to use curve() function without using x as the variable name inside expression?
On 31/01/15 21:10, C W wrote:> Hi Bill, > > One quick question. What if I wanted to use curve() for a uniform > distribution? > > Say, unif(0.5, 1.3), 0 elsewhere. > > My R code: > func <- function(min, max){ > 1 / (max - min) > } > > curve(func(min = 0.5, max = 1.3), from = 0, to = 2) > > curve() wants an expression, but I have a constant. And I want zero > everywhere else.Well if that's what you want, then say so!!! func <- function(x,min,max) { ifelse(x < min | x > max, 0, 1/(max - min)) } curve(func(u,0.5,1.3),0,2,xname="u") Or, better (?) curve(func(u,0.5,1.3),0,2,xname="u",type="s") which avoids the slight slope in the "vertical" lines. cheers, Rolf Turner -- Rolf Turner Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276 Home phone: +64-9-480-4619