Skip to content
Prev 108191 / 398498 Next

Using functions within functions (environment problems)

I don't think your subject line is relevant.  You do not have 'functions 
within functions': lmerFrames is not within lmer.  (You seem to be 
confusing functions within and calls from.)

Your example does not work (did you test it?).  When the erroneous runif 
call is corrected (it gives a result of length 0), I get a different error 
about 'weights', and indeed you have not specified 'weights' nor 'subset' 
nor 'na.action' nor 'offset'.

The following does work for me:

lmerWrapper <- function(formula, data, ...)
{

     xNew <- runif(length(data[,1]))
     fNew <- sample(1:4, length(data[,1]), replace = TRUE)
     data <- as.data.frame(cbind(data, xNew, fNew))
     formula <- update(formula, .~. + xNew + (1|fNew))
     out <- lmer (formula = formula, data = data, ...)
}

dat <- data.frame(Y = rnorm(100), X1 = rnorm(100), X2 = rnorm(100),
         F1 = as.factor(sample(1:4, 400, replace = T)))
test <- lmerWrapper (Y ~ X1 + X2 + (1|F1), data = dat)

so whatever your actual problem is, it is it seems not about finding xNew.


There is one potential problem I spotted.  One of the places a standard 
model-fitting function will look for variables is in the environment of 
'formula'.  This is an argument, and update.formula changes the 
environment, so it is possible that old (rather than additional) variables 
could disappear from view.
On Fri, 26 Jan 2007, Colin Beale wrote: