| 1 |
context("3-level lme objects") |
| 2 |
set.seed(20190513) |
| 3 |
|
| 4 |
# skip_if_not_installed("lme4") |
| 5 |
skip_if_not_installed("nlme") |
| 6 |
skip_if_not_installed("mlmRev") |
| 7 |
|
| 8 |
# suppressMessages(library(lme4, quietly=TRUE)) |
| 9 |
library(nlme, quietly=TRUE, warn.conflicts=FALSE) |
| 10 |
library(mlmRev, quietly=TRUE, warn.conflicts=FALSE) |
| 11 |
|
| 12 |
school_subset <- levels(egsingle$schoolid) |
| 13 |
school_subset <- sample(school_subset, size = 15) |
| 14 |
egsingle <- droplevels(subset(egsingle, schoolid %in% school_subset)) |
| 15 |
|
| 16 |
|
| 17 |
obj_A1 <- lme(math ~ year * size + female + black + hispanic, |
| 18 |
random = list(~ year | schoolid, ~ 1 | childid), |
| 19 |
data = egsingle, |
| 20 |
control = lmeControl(tolerance = 1e-4, opt = "optim")) |
| 21 |
obj_A2 <- update(obj_A1, weights = varIdent(form = ~ 1 | female)) |
| 22 |
obj_A3 <- update(obj_A1, correlation = corExp(form = ~ year)) |
| 23 |
obj_A4 <- update(obj_A2, correlation = corExp(form = ~ year)) |
| 24 |
objects <- list(A1 = obj_A1, A2 = obj_A2, A3 = obj_A3, A4 = obj_A4) |
| 25 |
|
| 26 |
CR2_mats <- lapply(objects, vcovCR, type = "CR2") |
| 27 |
|
| 28 |
test_that("bread works", { |
| 29 |
bread_checks <- lapply(objects, check_bread, cluster = egsingle$schoolid, y = egsingle$math) |
| 30 |
expect_true(all(unlist(bread_checks))) |
| 31 |
|
| 32 |
obj_vcovs <- lapply(objects, vcov) |
| 33 |
obj_bread <- lapply(objects, function(obj) obj$sigma^2 * sandwich::bread(obj) / v_scale(obj)) |
| 34 |
expect_equal(obj_vcovs, obj_bread) |
| 35 |
}) |
| 36 |
|
| 37 |
|
| 38 |
test_that("vcovCR options work for CR2", { |
| 39 |
skip_on_cran() |
| 40 |
|
| 41 |
expect_equal(vcovCR(obj_A1, cluster = egsingle$schoolid, type = "CR2"), CR2_mats[["A1"]]) |
| 42 |
expect_equal(vcovCR(obj_A1, type = "CR2", inverse_var = TRUE), CR2_mats[["A1"]]) |
| 43 |
expect_false(identical(vcovCR(obj_A1, type = "CR2", inverse_var = FALSE), CR2_mats[["A1"]])) |
| 44 |
target <- targetVariance(obj_A1) |
| 45 |
expect_equal(vcovCR(obj_A1, type = "CR2", target = target, inverse_var = TRUE), CR2_mats[["A1"]]) |
| 46 |
attr(CR2_mats[["A1"]], "inverse_var") <- FALSE |
| 47 |
expect_equal(vcovCR(obj_A1, type = "CR2", target = target, inverse_var = FALSE), CR2_mats[["A1"]]) |
| 48 |
|
| 49 |
expect_equal(vcovCR(obj_A2, cluster = egsingle$schoolid, type = "CR2"), CR2_mats[["A2"]]) |
| 50 |
expect_equal(vcovCR(obj_A2, type = "CR2", inverse_var = TRUE), CR2_mats[["A2"]]) |
| 51 |
expect_false(identical(vcovCR(obj_A2, type = "CR2", inverse_var = FALSE), CR2_mats[["A2"]])) |
| 52 |
target <- targetVariance(obj_A2) |
| 53 |
expect_equal(vcovCR(obj_A2, type = "CR2", target = target, inverse_var = TRUE), CR2_mats[["A2"]]) |
| 54 |
attr(CR2_mats[["A2"]], "inverse_var") <- FALSE |
| 55 |
expect_equal(vcovCR(obj_A2, type = "CR2", target = target, inverse_var = FALSE), CR2_mats[["A2"]]) |
| 56 |
|
| 57 |
expect_equal(vcovCR(obj_A3, cluster = egsingle$schoolid, type = "CR2"), CR2_mats[["A3"]]) |
| 58 |
expect_equal(vcovCR(obj_A3, type = "CR2", inverse_var = TRUE), CR2_mats[["A3"]]) |
| 59 |
expect_false(identical(vcovCR(obj_A3, type = "CR2", inverse_var = FALSE), CR2_mats[["A3"]])) |
| 60 |
target <- targetVariance(obj_A3) |
| 61 |
expect_equal(vcovCR(obj_A3, type = "CR2", target = target, inverse_var = TRUE), CR2_mats[["A3"]]) |
| 62 |
attr(CR2_mats[["A3"]], "inverse_var") <- FALSE |
| 63 |
expect_equal(vcovCR(obj_A3, type = "CR2", target = target, inverse_var = FALSE), CR2_mats[["A3"]]) |
| 64 |
|
| 65 |
expect_equal(vcovCR(obj_A4, cluster = egsingle$schoolid, type = "CR2"), CR2_mats[["A4"]]) |
| 66 |
expect_equal(vcovCR(obj_A4, type = "CR2", inverse_var = TRUE), CR2_mats[["A4"]]) |
| 67 |
expect_false(identical(vcovCR(obj_A4, type = "CR2", inverse_var = FALSE), CR2_mats[["A4"]])) |
| 68 |
target <- targetVariance(obj_A4) |
| 69 |
expect_equal(vcovCR(obj_A4, type = "CR2", target = target, inverse_var = TRUE), CR2_mats[["A4"]]) |
| 70 |
attr(CR2_mats[["A4"]], "inverse_var") <- FALSE |
| 71 |
expect_equal(vcovCR(obj_A4, type = "CR2", target = target, inverse_var = FALSE), CR2_mats[["A4"]]) |
| 72 |
|
| 73 |
}) |
| 74 |
|
| 75 |
test_that("vcovCR options work for CR4", { |
| 76 |
skip_on_cran() |
| 77 |
skip("Not worrying about CR4 for now.") |
| 78 |
CR4_mats <- lapply(objects, vcovCR, type = "CR4") |
| 79 |
|
| 80 |
expect_equal(vcovCR(obj_A1, cluster = egsingle$schoolid, type = "CR4"), CR4_mats[["A1"]]) |
| 81 |
expect_equal(vcovCR(obj_A1, type = "CR4", inverse_var = TRUE), CR4_mats[["A1"]]) |
| 82 |
expect_false(identical(vcovCR(obj_A1, type = "CR4", inverse_var = FALSE), CR4_mats[["A1"]])) |
| 83 |
target <- targetVariance(obj_A1) |
| 84 |
expect_equal(vcovCR(obj_A1, type = "CR4", target = target, inverse_var = TRUE), CR4_mats[["A1"]]) |
| 85 |
attr(CR4_mats[["A1"]], "inverse_var") <- FALSE |
| 86 |
expect_equal(vcovCR(obj_A1, type = "CR4", target = target, inverse_var = FALSE), CR4_mats[["A1"]]) |
| 87 |
|
| 88 |
expect_equal(vcovCR(obj_A2, cluster = egsingle$schoolid, type = "CR4"), CR4_mats[["A2"]]) |
| 89 |
expect_equal(vcovCR(obj_A2, type = "CR4", inverse_var = TRUE), CR4_mats[["A2"]]) |
| 90 |
expect_false(identical(vcovCR(obj_A2, type = "CR4", inverse_var = FALSE), CR4_mats[["A2"]])) |
| 91 |
target <- targetVariance(obj_A2) |
| 92 |
expect_equal(vcovCR(obj_A2, type = "CR4", target = target, inverse_var = TRUE), CR4_mats[["A2"]]) |
| 93 |
attr(CR4_mats[["A2"]], "inverse_var") <- FALSE |
| 94 |
expect_equal(vcovCR(obj_A2, type = "CR4", target = target, inverse_var = FALSE), CR4_mats[["A2"]]) |
| 95 |
|
| 96 |
expect_equal(vcovCR(obj_A3, cluster = egsingle$schoolid, type = "CR4"), CR4_mats[["A3"]]) |
| 97 |
expect_equal(vcovCR(obj_A3, type = "CR4", inverse_var = TRUE), CR4_mats[["A3"]]) |
| 98 |
expect_false(identical(vcovCR(obj_A3, type = "CR4", inverse_var = FALSE), CR4_mats[["A3"]])) |
| 99 |
target <- targetVariance(obj_A3) |
| 100 |
expect_equal(vcovCR(obj_A3, type = "CR4", target = target, inverse_var = TRUE), CR4_mats[["A3"]]) |
| 101 |
attr(CR4_mats[["A3"]], "inverse_var") <- FALSE |
| 102 |
expect_equal(vcovCR(obj_A3, type = "CR4", target = target, inverse_var = FALSE), CR4_mats[["A3"]]) |
| 103 |
|
| 104 |
expect_equal(vcovCR(obj_A4, cluster = egsingle$schoolid, type = "CR4"), CR4_mats[["A4"]]) |
| 105 |
expect_equal(vcovCR(obj_A4, type = "CR4", inverse_var = TRUE), CR4_mats[["A4"]]) |
| 106 |
expect_false(identical(vcovCR(obj_A4, type = "CR4", inverse_var = FALSE), CR4_mats[["A4"]])) |
| 107 |
target <- targetVariance(obj_A4) |
| 108 |
expect_equal(vcovCR(obj_A4, type = "CR4", target = target, inverse_var = TRUE), CR4_mats[["A4"]]) |
| 109 |
attr(CR4_mats[["A4"]], "inverse_var") <- FALSE |
| 110 |
expect_equal(vcovCR(obj_A4, type = "CR4", target = target, inverse_var = FALSE), CR4_mats[["A4"]]) |
| 111 |
|
| 112 |
}) |
| 113 |
|
| 114 |
|
| 115 |
test_that("CR2 is target-unbiased", { |
| 116 |
skip_on_cran() |
| 117 |
CR2_checks <- mapply(check_CR, obj = objects, vcov = CR2_mats) |
| 118 |
expect_true(all(CR2_checks)) |
| 119 |
# CR4_checks <- mapply(check_CR, obj = objects, vcov = CR4_mats) |
| 120 |
# expect_true(all(CR4_checks)) |
| 121 |
}) |
| 122 |
|
| 123 |
|
| 124 |
CR_types <- paste0("CR",0:3) |
| 125 |
|
| 126 |
test_that("Order doesn't matter.", { |
| 127 |
skip_on_cran() |
| 128 |
|
| 129 |
check_sort_order(obj_A1, egsingle) |
| 130 |
check_sort_order(obj_A2, egsingle) |
| 131 |
check_sort_order(obj_A3, egsingle) |
| 132 |
check_sort_order(obj_A4, egsingle) |
| 133 |
|
| 134 |
}) |
| 135 |
|
| 136 |
|
| 137 |
test_that("clubSandwich works with dropped observations", { |
| 138 |
skip_on_cran() |
| 139 |
dat_miss <- egsingle |
| 140 |
dat_miss$math[sample.int(nrow(egsingle), size = round(nrow(egsingle) / 10))] <- NA |
| 141 |
obj_dropped <- update(obj_A4, data = dat_miss, na.action = na.omit) |
| 142 |
obj_complete <- update(obj_A4, data = dat_miss, subset = !is.na(math)) |
| 143 |
|
| 144 |
# obj <- obj_dropped |
| 145 |
# cluster <- nlme::getGroups(obj, level = 1) |
| 146 |
# target <- NULL |
| 147 |
# inverse_var <- TRUE |
| 148 |
# type <- "CR2" |
| 149 |
# form <- "sandwich" |
| 150 |
# |
| 151 |
# full_grps <- get_cor_grouping(obj) |
| 152 |
# R_list <- nlme::corMatrix(obj$modelStruct$corStruct) |
| 153 |
# levels <- names(R_list) |
| 154 |
# grps <- get_cor_grouping(obj, levels = names(R_list)) |
| 155 |
# |
| 156 |
# V_list <- build_var_cor_mats(obj) |
| 157 |
# V_grps <- attr(V_list, "groups") |
| 158 |
# ZDZ_list <- build_RE_mats(obj) |
| 159 |
# ZDZ_grps <- attr(ZDZ_list, "groups") |
| 160 |
# |
| 161 |
# V_dim <- sapply(V_list, nrow) |
| 162 |
# identical(names(V_dim), names(table(V_grps))) |
| 163 |
# data.frame(dim = V_dim, grps = table(V_grps)) |
| 164 |
# table(V_dim == table(V_grps)) |
| 165 |
# dat_miss$x <- NA |
| 166 |
# dat_miss$x[!is.na(dat_miss$math)] <- V_grps |
| 167 |
|
| 168 |
CR_drop <- lapply(CR_types, function(x) vcovCR(obj_dropped, type = x)) |
| 169 |
CR_complete <- lapply(CR_types, function(x) vcovCR(obj_complete, type = x)) |
| 170 |
expect_equal(CR_drop, CR_complete) |
| 171 |
|
| 172 |
test_drop <- lapply(CR_drop, function(x) coef_test(obj_dropped, vcov = x, test = "All", p_values = FALSE)) |
| 173 |
test_complete <- lapply(CR_complete, function(x) coef_test(obj_complete, vcov = x, test = "All", p_values = FALSE)) |
| 174 |
expect_equal(test_drop, test_complete) |
| 175 |
}) |
| 176 |
|
| 177 |
|
| 178 |
test_that("Possible to cluster at higher level than random effects", { |
| 179 |
skip_on_cran() |
| 180 |
|
| 181 |
# fit two-level model |
| 182 |
obj_2level <- lme(math ~ year * size + female + black + hispanic, |
| 183 |
random = ~ year | childid, |
| 184 |
data = egsingle) |
| 185 |
|
| 186 |
# cluster at level 3 |
| 187 |
V <- vcovCR(obj_2level, type = "CR2", cluster = egsingle$schoolid) |
| 188 |
expect_is(V, "vcovCR") |
| 189 |
|
| 190 |
# create 4th level |
| 191 |
n_districts <- nlevels(egsingle$schoolid) / 3 |
| 192 |
districtid <- rep(1:n_districts, each = 3)[egsingle$schoolid] |
| 193 |
|
| 194 |
# cluster at level 4 |
| 195 |
expect_is(vcovCR(obj_2level, type = "CR2", cluster = districtid), "vcovCR") |
| 196 |
expect_is(vcovCR(obj_A1, type = "CR2", cluster = districtid), "vcovCR") |
| 197 |
expect_is(vcovCR(obj_A2, type = "CR2", cluster = districtid), "vcovCR") |
| 198 |
expect_is(vcovCR(obj_A3, type = "CR2", cluster = districtid), "vcovCR") |
| 199 |
expect_is(vcovCR(obj_A4, type = "CR2", cluster = districtid), "vcovCR") |
| 200 |
}) |