It seems that calling lm()
from within a function or via lapply
screws up the $call
associated with a fit. Minimal working example:
> library(MASS)
> dat <- data.frame(x = 1:100, y=1:100)
> dat <- within(dat, z <- x + log(y) + rnorm(100))
> fits <- lapply(list(z ~ x + y, z ~ x + log(y)), lm, dat)
> stepAIC(fits[[1]]) # <-- error when I try to use the fit in other functions
Error in eval(expr, envir, enclos) : could not find function "FUN"
> fits[[1]]$call
FUN(formula = X[[i]], data = ..1) # Aha -- this must be why -- $call is screwed up
How do I resolve this problem and prevent the above error?