Skip to content

Commit 311b4ed

Browse files
committed
fix
1 parent 7366520 commit 311b4ed

File tree

2 files changed

+177
-23
lines changed

2 files changed

+177
-23
lines changed

tests/testthat/_snaps/compare_parameters.md

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
--------------------------------------------------------
4545
SD (Intercept: persons) | | | 1.08 ( 0.49, 2.37)
4646

47-
# compare_parameters, print_md
47+
# compare_parameters, print_md-1
4848

4949
Code
5050
print(out)
@@ -64,7 +64,38 @@
6464
| | | | | |
6565
|Observations | 180| | 180| |
6666

67-
---
67+
# compare_parameters, print_md-2
68+
69+
Code
70+
print(out)
71+
Output
72+
+----------------+-----------------------+--------+-----------------------+--------+
73+
| | lm1 | lm2 |
74+
+----------------+-----------------------+--------+-----------------------+--------+
75+
| Parameter | Coefficient (CI) | p | Coefficient (CI) | p |
76+
+================+=======================+========+=======================+========+
77+
| Groups |
78+
+----------------+-----------------------+--------+-----------------------+--------+
79+
| grp (2) | -4.31 (-15.95, 7.32) | 0.465 | 0.32 (-22.56, 23.20) | 0.978 |
80+
+----------------+-----------------------+--------+-----------------------+--------+
81+
| grp (3) | -1.31 (-13.47, 10.84) | 0.831 | 3.77 (-19.72, 27.26) | 0.752 |
82+
+----------------+-----------------------+--------+-----------------------+--------+
83+
| Interactions |
84+
+----------------+-----------------------+--------+-----------------------+--------+
85+
| Days * grp (2) | | | -1.01 ( -5.35, 3.32) | 0.645 |
86+
+----------------+-----------------------+--------+-----------------------+--------+
87+
| Days * grp (3) | | | -1.11 ( -5.53, 3.31) | 0.621 |
88+
+----------------+-----------------------+--------+-----------------------+--------+
89+
| Controls |
90+
+----------------+-----------------------+--------+-----------------------+--------+
91+
| Days | 10.44 ( 8.84, 12.03) | <0.001 | 11.23 ( 7.87, 14.60) | <0.001 |
92+
+----------------+-----------------------+--------+-----------------------+--------+
93+
| | | | | |
94+
+----------------+-----------------------+--------+-----------------------+--------+
95+
| Observations | 180 | | 180 | |
96+
+----------------+-----------------------+--------+-----------------------+--------+
97+
98+
# compare_parameters, print_md-3
6899

69100
Code
70101
print_md(cp)
@@ -88,3 +119,74 @@
88119
|SD (Intercept: Subject) | 37.06 ( 25.85, 53.13)| | 37.08 ( 25.85, 53.19)| |
89120
|SD (Residual) | 31.13 ( 27.89, 34.75)| | 31.30 ( 28.02, 34.96)| |
90121

