A more general approach is to use the
logLik() function. It returns an object with the attribute
df that gives the fitted models degrees of freedom. The benefit of this approach is that it works with many other model classes (including
glm). In the case of ordinary linear regression (
lm) this corresponds to the number of parameters + 1 for the estimate of the error variance.
For "lm" fits it is assumed that the scale has been estimated (by maximum likelihood or REML), and all the constants in the log-likelihood are included.
You can get the number of observations this way too.
> X1 <- rnorm(10)
> X2 <- rnorm(10)
> Y <- X1 + X2 + rnorm(10)
> model <- lm(Y~X1+X2)
> ll <- logLik(model)