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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With