Debian Codegraph @

tests/testthat/test_gls.R

raw
1 context("gls objects")
2 set.seed(20190513)
3
4 skip_if_not_installed("nlme")
5
6 library(nlme, quietly=TRUE, warn.conflicts=FALSE)
7
8 data(Ovary, package = "nlme")
9
10 Ovary$time_int <- 1:nrow(Ovary)
11
12 lm_hom <- gls(follicles ~ sin(2*pi*Time) + cos(2*pi*Time), data = Ovary)
13 lm_power <- gls(follicles ~ sin(2*pi*Time) + cos(2*pi*Time), data = Ovary,
14 weights = varPower())
15 lm_AR1 <- gls(follicles ~ sin(2*pi*Time) + cos(2*pi*Time), data = Ovary,
16 correlation = corAR1(form = ~ time_int | Mare))
17 lm_AR1_power <- update(lm_AR1, weights = varPower())
18
19 test_that("bread works", {
20 expect_true(check_bread(lm_hom, cluster = Ovary$Mare, y = Ovary$follicles))
21 expect_true(check_bread(lm_power, cluster = Ovary$Mare, y = Ovary$follicles))
22 expect_true(check_bread(lm_AR1, cluster = Ovary$Mare, y = Ovary$follicles))
23 expect_true(check_bread(lm_AR1_power, cluster = Ovary$Mare, y = Ovary$follicles))
24
25 expect_equal(vcov(lm_hom), lm_hom$sigma^2 * bread(lm_hom) / v_scale(lm_hom))
26 expect_equal(vcov(lm_power), lm_power$sigma^2 * bread(lm_power) / v_scale(lm_power))
27 expect_equal(vcov(lm_AR1), lm_AR1$sigma^2 * bread(lm_AR1) / v_scale(lm_AR1))
28 expect_equal(vcov(lm_AR1_power), lm_AR1_power$sigma^2 * bread(lm_AR1_power) / v_scale(lm_AR1_power))
29 })
30
31 test_that("vcovCR options work for CR2", {
32 CR2_AR1 <- vcovCR(lm_AR1, type = "CR2")
33 expect_equal(vcovCR(lm_AR1, cluster = Ovary$Mare, type = "CR2"), CR2_AR1)
34 expect_equal(vcovCR(lm_AR1, type = "CR2", inverse_var = TRUE), CR2_AR1)
35 expect_false(identical(vcovCR(lm_AR1, type = "CR2", inverse_var = FALSE), CR2_AR1))
36
37 target <- targetVariance(lm_AR1)
38 expect_equal(vcovCR(lm_AR1, type = "CR2", target = target, inverse_var = TRUE), CR2_AR1)
39 attr(CR2_AR1, "inverse_var") <- FALSE
40 expect_equal(vcovCR(lm_AR1, type = "CR2", target = target, inverse_var = FALSE), CR2_AR1)
41
42 CR2_power <- vcovCR(lm_AR1_power, type = "CR2")
43 expect_equal(vcovCR(lm_AR1_power, cluster = Ovary$Mare, type = "CR2"), CR2_power)
44 expect_equal(vcovCR(lm_AR1_power, type = "CR2", inverse_var = TRUE), CR2_power)
45 expect_false(identical(vcovCR(lm_AR1_power, type = "CR2", inverse_var = FALSE), CR2_power))
46
47 target <- targetVariance(lm_AR1_power, cluster = Ovary$Mare)
48 expect_equal(vcovCR(lm_AR1_power, type = "CR2", target = target, inverse_var = TRUE), CR2_power)
49 attr(CR2_power, "inverse_var") <- FALSE
50 expect_equal(vcovCR(lm_AR1_power, type = "CR2", target = target, inverse_var = FALSE), CR2_power)
51 })
52
53
54 test_that("vcovCR options work for CR4", {
55 CR4_AR1 <- vcovCR(lm_AR1, type = "CR4")
56 expect_equal(vcovCR(lm_AR1, cluster = Ovary$Mare, type = "CR4"), CR4_AR1)
57 expect_equal(vcovCR(lm_AR1, type = "CR4", inverse_var = TRUE), CR4_AR1)
58 expect_false(identical(vcovCR(lm_AR1, type = "CR4", inverse_var = FALSE), CR4_AR1))
59
60 target <- targetVariance(lm_AR1)
61 expect_equal(vcovCR(lm_AR1, type = "CR4", target = target, inverse_var = TRUE), CR4_AR1)
62 attr(CR4_AR1, "inverse_var") <- FALSE
63 expect_equal(vcovCR(lm_AR1, type = "CR4", target = target, inverse_var = FALSE), CR4_AR1)
64
65 CR4_power <- vcovCR(lm_AR1_power, type = "CR4")
66 expect_equal(vcovCR(lm_AR1_power, cluster = Ovary$Mare, type = "CR4"), CR4_power)
67 expect_equal(vcovCR(lm_AR1_power, type = "CR4", inverse_var = TRUE), CR4_power)
68 expect_false(identical(vcovCR(lm_AR1_power, type = "CR4", inverse_var = FALSE), CR4_power))
69
70 target <- targetVariance(lm_AR1_power)
71 expect_equal(vcovCR(lm_AR1_power, type = "CR4", target = target, inverse_var = TRUE), CR4_power)
72 attr(CR4_power, "inverse_var") <- FALSE
73 expect_equal(vcovCR(lm_AR1_power, type = "CR4", target = target, inverse_var = FALSE), CR4_power)
74 })
75
76
77 test_that("CR2 and CR4 are target-unbiased", {
78 expect_true(check_CR(lm_AR1, vcov = "CR2"))
79 expect_true(check_CR(lm_AR1_power, vcov = "CR2"))
80 expect_true(check_CR(lm_AR1, vcov = "CR4"))
81 expect_true(check_CR(lm_AR1_power, vcov = "CR4"))
82 })
83
84 test_that("get_data works.", {
85
86 re_order <- sample(nrow(Ovary))
87 egg_scramble <- Ovary[re_order,]
88 gls_scramble <- gls(follicles ~ sin(2*pi*Time) + cos(2*pi*Time),
89 data = egg_scramble)
90 scramble_dat <- get_data(gls_scramble)
91 expect_equal(egg_scramble, scramble_dat)
92
93 cars_30 <- rownames(subset(mtcars , mpg < 30))
94 m1 <- gls(mpg ~ hp, data = subset(mtcars , mpg < 30))
95 vcr1 <- vcovCR(m1, cluster = cars_30, type = "CR0")
96
97 dat30 <- subset(mtcars , mpg < 30)
98 m2 <- gls(mpg ~ hp, data = dat30)
99 vcr2 <- vcovCR(m2, cluster = cars_30, type = "CR0")
100
101 m3 <- gls(mpg ~ hp, data = subset(mtcars , mpg < 30))
102 m3$data <- dat30
103 vcr3 <- vcovCR(m3, cluster = cars_30, type = "CR0")
104
105 expect_equal(vcr1, vcr2)
106 expect_equal(vcr2, vcr3)
107 })
108
109
110 CR_types <- paste0("CR",0:4)
111
112 test_that("Order doesn't matter.", {
113 check_sort_order(lm_AR1_power, dat = Ovary,
114 tol = 10^-4, tol2 = 10^-3, tol3 = 10^-3)
115
116 })
117
118
119 test_that("clubSandwich works with dropped observations", {
120 dat_miss <- Ovary
121 dat_miss$follicles[sample.int(nrow(Ovary), size = round(nrow(Ovary) / 10))] <- NA
122 lm_dropped <- gls(follicles ~ sin(2*pi*Time) + cos(2*pi*Time), data = dat_miss,
123 correlation = corAR1(form = ~ 1 | Mare), na.action = na.omit)
124 lm_complete <- gls(follicles ~ sin(2*pi*Time) + cos(2*pi*Time),
125 data = dat_miss, subset = !is.na(follicles),
126 correlation = corAR1(form = ~ 1 | Mare))
127
128 CR_drop <- lapply(CR_types, function(x) vcovCR(lm_dropped, type = x))
129 CR_complete <- lapply(CR_types, function(x) vcovCR(lm_complete, type = x))
130 expect_equal(CR_drop, CR_complete)
131
132 test_drop <- lapply(CR_types, function(x) coef_test(lm_dropped, vcov = x, test = "All", p_values = FALSE))
133 test_complete <- lapply(CR_types, function(x) coef_test(lm_complete, vcov = x, test = "All", p_values = FALSE))
134 expect_equal(test_drop, test_complete)
135 })
136
137 test_that("Possible to cluster at higher level than random effects", {
138
139 # create higher level
140 pair_id <- rep(1:nlevels(Ovary$Mare), each = 3, length.out = nlevels(Ovary$Mare))[Ovary$Mare]
141
142 re_order <- sample(nrow(Ovary))
143 dat_scramble <- Ovary[re_order,]
144 pair_scramble <- pair_id[re_order]
145
146 # cluster at higher level
147 expect_is(vcovCR(lm_hom, type = "CR2", cluster = pair_id), "vcovCR")
148 expect_is(vcovCR(lm_power, type = "CR2", cluster = pair_id), "vcovCR")
149 expect_is(vcovCR(lm_AR1, type = "CR2", cluster = pair_id), "vcovCR")
150 V <- vcovCR(lm_AR1_power, type = "CR2", cluster = pair_id)
151 expect_is(V, "vcovCR")
152
153 expect_error(vcovCR(lm_AR1, type = "CR2", cluster = pair_scramble))
154 expect_error(vcovCR(lm_AR1_power, type = "CR2", cluster = pair_scramble))
155
156 # check that result does not depend on sort-order
157 V_scramble <- vcovCR(update(lm_AR1_power, data = dat_scramble),
158 type = "CR2", cluster = pair_scramble)
159 expect_equal(diag(V), diag(V_scramble), tol = 10^-6)
160 })
161
162