Hi

I’m working with a dataset I found on Statistical Rethinking, the Trolley datase. I’m following the book’s approach for this problem but I’m using STAN directly instead of “rethinking”. I’m regressing an ordinal variable against an ordinal predictor using the following model. “R” is an integer from 1 to 7 indicating how morally permissible the participant found a certain action and “E” is an integer from 1 to 8 representing the education level of that participant.

**Data prep**

```
dat <- list(
R = R ,
E = E,
alpha = rep( 2 , 7 ) )
```

**STAN model**

```
data{
int R[9930];
int E[9930];
vector[7] alpha;
}
parameters{
ordered[6] kappa;
real bE;
simplex[7] delta;
}
transformed parameters {
vector[9930] delta_sum;
vector[8] delta_j = append_row(0, delta);
vector[9930] phi;
for ( i in 1:9930 ) {
delta_sum[i] = sum(delta_j[1:E[i]]);
}
phi=bE*delta_sum;
}
model{
delta ~ dirichlet( alpha );
bE ~ normal( 0 , 1 );
kappa ~ normal( 0 , 1.5 );
R ~ ordered_logistic( phi , kappa );
}
```

I run the model with this code.

```
library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
parallel:::setDefaultClusterOptions(setup_strategy = "sequential")
fit<-stan("model.stan", data=dat)
```

The model run well and with no issues. However I found that some delta_sum are NaN values. These NaN values all occur in situations where E=1. Why is that? What am I doing wrong?

I have a second question. Suppose I have reason to believe the association between R and E might change depending on the participant’s religion/country/sate, etc. How could/should I add a varying effects for religion/country/sate?

Thanks

I’m attaching the data in case it helps: data.csv (38.8 KB)