Learn R Programming

clubSandwich (version 0.5.11)

linear_contrast: Calculate confidence intervals and p-values for linear contrasts of regression coefficients in a fitted model

Description

linear_contrast reports confidence intervals and (optionally) p-values for linear contrasts of regression coefficients from a fitted model, using a sandwich estimator for the standard errors and (optionally) a small sample correction for the critical values. The default small-sample correction is based on a Satterthwaite approximation.

Usage

linear_contrast(
  obj,
  vcov,
  contrasts,
  level = 0.95,
  test = "Satterthwaite",
  ...,
  p_values = FALSE
)

Value

A data frame containing estimated contrasts, standard errors, confidence intervals, and (optionally) p-values.

Arguments

obj

Fitted model for which to calculate confidence intervals.

vcov

Variance covariance matrix estimated using vcovCR or a character string specifying which small-sample adjustment should be used to calculate the variance-covariance.

contrasts

A contrast matrix, or a list of multiple contrast matrices to test. See details and examples.

level

Desired coverage level for confidence intervals.

test

Character vector specifying which small-sample corrections to calculate. "z" returns a z test (i.e., using a standard normal reference distribution). "naive-t" returns a t test with m - 1 degrees of freedom, where m is the number of unique clusters. "naive-tp" returns a t test with m - p degrees of freedom, where p is the number of regression coefficients in obj. "Satterthwaite" returns a Satterthwaite correction. Unlike in coef_test(), "saddlepoint" is not currently supported in conf_int() because saddlepoint confidence intervals do not have a closed-form solution.

...

Further arguments passed to vcovCR, which are only needed if vcov is a character string.

p_values

Logical indicating whether to report p-values. The default value is FALSE.

Details

Constraints can be specified directly as q X p matrices or indirectly through constrain_pairwise, constrain_equal, or constrain_zero.

See Also

vcovCR

Examples

Run this code

data("ChickWeight", package = "datasets")
lm_fit <- lm(weight ~ 0 + Diet + Time:Diet, data = ChickWeight)

# Pairwise comparisons of diet-by-time slopes
linear_contrast(lm_fit, vcov = "CR2", cluster = ChickWeight$Chick, 
                contrasts = constrain_pairwise("Diet.:Time", reg_ex = TRUE))


if (requireNamespace("carData", quietly = TRUE)) withAutoprint({

  data(Duncan, package = "carData")
  Duncan$cluster <- sample(LETTERS[1:8], size = nrow(Duncan), replace = TRUE)

  Duncan_fit <- lm(prestige ~ 0 + type + income + type:income + type:education, data=Duncan)
  # Note that type:income terms are interactions because main effect of income is included
  # but type:education terms are separate slopes for each unique level of type

  # Pairwise comparisons of type-by-education slopes
  linear_contrast(Duncan_fit, vcov = "CR2", cluster = Duncan$cluster,
                  contrasts = constrain_pairwise(":education", reg_ex = TRUE),
                  test = "Satterthwaite")
 
  # Pairwise comparisons of type-by-income interactions
  linear_contrast(Duncan_fit, vcov = "CR2", cluster = Duncan$cluster,
                  contrasts = constrain_pairwise(":income", reg_ex = TRUE, with_zero = TRUE),
                  test = "Satterthwaite")
                  
})

Run the code above in your browser using DataLab