So I just tried that with a single model and it does work! It produces a single model in the environment I can pull parameter estimates from. Having trouble adapting the iterative function to use it though; the command I use (parameters::model_parameters
) to extract the estimates I need can’t read the captured output when its piped from update()
. I get an error that says (possible class 'character' not supported'): argument is not interpreted as logical
.
# works
output <- capture.output(fit <- brm(data = d1,
family = gaussian,
y ~ 1 + treatment,
prior = c(prior(normal(42.5, 5), class = Intercept),
prior(normal(0, 10), class = b),
prior(lognormal(0, 5), class = sigma)),
seed = 4))
# fails
capture.output(update(fit_lin,
newdata = d1,
seed = 1)) |>
parameters::model_parameters("median", ci_method = "HDI", ci=.89) |>
as_tibble() |>
select(1, 3, 5:6)
Full code for reproducability here:
pacman::p_load(brms, easystats, tidyverse, janitor, flextable)
# define the means and SD's for the idealized data
mu_struc=40
sd_struc=10
mu_UNstruc=43
sd_UNstruc=10
# determine the group size
n <- 50
# simulate the data
set.seed(1)
d1 <-
tibble(group = rep(c("structured", "unstructured"), each = n)) %>%
mutate(treatment = ifelse(group == "structured", 0, 1),
y = ifelse(group == "structured",
rnorm(n, mean = mu_struc, sd = sd_struc),
rnorm(n, mean = mu_UNstruc, sd = sd_UNstruc)))
# create practice model
fit_lin=brm(data = d1,
family = gaussian,
y ~ 1 + treatment,
prior = c(prior(normal(42.5, 5), class = Intercept),
prior(normal(0, 10), class = b),
prior(lognormal(0, 5), class = sigma)),
seed = 4)
# SIMULATE
# A. Set the number of simulations
n_sim <- 1500
# B. Load function (substituting broom for easystats)
sim_d_and_fit_easystats <- function(seed, n) {
set.seed(seed)
d <-
tibble(group = rep(c("control", "treatment"), each = n)) |>
mutate(treatment = ifelse(group == "control", 0, 1),
y = ifelse(group == "control",
rnorm(n, mean = mu_struc, sd = sd_struc),
rnorm(n, mean = mu_UNstruc, sd = sd_UNstruc)))
update(fit_lin,
newdata = d,
seed = seed) |>
model_parameters("median", ci_method = "HDI", ci=.89) |> as_tibble() |> select(1, 3, 5:6) |>
filter(Parameter == "b_treatment")
}
# C. Run simulation, with time benchmark
t1=tictoc::tic()
sim <-
tibble(seed = 1:n_sim) |>
mutate(tidy = map(.x= seed, .f= sim_d_and_fit_easystats, n = 50)) |> # change sample size as necessary
unnest(tidy) |>
mutate(width = CI_high - CI_low) # compute interval widths
t2=tictoc::toc()
All credit for the above goes to Solomon Kurz by the way. Absolutely amazing tutorial on his blog that I tweaked only slightly.
(In case it matters, I’m running R version 4.2.1; and brms v2.17.0)