library(knitr) options(knitr.kable.NA = "") options(digits = 2) knitr::opts_chunk$set(comment = "#>") if (!requireNamespace("poorman", quietly = TRUE) || !requireNamespace("clubSandwich", quietly = TRUE) || !requireNamespace("sandwich", quietly = TRUE) || !requireNamespace("lme4", quietly = TRUE)) { knitr::opts_chunk$set(eval = FALSE) } else { library(parameters) library(poorman) library(clubSandwich) library(lme4) } set.seed(333)
The model_parameters()
function also allows the computation of standard errors, confidence intervals and p-values based on robust covariance matrix estimation from model parameters. Robust estimation is based on the packages sandwich and clubSandwich, so all models supported by either of these packages work with model_parameters()
.
There are two arguments responsible to define the heteroscedasticity-consistent covariance matrix, that should be used for robust estimation: vcov
and vcov_args
.
The vcov
argument defines the variance-covariance matrix used to compute uncertainty estimates. This argument accepts following inputs:
stats::vcov()
)"HC"
, "HC0"
, "HC1"
, "HC2"
, "HC3"
, "HC4"
, "HC4m"
, "HC5"
. See ?sandwich::vcovHC
"CR"
, "CR0"
, "CR1"
, "CR1p"
, "CR1S"
, "CR2"
, "CR3"
. See ?clubSandwich::vcovCR
"BS"
, "xy"
, "residual"
, "wild"
, "mammen"
, "fractional"
, "jackknife"
, "norm"
, "webb"
. See ?sandwich::vcovBS
sandwich
package functions: "HAC"
, "PC"
, "vCL"
, "PL"
.The simplest option, model_parameters(vcov = "HC)
, internally calls sandwich::vcovHC(type = "HC3")
. However, vcov
and vcov_args
can be used for different options to calculate the heteroscedasticity-consistent covariance matrix (see ?standard_error_robust
for further details).
Let us start with a simple example, which uses a heteroskedasticity-consistent covariance matrix estimation with estimation-type "HC3" (i.e. sandwich::vcovHC(type = "HC3")
is called):
data(iris) model <- lm(Petal.Length ~ Sepal.Length * Species + Sepal.Width, data = iris) # model parameters, where SE, CI and p-values are based on robust estimation mp <- model_parameters(model, vcov = "HC") mp # compare standard errors to result from sandwich-package mp$SE unname(sqrt(diag(sandwich::vcovHC(model))))
The specific estimation type can be either passed as string to vcov
, or changed with the type
option that is passed to the vcov_args
argument:
vcov = "HC1"
vcov = "HC", vcov_args = list(type = "HC1")
.If the type
is acceptes by different functions, e.g., both sandwich::vcovHC()
and sandwich::vcovCL()
accepts estimation types HC0 to HC3, we need the "long" form to specify all relevant options. In the next example, we use a clustered covariance matrix estimation with HC1-estimation type.
# change estimation-type mp <- model_parameters(model, vcov = "CL", vcov_args = list(type = "HC2")) mp # compare standard errors to result from sandwich-package mp$SE unname(sqrt(diag(sandwich::vcovCL(model, type = "HC2"))))
Usually, clustered covariance matrix estimation is used when there is a cluster-structure in the data. The variable indicating the cluster-structure can be defined in sandwich::vcovCL()
with the cluster
-argument. As mentioned above, in model_parameters()
, additional arguments that should be passed down to functions from the sandwich package can be specified in vcov_args
:
iris$cluster <- factor(rep(LETTERS[1:8], length.out = nrow(iris))) # change estimation-type, defining additional arguments mp <- model_parameters( model, vcov = "CL", vcov_args = list(type = "HC2", cluster = iris$cluster) ) mp # compare standard errors to result from sandwich-package mp$SE unname(sqrt(diag(sandwich::vcovCL(model, type = "HC2", cluster = iris$cluster))))
Cluster-robust estimation of the variance-covariance matrix can also be achieved using clubSandwich::vcovCR()
. Thus, when vcov = "CR"
, the related function from the clubSandwich package is called. Note that this function requires the specification of the cluster
-argument.
# create fake-cluster-variable, to demonstrate cluster robust standard errors iris$cluster <- factor(rep(LETTERS[1:8], length.out = nrow(iris))) # cluster-robust estimation mp <- model_parameters( model, vcov = "CR", vcov_args = list(type = "CR1", cluster = iris$cluster) ) mp # compare standard errors to result from clubSsandwich-package mp$SE unname(sqrt(diag(clubSandwich::vcovCR(model, type = "CR1", cluster = iris$cluster))))
Finally, robust estimation can be combined with standardization. However, robust covariance matrix estimation only works for standardize = "refit"
.
# model parameters, robust estimation on standardized model model_parameters(model, standardize = "refit", vcov = "HC")
For linear mixed models, that by definition have a clustered ("hierarchical" or multilevel) structure in the data, it is also possible to estimate a cluster-robust covariance matrix. This is possible due to the clubSandwich package, thus we need to define the same arguments as in the above example.
library(lme4) data(iris) set.seed(1234) iris$grp <- as.factor(sample(1:3, nrow(iris), replace = TRUE)) # fit example model model <- lme4::lmer( Sepal.Length ~ Species * Sepal.Width + Petal.Length + (1 | grp), data = iris ) # normal model parameters, like from 'summary()' model_parameters(model) # model parameters, cluster robust estimation for mixed models model_parameters( model, vcov = "CR", vcov_args = list(type = "CR1", cluster = iris$grp) )
Again, robust estimation can be combined with standardization for linear mixed models as well, which in such cases also only works for standardize = "refit"
.
# model parameters, cluster robust estimation on standardized mixed model model_parameters( model, standardize = "refit", vcov = "CR", vcov_args = list(type = "CR1", cluster = iris$grp) )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.