Skip to content

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")

rxg added 4 commits December 8, 2022 10:04
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