Learn R Programming

sjstats (version 0.17.4)

check_assumptions: Check model assumptions

Description

  • outliers() detects outliers in (generalized) linear models.

  • heteroskedastic() checks a linear model for (non-)constant error variance.

  • autocorrelation() checks for independence of errors.

  • normality() checks linear models for (non-)normality of residuals.

  • multicollin() checks predictors of linear models for multicollinearity.

  • check_assumptions() checks all of the above assumptions.

Usage

check_assumptions(x, model.column = NULL, as.logical = FALSE, ...)

outliers(x, iterations = 5)

heteroskedastic(x, model.column = NULL)

autocorrelation(x, model.column = NULL, ...)

normality(x, model.column = NULL)

multicollin(x, model.column = NULL)

Arguments

x

Fitted lm (for outliers(), may also be a glm model), or a (nested) data frame with a list-variable that contains fitted model objects.

model.column

Name or index of the list-variable that contains the fitted model objects. Only applies, if x is a nested data frame (e.g with models fitted to bootstrap replicates).

as.logical

Logical, if TRUE, the values returned by check_assumptions() are TRUE or FALSE, indicating whether each violation of model assumotion holds true or not. If FALSE (the default), the p-value of the respective test-statistics is returned.

...

Other arguments, passed down to durbinWatsonTest.

iterations

Numeric, indicates the number of iterations to remove outliers.

Value

A data frame with the respective statistics.

Details

These functions are wrappers that compute various test statistics, however, each of them returns a tibble instead of a list of values. Furthermore, all functions can also be applied to multiples models in stored in list-variables (see 'Examples').

outliers() wraps outlierTest and iteratively removes outliers for iterations times, or if the r-squared value (for glm: the AIC) did not improve after removing outliers. The function returns a tibble with r-squared and AIC statistics for the original and updated model, as well as the update model itself ($updated.model), the number ($removed.count) and indices of the removed observations ($removed.obs).

heteroskedastic() wraps ncvTest and returns the p-value of the test statistics as tibble. A p-value < 0.05 indicates a non-constant variance (heteroskedasticity).

autocorrelation() wraps durbinWatsonTest and returns the p-value of the test statistics as tibble. A p-value < 0.05 indicates autocorrelated residuals. In such cases, robust standard errors (see robust return more accurate results for the estimates, or maybe a mixed model with error term for the cluster groups should be used.

normality() calls shapiro.test and checks the standardized residuals for normal distribution. The p-value of the test statistics is returned as tibble. A p-value < 0.05 indicates a significant deviation from normal distribution. Note that this formal test almost always yields significant results for the distribution of residuals and visual inspection (e.g. qqplots) are preferable (see plot_model with type = "diag").

multicollin() wraps vif and returns the maximum vif-value from a model as tibble. If this value is larger than about 4, multicollinearity exists, else not. In case of multicollinearity, the names of independent variables that vioalte contribute to multicollinearity are printed to the console.

check_assumptions() runs all of the above tests and returns a tibble with all test statistics included. In case the p-values are too confusing, use the as.logical argument, where all p-values are replaced with either TRUE (in case of violation) or FALSE (in case of model conforms to assumption of linar regression).

Examples

Run this code
# NOT RUN {
data(efc)

fit <- lm(barthtot ~ c160age + c12hour + c161sex + c172code, data = efc)
outliers(fit)
heteroskedastic(fit)
autocorrelation(fit)
normality(fit)
check_assumptions(fit)

fit <- lm(barthtot ~ c160age + c12hour + c161sex + c172code + neg_c_7,
          data = efc)
outliers(fit)
check_assumptions(fit, as.logical = TRUE)

# apply function to multiple models in list-variable
library(purrr)
library(dplyr)
tmp <- efc %>%
  bootstrap(50) %>%
  mutate(
    models = map(strap, ~lm(neg_c_7 ~ e42dep + c12hour + c161sex, data = .x))
  )

# for list-variables, argument 'model.column' is the
# quoted name of the list-variable with fitted models
tmp %>% normality("models")
tmp %>% heteroskedastic("models")

# Durbin-Watson-Test from package 'car' takes a little bit longer due
# to simulation of p-values...
# }
# NOT RUN {
tmp %>% check_assumptions("models", as.logical = TRUE, reps = 100)
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab