## for timing purposes, a small number of bootstrap replicates is used in the
## examples. Run them with a higher or default `nboot` argument for better performance
## RESI on a linear model
# fit linear model
mod = lm(charges ~ region * age + bmi + sex, data = RESI::insurance)
# run resi on fitted model with desired number of bootstrap replicates
# store bootstrap results for calculating different CIs later
resi_obj = resi(mod, nboot = 50, store.boot = TRUE)
# print output
resi_obj
# fit a reduced model for comparison
mod_red = lm(charges ~ bmi, data = RESI::insurance)
# running resi and including the reduced model will provide almost the exact same
# output as not including a reduced model. The difference is that the "overall"
# portion of the output will compare the full model to the reduced model.
# The "summary" and "anova" RESI estimates will be the same. (The bootstrapped
# confidence intervals may differ.)
resi(model.full = mod, model.reduced = mod_red, nboot = 10)
# used stored bootstrap results to get a different alpha-level confidence interval
summary(resi_obj, alpha = c(0.01, 0.1))
car::Anova(resi_obj, alpha = c(0.01, 0.1))
# the result of resi, as well as the summary or Anova of a `resi` object can be plotted
# if the resi object was created with the store.boot = `TRUE` option, any alpha
# can be specified
plot(resi_obj, alpha = 0.01)
# if the variable names on the y-axis are too long, you can reduce their size with
# the ycex.axis argument (or use regular common solutions like changing the margins)
plot(resi_obj, alpha = 0.01, ycex.axis = 0.5)
# for some model types and formula structures, data argument is required
if(requireNamespace("splines")){
# fit logistic regression model with splines
mod = glm(smoker ~ splines::ns(age, df = 3) + region, data = RESI::insurance,
family = "binomial")
# specify additional arguments to the variance-covariance function via vcov.args
resi_obj = resi(mod, data = RESI::insurance, alpha = 0.01,
vcov.args = list(type = "HC0"), nboot = 25)
summary(resi_obj)
car::Anova(resi_obj)}
## RESI on a survival model with alternate Z2S
if(requireNamespace("survival")){
# fit coxph model on example data from survival package
# Note: for survival models, you need to specify robust variance in the model
# creation. resi will ignore the vcovfunc argument for this reason.
mod.coxph = survival::coxph(survival::Surv(time, status) ~ age + sex + wt.loss,
data=survival::lung, robust = TRUE)
# run resi on the model
# to use the alternative Z to RESI formula (which is equal in absolute value to the
# chi-square to RESI (S) formula), specify unbiased = FALSE.
resi(mod.coxph, data = survival::lung, unbiased = FALSE, nboot = 10)}
Run the code above in your browser using DataLab