Page 287 - Applied Statistics with R
P. 287
13.3. UNUSUAL OBSERVATIONS 287
sum(diag(H))
## [1] 3
Alternatively, the method we will use more often, is to simply fit a regression,
then use the hatvalues() function, which returns the leverages.
lev_fit = lm(y ~ ., data = lev_ex)
hatvalues(lev_fit)
## 1 2 3 4 5 6 7 8
## 0.6000 0.3750 0.2875 0.1250 0.4000 0.2125 0.5875 0.4125
Again, note that here we have “used” the values to fit the regression, but R
still ignores them when calculating the leverages, as leverages only depend on
the values.
coef(lev_fit)
## (Intercept) x1 x2
## 3.7 -0.7 4.4
Let’s see what happens to these coefficients when we modify the y value of the
point with the highest leverage.
which.max(hatvalues(lev_fit))
## 1
## 1
lev_ex[which.max(hatvalues(lev_fit)),]
## x1 x2 y
## 1 0 1 11
We see that the original y value is 11. We’ll create a copy of the data, and
modify this point to have a y value of 20.
lev_ex_1 = lev_ex
lev_ex_1$y[1] = 20
lm(y ~ ., data = lev_ex_1)

