| Title: | Comprehensive Diagnostics for Statistical Models |
|---|---|
| Description: | Provides a unified framework for diagnosing common issues in statistical models including linear models, generalized linear models (logistic and Poisson regression), and survival models. Implements tests for multicollinearity, heteroscedasticity, autocorrelation, normality, influential observations, overdispersion, zero-inflation, and proportional hazards assumptions. Includes visualization methods for graphical diagnostics. Methods are based on established approaches including Fox and Monette (1992) <doi:10.1080/01621459.1992.10475190>, Breusch and Pagan (1979) <doi:10.2307/1911963>, and Dean and Lawless (1989) <doi:10.1080/01621459.1989.10478792>. |
| Authors: | Emmanuel Adewuyi [aut, cre] (ORCID: <https://orcid.org/0000-0002-7920-6866>), Adewale Lukman [aut] (ORCID: <https://orcid.org/0000-0003-2881-1297>), Abiola Owolabi [aut] (ORCID: <https://orcid.org/0000-0001-9843-5085>) |
| Maintainer: | Emmanuel Adewuyi <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.2 |
| Built: | 2026-06-09 19:25:28 UTC |
| Source: | https://github.com/teniola17/modeldiag |
Performs the Durbin-Watson test for first-order autocorrelation in residuals.
check_autocorrelation(model)check_autocorrelation(model)
model |
A fitted lm object. |
An htest object or NA if computation fails.
model <- lm(mpg ~ wt + hp, data = mtcars) check_autocorrelation(model)model <- lm(mpg ~ wt + hp, data = mtcars) check_autocorrelation(model)
Performs a Box-Tidwell test for linearity of continuous predictors in the logit.
check_box_tidwell(model)check_box_tidwell(model)
model |
A fitted glm object. |
A Box-Tidwell test result or NA if computation fails.
model <- glm(am ~ wt + hp, data = mtcars, family = binomial) check_box_tidwell(model)model <- glm(am ~ wt + hp, data = mtcars, family = binomial) check_box_tidwell(model)
Provides guidance for checking nonlinear functional form in Cox models using martingale residuals.
check_functional_form_coxph(model)check_functional_form_coxph(model)
model |
A fitted coxph object. |
A character message or NA if computation fails.
if (requireNamespace("survival", quietly = TRUE)) { model <- survival::coxph( survival::Surv(time, status) ~ age + sex, data = survival::lung ) check_functional_form_coxph(model) }if (requireNamespace("survival", quietly = TRUE)) { model <- survival::coxph( survival::Surv(time, status) ~ age + sex, data = survival::lung ) check_functional_form_coxph(model) }
Performs Breusch-Pagan test for heteroskedasticity.
check_heteroskedasticity(model)check_heteroskedasticity(model)
model |
A fitted lm object. |
An htest object or NA if computation fails.
model <- lm(mpg ~ wt + hp, data = mtcars) check_heteroskedasticity(model)model <- lm(mpg ~ wt + hp, data = mtcars) check_heteroskedasticity(model)
Performs the Hosmer-Lemeshow goodness-of-fit test for binomial glm models.
check_hosmer_lemeshow(model)check_hosmer_lemeshow(model)
model |
A fitted binomial glm object. |
An htest object or NA if computation fails or the model is not binomial.
model <- glm(am ~ wt + hp, data = mtcars, family = binomial) check_hosmer_lemeshow(model)model <- glm(am ~ wt + hp, data = mtcars, family = binomial) check_hosmer_lemeshow(model)
Identifies influential observations in Cox models using dfbeta residuals.
check_influential_coxph(model)check_influential_coxph(model)
model |
A fitted coxph object. |
A list containing dfbeta residuals and influential point indices, or NA if computation fails.
if (requireNamespace("survival", quietly = TRUE)) { model <- survival::coxph( survival::Surv(time, status) ~ age + sex, data = survival::lung ) check_influential_coxph(model) }if (requireNamespace("survival", quietly = TRUE)) { model <- survival::coxph( survival::Surv(time, status) ~ age + sex, data = survival::lung ) check_influential_coxph(model) }
Identifies influential observations in generalized linear models using Cook's distance.
check_influential_glm(model)check_influential_glm(model)
model |
A fitted glm object. |
A list containing Cook's distances and influential point indices.
model <- glm(am ~ wt + hp, data = mtcars, family = binomial) check_influential_glm(model)model <- glm(am ~ wt + hp, data = mtcars, family = binomial) check_influential_glm(model)
Performs Ramsey's RESET test for linear model misspecification.
check_linearity(model, power = 2)check_linearity(model, power = 2)
model |
A fitted lm object. |
power |
Powers of fitted values to include in the test. |
An htest object with an added note, or NA if computation fails.
model <- lm(mpg ~ wt + hp, data = mtcars) check_linearity(model)model <- lm(mpg ~ wt + hp, data = mtcars) check_linearity(model)
Performs the Shapiro-Wilk test on model residuals.
check_normality(model)check_normality(model)
model |
A fitted model object. |
An htest object or NA if computation fails.
model <- lm(mpg ~ wt + hp, data = mtcars) check_normality(model)model <- lm(mpg ~ wt + hp, data = mtcars) check_normality(model)
Identifies influential observations using Cook's distance.
check_outliers(model, cutoff = 4/length(residuals(model)))check_outliers(model, cutoff = 4/length(residuals(model)))
model |
A fitted model object. |
cutoff |
Cook's distance cutoff. Defaults to 4 divided by the number of observations. |
A list containing Cook's distances and influential point indices.
model <- lm(mpg ~ wt + hp, data = mtcars) check_outliers(model)model <- lm(mpg ~ wt + hp, data = mtcars) check_outliers(model)
Checks whether a Poisson glm appears overdispersed using the residual deviance to degrees-of-freedom ratio.
check_overdispersion(model)check_overdispersion(model)
model |
A fitted Poisson glm object. |
A list with overdispersion diagnostics, or NA if the model is not Poisson or computation fails.
model <- glm(carb ~ wt + hp, data = mtcars, family = poisson) check_overdispersion(model)model <- glm(carb ~ wt + hp, data = mtcars, family = poisson) check_overdispersion(model)
Performs a proportional hazards test for a fitted Cox model using Schoenfeld residuals.
check_proportional_hazards(model)check_proportional_hazards(model)
model |
A fitted coxph object. |
A cox.zph result or NA if computation fails.
if (requireNamespace("survival", quietly = TRUE)) { model <- survival::coxph( survival::Surv(time, status) ~ age + sex, data = survival::lung ) check_proportional_hazards(model) }if (requireNamespace("survival", quietly = TRUE)) { model <- survival::coxph( survival::Surv(time, status) ~ age + sex, data = survival::lung ) check_proportional_hazards(model) }
Summarizes deviance residuals for a generalized linear model.
check_residual_analysis(model)check_residual_analysis(model)
model |
A fitted model object. |
A list of residual summary statistics, or NA if computation fails.
model <- glm(carb ~ wt + hp, data = mtcars, family = poisson) check_residual_analysis(model)model <- glm(carb ~ wt + hp, data = mtcars, family = poisson) check_residual_analysis(model)
Checks for simple indicators of complete or quasi-complete separation.
check_separation(model)check_separation(model)
model |
A fitted glm object. |
A character message describing whether separation was detected, or NA if the model is not a glm.
model <- glm(am ~ wt + hp, data = mtcars, family = binomial) check_separation(model)model <- glm(am ~ wt + hp, data = mtcars, family = binomial) check_separation(model)
Computes variance inflation factors to detect multicollinearity.
check_vif(model)check_vif(model)
model |
A fitted model object. |
A list describing whether VIF computation succeeded. Successful results include a 'vif' element containing the variance inflation factors.
model <- lm(mpg ~ wt + hp + disp, data = mtcars) check_vif(model)model <- lm(mpg ~ wt + hp + disp, data = mtcars) check_vif(model)
Compares observed zeros with the number expected under a fitted Poisson glm.
check_zero_inflation(model)check_zero_inflation(model)
model |
A fitted Poisson glm object. |
A list with zero-inflation diagnostics, or NA if the model is not Poisson or computation fails.
model <- glm(carb ~ wt + hp, data = mtcars, family = poisson) check_zero_inflation(model)model <- glm(carb ~ wt + hp, data = mtcars, family = poisson) check_zero_inflation(model)
This is a generic function for performing diagnostic checks on statistical models. It dispatches to specific methods based on the model type.
## S3 method for class 'glm' diagnose_model(model, ...) ## S3 method for class 'lm' diagnose_model(model, ...) ## S3 method for class 'coxph' diagnose_model(model, ...) diagnose_model(model, ...)## S3 method for class 'glm' diagnose_model(model, ...) ## S3 method for class 'lm' diagnose_model(model, ...) ## S3 method for class 'coxph' diagnose_model(model, ...) diagnose_model(model, ...)
model |
A fitted model object. |
... |
Additional arguments passed to specific methods. |
An object of class "model_diagnostics" containing the results of various diagnostic tests.
# Linear model diagnostics model_lm <- lm(mpg ~ wt + hp, data = mtcars) diag_lm <- diagnose_model(model_lm) summary(diag_lm) plot(diag_lm) # Logistic regression diagnostics model_glm <- glm(am ~ wt + hp, data = mtcars, family = binomial) diag_glm <- diagnose_model(model_glm) summary(diag_glm) # Poisson regression diagnostics model_pois <- glm(carb ~ wt + hp, data = mtcars, family = poisson) diag_pois <- diagnose_model(model_pois) summary(diag_pois) # Cox proportional hazards diagnostics library(survival) data(lung) model_cox <- coxph(Surv(time, status) ~ age + sex + ph.ecog, data = lung) diag_cox <- diagnose_model(model_cox) summary(diag_cox)# Linear model diagnostics model_lm <- lm(mpg ~ wt + hp, data = mtcars) diag_lm <- diagnose_model(model_lm) summary(diag_lm) plot(diag_lm) # Logistic regression diagnostics model_glm <- glm(am ~ wt + hp, data = mtcars, family = binomial) diag_glm <- diagnose_model(model_glm) summary(diag_glm) # Poisson regression diagnostics model_pois <- glm(carb ~ wt + hp, data = mtcars, family = poisson) diag_pois <- diagnose_model(model_pois) summary(diag_pois) # Cox proportional hazards diagnostics library(survival) data(lung) model_cox <- coxph(Surv(time, status) ~ age + sex + ph.ecog, data = lung) diag_cox <- diagnose_model(model_cox) summary(diag_cox)
Generates diagnostic plots for the fitted model.
## S3 method for class 'model_diagnostics' plot(x, ...)## S3 method for class 'model_diagnostics' plot(x, ...)
x |
An object of class "model_diagnostics". |
... |
Additional arguments passed to plotting functions. |
None (plots are displayed).
Prints a summary of the model diagnostics object.
## S3 method for class 'model_diagnostics' print(x, ...)## S3 method for class 'model_diagnostics' print(x, ...)
x |
An object of class "model_diagnostics". |
... |
Additional arguments passed to print. |
The object x, invisibly.
Provides a detailed summary of diagnostic test results.
## S3 method for class 'model_diagnostics' summary(object, ...)## S3 method for class 'model_diagnostics' summary(object, ...)
object |
An object of class "model_diagnostics". |
... |
Additional arguments (currently ignored). |
The object, invisibly.