122+
---
123+
124+
Code
125+
display(cp, format = "tt")
126+
Output
127+
+-------------------------+-----------------------+--------+-----------------------+--------+
128+
| | lm1 | lm2 |
129+
+-------------------------+-----------------------+--------+-----------------------+--------+
130+
| Parameter | Coefficient (CI) | p | Coefficient (CI) | p |
131+
+=========================+=======================+========+=======================+========+
132+
| Fixed Effects |
133+
+-------------------------+-----------------------+--------+-----------------------+--------+
134+
| Days | 10.44 ( 8.84, 12.03) | <0.001 | 11.23 ( 7.87, 14.60) | <0.001 |
135+
+-------------------------+-----------------------+--------+-----------------------+--------+
136+
| grp (2) | -4.31 (-15.95, 7.32) | 0.465 | 0.32 (-22.56, 23.20) | 0.978 |
137+
+-------------------------+-----------------------+--------+-----------------------+--------+
138+
| grp (3) | -1.31 (-13.47, 10.84) | 0.831 | 3.77 (-19.72, 27.26) | 0.752 |
139+
+-------------------------+-----------------------+--------+-----------------------+--------+
140+
| Days * grp (2) | | | -1.01 ( -5.35, 3.32) | 0.645 |
141+
+-------------------------+-----------------------+--------+-----------------------+--------+
142+
| Days * grp (3) | | | -1.11 ( -5.53, 3.31) | 0.621 |
143+
+-------------------------+-----------------------+--------+-----------------------+--------+
144+
| Random Effects |
145+
+-------------------------+-----------------------+--------+-----------------------+--------+
146+
| SD (Intercept: Subject) | 37.06 ( 25.85, 53.13) | | 37.08 ( 25.85, 53.19) | |
147+
+-------------------------+-----------------------+--------+-----------------------+--------+
148+
| SD (Residual) | 31.13 ( 27.89, 34.75) | | 31.30 ( 28.02, 34.96) | |
149+
+-------------------------+-----------------------+--------+-----------------------+--------+
150+
151+
# compare_parameters, print_md-4
152+
153+
Code
154+
print(out)
155+
Output
156+
157+
158+
|Parameter | Coefficient (CI) (lm1)| p (lm1)| Coefficient (CI) (lm2)| p (lm2)|
159+
|:--------------|----------------------:|-------:|----------------------:|-------:|
160+
|Groups | | | | |
161+
|grp (1) | 0.00| | 0.00| |
162+
|grp (2) | -4.31 (-15.95, 7.32)| 0.465| 0.32 (-22.56, 23.20)| 0.978|
163+
|grp (3) | -1.31 (-13.47, 10.84)| 0.831| 3.77 (-19.72, 27.26)| 0.752|
164+
|Interactions | | | | |
165+
|Days * grp (2) | | | -1.01 ( -5.35, 3.32)| 0.645|
166+
|Days * grp (3) | | | -1.11 ( -5.53, 3.31)| 0.621|
167+
|Controls | | | | |
168+
|Days | 10.44 ( 8.84, 12.03)| <0.001| 11.23 ( 7.87, 14.60)| <0.001|
169+
| | | | | |
170+
|Observations | 180| | 180| |
171+
172+
# compare_parameters, print_md-5
173+
174+
Code
175+
print(out)
176+
Output
177+
178+
179+
|Parameter | lm1| lm2|
180+
|:--------------|---------------------:|---------------------:|
181+
|Groups | | |
182+
|grp (1) | 0.00| 0.00|
183+
|grp (2) | -4.31 (-15.95, 7.32)| 0.32 (-22.56, 23.20)|
184+
|grp (3) | -1.31 (-13.47, 10.84)| 3.77 (-19.72, 27.26)|
185+
|Interactions | | |
186+
|Days * grp (2) | | -1.01 ( -5.35, 3.32)|
187+
|Days * grp (3) | | -1.11 ( -5.53, 3.31)|
188+
|Controls | | |
189+
|Days | 10.44 ( 8.84, 12.03)| 11.23 ( 7.87, 14.60)|
190+
| | | |
191+
|Observations | 180| 180|
192+

tests/testthat/test-compare_parameters.R

Lines changed: 73 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,10 @@ withr::with_options(
120120
})
121121

122122

