Can not find user defined function (variable "partial_sum" does not exist.)

Hi,

I’m trying to speed up my model by following the users guide on parallelization, reducing sums (in R, using rstan). I removed rstan and re-installed everything (see here). But I’m still struggling with why the function is not found. Thanks for feedback!

Here is some toy data and the error message:

set.seed(1)
x1 = rnorm(1000)          
z = 1 + 2*x1 
pr = 1/(1+exp(-z))         
y = rbinom(1000,1,pr)      

datlist <- list(N=length(y), y=y, x=x1)

logistic_fit <- stan(file="logistic.stan", data=datlist, iter = 200, chains = 4, seed = 12591)

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Variable "partial_sum" does not exist.
 error in 'modela1586b80d890_logistic' at line 20, column 34
  -------------------------------------------------
    18:   int grainsize = 1;
    19:   beta ~ std_normal();
    20:   target += reduce_sum(partial_sum, y,
                                         ^
    21:                        grainsize,
  -------------------------------------------------

Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  failed to parse Stan model 'logistic' due to the above error.

The model copied & pasted from the users guide:

//logistic.stan
functions {
  real partial_sum(int[] y_slice,
                   int start, int end,
                   vector x,
                   vector beta) {
    return bernoulli_logit_lpmf(y_slice | beta[1] + beta[2] * x[start:end]);
  }
}
data {
  int N;
  int y[N];
  vector[N] x;
}
parameters {
  vector[2] beta;
}
model {
  int grainsize = 1;
  beta ~ std_normal();
  target += reduce_sum(partial_sum, y,
                       grainsize,
                       x, beta);
}

Session Info:

sessionInfo()
R version 4.0.0 (2020-04-24)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.5

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rstan_2.19.3         ggplot2_3.3.2        StanHeaders_2.21.0-3


1 Like

The reduce_sum facility is only available in versions 2.23 and up, and RStan is currently only up to 2.21.

If you want to use reduce_sum and control Stan via R, your best option (for the moment) is to use cmdstanR, instructions here. This package uses cmdstan, which is always the most recent version, but through R.

1 Like