Debian Codegraph @

tests/testthat/test_lme_MVML.R

raw
1 context("multi-variate multi-level lme objects")
2 set.seed(20190513)
3
4 skip_on_cran()
5
6 dat <- read.table(file="https://raw.githubusercontent.com/wviechtb/multivariate_multilevel_models/master/data.dat", header=TRUE, sep="\t")
7 dat$pa <- rowMeans(dat[, grepl("pa", names(dat))])
8 dat$na <- rowMeans(dat[, grepl("na", names(dat))])
9
10 # keep only variables that are needed
11 dat <- dat[, c("id", "sex", "beep", "pa", "na")]
12
13 # keep only the first 10 IDs
14 dat <- subset(dat, id <= 10)
15
16 # change into very long format
17 dat <- reshape(dat, direction="long", varying=c("pa","na"), v.names="y", idvar="obs", timevar="outcome")
18 dat$obs <- NULL
19 dat <- dat[order(dat$id, dat$beep, dat$outcome),]
20 rownames(dat) <- 1:nrow(dat)
21 dat$outnum <- dat$outcome
22 dat$outcome <- factor(dat$outcome)
23
24
25 library(nlme, quietly=TRUE, warn.conflicts=FALSE)
26
27 MVML_full <- lme(y ~ outcome - 1,
28 random = ~ outcome - 1 | id,
29 weights = varIdent(form = ~ 1 | outcome),
30 correlation = corSymm(form = ~ outnum | id/beep),
31 data = dat, na.action = na.omit)
32
33 MVML_diag <- lme(y ~ outcome - 1,
34 random = ~ outcome - 1 | id,
35 weights = varIdent(form = ~ 1 | outcome),
36 data = dat, na.action = na.omit)
37
38 gls_full <- gls(y ~ outcome - 1,
39 weights = varIdent(form = ~ 1 | outcome),
40 correlation = corSymm(form = ~ outnum | id/beep),
41 data = dat, na.action = na.omit)
42
43 objects <- list(MVML_full = MVML_full, MVML_diag = MVML_diag, gls = gls_full)
44
45 CR2_mats <- lapply(objects, vcovCR, type = "CR2")
46
47 test_that("bread works", {
48 bread_checks <- lapply(objects, check_bread, cluster = dat$id, y = dat$y)
49 expect_true(all(unlist(bread_checks)))
50
51 obj_vcovs <- lapply(objects, vcov)
52 obj_bread <- lapply(objects, function(obj) obj$sigma^2 * sandwich::bread(obj) / v_scale(obj))
53 expect_equal(obj_vcovs, obj_bread)
54 })
55
56
57 test_that("vcovCR options work for CR2", {
58
59 expect_identical(vcovCR(MVML_full, cluster = dat$id, type = "CR2"), CR2_mats[["MVML_full"]])
60 expect_equal(vcovCR(MVML_full, type = "CR2", inverse_var = TRUE), CR2_mats[["MVML_full"]])
61 expect_false(identical(vcovCR(MVML_full, type = "CR2", inverse_var = FALSE), CR2_mats[["MVML_full"]]))
62 target <- targetVariance(MVML_full)
63 expect_equal(vcovCR(MVML_full, type = "CR2", target = target, inverse_var = TRUE), CR2_mats[["MVML_full"]])
64 attr(CR2_mats[["MVML_full"]], "inverse_var") <- FALSE
65 expect_equal(vcovCR(MVML_full, type = "CR2", target = target, inverse_var = FALSE), CR2_mats[["MVML_full"]])
66
67 })
68
69
70 test_that("CR2 is target-unbiased", {
71
72 CR2_checks <- mapply(check_CR, obj = objects, vcov = CR2_mats)
73 expect_true(all(CR2_checks))
74 })
75
76
77 CR_types <- paste0("CR",0:3)
78
79 test_that("Order doesn't matter.", {
80
81 check_sort_order(MVML_full, dat, seed = 20200530)
82 check_sort_order(MVML_diag, dat, seed = 20200530)
83
84 })
85
86
87 test_that("clubSandwich works with dropped observations", {
88
89 dat_miss <- dat
90 dat_miss$y[sample.int(nrow(dat), size = round(nrow(dat) / 10))] <- NA
91 obj_dropped <- update(MVML_full, data = dat_miss, na.action = na.omit)
92 obj_complete <- update(MVML_full, data = dat_miss, subset = !is.na(y))
93
94 CR_drop <- lapply(CR_types, function(x) vcovCR(obj_dropped, type = x))
95 CR_complete <- lapply(CR_types, function(x) vcovCR(obj_complete, type = x))
96 expect_identical(CR_drop, CR_complete)
97
98 test_drop <- lapply(CR_drop, function(x) coef_test(obj_dropped, vcov = x, test = "All", p_values = FALSE))
99 test_complete <- lapply(CR_complete, function(x) coef_test(obj_complete, vcov = x, test = "All", p_values = FALSE))
100 expect_identical(test_drop, test_complete)
101 })
102
103
104 test_that("Possible to cluster at higher level than random effects", {
105
106 # create 4th level
107 n_groups <- nlevels(factor(dat$id))
108 group_id <- rep(1:n_groups, each = 4)[dat$id]
109
110 # cluster at level 4
111 expect_is(vcovCR(MVML_full, type = "CR2", cluster = group_id), "vcovCR")
112 expect_is(vcovCR(MVML_diag, type = "CR2", cluster = group_id), "vcovCR")
113
114 })