Strange error - functions

I am trying to use reduce_sum to speed up estimation using within chain parallelization.

I have created the function to be used in reduce_sum as shown in vignette in stan manual.
However upon executing the code throws error stating the fuction does not exist.

functions {
  real partial_sum(int[] y_slice,
                   int start, 
                   int end,
                   matrix x1,
                   matrix x2,
                   vector beta,
                   matrix rbeta,
                   int J, 
                   int[] ll) {

    return bernoulli_logit_lpmf(y_slice[start:end] |x1[start:end,:] * beta + (x2[start:end,:] .* rbeta[ll[start:end],:]) * rep_vector(1,J));
  }
}
data {
  int<lower=0> N;//Number of observations
  int<lower=1> J;//Number of predictors with random slope
  int<lower=1> K;//Number of predictors with non-random slope
  int<lower=1> L;//Number of customers/groups
  int<lower=0,upper=1> y[N];//Binary response variable
  int<lower=1,upper=L> ll[N];//Number of observations in groups
  matrix[N,K] x1;
  matrix[N,J] x2;
}
parameters {
  vector[J] rbeta_mu; //mean of distribution of beta parameters
  vector<lower=0>[J] rbeta_sigma; //variance of distribution of beta parameters
  vector[J] beta_raw[L]; //group-specific parameters beta
  vector[K] beta;
}
transformed parameters {
  vector[J] rbeta[L];
  for (l in 1:L)
    rbeta[l] = rbeta_mu + rbeta_sigma .* beta_raw[l]; // coefficients on x
}
model {
  rbeta_mu ~ normal(0,100);
  rbeta_sigma ~ cauchy(0,10);
  beta~normal(0,10);
  for (l in 1:L)
    beta_raw[l] ~ normal(0,1);
    
  target += reduce_sum(partial_sum,y,1,x1,x2,beta,rbeta,J,ll);
}

I get the following erroer:

Any help with resolving this is greatly appreciated.

reduce sum is not available on the rstan on CRAN. You can try the experimental version below or use cmdstanr

remotes::install_git("https://github.com/stan-dev/rstan", subdir = "StanHeaders", ref = "experimental") 
remotes::install_git("https://github.com/stan-dev/rstan", subdir = "rstan/rstan", ref = "experimental") 
2 Likes

@stevebronder When executing said code to install RStan i get the following error on mac:

remotes::install_git(“GitHub - stan-dev/rstan: RStan, the R interface to Stan”, subdir = “StanHeaders”, ref = “experimental”)
remotes::install_git(“GitHub - stan-dev/rstan: RStan, the R interface to Stan”, subdir = “rstan/rstan”, ref = “experimental”)

remotes::install_git(“GitHub - stan-dev/rstan: RStan, the R interface to Stan”, subdir = “StanHeaders”, ref = “experimental”)
remotes::install_git(“GitHub - stan-dev/rstan: RStan, the R interface to Stan”, subdir = “rstan/rstan”, ref = “experimental”)

Sorry I don’t see an error message in your last reply? It might have been that I pasted the code wrong at first and there was no newline between both commands

(Edited the code to fix)

This is the error i get :

It worked on windows. Thank you .