Skip to content

Commit f8e8789

Browse files
authored
Inhouse-processing of parameter-tidying (#447)
* Inhouse-processing of parameter-tidying * no nested if-sztatement * Update DESCRIPTION * Update DESCRIPTION * Update ordinal.md * Update test-estimate_grouplevel.R * Update DESCRIPTION
1 parent 8c78ce0 commit f8e8789

File tree

4 files changed

+48
-59
lines changed

4 files changed

+48
-59
lines changed

DESCRIPTION

+2-1
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.10.0.2
4+
Version: 0.10.0.3
55
Authors@R:
66
c(person(given = "Dominique",
77
family = "Makowski",
@@ -102,3 +102,4 @@ Roxygen: list(markdown = TRUE)
102102
Config/Needs/check: stan-dev/cmdstanr
103103
Config/Needs/website: easystats/easystatstemplate
104104
LazyData: true
105+
Remotes: easystats/bayestestR, easystats/parameters

R/format.R

+13-25
Original file line numberDiff line numberDiff line change
@@ -498,31 +498,19 @@ format.marginaleffects_contrasts <- function(x, model = NULL, p_adjust = NULL, c
498498
# coefficient column is named, because we replace that column name with an
499499
# appropriate name of the predictions (e.g. "Difference", "Probability" or
500500
# "Mean")
501-
if (is.null(attributes(x)$posterior_draws)) {
502-
# frequentist
503-
params <- suppressWarnings(parameters::model_parameters(x, ci = ci, verbose = FALSE))
504-
coefficient_name <- intersect(
505-
c(attributes(params)$coefficient_name, "Coefficient", "Slope", "Predicted"),
506-
colnames(params)
507-
)[1]
508-
} else {
509-
# Bayesian
510-
params <- suppressWarnings(bayestestR::describe_posterior(x, ci = ci, verbose = FALSE, ...))
511-
## FIXME: needs to be fixed in bayestestR: categorical models don't return group column
512-
# see https://github.com/easystats/bayestestR/issues/692
513-
if (info$is_categorical) {
514-
params$group <- .safe(x$group)
515-
}
516-
coefficient_name <- intersect(
517-
c(attributes(params)$coefficient_name, "Median", "Mean", "MAP"),
518-
colnames(params)
519-
)[1]
520-
# we need to remove some more columns
521-
remove_columns <- c(remove_columns, "rowid")
522-
# and modify the estimate name - if it's not a dpar
523-
if (!is.null(estimate_name) && !tolower(estimate_name) %in% .brms_aux_elements()) {
524-
estimate_name <- coefficient_name
525-
}
501+
params <- suppressWarnings(parameters::model_parameters(x, ci = ci, verbose = FALSE, ...))
502+
# the different functions and models (Bayesian, frequentist) have different
503+
# column names for their "coefficient". We now extract the relevant one.
504+
possible_colnames <- c(
505+
attributes(params)$coefficient_name,
506+
"Coefficient", "Slope", "Predicted", "Median", "Mean", "MAP"
507+
)
508+
coefficient_name <- intersect(possible_colnames, colnames(params))[1]
509+
# we need to remove some more columns
510+
remove_columns <- c(remove_columns, "rowid")
511+
# and modify the estimate name - if it's not a dpar
512+
if (!is.null(attributes(x)$posterior_draws) && !is.null(estimate_name) && !tolower(estimate_name) %in% .brms_aux_elements()) { # nolint
513+
estimate_name <- coefficient_name
526514
}
527515
# rename the "term" and "hypothesis" column (which we get from contrasts)
528516
colnames(params)[colnames(params) == "term"] <- "Parameter"

tests/testthat/_snaps/windows/ordinal.md

+31-31
Original file line numberDiff line numberDiff line change
@@ -49,38 +49,38 @@
4949
Output
5050
Estimated Marginal Means
5151
52-
Sepal.Width | Response | Median | 95% CI | pd | ROPE | % in ROPE
52+
Sepal.Width | Response | Median | 95% CI | pd | ROPE | % in ROPE
5353
-----------------------------------------------------------------------------------
54-
2.00 | setosa | 0.00 | [0.00, 0.01] | 100% | [-0.10, 0.10] | 100%
55-
2.00 | versicolor | 0.86 | [0.65, 0.95] | 100% | [-0.10, 0.10] | 0%
56-
2.00 | virginica | 0.14 | [0.05, 0.34] | 100% | [-0.10, 0.10] | 28.00%
57-
2.27 | setosa | 0.00 | [0.00, 0.02] | 100% | [-0.10, 0.10] | 100%
58-
2.27 | versicolor | 0.78 | [0.60, 0.90] | 100% | [-0.10, 0.10] | 0%
59-
2.27 | virginica | 0.21 | [0.10, 0.40] | 100% | [-0.10, 0.10] | 0%
60-
2.53 | setosa | 0.02 | [0.01, 0.06] | 100% | [-0.10, 0.10] | 100%
61-
2.53 | versicolor | 0.66 | [0.52, 0.78] | 100% | [-0.10, 0.10] | 0%
62-
2.53 | virginica | 0.32 | [0.20, 0.45] | 100% | [-0.10, 0.10] | 0%
63-
2.80 | setosa | 0.09 | [0.04, 0.16] | 100% | [-0.10, 0.10] | 65.72%
64-
2.80 | versicolor | 0.49 | [0.39, 0.59] | 100% | [-0.10, 0.10] | 0%
65-
2.80 | virginica | 0.41 | [0.32, 0.52] | 100% | [-0.10, 0.10] | 0%
66-
3.07 | setosa | 0.27 | [0.19, 0.36] | 100% | [-0.10, 0.10] | 0%
67-
3.07 | versicolor | 0.30 | [0.21, 0.39] | 100% | [-0.10, 0.10] | 0%
68-
3.07 | virginica | 0.43 | [0.33, 0.53] | 100% | [-0.10, 0.10] | 0%
69-
3.33 | setosa | 0.57 | [0.47, 0.68] | 100% | [-0.10, 0.10] | 0%
70-
3.33 | versicolor | 0.12 | [0.06, 0.21] | 100% | [-0.10, 0.10] | 29.76%
71-
3.33 | virginica | 0.30 | [0.20, 0.40] | 100% | [-0.10, 0.10] | 0%
72-
3.60 | setosa | 0.82 | [0.68, 0.91] | 100% | [-0.10, 0.10] | 0%
73-
3.60 | versicolor | 0.03 | [0.01, 0.08] | 100% | [-0.10, 0.10] | 100%
74-
3.60 | virginica | 0.14 | [0.07, 0.26] | 100% | [-0.10, 0.10] | 17.37%
75-
3.87 | setosa | 0.94 | [0.83, 0.98] | 100% | [-0.10, 0.10] | 0%
76-
3.87 | versicolor | 0.01 | [0.00, 0.03] | 100% | [-0.10, 0.10] | 100%
77-
3.87 | virginica | 0.05 | [0.02, 0.15] | 100% | [-0.10, 0.10] | 89.59%
78-
4.13 | setosa | 0.98 | [0.91, 1.00] | 100% | [-0.10, 0.10] | 0%
79-
4.13 | versicolor | 0.00 | [0.00, 0.01] | 100% | [-0.10, 0.10] | 100%
80-
4.13 | virginica | 0.02 | [0.00, 0.08] | 100% | [-0.10, 0.10] | 100%
81-
4.40 | setosa | 0.99 | [0.95, 1.00] | 100% | [-0.10, 0.10] | 0%
82-
4.40 | versicolor | 0.00 | [0.00, 0.00] | 100% | [-0.10, 0.10] | 100%
83-
4.40 | virginica | 0.01 | [0.00, 0.04] | 100% | [-0.10, 0.10] | 100%
54+
2.00 | setosa | 0.00 | [0.00, 0.01] | 100% | [-0.10, 0.10] | 100%
55+
2.00 | versicolor | 0.86 | [0.65, 0.95] | 100% | [-0.10, 0.10] | 0%
56+
2.00 | virginica | 0.14 | [0.05, 0.34] | 100% | [-0.10, 0.10] | 28.00%
57+
2.27 | setosa | 0.00 | [0.00, 0.02] | 100% | [-0.10, 0.10] | 100%
58+
2.27 | versicolor | 0.78 | [0.60, 0.90] | 100% | [-0.10, 0.10] | 0%
59+
2.27 | virginica | 0.21 | [0.10, 0.40] | 100% | [-0.10, 0.10] | 0%
60+
2.53 | setosa | 0.02 | [0.01, 0.06] | 100% | [-0.10, 0.10] | 100%
61+
2.53 | versicolor | 0.66 | [0.52, 0.78] | 100% | [-0.10, 0.10] | 0%
62+
2.53 | virginica | 0.32 | [0.20, 0.45] | 100% | [-0.10, 0.10] | 0%
63+
2.80 | setosa | 0.09 | [0.04, 0.16] | 100% | [-0.10, 0.10] | 65.72%
64+
2.80 | versicolor | 0.49 | [0.39, 0.59] | 100% | [-0.10, 0.10] | 0%
65+
2.80 | virginica | 0.41 | [0.32, 0.52] | 100% | [-0.10, 0.10] | 0%
66+
3.07 | setosa | 0.27 | [0.19, 0.36] | 100% | [-0.10, 0.10] | 0%
67+
3.07 | versicolor | 0.30 | [0.21, 0.39] | 100% | [-0.10, 0.10] | 0%
68+
3.07 | virginica | 0.43 | [0.33, 0.53] | 100% | [-0.10, 0.10] | 0%
69+
3.33 | setosa | 0.57 | [0.47, 0.68] | 100% | [-0.10, 0.10] | 0%
70+
3.33 | versicolor | 0.12 | [0.06, 0.21] | 100% | [-0.10, 0.10] | 29.76%
71+
3.33 | virginica | 0.30 | [0.20, 0.40] | 100% | [-0.10, 0.10] | 0%
72+
3.60 | setosa | 0.82 | [0.68, 0.91] | 100% | [-0.10, 0.10] | 0%
73+
3.60 | versicolor | 0.03 | [0.01, 0.08] | 100% | [-0.10, 0.10] | 100%
74+
3.60 | virginica | 0.14 | [0.07, 0.26] | 100% | [-0.10, 0.10] | 17.37%
75+
3.87 | setosa | 0.94 | [0.83, 0.98] | 100% | [-0.10, 0.10] | 0%
76+
3.87 | versicolor | 0.01 | [0.00, 0.03] | 100% | [-0.10, 0.10] | 100%
77+
3.87 | virginica | 0.05 | [0.02, 0.15] | 100% | [-0.10, 0.10] | 89.59%
78+
4.13 | setosa | 0.98 | [0.91, 1.00] | 100% | [-0.10, 0.10] | 0%
79+
4.13 | versicolor | 0.00 | [0.00, 0.01] | 100% | [-0.10, 0.10] | 100%
80+
4.13 | virginica | 0.02 | [0.00, 0.08] | 100% | [-0.10, 0.10] | 100%
81+
4.40 | setosa | 0.99 | [0.95, 1.00] | 100% | [-0.10, 0.10] | 0%
82+
4.40 | versicolor | 0.00 | [0.00, 0.00] | 100% | [-0.10, 0.10] | 100%
83+
4.40 | virginica | 0.01 | [0.00, 0.04] | 100% | [-0.10, 0.10] | 100%
8484
8585
Variable predicted: Species
8686
Predictors modulated: Sepal.Width

tests/testthat/test-estimate_grouplevel.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,11 @@ test_that("estimate_grouplevel - Bayesian, rstanarm", {
129129
skip_if(is.null(m))
130130

131131
out <- estimate_grouplevel(m)
132-
expect_identical(dim(out), c(4L, 8L))
132+
expect_identical(dim(out), c(3L, 8L))
133133
expect_named(out, c("Group", "Level", "Parameter", "Median", "MAD", "CI", "CI_low", "CI_high"))
134134

135135
out <- estimate_grouplevel(m, dispersion = FALSE)
136-
expect_identical(dim(out), c(4L, 7L))
136+
expect_identical(dim(out), c(3L, 7L))
137137

138138
out <- estimate_grouplevel(m, type = "total", dispersion = FALSE)
139139
expect_identical(dim(out), c(3L, 4L))

0 commit comments

Comments
 (0)