Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

predict() for glm.fit does not work. why?

Tags:

r

glm

prediction

I've built glm model in R using glm.fit() function:

m <- glm.fit(x = as.matrix(df[,x.id]), y = df[,y.id], family = gaussian())  

Afterwards, I tried to make some predictions, using (I am not sure that I chose s correctly):

    predict.glm(m, x, s = 0.005)

And got an error:

    Error in terms.default(object) : no terms component nor attribute

Here https://stat.ethz.ch/pipermail/r-help/2004-September/058242.html I found some sort of solution to a problem:

predict.glm.fit<-function(glmfit, newmatrix){
       newmatrix<-cbind(1,newmatrix)
        coef <- rbind(1, as.matrix(glmfit$coef))
        eta <- as.matrix(newmatrix) %*% as.matrix(coef)
        exp(eta)/(1 + exp(eta))
   }

But I can not figure out if it is not possible to use glm.fit and predict afterwards. Why it is possible or not? And how should one choose s correctly?

N.B. The problem can be ommited if using glm() function. But glm() function asks for formula, which is not quite convenient in some cases. STill if someone wants to use glm.fit & predictions afterwards here is some solution: https://stat.ethz.ch/pipermail/r-help/2004-September/058242.html

like image 796
Boddha Avatar asked Mar 23 '23 11:03

Boddha


1 Answers

You should be using glm not glm.fit. glm.fit is the workhorse of glm but glm returns an object of class c("glm", "lm") for which there is a predict.glm method. Then you only have to apply predict to the object returned by glm (possibly with some new data specified and the type of prediction that you want) and the generic predict function will select the correct method function.

like image 166
Ken Knoblauch Avatar answered Apr 06 '23 00:04

Ken Knoblauch