Debian Codegraph @

tests/testthat/test_mmrm_weighted.R

raw
1 context("weighted 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 fev_data$wt <- 1L + rpois(nrow(fev_data), lambda = 3)
14
15 # Unstructured covariance with weights
16 obj_us <- mmrm(
17 FEV1 ~ RACE + SEX + ARMCD + us(AVISIT | USUBJID),
18 data = fev_data,
19 weights = fev_data$wt
20 )
21
22 # Toeplitz covariance with weights
23 obj_toep <- mmrm(
24 FEV1 ~ RACE + SEX + ARMCD * AVISIT + toep(AVISIT | USUBJID),
25 data = fev_data,
26 weights = fev_data$wt
27 )
28
29 # Grouped covariance (separate covariance per treatment arm) with weights
30 obj_grouped <- mmrm(
31 FEV1 ~ RACE + SEX + ARMCD * AVISIT + us(AVISIT | ARMCD / USUBJID),
32 data = fev_data,
33 weights = fev_data$wt
34 )
35
36 # Extract cluster variables for fev_data models
37 ff_us <- component(obj_us, "full_frame")
38 cluster_us <- droplevels(as.factor(ff_us[[obj_us$formula_parts$subject_var]]))
39
40 ff_toep <- component(obj_toep, "full_frame")
41 cluster_toep <- droplevels(as.factor(ff_toep[[obj_toep$formula_parts$subject_var]]))
42
43 ff_grouped <- component(obj_grouped, "full_frame")
44 cluster_grouped <- droplevels(as.factor(ff_grouped[[obj_grouped$formula_parts$subject_var]]))
45
46
47 # Dataset 2: BodyWeight (from nlme) — rat growth data
48 # 16 rats, 3 diet groups, 11 time points
49
50 data(BodyWeight, package = "nlme")
51 bw_data <- as.data.frame(BodyWeight)
52 bw_data$Time_f <- factor(bw_data$Time)
53 bw_data$wt <- 1L + rpois(nrow(bw_data), lambda = 2)
54
55 # Compound symmetry covariance with weights
56 obj_bw <- mmrm(
57 weight ~ Diet + cs(Time_f | Rat),
58 data = bw_data,
59 weights = bw_data$wt
60 )
61
62 ff_bw <- component(obj_bw, "full_frame")
63 cluster_bw <- droplevels(as.factor(ff_bw[[obj_bw$formula_parts$subject_var]]))
64
65
66 # Dataset 3: Orthodont (from nlme) — orthodontic growth data
67 # 27 subjects (16 Male, 11 Female), ages 8/10/12/14
68
69 data(Orthodont, package = "nlme")
70 orth_data <- as.data.frame(Orthodont)
71 orth_data$age_f <- factor(orth_data$age)
72 orth_data$Subject <- factor(as.character(orth_data$Subject))
73 orth_data$wt <- 1L + rpois(nrow(orth_data), lambda = 2)
74
75 # AR(1) covariance with weights
76 obj_orth <- mmrm(
77 distance ~ Sex + age_f + ar1(age_f | Subject),
78 data = orth_data,
79 weights = orth_data$wt
80 )
81
82 ff_orth <- component(obj_orth, "full_frame")
83 cluster_orth <- droplevels(as.factor(ff_orth[[obj_orth$formula_parts$subject_var]]))
84
85
86 ### Tests
87
88 test_that("bread works for weighted models", {
89 # fev_data weighted models
90 expect_true(check_bread(obj_us, cluster = cluster_us, y = ff_us$FEV1))
91 expect_true(check_bread(obj_toep, cluster = cluster_toep, y = ff_toep$FEV1))
92
93 # BodyWeight weighted model
94 expect_true(check_bread(obj_bw, cluster = cluster_bw, y = ff_bw$weight))
95
96 # Orthodont weighted model
97 expect_true(check_bread(obj_orth, cluster = cluster_orth, y = ff_orth$distance))
98
99 # Verify bread formula: vcov(obj) = bread(obj) / v_scale(obj)
100 expect_equal(vcov(obj_us), bread(obj_us) / v_scale(obj_us))
101 expect_equal(vcov(obj_toep), bread(obj_toep) / v_scale(obj_toep))
102 expect_equal(vcov(obj_bw), bread(obj_bw) / v_scale(obj_bw))
103 expect_equal(vcov(obj_orth), bread(obj_orth) / v_scale(obj_orth))
104 })
105
106
107 test_that("targetVariance incorporates weights correctly", {
108 # For the weighted model, targetVariance should differ from the unweighted
109 # VarCorr by the weight scaling factor G_i^{-1/2} Sigma G_i^{-1/2}
110
111 tv_us <- targetVariance(obj_us, cluster_us)
112 vc_us <- VarCorr(obj_us)
113 wts_us <- ff_us[["(weights)"]]
114 visits_us <- ff_us[[obj_us$formula_parts$visit_var]]
115 visit_levels_us <- levels(visits_us)
116
117 obs_by_subject_us <- split(seq_along(cluster_us), cluster_us)
118
119 # Check each subject's target variance matches manual weight computation
120 for (subj in names(obs_by_subject_us)) {
121 rows <- obs_by_subject_us[[subj]]
122 idx <- match(as.character(visits_us[rows]), visit_levels_us)
123 Sigma_subj <- unname(vc_us[idx, idx, drop = FALSE])
124 w_inv_sqrt <- 1 / sqrt(wts_us[rows])
125 expected <- tcrossprod(w_inv_sqrt) * Sigma_subj
126 expect_equal(tv_us[[subj]], expected, tolerance = 1e-10)
127 }
128
129 # For an unweighted model, targetVariance should equal VarCorr subsets directly
130 obj_bw_unwt <- mmrm(
131 weight ~ Diet + cs(Time_f | Rat),
132 data = bw_data
133 )
134 ff_bw_unwt <- component(obj_bw_unwt, "full_frame")
135 cluster_bw_unwt <- droplevels(as.factor(ff_bw_unwt[[obj_bw_unwt$formula_parts$subject_var]]))
136
137 tv_bw <- targetVariance(obj_bw_unwt, cluster_bw_unwt)
138 vc_bw <- VarCorr(obj_bw_unwt)
139 visits_bw <- ff_bw_unwt[[obj_bw_unwt$formula_parts$visit_var]]
140 visit_levels_bw <- levels(visits_bw)
141 obs_by_subject_bw <- split(seq_along(cluster_bw_unwt), cluster_bw_unwt)
142
143 for (subj in names(obs_by_subject_bw)) {
144 rows <- obs_by_subject_bw[[subj]]
145 idx <- match(as.character(visits_bw[rows]), visit_levels_bw)
146 expected <- unname(vc_bw[idx, idx, drop = FALSE])
147 expect_equal(tv_bw[[subj]], expected, tolerance = 1e-10)
148 }
149 })
150
151
152 test_that("vcovCR options work for CR2 with weighted models", {
153 # fev_data: weighted unstructured
154 CR2_us <- vcovCR(obj_us, type = "CR2")
155 expect_equal(vcovCR(obj_us, cluster = cluster_us, type = "CR2"), CR2_us)
156 expect_equal(vcovCR(obj_us, type = "CR2", inverse_var = TRUE), CR2_us)
157 expect_false(identical(vcovCR(obj_us, type = "CR2", inverse_var = FALSE), CR2_us))
158
159 target <- targetVariance(obj_us, cluster_us)
160 expect_equal(vcovCR(obj_us, type = "CR2", target = target, inverse_var = TRUE), CR2_us)
161 attr(CR2_us, "inverse_var") <- FALSE
162 expect_equal(vcovCR(obj_us, type = "CR2", target = target, inverse_var = FALSE), CR2_us)
163
164 # BodyWeight weighted
165 CR2_bw <- vcovCR(obj_bw, type = "CR2")
166 expect_equal(vcovCR(obj_bw, cluster = cluster_bw, type = "CR2"), CR2_bw)
167 expect_equal(vcovCR(obj_bw, type = "CR2", inverse_var = TRUE), CR2_bw)
168 expect_false(identical(vcovCR(obj_bw, type = "CR2", inverse_var = FALSE), CR2_bw))
169
170 target_bw <- targetVariance(obj_bw, cluster_bw)
171 expect_equal(vcovCR(obj_bw, type = "CR2", target = target_bw, inverse_var = TRUE), CR2_bw)
172 attr(CR2_bw, "inverse_var") <- FALSE
173 expect_equal(vcovCR(obj_bw, type = "CR2", target = target_bw, inverse_var = FALSE), CR2_bw)
174
175 # Orthodont weighted
176 CR2_orth <- vcovCR(obj_orth, type = "CR2")
177 expect_equal(vcovCR(obj_orth, cluster = cluster_orth, type = "CR2"), CR2_orth)
178 expect_equal(vcovCR(obj_orth, type = "CR2", inverse_var = TRUE), CR2_orth)
179 expect_false(identical(vcovCR(obj_orth, type = "CR2", inverse_var = FALSE), CR2_orth))
180
181 target_orth <- targetVariance(obj_orth, cluster_orth)
182 expect_equal(vcovCR(obj_orth, type = "CR2", target = target_orth, inverse_var = TRUE), CR2_orth)
183 attr(CR2_orth, "inverse_var") <- FALSE
184 expect_equal(vcovCR(obj_orth, type = "CR2", target = target_orth, inverse_var = FALSE), CR2_orth)
185 })
186
187
188 test_that("vcovCR options work for CR4 with weighted models", {
189 # fev_data weighted
190 CR4_us <- vcovCR(obj_us, type = "CR4")
191 expect_equal(vcovCR(obj_us, cluster = cluster_us, type = "CR4"), CR4_us)
192 expect_equal(vcovCR(obj_us, type = "CR4", inverse_var = TRUE), CR4_us)
193 expect_false(identical(vcovCR(obj_us, type = "CR4", inverse_var = FALSE), CR4_us))
194
195 target <- targetVariance(obj_us, cluster_us)
196 expect_equal(vcovCR(obj_us, type = "CR4", target = target, inverse_var = TRUE), CR4_us)
197 attr(CR4_us, "inverse_var") <- FALSE
198 expect_equal(vcovCR(obj_us, type = "CR4", target = target, inverse_var = FALSE), CR4_us)
199
200 # BodyWeight weighted
201 CR4_bw <- vcovCR(obj_bw, type = "CR4")
202 expect_equal(vcovCR(obj_bw, cluster = cluster_bw, type = "CR4"), CR4_bw)
203 expect_equal(vcovCR(obj_bw, type = "CR4", inverse_var = TRUE), CR4_bw)
204 expect_false(identical(vcovCR(obj_bw, type = "CR4", inverse_var = FALSE), CR4_bw))
205
206 target_bw <- targetVariance(obj_bw, cluster_bw)
207 expect_equal(vcovCR(obj_bw, type = "CR4", target = target_bw, inverse_var = TRUE), CR4_bw)
208 attr(CR4_bw, "inverse_var") <- FALSE
209 expect_equal(vcovCR(obj_bw, type = "CR4", target = target_bw, inverse_var = FALSE), CR4_bw)
210
211 # Orthodont weighted
212 CR4_orth <- vcovCR(obj_orth, type = "CR4")
213 expect_equal(vcovCR(obj_orth, cluster = cluster_orth, type = "CR4"), CR4_orth)
214 expect_equal(vcovCR(obj_orth, type = "CR4", inverse_var = TRUE), CR4_orth)
215 expect_false(identical(vcovCR(obj_orth, type = "CR4", inverse_var = FALSE), CR4_orth))
216
217 target_orth <- targetVariance(obj_orth, cluster_orth)
218 expect_equal(vcovCR(obj_orth, type = "CR4", target = target_orth, inverse_var = TRUE), CR4_orth)
219 attr(CR4_orth, "inverse_var") <- FALSE
220 expect_equal(vcovCR(obj_orth, type = "CR4", target = target_orth, inverse_var = FALSE), CR4_orth)
221 })
222
223
224 test_that("CR2 and CR4 are target-unbiased for weighted models", {
225 # fev_data weighted
226 expect_true(check_CR(obj_us, vcov = "CR2"))
227 expect_true(check_CR(obj_us, vcov = "CR4"))
228 expect_true(check_CR(obj_toep, vcov = "CR2"))
229 expect_true(check_CR(obj_toep, vcov = "CR4"))
230
231 # BodyWeight weighted
232 expect_true(check_CR(obj_bw, vcov = "CR2"))
233 expect_true(check_CR(obj_bw, vcov = "CR4"))
234
235 # Orthodont weighted
236 expect_true(check_CR(obj_orth, vcov = "CR2"))
237 expect_true(check_CR(obj_orth, vcov = "CR4"))
238 })
239
240
241 test_that("grouped covariance model works for weighted models", {
242 # bread works for weighted grouped model
243 expect_true(check_bread(obj_grouped, cluster = cluster_grouped, y = ff_grouped$FEV1))
244 expect_equal(vcov(obj_grouped), bread(obj_grouped) / v_scale(obj_grouped))
245
246 # CR2 produces a valid vcovCR object
247 CR2_grouped <- vcovCR(obj_grouped, type = "CR2")
248 expect_s3_class(CR2_grouped, "vcovCR")
249 expect_equal(vcovCR(obj_grouped, cluster = cluster_grouped, type = "CR2"), CR2_grouped)
250
251 # CR2 and CR4 are target-unbiased for weighted grouped model
252 expect_true(check_CR(obj_grouped, vcov = "CR2"))
253 expect_true(check_CR(obj_grouped, vcov = "CR4"))
254
255 # coef_test works on weighted grouped model
256 test_grouped <- coef_test(obj_grouped, vcov = "CR2")
257 expect_s3_class(test_grouped, "data.frame")
258 expect_true(all(test_grouped$SE > 0))
259 })
260
261
262 test_that("Order doesn't matter for weighted models", {
263 # Manual sort-order check for weighted mmrm models.
264 # check_sort_order uses update(obj, data = ...) which replays the original
265 # weights= argument literally, so we refit manually with scrambled data
266
267 # fev_data weighted
268 re_order <- sample(nrow(fev_data))
269 fev_scramble <- fev_data[re_order, ]
270 obj_us_scramble <- mmrm(
271 FEV1 ~ RACE + SEX + ARMCD + us(AVISIT | USUBJID),
272 data = fev_scramble,
273 weights = fev_scramble$wt
274 )
275 CR2_orig <- vcovCR(obj_us, type = "CR2")
276 CR2_scramble <- vcovCR(obj_us_scramble, type = "CR2")
277 expect_equivalent(as.matrix(CR2_orig), as.matrix(CR2_scramble), tolerance = 1e-6)
278
279 # BodyWeight weighted
280 re_order_bw <- sample(nrow(bw_data))
281 bw_scramble <- bw_data[re_order_bw, ]
282 obj_bw_scramble <- mmrm(
283 weight ~ Diet + cs(Time_f | Rat),
284 data = bw_scramble,
285 weights = bw_scramble$wt
286 )
287 CR2_bw_orig <- vcovCR(obj_bw, type = "CR2")
288 CR2_bw_scramble <- vcovCR(obj_bw_scramble, type = "CR2")
289 expect_equivalent(as.matrix(CR2_bw_orig), as.matrix(CR2_bw_scramble), tolerance = 1e-6)
290
291 # Orthodont weighted
292 re_order_orth <- sample(nrow(orth_data))
293 orth_scramble <- orth_data[re_order_orth, ]
294 obj_orth_scramble <- mmrm(
295 distance ~ Sex + age_f + ar1(age_f | Subject),
296 data = orth_scramble,
297 weights = orth_scramble$wt
298 )
299 CR2_orth_orig <- vcovCR(obj_orth, type = "CR2")
300 CR2_orth_scramble <- vcovCR(obj_orth_scramble, type = "CR2")
301 expect_equivalent(as.matrix(CR2_orth_orig), as.matrix(CR2_orth_scramble), tolerance = 1e-6)
302 })
303
304
305 test_that("different covariance structures all work for weighted models", {
306 CR_types <- paste0("CR", 0:4)
307
308 # us weighted (fev_data)
309 CR_us <- lapply(CR_types, function(x) vcovCR(obj_us, type = x))
310 expect_true(all(sapply(CR_us, inherits, "vcovCR")))
311
312 # toep weighted (fev_data)
313 CR_toep <- lapply(CR_types, function(x) vcovCR(obj_toep, type = x))
314 expect_true(all(sapply(CR_toep, inherits, "vcovCR")))
315
316 # cs weighted (BodyWeight)
317 CR_bw <- lapply(CR_types, function(x) vcovCR(obj_bw, type = x))
318 expect_true(all(sapply(CR_bw, inherits, "vcovCR")))
319
320 # ar1 weighted (Orthodont)
321 CR_orth <- lapply(CR_types, function(x) vcovCR(obj_orth, type = x))
322 expect_true(all(sapply(CR_orth, inherits, "vcovCR")))
323 })
324
325
326 test_that("coef_test and conf_int work for weighted models", {
327 # fev_data weighted
328 test_us <- coef_test(obj_us, vcov = "CR2", test = "All")
329 expect_s3_class(test_us, "data.frame")
330 expect_true(all(test_us$SE > 0))
331 expect_true(all(test_us$df_Satt > 0))
332
333 ci_us <- conf_int(obj_us, vcov = "CR2")
334 expect_s3_class(ci_us, "data.frame")
335 expect_true(all(ci_us$CI_L < ci_us$CI_U))
336 expect_true(all(ci_us$SE > 0))
337
338 # BodyWeight weighted
339 test_bw <- coef_test(obj_bw, vcov = "CR2", test = "All")
340 expect_s3_class(test_bw, "data.frame")
341 expect_true(all(test_bw$SE > 0))
342
343 ci_bw <- conf_int(obj_bw, vcov = "CR2")
344 expect_s3_class(ci_bw, "data.frame")
345 expect_true(all(ci_bw$CI_L < ci_bw$CI_U))
346
347 # Orthodont weighted
348 test_orth <- coef_test(obj_orth, vcov = "CR2", test = "All")
349 expect_s3_class(test_orth, "data.frame")
350 expect_true(all(test_orth$SE > 0))
351
352 ci_orth <- conf_int(obj_orth, vcov = "CR2")
353 expect_s3_class(ci_orth, "data.frame")
354 expect_true(all(ci_orth$CI_L < ci_orth$CI_U))
355
356 # Multiple CR types all produce valid coef_test / conf_int results
357 for (cr_type in c("CR0", "CR1", "CR2")) {
358 test_cr <- coef_test(obj_us, vcov = cr_type)
359 expect_s3_class(test_cr, "data.frame")
360 ci_cr <- conf_int(obj_us, vcov = cr_type)
361 expect_s3_class(ci_cr, "data.frame")
362 }
363 })
364
365
366 test_that("Wald_test works for weighted models", {
367 # fev_data weighted
368 coefs_us <- coef_CS(obj_us)
369 pairs_us <- utils::combn(length(coefs_us), 2, simplify = FALSE)[1:3]
370 constraints_us <- lapply(pairs_us, constrain_zero, coefs = coefs_us)
371
372 Wald_us <- Wald_test(obj_us, constraints = constraints_us, vcov = "CR2", test = "All")
373 expect_true(is.list(Wald_us))
374 expect_true(all(sapply(Wald_us, function(w) all(w$Fstat >= 0))))
375
376 # BodyWeight weighted
377 coefs_bw <- coef_CS(obj_bw)
378 pairs_bw <- utils::combn(length(coefs_bw), 2, simplify = FALSE)
379 constraints_bw <- lapply(pairs_bw, constrain_zero, coefs = coefs_bw)
380
381 Wald_bw <- Wald_test(obj_bw, constraints = constraints_bw, vcov = "CR2", test = "All")
382 expect_true(is.list(Wald_bw))
383 expect_true(all(sapply(Wald_bw, function(w) all(w$Fstat >= 0))))
384
385 # Orthodont weighted
386 coefs_orth <- coef_CS(obj_orth)
387 pairs_orth <- utils::combn(length(coefs_orth), 2, simplify = FALSE)[1:3]
388 constraints_orth <- lapply(pairs_orth, constrain_zero, coefs = coefs_orth)
389
390 Wald_orth <- Wald_test(obj_orth, constraints = constraints_orth, vcov = "CR2", test = "All")
391 expect_true(is.list(Wald_orth))
392 expect_true(all(sapply(Wald_orth, function(w) all(w$Fstat >= 0))))
393 })
394
395
396 test_that("weighted and unweighted results differ", {
397 # Fit identical model formula, one with weights and one without
398 obj_us_unwt <- mmrm(
399 FEV1 ~ RACE + SEX + ARMCD + us(AVISIT | USUBJID),
400 data = fev_data
401 )
402
403 CR2_wt <- vcovCR(obj_us, type = "CR2")
404 CR2_unwt <- vcovCR(obj_us_unwt, type = "CR2")
405
406 # The weighted and unweighted CRVEs should not be identical
407 expect_false(identical(as.matrix(CR2_wt), as.matrix(CR2_unwt)))
408
409 # But both should be valid vcovCR objects
410 expect_s3_class(CR2_wt, "vcovCR")
411 expect_s3_class(CR2_unwt, "vcovCR")
412 })
413
414
415 test_that("weight scale does not matter for weighted models", {
416 # Fit model with scaled-up weights (multiply all weights by a constant)
417 fev_data$wt_scaled <- fev_data$wt * 5
418
419 obj_us_scaled <- mmrm(
420 FEV1 ~ RACE + SEX + ARMCD + us(AVISIT | USUBJID),
421 data = fev_data,
422 weights = fev_data$wt_scaled
423 )
424
425 CR_types <- paste0("CR", 0:4)
426
427 CR_orig <- lapply(CR_types, function(x) vcovCR(obj_us, type = x))
428 CR_scaled <- lapply(CR_types, function(x) vcovCR(obj_us_scaled, type = x))
429
430 expect_equal(
431 lapply(CR_orig, as.matrix),
432 lapply(CR_scaled, as.matrix),
433 tolerance = 5e-4
434 )
435 })
436
437
438 test_that("clubSandwich works with dropped observations (weighted)", {
439
440 dat_miss <- fev_data
441 dat_miss$FEV1[sample.int(nrow(fev_data), size = round(nrow(fev_data) / 10))] <- NA
442
443 # mmrm auto-drops NA rows (no na.action parameter)
444 obj_dropped <- mmrm(
445 FEV1 ~ RACE + SEX + ARMCD + us(AVISIT | USUBJID),
446 data = dat_miss,
447 weights = dat_miss$wt
448 )
449
450 # mmrm has no subset parameter — manually subset the data
451 dat_complete <- dat_miss[!is.na(dat_miss$FEV1), ]
452 obj_complete <- mmrm(
453 FEV1 ~ RACE + SEX + ARMCD + us(AVISIT | USUBJID),
454 data = dat_complete,
455 weights = dat_complete$wt
456 )
457
458 CR_types <- paste0("CR", 0:4)
459
460 CR_drop <- lapply(CR_types, function(x) vcovCR(obj_dropped, type = x))
461 CR_complete <- lapply(CR_types, function(x) vcovCR(obj_complete, type = x))
462 expect_equal(CR_drop, CR_complete)
463
464 test_drop <- lapply(CR_types, function(x) coef_test(obj_dropped, vcov = x, test = "All", p_values = FALSE))
465 test_complete <- lapply(CR_types, function(x) coef_test(obj_complete, vcov = x, test = "All", p_values = FALSE))
466 expect_equal(test_drop, test_complete)
467 })
468