The asympDiag
package provides tools for diagnosing
statistical regression models using Monte Carlo simulations. It helps
assess the adequacy of asymptotic approximations for Wald tests and
verify residuals with envelopes. Most of the functions uses generic
methods from the stats
package and are compatible with
models fitted with the functions lm
, glm
,
glm.nb
, lmer
and glmer
. If your
model class is not compatible, you can still generate the diagnostic
plots with the responses
and refit_fn
arguments.
You can install the development version of asympDiag from GitHub with:
# install.packages("devtools")
::install_github("Alvaro-Kothe/asympDiag") devtools
Below is an example using envelope()
to create a
residual diagnostic plot based on Monte Carlo simulations.
library(asympDiag)
# Example data
<- c(18, 17, 15, 20, 10, 20, 25, 13, 12)
counts <- gl(3, 1, 9)
outcome <- gl(3, 3)
treatment
# Fitting a Poisson regression model
<- glm(counts ~ outcome + treatment, family = poisson())
glm.D93
# Creating an envelope plot for residual diagnostics
envelope(glm.D93)
The envelope()
function generates an envelope plot that
compares observed residuals to those expected under the model, helping
to identify potential model misspecifications.
Next, after fitting a valid model, you can use
simulate_wald_pvalues()
to generate p-values through
simulation and check if the sample size is sufficient for the Wald
test’s asymptotic approximation:
# Simulating p-values to assess asymptotic approximation validity
simulate_wald_pvalues(glm.D93, nsim = 10000)
The function simulate_wald_pvalues()
provides a
distribution of p-values from the Monte Carlo simulation, enabling a
deeper assessment of whether the sample size supports reliable inference
based on the Wald test.