From c59c087463686c38cfe2923afda46603dbe39c20 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 7 Jan 2025 14:30:59 +0100 Subject: [PATCH] brms: wrong parameters info Fixes #1058 --- DESCRIPTION | 2 +- NEWS.md | 3 ++ R/extract_parameters.R | 3 ++ R/format.R | 3 ++ tests/testthat/_snaps/printing-stan.md | 74 +++++++++++++++++++++----- tests/testthat/test-printing-stan.R | 13 +++++ 6 files changed, 83 insertions(+), 15 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 3c4b622b2..3b3cddc19 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: parameters Title: Processing of Model Parameters -Version: 0.24.0.6 +Version: 0.24.0.7 Authors@R: c(person(given = "Daniel", family = "Lüdecke", diff --git a/NEWS.md b/NEWS.md index 4f179d778..ffe502d2c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,9 @@ * `model_parameters()` now gives informative error messages for more model classes than before when the function fails to extract model parameters. +* Improved information for credible intervals and sampling method from output + of `model_parameters()` for Bayesian models. + ## Bug fixes * Fixed issue when printing `model_parameters()` with models from `mgcv::gam()`. diff --git a/R/extract_parameters.R b/R/extract_parameters.R index 98450ad8d..c1a36da67 100644 --- a/R/extract_parameters.R +++ b/R/extract_parameters.R @@ -839,6 +839,9 @@ } rownames(parameters) <- NULL + # indicate it's a Bayesian model + attr(parameters, "is_bayesian") <- TRUE + parameters } diff --git a/R/format.R b/R/format.R index cb25e421b..4fa952b38 100644 --- a/R/format.R +++ b/R/format.R @@ -747,6 +747,7 @@ format.parameters_sem <- function(x, ci_method <- .additional_arguments(x, "ci_method", NULL) test_statistic <- .additional_arguments(x, "test_statistic", NULL) bootstrap <- .additional_arguments(x, "bootstrap", FALSE) + is_bayesian <- .additional_arguments(x, "is_bayesian", FALSE) simulated <- .additional_arguments(x, "simulated", FALSE) residual_df <- .additional_arguments(x, "residual_df", NULL) random_variances <- .additional_arguments(x, "ran_pars", FALSE) @@ -825,6 +826,8 @@ format.parameters_sem <- function(x, # bootstrapped intervals if (isTRUE(bootstrap)) { msg <- paste0("\nUncertainty intervals (", string_tailed, ") are ", string_method, "intervals.") + } else if (isTRUE(is_bayesian)) { + msg <- paste0("\nUncertainty intervals (", string_tailed, ") computed using a ", string_method, "distribution ", string_approx, "approximation.") # nolint } else { msg <- paste0("\nUncertainty intervals (", string_tailed, ") and p-values (two-tailed) computed using a ", string_method, "distribution ", string_approx, "approximation.") # nolint } diff --git a/tests/testthat/_snaps/printing-stan.md b/tests/testthat/_snaps/printing-stan.md index fc13cd1fc..9b1281746 100644 --- a/tests/testthat/_snaps/printing-stan.md +++ b/tests/testthat/_snaps/printing-stan.md @@ -18,8 +18,8 @@ sigma | 2.67 | [2.06, 3.51] | 100% | 1.000 | 2390.00 Message - Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed - using a MCMC distribution approximation. + Uncertainty intervals (equal-tailed) computed using a MCMC distribution + approximation. --- @@ -49,8 +49,8 @@ Cor (Intercept~wt: gear) | -0.25 | [-0.99, 0.83] | 62.48% | 1.106 | 36.00 Message - Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed - using a MCMC distribution approximation. + Uncertainty intervals (equal-tailed) computed using a MCMC distribution + approximation. --- @@ -79,8 +79,8 @@ Cor (Intercept~Days: Subject) | 0.09 | [-0.47, 0.67] | 60.42% | 1.003 | 899.00 Message - Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed - using a MCMC distribution approximation. + Uncertainty intervals (equal-tailed) computed using a MCMC distribution + approximation. --- @@ -109,8 +109,8 @@ SD (Intercept: Subject) | 38.51 | [26.89, 55.98] | 100% | 1.003 | 1254.00 Message - Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed - using a MCMC distribution approximation. + Uncertainty intervals (equal-tailed) computed using a MCMC distribution + approximation. --- @@ -137,8 +137,8 @@ SD (Intercept: Species) | 1.68 | [0.64, 3.64] | 100% | 1.003 | 796.00 Message - Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed - using a MCMC distribution approximation. + Uncertainty intervals (equal-tailed) computed using a MCMC distribution + approximation. --- @@ -168,8 +168,8 @@ Cor (Intercept~wt: gear) | -0.38 | [-0.99, 0.82] | 76.85% | 1.003 | 854.00 Message - Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed - using a MCMC distribution approximation. + Uncertainty intervals (equal-tailed) computed using a MCMC distribution + approximation. --- @@ -238,6 +238,52 @@ mpg | -1.80 | [ -3.20, -0.90] | 100% | 1.002 | 1021.00 Message - Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed - using a MCMC distribution approximation. + Uncertainty intervals (equal-tailed) computed using a MCMC distribution + approximation. + +# print-information + + Code + out + Output + # Fixed Effects + + Parameter | Median | 95% CI | pd | Rhat | ESS + ---------------------------------------------------------------- + (Intercept) | 39.68 | [36.12, 43.27] | 100% | 1.000 | 5242.00 + wt | -3.20 | [-4.79, -1.65] | 99.95% | 1.000 | 2071.00 + cyl | -1.49 | [-2.36, -0.64] | 99.95% | 1.000 | 1951.00 + + # Sigma + + Parameter | Median | 95% CI | pd | Rhat | ESS + ---------------------------------------------------------- + sigma | 2.63 | [2.06, 3.51] | 100% | 1.000 | 2390.00 + Message + + Uncertainty intervals (equal-tailed) computed using a MCMC distribution + approximation. + +--- + + Code + out + Output + # Fixed Effects + + Parameter | Median | 95% CI | pd | Rhat | ESS + ---------------------------------------------------------------- + (Intercept) | 39.68 | [36.27, 43.34] | 100% | 1.000 | 5242.00 + wt | -3.20 | [-4.70, -1.57] | 99.95% | 1.000 | 2071.00 + cyl | -1.49 | [-2.38, -0.68] | 99.95% | 1.000 | 1951.00 + + # Sigma + + Parameter | Median | 95% CI | pd | Rhat | ESS + ---------------------------------------------------------- + sigma | 2.63 | [1.99, 3.39] | 100% | 1.000 | 2390.00 + Message + + Uncertainty intervals (highest-density) computed using a MCMC + distribution approximation. diff --git a/tests/testthat/test-printing-stan.R b/tests/testthat/test-printing-stan.R index 4fa16d2ed..107aa73ff 100644 --- a/tests/testthat/test-printing-stan.R +++ b/tests/testthat/test-printing-stan.R @@ -45,5 +45,18 @@ withr::with_options( mp9 <- model_parameters(m9, effects = "all", component = "all", centrality = "mean") expect_snapshot(mp9) }) + + test_that("print-information", { + skip_if_offline() + skip_if_not_installed("httr2") + + m <- insight::download_model("brms_1") + out <- model_parameters(m) + expect_snapshot(out) + out <- model_parameters(m, ci_method = "HDI") + expect_snapshot(out) + m <- insight::download_model("stanreg_glm_1") + out <- model_parameters(m) + }) } )