[R] problem with a function

Sarah Goslee sarah.goslee at gmail.com
Fri May 28 18:03:23 CEST 2010


>From your code:

mean <- c(rep(mu0, mzero), rep(mu1,m-mzero))

mean() is a function. If you overwrite it with data, you may mess
other things up -
any function you call that calls mean will now fail (at best).

var.f <- function(rho) {
   (1-rho)*diag(m)+rho*J%*%t(J)
}

var.f() is a complete function, except that m and J are not passed
as arguments. Instead, you rely on them being present in the
calling environment, and that is both dangerous and bad practice.

Sarah

On Fri, May 28, 2010 at 12:00 PM, li li <hannah.hlx at gmail.com> wrote:
> I am not sure about "overwrite mean() with data".  My purpose was
> to generate random numbers that are from a multivariate normal
> distribution with the mean vector.
>
> For the var.f function, since I already specify m and J, so the only
> variable is really rho, so I wrote it as a function of rho only.
>
> Could you be a little more specific? Thanks a lot again.
> 2010/5/28 Sarah Goslee <sarah.goslee at gmail.com>
>>
>> There are a bunch of problems in your code:
>> you overwrite mean() with data, and that could screw things up.
>> you have a function var.f that isn't passed all the arguments it needs.
>> est.4 is defined several times, each overwriting the previous.
>>
>> First you need to clean up these sorts of problems since they can
>> lead to all kinds of bizarre results.
>>
>> Then, if you are still getting unexpected results, please send the list
>> a minimal example so that we can take a look.
>>
>> Sarah

-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list