Ordered Logistic Regression Model Issues

When estimating this ordered logistic regression model, I get the following error message:

[1] "Error in sampler$call_sampler(args_list[[i]]) : Initialization failed."
error occurred during calling the sampler; sampling not done

Here is my stan model:

data {
  int N; //number of observations
  //Outcome
  int<lower=0> L; //number of ordinal categories
  int<lower=0, upper=L> act[N]; 
  //Clusterin factor
  int K; //number of respondents
  int respondent[N]; //respondent ID
  //measurement level predictors
  real prevalence[N]; 
}
parameters {
  real beta[K]; //respondent slopes for prevalence
  ordered[L-1] C; //cut points for ordered outcome
  real beta_top; //fixed effect intercept
  real<lower=0> sigma_top; 
}
model {
  for(i in 1:N) {
    int a_respondent;
    a_respondent = respondent[i];
    act[i] ~ ordered_logistic(prevalence[i] * beta[a_respondent], C);
  }
  //priors
  beta ~ normal(beta_top, sigma_top); 
  //hyperpriors
  beta_top ~ normal(0,2);
  sigma_top ~ cauchy(0,2);
}

act is of type integer, on a 0-7 scale.
L is equal to seven.
prevalence is of type real.
respondent is the clustering factor for the hierarchical model.

This is my R script:

#Import Stan Model
#Model 1 - Ordered Logistic Distribution
logistic_hierarchical <- stan_model("Ordinal_Logistic_Model_Hierarchical.stan")

#Data
act <- df$act
act<- as.integer(act)
prevalence <- df$prev_swc
respondent <- df$respondent_id
respondent <- as.factor(df$respondent_id)

data_logistic <- list(L = nlevels(as.factor(act)), act = as.integer(act), 
                      prevalence = prevalence, N=length((act)), K = nlevels(respondent), respondent = as.integer(respondent))

#Execute
options(mc.cores = parallel::detectCores(4))
hierarchical_logistic_model <- sampling(logistic_hierarchical, data = data_logistic, chains = 4,
                                iter =2000 refresh = 0)

Is anyone able to help?

It might be because ordered_logistic() expects that levels start at 1. This is the error I get when running your model with cases where act = 0:

Chain 1 Exception: ordered_logistic: Random variable is 0, but must be in the interval [1, 8] (in 'C:/Users/Simon/AppData/Local/Temp/Rtmp2RHbJf/model-22ac4c511853.stan', line 22, column 4 to column 69)

Note that your data constraint int<lower=0, upper=L> act[N]; allows values outside your expected range, since L = 8 in your case. It won’t cause a problem in your case, but worth noting in case your code changes.

See the documentation for more details.

2 Likes

That worked! Thank you! I’ve spent days trying to figure out what the issue is, so i really appreciate this!

Marked as solution!