get_residuals {api2lm} | R Documentation |
Extract residuals from a model
Description
Extracts different types of residuals from a fitted model. The types of residuals are discussed in Details.
Usage
get_residuals(
x,
rtype = c("ordinary", "standardized", "studentized", "jackknife", "loo", "deleted",
"internally studentized", "externally studentized")
)
Arguments
x |
An |
rtype |
The desired residual type. The options are
|
Details
For observations 1, 2, \ldots, n
, let:
-
Y_i
denote the response value for thei
th observation. -
\hat{Y}_i
denote the fitted value for thei
th observation. -
h_i
denote the leverage value for thei
th observation.
We assume that \mathrm{sd}(Y_i) = \sigma
for
i \in \{1, 2, \ldots, n\}
and that \hat{\sigma}
is the estimate produced by sigma(x)
, where x
is the fitted model object.
The ordinary residual for the i
th
observation is computed as
\hat{\epsilon}_i = Y_i - \hat{Y}_i.
The variance of the i
th ordinary residual under standard
assumptions is \sigma^2(1-h_i)
.
The standardized residual for the i
th observation
is computed as
r_i = \frac{\hat{\epsilon}_i}{\hat{\sigma}\sqrt{1-h_i}}.
The standardized residual is also known as the internally studentized residual.
Let \hat{Y}_{i(i)}
denote the predicted value of
Y_i
for the model fit with all n
observations
except observation i
. The leave-one-out (LOO) residual for observation i
is
computed as
l_i = Y_i - \hat{Y}_{i(i)} = \frac{\hat{\epsilon}_i}{1-h_i}.
The LOO residual is also known as the deleted or jackknife residual.
The studentized residual for the i
th observation
is computed as
t_i = \frac{l_i}{\hat{\sigma}_{(i)}\sqrt{1-h_i}},
where \hat{\sigma}_{(i)}
is the leave-one-out estimate
of \sigma
.
The studentized residual is also known as the externally studentized residual.
Value
A vector of residals.
Examples
lmod <- lm(Girth ~ Height, data = trees)
# ordinary residuals
rord <- get_residuals(lmod)
all.equal(rord, residuals(lmod))
# standardized residuals
rstand <- get_residuals(lmod, "standardized")
all.equal(rstand, rstandard(lmod))
# studentized residuals
rstud <- get_residuals(lmod, "studentized")
all.equal(rstud, rstudent(lmod))
# loo residuals
rl <- get_residuals(lmod, "loo")
all.equal(rl, rloo(lmod))