sim() not working for models with simplex

Open
#440 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Start by running the reproduction script in the issue: the ulam model m12.6 with a simplex[8] parameter delta, then sim() on it. Compare the simulated outcomes against the observed Trolley responses, and check how sim() handles simplex parameters versus the plain-parameter model m12.7, which works. The issue has no comments or linked pull request, so the cause is still open. Done means sim() on the simplex model produces a distribution matching the data, with a test covering it.

Written by the indexing model from the issue text.

Description

Hello everyone,
I faced odd results from sim() using it with (ulam) models that have simplex predictors in it. I tested it with a modified version of model m12.6:

data(Trolley)
d <- Trolley %>% sample_n(1000)
edu_levels <- c( 6 , 1 , 8 , 4 , 7 , 2 , 5 , 3 )
d$edu_new <- edu_levels[ d$edu ]

dat <- list(
             R = d$response ,
             action = d$action,
             intention = d$intention,
             contact = d$contact,
             E = as.integer( d$edu_new ), # edu_new as an index
             alpha = rep( 2 , 8 ) ) # delta prior

m12.6 <- ulam(
  alist(
    R ~ ordered_logistic( phi , kappa ),
    phi <- bE*sum( delta[1:E] ) + bA*action + bI*intention + bC*contact,
    kappa ~ normal( 0 , 1.5 ),
    c(bA,bI,bC,bE) ~ normal( 0 , 1 ),
    simplex[8]: delta ~ dirichlet( alpha )
  ), data=dat , chains=4 , cores=4 )
summary(m12.6)
temp <- sim(m12.6) %>% as_tibble()

d$response %>% hist()
temp %>% pivot_longer(everything()) %>% pull(value) %>% hist()

I had to estimate the first parameter of the simplex model as well instead setting it zero to get sim() working.

The original data looks like:
grafik

The prediction of the model with simplex looks like:
grafik

Using the model m12.7 works just fine:

dat$edu_norm <- normalize( d$edu_new )
m12.7 <- ulam(
  alist(
    R ~ ordered_logistic( mu , cutpoints ),
    mu <- bE*edu_norm + bA*action + bI*intention + bC*contact,
    c(bA,bI,bC,bE) ~ normal( 0 , 1 ),
    cutpoints ~ normal( 0 , 1.5 )
  ), data=dat , chains=4 , cores=4 )
temp2 <- sim(m12.7) %>% as_tibble()

temp2 %>% pivot_longer(everything()) %>% pull(value) %>% hist()

The prediction looks well:
grafik

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rmcelreath/rethinking

All issues in rmcelreath/rethinking

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.