Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: modelbased
Title: Estimation of Model-Based Predictions, Contrasts and Means
Version: 0.17.0.9
Version: 0.17.0.10
Authors@R:
c(person(given = "Dominique",
family = "Makowski",
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
* `comparison = "ratio"` is a new shortcut to compute pairwise comparisons
as ratios.

* The printed footer of `estimate_prediction()` and similar functions now
names prediction intervals as such (#485).

## Bug fixes

* Fixed issue in `estimate_contrasts()` with wrong assignment of estimates in
Expand Down
11 changes: 10 additions & 1 deletion R/estimate_predicted.R
Original file line number Diff line number Diff line change
Expand Up @@ -591,12 +591,21 @@ estimate_relation <- function(
attr(out, "coef_name") <- "Predicted"
attr(out, "model_info") <- insight::model_info(model, response = 1)
attr(out, "predict") <- predict
# for the prediction-interval line in the footer, we use the type of
# predictions that insight actually used, because insight may change
# "prediction" into "expectation" for some models
footer_predict <- attr(predictions, "predict", exact = TRUE)
if (is.null(footer_predict)) {
footer_predict <- predict
}
footer_info <- c(grid_specs, list(predict = predict), transform = !is.null(transform))
footer_info$predict_used <- footer_predict
attr(out, "table_footer") <- .table_footer(
out,
by = grid_specs$at,
type = "predictions",
model = model,
info = c(grid_specs, list(predict = predict), transform = !is.null(transform))
info = footer_info
)

attributes(out) <- c(
Expand Down
10 changes: 10 additions & 0 deletions R/table_footer.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,16 @@
}
}

# tell user about prediction intervals -------------------------------------

if (
identical(type, "predictions") &&
identical(info$predict_used, "prediction") &&
"CI_low" %in% colnames(x)
) {
table_footer <- paste0(table_footer, "\nIntervals are prediction intervals.")
Comment thread
jmgirard marked this conversation as resolved.
}

# Parameter labels for special hypothesis testing --------------------------

# for special hypothesis testing, like "(b1 - b2) = (b4 - b3)", we want to
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/_snaps/brms.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

Variable predicted: rt, response
Predictions are on the response-scale.
Intervals are prediction intervals.

---

Expand Down Expand Up @@ -53,4 +54,5 @@

Variable predicted: rt, response
Predictions are on the response-scale.
Intervals are prediction intervals.

47 changes: 47 additions & 0 deletions tests/testthat/test-table_footer.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,50 @@ test_that("table_footer", {
out <- utils::capture.output(estimate_means(m, "c172code", estimate = "average", verbose = FALSE))
expect_length(out, 10)
})


test_that("table_footer, prediction intervals", {
pi_line <- "Intervals are prediction intervals."
m_lm <- lm(mpg ~ wt, data = mtcars)
m_glm <- glm(vs ~ wt, data = mtcars, family = binomial())

out <- utils::capture.output(estimate_prediction(m_lm, data = "grid", length = 3))
expect_identical(sum(out == pi_line), 1L)
out <- utils::capture.output(estimate_prediction(m_glm, data = "grid", length = 3))
expect_identical(sum(out == pi_line), 1L)
out <- utils::capture.output(estimate_relation(m_lm, predict = "prediction", length = 3))
expect_identical(sum(out == pi_line), 1L)

# no intervals, no line
out <- utils::capture.output(estimate_prediction(m_lm, data = "grid", length = 3, ci = NULL))
expect_false(pi_line %in% out)

# no prediction intervals
for (m in list(m_lm, m_glm)) {
out <- utils::capture.output(estimate_expectation(m, data = "grid", length = 3))
expect_false(pi_line %in% out)
out <- utils::capture.output(estimate_relation(m, length = 3))
expect_false(pi_line %in% out)
out <- utils::capture.output(estimate_link(m, length = 3))
expect_false(pi_line %in% out)
}

skip_if_not_installed("lme4")
m_lmer <- lme4::lmer(mpg ~ wt + (1 | cyl), data = mtcars)
out <- utils::capture.output(estimate_prediction(m_lmer, data = "grid", length = 3))
expect_identical(sum(out == pi_line), 1L)
})


test_that("table_footer, no prediction intervals if insight falls back", {
skip_if_not_installed("mgcv")
m <- mgcv::gam(mpg ~ s(wt), data = mtcars)
# insight returns expectations for gam-models, even if prediction is requested
predict_type <- attr(
suppressWarnings(insight::get_predicted(m, predict = "prediction", ci = 0.95)),
"predict"
)
skip_if(identical(predict_type, "prediction"))
out <- utils::capture.output(suppressWarnings(estimate_prediction(m, data = "grid", length = 3)))
expect_false("Intervals are prediction intervals." %in% out)
})
Loading