Issue with dimensions and indexes in transformed parameters block

I’m trying to estimate the hierarchical linear model below, but R is bringing up the following error:

‘Too many indexes, expression dimensions=0, index found =1’.

The issue is with the transformed parameters block, but I can’t seem to figure what the issue is. Does anyone have any ideas?

data {
  int<lower=1> N;      //Number of data points
  int behaviour[N];       //Outcome - QRP Own Behaviour
  //Clustering Factor
  int K;      //Number of respondents
  int<lower=0> respondent[N];      //Vector of respondent ID
  //Measurement Level Predictors
  real prevalence[N];      
  real trust_sci[N];      
  //Person Level Predictors
  real prevalence_b;      
  int field_artshum;      
  int field_nattech;      
  int field_medhealth;    
  real career_stage;      

parameters {
  real alpha_raw[K];      
  real beta_p_r[K];      
  real beta_trust_sci[K];      
  real<lower=0> sigma_resp[K];      
  real alpha_top;     
  real<lower=0> alpha_sigma;
  real beta_p_top;     
  real<lower=0> beta_p_sigma;      
  real beta_ts_top;      
  real<lower=0> beta_ts_sigma;      
  real beta_p_b;      
  real beta_artshum; 
  real beta_nattech; 
  real beta_medhealth; 
  real beta_career; 
  real lambda; 

transformed parameters {
  real alpha[K];
  for(i in 1:K){
    alpha[i] = alpha_sigma * alpha_raw[i] + alpha_top + beta_p_b*prevalence_b[i];

model {
  for(i in 1:N) {
    int a_respondent;
    a_respondent = respondent[i];
    behaviour[i] ~ exp_mod_normal(alpha[a_respondent] + beta_p_r[a_respondent]*prevalence[i] +
    beta_trust_sci*trust_sci[i], sigma_resp[a_respondent], lambda);
  alpha_raw ~ normal(0, 1);
  beta_p_r ~ normal(beta_p_top, beta_p_sigma);
  beta_trust_sci ~ normal(beta_ts_top, beta_ts_sigma);
  sigma_resp ~ cauchy(0,10);
  alpha_top ~ normal(0,2);
  beta_p_top ~ normal(0,2);
  beta_ts_top ~ normal(0,2);
  alpha_sigma ~ normal(0,2);
  beta_p_sigma ~ normal(0,2);
  beta_ts_sigma ~ normal(0,2);
  beta_p_b ~ normal(0,2);

generated quantities{
  real alpha_overall;
  real beta_p_overall;
  real logLikelihood[N];
  vector[N] y_rep;
  alpha_overall = exp_mod_normal_rng(alpha_top, alpha_sigma, lambda);
  beta_p_overall = exp_mod_normal_rng(beta_p_top, beta_p_sigma, lambda);
  for (i in 1:N) {
    y_rep[i] = exp_mod_normal_rng(alpha[respondent] + beta_p_r[respondent]*prevalence[i] +
    beta_trust_sci*trust_sci[i], sigma_resp[respondent], lambda);
  for (i in 1:N) {
    logLikelihood[i] = exp_mod_normal_lpdf(behaviour[i] | alpha[respondent] + beta_p_r[respondent]*prevalence[i] +
    beta_trust_sci*trust_sci[i], sigma_resp[respondent], lambda);

the problem appears to be the line:

    alpha[i] = alpha_sigma * alpha_raw[i] + alpha_top + beta_p_b*prevalence_b[i];

where prevalence_b[i] is indexed by i, but is defined as real prevalence_b;, i.e. with no dimensions.

Best of luck with your model!

1 Like

The problem is that you try to iterate over prevalence_b, which you declare to be a non-array real.
Naturally prevalence_b has no indices you can call.


Thank you, @martinmodrak ! That seems to have resolved the issue! (: Marked as solution.