Skip to content

Commit 3b0ec8f

Browse files
committed
fixes with printing
1 parent 77e3b62 commit 3b0ec8f

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: modelbased
33
Title: Estimation of Model-Based Predictions, Contrasts and Means
4-
Version: 0.14.0.4
4+
Version: 0.14.0.5
55
Authors@R:
66
c(person(given = "Dominique",
77
family = "Makowski",

R/format.R

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ format.estimate_contrasts <- function(
1616
}
1717
# change parameter name for context effects
1818
if (isTRUE(attributes(x)$context_effects)) {
19-
x$Parameter <- "Average slope"
19+
x$Parameter <- NULL
2020
}
2121

2222
# don't print columns of adjusted_for variables
@@ -174,7 +174,14 @@ format.marginaleffects_means <- function(x, model, ci = 0.95, ...) {
174174
)
175175
# for inequality analysis, we want to keep the stratification variable
176176
remove_columns <- setdiff(remove_columns, attributes(x)$hypothesis_by)
177-
} else if (is_contrast_analysis || context_effects) {
177+
} else if (context_effects) {
178+
# for contrasting average slopes (context effects), the estimate might be
179+
# an odds ratio or IRR by default - thus, we want to keep those names
180+
estimate_name <- .guess_estimate_name(predict_type, transform, info)
181+
if (identical(estimate_name, "Mean")) {
182+
estimate_name <- "Difference"
183+
}
184+
} else if (is_contrast_analysis) {
178185
estimate_name <- "Difference"
179186
} else {
180187
# for simple means, we don't want p-values

R/table_footer.R

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,23 @@
117117
`invlink(link)` = "response",
118118
predict
119119
)
120-
table_footer <- paste0(
121-
table_footer,
122-
"\n",
123-
result_type,
124-
" are on the ",
125-
predict,
126-
"-scale."
127-
)
120+
# we may have link-scale and transformation. to avoid confusion, tell this
121+
# here
122+
if (predict == "link" && !is.null(transform)) {
123+
predict <- paste0(transform, "-transformed ", predict)
124+
}
125+
# if we have contrasts of slope for GLMs, the default is to calculate
126+
# ORs or IRRs - in this case, we don't add information about the scale
127+
if (!any(c("Odds_Ratio", "IRR") %in% colnames(x))) {
128+
table_footer <- paste0(
129+
table_footer,
130+
"\n",
131+
result_type,
132+
" are on the ",
133+
predict,
134+
"-scale."
135+
)
136+
}
128137
} else if (isTRUE(model_info$is_linear) && !isTRUE(transform)) {
129138
# add information about response transformation
130139
trans_fun <- .safe(insight::find_transformation(model))

tests/testthat/test-estimate_contrasts_context.R

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ test_that("estimate_contrast, context effects, linear", {
1212
b <- coef(summary(m))[2:3, 1]
1313
se <- coef(summary(m))[2:3, 2]
1414

15-
out <- modelbased::estimate_contrasts(
15+
out <- estimate_contrasts(
1616
m,
1717
c("bill_len_between", "bill_len_within"),
1818
comparison = "context"
1919
)
20-
expect_equal(out$Mean, b[1] - b[2], tolerance = 1e-4, ignore_attr = TRUE)
20+
expect_equal(out$Difference, b[1] - b[2], tolerance = 1e-4, ignore_attr = TRUE)
2121
expect_equal(out$SE, sqrt((se[1]^2 + se[2]^2)), tolerance = 1e-4, ignore_attr = TRUE)
2222
expect_true(!is.null(out$p))
23+
24+
output <- capture.output(out)
25+
expect_identical(output[3], "Difference | SE | 95% CI | z | p")
2326
})
2427

2528
test_that("estimate_contrast, context effects, glm", {
@@ -31,23 +34,26 @@ test_that("estimate_contrast, context effects, glm", {
3134
b <- coef(summary(m))[2:3, 1]
3235
se <- coef(summary(m))[2:3, 2]
3336

34-
out <- modelbased::estimate_contrasts(
37+
out <- estimate_contrasts(
3538
m,
3639
c("bill_len_between", "bill_len_within"),
3740
comparison = "context"
3841
)
3942
expect_equal(out$Odds_Ratio, exp(b[1] - b[2]), tolerance = 1e-4, ignore_attr = TRUE)
4043
expect_true(!is.null(out$p))
4144

42-
out <- modelbased::estimate_contrasts(
45+
output <- capture.output(out)
46+
expect_identical(output[3], "Odds_Ratio | 95% CI | p")
47+
48+
out <- estimate_contrasts(
4349
m,
4450
c("bill_len_between", "bill_len_within"),
4551
comparison = "slope"
4652
)
4753
expect_equal(out$Odds_Ratio, exp(b[1] - b[2]), tolerance = 1e-4, ignore_attr = TRUE)
4854
expect_true(!is.null(out$p))
4955

50-
out <- modelbased::estimate_contrasts(
56+
out <- estimate_contrasts(
5157
m,
5258
c("bill_len_between", "bill_len_within"),
5359
comparison = "context",

0 commit comments

Comments
 (0)