Dear all, I'm having some problems with using the step() function inside another function. I think it is an environment problem but I do not know how to overcome it. Any suggestions are appreciated. I've prepared a simple example to illustrate my problem:> library(MASS) > data(Boston) > my.fun <- function(dataset) {+ l <- lm(medv ~ .,data=dataset) + final.l <- step(l) + }> model <- my.fun(Boston)Start: AIC= 1589.64 medv ~ crim + zn + indus + chas + nox + rm + age + dis + rad + tax + ptratio + black + lstat Df Sum of Sq RSS AIC - age 1 0.1 11078.8 1587.6 - indus 1 2.5 11081.3 1587.8 <none> 11078.8 1589.6 - chas 1 219.0 11297.8 1597.5 - tax 1 242.3 11321.0 1598.6 - crim 1 243.2 11322.0 1598.6 - zn 1 257.5 11336.3 1599.3 - black 1 270.6 11349.4 1599.9 - rad 1 479.2 11557.9 1609.1 - nox 1 487.2 11565.9 1609.4 - ptratio 1 1194.2 12273.0 1639.4 - dis 1 1232.4 12311.2 1641.0 - rm 1 1871.3 12950.1 1666.6 - lstat 1 2410.8 13489.6 1687.3 Error in model.frame.default(formula = medv ~ crim + zn + indus + chas + : Object "dataset" not found Apparently the step() function is not able to find the "dataset" object which is created inside the my.fun() function. My system information:> R.version_ platform i686-pc-linux-gnu arch i686 os linux-gnu system i686, linux-gnu status major 1 minor 6.1 year 2002 month 11 day 01 language R Thank you for any hints. -- Luis Torgo FEP/LIACC, University of Porto Phone : (+351) 22 607 88 30 Machine Learning Group Fax : (+351) 22 600 36 54 R. Campo Alegre, 823 email : ltorgo at liacc.up.pt 4150 PORTO - PORTUGAL WWW : liacc.up.pt/~ltorgo
Use R-devel, where this has been changed and your example works. On Mon, 24 Mar 2003, Luis Torgo wrote:> Dear all, > I'm having some problems with using the step() function inside another > function. I think it is an environment problem but I do not know how to > overcome it. Any suggestions are appreciated. > > I've prepared a simple example to illustrate my problem: > > > library(MASS) > > data(Boston) > > my.fun <- function(dataset) { > + l <- lm(medv ~ .,data=dataset) > + final.l <- step(l) > + } > > model <- my.fun(Boston) > Start: AIC= 1589.64 > medv ~ crim + zn + indus + chas + nox + rm + age + dis + rad + > tax + ptratio + black + lstat > > Df Sum of Sq RSS AIC > - age 1 0.1 11078.8 1587.6 > - indus 1 2.5 11081.3 1587.8 > <none> 11078.8 1589.6 > - chas 1 219.0 11297.8 1597.5 > - tax 1 242.3 11321.0 1598.6 > - crim 1 243.2 11322.0 1598.6 > - zn 1 257.5 11336.3 1599.3 > - black 1 270.6 11349.4 1599.9 > - rad 1 479.2 11557.9 1609.1 > - nox 1 487.2 11565.9 1609.4 > - ptratio 1 1194.2 12273.0 1639.4 > - dis 1 1232.4 12311.2 1641.0 > - rm 1 1871.3 12950.1 1666.6 > - lstat 1 2410.8 13489.6 1687.3 > Error in model.frame.default(formula = medv ~ crim + zn + indus + chas + : > Object "dataset" not found > > Apparently the step() function is not able to find the "dataset" object which > is created inside the my.fun() function. > > My system information: > > R.version > _ > platform i686-pc-linux-gnu > arch i686 > os linux-gnu > system i686, linux-gnu > status > major 1 > minor 6.1 > year 2002 > month 11 > day 01 > language R > > Thank you for any hints. > >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, 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
On Mon, 24 Mar 2003, Luis Torgo wrote:> Dear all, > I'm having some problems with using the step() function inside another > function. I think it is an environment problem but I do not know how to > overcome it. Any suggestions are appreciated.It's a bug. It's been reported and fixed for some time in the development version. -thomas