[R] glm.fit and predict.glm: error ' no terms component'
Thomas Lumley
tlumley at u.washington.edu
Thu Sep 30 16:16:36 CEST 2004
On Thu, 30 Sep 2004, Christoph Lehmann wrote:
> many thanks I did it the following way, based on Thomas' suggestion
There was a reason why I didn't call the function predict.glm.fit: it
isn't a method for predict. You will be calling it directly, rather than
via predict as you should for a method.
-thomas
> 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))
> }
>
>
> cheers
>
> christoph
>
>
>
>
>
>
>
> Thomas Lumley wrote:
>> On Wed, 29 Sep 2004, Christoph Lehmann wrote:
>>
>>> Hi
>>>
>>> when I fit a glm by
>>>
>>> glm.fit(x,y,family = binomial())
>>> and then try to use the object for prediction of newdata by:
>>>
>>> predict.glm(object, newdata)
>>>
>>> I get the error:
>>>
>>> Error in terms.default(object) : no terms component
>>>
>>> I know I can use glm() and a formula, but for my case I prefer
>>> glm.fit(x,y)...
>>
>>
>> Well, you can't use predict.glm that way. As the function name suggests,
>> it is a predict method for objects of class "glm", which in your case you
>> do not have.
>>
>> There are two reasons why it won't work. For type="terms" the formula is
>> needed to identify terms, and for any type of prediction the formula is
>> needed to convert the data frame newdata into a model matrix.
>>
>> You would need to write a function where the new data was a model matrix.
>> If you only need point predictions then
>>
>> predict_glm_fit<-function(glmfit, newmatrix, addintercept=TRUE){
>> if (addintercept)
>> newmatrix<-cbind(1,newmatrix)
>> eta<-glmfit$coef %*% newmatrix
>> family$linkinv(eta)
>> }
>>
>> would work.
>>
>> -thomas
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide!
>> http://www.R-project.org/posting-guide.html
>>
>>
>
>
Thomas Lumley Assoc. Professor, Biostatistics
tlumley at u.washington.edu University of Washington, Seattle
More information about the R-help
mailing list