Originally we had planned for you to use Posit Cloud not just for quizzes/exams, but for in-class work and homework too
It turns out that the limits on the free version of Posit Cloud are too restrictive
New plan:
You can use Posit Cloud for quizzes/exams
You can use RStudio (install on your computer) for in-class work and homework
Measuring prediction error
Predicting price from area
Residuals and fitted values
The fitted value (“y hat”) for observation i is the predicted value from the regression line:
\widehat{y}_i = \widehat{\beta_0} + \widehat{\beta_1} \times \text{area}_i
The residual is the corresponding prediction error: e_i = y_i - \widehat{y}_i = \text{observed price} - \text{predicted price}
The fitted line tells us about the part of price predictable from area
The residuals tell us about the part of price not predictable from area
Visualizing residuals and fitted values
What does a residual tell us?
What is a “typical” residual?
The residual tells us the prediction error for a given point
The standard deviation of the residuals tells us the typical size of these prediction errors (in absolute value)
It’s a measure of how variable our prediction errors are
Low variability: our predictions are usually close to actual values
High variability: our predictions are often far off from actual values
In R, this is called the Residual Standard Error (RSE)
summary(fit)
Call:lm(formula = price ~ area, data = houses)Residuals: Min 1Q Median 3Q Max -604840 -90641 2955 83298 519720 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 4296.668 21017.955 0.2044 0.8381 area 279.139 10.101 27.6337 <0.0000000000000002 ***---Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1Residual standard error: 155630 on 506 degrees of freedomMultiple R-squared: 0.60146, Adjusted R-squared: 0.60067 F-statistic: 763.62 on 1 and 506 DF, p-value: < 0.000000000000000222
From line 16: RSE = $155,630
Another interpretation of RSE
The residuals look approximately Normally distributed
The mean of the residuals is 0
The standard deviation of the residuals is the RSE
95% of a Normal distribution is within 2 standard deviations of the mean
Therefore: 95% of the time, the prediction error is within \pm 2 \times \text{RSE} of the predicted value
Correlation
\text{Cor}(X,Y) measures linear association between two variables
Between -1 and +1
Sign indicates direction of linear relationship
Magnitude indicates strength of linear relationship (0 = none, \pm 1 = perfect)
Correlation
R^2 (R-squared)
R^2 is a measure of the strength of the linear relationship between the Y and all X’s in the linear regression
R^2 ranges from 0 to 1
0 = no linear relationship
1 = perfect linear relationship
Defining R^2
R^2 has two equivalent definitions:
The squared correlation between observed and fitted values from the regression model
The proportion of variance in the outcome Y “explained” by the regression model
Finding R^2
summary(fit)
Call:lm(formula = price ~ area, data = houses)Residuals: Min 1Q Median 3Q Max -604840 -90641 2955 83298 519720 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 4296.668 21017.955 0.2044 0.8381 area 279.139 10.101 27.6337 <0.0000000000000002 ***---Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1Residual standard error: 155630 on 506 degrees of freedomMultiple R-squared: 0.60146, Adjusted R-squared: 0.60067 F-statistic: 763.62 on 1 and 506 DF, p-value: < 0.000000000000000222