model data
library(tidyverse)
set.seed(1)
n <- 50 # number of reps/group
mu_n <- 0 # means for each group
mu_c1 <- 0.2
mu_c2 <- 0.3
mu_c3 <- 0.3
mu_c4 <- 0.4
d <- tibble(group = rep(c("normal", "covid1", "covid2", "covid3", "covid4"), each = n),
cases = rep(c (1, 2, 3, 4, 5), each = n)) %>%
rowwise() %>%
mutate(y = case_when(
group == "normal" ~ rnorm(n = 1, mean = mu_n, sd = 1),
group == "covid1" ~ rnorm(n = 1, mean = mu_c1, sd = 1),
group == "covid2" ~ rnorm(n = 1, mean = mu_c2, sd = 1),
group == "covid3" ~ rnorm(n = 1, mean = mu_c3, sd = 1),
group == "covid4" ~ rnorm(n = 1, mean = mu_c4, sd = 1)
)) %>%
ungroup()
the initial model runs well
fit <-
brm(data = d,
family = gaussian,
y ~0+ Intercept + cases,
prior = c(prior(normal(0,2), class = b),
prior(student_t(3,1,1), class = sigma)),
fitted(d$"normal", d$"covid4"),
seed =1)
update the model
sim_d_and_fit <-
function
(seed, n, probs=c(0.05, 0.95)) {
mu_n <- 0 # means for each group
mu_c1 <- 0.2
mu_c2 <- 0.3
mu_c3 <- 0.3
mu_c4 <- 0.4
set.seed(seed)
d <- tibble(group = rep(c("normal", "covid1", "covid2", "covid3", "covid4"), each = n),
cases = rep(c (1, 2, 3, 4, 5), each = n)) %>%
rowwise() %>%
mutate(y = case_when(
group == "normal" ~ rnorm(n = 1, mean = mu_n, sd = 1),
group == "covid1" ~ rnorm(n = 1, mean = mu_c1, sd = 1),
group == "covid2" ~ rnorm(n = 1, mean = mu_c2, sd = 1),
group == "covid3" ~ rnorm(n = 1, mean = mu_c3, sd = 1),
group == "covid4" ~ rnorm(n = 1, mean = mu_c4, sd = 1)
)) %>%
ungroup()
update(fit,
newdata = d,
seed = seed) %>%
fixef(probs = probs) %>%
data.frame() %>%
tibble(diff_fm = d$y[d$group=="covid4"]-d$y[d$group=="normal"])%>%
rownames_to_column("parameters") %>%
filter(parameter =="cases")
}
after running the model
n_sim <-100
s1 <-
tibble(seed =1:n_sim) %>%
mutate(b1 = map(seed, sim_d_and_fit, n =60)) %>%
unnest(b1)
S1 has 0 observation of 7 variables