# Learning hat matrix

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?

• en.wikipedia.org/wiki/Hat_matrix This looks like it will help. Oct 28, 2015 at 20:25
• (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?
– whuber
Oct 28, 2015 at 20:39
• Sorry I have corrected it now. Yes please whatever is relevant to solve the question Oct 28, 2015 at 20:42
• 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. Oct 28, 2015 at 21:42
• 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? Oct 29, 2015 at 0:41

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.