# What is .hat in regression output

The augment() function in the broom package for R creates a dataframe of predicted values from a regression model. Columns created include the fitted values, the standard error of the fit and Cook's distance. They also include something with which I'm not familar and that is the column .hat.

Can anyone explain what this value is, and is it different between linear regression and logistic regression?

library(broom)
data(mtcars)

m1 <- lm(mpg ~ wt, data = mtcars)

.rownames  mpg    wt  .fitted   .se.fit     .resid       .hat   .sigma      .cooksd
1         Mazda RX4 21.0 2.620 23.28261 0.6335798 -2.2826106 0.04326896 3.067494 1.327407e-02
2     Mazda RX4 Wag 21.0 2.875 21.91977 0.5714319 -0.9197704 0.03519677 3.093068 1.723963e-03
3        Datsun 710 22.8 2.320 24.88595 0.7359177 -2.0859521 0.05837573 3.072127 1.543937e-02
4    Hornet 4 Drive 21.4 3.215 20.10265 0.5384424  1.2973499 0.03125017 3.088268 3.020558e-03
5 Hornet Sportabout 18.7 3.440 18.90014 0.5526562 -0.2001440 0.03292182 3.097722 7.599578e-05
6           Valiant 18.1 3.460 18.79325 0.5552829 -0.6932545 0.03323551 3.095184 9.210650e-04
• I do not use that package but I assume it is a value from the diagonal of the so-called hat matrix. Try this Wikipedia page en.wikipedia.org/wiki/Projection_matrix and see if it answers you question. – mdewey Jan 15 '17 at 14:58

Those would be the diagonal elements of the hat-matrix which describe the leverage each point has on its fitted values.

If one fits $\vec{Y} = \mathbf{X} \vec{\beta} + \vec{\epsilon}$ then $\mathbf{H} = \mathbf{X}(\mathbf{X}^T\mathbf{X})^{-1}\mathbf{X}^T$.

In this example:

$$\begin{pmatrix}Y_1\\ \vdots\\ Y_{32}\end{pmatrix} = \begin{pmatrix} 1 & 2.620\\ \vdots\\ 1 & 2.780 \end{pmatrix} \cdot \begin{pmatrix} \beta_0\\ \beta_1 \end{pmatrix} + \begin{pmatrix}\epsilon_1\\ \vdots\\ \epsilon_{32}\end{pmatrix}$$

Then calculating this $\mathbf{H}$ matrix results in:

library(MASS)
wt <- mtcars[,6]

X <- matrix(cbind(rep(1,32),wt), ncol=2)

X%*%ginv(t(X)%*%X)%*%t(X)

Where this last matrix is a $32\times 32$ matrix and contains these hat values on the diagonal.

Hat matrix on Wikpedia

### Fun fact

It is called the hat matrix since it puts the hat on $\vec{Y}$: $$\hat{\vec{Y}} = \mathbf{X}\vec{Y}$$