Could someone point me in the right direction for the following issue: A function is defined as follows: tfun <- function(dat) { fmla <- as.formula("y~x+z") dat2 <- dat mdl <- lm(fmla,dat2) mdl <- step(mdl) } Then the following code dat <- data.frame(x=1:10,z=1:10,y=(1:10)^2+10*(1:10)) tfun(dat) generates the output Start: AIC= 43.67 y ~ x + z Error in model.frame.default(formula = y ~ z, data = dat2, drop.unused.levels = TRUE) : Object "dat2" not found Any help or pointers gratefully accepted. Thanks, Darryl Greig (darryl at hpli.hpl.hp.com)
ripley@stats.ox.ac.uk
2003-Mar-02 17:18 UTC
[R] model.frame.default problem in function definition
This works in R-devel, for which NEWS says o step(), add1.default() and drop1.default() now work somewhat better if called from a function. It's mainly a scoping problem, related to changes made way back in 1.2.x. However, it is also an bad example, as z is really excluded in the fit. stepAIC in the latest MASS (VR_7.0-11) will work, but warns about this. I hope that this is just an over-simplification of the real problem. On Sun, 2 Mar 2003, Darryl Greig wrote:> Could someone point me in the right direction for the following issue: > > A function is defined as follows: > > tfun <- function(dat) > { > fmla <- as.formula("y~x+z") > dat2 <- dat > mdl <- lm(fmla,dat2) > mdl <- step(mdl) > } > > Then the following code > > dat <- data.frame(x=1:10,z=1:10,y=(1:10)^2+10*(1:10)) > tfun(dat) > > generates the output > > Start: AIC= 43.67 > y ~ x + z > > Error in model.frame.default(formula = y ~ z, data = dat2, > drop.unused.levels = TRUE) : > Object "dat2" not found > > Any help or pointers gratefully accepted. > > Thanks, > Darryl Greig (darryl at hpli.hpl.hp.com) > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > http://www.stat.math.ethz.ch/mailman/listinfo/r-help >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595