Skip to content

Commit

Permalink
brms: wrong parameters info
Browse files Browse the repository at this point in the history
Fixes #1058
  • Loading branch information
strengejacke committed Jan 7, 2025
1 parent 9b7c53b commit c59c087
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 15 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`.
Expand Down
3 changes: 3 additions & 0 deletions R/extract_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,9 @@
}

rownames(parameters) <- NULL
# indicate it's a Bayesian model
attr(parameters, "is_bayesian") <- TRUE

parameters
}

Expand Down
3 changes: 3 additions & 0 deletions R/format.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
74 changes: 60 additions & 14 deletions tests/testthat/_snaps/printing-stan.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

---

Expand Down Expand Up @@ -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.

---

Expand Down Expand Up @@ -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.

---

Expand Down Expand Up @@ -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.

---

Expand All @@ -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.

---

Expand Down Expand Up @@ -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.

---

Expand Down Expand Up @@ -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.

13 changes: 13 additions & 0 deletions tests/testthat/test-printing-stan.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
)

0 comments on commit c59c087

Please sign in to comment.