I'm stuck learning the hat matrix and wondered if someone could help with a question. If I have the model $$Y_i =\beta_0+\beta_1X_i+\epsilon_i,i = 1,2,3 \dots n,$$ how can I calculate the hat matrix as

$$H = X(X^\prime X)^{-1}X^\prime$$

And what would the $h_{ij}$ element be?

  • $\begingroup$ en.wikipedia.org/wiki/Hat_matrix This looks like it will help. $\endgroup$ Commented Oct 28, 2015 at 20:25
  • $\begingroup$ (1) That minus sign does not belong in the expression for $H$. I guess you intend it to mean an inverse or a pseudo-inverse of the quantity within parentheses. (2) Are you perhaps asking what a matrix is and how to multiply matrices? $\endgroup$
    – whuber
    Commented Oct 28, 2015 at 20:39
  • $\begingroup$ Sorry I have corrected it now. Yes please whatever is relevant to solve the question $\endgroup$ Commented Oct 28, 2015 at 20:42
  • 2
    $\begingroup$ Assuming that your linear model is mod in R you would write something like: X = model.matrix(mod); (H = (X %*% solve(crossprod(X)) %*% t(X))). See this thread here for more details. $\endgroup$
    – usεr11852
    Commented Oct 28, 2015 at 21:42
  • 1
    $\begingroup$ It's unclear what you are asking. You can calculate the hat matrix using the formula that you wrote down. Is there something particular about that formula that you do not understand? $\endgroup$ Commented Oct 29, 2015 at 0:41

1 Answer 1


We can write the model in matrix notation as

$$ Y = \beta X + \epsilon $$

The OLS solution for the vector of regression coefficients $\beta$ is:

$$ \hat{\beta} = (X'X)^{-1} X'Y $$

The hat matrix is the projection matrix that maps the response vector $Y$ to the vector of fitted values $\hat{Y}$ (hence the name "hat" matrix). That is:

$$ \hat{Y} = HY$$

Now, since
$$ \hat{Y} = X \hat{\beta} = X(X'X)^{-1} X'Y $$

it immediately follows that

$$ H = X(X'X)^{-1} X'$$

as required.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.