[R] object not found inside step() function

David Winsemius dwinsemius at comcast.net
Mon Nov 16 21:03:35 CET 2009


On Nov 16, 2009, at 2:31 PM, shuai yuan wrote:

> Hi, there,
>
> My appologize if someone ask the same question before. I searched the
> mailing list and found one similar post, but not what i want.
>
> The problem for me is, I use the step( glm()....) to do naive forward
> selection for logistic regression.  My code is functional
> in the open environment. But if I wrap it up as a function, then R  
> keeps
> saying "object 'a' not found". Actually,  data frame
> "a" is inside the function.
>
> I did some search online. i guess the reason may be R did not keep  
> the data
> in glm() output after building the model but not sure.
> Can anyone please tell me how to work around this problem?
>
> Thanks a lot in advance.
>
> I am using R 2.9.0. Here is the sample code:
>
> #------------
> naivelr<-function(x,y){
> :
> :
> :
>
> a<-data.frame(x)
>
> form<- 
> paste("y~1+",paste(grep("X.*",names(a),value=T),collapse="+"),sep="")
>
> if(is.null(force.in)!=T){
> lowmo<-paste("y~1+",paste(grep("X.*",names(a) 
> [force.in],value=T),collapse="+"),sep="")
>
> } else
> {lowmo<-"y~1"}
>
> lower1<-glm(lowmo,family="binomial",data=data.frame(a,y))
> upper1<-glm(form,family="binomial",data=data.frame(a,y))

You are sticking data.frame= a inside another data.frame ????

>
>
> stepout<- 
> step 
> (lower1 
> ,scope 
> =list(lower=lower1,upper=upper1),direction="forward",k=0,trace=100)

Thats not the way I remember "step"-ping. I thought you made a fit and  
then "stepped" the formulas (using the same data), rather putting the  
whole glm object into a "lower" and an "upper". I could be wrong about  
that since I try to avoid using stepwise methods.

> # here is the error:Start:
> #AIC=689.62
> #y ~ 1
> #Error in data.frame(a, y) : object 'a' not found---but "a" is there!

But it's probably not in a form that can be interpreted. Consider  
adding y as a column in "a".

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list