| 1 |
context("mmrm objects") |
| 2 |
set.seed(20190513) |
| 3 |
|
| 4 |
skip_if_not_installed("mmrm") |
| 5 |
skip_if_not_installed("nlme") |
| 6 |
|
| 7 |
library(mmrm, quietly = TRUE, warn.conflicts = FALSE) |
| 8 |
|
| 9 |
# Dataset 1: fev_data (from mmrm package) |
| 10 |
|
| 11 |
data(fev_data, package = "mmrm") |
| 12 |
|
| 13 |
# Unstructured covariance |
| 14 |
obj_us <- mmrm( |
| 15 |
FEV1 ~ RACE + SEX + ARMCD * AVISIT + us(AVISIT | USUBJID), |
| 16 |
data = fev_data |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
# Toeplitz covariance |
| 21 |
obj_toep <- mmrm( |
| 22 |
FEV1 ~ RACE + SEX + ARMCD * AVISIT + toep(AVISIT | USUBJID), |
| 23 |
data = fev_data |
| 24 |
) |
| 25 |
|
| 26 |
# Grouped covariance (separate covariance per treatment arm) |
| 27 |
obj_grouped <- mmrm( |
| 28 |
FEV1 ~ RACE + SEX + ARMCD * AVISIT + us(AVISIT | ARMCD / USUBJID), |
| 29 |
data = fev_data |
| 30 |
) |
| 31 |
|
| 32 |
# Extract cluster variables for fev_data models |
| 33 |
ff_us <- component(obj_us, "full_frame") |
| 34 |
cluster_us <- droplevels(as.factor(ff_us[[obj_us$formula_parts$subject_var]])) |
| 35 |
|
| 36 |
ff_toep <- component(obj_toep, "full_frame") |
| 37 |
cluster_toep <- droplevels(as.factor(ff_toep[[obj_toep$formula_parts$subject_var]])) |
| 38 |
|
| 39 |
ff_grouped <- component(obj_grouped, "full_frame") |
| 40 |
cluster_grouped <- droplevels(as.factor(ff_grouped[[obj_grouped$formula_parts$subject_var]])) |
| 41 |
|
| 42 |
|
| 43 |
# Dataset 2: BodyWeight (from nlme) — rat growth data |
| 44 |
# 16 rats, 3 diet groups, 11 time points |
| 45 |
|
| 46 |
data(BodyWeight, package = "nlme") |
| 47 |
bw_data <- as.data.frame(BodyWeight) |
| 48 |
bw_data$Time_f <- factor(bw_data$Time) |
| 49 |
|
| 50 |
# Compound symmetry covariance |
| 51 |
obj_bw <- mmrm( |
| 52 |
weight ~ Diet + cs(Time_f | Rat), |
| 53 |
data = bw_data |
| 54 |
) |
| 55 |
|
| 56 |
ff_bw <- component(obj_bw, "full_frame") |
| 57 |
cluster_bw <- droplevels(as.factor(ff_bw[[obj_bw$formula_parts$subject_var]])) |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
# Dataset 3: Orthodont (from nlme) — orthodontic growth data |
| 62 |
# 27 subjects (16 Male, 11 Female), ages 8/10/12/14 |
| 63 |
|
| 64 |
data(Orthodont, package = "nlme") |
| 65 |
orth_data <- as.data.frame(Orthodont) |
| 66 |
orth_data$age_f <- factor(orth_data$age) |
| 67 |
orth_data$Subject <- factor(as.character(orth_data$Subject)) # Orthodont$Subject is an ordered factor |
| 68 |
|
| 69 |
# AR(1) covariance |
| 70 |
obj_orth <- mmrm( |
| 71 |
distance ~ Sex + age_f + ar1(age_f | Subject), |
| 72 |
data = orth_data |
| 73 |
) |
| 74 |
|
| 75 |
ff_orth <- component(obj_orth, "full_frame") |
| 76 |
cluster_orth <- droplevels(as.factor(ff_orth[[obj_orth$formula_parts$subject_var]])) |
| 77 |
|
| 78 |
|
| 79 |
CR_types <- paste0("CR", 0:4) |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
test_that("bread works", { |
| 84 |
# fev_data models |
| 85 |
expect_true(check_bread(obj_us, cluster = cluster_us, y = ff_us$FEV1)) |
| 86 |
expect_true(check_bread(obj_toep, cluster = cluster_toep, y = ff_toep$FEV1)) |
| 87 |
|
| 88 |
# BodyWeight model |
| 89 |
expect_true(check_bread(obj_bw, cluster = cluster_bw, y = ff_bw$weight)) |
| 90 |
|
| 91 |
# Orthodont model |
| 92 |
expect_true(check_bread(obj_orth, cluster = cluster_orth, y = ff_orth$distance)) |
| 93 |
|
| 94 |
# Verify bread formula: vcov(obj) = bread(obj) / v_scale(obj) |
| 95 |
expect_equal(vcov(obj_us), bread(obj_us) / v_scale(obj_us)) |
| 96 |
expect_equal(vcov(obj_toep), bread(obj_toep) / v_scale(obj_toep)) |
| 97 |
expect_equal(vcov(obj_bw), bread(obj_bw) / v_scale(obj_bw)) |
| 98 |
expect_equal(vcov(obj_orth), bread(obj_orth) / v_scale(obj_orth)) |
| 99 |
}) |
| 100 |
|
| 101 |
|
| 102 |
test_that("vcovCR options work for CR2", { |
| 103 |
# fev_data: unstructured |
| 104 |
CR2_us <- vcovCR(obj_us, type = "CR2") |
| 105 |
expect_equal(vcovCR(obj_us, cluster = cluster_us, type = "CR2"), CR2_us) |
| 106 |
expect_equal(vcovCR(obj_us, type = "CR2", inverse_var = TRUE), CR2_us) |
| 107 |
expect_false(identical(vcovCR(obj_us, type = "CR2", inverse_var = FALSE), CR2_us)) |
| 108 |
|
| 109 |
target <- targetVariance(obj_us, cluster_us) |
| 110 |
expect_equal(vcovCR(obj_us, type = "CR2", target = target, inverse_var = TRUE), CR2_us) |
| 111 |
attr(CR2_us, "inverse_var") <- FALSE |
| 112 |
expect_equal(vcovCR(obj_us, type = "CR2", target = target, inverse_var = FALSE), CR2_us) |
| 113 |
|
| 114 |
# BodyWeight: compound symmetry |
| 115 |
CR2_bw <- vcovCR(obj_bw, type = "CR2") |
| 116 |
expect_equal(vcovCR(obj_bw, cluster = cluster_bw, type = "CR2"), CR2_bw) |
| 117 |
expect_equal(vcovCR(obj_bw, type = "CR2", inverse_var = TRUE), CR2_bw) |
| 118 |
expect_false(identical(vcovCR(obj_bw, type = "CR2", inverse_var = FALSE), CR2_bw)) |
| 119 |
|
| 120 |
target_bw <- targetVariance(obj_bw, cluster_bw) |
| 121 |
expect_equal(vcovCR(obj_bw, type = "CR2", target = target_bw, inverse_var = TRUE), CR2_bw) |
| 122 |
attr(CR2_bw, "inverse_var") <- FALSE |
| 123 |
expect_equal(vcovCR(obj_bw, type = "CR2", target = target_bw, inverse_var = FALSE), CR2_bw) |
| 124 |
|
| 125 |
# Orthodont: AR(1) |
| 126 |
CR2_orth <- vcovCR(obj_orth, type = "CR2") |
| 127 |
expect_equal(vcovCR(obj_orth, cluster = cluster_orth, type = "CR2"), CR2_orth) |
| 128 |
expect_equal(vcovCR(obj_orth, type = "CR2", inverse_var = TRUE), CR2_orth) |
| 129 |
expect_false(identical(vcovCR(obj_orth, type = "CR2", inverse_var = FALSE), CR2_orth)) |
| 130 |
|
| 131 |
target_orth <- targetVariance(obj_orth, cluster_orth) |
| 132 |
expect_equal(vcovCR(obj_orth, type = "CR2", target = target_orth, inverse_var = TRUE), CR2_orth) |
| 133 |
attr(CR2_orth, "inverse_var") <- FALSE |
| 134 |
expect_equal(vcovCR(obj_orth, type = "CR2", target = target_orth, inverse_var = FALSE), CR2_orth) |
| 135 |
}) |
| 136 |
|
| 137 |
|
| 138 |
test_that("vcovCR options work for CR4", { |
| 139 |
# fev_data |
| 140 |
CR4_us <- vcovCR(obj_us, type = "CR4") |
| 141 |
expect_equal(vcovCR(obj_us, cluster = cluster_us, type = "CR4"), CR4_us) |
| 142 |
expect_equal(vcovCR(obj_us, type = "CR4", inverse_var = TRUE), CR4_us) |
| 143 |
expect_false(identical(vcovCR(obj_us, type = "CR4", inverse_var = FALSE), CR4_us)) |
| 144 |
|
| 145 |
target <- targetVariance(obj_us, cluster_us) |
| 146 |
expect_equal(vcovCR(obj_us, type = "CR4", target = target, inverse_var = TRUE), CR4_us) |
| 147 |
attr(CR4_us, "inverse_var") <- FALSE |
| 148 |
expect_equal(vcovCR(obj_us, type = "CR4", target = target, inverse_var = FALSE), CR4_us) |
| 149 |
|
| 150 |
# Orthodont |
| 151 |
CR4_orth <- vcovCR(obj_orth, type = "CR4") |
| 152 |
expect_equal(vcovCR(obj_orth, cluster = cluster_orth, type = "CR4"), CR4_orth) |
| 153 |
expect_equal(vcovCR(obj_orth, type = "CR4", inverse_var = TRUE), CR4_orth) |
| 154 |
expect_false(identical(vcovCR(obj_orth, type = "CR4", inverse_var = FALSE), CR4_orth)) |
| 155 |
|
| 156 |
target_orth <- targetVariance(obj_orth, cluster_orth) |
| 157 |
expect_equal(vcovCR(obj_orth, type = "CR4", target = target_orth, inverse_var = TRUE), CR4_orth) |
| 158 |
attr(CR4_orth, "inverse_var") <- FALSE |
| 159 |
expect_equal(vcovCR(obj_orth, type = "CR4", target = target_orth, inverse_var = FALSE), CR4_orth) |
| 160 |
}) |
| 161 |
|
| 162 |
|
| 163 |
test_that("CR2 and CR4 are target-unbiased", { |
| 164 |
# fev_data models |
| 165 |
expect_true(check_CR(obj_us, vcov = "CR2")) |
| 166 |
expect_true(check_CR(obj_us, vcov = "CR4")) |
| 167 |
expect_true(check_CR(obj_toep, vcov = "CR2")) |
| 168 |
expect_true(check_CR(obj_toep, vcov = "CR4")) |
| 169 |
|
| 170 |
# BodyWeight |
| 171 |
expect_true(check_CR(obj_bw, vcov = "CR2")) |
| 172 |
expect_true(check_CR(obj_bw, vcov = "CR4")) |
| 173 |
|
| 174 |
# Orthodont |
| 175 |
expect_true(check_CR(obj_orth, vcov = "CR2")) |
| 176 |
expect_true(check_CR(obj_orth, vcov = "CR4")) |
| 177 |
}) |
| 178 |
|
| 179 |
|
| 180 |
test_that("grouped covariance model works", { |
| 181 |
# bread works for grouped model |
| 182 |
expect_true(check_bread(obj_grouped, cluster = cluster_grouped, y = ff_grouped$FEV1)) |
| 183 |
expect_equal(vcov(obj_grouped), bread(obj_grouped) / v_scale(obj_grouped)) |
| 184 |
|
| 185 |
# CR2 produces a valid vcovCR object |
| 186 |
CR2_grouped <- vcovCR(obj_grouped, type = "CR2") |
| 187 |
expect_s3_class(CR2_grouped, "vcovCR") |
| 188 |
expect_equal(vcovCR(obj_grouped, cluster = cluster_grouped, type = "CR2"), CR2_grouped) |
| 189 |
|
| 190 |
# CR2 and CR4 are target-unbiased for grouped model |
| 191 |
expect_true(check_CR(obj_grouped, vcov = "CR2")) |
| 192 |
expect_true(check_CR(obj_grouped, vcov = "CR4")) |
| 193 |
|
| 194 |
# coef_test works on grouped model |
| 195 |
test_grouped <- coef_test(obj_grouped, vcov = "CR2") |
| 196 |
expect_s3_class(test_grouped, "data.frame") |
| 197 |
expect_true(all(test_grouped$SE > 0)) |
| 198 |
}) |
| 199 |
|
| 200 |
|
| 201 |
test_that("Order doesn't matter", { |
| 202 |
|
| 203 |
# fev_data |
| 204 |
check_sort_order(obj_us, fev_data) |
| 205 |
|
| 206 |
# BodyWeight |
| 207 |
check_sort_order(obj_bw, bw_data) |
| 208 |
|
| 209 |
# Orthodont |
| 210 |
check_sort_order(obj_orth, orth_data) |
| 211 |
}) |
| 212 |
|
| 213 |
|
| 214 |
test_that("different covariance structures all work", { |
| 215 |
|
| 216 |
# us (fev_data) |
| 217 |
CR_us <- lapply(CR_types, function(x) vcovCR(obj_us, type = x)) |
| 218 |
expect_true(all(sapply(CR_us, inherits, "vcovCR"))) |
| 219 |
|
| 220 |
# toep (fev_data) |
| 221 |
CR_toep <- lapply(CR_types, function(x) vcovCR(obj_toep, type = x)) |
| 222 |
expect_true(all(sapply(CR_toep, inherits, "vcovCR"))) |
| 223 |
|
| 224 |
# cs (BodyWeight) |
| 225 |
CR_bw <- lapply(CR_types, function(x) vcovCR(obj_bw, type = x)) |
| 226 |
expect_true(all(sapply(CR_bw, inherits, "vcovCR"))) |
| 227 |
|
| 228 |
# ar1 (Orthodont) |
| 229 |
CR_orth <- lapply(CR_types, function(x) vcovCR(obj_orth, type = x)) |
| 230 |
expect_true(all(sapply(CR_orth, inherits, "vcovCR"))) |
| 231 |
}) |
| 232 |
|
| 233 |
|
| 234 |
test_that("coef_test and conf_int work", { |
| 235 |
# fev_data |
| 236 |
test_us <- coef_test(obj_us, vcov = "CR2", test = "All") |
| 237 |
expect_s3_class(test_us, "data.frame") |
| 238 |
expect_true(all(test_us$SE > 0)) |
| 239 |
expect_true(all(test_us$df_Satt > 0)) |
| 240 |
|
| 241 |
ci_us <- conf_int(obj_us, vcov = "CR2") |
| 242 |
expect_s3_class(ci_us, "data.frame") |
| 243 |
expect_true(all(ci_us$CI_L < ci_us$CI_U)) |
| 244 |
expect_true(all(ci_us$SE > 0)) |
| 245 |
|
| 246 |
# BodyWeight |
| 247 |
test_bw <- coef_test(obj_bw, vcov = "CR2", test = "All") |
| 248 |
expect_s3_class(test_bw, "data.frame") |
| 249 |
expect_true(all(test_bw$SE > 0)) |
| 250 |
|
| 251 |
ci_bw <- conf_int(obj_bw, vcov = "CR2") |
| 252 |
expect_s3_class(ci_bw, "data.frame") |
| 253 |
expect_true(all(ci_bw$CI_L < ci_bw$CI_U)) |
| 254 |
|
| 255 |
# Orthodont |
| 256 |
test_orth <- coef_test(obj_orth, vcov = "CR2", test = "All") |
| 257 |
expect_s3_class(test_orth, "data.frame") |
| 258 |
expect_true(all(test_orth$SE > 0)) |
| 259 |
|
| 260 |
ci_orth <- conf_int(obj_orth, vcov = "CR2") |
| 261 |
expect_s3_class(ci_orth, "data.frame") |
| 262 |
expect_true(all(ci_orth$CI_L < ci_orth$CI_U)) |
| 263 |
|
| 264 |
# Multiple CR types all produce valid coef_test / conf_int results |
| 265 |
for (cr_type in c("CR0", "CR1", "CR2")) { |
| 266 |
test_cr <- coef_test(obj_us, vcov = cr_type) |
| 267 |
expect_s3_class(test_cr, "data.frame") |
| 268 |
ci_cr <- conf_int(obj_us, vcov = cr_type) |
| 269 |
expect_s3_class(ci_cr, "data.frame") |
| 270 |
} |
| 271 |
}) |
| 272 |
|
| 273 |
|
| 274 |
test_that("Wald_test works", { |
| 275 |
# fev_data — test first 3 pairwise contrasts |
| 276 |
coefs_us <- coef_CS(obj_us) |
| 277 |
pairs_us <- utils::combn(length(coefs_us), 2, simplify = FALSE)[1:3] |
| 278 |
constraints_us <- lapply(pairs_us, constrain_zero, coefs = coefs_us) |
| 279 |
|
| 280 |
Wald_us <- Wald_test(obj_us, constraints = constraints_us, vcov = "CR2", test = "All") |
| 281 |
expect_true(is.list(Wald_us)) |
| 282 |
expect_true(all(sapply(Wald_us, function(w) all(w$Fstat >= 0)))) |
| 283 |
|
| 284 |
# BodyWeight — all pairwise (only 3 coefficients, so 3 pairs) |
| 285 |
coefs_bw <- coef_CS(obj_bw) |
| 286 |
pairs_bw <- utils::combn(length(coefs_bw), 2, simplify = FALSE) |
| 287 |
constraints_bw <- lapply(pairs_bw, constrain_zero, coefs = coefs_bw) |
| 288 |
|
| 289 |
Wald_bw <- Wald_test(obj_bw, constraints = constraints_bw, vcov = "CR2", test = "All") |
| 290 |
expect_true(is.list(Wald_bw)) |
| 291 |
expect_true(all(sapply(Wald_bw, function(w) all(w$Fstat >= 0)))) |
| 292 |
|
| 293 |
# Orthodont — first 3 pairwise contrasts |
| 294 |
coefs_orth <- coef_CS(obj_orth) |
| 295 |
pairs_orth <- utils::combn(length(coefs_orth), 2, simplify = FALSE)[1:3] |
| 296 |
constraints_orth <- lapply(pairs_orth, constrain_zero, coefs = coefs_orth) |
| 297 |
|
| 298 |
Wald_orth <- Wald_test(obj_orth, constraints = constraints_orth, vcov = "CR2", test = "All") |
| 299 |
expect_true(is.list(Wald_orth)) |
| 300 |
expect_true(all(sapply(Wald_orth, function(w) all(w$Fstat >= 0)))) |
| 301 |
}) |
| 302 |
|
| 303 |
|
| 304 |
test_that("clubSandwich works with dropped observations", { |
| 305 |
|
| 306 |
dat_miss <- fev_data |
| 307 |
dat_miss$FEV1[sample.int(nrow(fev_data), size = round(nrow(fev_data) / 10))] <- NA |
| 308 |
|
| 309 |
# mmrm auto-drops NA rows (no na.action parameter) |
| 310 |
obj_dropped <- mmrm( |
| 311 |
FEV1 ~ RACE + SEX + ARMCD * AVISIT + us(AVISIT | USUBJID), |
| 312 |
data = dat_miss |
| 313 |
) |
| 314 |
|
| 315 |
# mmrm has no subset parameter — manually subset the data |
| 316 |
dat_complete <- dat_miss[!is.na(dat_miss$FEV1), ] |
| 317 |
obj_complete <- mmrm( |
| 318 |
FEV1 ~ RACE + SEX + ARMCD * AVISIT + us(AVISIT | USUBJID), |
| 319 |
data = dat_complete |
| 320 |
) |
| 321 |
|
| 322 |
CR_drop <- lapply(CR_types, function(x) vcovCR(obj_dropped, type = x)) |
| 323 |
CR_complete <- lapply(CR_types, function(x) vcovCR(obj_complete, type = x)) |
| 324 |
expect_equal(CR_drop, CR_complete) |
| 325 |
|
| 326 |
test_drop <- lapply(CR_types, function(x) coef_test(obj_dropped, vcov = x, test = "All", p_values = FALSE)) |
| 327 |
test_complete <- lapply(CR_types, function(x) coef_test(obj_complete, vcov = x, test = "All", p_values = FALSE)) |
| 328 |
expect_equal(test_drop, test_complete) |
| 329 |
}) |
| 330 |
|
| 331 |
test_that("clubSandwich agrees with mmrm internal calculations.", { |
| 332 |
|
| 333 |
compare_mmrm_vcov(obj_us, tol = 5e-4) |
| 334 |
compare_mmrm_vcov(obj_toep, tol = 5e-4) |
| 335 |
compare_mmrm_vcov(obj_grouped, tol = 5e-4) |
| 336 |
|
| 337 |
compare_mmrm_vcov(obj_bw, tol = 5e-4) |
| 338 |
compare_mmrm_vcov(obj_orth, tol = 7e-4) |
| 339 |
}) |
| 340 |
|