123-
test_that("compare_parameters, print_md", {
123+
test_that("compare_parameters, print_md-1", {
124124
skip_if_not_installed("lme4")
125125
skip_if_not_installed("knitr")
126+
skip_if_not_installed("tinytable")
126127

127128
data(sleepstudy, package = "lme4")
128129
set.seed(1234)
@@ -132,24 +133,62 @@ withr::with_options(
132133

133134
cp <- compare_parameters(lm1, lm2, select = "{estimate} ({ci})|{p}", drop = "^\\(Intercept")
134135
out <- print_md(cp, groups = list(
135-
Groups = c("grp (2)", "grp (3)"),
136-
Interactions = c("Days * grp (2)", "Days * grp (3)"),
137-
Controls = "Days"
136+
Groups = c(2, 3),
137+
Interactions = c(4, 5),
138+
Controls = 1
138139
))
139140
expect_snapshot(print(out))
141+
})
142+
143+
test_that("compare_parameters, print_md-2", {
144+
skip_if_not_installed("lme4")
145+
skip_if_not_installed("knitr")
146+
skip_if_not_installed("tinytable")
147+
148+
data(sleepstudy, package = "lme4")
149+
set.seed(1234)
150+
sleepstudy$grp <- as.factor(sample.int(3, nrow(sleepstudy), replace = TRUE))
151+
lm1 <- lme4::lmer(Reaction ~ Days + grp + (1 | Subject), data = sleepstudy)
152+
lm2 <- lme4::lmer(Reaction ~ Days * grp + (1 | Subject), data = sleepstudy)
153+
154+
cp <- compare_parameters(lm1, lm2, select = "{estimate} ({ci})|{p}", drop = "^\\(Intercept")
155+
out <- display(cp, groups = list(
156+
Groups = c(2, 3),
157+
Interactions = c(4, 5),
158+
Controls = 1
159+
),
160+
format = "tt"
161+
)
162+
expect_snapshot(print(out))
163+
})
164+
165+
test_that("compare_parameters, print_md-3", {
166+
skip_if_not_installed("lme4")
167+
skip_if_not_installed("knitr")
168+
skip_if_not_installed("tinytable")
169+
170+
data(sleepstudy, package = "lme4")
171+
set.seed(1234)
172+
sleepstudy$grp <- as.factor(sample.int(3, nrow(sleepstudy), replace = TRUE))
173+
lm1 <- lme4::lmer(Reaction ~ Days + grp + (1 | Subject), data = sleepstudy)
174+
lm2 <- lme4::lmer(Reaction ~ Days * grp + (1 | Subject), data = sleepstudy)
140175

141176
cp <- compare_parameters(lm1, lm2, select = "{estimate} ({ci})|{p}", drop = "^\\(Intercept", effects = "all")
142177
expect_snapshot(print_md(cp))
178+
expect_snapshot(display(cp, format = "tt"))
179+
})
180+
181+
test_that("compare_parameters, print_md-4", {
182+
skip_if_not_installed("lme4")
183+
skip_if_not_installed("knitr")
184+
skip_if_not_installed("tinytable")
185+
186+
data(sleepstudy, package = "lme4")
187+
set.seed(1234)
188+
sleepstudy$grp <- as.factor(sample.int(3, nrow(sleepstudy), replace = TRUE))
189+
lm1 <- lme4::lmer(Reaction ~ Days + grp + (1 | Subject), data = sleepstudy)
190+
lm2 <- lme4::lmer(Reaction ~ Days * grp + (1 | Subject), data = sleepstudy)
143191

144-
# error
145-
expect_error(
146-
print_md(cp, groups = list(
147-
Groups = c("grp (2)", "grp (3)"),
148-
Interactions = c("Days * grp (2)", "Days * grp (3)"),
149-
Controls = "Days"
150-
)),
151-
regex = "Cannot combine"
152-
)
153192

154193
# with reference level
155194
cp <- compare_parameters(lm1, lm2, select = "{estimate} ({ci})|{p}", drop = "^\\(Intercept", include_reference = TRUE)
@@ -160,6 +199,19 @@ withr::with_options(
160199
))
161200
expect_snapshot(print(out))
162201

202+
})
203+
204+
test_that("compare_parameters, print_md-5", {
205+
skip_if_not_installed("lme4")
206+
skip_if_not_installed("knitr")
207+
skip_if_not_installed("tinytable")
208+
209+
data(sleepstudy, package = "lme4")
210+
set.seed(1234)
211+
sleepstudy$grp <- as.factor(sample.int(3, nrow(sleepstudy), replace = TRUE))
212+
lm1 <- lme4::lmer(Reaction ~ Days + grp + (1 | Subject), data = sleepstudy)
213+
lm2 <- lme4::lmer(Reaction ~ Days * grp + (1 | Subject), data = sleepstudy)
214+
163215
# with reference level
164216
cp <- compare_parameters(lm1, lm2, drop = "^\\(Intercept", include_reference = TRUE)
165217
out <- print_md(cp, groups = list(
@@ -173,8 +225,8 @@ withr::with_options(
173225
cp <- compare_parameters(lm1, lm2, select = "{estimate} ({ci})|{p}", drop = "^\\(Intercept")
174226
expect_error(
175227
print_md(cp, groups = list(
176-
Groups = c("grp (2)", "grp (3)"),
177-
Interactions = c("Days * grp (2)", "Days * grp (3)"),
228+
Groups = c(2, 3),
229+
Interactions = c(4, 5),
178230
Controls = "XDays"
179231
)),
180232
regex = "Some group indices"
@@ -191,19 +243,19 @@ withr::with_options(
191243
# output identical for both calls
192244
cp1 <- compare_parameters(lm1, lm2, select = "{estimate} ({ci})|{p}", drop = "^\\(Intercept")
193245
out1 <- capture.output(print_md(cp1, groups = list(
194-
Groups = c("grp (2)", "grp (3)"),
195-
Interactions = c("Days * grp (2)", "Days * grp (3)"),
196-
Controls = "Days"
246+
Groups = 2:3,
247+
Interactions = 4:5,
248+
Controls = 1
197249
)))
198250
cp2 <- compare_parameters(
199251
lm1,
200252
lm2,
201253
select = "{estimate} ({ci})|{p}",
202254
drop = "^\\(Intercept",
203255
groups = list(
204-
Groups = c("grp (2)", "grp (3)"),
205-
Interactions = c("Days * grp (2)", "Days * grp (3)"),
206-
Controls = "Days"
256+
Groups = c(2, 3),
257+
Interactions = c(4, 5),
258+
Controls = 1
207259
)
208260
)
209261
out2 <- capture.output(print_md(cp2))

0 commit comments

Comments
 (0)