knitr::opts_chunk$set(collapse = TRUE, comment = "#>", dev = "png", fig.width = 7, fig.height = 5, message = FALSE, warning = FALSE)
if (!requireNamespace("sandwich", quietly = TRUE) ||
    !requireNamespace("clubSandwich", quietly = TRUE)) {
  knitr::opts_chunk$set(eval = FALSE)
}

This vignette demonstrate how to compute confidence intervals based on (cluster) robust variance-covariance matrices for standard errors.

First, we load the required packages and create a sample data set with a binomial and continuous variable as predictor as well as a group factor.

library(ggeffects)
set.seed(123)

# example taken from "?clubSandwich::vcovCR"
m <- 8
cluster <- factor(rep(LETTERS[1:m], 3 + rpois(m, 5)))
n <- length(cluster)
X <- matrix(rnorm(3 * n), n, 3)
nu <- rnorm(m)[cluster]
e <- rnorm(n)
y <- X %*% c(0.4, 0.3, -0.3) + nu + e
dat <- data.frame(y, X, cluster, row = 1:n)

# fit linear model
model <- lm(y ~ X1 + X2 + X3, data = dat)

Predictions with normal standard errors

In this example, we use the normal standard errors, as returned by predict(), to compute confidence intervals.

predict_response(model, "X1")
me <- predict_response(model, "X1")
plot(me)

Predictions with HC-estimated standard errors

Now, we use sandwich::vcovHC() to estimate heteroskedasticity-consistent standard errors. To do so, first the function name, vcovHC(), must be supplied to the vcov_fun-argument. sandwich::vcovHC(), in turn, has different types of estimation. This must be specified in vcov_type. As a shortcut, the estimation type can be directly passed to vcov_fun, however, this would only call sandwich::vcovHC().

# shorter: predict_response(model, "X1", vcov_fun = "HC0"). This is equivalent to the following:
predict_response(model, "X1", vcov_fun = "vcovHC", vcov_type = "HC0")
me <- predict_response(model, "X1", vcov_fun = "vcovHC", vcov_type = "HC0")
plot(me)

Passing a function to vcov_fun

Instead of character strings, the vcov_fun argument also accepts a function that returns a variance-covariance matrix. Further arguments that need to be passed to that functions should be provided as list to the vcov_args argument. Thus, we can rewrite the above code-chunk in the following way:

predict_response(
  model,
  "X1",
  vcov_fun = sandwich::vcovHC,
  vcov_args = list(type = "HC0")
)

Predictions with cluster-robust standard errors

The last example shows how to define cluster-robust standard errors. These are based on clubSandwich::vcovCR(). Thus, vcov_fun = "vcovCR" is always required when estimating cluster robust standard errors. clubSandwich::vcovCR() has also different estimation types, which must be specified in vcov_type. Furthermore, clubSandwich::vcovCR() requires the cluster-argument, which must be specified in vcov_args:

predict_response(
  model, "X1", vcov_fun = "vcovCR", vcov_type = "CR0",
  vcov_args = list(cluster = dat$cluster)
)
me <- predict_response(
  model, "X1", vcov_fun = "vcovCR", vcov_type = "CR0",
  vcov_args = list(cluster = dat$cluster)
)
plot(me)


strengejacke/ggeffects documentation built on Sept. 26, 2024, 10:42 p.m.