| check_assumptions {sjstats} | R Documentation |
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.
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)
x |
Fitted |
model.column |
Name or index of the list-variable that contains the fitted
model objects. Only applies, if |
as.logical |
Logical, if |
... |
Other arguments, passed down to |
iterations |
Numeric, indicates the number of iterations to remove outliers. |
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 logical result as tibble. TRUE, if 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).
A tibble with the respective statistics.
These formal tests are very strict and in most cases violation of model
assumptions are alerted, though the model is actually ok. It is
preferable to check model assumptions based on visual inspection
(see plot_model with type = "diag").
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)
## End(Not run)