| 1 |
## ----echo = FALSE, results = "asis", message = FALSE, warning = FALSE--------- |
| 2 |
library(clubSandwich) |
| 3 |
AER_available <- requireNamespace("AER", quietly = TRUE) |
| 4 |
|
| 5 |
knitr::opts_chunk$set(eval = AER_available) |
| 6 |
|
| 7 |
if (!AER_available) cat("# Building this vignette requires the AER package. Please install it. {-}") |
| 8 |
|
| 9 |
## ----message = FALSE, warning = FALSE----------------------------------------- |
| 10 |
library(clubSandwich) |
| 11 |
|
| 12 |
data(STAR, package = "AER") |
| 13 |
|
| 14 |
# clean up a few variables |
| 15 |
levels(STAR$stark)[3] <- "aide" |
| 16 |
levels(STAR$schoolk)[1] <- "urban" |
| 17 |
STAR <- subset(STAR, |
| 18 |
!is.na(schoolidk), |
| 19 |
select = c(schoolidk, schoolk, stark, gender, ethnicity, math1, lunchk)) |
| 20 |
head(STAR) |
| 21 |
|
| 22 |
## ----------------------------------------------------------------------------- |
| 23 |
args(Wald_test) |
| 24 |
|
| 25 |
## ----type-treat--------------------------------------------------------------- |
| 26 |
|
| 27 |
lm_trt <- lm(math1 ~ stark, data = STAR) |
| 28 |
V_trt <- vcovCR(lm_trt, cluster = STAR$schoolidk, type = "CR2") |
| 29 |
coef_test(lm_trt, vcov = V_trt) |
| 30 |
|
| 31 |
|
| 32 |
## ----------------------------------------------------------------------------- |
| 33 |
C_trt <- matrix(c(0,0,1,0,0,1), 2, 3) |
| 34 |
C_trt |
| 35 |
Wald_test(lm_trt, constraints = C_trt, vcov = V_trt) |
| 36 |
|
| 37 |
## ----------------------------------------------------------------------------- |
| 38 |
args(constrain_zero) |
| 39 |
|
| 40 |
## ----------------------------------------------------------------------------- |
| 41 |
constrain_zero(2:3, coefs = coef(lm_trt)) |
| 42 |
|
| 43 |
## ----------------------------------------------------------------------------- |
| 44 |
constrain_zero(c("starksmall","starkaide"), coefs = coef(lm_trt)) |
| 45 |
|
| 46 |
## ----------------------------------------------------------------------------- |
| 47 |
constrain_zero("^stark", coefs = coef(lm_trt), reg_ex = TRUE) |
| 48 |
|
| 49 |
## ----------------------------------------------------------------------------- |
| 50 |
C_trt <- constrain_zero(2:3, coefs = coef(lm_trt)) |
| 51 |
Wald_test(lm_trt, constraints = C_trt, vcov = V_trt) |
| 52 |
|
| 53 |
## ----------------------------------------------------------------------------- |
| 54 |
Wald_test(lm_trt, constraints = constrain_zero(2:3), vcov = V_trt) |
| 55 |
|
| 56 |
## ----type-sep----------------------------------------------------------------- |
| 57 |
|
| 58 |
lm_sep <- lm(math1 ~ 0 + stark, data = STAR) |
| 59 |
V_sep <- vcovCR(lm_sep, cluster = STAR$schoolidk, type = "CR2") |
| 60 |
coef_test(lm_sep, vcov = V_sep) |
| 61 |
|
| 62 |
|
| 63 |
## ----------------------------------------------------------------------------- |
| 64 |
args(constrain_equal) |
| 65 |
|
| 66 |
## ----------------------------------------------------------------------------- |
| 67 |
constrain_equal(1:3, coefs = coef(lm_sep)) |
| 68 |
|
| 69 |
## ----------------------------------------------------------------------------- |
| 70 |
constrain_equal(c("starkregular","starksmall","starkaide"), coefs = coef(lm_sep)) |
| 71 |
|
| 72 |
## ----------------------------------------------------------------------------- |
| 73 |
constrain_equal("^stark", coefs = coef(lm_sep), reg_ex = TRUE) |
| 74 |
|
| 75 |
## ----------------------------------------------------------------------------- |
| 76 |
C_sep <- constrain_equal("^stark", coefs = coef(lm_sep), reg_ex = TRUE) |
| 77 |
Wald_test(lm_sep, constraints = C_sep, vcov = V_sep) |
| 78 |
|
| 79 |
## ----------------------------------------------------------------------------- |
| 80 |
Wald_test(lm_sep, constraints = constrain_equal(1:3), vcov = V_sep) |
| 81 |
|
| 82 |
## ----------------------------------------------------------------------------- |
| 83 |
lm_urbanicity <- lm(math1 ~ schoolk * stark + gender + ethnicity + lunchk, data = STAR) |
| 84 |
V_urbanicity <- vcovCR(lm_urbanicity, cluster = STAR$schoolidk, type = "CR2") |
| 85 |
coef_test(lm_urbanicity, vcov = V_urbanicity) |
| 86 |
|
| 87 |
## ----------------------------------------------------------------------------- |
| 88 |
Wald_test(lm_urbanicity, |
| 89 |
constraints = constrain_zero("schoolk.+:stark", reg_ex = TRUE), |
| 90 |
vcov = V_urbanicity) |
| 91 |
|
| 92 |
## ----------------------------------------------------------------------------- |
| 93 |
Wald_test(lm_urbanicity, |
| 94 |
constraints = constrain_zero("schoolk.+:starksmall", reg_ex = TRUE), |
| 95 |
vcov = V_urbanicity) |
| 96 |
|
| 97 |
## ----------------------------------------------------------------------------- |
| 98 |
C_list <- list( |
| 99 |
`Any interaction` = constrain_zero("schoolk.+:stark", |
| 100 |
coef(lm_urbanicity), reg_ex = TRUE), |
| 101 |
`Small vs regular` = constrain_zero("schoolk.+:starksmall", |
| 102 |
coef(lm_urbanicity), reg_ex = TRUE) |
| 103 |
) |
| 104 |
|
| 105 |
Wald_test(lm_urbanicity, |
| 106 |
constraints = C_list, |
| 107 |
vcov = V_urbanicity) |
| 108 |
|
| 109 |
|
| 110 |
## ----------------------------------------------------------------------------- |
| 111 |
|
| 112 |
Wald_test(lm_urbanicity, |
| 113 |
constraints = C_list, |
| 114 |
vcov = V_urbanicity, |
| 115 |
tidy = TRUE) |
| 116 |
|
| 117 |
|
| 118 |
## ----------------------------------------------------------------------------- |
| 119 |
|
| 120 |
Wald_test( |
| 121 |
lm_urbanicity, |
| 122 |
constraints = list( |
| 123 |
`Any interaction` = constrain_zero("schoolk.+:stark", reg_ex = TRUE), |
| 124 |
`Small vs regular` = constrain_zero("schoolk.+:starksmall", reg_ex = TRUE) |
| 125 |
), |
| 126 |
vcov = V_urbanicity, |
| 127 |
tidy = TRUE |
| 128 |
) |
| 129 |
|
| 130 |
|
| 131 |
## ----------------------------------------------------------------------------- |
| 132 |
coef_test(lm_sep, vcov = V_sep) |
| 133 |
|
| 134 |
## ----------------------------------------------------------------------------- |
| 135 |
C_pairs <- constrain_pairwise(1:3, coefs = coef(lm_sep)) |
| 136 |
C_pairs |
| 137 |
|
| 138 |
## ----------------------------------------------------------------------------- |
| 139 |
Wald_test(lm_sep, constraints = C_pairs, vcov = V_sep, tidy = TRUE) |
| 140 |
|
| 141 |
## ----------------------------------------------------------------------------- |
| 142 |
t_stats <- coef_test(lm_trt, vcov = V_trt)$tstat[2:3] |
| 143 |
F_stats <- Wald_test(lm_sep, constraints = C_pairs, vcov = V_sep, tidy = TRUE)$Fstat[1:2] |
| 144 |
all.equal(t_stats^2, F_stats) |
| 145 |
|
| 146 |
## ----------------------------------------------------------------------------- |
| 147 |
coef_test(lm_urbanicity, vcov = V_urbanicity) |
| 148 |
|
| 149 |
## ----------------------------------------------------------------------------- |
| 150 |
Wald_test(lm_urbanicity, |
| 151 |
constraints = constrain_pairwise(":starksmall", reg_ex = TRUE, with_zero = TRUE), |
| 152 |
vcov = V_urbanicity, |
| 153 |
tidy = TRUE) |
| 154 |
|