Error in R running stan model

I’m having trouble running my stan model I think its because of the R update. I was able to revert to a previous version on my desktop my laptop is having issues with that. I read on there that I need to pass data to stan before running the code. However, I’m not exactly sure how to do that. I’m still learning R and I understand what that means or what to do. Heres the section with the model, I need help with the code to pass data to stan.


define STAN model


model <- "

data {
int<lower=0> n;
int<lower=0> k_binary;
int<lower=0> k_ordered;
int<lower=0> y[n,k_binary+k_ordered];

parameters {
real alpha[k_binary];
real<lower=0> beta[k_binary+k_ordered];
ordered[2] cut_points;
vector[n] theta;

transformed parameters {

model {
// priors
theta ~ normal(0,1); //priors on latent variable

alpha ~ normal(0,1); //priors for the intercepts (these are variances not precision)
beta ~ normal(0,1); // priors for the slope. This is truncated so that the lowest possible value is 0

// likelihood (link the parameters to the item values)
for(j in 1:k_binary){
    y[,j] ~ bernoulli_logit(alpha[j] + beta[j] * theta);

for(j in 1:k_ordered){
    y[,k_binary+j] ~ ordered_logistic(beta[k_binary+j] * theta, cut_points);




end STAN model definition


Here is one way to pass data to Stan and run the model with the default conditions. You should take a look at the help section for the stan function to learn about what can be adjusted.
Your model needs values for n, k_binary, k_ordered, and y. You can pass those values to Stan using a named list. Let’s say you have variables in R named N, K_b, K_o and Y that store the values you want to pass plus the code for the model that you stored in the variable model.

dataList <- list(n = N, k_binary = K_b, k_ordered = K_o, y = Y)
stanFit <- stan(model_code = model, data = dataList)

That will put the result of the run into the object stanFit. Does that make sense?

1 Like