I'd like to retrieve the values of a second order polynomial regression line based on a list of values for a parameter.
Here is the model:
fit <- lm(y ~ poly(age, 2) + height + age*height)
I would like to use a list of values for age and retrieve the value on the regression line, as well as the standard deviation and standard errors. 'age' is a continuous variable, but I want to create an array of discrete values and return the predicted values from the regression line.
Example:
age <- c(10, 11, 12, 13, 14)
Since you have an interaction term, the regression coefficients for either the linear or quadratic age
term (or both together) only have meaning when you simultaneously specify what value of height
is being considered. So to get predictions when the height is at its mean value you would do this:
predict(fit, data.frame(age=c(10, 11, 12, 13, 14), height=mean(height) ) )
bouncyball
brings up a good point. You asked about "standard deviation and standard errors", but coefficients and predictions don't have "standard deviations" as the term is usually used, but ratehr "standard errors of the estimate" usually shortened to just standard errors.
predict(fit, data.frame(age=c(10, 11, 12, 13, 14), height=mean(height) ), se.fit=TRUE )
I suppose if you did a bootstrap run and looked at the standard deviations of the separate coefficients as an estimate of the std error of the coefficients, that might be argued to be a standard deviation, but it would be in the scale of the parameter space rather than on the scale of the variables.
Your data has 2 variables, so you need to provide both an age and a height.
For example, using simulated data:
age = sample(10)
height = sort(rnorm(10, 6, 1))
y = sort(rnorm(10, 150, 30))
fit <- lm(y ~ age + poly(age, 2) + height + age*height)
To get predictions specify age and heights and then predict:
# I'm using my own heights, you should choose the values you're interested in
new.data <- data.frame(age=c(10, 11, 12, 13, 14) ,
height=c(5.7, 6.3, 5.8, 5.9, 6.0) )
> predict(fit, new.data)
1 2 3 4 5
132.76675715 137.70712251 113.39494557 102.07262016 88.84240532
To get confidence bands for each prediction
> predict(fit, new.data, interval="confidence")
fit lwr upr
1 132.76675715 96.0957812269 169.43773307
2 137.70712251 73.2174486246 202.19679641
3 113.39494557 39.5470153667 187.24287578
4 102.07262016 3.5466926099 200.59854771
5 88.84240532 -37.7404171712 215.42522781
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With