Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tidy.brmsfit() output for model without a ran_pars component #148

Merged
merged 3 commits into from
Mar 30, 2024
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
16 changes: 12 additions & 4 deletions R/brms_tidiers.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,14 @@ tidy.brmsfit <- function(x, parameters = NA,
## don't want to remove these pieces, so use look*behind*
ran_pars = sprintf("(?<=(%s))", c("sd_", "cor_", "sigma")),
components = sprintf("(?<=%s)", c("zi_","disp_"))
)
prefs <- list(
)
prefs <- list(
fixed = "b_", ran_vals = "r_",
## no lookahead (doesn't work with grep[l])
ran_pars = c("sd_", "cor_", "sigma"),
components = c("zi_", "disp_")
)
pref_RE <- mkRE(prefs[effects])
)
pref_RE <- mkRE(prefs[effects])
if (use_effects) {
## prefixes distinguishing fixed, random effects

Expand Down Expand Up @@ -240,6 +240,14 @@ tidy.brmsfit <- function(x, parameters = NA,

}
out <- dplyr::bind_rows(res_list, .id = "effect")
# In the case where nrow(res_list$fixed) > 0 but nrow(res_list$ran_pars) == 0,
# the out object needs to be fixed a bit (replace columns with unexpected
# lists of NULL by expected vectors of NA).
for (col in c("group", "term")) {
if (is.list(out[[col]]) && all(sapply(out[[col]], is.null))) {
out[[col]] <- rep(NA, nrow(out))
}
}
v <- if (fixed.only) seq(nrow(out)) else is.na(out$term)
newterms <- stringr::str_remove(terms[v], mkRE(prefs[c("fixed")]))
if (fixed.only) {
Expand Down
Binary file modified inst/extdata/brms_example.rda
Binary file not shown.
14 changes: 13 additions & 1 deletion inst/extdata/run_examples.R
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,20 @@ if (run_brms) {
iter=200)
brms_RE <- hack_size(brms_RE)

# Example taken from ?brms::brm
## Probit regression using the binomial family
ntrials <- sample(1:10, 100, TRUE)
success <- rbinom(100, size = ntrials, prob = 0.4)
x <- rnorm(100)
data4 <- data.frame(ntrials, success, x)
fit4 <- brm(success | trials(ntrials) ~ x, data = data4,
family = binomial("probit"))
brms_brm_fit4 <- hack_size(fit4)


save_file(brms_crossedRE, brms_zip, brms_multi, brms_noran,
brms_multi_RE, brms_RE, pkg = pkg, type = "rda")
brms_multi_RE, brms_RE, brms_brm_fit4,
pkg = pkg, type = "rda")
})
}

Expand Down
32 changes: 32 additions & 0 deletions tests/testthat/test-brms.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,36 @@ if (require(brms, quietly = TRUE) && require(rstanarm, quietly=TRUE)) {
## GH #101
gg <- glance(brms_noran)
expect_equal(names(gg),c("algorithm","pss","nobs","sigma"))

## Check the descriptive columns of tidy summaries
### brms_RE
expected <- tibble::tribble(
~effect, ~component, ~group, ~term,
"fixed", "cond", NA, "(Intercept)",
"fixed", "cond", NA, "Days_extra",
"ran_pars", "cond", "Subject", "sd__(Intercept)",
"ran_pars", "cond", "Subject", "sd__Days_extra",
"ran_pars", "cond", "Subject", "cor__(Intercept).Days_extra",
"ran_pars", "cond", "Residual", "sd__Observation"
)
observed <- suppressWarnings(tidy(brms_RE))
expect_equal(observed[, 1:4], expected)
### brms_noran
expected <- tibble::tribble(
~effect, ~component, ~group, ~term,
"fixed", "cond", NA, "(Intercept)",
"fixed", "cond", NA, "wt",
"ran_pars", "cond", "Residual", "sd__Observation"
)
observed <- suppressWarnings(tidy(brms_noran))
expect_equal(observed[, 1:4], expected)
### brms_brm_fit4
expected <- tibble::tribble(
~effect, ~component, ~group, ~term,
"fixed", "cond", NA, "(Intercept)",
"fixed", "cond", NA, "x"
)
observed <- suppressWarnings(tidy(brms_brm_fit4))
expect_equal(observed[, 1:4], expected)

}