I've recently encountered an issue when trying to use the predict.glm
function.

 

I've gotten into the habit of using the dataframe$variablename method of
specifying terms in my model statements.  I thought this unambiguous
notation would be acceptable in all situations but it seems models
written this way are not accepted by the predict function.  Perhaps
others have encountered this problem as well.

 

The code below illustrates the issue.

 

 

######

## linear model example

 

# this works

 x<-1:100

 y<-2*x

 

 lm1<-glm(y~x)

 pred1<-predict(lm1,newdata=data.frame(x=101:150))

 

## so does this

 x<-1:100

 y<-2*x

 orig.df<-data.frame(x1=x,y1=y)

 

 lm1<-glm(y1~x1,data=orig.df)

 pred1<-predict(lm1,newdata=data.frame(x1=101:150))

 

## this does not run

 x<-1:100

 y<-2*x

 orig.df<-data.frame(x1=x,y1=y)

 

 lm1<-glm(orig.df$y1~orig.df$x1,data=orig.df)

 pred1<-predict(lm1,newdata=data.frame(x1=101:150))

 

 

The final statement generates the following warning:

 

Warning message:

'newdata' had 50 rows but variable(s) found have 100 rows

 

 

Hope this is of some help.

 

 

 

Brian Van Hezewijk 

 


	[[alternative HTML version deleted]]


