| 1 |
context("mlm objects") |
| 2 |
set.seed(20190513) |
| 3 |
|
| 4 |
n <- nrow(iris) |
| 5 |
lm_fit <- lm(cbind(Sepal.Length, Sepal.Width) ~ Species + Petal.Length, data = iris) |
| 6 |
lm_A_fit <- lm(Sepal.Length ~ Species + Petal.Length, data = iris) |
| 7 |
lm_B_fit <- lm(Sepal.Width ~ Species + Petal.Length, data = iris) |
| 8 |
WLS_fit <- lm(cbind(Sepal.Length, Sepal.Width) ~ Species + Petal.Length, data = iris, weights = Petal.Width) |
| 9 |
CR_types <- paste0("CR",0:4) |
| 10 |
|
| 11 |
test_that("bread works", { |
| 12 |
|
| 13 |
expect_equal(bread.mlm(lm_fit), sandwich:::bread.mlm(lm_fit)) |
| 14 |
|
| 15 |
y <- with(iris, as.vector(rbind(Sepal.Length, Sepal.Width))) |
| 16 |
cluster <- rep(rownames(iris), each = ncol(residuals(lm_fit))) |
| 17 |
expect_true(check_bread(lm_fit, cluster = cluster, y = y)) |
| 18 |
|
| 19 |
expect_true(check_bread(WLS_fit, cluster = cluster, y = y)) |
| 20 |
|
| 21 |
}) |
| 22 |
|
| 23 |
|
| 24 |
test_that("CR2 and CR4 are target-unbiased", { |
| 25 |
expect_true(check_CR(lm_fit, vcov = "CR2")) |
| 26 |
expect_true(check_CR(WLS_fit, vcov = "CR2")) |
| 27 |
expect_true(check_CR(lm_fit, vcov = "CR4")) |
| 28 |
expect_true(check_CR(WLS_fit, vcov = "CR4")) |
| 29 |
}) |
| 30 |
|
| 31 |
test_that("vcovCR is mostly equivalent to vcovHC when clusters are all of size 1", { |
| 32 |
library(sandwich, quietly=TRUE) |
| 33 |
CR_mats <- sapply(c("CR0","CR2","CR3","CR1","CR1p","CR1S"), |
| 34 |
function(t) as.matrix(vcovCR(lm_fit, type = t)), |
| 35 |
simplify = FALSE, USE.NAMES = TRUE) |
| 36 |
HC_mats <- sapply(c("HC0","HC2","HC3","HC1"), |
| 37 |
function(t) vcovHC(lm_fit, type = t), |
| 38 |
simplify = FALSE, USE.NAMES = TRUE) |
| 39 |
|
| 40 |
expect_equal(CR_mats$CR0, HC_mats$HC0) |
| 41 |
expect_equal(CR_mats$CR2, HC_mats$HC2) |
| 42 |
expect_equal(CR_mats$CR3, HC_mats$HC3) |
| 43 |
|
| 44 |
J <- nobs(lm_fit) |
| 45 |
p <- ncol(model.matrix(lm_fit)) |
| 46 |
N <- nrow(model_matrix(lm_fit)) |
| 47 |
expect_equal(CR_mats$CR1 * (J - 1), HC_mats$HC1 * (J - p)) |
| 48 |
expect_equal(CR_mats$CR1p * (J - 2 * p), HC_mats$HC1 * (J - p)) |
| 49 |
expect_equal(CR_mats$CR1S * (J - 1) * (N - 2 * p) / (N - 1), HC_mats$HC1 * (J - p)) |
| 50 |
|
| 51 |
HC_A_mats <- sapply(c("HC0","HC2","HC3"), |
| 52 |
function(t) vcovHC(lm_A_fit, type = t), |
| 53 |
simplify = FALSE, USE.NAMES = TRUE) |
| 54 |
HC_B_mats <- sapply(c("HC0","HC2","HC3"), |
| 55 |
function(t) vcovHC(lm_B_fit, type = t), |
| 56 |
simplify = FALSE, USE.NAMES = TRUE) |
| 57 |
expect_equal(CR_mats$CR0[1:p,1:p], HC_A_mats$HC0, check.attributes = FALSE) |
| 58 |
expect_equal(CR_mats$CR2[1:p,1:p], HC_A_mats$HC2, check.attributes = FALSE) |
| 59 |
expect_equal(CR_mats$CR3[1:p,1:p], HC_A_mats$HC3, check.attributes = FALSE) |
| 60 |
expect_equal(CR_mats$CR0[p + 1:p,p + 1:p], HC_B_mats$HC0, check.attributes = FALSE) |
| 61 |
expect_equal(CR_mats$CR2[p + 1:p,p + 1:p], HC_B_mats$HC2, check.attributes = FALSE) |
| 62 |
expect_equal(CR_mats$CR3[p + 1:p,p + 1:p], HC_B_mats$HC3, check.attributes = FALSE) |
| 63 |
}) |
| 64 |
|
| 65 |
test_that("mlm is equivalent to lm with long data.", { |
| 66 |
iris_long <- reshape(iris, c("Sepal.Length","Sepal.Width"), |
| 67 |
direction = "long", times = "outcome") |
| 68 |
iris_long$outcome <- paste0("Sepal.", iris_long$time) |
| 69 |
|
| 70 |
lm_long <- lm(Sepal ~ 0 + outcome + outcome:Species + outcome:Petal.Length, data = iris_long) |
| 71 |
i <- order(rep(1:2, 4)) |
| 72 |
expect_equal(coef_CS(lm_fit), coef(lm_long)[i], check.attributes = FALSE) |
| 73 |
|
| 74 |
CR_fit <- lapply(CR_types, function(x) as.matrix(vcovCR(lm_fit, type = x))) |
| 75 |
CR_long <- lapply(CR_types, function(x) vcovCR(lm_long, type = x, cluster = iris_long$id)[i,i]) |
| 76 |
expect_equivalent(CR_fit, CR_long) |
| 77 |
|
| 78 |
test_fit <- lapply(CR_types, function(x) coef_test(lm_fit, vcov = x, test = "All", p_values = FALSE)) |
| 79 |
test_long <- lapply(CR_types, function(x) coef_test(lm_long, vcov = x, cluster = iris_long$id, test = "All", p_values = FALSE)[i,]) |
| 80 |
compare_ttests(test_fit, test_long) |
| 81 |
|
| 82 |
CR_fit <- lapply(CR_types, function(x) as.matrix(vcovCR(lm_fit, type = x, cluster = iris$Petal.Length))) |
| 83 |
CR_long <- lapply(CR_types, function(x) vcovCR(lm_long, type = x, cluster = iris_long$Petal.Length)[i,i]) |
| 84 |
expect_equivalent(CR_fit, CR_long) |
| 85 |
|
| 86 |
test_fit <- lapply(CR_types, function(x) coef_test(lm_fit, vcov = x, test = "All", p_values = FALSE)) |
| 87 |
test_long <- lapply(CR_types, function(x) coef_test(lm_long, vcov = x, cluster = iris_long$id, test = "All", p_values = FALSE)[i,]) |
| 88 |
compare_ttests(test_fit, test_long) |
| 89 |
|
| 90 |
}) |
| 91 |
|
| 92 |
test_that("Order doesn't matter.",{ |
| 93 |
|
| 94 |
skip_on_cran() |
| 95 |
check_sort_order(WLS_fit, iris) |
| 96 |
|
| 97 |
}) |
| 98 |
|
| 99 |
test_that("clubSandwich works with dropped covariates", { |
| 100 |
dat_miss <- iris |
| 101 |
dat_miss$Petal.Length[sample.int(n, size = round(n / 10))] <- NA |
| 102 |
lm_dropped <- update(lm_fit, data = dat_miss) |
| 103 |
dat_complete <- subset(dat_miss, !is.na(Petal.Length)) |
| 104 |
lm_complete <- update(lm_fit, data = dat_complete) |
| 105 |
|
| 106 |
CR_drop <- lapply(CR_types, function(x) vcovCR(lm_dropped, type = x)) |
| 107 |
CR_complete <- lapply(CR_types, function(x) vcovCR(lm_complete, type = x)) |
| 108 |
expect_equal(CR_drop, CR_complete) |
| 109 |
|
| 110 |
test_drop <- lapply(CR_types, function(x) coef_test(lm_dropped, vcov = x, test = "All", p_values = FALSE)) |
| 111 |
test_complete <- lapply(CR_types, function(x) coef_test(lm_complete, vcov = x, test = "All", p_values = FALSE)) |
| 112 |
expect_equal(test_drop, test_complete) |
| 113 |
}) |
| 114 |
|
| 115 |
|
| 116 |
test_that("clubSandwich works with dropped outcomes", { |
| 117 |
dat_miss <- iris |
| 118 |
n <- nrow(dat_miss) |
| 119 |
dat_miss$Sepal.Length[sample.int(n, size = round(n / 10))] <- NA |
| 120 |
dat_miss$Sepal.Width[sample.int(n, size = round(n / 10))] <- NA |
| 121 |
lm_dropped <- update(lm_fit, data = dat_miss) |
| 122 |
dat_complete <- subset(dat_miss, !is.na(Sepal.Length) & !is.na(Sepal.Width)) |
| 123 |
lm_complete <- update(lm_fit, data = dat_complete) |
| 124 |
|
| 125 |
CR_drop <- lapply(CR_types, function(x) vcovCR(lm_dropped, type = x)) |
| 126 |
CR_complete <- lapply(CR_types, function(x) vcovCR(lm_complete, type = x)) |
| 127 |
expect_equal(CR_drop, CR_complete) |
| 128 |
|
| 129 |
test_drop <- lapply(CR_types, function(x) coef_test(lm_dropped, vcov = x, test = "All", p_values = FALSE)) |
| 130 |
test_complete <- lapply(CR_types, function(x) coef_test(lm_complete, vcov = x, test = "All", p_values = FALSE)) |
| 131 |
compare_ttests(test_drop, test_complete) |
| 132 |
}) |
| 133 |
|
| 134 |
test_that("clubSandwich works with dropped outcomes, covariates, and weights", { |
| 135 |
dat_miss <- iris |
| 136 |
n <- nrow(dat_miss) |
| 137 |
dat_miss$Sepal.Length[sample.int(n, size = round(n / 5))] <- NA |
| 138 |
dat_miss$Sepal.Width[sample.int(n, size = round(n / 5))] <- NA |
| 139 |
dat_miss$Petal.Length[sample.int(n, size = round(n / 5))] <- NA |
| 140 |
dat_miss$Petal.Width[sample.int(n, size = round(n / 5))] <- NA |
| 141 |
WLS_dropped <- update(WLS_fit, data = dat_miss) |
| 142 |
dat_complete <- subset(dat_miss, |
| 143 |
!is.na(Petal.Length) & !is.na(Petal.Width) & |
| 144 |
!is.na(Sepal.Length) & !is.na(Sepal.Width)) |
| 145 |
WLS_complete <- update(WLS_fit, data = dat_complete) |
| 146 |
|
| 147 |
CR_drop <- lapply(CR_types, function(x) vcovCR(WLS_dropped, type = x)) |
| 148 |
CR_complete <- lapply(CR_types, function(x) vcovCR(WLS_complete, type = x)) |
| 149 |
expect_equal(CR_drop, CR_complete) |
| 150 |
|
| 151 |
test_drop <- lapply(CR_types, function(x) coef_test(WLS_dropped, vcov = x, test = "All", p_values = FALSE)) |
| 152 |
test_complete <- lapply(CR_types, function(x) coef_test(WLS_complete, vcov = x, test = "All", p_values = FALSE)) |
| 153 |
expect_equal(test_drop, test_complete) |
| 154 |
}) |
| 155 |
|
| 156 |
test_that("weight scale doesn't matter", { |
| 157 |
|
| 158 |
lm_fit_w <- update(lm_fit, weights = rep(10, nrow(iris))) |
| 159 |
|
| 160 |
unweighted_fit <- lapply(CR_types, function(x) vcovCR(lm_fit, type = x)) |
| 161 |
weighted_fit <- lapply(CR_types, function(x) vcovCR(lm_fit_w, type = x)) |
| 162 |
|
| 163 |
expect_equal(lapply(unweighted_fit, as.matrix), |
| 164 |
lapply(weighted_fit, as.matrix)) |
| 165 |
|
| 166 |
target <- rep(1 + rpois(nrow(iris), lambda = 8), each = ncol(residuals(lm_fit))) |
| 167 |
unweighted_fit <- lapply(CR_types, function(x) vcovCR(lm_fit, type = x, target = target)) |
| 168 |
weighted_fit <- lapply(CR_types, function(x) vcovCR(lm_fit_w, type = x, target = target * 15)) |
| 169 |
|
| 170 |
expect_equal(lapply(unweighted_fit, as.matrix), |
| 171 |
lapply(weighted_fit, as.matrix)) |
| 172 |
|
| 173 |
}) |
| 174 |
|
| 175 |
test_that("clubSandwich works with weights of zero.", { |
| 176 |
|
| 177 |
data("LifeCycleSavings", package = "datasets") |
| 178 |
n_life <- nrow(LifeCycleSavings) |
| 179 |
|
| 180 |
LifeCycleSavings$cl <- substr(rownames(LifeCycleSavings), 1, 1) |
| 181 |
table(LifeCycleSavings$cl) |
| 182 |
LifeCycleSavings$wt <- rpois(nrow(LifeCycleSavings), lambda = 0.8) |
| 183 |
table(LifeCycleSavings$wt) |
| 184 |
|
| 185 |
lm_full <- lm(cbind(dpi, ddpi) ~ pop15 + pop75 + sr, data = LifeCycleSavings, weights = wt) |
| 186 |
LCS_sub <- subset(LifeCycleSavings, wt > 0) |
| 187 |
lm_sub <- lm(cbind(dpi, ddpi) ~ pop15 + pop75 + sr, data = LCS_sub, weights = wt) |
| 188 |
|
| 189 |
CR_full <- lapply(CR_types, function(x) vcovCR(lm_full, type = x)) |
| 190 |
CR_sub <- lapply(CR_types, function(x) vcovCR(lm_sub, type = x)) |
| 191 |
expect_equal(CR_full, CR_sub, check.attributes = FALSE) |
| 192 |
|
| 193 |
test_full <- lapply(CR_types, function(x) coef_test(lm_full, vcov = x, test = c("z","naive-t","Satterthwaite"), p_values = TRUE)) |
| 194 |
test_sub <- lapply(CR_types, function(x) coef_test(lm_sub, vcov = x, test = c("z","naive-t","Satterthwaite"), p_values = TRUE)) |
| 195 |
expect_equal(test_full, test_sub, check.attributes = FALSE) |
| 196 |
|
| 197 |
CR_full <- lapply(CR_types, function(x) vcovCR(lm_full, cluster = LifeCycleSavings$cl, type = x)) |
| 198 |
CR_sub <- lapply(CR_types, function(x) vcovCR(lm_sub, cluster = LCS_sub$cl, type = x)) |
| 199 |
expect_equal(CR_full, CR_sub, check.attributes = FALSE) |
| 200 |
|
| 201 |
test_full <- lapply(CR_types, function(x) coef_test(lm_full, vcov = x, cluster = LifeCycleSavings$cl, test = c("z","naive-t","Satterthwaite"), p_values = TRUE)) |
| 202 |
test_sub <- lapply(CR_types, function(x) coef_test(lm_sub, vcov = x, cluster = LCS_sub$cl, test = c("z","naive-t","Satterthwaite"), p_values = TRUE)) |
| 203 |
expect_equal(test_full, test_sub, check.attributes = FALSE) |
| 204 |
|
| 205 |
dat_miss <- LifeCycleSavings |
| 206 |
miss_indicator <- sample.int(n_life, size = round(n_life / 5)) |
| 207 |
dat_miss$pop15[miss_indicator] <- NA |
| 208 |
dat_miss$cl[miss_indicator] <- NA |
| 209 |
with(dat_miss, table(wt, is.na(pop15))) |
| 210 |
|
| 211 |
lm_dropped <- lm(cbind(dpi, ddpi) ~ pop15 + pop75 + sr, data = dat_miss, weights = wt) |
| 212 |
dat_complete <- subset(dat_miss, !is.na(pop15)) |
| 213 |
lm_complete <- lm(cbind(dpi, ddpi) ~ pop15 + pop75 + sr, data = dat_complete, weights = wt) |
| 214 |
|
| 215 |
CR_drop <- lapply(CR_types, function(x) vcovCR(lm_dropped, type = x)) |
| 216 |
CR_complete <- lapply(CR_types, function(x) vcovCR(lm_complete, type = x)) |
| 217 |
expect_equal(CR_drop, CR_complete) |
| 218 |
|
| 219 |
test_drop <- lapply(CR_types, function(x) coef_test(lm_dropped, vcov = x, test = "All", p_values = FALSE)) |
| 220 |
test_complete <- lapply(CR_types, function(x) coef_test(lm_complete, vcov = x, test = "All", p_values = FALSE)) |
| 221 |
expect_equal(test_drop, test_complete) |
| 222 |
|
| 223 |
CR_drop <- lapply(CR_types, function(x) vcovCR(lm_dropped, cluster = dat_miss$cl, type = x)) |
| 224 |
CR_complete <- lapply(CR_types, function(x) vcovCR(lm_complete, cluster = dat_complete$cl, type = x)) |
| 225 |
expect_equal(CR_drop, CR_complete) |
| 226 |
|
| 227 |
test_drop <- lapply(CR_types, function(x) coef_test(lm_dropped, vcov = x, cluster = dat_miss$cl, test = "All", p_values = FALSE)) |
| 228 |
test_complete <- lapply(CR_types, function(x) coef_test(lm_complete, vcov = x, cluster = dat_complete$cl, test = "All", p_values = FALSE)) |
| 229 |
expect_equal(test_drop, test_complete) |
| 230 |
|
| 231 |
}) |