I’m going through exercises from Richard McElreath’s Statistical Rethinking book, and I’m trying to get myself acquainted with greta
in the process. I’m having some specific issues with Exercise 12M4 from the book. I’m not sure what I’m doing differently between the rethinking
code and the greta
code.
library(rethinking)
data(chimpanzees)
chimpanzees$recipient <- NULL
chimpanzees$block_id <- chimpanzees$block
m2 <- map2stan(
alist(
pulled_left ~ dbinom(1, p),
logit(p) <- a + a_actor[actor] + a_block[block_id] + (bp + bpc * condition) * prosoc_left,
a_actor[actor] ~ dnorm(mean_actor, sigma_actor),
a_block[block_id] ~ dnorm(mean_block, sigma_block),
c(a, mean_actor, mean_block, bp, bpc) ~ dnorm(0, 10),
c(sigma_actor, sigma_block) ~ dcauchy(0, 1)
), data = chimpanzees, warmup = 1000, iter = 6000, chains = 4, cores = 3
)
precis(m2)
plot(m2)
The trace plots show a healthy mixing of the chains.
Here’s my attempt in greta
:
actor <- as_data(chimpanzees$actor)
block_id <- as_data(chimpanzees$block)
condition <- as_data(chimpanzees$condition)
prosoc_left <- as_data(chimpanzees$prosoc_left)
pulled_left <- as_data(chimpanzees$pulled_left)
a <- normal(0, 10)
a_a <- normal(0, 10)
a_b <- normal(0, 10)
sigma_act <- cauchy(0, 1, truncation = c(0, Inf))
sigma_block <- cauchy(0, 1, truncation = c(0, Inf))
beta_p <- normal(0, 10)
beta_pc <- normal(0, 10)
a_act <- normal(a_a, sigma_act, dim = max(chimpanzees$actor))
a_block <- normal(a_b, sigma_block, dim = max(chimpanzees$block))
chimp_model <- a + a_act[actor] + a_block[block_id] + (beta_p + beta_pc * condition) * prosoc_left
p_chimps <- ilogit(chimp_model)
distribution(pulled_left) <- binomial(1, p_chimps)
chimp_model_results <- model(a, a_a, a_b, a_act, a_block, beta_p, beta_pc)
chimp_model_draws <- mcmc(chimp_model_results)
bayesplot::mcmc_trace(chimp_model_draws)
summary(chimp_model_draws)
The trace plots here are brutal. What am I missing in order to replicate the results?