Debian Codegraph @

tests/testthat/test_geeglm.R

raw
1 context("geeglm objects")
2 set.seed(202201030)
3
4 skip_if_not_installed("geepack")
5
6 library(geepack)
7
8 J <- 20
9 tp <- 5
10 # Simulating a dataset
11 idvar <- rep(1:J, each=tp)
12 idvar2 <- factor(sample(LETTERS)[idvar])
13 timeorder <- rep(1:tp, J)
14 tvar <- timeorder + rnorm(length(timeorder))
15 x1 <- rnorm(length(timeorder))
16 x2 <- rnorm(J)[idvar]
17 uuu <- rep(rnorm(J), each=tp)
18 yvar <- 1 + 0.4 * x1 + 0.2 * x2 + 2 * tvar + uuu + rnorm(length(tvar))
19 simdat <- data.frame(idvar, timeorder, x1, x2, tvar, yvar, idvar2)
20
21 simdatPerm <- simdat[sample(nrow(simdat)),]
22 simdatPerm <- simdatPerm[order(simdatPerm$idvar),]
23 wav <- simdatPerm$timeorder
24
25 # AR1 + wave
26 geeglm_AR1_wav <- geeglm(yvar ~ tvar + x1, id = idvar,
27 data = simdatPerm,
28 corstr = "ar1", waves = timeorder)
29 geeglm_AR1_wav
30
31 # AR1
32 geeglm_AR1 <- geeglm(yvar ~ tvar, id = idvar, data = simdat, family = "gaussian", corstr = "ar1")
33
34 # Independence
35 geeglm_ind <- geeglm(yvar ~ tvar + x1 + x2, id = idvar, data = simdat, corstr = "independence")
36
37 # Exchangeable
38 geeglm_exch <- geeglm(yvar ~ tvar + x2, id = idvar, data = simdat, corstr = "exchangeable")
39 geeglm_exch2 <- geeglm(yvar ~ tvar + x2, id = idvar2, data = simdat, corstr = "exchangeable")
40
41 # Unstructured
42 geeglm_unstr <- geeglm(yvar ~ tvar, id = idvar, data = simdat, corstr = "unstructured")
43 geeglm_unstr2 <- geeglm(yvar ~ tvar, id = idvar2, data = simdat, corstr = "unstructured")
44
45 # User-defined
46 zcor <- genZcor(clusz = table(simdat$idvar),
47 waves = simdat$timeorder,
48 corstrv = 4)
49 zcor_user <- zcor
50
51 geeglm_user <- geeglm(yvar ~ tvar + x1,
52 id = idvar, waves = timeorder,
53 data = simdat,
54 zcor = zcor_user, corstr = "userdefined")
55
56 # User-defined, Toeplitz
57 zcor_toep <- matrix(NA, nrow(zcor), 4)
58 zcor_toep[,1] <- apply(zcor[,c(1, 5, 8,10)], 1, sum)
59 zcor_toep[,2] <- apply(zcor[,c(2, 6, 9)], 1, sum)
60 zcor_toep[,3] <- apply(zcor[,c(3, 7)], 1, sum)
61 zcor_toep[,4] <- zcor[,4]
62
63 geeglm_toep <- geeglm(yvar ~ tvar + x1,
64 id = idvar, waves = timeorder,
65 data = simdat,
66 zcor = zcor_toep, corstr = "userdefined")
67
68 # Fixed correlation
69 cor_fix <- matrix(c(1 , 0.5 , 0.25, 0.125, 0.125,
70 0.5 , 1 , 0.25, 0.125, 0.125,
71 0.25 , 0.25 , 1 , 0.5 , 0.125,
72 0.125, 0.125, 0.5 , 1 , 0.125,
73 0.125, 0.125, 0.125, 0.125, 1 ), nrow=5, ncol=5)
74 zcor_fix <- fixed2Zcor(cor_fix, id=simdat$idvar, waves=simdat$timeorder)
75
76 geeglm_fix <- geeglm(yvar ~ tvar + x1 + x2,
77 id = idvar, waves = timeorder,
78 data = simdat,
79 zcor = zcor_fix, corstr = "fixed")
80
81 test_that("bread works", {
82
83 expect_true(check_bread(geeglm_AR1_wav, cluster = simdatPerm$idvar, y = simdatPerm$yvar, tol = 1e-5))
84 expect_true(check_bread(geeglm_AR1, cluster = simdat$idvar, y = simdat$yvar, tol = 1e-5))
85 expect_true(check_bread(geeglm_ind, cluster = simdat$idvar, y = simdat$yvar, tol = 1e-5))
86 expect_true(check_bread(geeglm_exch, cluster = simdat$idvar, y = simdat$yvar, tol = 1e-5))
87 expect_true(check_bread(geeglm_exch2, cluster = simdat$idvar2, y = simdat$yvar, tol = 1e-5))
88 expect_true(check_bread(geeglm_unstr, cluster = simdat$idvar, y = simdat$yvar, tol = 1e-5))
89 expect_true(check_bread(geeglm_unstr2, cluster = simdat$idvar2, y = simdat$yvar, tol = 1e-5))
90 expect_true(check_bread(geeglm_user, cluster = simdat$idvar, y = simdat$yvar, tol = 1e-5))
91 expect_true(check_bread(geeglm_toep, cluster = simdat$idvar, y = simdat$yvar, tol = 1e-5))
92 expect_true(check_bread(geeglm_fix, cluster = simdat$idvar, y = simdat$yvar, tol = 1e-5))
93
94 })
95
96
97 test_that("vcovCR options work for CR2", {
98
99 CR2_AR1_wav <- vcovCR(geeglm_AR1_wav, type = "CR2")
100 expect_equal(vcovCR(geeglm_AR1_wav, cluster = simdatPerm$idvar, type = "CR2"), CR2_AR1_wav)
101 expect_equal(vcovCR(geeglm_AR1_wav, cluster = simdatPerm$idvar, type = "CR2", inverse_var = TRUE), CR2_AR1_wav)
102 expect_false(identical(vcovCR(geeglm_AR1_wav, type = "CR2", cluster = simdatPerm$idvar, inverse_var = FALSE), CR2_AR1_wav))
103
104 target <- targetVariance(geeglm_AR1_wav, cluster = simdatPerm$idvar)
105 expect_equal(vcovCR(geeglm_AR1_wav, type = "CR2", target = target, inverse_var = TRUE),
106 CR2_AR1_wav, ignore_attr = TRUE)
107 attr(CR2_AR1_wav, "inverse_var") <- FALSE
108 expect_equal(vcovCR(geeglm_AR1_wav, type = "CR2", target = target, inverse_var = FALSE),
109 CR2_AR1_wav, ignore_attr = TRUE)
110
111 })
112
113
114 test_that("vcovCR options work for CR4", {
115 CR4_AR1_wav <- vcovCR(geeglm_AR1_wav, type = "CR4")
116 expect_equal(vcovCR(geeglm_AR1_wav, cluster = simdatPerm$idvar, type = "CR4"), CR4_AR1_wav)
117 expect_equal(vcovCR(geeglm_AR1_wav, cluster = simdatPerm$idvar, type = "CR4", inverse_var = TRUE), CR4_AR1_wav)
118 expect_false(identical(vcovCR(geeglm_AR1_wav, cluster = simdatPerm$idvar, type = "CR4", inverse_var = FALSE), CR4_AR1_wav))
119
120 target <- targetVariance(geeglm_AR1_wav, cluster = simdatPerm$idvar)
121 expect_equal(vcovCR(geeglm_AR1_wav, cluster = simdatPerm$idvar, type = "CR4",
122 target = target, inverse_var = TRUE), CR4_AR1_wav, ignore_attr = TRUE)
123 attr(CR4_AR1_wav, "inverse_var") <- FALSE
124 expect_equal(vcovCR(geeglm_AR1_wav, cluster = simdatPerm$idvar, type = "CR4",
125 target = target, inverse_var = FALSE), CR4_AR1_wav, ignore_attr = TRUE)
126 })
127
128
129 test_that("CR2 and CR4 are target-unbiased", {
130
131 expect_true(check_CR(geeglm_AR1_wav, cluster = simdatPerm$idvar, vcov = "CR2"))
132 expect_true(check_CR(geeglm_AR1, cluster = simdat$idvar, vcov = "CR2"))
133 expect_true(check_CR(geeglm_ind, cluster = simdat$idvar, vcov = "CR2"))
134 expect_true(check_CR(geeglm_exch, cluster = simdat$idvar, vcov = "CR2"))
135 expect_true(check_CR(geeglm_exch2, cluster = simdat$idvar2, vcov = "CR2"))
136 expect_true(check_CR(geeglm_unstr, cluster = simdat$idvar, vcov = "CR2"))
137 expect_true(check_CR(geeglm_unstr2, cluster = simdat$idvar2, vcov = "CR2"))
138 expect_true(check_CR(geeglm_user, cluster = simdat$idvar, vcov = "CR2"))
139 expect_true(check_CR(geeglm_toep, cluster = simdat$idvar, vcov = "CR2"))
140 expect_true(check_CR(geeglm_fix, cluster = simdat$idvar, vcov = "CR2"))
141
142 expect_true(check_CR(geeglm_AR1_wav, cluster = simdatPerm$idvar, vcov = "CR4"))
143 expect_true(check_CR(geeglm_AR1, cluster = simdat$idvar, vcov = "CR4"))
144 expect_true(check_CR(geeglm_ind, cluster = simdat$idvar, vcov = "CR4"))
145 expect_true(check_CR(geeglm_exch, cluster = simdat$idvar, vcov = "CR4"))
146 expect_true(check_CR(geeglm_exch2, cluster = simdat$idvar, vcov = "CR4"))
147 expect_true(check_CR(geeglm_unstr, cluster = simdat$idvar, vcov = "CR4"))
148 expect_true(check_CR(geeglm_unstr2, cluster = simdat$idvar, vcov = "CR4"))
149 expect_true(check_CR(geeglm_user, cluster = simdat$idvar, vcov = "CR4"))
150 expect_true(check_CR(geeglm_toep, cluster = simdat$idvar, vcov = "CR4"))
151 expect_true(check_CR(geeglm_fix, cluster = simdat$idvar, vcov = "CR4"))
152
153 })
154
155
156
157 CR_types <- paste0("CR",0:4)
158
159 test_that("Order doesn't matter.", {
160
161 skip_on_cran()
162
163 check_sort_order(geeglm_ind, dat = simdat, CR_types = CR_types)
164 check_sort_order(geeglm_ind, dat = simdat, arrange = "idvar2", CR_types = CR_types)
165 check_sort_order(geeglm_exch, dat = simdat, arrange = "idvar", CR_types = CR_types)
166 check_sort_order(geeglm_exch2, dat = simdat, arrange = "idvar", CR_types = CR_types)
167 check_sort_order(geeglm_exch2, dat = simdat, arrange = "idvar2", CR_types = CR_types)
168 expect_error(check_sort_order(geeglm_unstr, dat = simdat, arrange = "idvar", CR_types = CR_types))
169
170 })
171
172
173 test_that("clubSandwich works with dropped observations", {
174
175 dat_miss <- simdat
176 dat_miss$yvar[sample.int(nrow(simdat), size = round(nrow(simdat) / 10))] <- NA
177 dat_complete <- subset(dat_miss, !is.na(yvar))
178
179 mod_dropped <- geeglm(yvar ~ tvar, id = idvar,
180 data = dat_miss, corstr = "independence")
181 mod_complete <- geeglm(yvar ~ tvar, id = idvar,
182 data = dat_complete, corstr = "independence")
183
184 CR_drop <- lapply(CR_types, function(x) vcovCR(mod_dropped, type = x))
185 CR_complete <- lapply(CR_types, function(x) vcovCR(mod_complete, type = x))
186 expect_equal(CR_drop, CR_complete)
187
188 test_drop <- lapply(CR_types, function(x) coef_test(mod_dropped, cluster = dat_miss$idvar, vcov = x, test = "All", p_values = FALSE))
189 test_complete <- lapply(CR_types, function(x) coef_test(mod_complete, cluster = dat_complete$idvar, vcov = x, test = "All", p_values = FALSE))
190 compare_ttests(test_drop, test_complete)
191
192 })
193
194
195 test_that("vcovCR works for clustering variables higher than id variable.", {
196
197 # create higher level
198 pair_id <- rep(1:nlevels(as.factor(simdat$idvar)), each = 3, length.out = nlevels(as.factor(simdat$idvar)))[as.factor(simdat$idvar)] # factor cluster
199 pair_id <- factor(pair_id)
200
201 # cluster at higher level
202 V_AR1 <- vcovCR(geeglm_AR1, type = "CR2", cluster = pair_id)
203 V_AR1_wav <- vcovCR(geeglm_AR1_wav, type = "CR2", cluster = pair_id)
204 V_ind <- vcovCR(geeglm_ind, type = "CR2", cluster = pair_id)
205 V_exch <- vcovCR(geeglm_exch, type = "CR2", cluster = pair_id)
206 V_exch2 <- vcovCR(geeglm_exch2, type = "CR2", cluster = pair_id)
207 V_unstr <- vcovCR(geeglm_unstr, type = "CR2", cluster = pair_id)
208 V_unstr2 <- vcovCR(geeglm_unstr2, type = "CR2", cluster = pair_id)
209 V_user <- vcovCR(geeglm_user, type = "CR2", cluster = pair_id)
210 V_toep <- vcovCR(geeglm_toep, type = "CR2", cluster = pair_id)
211 V_fix <- vcovCR(geeglm_fix, type = "CR2", cluster = pair_id)
212 expect_is(V_AR1, "vcovCR")
213 expect_is(V_AR1_wav, "vcovCR")
214 expect_is(V_ind, "vcovCR")
215 expect_is(V_exch, "vcovCR")
216 expect_is(V_exch2, "vcovCR")
217 expect_is(V_unstr, "vcovCR")
218 expect_is(V_unstr2, "vcovCR")
219 expect_is(V_user, "vcovCR")
220 expect_is(V_toep, "vcovCR")
221 expect_is(V_fix, "vcovCR")
222
223 # check that result does not depend on sort-order
224 scramble_id <- factor(simdat$idvar, levels = sample(1:J))
225 dat_higher <- cbind(simdat, pair_id = factor(pair_id), scramble_id)
226 dat_higher <- dat_higher[order(dat_higher$scramble_id),]
227
228 dat_scramble <- dat_higher[sample(nrow(dat_higher)),]
229 dat_scramble <- dat_scramble[order(dat_scramble$scramble_id),]
230
231 V_AR1_scramble <- vcovCR(update(geeglm_AR1, data = dat_higher),
232 type = "CR2", cluster = dat_higher$pair_id)
233 expect_equal(V_AR1, V_AR1_scramble, tol = 10^-6, check.attributes = FALSE)
234
235 V_AR1_wav_scramble <- vcovCR(update(geeglm_AR1_wav, data = dat_higher),
236 type = "CR2", cluster = dat_higher$pair_id)
237 expect_equal(V_AR1_wav, V_AR1_wav_scramble, tol = 10^-6, check.attributes = FALSE)
238
239 V_ind_scramble <- vcovCR(update(geeglm_ind, data = dat_scramble),
240 type = "CR2", cluster = dat_scramble$pair_id)
241 expect_equal(V_ind, V_ind_scramble, tol = 10^-6, check.attributes = FALSE)
242
243 V_exch2_scramble <- vcovCR(update(geeglm_exch2, data = dat_scramble),
244 type = "CR2", cluster = dat_scramble$pair_id)
245 expect_equal(V_exch2, V_exch2_scramble, tol = 10^-6, check.attributes = FALSE)
246
247 V_unstr_scramble <- vcovCR(update(geeglm_unstr, data = dat_higher),
248 type = "CR2", cluster = dat_higher$pair_id)
249 expect_equal(V_unstr, V_unstr_scramble, tol = 10^-6, check.attributes = FALSE)
250
251 })
252
253
254 check_geeglm <- function(obj, type = c("CR0","CR3")) {
255
256 if (obj$std.err == "san.se") {
257 const <- 1
258 cr_club <- as.matrix(vcovCR(obj, type = "CR0"))
259 } else if (obj$std.err == "jack") {
260 J <- length(unique(obj$id))
261 p <- nrow(obj$geese$infls)
262 const <- (J - p) / J
263
264 cr_club <- as.matrix(vcovCR(obj, type = "CR3"))
265 }
266
267 cr_pack <- stats::vcov(obj)
268
269 expect_equal(cr_pack, const * cr_club)
270
271 }
272
273 geeglm_user_jack <- update(geeglm_user, std.err = "jack")
274 geeglm_toep_jack <- update(geeglm_toep, std.err = "jack")
275 geeglm_fix_jack <- update(geeglm_fix, std.err = "jack")
276
277 test_that("vcovCR agrees with geeglm for CR0 and CR3.", {
278
279 check_geeglm(geeglm_AR1_wav)
280 check_geeglm(geeglm_AR1)
281 check_geeglm(geeglm_ind)
282 check_geeglm(geeglm_exch)
283 check_geeglm(geeglm_exch2)
284 check_geeglm(geeglm_unstr)
285 check_geeglm(geeglm_unstr2)
286 check_geeglm(geeglm_user)
287 check_geeglm(geeglm_toep)
288 check_geeglm(geeglm_fix)
289
290 check_geeglm(update(geeglm_AR1_wav, std.err = "jack"))
291 check_geeglm(update(geeglm_AR1, std.err = "jack"))
292 check_geeglm(update(geeglm_ind, std.err = "jack"))
293 check_geeglm(update(geeglm_exch, std.err = "jack"))
294 check_geeglm(update(geeglm_exch2, std.err = "jack"))
295 check_geeglm(update(geeglm_unstr, std.err = "jack"))
296 check_geeglm(update(geeglm_unstr2, std.err = "jack"))
297
298 check_geeglm(geeglm_user_jack)
299 check_geeglm(geeglm_toep_jack)
300 check_geeglm(geeglm_fix_jack)
301
302 })
303
304 test_that("vcovCR agrees with geeglm for examples from geepack documentation.", {
305
306 # Poisson with unstructured correlation
307
308 data(seizure, package = "geepack")
309 seiz.l <- reshape(seizure,
310 varying=list(c("base","y1", "y2", "y3", "y4")),
311 v.names="y", times=0:4, direction="long")
312 seiz.l <- seiz.l[order(seiz.l$id, seiz.l$time),]
313 seiz.l$t <- ifelse(seiz.l$time == 0, 8, 2)
314 seiz.l$x <- ifelse(seiz.l$time == 0, 0, 1)
315 m_pois_un <- geeglm(y ~ offset(log(t)) + x + trt + x:trt, id = id,
316 data=seiz.l, corstr="exch", family=poisson, std.err = "jack")
317 m_pois_un3 <- update(m_pois_un)
318 check_geeglm(m_pois_un)
319 check_geeglm(m_pois_un3)
320
321 # Poisson with no correlation
322 m_pois_id <- geeglm(y ~ offset(log(t)) + x + trt + x:trt, id = id,
323 data = seiz.l, family = poisson,
324 corstr = "independence", std.err = "jack")
325 check_geeglm(m_pois_id)
326
327
328 # Binomial with exchangeable correlation
329 data(ohio, package = "geepack")
330 m_bin_ex <- geeglm(resp ~ age + smoke + age:smoke, id=id, data=ohio,
331 family=binomial, corstr="exch", scale.fix=TRUE, std.err = "jack")
332 check_geeglm(m_bin_ex)
333
334 # Binomial with AR(1) correlation
335 m_bin_ar <- geeglm(resp ~ age + smoke + age:smoke, id=id, data=ohio,
336 family=binomial, corstr="ar1", scale.fix=TRUE, std.err = "jack")
337 check_geeglm(m_bin_ar)
338
339 # Binomial with unstructured correlation
340 data(respiratory, package = "geepack")
341 respiratory$center <- factor(respiratory$center)
342 respiratory$id_center <- factor(paste(respiratory$center, respiratory$id, sep = "-"))
343
344 m_bin_un <- geeglm(outcome ~ center + treat + age + baseline,
345 data = respiratory,
346 id = id_center,
347 family = binomial(),
348 corstr = "unstructured", std.err = "jack")
349
350 check_geeglm(m_bin_un)
351
352
353 })