As stated in the title, how do you manually calculate the variance of the least squares estimator in R?

I know that the least estimates have the following formula:

$$\hat{\beta}=(X^TX)^{-1} X^T Y, $$

and the variance of the least squares estimator is given by

$$Var(\hat{\beta}) = σ^2(X^TX)^{−1}$$

  • 1
    $\begingroup$ Apply vcov to the regression object. You can find the algebraic formulas elsewhere here on CV if you want to work them out. Books on numerical linear algebra will provide effective algorithms for inverting $X^\prime X.$ I'm pretty sure most R regression functions use QR decomposition. $\endgroup$
    – whuber
    Aug 30, 2022 at 15:12
  • $\begingroup$ @whuber, I know how to use vcov. My question clearly stated how to do that "manually," so I can understand that concept comprehensively. An R example would only serve to help me understand this concept. $\endgroup$
    – Ereck
    Aug 30, 2022 at 15:16
  • 1
    $\begingroup$ There are many ways to interpret "manually." Most of them are covered in other threads, as I mentioned, and if you want to get into great detail that would be off topic here because the question is devolving into one about algorithms rather than being of statistical interest. $\endgroup$
    – whuber
    Aug 30, 2022 at 15:17
  • $\begingroup$ @whuber, I can easily find in R (𝑋′𝑋), but what about sigma^2? $\endgroup$
    – Ereck
    Aug 30, 2022 at 15:20
  • 1
    $\begingroup$ See, inter alia, stats.stackexchange.com/questions/72157, stats.stackexchange.com/questions/222233, stats.stackexchange.com/questions/117406 (formulas and references appear in the question itself), etc. For OLS regression $\hat\sigma^2$ is the sum of squared residuals (SSR) divided by $n-k$ where $k$ is the rank of the model matrix, so "SSR" provides a good search keyword. $\endgroup$
    – whuber
    Aug 30, 2022 at 15:37


Browse other questions tagged or ask your own question.