# How to get prediction intervals at mean & at max of covariate values in R

I'm still working on R problems from a book, and using my spending data.

Part 1: I need to predict the amount that a male with average data for status, income and verbal would spend along a 95% CI. (NB, male is coded as 0 and female 1 in the data set.)

First I used the linear model newspend<-lm(sepnding ~ status + income + verbal + sex) and from a previous question the sex coefficient is for female, so how would I get the male?

> qt(0.975,42)
[1] 2.018082
> c(-22.12-2.02*8.21,-22.12+2.02*8.21)
[1] -38.7042  -5.5358


First Question: I don't know how what this means and how will I get a 95% CI for males if this is for females?

Part 2: Then repeat the prediction for males with maximal values of status, income and verbal and determine which CI is wider. Second, I used the max. values for the data in a new equation.

     sex             status          income
Min.   :0.0000   Min.   :18.00   Min.   : 0.600
1st Qu.:0.0000   1st Qu.:28.00   1st Qu.: 2.000
Median :0.0000   Median :43.00   Median : 3.250
Mean   :0.4043   Mean   :45.23   Mean   : 4.642
3rd Qu.:1.0000   3rd Qu.:61.50   3rd Qu.: 6.210
Max.   :1.0000   Max.   :75.00   Max.   :15.000

verbal          spending
Min.   : 1.00   Min.   :  0.0
1st Qu.: 6.00   1st Qu.:  1.1
Median : 7.00   Median :  6.0
Mean   : 6.66   Mean   : 19.3
3rd Qu.: 8.00   3rd Qu.: 19.4
Max.   :10.00   Max.   :156.0


x0<-data.frame(status=75.00, income=15.00, verbal=10.00, sex=1.00)
I'm not sure if this is how I get the maximal values for a male? (Or should the 2nd part be a new question since it's a lot of information?)

• You have obtained confidence interval not for females (average spending), but for the difference between males and females (with respect to other features in the model). – O_Devinyak Sep 26 '12 at 7:31
• so my prediction is incorrect for what I am trying to determine for the problem. Please clarify....thanks – MsSnowy Sep 26 '12 at 17:46

Here is how you set up the date for male average spending estimate with CIs:

mavg<-data.frame(status=45.23, income=4.642, verbal=6.66, sex=0.00);


...female average:

favg<-data.frame(status=45.23, income=4.642, verbal=6.66, sex=1.00);


...male max:

mmax<-data.frame(status=75.00, income=15.00, verbal=10.00, sex=0.00);


...female max:

fmax<-data.frame(status=75.00, income=15.00, verbal=10.00, sex=1.00);


The general way to get the predictions given a new dataset is...

predict(newspend,newdata=FOO,interval='conf');


...where FOO is whichever one of your new data.frames you want the results for. Or you can combine them into one data.frame and get all the results at once:

mfavgmax<-rbind(mavg,favg,mmax,fmax);
predict(newspend,newdata=mfavgmax,interval='conf');


Be warned though: your model and your predictions assume that status, income, verbal, and sex all affect spending independently of each other. In real life data is rarely that clean. I would check whether there are significant interaction terms:

install.packages(MASS); # Only need to run this line once
library(MASS);
newspend.aic<-stepAIC(newspend,trace=F,direction='both',scope=list(lower=.~.,upper=.~(.)^4));


Now you can do predict on newspend.aic like you did above. You might have new interaction terms in the model.

Another problem is that status, income, verbal, and sex may be correlated with each other (this is called multicollinearity). So, the population maximum (or average, etc.) for males might be different than for females. At high statuses income mean/max will obviously be different than at lower statuses. For categorical variables like male/female, you can at least get the mean/max for that category and use that for your predictions.

But what if you want to get estimates for spending as status increases continuously form 18 to 75? Sure predict will give you estimates with income and verbal set to any number you like... but income and verbal are not static, they probably correlated with status and with each other... and how to pick reasonable values for these numeric variables for prediction purposes is the question that's stumping me right now and how I happened onto your question.

Hope my answer helps, and it's scary, isn't it, how fast you get to the deep end of the pool with a simple-seeming problem?