Hi all,
I recently ran through Solomon Kurz’s amazing blog post on power simulations, and I’ve been trying to translate it from brms to rstanarm since I’m only familiar with the latter package. For some reason though, when I run the stan_glm model in the early blocks just by itself it works perfectly fine, but when I try his code that iteratively updates the model and tries to run many simulations, it never finishes. The brms simulation finished in 46 seconds, whereas the rstanarm loop kept going until I aborted it a full 6 minutes later. Anyone know what’s going wrong? I’ll post the code below so you don’t have to reference the OG article…
n <- 10
z <- 7
set.seed(3)
d <- tibble(y = rbinom(n = 50, size = 1, prob = .25))
fit1 <-
brms::brm(data = d,
family = binomial,
y | trials(1) ~ 1,
prior(normal(0, 2), class = Intercept),
seed = 3)
#### rstanarm ####
fit2=stan_glm(y~1,
family = binomial(link = "logit"),
data=d,
prior = student_t(5,location=NULL, scale=NULL, autoscale = TRUE),
#prior_intercept = normal(),
#prior_PD = FALSE,
algorithm = c("sampling"),
mean_PPD = TRUE,
adapt_delta = 0.95,
#QR = FALSE,
#sparse = FALSE,
chains=4,iter=4000,cores=4)
sim_data_fit <- function(seed, n_player) {
n_trials <- 1 # do not touch
prob_hit <- .25 # probability of event from 0 to 1
set.seed(seed)
d <- tibble(y = rbinom(n = n_player,
size = n_trials,
prob = prob_hit))
update(fit1,
newdata = d,
seed = seed) %>%
posterior_samples() %>%
transmute(p = inv_logit_scaled(b_Intercept)) %>%
median_qi() %>%
select(.lower:.upper)
}
#### rstanarm version of the above function ####
sim_data_fit_rstanarm <- function(seed, n_player) {
n_trials <- 1
prob_hit <- .25
set.seed(seed)
d <- tibble(y = rbinom(n = n_player,
size = n_trials,
prob = prob_hit))
update(fit2,
data = d,
seed = seed) %>%
posterior_samples() %>%
rename("Intercept"="(Intercept)") %>% # extract posterior draws
transmute(p = inv_logit_scaled(Intercept)) %>%# convert back from log-odds scale
tidybayes::median_qi() %>% #
select(.lower:.upper)
}
EDIT: I just tried re-running the models again and letting the rstanarm version do it’s thing to see if it eventually finishes. It did, though the time difference between the two is staggering: the brms simulations finished in 1.8 minutes (on my old laptop now) vs. 25 minutes for rstanarm. Does anyone know why rstanarm took about 12 times as long as brms here? And if there’s anything that can be done to reduce that difference?