halptekin
2011-Jul-19 17:03 UTC
[R] How to get predicted values of y for different x values?
Here is my model with interaction terms and control variables (I changed variables names for easy read): reg1 <- lm(y ~ x1*x2*x3 +control1 + control2 + control3) x1 ranges from 0 to 6; x2 from 0 to 5; and x3 from 0 to 4. All three are discrete ordinal variables; but I will treat them as continuous variables. (a) How can I see the predicted values of y for each of these scenarios (210 scenarios I guess)? (b) How can I see the predicted value of y for the minimum and maximum values of x1, x2, and x3 (8 scenarios)? (c) How can I see the predicted value of y for x1=6; x2=5; and x3=4 (1 scenario)? -- View this message in context: http://r.789695.n4.nabble.com/How-to-get-predicted-values-of-y-for-different-x-values-tp3678662p3678662.html Sent from the R help mailing list archive at Nabble.com.
Jeff Newmiller
2011-Jul-19 18:36 UTC
[R] How to get predicted values of y for different x values?
?predict Use data.frame() to generate input vectors (newdata) for which you want predicted values. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil@dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. halptekin <halptekin@gmail.com> wrote: Here is my model with interaction terms and control variables (I changed variables names for easy read): reg1 <- lm(y ~ x1*x2*x3 +control1 + control2 + control3) x1 ranges from 0 to 6; x2 from 0 to 5; and x3 from 0 to 4. All three are discrete ordinal variables; but I will treat them as continuous variables. (a) How can I see the predicted values of y for each of these scenarios (210 scenarios I guess)? (b) How can I see the predicted value of y for the minimum and maximum values of x1, x2, and x3 (8 scenarios)? (c) How can I see the predicted value of y for x1=6; x2=5; and x3=4 (1 scenario)? -- View this message in context: http://r.789695.n4.nabble.com/How-to-get-predicted-values-of-y-for-different-x-values-tp3678662p3678662.html Sent from the R help mailing list archive at Nabble.com. _____________________________________________ R-help@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. [[alternative HTML version deleted]]
David Winsemius
2011-Jul-19 19:14 UTC
[R] How to get predicted values of y for different x values?
On Jul 19, 2011, at 2:36 PM, Jeff Newmiller wrote:> ?predict > > Use data.frame() to generate input vectors (newdata) for which you > want predicted values.The OP probably needs to use expand.grid to generate the spanning combinations of x values. He will in addition need to include values for "control" variables. dfrm <- expand.grid(x1 = 0: 6, x2 = 0: 5, x3 = 0 : 4) dfrm$control1 <- <some value of same class as original control1 data> repeat x 2 -- David.> --------------------------------------------------------------------------- > Jeff Newmiller The ..... ..... Go Live... > DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... > Live: OO#.. Dead: OO#.. Playing > Research Engineer (Solar/Batteries O.O#. #.O#. with > /Software/Embedded Controllers) .OO#. .OO#. rocks...1k > --------------------------------------------------------------------------- > Sent from my phone. Please excuse my brevity. > > halptekin <halptekin at gmail.com> wrote: > > Here is my model with interaction terms and control variables (I > changed > variables names for easy read): > > reg1 <- lm(y ~ x1*x2*x3 +control1 + control2 + control3) > > x1 ranges from 0 to 6; x2 from 0 to 5; and x3 from 0 to 4. All three > are > discrete ordinal variables; but I will treat them as continuous > variables. > > (a) How can I see the predicted values of y for each of these > scenarios (210 > scenarios I guess)? > (b) How can I see the predicted value of y for the minimum and maximum > values of x1, x2, and x3 (8 scenarios)? > (c) How can I see the predicted value of y for x1=6; x2=5; and x3=4 (1 > scenario)? > > -- > View this message in context: http://r.789695.n4.nabble.com/How-to-get-predicted-values-of-y-for-different-x-values-tp3678662p3678662.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. > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 West Hartford, CT
Daniel Malter
2011-Jul-19 21:12 UTC
[R] How to get predicted values of y for different x values?
Please read the posting guide (requires a self-contained example of code) and consult the help pages before posting. If you type ?predict.lm the help page clearly states that the argument 'newdata' takes "[a]n optional data frame in which to look for variables with which to predict..." x1<-rnorm(100) x2<-rnorm(100) e<-rnorm(100) y<-x1+x2+x1*x2+e reg<-lm(y~x1*x2) summary(reg) predict(reg,newdata=data.frame(x1=2,x2=2)) HTH, Daniel halptekin wrote:> > Here is my model with interaction terms and control variables (I changed > variables names for easy read): > > reg1 <- lm(y ~ x1*x2*x3 +control1 + control2 + control3) > > x1 ranges from 0 to 6; x2 from 0 to 5; and x3 from 0 to 4. All three are > discrete ordinal variables; but I will treat them as continuous variables. > (a) How can I see the predicted values of y for each of these scenarios > (210 y values I guess)? > (b) How can I see the predicted value of y for the minimum and maximum > values of x1, x2, and x3 (8 y values)? > (c) How can I see the predicted value of y for x1=6; x2=5; and x3=4 (only > one y value)? >-- View this message in context: http://r.789695.n4.nabble.com/How-to-get-predicted-values-of-y-for-different-x-values-tp3678658p3679351.html Sent from the R help mailing list archive at Nabble.com.