Hi, probably just a quick question: can I somehow change the formula used with predict? E.g., the regression was run on "y ~ u + v + w" but for the prediction the term v should be removed from the formula contained in the regression object and only "y ~ u + w" be used. I could use model.matrix etc. to do the predictions but it would be very helpful to know a simpler way. Thanks so much, Werner __________________________ verf?gt ?ber einen herausragenden Schutz gegen Massenmails. http://mail.yahoo.com
Remove term from formula for predict.lm
8 messages · Werner W., Henrique Dallazuanna, Gabor Grothendieck +3 more
Try this; mod1 <- lm(y ~ u + v + w, data = d) update(mod1, . ~ . -v)
On Tue, Jan 19, 2010 at 2:10 PM, Werner W. <pensterfuzzer at yahoo.de> wrote:
Hi, probably just a quick question: can I somehow change the formula used with predict? E.g., the regression was run on "y ~ u + v + w" but for the prediction the term v should be removed from the formula contained in the regression object and only "y ~ u + w" be used. I could use model.matrix etc. to do the predictions but it would be very helpful to know a simpler way. Thanks so much, ?Werner
__________________________ ?verf?gt ?ber einen herausragenden Schutz gegen Massenmails. http://mail.yahoo.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.
Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
This recomputes the lm but if that is ok then: mod <- lm(y1 ~ x1 + x2 + x3 + x4, anscombe) mod$call$formula <- update(as.formula(mod$call$formula), ~ . - x1 - x2) predict(eval(mod$call), list(x3 = 1, x4 = 1))
On Tue, Jan 19, 2010 at 11:10 AM, Werner W. <pensterfuzzer at yahoo.de> wrote:
Hi, probably just a quick question: can I somehow change the formula used with predict? E.g., the regression was run on "y ~ u + v + w" but for the prediction the term v should be removed from the formula contained in the regression object and only "y ~ u + w" be used. I could use model.matrix etc. to do the predictions but it would be very helpful to know a simpler way. Thanks so much, ?Werner
__________________________ ?verf?gt ?ber einen herausragenden Schutz gegen Massenmails. http://mail.yahoo.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.
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. @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.
Werner, You could set 0 to that regressors you don't want to consider for prediction. da <- expand.grid(A=1:20, B=rnorm(20, 4, 0.2), C=10^seq(1,2,l=20)) da$y <- rnorm(da$A, 0, 0.3) m0 <- lm(y~A+B+C, data=da) new <- da new$C <- 0 predict(m0)[1:5] predict(m0, newdata=new)[1:5] At your disposal. Walmes. ----- ..oooO .................................................................................................. ..(....)... 0ooo... Walmes Zeviani ...\..(.....(.....)... Master in Statistics and Agricultural Experimentation ....\_)..... )../.... walmeszeviani at hotmail.com, Lavras - MG, Brasil ............ (_/............................................................................................
View this message in context: http://n4.nabble.com/Remove-term-from-formula-for-predict-lm-tp1017686p1017759.html Sent from the R help mailing list archive at Nabble.com.
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
If you want to keep the same coefficients but ignore certain ones just use 0 for the ones you don't want: mod <- lm(Sepal.Length ~ Petal.Length + Petal.Width, iris) predict(mod, list(Petal.Length = 3, Petal.Width = 0)) Regarding the problem with the example, the example data was not the best for showing this because there are co-linear columns in the anscombe data set. Using iris we see that it does produce different answers:
mod <- lm(Sepal.Length ~ Petal.Length + Petal.Width, iris) predict(eval(mod$call), list(Petal.Length = 3, Petal.Width = 3))
1 4.857262
mod$call$formula <- update(as.formula(mod$call$formula), ~ . - Petal.Width) predict(eval(mod$call), list(Petal.Length = 3))
1 5.53337
On Tue, Jan 19, 2010 at 12:05 PM, werner w <pensterfuzzer at yahoo.de> 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. @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.
Thanks a lot for the answers! Somehow I thought there will be a facility to modify the formula. But setting unwanted variables to zero will of course work and maybe is simple enough. Thanks again!
View this message in context: http://n4.nabble.com/Remove-term-from-formula-for-predict-lm-tp1017686p1017919.html Sent from the R help mailing list archive at Nabble.com.