@@ -7,7 +7,7 @@ skip_if_not_installed("datawizard")
77
88
99test_that(" estimate_contrast, marginal effects inequalities" , {
10- data(penguins )
10+ data(penguins , package = " datasets " )
1111 penguins $ long_bill <- factor (
1212 datawizard :: categorize(penguins $ bill_len ),
1313 labels = c(" short" , " long" )
@@ -88,14 +88,22 @@ test_that("estimate_contrast, marginal effects inequalities", {
8888 tolerance = 1e-4 ,
8989 ignore_attr = TRUE
9090 )
91- expect_named(out , c(" Parameter" , " Mean_Difference" , " SE" , " CI_low" , " CI_high" , " z" , " p" ))
91+ expect_named(
92+ out ,
93+ c(" Parameter" , " Mean_Difference" , " SE" , " CI_low" , " CI_high" , " z" , " p" )
94+ )
9295 expect_identical(
9396 out $ Parameter ,
9497 c(" Biscoe - Dream" , " Biscoe - Torgersen" , " Dream - Torgersen" )
9598 )
9699
97100 # pairwise inequality comparisons with `by`
98- out <- estimate_contrasts(m , " species" , by = " island" , comparison = " inequality_pairwise" )
101+ out <- estimate_contrasts(
102+ m ,
103+ " species" ,
104+ by = " island" ,
105+ comparison = " inequality_pairwise"
106+ )
99107 expect_equal(
100108 out $ Mean_Difference ,
101109 c(0.127399 , 0.000135 , - 0.127264 ),
@@ -110,7 +118,11 @@ test_that("estimate_contrast, marginal effects inequalities", {
110118 )
111119
112120 # check formula interface for grouping
113- m <- glm(long_bill ~ species * sex * island + bill_dep , data = penguins , family = " binomial" )
121+ m <- glm(
122+ long_bill ~ species * sex * island + bill_dep ,
123+ data = penguins ,
124+ family = " binomial"
125+ )
114126 out <- suppressWarnings(estimate_contrasts(
115127 m ,
116128 " species" ,
@@ -153,7 +165,11 @@ test_that("estimate_contrast, marginal effects inequalities", {
153165 expect_identical(dim(out ), c(60L , 9L ))
154166
155167 # contrasting slopes
156- m <- glm(long_bill ~ sex + species + island * bill_dep , data = penguins , family = " binomial" )
168+ m <- glm(
169+ long_bill ~ sex + species + island * bill_dep ,
170+ data = penguins ,
171+ family = " binomial"
172+ )
157173 out <- estimate_contrasts(
158174 m ,
159175 " bill_dep" ,
@@ -183,15 +199,25 @@ test_that("estimate_contrast, marginal effects inequalities", {
183199
184200
185201test_that(" estimate_contrast, inequality ratios" , {
186- data(penguins )
202+ data(penguins , package = " datasets " )
187203 m <- lm(bill_len ~ species * bill_dep + island , data = penguins )
188204
189205 # pairwise ratios for slopes
190206 out <- estimate_contrasts(m , " bill_dep" , by = " species" , comparison = ratio ~ pairwise )
191- expect_equal(out $ Ratio , c(2.262453 , 2.378612 , 1.051342 ), tolerance = 1e-4 , ignore_attr = TRUE )
207+ expect_equal(
208+ out $ Ratio ,
209+ c(2.262453 , 2.378612 , 1.051342 ),
210+ tolerance = 1e-4 ,
211+ ignore_attr = TRUE
212+ )
192213
193214 # inequality ratios for slopes
194- out <- estimate_contrasts(m , " bill_dep" , by = " species" , comparison = ratio ~ inequality )
215+ out <- estimate_contrasts(
216+ m ,
217+ " bill_dep" ,
218+ by = " species" ,
219+ comparison = ratio ~ inequality
220+ )
195221 expect_equal(out $ Mean_Ratio , 1.897469 , tolerance = 1e-4 , ignore_attr = TRUE )
196222 expect_identical(out $ Parameter , " species" )
197223
@@ -217,8 +243,18 @@ test_that("estimate_contrast, inequality ratios", {
217243 expect_identical(as.character(out $ sex ), c(" female" , " male" ))
218244
219245 # pairwise inequality ratios
220- out <- estimate_contrasts(m , " island" , by = " sex" , comparison = ratio ~ inequality + pairwise )
221- expect_equal(out $ Mean_Ratio_Difference , 0.01355006 , tolerance = 1e-4 , ignore_attr = TRUE )
246+ out <- estimate_contrasts(
247+ m ,
248+ " island" ,
249+ by = " sex" ,
250+ comparison = ratio ~ inequality + pairwise
251+ )
252+ expect_equal(
253+ out $ Mean_Ratio_Difference ,
254+ 0.01355006 ,
255+ tolerance = 1e-4 ,
256+ ignore_attr = TRUE
257+ )
222258})
223259
224260
@@ -232,16 +268,25 @@ test_that("estimate_contrast, slopes, inequality pairwise", {
232268 m <- lm(QoL ~ time * education * grp , data = qol_cancer )
233269
234270 # test integer handling
235- expect_silent(
236- estimate_contrasts(m , " time" , by = " education" , integer_as_continuous = TRUE )
237- )
238- expect_silent(
239- estimate_contrasts(m , " time" , by = " education" , integer_as_continuous = 2 )
240- )
271+ expect_silent(estimate_contrasts(
272+ m ,
273+ " time" ,
274+ by = " education" ,
275+ integer_as_continuous = TRUE
276+ ))
277+ expect_silent(estimate_contrasts(
278+ m ,
279+ " time" ,
280+ by = " education" ,
281+ integer_as_continuous = 2
282+ ))
241283 # we also show no warning when user explicitly sets integer_as_continuous
242- expect_silent(
243- estimate_contrasts(m , " time" , by = " education" , integer_as_continuous = 10 )
244- )
284+ expect_silent(estimate_contrasts(
285+ m ,
286+ " time" ,
287+ by = " education" ,
288+ integer_as_continuous = 10
289+ ))
245290 expect_message(
246291 estimate_contrasts(m , " time" , by = " education" ),
247292 regex = " Numeric variable appears to be ordinal"
@@ -268,15 +313,15 @@ test_that("estimate_contrast, slopes, inequality pairwise", {
268313 estimate = " average"
269314 )
270315 out2 <- marginaleffects :: hypotheses(
271- marginaleffects :: avg_slopes(m , variables = " time" , by = " education" , hypothesis = ~ pairwise ),
272- hypothesis = ~ I(mean(abs(x )))
273- )
274- expect_equal(
275- out1 $ Mean_Difference ,
276- out2 $ estimate ,
277- tolerance = 1e-4 ,
278- ignore_attr = TRUE
316+ marginaleffects :: avg_slopes(
317+ m ,
318+ variables = " time" ,
319+ by = " education" ,
320+ hypothesis = ~ pairwise
321+ ),
322+ hypothesis = ~ I(mean(abs(x )))
279323 )
324+ expect_equal(out1 $ Mean_Difference , out2 $ estimate , tolerance = 1e-4 , ignore_attr = TRUE )
280325
281326 # inequality with slopes and grouping
282327 out <- estimate_contrasts(
@@ -286,29 +331,39 @@ test_that("estimate_contrast, slopes, inequality pairwise", {
286331 comparison = ~ inequality ,
287332 integer_as_continuous = TRUE
288333 )
289- expect_equal(out $ Mean_Difference , c(4.742403 , 2.883987 ), tolerance = 1e-4 , ignore_attr = TRUE )
334+ expect_equal(
335+ out $ Mean_Difference ,
336+ c(4.742403 , 2.883987 ),
337+ tolerance = 1e-4 ,
338+ ignore_attr = TRUE
339+ )
290340 expect_identical(out $ Parameter , c(" education: Group 1" , " education: Group 2" ))
291341
292342 # inequality with slopes and grouping, using formula interface
293343 out <- estimate_contrasts(
294344 m ,
295345 " time" ,
296346 by = c(" education" , " grp" ),
297- comparison = ~ inequality | grp ,
347+ comparison = ~ inequality | grp ,
298348 integer_as_continuous = TRUE
299349 )
300- expect_equal(out $ Mean_Difference , c(4.742403 , 2.883987 ), tolerance = 1e-4 , ignore_attr = TRUE )
350+ expect_equal(
351+ out $ Mean_Difference ,
352+ c(4.742403 , 2.883987 ),
353+ tolerance = 1e-4 ,
354+ ignore_attr = TRUE
355+ )
301356 expect_identical(out $ Parameter , c(" education: Group 1" , " education: Group 2" ))
302357
303358 # pairwise inequality with slopes and grouping
304359 out <- estimate_contrasts(
305360 m ,
306361 " time" ,
307362 by = c(" education" , " grp" ),
308- comparison = inequality ~ pairwise | grp ,
363+ comparison = inequality ~ pairwise | grp ,
309364 integer_as_continuous = TRUE
310365 )
311- expect_equal(out $ Mean_Difference , 1.858416 , tolerance = 1e-4 , ignore_attr = TRUE )
366+ expect_equal(out $ Mean_Difference , 1.858416 , tolerance = 1e-4 , ignore_attr = TRUE )
312367 expect_identical(out $ Parameter , " Group 1 - Group 2" )
313368
314369 # validate against marginaleffects
@@ -321,15 +376,15 @@ test_that("estimate_contrast, slopes, inequality pairwise", {
321376 integer_as_continuous = TRUE
322377 )
323378 out2 <- marginaleffects :: hypotheses(
324- marginaleffects :: avg_slopes(m , variables = " time" , by = c(" education" , " grp" ), hypothesis = ~ pairwise | grp ),
325- hypothesis = ~ I(mean(abs(x ))) | grp
326- )
327- expect_equal(
328- out1 $ Mean_Difference ,
329- out2 $ estimate ,
330- tolerance = 1e-4 ,
331- ignore_attr = TRUE
379+ marginaleffects :: avg_slopes(
380+ m ,
381+ variables = " time" ,
382+ by = c(" education" , " grp" ),
383+ hypothesis = ~ pairwise | grp
384+ ),
385+ hypothesis = ~ I(mean(abs(x ))) | grp
332386 )
387+ expect_equal(out1 $ Mean_Difference , out2 $ estimate , tolerance = 1e-4 , ignore_attr = TRUE )
333388
334389 # pairwise inequality with slopes and grouping, using string
335390 out <- estimate_contrasts(
@@ -359,7 +414,12 @@ test_that("estimate_contrast, slopes, inequality pairwise", {
359414 comparison = ratio ~ inequality ,
360415 integer_as_continuous = TRUE
361416 )
362- expect_equal(out $ Mean_Ratio , c(0.0198939 , 1.9717087 ), tolerance = 1e-4 , ignore_attr = TRUE )
417+ expect_equal(
418+ out $ Mean_Ratio ,
419+ c(0.0198939 , 1.9717087 ),
420+ tolerance = 1e-4 ,
421+ ignore_attr = TRUE
422+ )
363423 expect_identical(out $ Parameter , c(" education: Group 1" , " education: Group 2" ))
364424
365425 out <- estimate_contrasts(
@@ -375,12 +435,12 @@ test_that("estimate_contrast, slopes, inequality pairwise", {
375435 m ,
376436 " time" ,
377437 by = c(" education" , " grp" ),
378- comparison = ~ pairwise | grp ,
438+ comparison = ~ pairwise | grp ,
379439 integer_as_continuous = TRUE
380440 )
381441 expect_identical(dim(out ), c(6L , 9L ))
382442 expect_named(
383443 out ,
384- c(" Parameter" , " grp" , " Difference" , " SE" , " CI_low" , " CI_high" , " t" , " df" , " p" )
444+ c(" Parameter" , " grp" , " Difference" , " SE" , " CI_low" , " CI_high" , " t" , " df" , " p" )
385445 )
386446})
0 commit comments