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 lm object

rtype

The desired residual type. The options are "ordinary", "standardized", "studentized", "jackknife", "loo", "deleted", "internally studentized", and "externally studentized".

Details

For observations 1,2,,n1, 2, \ldots, n, let:

  1. YiY_i denote the response value for the iith observation.

  2. Y^i\hat{Y}_i denote the fitted value for the iith observation.

  3. hih_i denote the leverage value for the iith observation.

We assume that sd(Yi)=σ\mathrm{sd}(Y_i) = \sigma for i{1,2,,n}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 iith observation is computed as

ϵ^i=YiY^i.\hat{\epsilon}_i = Y_i - \hat{Y}_i.

The variance of the ith ordinary residual under standard assumptions is σ2(1hi)\sigma^2(1-h_i).

The standardized residual for the iith observation is computed as

ri=ϵ^iσ^1hi.r_i = \frac{\hat{\epsilon}_i}{\hat{\sigma}\sqrt{1-h_i}}.

The standardized residual is also known as the internally studentized residual.

Let Y^i(i)\hat{Y}_{i(i)} denote the predicted value of YiY_i for the model fit with all nn observations except observation ii. The leave-one-out (LOO) residual for observation ii is computed as

li=YiY^i(i)=ϵ^i1hi.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 iith observation is computed as

ti=liσ^(i)1hi,t_i = \frac{l_i}{\hat{\sigma}_{(i)}\sqrt{1-h_i}},

where σ^(i)\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))

[Package api2lm version 0.2 Index]