Skip to content

Commit 3cda2dc

Browse files
committed
add tests
1 parent 9b40278 commit 3cda2dc

File tree

5 files changed

+308
-1
lines changed

5 files changed

+308
-1
lines changed

R/test_likelihoodratio.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ test_likelihoodratio.ListNestedRegressions <- function(objects, estimator = "ML"
126126
p <- stats::pchisq(chi2, abs(dfs_diff), lower.tail = FALSE)
127127

128128
out <- data.frame(
129-
Deviance = devs,
130129
df = dfs,
131130
df_diff = dfs_diff,
131+
deviance = devs,
132132
Chi2 = chi2,
133133
p = p,
134134
stringsAsFactors = FALSE

tests/testthat/_snaps/check_dag.md

Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
# check_dag
2+
3+
Code
4+
print(dag)
5+
Output
6+
# Check for correct adjustment sets
7+
- Outcome: y
8+
- Exposure: x
9+
10+
Identification of direct and total effects
11+
12+
Model is correctly specified.
13+
No adjustment needed to estimate the direct and total effect of `x` on `y`.
14+
15+
16+
---
17+
18+
Code
19+
print(dag)
20+
Output
21+
# Check for correct adjustment sets
22+
- Outcome: y
23+
- Exposure: x
24+
- Adjustment: b
25+
26+
Identification of direct and total effects
27+
28+
Model is correctly specified.
29+
All minimal sufficient adjustments to estimate the direct and total effect were done.
30+
31+
32+
---
33+
34+
Code
35+
print(dag)
36+
Output
37+
# Check for correct adjustment sets
38+
- Outcome: y
39+
- Exposure: x
40+
41+
Identification of direct and total effects
42+
43+
Incorrectly adjusted!
44+
To estimate the direct and total effect, at least adjust for `b`. Currently, the model does not adjust for any variables.
45+
46+
47+
---
48+
49+
Code
50+
print(dag)
51+
Output
52+
# Check for correct adjustment sets
53+
- Outcome: y
54+
- Exposure: x
55+
- Adjustment: c
56+
57+
Identification of direct and total effects
58+
59+
Incorrectly adjusted!
60+
To estimate the direct and total effect, at least adjust for `b` and `c`. Currently, the model only adjusts for `c`. You possibly also need to adjust for `b` to block biasing paths.
61+
62+
63+
---
64+
65+
Code
66+
print(dag)
67+
Output
68+
# Check for correct adjustment sets
69+
- Outcome: y
70+
- Exposure: x
71+
- Adjustment: c
72+
73+
Identification of direct and total effects
74+
75+
Incorrectly adjusted!
76+
To estimate the direct and total effect, at least adjust for `b` and `c`. Currently, the model only adjusts for `c`. You possibly also need to adjust for `b` to block biasing paths.
77+
78+
79+
---
80+
81+
Code
82+
print(dag)
83+
Output
84+
# Check for correct adjustment sets
85+
- Outcome: mpg
86+
- Exposure: wt
87+
- Adjustments: cyl, disp and gear
88+
89+
Identification of direct and total effects
90+
91+
Model is correctly specified.
92+
All minimal sufficient adjustments to estimate the direct and total effect were done.
93+
94+
95+
# check_dag, multiple adjustment sets
96+
97+
Code
98+
print(dag)
99+
Output
100+
# Check for correct adjustment sets
101+
- Outcome: exam
102+
- Exposure: podcast
103+
104+
Identification of direct and total effects
105+
106+
Incorrectly adjusted!
107+
To estimate the direct and total effect, at least adjust for one of the following sets:
108+
- alertness, prepared
109+
- alertness, skills_course
110+
- mood, prepared
111+
- mood, skills_course.
112+
Currently, the model does not adjust for any variables.
113+
114+
115+
---
116+
117+
Code
118+
print(dag)
119+
Output
120+
# Check for correct adjustment sets
121+
- Outcome: exam
122+
- Exposure: podcast
123+
- Adjustments: alertness and prepared
124+
125+
Identification of direct and total effects
126+
127+
Model is correctly specified.
128+
All minimal sufficient adjustments to estimate the direct and total effect were done.
129+
130+
131+
---
132+
133+
Code
134+
print(dag)
135+
Output
136+
# Check for correct adjustment sets
137+
- Outcome: exam
138+
- Exposure: podcast
139+
- Adjustment: alertness
140+
141+
Identification of direct and total effects
142+
143+
Incorrectly adjusted!
144+
To estimate the direct and total effect, at least adjust for one of the following sets:
145+
- alertness, prepared
146+
- alertness, skills_course.
147+
Currently, the model only adjusts for `alertness`. You possibly also need to adjust for `prepared` and `skills_course` to block biasing paths.
148+
149+
150+
# check_dag, different adjustements for total and direct
151+
152+
Code
153+
print(dag)
154+
Output
155+
# Check for correct adjustment sets
156+
- Outcome: outcome
157+
- Exposure: exposure
158+
159+
Identification of direct effects
160+
161+
Incorrectly adjusted!
162+
To estimate the direct effect, at least adjust for `x1` and `x2`. Currently, the model does not adjust for any variables.
163+
164+
Identification of total effects
165+
166+
Incorrectly adjusted!
167+
To estimate the total effect, at least adjust for `x1`. Currently, the model does not adjust for any variables.
168+
169+
170+
---
171+
172+
Code
173+
print(dag)
174+
Output
175+
# Check for correct adjustment sets
176+
- Outcome: outcome
177+
- Exposure: exposure
178+
- Adjustment: x1
179+
180+
Identification of direct effects
181+
182+
Incorrectly adjusted!
183+
To estimate the direct effect, at least adjust for `x1` and `x2`. Currently, the model only adjusts for `x1`. You possibly also need to adjust for `x2` to block biasing paths.
184+
185+
Identification of total effects
186+
187+
Model is correctly specified.
188+
All minimal sufficient adjustments to estimate the total effect were done.
189+
190+
191+
---
192+
193+
Code
194+
print(dag)
195+
Output
196+
# Check for correct adjustment sets
197+
- Outcome: outcome
198+
- Exposure: exposure
199+
- Adjustment: x2
200+
201+
Identification of direct effects
202+
203+
Incorrectly adjusted!
204+
To estimate the direct effect, at least adjust for `x1` and `x2`. Currently, the model only adjusts for `x2`. You possibly also need to adjust for `x1` to block biasing paths.
205+
206+
Identification of total effects
207+
208+
Incorrectly adjusted!
209+
To estimate the total effect, do not adjust for `x2`.
210+
211+
212+
---
213+
214+
Code
215+
print(dag)
216+
Output
217+
# Check for correct adjustment sets
218+
- Outcome: outcome
219+
- Exposure: exposure
220+
- Adjustments: x1 and x2
221+
222+
Identification of direct effects
223+
224+
Model is correctly specified.
225+
All minimal sufficient adjustments to estimate the direct effect were done.
226+
227+
Identification of total effects
228+
229+
Incorrectly adjusted!
230+
To estimate the total effect, do not adjust for some or all of `x1` and `x2`.
231+
232+
233+
# check_dag, collider bias
234+
235+
Code
236+
print(dag)
237+
Output
238+
# Check for correct adjustment sets
239+
- Outcome: SMD_ICD11
240+
- Exposure: agegroup
241+
- Adjustments: edgroup3, gender_kid, pss4_kid_sum_2sd and residence
242+
243+
Identification of direct effects
244+
245+
Incorrectly adjusted!
246+
To estimate the direct effect, at least adjust for `edgroup3`, `gender_kid`, `pss4_kid_sum_2sd`, `residence` and `sm_h_total_kid`. Currently, the model only adjusts for `edgroup3`, `gender_kid`, `pss4_kid_sum_2sd` and `residence`. You possibly also need to adjust for `sm_h_total_kid` to block biasing paths.
247+
248+
Identification of total effects
249+
250+
Model is correctly specified.
251+
All minimal sufficient adjustments to estimate the total effect were done.
252+
253+
254+
---
255+
256+
Code
257+
print(dag)
258+
Output
259+
# Check for correct adjustment sets
260+
- Outcome: SMD_ICD11
261+
- Exposure: agegroup
262+
- Adjustments: edgroup3, gender_kid, pss4_kid_sum_2sd, residence and sm_h_total_kid
263+
- Collider: sm_h_total_kid
264+
265+
Identification of direct effects
266+
267+
Incorrectly adjusted!
268+
Your model adjusts for a potential collider. To estimate the direct effect, do not adjust for `sm_h_total_kid` to avoid collider-bias. It is recommended to double-check for the collider-bias on the dagitty-website.
269+
270+
Identification of total effects
271+
272+
Incorrectly adjusted!
273+
Your model adjusts for a potential collider. To estimate the total effect, do not adjust for `sm_h_total_kid` to avoid collider-bias. It is recommended to double-check for the collider-bias on the dagitty-website.
274+
275+

tests/testthat/_snaps/mclogit.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# model_performance
2+
3+
Code
4+
model_performance(mod_mb)
5+
Output
6+
# Indices of model performance
7+
8+
AIC | BIC | Nagelkerke's R2 | RMSE | Sigma
9+
-------------------------------------------------
10+
38.823 | 47.618 | 0.836 | 0.298 | 1.016
11+
12+
---
13+
14+
Code
15+
model_performance(mod_mc)
16+
Output
17+
# Indices of model performance
18+
19+
AIC | BIC | Nagelkerke's R2 | RMSE | Sigma
20+
-------------------------------------------------
21+
13.228 | 24.424 | 0.998 | 0.009 | 0.068
22+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# check_outliers.rma
2+
3+
Code
4+
out
5+
Output
6+
2 outliers detected: studies 4 (Hart & Sutherland) and 8 (TPT Madras).
7+

tests/testthat/test-test_likelihoodratio.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ test_that("test_likelihoodratio - lme4 ML", {
6363
t1 <- test_lrt(m1, m2, m3)
6464
t2 <- suppressMessages(anova(m1, m2, m3))
6565
expect_equal(attributes(t1)$estimator, "ml")
66+
expect_equal(t1$deviance, c(202.2215, 116.9578, 116.1164), tolerance = 1e-3)
6667
expect_equal(t1$Chi2, c(NA, 85.26365, 0.84141), tolerance = 1e-3)
6768
expect_equal(t1$p, c(NA, 0, 0.35899), tolerance = 1e-3)
6869
# close, but not the same
@@ -80,6 +81,7 @@ test_that("test_likelihoodratio - lme4 OLS", {
8081
test_that("test_likelihoodratio - lme4 REML", {
8182
expect_warning(t3 <- test_lrt(m1, m2, m3, estimator = "REML"))
8283
expect_equal(attributes(t3)$estimator, "reml")
84+
expect_equal(t3$deviance, c(210.9834, 121.6540, 124.5104), tolerance = 1e-3)
8385
expect_equal(t3$Chi2, c(NA, 89.32933, 2.85635), tolerance = 1e-3)
8486
expect_equal(t3$p, c(NA, 0, 0.09101), tolerance = 1e-3)
8587
})
@@ -91,6 +93,7 @@ m3 <- glm(am ~ mpg + hp + vs, data = mtcars, family = binomial())
9193
test_that("test_likelihoodratio - glm", {
9294
t1 <- anova(m1, m2, m3, test = "LRT")
9395
t2 <- test_lrt(m1, m2, m3)
96+
expect_equal(t1$`Resid. Dev`, t2$deviance, tolerance = 1e-3)
9497
expect_equal(t1$`Pr(>Chi)`, t2$p, tolerance = 1e-3)
9598
expect_equal(t1$Deviance, t2$Chi2, tolerance = 1e-3)
9699
expect_equal(attributes(t2)$estimator, "ml")

0 commit comments

Comments
 (0)