[R] Remove term from formula for predict.lm

David Winsemius dwinsemius at comcast.net
Tue Jan 19 18:29:06 CET 2010


On Jan 19, 2010, at 12:05 PM, werner w wrote:

>
> Thanks Gabor and Henrique!
>
> Sorry for the imprecise question. I want predict() to use the  
> coefficients
> estimated by the original regression but to exclude terms from the
> prediction formula. If I originally estimated y ~ x1 + x2 and got
> coefficients b0, b1, b2, I would like to remove x2 and predict y =  
> b0 +
> b1*x1 using the the originally estimated coefficients b0 and b1.

So just set x2 = 0 in your newdata argument.

>
> @Gabor: I tried your suggestion but it seems although predict now  
> accepts a
> list with fewer variables, the new function is not used so that the
> coefficient does not change.
>
>> mod <- lm(y1 ~ x1 + x2 + x3 + x4, anscombe)
>> predict(eval(mod$call), list(x1=1,x2=1,x3=1, x4=1))
>       1
> 4.684909
> Warning message:
> In predict.lm(eval(mod$call), list(x1 = 1, x2 = 1, x3 = 1, x4 = 1)) :
>  prediction from a rank-deficient fit may be misleading
>> mod$call$formula <- update(as.formula(mod$call$formula), ~ . - x1 -  
>> x2)
>> predict(eval(mod$call), list(x3=1, x4=1))
>       1
> 4.684909
>>
>
> Many thanks,
>  Werner
> -- 
> View this message in context: http://n4.nabble.com/Remove-term-from-formula-for-predict-lm-tp1017686p1017749.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list