| 1 |
context("2-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 |
obj_A <- lme(weight ~ Time * Diet, data=BodyWeight, ~ Time | Rat) |
| 13 |
obj_A2 <- update(obj_A, weights = varPower()) |
| 14 |
obj_A3 <- update(obj_A, correlation = corExp(form = ~ Time)) |
| 15 |
obj_A4 <- update(obj_A2, correlation = corExp(form = ~ Time)) |
| 16 |
obj_B <- lme(distance ~ age, random = ~ age, data = Orthodont) |
| 17 |
|
| 18 |
test_that("bread works", { |
| 19 |
expect_true(check_bread(obj_A, cluster = BodyWeight$Rat, y = BodyWeight$weight)) |
| 20 |
expect_true(check_bread(obj_A2, cluster = BodyWeight$Rat, y = BodyWeight$weight, tol = 5 * 10^-5)) |
| 21 |
expect_true(check_bread(obj_A3, cluster = BodyWeight$Rat, y = BodyWeight$weight)) |
| 22 |
expect_true(check_bread(obj_A4, cluster = BodyWeight$Rat, y = BodyWeight$weight)) |
| 23 |
expect_true(check_bread(obj_B, cluster = Orthodont$Subject, y = Orthodont$distance)) |
| 24 |
|
| 25 |
expect_equal(vcov(obj_A), obj_A$sigma^2 * bread(obj_A) / v_scale(obj_A)) |
| 26 |
expect_equal(vcov(obj_A2), obj_A2$sigma^2 * bread(obj_A2) / v_scale(obj_A2)) |
| 27 |
expect_equal(vcov(obj_A3), obj_A3$sigma^2 * bread(obj_A3) / v_scale(obj_A3)) |
| 28 |
expect_equal(vcov(obj_A4), obj_A4$sigma^2 * bread(obj_A4) / v_scale(obj_A4)) |
| 29 |
expect_equal(vcov(obj_B), obj_B$sigma^2 * bread(obj_B) / v_scale(obj_B)) |
| 30 |
|
| 31 |
}) |
| 32 |
|
| 33 |
test_that("vcovCR options work for CR2", { |
| 34 |
CR2_A <- vcovCR(obj_A, type = "CR2") |
| 35 |
expect_equal(vcovCR(obj_A, cluster = BodyWeight$Rat, type = "CR2"), CR2_A) |
| 36 |
expect_equal(vcovCR(obj_A, type = "CR2", inverse_var = TRUE), CR2_A) |
| 37 |
expect_false(identical(vcovCR(obj_A, type = "CR2", inverse_var = FALSE), CR2_A)) |
| 38 |
|
| 39 |
target <- targetVariance(obj_A) |
| 40 |
expect_equal(vcovCR(obj_A, type = "CR2", target = target, inverse_var = TRUE), CR2_A) |
| 41 |
attr(CR2_A, "inverse_var") <- FALSE |
| 42 |
expect_equal(vcovCR(obj_A, type = "CR2", target = target, inverse_var = FALSE), CR2_A) |
| 43 |
|
| 44 |
CR2_A2 <- vcovCR(obj_A2, type = "CR2") |
| 45 |
expect_equal(vcovCR(obj_A2, cluster = BodyWeight$Rat, type = "CR2"), CR2_A2) |
| 46 |
expect_equal(vcovCR(obj_A2, type = "CR2", inverse_var = TRUE), CR2_A2) |
| 47 |
expect_false(identical(vcovCR(obj_A2, type = "CR2", inverse_var = FALSE), CR2_A2)) |
| 48 |
|
| 49 |
target <- targetVariance(obj_A2) |
| 50 |
expect_equal(vcovCR(obj_A2, type = "CR2", target = target, inverse_var = TRUE), CR2_A2) |
| 51 |
attr(CR2_A2, "inverse_var") <- FALSE |
| 52 |
expect_equal(vcovCR(obj_A2, type = "CR2", target = target, inverse_var = FALSE), CR2_A2) |
| 53 |
|
| 54 |
CR2_A3 <- vcovCR(obj_A3, type = "CR2") |
| 55 |
expect_equal(vcovCR(obj_A3, cluster = BodyWeight$Rat, type = "CR2"), CR2_A3) |
| 56 |
expect_equal(vcovCR(obj_A3, type = "CR2", inverse_var = TRUE), CR2_A3) |
| 57 |
expect_false(identical(vcovCR(obj_A3, type = "CR2", inverse_var = FALSE), CR2_A3)) |
| 58 |
|
| 59 |
target <- targetVariance(obj_A3) |
| 60 |
expect_equal(vcovCR(obj_A3, type = "CR2", target = target, inverse_var = TRUE), CR2_A3) |
| 61 |
attr(CR2_A3, "inverse_var") <- FALSE |
| 62 |
expect_equal(vcovCR(obj_A3, type = "CR2", target = target, inverse_var = FALSE), CR2_A3) |
| 63 |
|
| 64 |
CR2_B <- vcovCR(obj_B, type = "CR2") |
| 65 |
expect_equal(vcovCR(obj_B, cluster = Orthodont$Subject, type = "CR2"), CR2_B) |
| 66 |
expect_equal(vcovCR(obj_B, type = "CR2", inverse_var = TRUE), CR2_B) |
| 67 |
expect_false(identical(vcovCR(obj_B, type = "CR2", inverse_var = FALSE), CR2_B)) |
| 68 |
|
| 69 |
target <- targetVariance(obj_B) |
| 70 |
expect_equal(vcovCR(obj_B, type = "CR2", target = target, inverse_var = TRUE), CR2_B) |
| 71 |
attr(CR2_B, "inverse_var") <- FALSE |
| 72 |
expect_equal(vcovCR(obj_B, type = "CR2", target = target, inverse_var = FALSE), CR2_B) |
| 73 |
}) |
| 74 |
|
| 75 |
test_that("vcovCR options work for CR4", { |
| 76 |
CR4_A <- vcovCR(obj_A, type = "CR4") |
| 77 |
expect_equal(vcovCR(obj_A, cluster = BodyWeight$Rat, type = "CR4"), CR4_A) |
| 78 |
expect_equal(vcovCR(obj_A, type = "CR4", inverse_var = TRUE), CR4_A) |
| 79 |
expect_false(identical(vcovCR(obj_A, type = "CR4", inverse_var = FALSE), CR4_A)) |
| 80 |
|
| 81 |
target <- targetVariance(obj_A) |
| 82 |
expect_equal(vcovCR(obj_A, type = "CR4", target = target, inverse_var = TRUE), CR4_A) |
| 83 |
attr(CR4_A, "inverse_var") <- FALSE |
| 84 |
expect_equal(vcovCR(obj_A, type = "CR4", target = target, inverse_var = FALSE), CR4_A) |
| 85 |
|
| 86 |
CR4_B <- vcovCR(obj_B, type = "CR4") |
| 87 |
expect_equal(vcovCR(obj_B, cluster = Orthodont$Subject, type = "CR4"), CR4_B) |
| 88 |
expect_equal(vcovCR(obj_B, type = "CR4", inverse_var = TRUE), CR4_B) |
| 89 |
expect_false(identical(vcovCR(obj_B, type = "CR4", inverse_var = FALSE), CR4_B)) |
| 90 |
|
| 91 |
target <- targetVariance(obj_B) |
| 92 |
expect_equal(vcovCR(obj_B, type = "CR4", target = target, inverse_var = TRUE), CR4_B) |
| 93 |
attr(CR4_B, "inverse_var") <- FALSE |
| 94 |
expect_equal(vcovCR(obj_B, type = "CR4", target = target, inverse_var = FALSE), CR4_B) |
| 95 |
}) |
| 96 |
|
| 97 |
|
| 98 |
test_that("CR2 and CR4 are target-unbiased", { |
| 99 |
expect_true(check_CR(obj_A, vcov = "CR2")) |
| 100 |
expect_true(check_CR(obj_B, vcov = "CR2")) |
| 101 |
expect_true(check_CR(obj_A, vcov = "CR4")) |
| 102 |
expect_true(check_CR(obj_B, vcov = "CR4")) |
| 103 |
}) |
| 104 |
|
| 105 |
|
| 106 |
CR_types <- paste0("CR",0:4) |
| 107 |
|
| 108 |
test_that("Order doesn't matter.", { |
| 109 |
|
| 110 |
check_sort_order(obj_A, BodyWeight) |
| 111 |
|
| 112 |
}) |
| 113 |
|
| 114 |
|
| 115 |
test_that("clubSandwich works with dropped observations", { |
| 116 |
dat_miss <- BodyWeight |
| 117 |
dat_miss$weight[sample.int(nrow(BodyWeight), size = round(nrow(BodyWeight) / 10))] <- NA |
| 118 |
obj_dropped <- update(obj_A, data = dat_miss, na.action = na.omit) |
| 119 |
obj_complete <- update(obj_A, data = dat_miss, subset = !is.na(weight)) |
| 120 |
|
| 121 |
CR_drop <- lapply(CR_types, function(x) vcovCR(obj_dropped, type = x)) |
| 122 |
CR_complete <- lapply(CR_types, function(x) vcovCR(obj_complete, type = x)) |
| 123 |
expect_equal(CR_drop, CR_complete) |
| 124 |
|
| 125 |
test_drop <- lapply(CR_types, function(x) coef_test(obj_dropped, vcov = x, test = "All", p_values = FALSE)) |
| 126 |
test_complete <- lapply(CR_types, function(x) coef_test(obj_complete, vcov = x, test = "All", p_values = FALSE)) |
| 127 |
expect_equal(test_drop, test_complete) |
| 128 |
}) |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
test_that("lme agrees with gls", { |
| 133 |
|
| 134 |
lme_fit <- lme(weight ~ Time * Diet, data=BodyWeight, ~ 1 | Rat) |
| 135 |
gls_fit <- gls(weight ~ Time * Diet, data=BodyWeight, |
| 136 |
correlation = corCompSymm(form = ~ 1 | Rat)) |
| 137 |
|
| 138 |
CR_lme <- lapply(CR_types, function(x) vcovCR(lme_fit, type = x)) |
| 139 |
CR_gls <- lapply(CR_types, function(x) vcovCR(gls_fit, type = x)) |
| 140 |
# max_ratio <- mapply(function(a, b) max(abs(a / b - 1)), CR_lme, CR_gls) |
| 141 |
# expect_true(all(max_ratio < 10^-4)) |
| 142 |
expect_equivalent(CR_lme, CR_gls, tolerance = 10^-4) |
| 143 |
|
| 144 |
test_lme <- lapply(CR_types, function(x) coef_test(lme_fit, vcov = x, test = "All", p_values = FALSE)) |
| 145 |
test_gls <- lapply(CR_types, function(x) coef_test(gls_fit, vcov = x, test = "All", p_values = FALSE)) |
| 146 |
compare_ttests(test_lme, test_gls) |
| 147 |
|
| 148 |
constraints <- c(combn(length(coef(lme_fit)), 2, simplify = FALSE), |
| 149 |
combn(length(coef(lme_fit)), 3, simplify = FALSE)) |
| 150 |
Wald_lme <- Wald_test(lme_fit, constraints = constrain_zero(constraints), vcov = "CR2", test = "All") |
| 151 |
Wald_gls <- Wald_test(gls_fit, constraints = constrain_zero(constraints), vcov = "CR2", test = "All") |
| 152 |
compare_Waldtests(Wald_lme, Wald_gls) |
| 153 |
}) |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
test_that("Emply levels are dropped in model_matrix", { |
| 158 |
|
| 159 |
data(AchievementAwardsRCT) |
| 160 |
|
| 161 |
AA_RCT_females <- subset(AchievementAwardsRCT, sex=="Girl" & year != "1999") |
| 162 |
|
| 163 |
AA_RCT_females <- within(AA_RCT_females, { |
| 164 |
sibs_4 <- siblings >= 4 |
| 165 |
treated2001 <- treated * (year=="2001") |
| 166 |
}) |
| 167 |
|
| 168 |
lme_fit <- lme(Bagrut_status ~ year * school_type + |
| 169 |
father_ed + mother_ed + immigrant + sibs_4 + |
| 170 |
qrtl + treated2001:half, |
| 171 |
random = ~ 1 | school_id, |
| 172 |
data = AA_RCT_females) |
| 173 |
|
| 174 |
betas <- fixef(lme_fit) |
| 175 |
X <- model_matrix(lme_fit) |
| 176 |
expect_identical(names(betas), colnames(X)) |
| 177 |
|
| 178 |
}) |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
test_that("Possible to cluster at higher level than random effects", { |
| 183 |
|
| 184 |
n_districts <- 10 |
| 185 |
n_schools_per <- rnbinom(n_districts, size = 4, prob = 0.3) |
| 186 |
n_schools <- sum(n_schools_per) |
| 187 |
n_students_per <- 10 |
| 188 |
n_students <- n_schools * n_students_per |
| 189 |
|
| 190 |
# identifiers for each level |
| 191 |
district_id <- factor(rep(1:n_districts, n_schools_per * n_students_per)) |
| 192 |
school_id <- factor(rep(1:sum(n_schools_per), each = n_students_per)) |
| 193 |
student_id <- 1:n_students |
| 194 |
|
| 195 |
# simulated outcome |
| 196 |
Y <- rnorm(n_districts)[district_id] + rnorm(n_schools)[school_id] + rnorm(n_students) |
| 197 |
X <- rnorm(n_students) |
| 198 |
dat <- data.frame(district_id, school_id, student_id, Y, X) |
| 199 |
dat_scramble <- dat[sample(nrow(dat)),] |
| 200 |
|
| 201 |
# fit two-level model |
| 202 |
lme_2level <- lme(Y ~ X, random = ~ 1 | school_id, data = dat) |
| 203 |
|
| 204 |
# cluster at level 3 |
| 205 |
V <- vcovCR(lme_2level, type = "CR2", cluster = dat$district_id) |
| 206 |
expect_is(V, "vcovCR") |
| 207 |
expect_error(vcovCR(lme_2level, type = "CR2", cluster = dat_scramble$district_id)) |
| 208 |
|
| 209 |
# check that result does not depend on sort-order |
| 210 |
V_scramble <- vcovCR(lme(Y ~ X, random = ~ 1 | school_id, data = dat_scramble), |
| 211 |
type = "CR2", cluster = dat_scramble$district_id) |
| 212 |
expect_equal(as.matrix(V), as.matrix(V_scramble)) |
| 213 |
}) |