Learn R Programming

api2lm (version 0.2)

get_residuals: 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")
)

Value

A vector of residals.

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, \ldots, n\), let:

  1. \(Y_i\) denote the response value for the \(i\)th observation.

  2. \(\hat{Y}_i\) denote the fitted value for the \(i\)th observation.

  3. \(h_i\) denote the leverage value for the \(i\)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 ith 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.

Examples

Run this code
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))

Run the code above in your browser using DataLab