Skip to contents

residuals() returns response residuals or Pearson-style residuals from a drmTMB fit.

Usage

# S3 method for class 'drmTMB'
residuals(object, type = c("response", "pearson"), ...)

Arguments

object

A drmTMB fit.

type

Residual type: "response" or "pearson".

...

Reserved for future residual options.

Value

A numeric vector for univariate models, or a two-column matrix for bivariate Gaussian models.

Details

For univariate Gaussian models, response residuals are y - mu. Pearson residuals divide by the fitted observation standard deviation. If a dense known sampling covariance was used, Pearson residuals are whitened by the fitted total observation covariance.

For lognormal models, response residuals are y - fitted_mean. Pearson residuals are computed on the log-response scale as (log(y) - mu) / sigma. For Gamma models, response residuals are y - mu and Pearson residuals divide by the fitted Gamma standard deviation mu * sigma, where sigma is the coefficient of variation. For beta-binomial models, response residuals are observed success proportions minus fitted mu, and Pearson residuals divide by the fitted beta-binomial proportion standard deviation. For cumulative-logit ordinal models, response residuals are the observed ordered-category score minus the fitted expected score, and Pearson residuals divide by the fitted category-score standard deviation. For Poisson models, response residuals are y - mu and Pearson residuals divide by sqrt(mu). For zero-inflated Poisson models, response residuals are y - (1 - zi) * mu, and Pearson residuals divide by sqrt((1 - zi) * mu * (1 + zi * mu)). For negative-binomial 2 models, Pearson residuals divide by sqrt(mu + sigma^2 * mu^2). For zero-truncated NB2 models, response residuals are y - mu / (1 - Pr_NB2(0)), and Pearson residuals divide by the conditional positive-count standard deviation. For hurdle NB2 models, response residuals are y - (1 - hu) * mu / (1 - Pr_NB2(0)), and Pearson residuals divide by the unconditional standard deviation implied by the hurdle-zero mixture. For zero-inflated NB2 models, response residuals are y - (1 - zi) * mu, and Pearson residuals divide by the unconditional standard deviation implied by the structural-zero mixture.

For bivariate Gaussian models, response residuals are returned as a two-column matrix. Pearson residuals are standardized and whitened using the fitted residual sigma1, sigma2, and rho12, or using the full row-paired observation covariance when a dense bivariate known V was supplied.

Examples

dat <- data.frame(y = c(0.2, 0.5, 1.1, 1.4), x = c(-1, 0, 1, 2))
fit <- drmTMB(bf(y ~ x, sigma ~ 1), data = dat)
residuals(fit)
#> [1]  0.03000001 -0.09000002  0.08999995 -0.03000008
residuals(fit, type = "pearson")
#> [1]  0.4472135 -1.3416404  1.3416394 -0.4472146