Reduce sum with Poisson mixture of different weight

Please see below code and error message. I believe sum of type real can be initiated as 0 so I don’t know what went wrong. Could someone please help?

In addition, is there a way to vectorize the mixture model specification to some like below.
log_mix(lambda[start:end], poisson_lpmf(y_slice | pe[start:end]),poisson_lpmf(y_slice| pc[start:end]))

I only came across one example when the weight is same (i.e. a scalar rather than a vector) across every observation like in below example, however there seem to be some recent discussion even if this is legit or not.

rstan:::rstudio_stanc(“vitiligo anticipated pooled multi thread.stan”)
Error in stanc(filename, allow_undefined = TRUE) : 0

Semantic error in ‘string’, line 5, column 4 to column 10:

Ill-typed arguments supplied to assignment operator =: lhs has type and rhs has type int

functions {
  real partial_sum(int[] y_slice,
                   int start, int end,
                   vector pe, vector pc, vector lambda){
    sum=0;
    for (i in 1:(end-start+1)) {
    sum += log_mix(lambda[i+start-1], poisson_lpmf(y_slice[i] | pe[i+start-1]),poisson_lpmf(y_slice[i] | pc[i+start-1]));
    }
    return sum;
  }
}
data {
  int<lower=0> n;                  // number of participants in blinded trials 
  vector <lower=0> [n] r;          //  randomization ratio in blinded trials (experiment:control)
  array[n] int<lower=0> y;               // vector of events in blinded trials 
  vector <lower=0> [n] t;          // vector of exposure in blinded trials
  
  int<lower=0> yctc;       // events in completed trials control arm
  real <lower=0> tctc;     // exposure in completed trialscontrol arm
  
  int<lower=0> ycte;       // events in completed trials experiment arm
  real <lower=0> tcte;     // exposure in completed trials experiment arm
  
  int<lower=1> n_thetac;          // number of components for historical mixture prior of control arm
  vector<lower=0, upper=1>  [n_thetac] pwc;  //  mixture components prior: weight
  vector<lower=0> [n_thetac] pac;  //  mixture components prior: alpha
  vector<lower=0> [n_thetac] pbc;  //  mixture components prior: beta
}

// now potential different rand ratio with different blinded trials
transformed data {
  vector <lower=0, upper=1> [n] lambda;
  lambda=r./(r+1) ;// prob. being in active arm
}

parameters {
  real<lower=0> thetac;// mean control arm blind trial hazard rate (unit time count)  
  real<lower=0> thetae;// mean experiment arm blind trial hazard rate (unit time count)
  }

transformed parameters {
  vector<lower=0> [n]  pc;   // blind trial control: adjusted by exposure time
  vector<lower=0> [n]  pe;   // blind trial active arm: adjusted by exposure time
  
    pc=t*thetac; // adjusted by exposure time for subject 
    pe=t*thetae; // adjusted by exposure time for subject 
  
}

model {
 vector [n_thetac] lp_thetac;
 
// a mixture gamma prior for control;
  for (i in 1:n_thetac) {
        lp_thetac[i]= gamma_lpdf(thetac|pac[i],pbc[i]);
      }
   target += log_mix(pwc,  lp_thetac);
   
// likelihood for completed trial data
  yctc~poisson(tctc*thetac);
  ycte~poisson(tcte*thetae);
 
//blind trial a mixture poisson distribution;

  int grainsize = 1;
  target += reduce_sum(partial_sum, y,
                       grainsize,
                       pe,pc,lambda);
}

Hi

There are two problems here.

  1. sum is a stan function, hence the name is reserved
  2. All variables should be initialized with a predefined datatype

Something along of the lines of real s=0; should work.

Based on the docs Real-Valued Basic Functions, there is a vectorized implementation. I have not used this before, but would assume it would work if the datatypes supplied are arrays

Watch out with vectorising mixture models, the manual dedicates a section to it.