Skip to contents

Calculates residuals from the oblr model, such as deviance, Pearson, and others.

Usage

# S3 method for class 'oblr'
residuals(
  object,
  type = c("deviance", "pearson", "raw", "standardized", "studentized_internal",
    "studentized_external", "leverage_adjusted"),
  ...
)

Arguments

object

An object of class oblr.

type

The type of residuals to calculate: "deviance", "pearson", "raw", "standardized", "studentized_internal", "studentized_external", "leverage_adjusted".

...

Additional arguments passed to or from other methods.

Value

A numeric vector of residuals.

Details

The following types of residuals can be calculated:

  • Raw Residuals: The difference between observed and predicted values: $$e_i = y_i - \hat{y}_i$$

  • Deviance Residuals: Deviance residuals measure the contribution of each observation to the model deviance. For logistic regression, it is defined as: $$e_i^{\text{Deviance}} = \text{sign}(y_i - \hat{y}_i) \sqrt{2 \left[ y_i \log\left(\frac{y_i}{\hat{y}_i}\right) + (1 - y_i) \log\left(\frac{1 - y_i}{1 - \hat{y}_i}\right) \right]}$$ where \(\hat{y}_i\) is the predicted probability, and \(y_i\) is the observed value.

  • Pearson Residuals: These residuals scale the raw residuals by the estimated standard deviation: $$e_i^{\text{Pearson}} = \frac{y_i - \hat{y}_i}{\sqrt{\hat{y}_i (1 - \hat{y}_i)}}$$ Pearson residuals are used to assess goodness of fit in generalized linear models.

  • Standardized Residuals: These residuals standardize the raw residuals by dividing by the estimated standard deviation, adjusting for the fitted values: $$e_i^{\text{Standardized}} = \frac{e_i}{\sqrt{\hat{y}_i (1 - \hat{y}_i)}}$$

  • Internally Studentized Residuals: These residuals account for the leverage (influence) of each observation on its own fitted value: $$e_i^{\text{Internally Studentized}} = \frac{e_i}{\sqrt{\hat{y}_i (1 - \hat{y}_i)(1 - h_i)}}$$ where \(h_i\) is the leverage for the \(i\)-th observation, calculated from the hat matrix.

  • Externally Studentized Residuals: These residuals are similar to internally studentized residuals but exclude the \(i\)-th observation when estimating the variance: $$e_i^{\text{Externally Studentized}} = \frac{e_i}{\hat{\sigma}_{(i)} \sqrt{1 - h_i}}$$ where \(\hat{\sigma}_{(i)}\) is the estimated standard error excluding the \(i\)-th observation.

  • Leverage-Adjusted Residuals: These residuals adjust the raw residuals by the leverage value \(h_i\): $$e_i^{\text{Leverage-Adjusted}} = \frac{e_i}{\sqrt{1 - h_i}}$$