fit <- lm(mpg ~ cyl, data = mtcars)
envelope(fit)
# Use pearson residuals, and plot it agains the expected normal quantiles.
env_measures <- envelope(fit,
residual_fn = function(x) residuals.lm(x, type = "pearson"), plot.it = FALSE
)
plot(env_measures, distribution = stats::qnorm, colors = c("gray", "black"))
## Using custom refit_fn
if (require("survival")) {
fit <- survreg(Surv(futime, fustat) ~ ecog.ps + rx, ovarian,
dist = "exponential"
)
fitted_rate <- 1 / fitted(fit)
new_responses <- replicate(100, rexp(length(fitted_rate), fitted_rate), simplify = FALSE)
refit_surv_ovarian <- function(.y) {
survreg(Surv(.y, fustat) ~ ecog.ps + rx, ovarian, dist = "exponential")
}
env_measures <- envelope(fit,
responses = new_responses,
residual_fn = function(x) abs(residuals(x, type = "deviance")),
refit_fn = refit_surv_ovarian, plot.it = FALSE
)
# Absolute residuals are best shown with halfnormal quantiles
plot(env_measures, distribution = function(p) qnorm((1 + p) / 2))
}
Run the code above in your browser using DataLab