Skip to contents

[Experimental]

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)

links <- list(
  over = "scale",
  fun = "log_link"
)

model <- maxlogLreg(
  formulas = formulas,
  y_dist = distribution,
  fixed = fixed,
  link = links
)
#> Error in eval(predvars, data, env): object 'y' not found

# Using `residuals` method
cox_snell_residuals_test <- residuals(model, type = "cox-snell")
#> Error in eval(expr, envir, enclos): object 'model' not found
martingale_residuals_test <- residuals(model, type = "martingale")
#> Error in eval(expr, envir, enclos): object 'model' not found
deviance_residuals_test <- residuals(model, type = "right-censored-deviance")
#> Error in eval(expr, envir, enclos): object 'model' not found

# 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
)
#> Error in eval(expr, envir, enclos): object 'model' not found
martingale_residuals_ref <- status - cox_snell_residuals_ref
#> Error in eval(expr, envir, enclos): object 'cox_snell_residuals_ref' not found
deviance_residuals_ref <- sign(martingale_residuals_ref) * (
  -2 * (martingale_residuals_ref + status*log(status - martingale_residuals_ref))
)^ 0.5
#> Error in eval(expr, envir, enclos): object 'martingale_residuals_ref' not found


plot(cox_snell_residuals_test, cox_snell_residuals_ref)
#> Error in eval(expr, envir, enclos): object 'cox_snell_residuals_test' not found
plot(martingale_residuals_test, martingale_residuals_ref)
#> Error in eval(expr, envir, enclos): object 'martingale_residuals_test' not found
plot(deviance_residuals_test, deviance_residuals_ref)
#> Error in eval(expr, envir, enclos): object 'deviance_residuals_test' not found


#----------------------------------------------------------------------------