 residuals.maxlogL is the maxlogLreg specific method for the generic function residuals which extracts the residuals from a fitted model.

## Usage

# S3 method for maxlogL
residuals(object, parameter = NULL, type = "rqres", routine, ...)

## Arguments

object

an object of maxlogL class obtained by fitting a model with maxlogLreg.

parameter

a character which specifies residuals for a specific parameter.

type

a character with the type of residuals to be computed. The default value is type = "rqres", which is used to compute the normalized randomized quantile residuals.

routine

a character specifying the integration routine. integrate and gauss_quad are available. Custom routines can be defined but they must be compatible with the integration API.

...

further arguments for the integration routine.

## Value

a vector with the specified residuals of a maxlogLreg model.

## Details

For type = "deviance", the residuals are computed using the following expression

$$r^D_i = \mbox{sign}(y_i - \hat{\mu}_i) d_i^{1/2},$$

where $$d_i$$ is the residual deviance of each data point. In this context, $$\hat{\mu}$$ is the estimated mean, computed as the expected value using the estimated parameters of a fitted maxlogLreg model.

On the other hand, for type = "response" the computation is simpler

$$r^R_i = (y_i - \hat{\mu}_i).$$

## Author

Jaime Mosquera Gutiérrez, jmosquerag@unal.edu.co

## Examples

library(EstimationTools)

#----------------------------------------------------------------------------
# Example 1: Test deviance residuals
set.seed(123)
n <- 500
x <- runif(n = n, min = 0, max = 1)
y <- rweibull(n = n, shape = 1, scale = exp(4.5 + 0.5*x))
status <- rep(1, n) # sample(0:1, size = n, replace = TRUE)

distribution <- Surv(y, status) ~ dweibull

formulas <- list(
scale.fo = ~ x
)

fixed <- list(shape = 1)

over = "scale",
)

model <- maxlogLreg(
formulas = formulas,
y_dist = distribution,
fixed = fixed,
)

# Using residuals method
cox_snell_residuals_test <- residuals(model, type = "cox-snell")
martingale_residuals_test <- residuals(model, type = "martingale")
deviance_residuals_test <- residuals(model, type = "right-censored-deviance")

# From scratch
cox_snell_residuals_ref <- -pweibull(
q = y,
shape = 1,
scale = exp(cbind(rep(1, n), x) %*% cbind(coef(model))),
lower.tail = FALSE,
log.p = TRUE
)
martingale_residuals_ref <- status - cox_snell_residuals_ref
deviance_residuals_ref <- sign(martingale_residuals_ref) * (
-2 * (martingale_residuals_ref + status*log(status - martingale_residuals_ref))
)^ 0.5