Re: [R] predicting values from multiple regression

From: Anna Lee <ana-lee_at_web.de>
Date: Mon, 21 Mar 2011 11:01:31 +0100

Dennis: Thank you so much! I got it now - it just works perfectly. Thanks a lot! Anna

2011/3/21 Dennis Murphy <djmuser_at_gmail.com>:
> Hi:
>
> To amplify Ista's and David's comments:
>
> (1) You should not be inputting separate vectors into lm(), especially if
> you intend to do prediction. They should be combined into a data frame
> instead. This is not a requirement, but it's a much safer strategy for
> modeling in R.
> (2) Your covariate st does not have a linear component. It should,
> particularly if this is an empirical model rather than a theoretical one.
> (3) You should be using poly(var, 2) to create orthogonal columns in the
> model matrix for the variables that are to contain quadratic terms.
> (4) The newdata =  argument of predict.lm() [whose help page you should read
> carefully] requires a data frame with columns having precisely the same
> variable names as exist in the RHS of the model formula in lm().
>
> Example:
> dd <- data.frame(y = rnorm(50), x1 = rnorm(50), x2 = runif(50, -2, 2), x3 =
> rpois(50, 10))
>
> #  fit yhat = b0 + b1 * x1 + b2 * x1^2 + b3 * x2 + b4 * x3 + b5 * x3^2
> mod <- lm(y ~ poly(x1, 2) + x2 + poly(x3, 2), data = dd)
>
> # Note that the names of the variables in newd are the same as those on the
> RHS of the formula in mod
> newd <- data.frame(x1 = rnorm(5), x2 = runif(5, -2, 2), x3 = rpois(5,
> 10))      # new data points
> # Append predictions to newd
> cbind(newd, predict(mod, newdata = newd))             # predictions at new
> data points
>
> # To just get predictions at the observed points, all you need is
> predict(mod)
>
> HTH,
> Dennis
>
> On Sun, Mar 20, 2011 at 11:54 AM, Anna Lee <ana-lee_at_web.de> wrote:
>>
>> Hey List,
>>
>> I did a multiple regression and my final model looks as follows:
>>
>> model9<-lm(calP ~ nsP + I(st^2) + distPr + I(distPr^2))
>>
>> Now I tried to predict the values for calP from this model using the
>> following function:
>>
>> xv<-seq(0,89,by=1)
>> yv<-predict(model9,list(distPr=xv,st=xv,nsP=xv))
>>
>> The predicted values are however strange. Now I do not know weather
>> just the model does not fit the data (actually all coefficiets are
>> significant and the plot(model) shows a good shape) or wether I did
>> something wrong with my prediction command. Does anyone have an
>> idea???
>>
>> --
>>
>>
>> Thanks a lot, Anna
>>
>> ______________________________________________
>> 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.
>
>

-- 



Der Inhalt dieser E-Mail ist vertraulich. Sollte Ihnen die E-Mail
irrtümlich zugesandt worden sein, bitte ich Sie, mich unverzüglich zu
benachrichtigen und die E-Mail zu löschen.

This e-mail is confidential. If you have received it in error, please
notify me immediately and delete it from your system.

______________________________________________
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.
Received on Mon 21 Mar 2011 - 10:04:45 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 21 Mar 2011 - 11:10:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive