Debian Codegraph @

tests/testthat/test_linear_contrast_multiple_comparisons.R

raw
1 data("ChickWeight", package = "datasets")
2 lm_fit <- lm(weight ~ 0 + Diet + Time:Diet, data = ChickWeight)
3
4 test_that("linear_contrast multiple comparisons p-value adjustment works correctly", {
5
6 # taken from example usage
7 lc1 <- linear_contrast(lm_fit,
8 vcov = "CR2",
9 cluster = ChickWeight$Chick,
10 contrasts = constrain_pairwise("Diet.:Time", reg_ex = TRUE),
11 p_values = TRUE)
12
13 lc2 <- linear_contrast(lm_fit,
14 vcov = "CR2",
15 cluster = ChickWeight$Chick,
16 contrasts = constrain_pairwise("Diet.:Time", reg_ex = TRUE),
17 p_values = TRUE,
18 adjustment_method = "none")
19
20 expect_equal(lc1, lc2) # check explicitly stating adjustment_method default doesn't change anything
21
22 lc3 <- linear_contrast(lm_fit,
23 vcov = "CR2",
24 cluster = ChickWeight$Chick,
25 contrasts = constrain_pairwise("Diet.:Time", reg_ex = TRUE),
26 p_values = TRUE,
27 adjustment_method = "holm")
28
29 expect_false(all(lc2$p_val == lc3$p_val))
30 expect_equal(p.adjust(lc2$p_val, method = "holm"), lc3$p_val)
31
32 # test using adjustment without p_values = TRUE
33 expect_warning(lc1 <- linear_contrast(lm_fit,
34 vcov = "CR2",
35 cluster = ChickWeight$Chick,
36 contrasts = constrain_pairwise("Diet.:Time", reg_ex = TRUE),
37 p_values = FALSE, adjustment_method = "hochberg"))
38 lc1$p_val <- lc2$p_val
39 expect_equal(lc1, lc2)
40
41 # test using nonexistent adjustment type
42 # changed to expect error from match.arg
43 expect_error(linear_contrast(lm_fit,
44 vcov = "CR2",
45 cluster = ChickWeight$Chick,
46 contrasts = constrain_pairwise("Diet.:Time", reg_ex = TRUE),
47 p_values = TRUE,
48 adjustment_method = "nonexistent"),
49 "\'arg\' should be one of")
50
51 # test using p-adjustment when results have a length of 1
52 c_mat <- matrix(c(-1,1,rep(0,6)), nrow = 1)
53 expect_warning(
54 lc1 <- linear_contrast(lm_fit,
55 vcov = "CR2",
56 cluster = ChickWeight$Chick,
57 contrasts = c_mat,
58 p_values = TRUE,
59 adjustment_method = "BY") # changed from BF
60 )
61
62 lc2 <- linear_contrast(lm_fit,
63 vcov = "CR2",
64 cluster = ChickWeight$Chick,
65 contrasts = c_mat,
66 p_values = TRUE)
67
68 expect_identical(lc1, lc2)
69
70 lc4 <- linear_contrast(lm_fit,
71 vcov = "CR2",
72 cluster = ChickWeight$Chick,
73 contrasts = constrain_pairwise("Diet.$", reg_ex = TRUE),
74 p_values = TRUE,
75 adjustment_method = "none")
76
77 lc5 <- linear_contrast(lm_fit,
78 vcov = "CR2",
79 cluster = ChickWeight$Chick,
80 contrasts = constrain_pairwise("Diet.$", reg_ex = TRUE),
81 p_values = TRUE,
82 adjustment_method = "holm")
83
84 expect_false(all(lc4$p_val == lc5$p_val))
85 expect_equal(p.adjust(lc4$p_val, method = "holm"), lc5$p_val)
86
87 lc6 <- linear_contrast(lm_fit,
88 vcov = "CR2",
89 cluster = ChickWeight$Chick,
90 contrasts = constrain_pairwise("Diet.$", reg_ex = TRUE),
91 p_values = TRUE,
92 adjustment_method = "hochberg")
93
94 expect_false(all(lc4$p_val == lc6$p_val))
95 expect_equal(p.adjust(lc4$p_val, method = "hochberg"), lc6$p_val)
96
97 lc7 <- linear_contrast(lm_fit,
98 vcov = "CR2",
99 cluster = ChickWeight$Chick,
100 contrasts = constrain_pairwise("Diet.$", reg_ex = TRUE),
101 p_values = TRUE,
102 adjustment_method = "bonferroni")
103
104 expect_false(all(lc4$p_val == lc7$p_val))
105 expect_equal(p.adjust(lc4$p_val, method = "bonferroni"), lc7$p_val)
106
107 lc8 <- linear_contrast(lm_fit,
108 vcov = "CR2",
109 cluster = ChickWeight$Chick,
110 contrasts = constrain_pairwise("Diet.$", reg_ex = TRUE),
111 p_values = TRUE,
112 adjustment_method = "BH")
113
114 expect_false(all(lc4$p_val == lc8$p_val))
115 expect_equal(p.adjust(lc4$p_val, method = "BH"), lc8$p_val)
116
117 lc9 <- linear_contrast(lm_fit,
118 vcov = "CR2",
119 cluster = ChickWeight$Chick,
120 contrasts = constrain_pairwise("Diet.$", reg_ex = TRUE),
121 p_values = TRUE,
122 adjustment_method = "BY")
123
124 expect_false(all(lc4$p_val == lc9$p_val))
125 expect_equal(p.adjust(lc4$p_val, method = "BY"), lc9$p_val)
126
127 })