[R] how to keep the dropped term at each step when calling "step"?

Riyan Cheng rcheng at purdue.edu
Tue Dec 6 19:21:21 CET 2005


Hi all:

I am using the R function "step" to perform a model selection in backward 
direction. I'd like to automatically keep the dropped term at each step. So 
I wrote a filter function for the "keep" argument. However, the filter 
function cannot change the value of external variable and so doesn't work 
well. Anybody can help? Thank you in advance!

Regards,
Riyan Cheng

P.S., R code
###########################################
example(lm)
lm1 <- lm(Fertility ~ ., data = swiss)

tm<- attr(lm1$terms,"term.labels")

kp<- function(obj,aic){
  x<- attr(obj$terms,"term.labels")
  y<- setdiff(tm,x)
  if(length(y)==0)y=NULL
  tm<- x
  AIC<- aic

  list(n=length(x),dropped=y,AIC=aic)
}

g<- step(lm1,keep=kp,k=10)
g$keep
##############################################




More information about the R-help mailing list