Re: [R] how to plot an user-defined function

From: John Smith <zmring_at_gmail.com>
Date: Wed, 6 Feb 2008 18:34:20 -0500

Thank all of you for your helps. They are very helpful.

But I have a further question. Suppose I have the following mixed effect model

thetaMixed <- function(tau, i)
  {
    w <- 1 / (s^2 + tau^2)
    mu <- sum(theta * w) / sum(w)
    b <- s[i]^2 / (s[i]^2 + tau^2)
    theta[i]*(1-b) + mu*b)
  }

I want draw all the mixed effects in a single figure using for (i in 1:10)
{

   plot(Vectorize(thetaMixed), 0, 2, i=i) }
and hope plot will recognize that i is the argument for function thetaMixed, and 0, 2 are the range for tau. But it fails.

Could anyone kindly help me on this issue?

Thanks

On Feb 5, 2008 10:45 PM, Duncan Murdoch <murdoch_at_stats.uwo.ca> wrote:

> jim holtman wrote:
> > Your function 'll' only returns a single value when passed a vector:
> >
> >
> >> x <- seq(0,2,.1)
> >> ll(x)
> >>
> > [1] -7.571559
> >
> >
> > 'plot' expects to pass a vector to the function and have it return a
> > vector of the same length; e.g.,
> >
> >
> >> sin(x)
> >>
> > [1] 0.00000000 0.09983342 0.19866933 0.29552021 0.38941834 0.47942554
> > 0.56464247 0.64421769 0.71735609
> > [10] 0.78332691 0.84147098 0.89120736 0.93203909 0.96355819 0.98544973
> > 0.99749499 0.99957360 0.99166481
> > [19] 0.97384763 0.94630009 0.90929743
> >
> >
> > So you either have to rewrite your function, or have a loop that will
> > evaluate the function at each individual point and then plot it.
> >
> Or use Vectorize, e.g.

>

> plot(Vectorize(ll), 0, 2)
>

> Duncan Murdoch
> > On Feb 5, 2008 7:06 PM, John Smith <zmring_at_gmail.com> wrote:
> >
> >> Dear R-users,
> >>
> >> Suppose I have defined a likelihood function as ll(tau), how can I plot
> this
> >> likelihood function by calling it by plot?
> >>
> >> I want to do it like this:
> >>
> >> ll <- function(tau)
> >> {
> >> w <- 1 / (s^2 + tau^2)
> >> mu <- sum(theta * w) / sum(w)
> >> -1/2*sum((theta-mu)^2*w -log(w))
> >> }
> >> plot(ll, 0, 2)
> >>
> >>
> >>
> >> But have the following error:
> >> Error in xy.coords(x, y, xlabel, ylabel, log) :
> >> 'x' and 'y' lengths differ
> >> In addition: Warning messages:
> >> 1: In s^2 + tau^2 :
> >> longer object length is not a multiple of shorter object length
> >> 2: In theta * w :
> >> longer object length is not a multiple of shorter object length
> >> 3: In (theta - mu)^2 * w :
> >> longer object length is not a multiple of shorter object length
> >>
> >>
> >> Thanks
> >>
> >> [[alternative HTML version deleted]]
> >>
> >> ______________________________________________
> >> R-help_at_r-project.org mailing list
> >> 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
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. Received on Wed 06 Feb 2008 - 23:37:17 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 07 Feb 2008 - 01:30:12 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive