Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug diagnostic in sim_core signals error when model uses index variables #370

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

rxg
Copy link

@rxg rxg commented Dec 7, 2022

I'm having trouble using sim with models that have index variables. Some debug code is crashing, and I think this may fix it.

Here is an example program that works after this change:

library(rethinking)

# Nancy Howell's Dobe !Kung data
data(Howell1)
d <- Howell1[Howell1$age >= 18, ]
d$sex <- d$male + 1

model5 <- alist(
    w ~ dnorm(mu, sigma),
    mu <- a[sex] + b[sex] * (h - hbar),
    a[sex] ~ dnorm(60, 10), # intercept: average adult weight in kg (wikipedia)
    b[sex] ~ dlnorm(0, 1),  # slope: change in weight per change in height
    sigma ~ dunif(0, 10),   # displacement

    h ~ dnorm(hmu, hsigma),
    hmu <- ha[sex],
    ha[sex] ~ dnorm(170, 15), # intercept: average adult height in cm
    hsigma ~ dunif(0, 10)      # displacement
)
dat <- list(w = d$weight, h = d$height,  hbar = mean(d$height), sex = d$sex)
fit5 <- quap(model5, data = dat)
precis(fit5, depth = 2)

# This is similar to McElreath 2022 Lecture 04, 49:39, but isn't working
hwsim <- sim(fit5, data = list(sex = c(1, 2), hbar = mean(d$height)),
     vars = c("h", "w"))
# seems to be dying in simcore because
# as.character turns a[s] into a vector c("[","a","s")

Related pull request
fix for issue with link not adding intercept for linear models
rmcelreath#285

I'm having trouble using sim with models that have index variables.  Some debug code is crashing, and I think this may fix it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant