Model for ulam + log_lik yields wrong simulation (potential dup).
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 25/100
Research direction
Start from the reproduction script: fit the ulam model with log_lik = TRUE, then call sim(m_bug, data = d_new) and compare its means with the hand-built rnbinom simulation from extract.samples. The sim() method for ulam fits and the dgampois likelihood are the places to look, and the first thing to check is whether the log_lik setting changes how new data is passed through. Done means the simulated means match the reference values, and the log_lik case has a regression test.
Written by the indexing model from the issue text.
Description
The following model fails to proper simulate, due to a combination of log_lik = TRUE that is probably a variation of issue 269. The problem was found when simulating a model for the Hurricanes example on chapter 12.
library(rethinking)
d_train <- list(
y = c(10L, 150L),
x1 = c(0, 1),
x2 = c(1, 2)
)
m_bug <- ulam(
alist(
y ~ dgampois(lambda, phi),
lambda <- exp(a + b * x1 * x2),
a ~ dnorm(3, 1),
b ~ dnorm(0, 2),
phi ~ dexp(1)
),
data = d_train, chains = 4, cores = 4, log_lik = TRUE
)
d_new <- list(
x1 = c(0, 0, 0),
x2 = c(0, 5, 10)
)
sim_buggy <- sim(m_bug, data = d_new)
post <- extract.samples(m_bug)
sim_correct <- sapply(1:3, function(j) {
lam <- exp(post$a + post$b * d_new$x1[j] * d_new$x2[j])
rnbinom(length(lam), size = post$phi, mu = lam)
})
data.frame(
row = 1:3,
x1_input = d_new$x1,
x2_input = d_new$x2,
sim_buggy_mean = apply(sim_buggy, 2, mean),
sim_correct_mean = apply(sim_correct, 2, mean)
)
precis(m_bug)
yields:
> data.frame(
row = 1:3,
x1_input = d_new$x1,
x2_input = d_new$x2,
sim_buggy_mean = apply(sim_buggy, 2, mean),
sim_correct_mean = apply(sim_correct, 2, mean)
)
row x1_input x2_input sim_buggy_mean sim_correct_mean
1 1 0 0 22.308 24.5405
2 2 0 5 885.890 24.3275
3 3 0 10 21.515 22.1910
> precis(m_bug)
mean sd 5.5% 94.5% rhat ess_bulk ess_tail
a 2.80 0.75 1.67 4.05 1.00 974.99 1032.00
b 1.23 0.63 0.23 2.22 1.01 795.26 1000.43
phi 1.44 1.06 0.30 3.46 1.00 830.41 1029.27
If relevant, env is very plain:
$R --version
R version 4.6.2 (2026-10-29) -- "Diversified Portfolio"
Copyright (C) 2026 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu
> packageVersion("rethinking")
[1] ‘2.46’
- Dominant language
- R
- Stars
- 2.4k
- Forks
- 632
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from rmcelreath/rethinking
-
Typo: p.381Open
Difficulty 1/5 Under an hour Newbie friendliness 95/100
rmcelreath/rethinking#482 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
rmcelreath/rethinking#476 ·
-
Typo: p.302Open
Difficulty 1/5 Under an hour Newbie friendliness 88/100
rmcelreath/rethinking#475 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 62/100
rmcelreath/rethinking#474 ·
-
Comma splice: p.557Open
Difficulty 1/5 Under an hour Newbie friendliness 90/100
rmcelreath/rethinking#471 ·
All issues in rmcelreath/rethinking
Similar issues
-
1.7
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
pharmaverse/admiral#3235 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
SticsRPacks/CroPlotR#185 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 1 day