[R] stepAIC()

Siew Leng TENG siewlengteng at yahoo.com
Thu Jul 31 00:03:06 CEST 2003


Hi,

I am experiencing a baffling behaviour of stepAIC(),
and I hope to get any advice/help on this. Greatly
appreciate any kind advice given.

I am using stepAIC() to, say, select a model via
stepwise selection method.

R Version : 1.7.1
Windows ME

Many thanks!


***Issue :

When stepAIC() is placed within a function, it seems
that stepAIC() cannot detect the data matrix, and the
program is halted as a result. However, when the same
codes are copied, paste and run in R workspace,
stepAIC can execute and R is able to produce the
desired output.

***Actions taken :
I had tried to look into and manipulate environments
and formals(f), but with not much luck.


***Code snippets (an example):

  library(MASS)
  library(nls)

  Data<-data.frame(matrix(rnorm(120),ncol=6))
  colnames(Data)<-c("Y",paste("X",1:5,sep=""))

  f<-function(A)
  {
  a <-glm(Y~X1+X2+X3, data=A)
  b
<-stepAIC(a,scope=list(upper=~X1+X2+X3+X4+X5,lower=~1),direction="both",trace=FALSE)
  b
  }

 f(Data) 


R gives the following error :

> f(Data)
Error in model.frame.default(formula = Y ~ X1 + X3,
data = A, drop.unused.levels = TRUE) : 
        Object "A" not found


However, when the same codes are copied, paste and run
in the workspace, stepAIC() can run, and R is able to
produce the desired output :

A <- Data
a <-glm(Y~X1+X2+X3, data=A)
b
<-stepAIC(a,scope=list(upper=~X1+X2+X3+X4+X5,lower=~1),direction="both",trace=FALSE)
b

> b

Call:  glm(formula = Y ~ X4, data = A) 

Coefficients:
(Intercept)           X4  
     0.1942      -0.3314  

Degrees of Freedom: 19 Total (i.e. Null);  18 Residual
Null Deviance:      20.51 
Residual Deviance: 17.95        AIC: 60.6 
> 


__________________________________

Yahoo! SiteBuilder - Free, easy-to-use web site design software




More information about the R-help mailing list