@@ -52,8 +52,16 @@ test_that("marginaleffects()", {
5252 expect_named(
5353 out ,
5454 c(
55- " Parameter" , " Comparison" , " Coefficient" , " SE" , " Statistic" ,
56- " p" , " S" , " CI" , " CI_low" , " CI_high"
55+ " Parameter" ,
56+ " Comparison" ,
57+ " Coefficient" ,
58+ " SE" ,
59+ " Statistic" ,
60+ " p" ,
61+ " S" ,
62+ " CI" ,
63+ " CI_low" ,
64+ " CI_high"
5765 )
5866 )
5967 mfx <- marginaleffects :: avg_slopes(model , variables = " Petal.Length" , by = " Species" )
@@ -62,8 +70,17 @@ test_that("marginaleffects()", {
6270 expect_named(
6371 out ,
6472 c(
65- " Parameter" , " Comparison" , " Species" , " Coefficient" , " SE" , " Statistic" ,
66- " p" , " S" , " CI" , " CI_low" , " CI_high"
73+ " Parameter" ,
74+ " Comparison" ,
75+ " Species" ,
76+ " Coefficient" ,
77+ " SE" ,
78+ " Statistic" ,
79+ " p" ,
80+ " S" ,
81+ " CI" ,
82+ " CI_low" ,
83+ " CI_high"
6784 )
6885 )
6986})
@@ -74,10 +91,21 @@ test_that("predictions()", {
7491 p <- marginaleffects :: avg_predictions(x , by = " Species" )
7592 out <- model_parameters(p )
7693 expect_identical(nrow(out ), 3L )
77- expect_named(out , c(
78- " Predicted" , " Species" , " SE" , " CI" , " CI_low" , " CI_high" , " S" , " Statistic" ,
79- " df" , " p"
80- ))
94+ expect_named(
95+ out ,
96+ c(
97+ " Predicted" ,
98+ " Species" ,
99+ " SE" ,
100+ " CI" ,
101+ " CI_low" ,
102+ " CI_high" ,
103+ " S" ,
104+ " Statistic" ,
105+ " df" ,
106+ " p"
107+ )
108+ )
81109 out <- model_parameters(p , exponentiate = TRUE )
82110 expect_equal(out $ Predicted , c(30.81495 , 15.95863 , 19.57004 ), tolerance = 1e-4 )
83111})
@@ -124,16 +152,28 @@ test_that("hypotheses()", {
124152test_that(" slopes()" , {
125153 m <- lm(Sepal.Width ~ Species * Petal.Length , data = iris )
126154
127- x <- marginaleffects :: slopes(m ,
155+ x <- marginaleffects :: slopes(
156+ m ,
128157 variables = " Petal.Length" ,
129158 newdata = insight :: get_datagrid(m , by = " Species" )
130159 )
131160 out <- model_parameters(x )
132161 expect_named(
133162 out ,
134163 c(
135- " rowid" , " Parameter" , " Comparison" , " Coefficient" , " SE" , " Statistic" ,
136- " p" , " S" , " CI" , " CI_low" , " CI_high" , " Species" , " Petal.Length" ,
164+ " rowid" ,
165+ " Parameter" ,
166+ " Comparison" ,
167+ " Coefficient" ,
168+ " SE" ,
169+ " Statistic" ,
170+ " p" ,
171+ " S" ,
172+ " CI" ,
173+ " CI_low" ,
174+ " CI_high" ,
175+ " Species" ,
176+ " Petal.Length" ,
137177 " Predicted"
138178 )
139179 )
@@ -160,7 +200,10 @@ test_that("multiple contrasts: Issue #779", {
160200test_that(" model_parameters defaults to FALSE: Issue #916" , {
161201 data(mtcars )
162202 mod <- lm(mpg ~ wt , data = mtcars )
163- pred <- marginaleffects :: predictions(mod , newdata = marginaleffects :: datagrid(wt = c(1 , 2 )))
203+ pred <- marginaleffects :: predictions(
204+ mod ,
205+ newdata = marginaleffects :: datagrid(wt = c(1 , 2 ))
206+ )
164207 out1 <- model_parameters(pred )
165208 out2 <- model_parameters(pred , exponentiate = FALSE )
166209 expect_equal(out1 $ Predicted , out2 $ Predicted , tolerance = 1e-4 )
@@ -206,7 +249,11 @@ test_that("predictions, bmrs with special response formula", {
206249 m <- insight :: download_model(" brms_ipw_1" )
207250 skip_if(is.null(m ))
208251
209- x <- marginaleffects :: avg_predictions(m , variables = " treatment" , hypothesis = ~ pairwise )
252+ x <- marginaleffects :: avg_predictions(
253+ m ,
254+ variables = " treatment" ,
255+ hypothesis = ~ pairwise
256+ )
210257 out <- model_parameters(x )
211258 expect_identical(dim(out ), c(1L , 10L ))
212259})
@@ -220,7 +267,12 @@ test_that("modelbased, tidiers work", {
220267 data(penguins )
221268 m <- lm(bill_len ~ island * sex + bill_dep + species , data = penguins )
222269
223- out <- modelbased :: estimate_contrasts(m , " island" , by = " sex" , comparison = ratio ~ pairwise )
270+ out <- modelbased :: estimate_contrasts(
271+ m ,
272+ " island" ,
273+ by = " sex" ,
274+ comparison = ratio ~ pairwise
275+ )
224276 expect_named(
225277 out ,
226278 c(" Level1" , " Level2" , " sex" , " Ratio" , " SE" , " CI_low" , " CI_high" , " t" , " df" , " p" )
@@ -238,13 +290,27 @@ test_that("modelbased, tidiers work", {
238290 expect_named(
239291 params ,
240292 c(
241- " Parameter" , " Predicted" , " SE" , " CI" , " CI_low" , " CI_high" ,
242- " S" , " Statistic" , " df" , " p" , " sex"
293+ " Parameter" ,
294+ " Predicted" ,
295+ " SE" ,
296+ " CI" ,
297+ " CI_low" ,
298+ " CI_high" ,
299+ " S" ,
300+ " Statistic" ,
301+ " df" ,
302+ " p" ,
303+ " sex"
243304 )
244305 )
245306 expect_identical(dim(params ), c(6L , 11L ))
246307
247- out <- modelbased :: estimate_contrasts(m , " island" , by = " sex" , comparison = ratio ~ inequality )
308+ out <- modelbased :: estimate_contrasts(
309+ m ,
310+ " island" ,
311+ by = " sex" ,
312+ comparison = ratio ~ inequality
313+ )
248314 expect_named(out , c(" sex" , " Mean_Ratio" , " SE" , " CI_low" , " CI_high" , " z" , " p" ))
249315 expect_identical(dim(out ), c(2L , 7L ))
250316
@@ -255,7 +321,7 @@ test_that("modelbased, tidiers work", {
255321 newdata = datagrid ,
256322 hypothesis = ratio ~ pairwise | sex
257323 )
258- out <- marginaleffects :: hypotheses(out , hypothesis = ~ I(mean(abs(x ))) | sex )
324+ out <- marginaleffects :: hypotheses(out , hypothesis = ~ I(mean(abs(x ))) | sex )
259325 params <- model_parameters(out )
260326 expect_named(
261327 params ,
@@ -291,13 +357,61 @@ test_that("predictions, using bayestestR #1063", {
291357 skip_if(is.null(m ))
292358
293359 d <- insight :: get_datagrid(m , by = " Days" , include_random = TRUE )
294- x <- marginaleffects :: avg_predictions(m , newdata = d , by = " Days" , allow_new_levels = TRUE )
360+ x <- marginaleffects :: avg_predictions(
361+ m ,
362+ newdata = d ,
363+ by = " Days" ,
364+ allow_new_levels = TRUE
365+ )
295366 out <- model_parameters(x )
296367 expect_named(
297368 out ,
298369 c(
299- " Median" , " CI" , " CI_low" , " CI_high" , " pd" , " ROPE_CI" , " ROPE_low" ,
300- " ROPE_high" , " ROPE_Percentage" , " Days"
370+ " Median" ,
371+ " CI" ,
372+ " CI_low" ,
373+ " CI_high" ,
374+ " pd" ,
375+ " ROPE_CI" ,
376+ " ROPE_low" ,
377+ " ROPE_high" ,
378+ " ROPE_Percentage" ,
379+ " Days"
380+ )
381+ )
382+ })
383+
384+
385+ test_that(" predictions, don't remove Type column when it's a variable" , {
386+ skip_on_cran()
387+ skip_if_not_installed(" MASS" )
388+
389+ data(housing , package = " MASS" )
390+ m1 <- MASS :: polr(Sat ~ Infl + Type + Cont , data = housing , weights = Freq )
391+ emm <- marginaleffects :: avg_predictions(m1 , by = " Type" )
392+ out <- model_parameters(emm )
393+
394+ expect_named(
395+ out ,
396+ c(
397+ " Predicted" ,
398+ " Type" ,
399+ " SE" ,
400+ " CI" ,
401+ " CI_low" ,
402+ " CI_high" ,
403+ " S" ,
404+ " Statistic" ,
405+ " df" ,
406+ " p" ,
407+ " group"
301408 )
302409 )
410+
411+ skip_if_not_installed(" modelbased" )
412+ out <- modelbased :: estimate_means(m1 , by = " Type" )
413+ expect_named(
414+ out ,
415+ c(" Type" , " Response" , " Probability" , " SE" , " CI_low" , " CI_high" , " t" , " df" )
416+ )
303417})
0 commit comments