# New to R: computing the correlation of the residuals with the fitted values of a data set

I am new to R and trying to practice with some exercises. Given a data set with 40 observations and 5 variables. Spending is the the response and there are 4 predictors. I started with a linear model Residuals:

    Min      1Q  Median      3Q     Max
-51.082 -11.320  -1.451   9.452  94.252

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)  22.55565   17.19680   1.312   0.1968
sex         -22.11833    8.21111  -2.694   0.0101 *
status        0.05223    0.28111   0.186   0.8535
income        4.96198    1.02539   4.839 1.79e-05 ***
verbal       -2.95949    2.17215  -1.362   0.1803
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 22.69 on 42 degrees of freedom
Multiple R-squared: 0.5267, Adjusted R-squared: 0.4816
F-statistic: 11.69 on 4 and 42 DF,  p-value: 1.815e-06


First, is this what they mean by fit regression model and Secondly, how do I compute the correlation of the residuals with the fitted values?

• That is a trick question. Try to think through what you would expect the correlation between the residuals and the fitted values to be. Sep 8 '12 at 22:42

A linear model would be, e.g.,

fitted.model <- lm(spending ~ sex + status + income, data=dataset)


I guess that is what you did - and you probably called summary(fitted.model) to obtain the results you reported.

If you would like to see and use the fitted values and residuals you may call them using fitted() and resid(). So, e.g., if you want to calculate a correlation among fitted and residuals you could do

zapsmall(cor(fitted(fitted.model), resid(fitted.model)))


Another interesting feature is plot(fitted.model) to obtain a number of diagnostic plots. The first plot will give you the fitted vs residual plot.

Btw, who is "they?"

• Thank you, I calculated the correlation and the result was as follows cor(x,y)  -1.070659e-16 (a negative slope)But if I want to calculate the correlation of the residuals with one of the predictors like status..would i create lm2<-lm(spending ~ status, data=dataset) and perform the cor function against it? Also, if all predictors are constant, how would i predict spending for a male compared to a female? Sep 8 '12 at 23:02
• You missed the point. Try to think through what the result means. Sep 8 '12 at 23:12
• I fully agree with Dirk. There is a clear expectation on how the correlation between residuals and the fitted values has to be and your results fully agree with this expectations. The correlation is not really negative... Sep 9 '12 at 0:19
• Referring to the results of the lm, only 53% of the variation can be explained by the predictors. The correlation indicate a negative linear relationship between the variables, as one decrease the other increases. Sep 9 '12 at 1:31
• MsSnowy, try again with the modified calculation shown in this edited answer. Also, think about just how big a slope of $-10^{-16}$ would be: what would a line of that slope look like when plotted on axes extensive enough to show the full ranges of the predictions and the residuals?
– whuber
Sep 9 '12 at 15:02