# Degrees of freedom ANOVA table for regression

I've calculated the following ANOVA table for a linear regression in R:

anova(lm(mpg ~ drat, mtcars))

Analysis of Variance Table

Response: mpg
Df Sum Sq Mean Sq F value Pr(>F)
drat 1 522.48 522.48 25.97 1.776e-05 ***
Residuals 30 603.57 20.12
---


I'm wondering how the degrees of freedom for the residuals are calculated. I can see it is n-2, but why minus 2?

## 1 Answer

It is $n-2$ because you have fitted the intercept and a slope for drat. Generally, if you have $p$ predictors and the intercept, the degrees of freedom for the residuals are $n-p-1$ (with $n$ being the sample size). The degrees of freedom are the sample size minus the number of estimated parameters. This document provides a nice annotation for the ANOVA table in R (from page 21 onwards).