Hi, I’m running a complex Stan model via RStan. It estimates population densities over space and time. I want to calculate some summary statistics of these populations (like center of spatial distribution, edge of spatial distribution, etc) within Stan as generated quantities, so they have their own posterior distributions, rather than calculating them post hoc in R.

Some of these are calculated as weighted quantiles. For example, the center of spatial distribution is the median of observations across space, weighted by the size (abundance) of each observation. The edge is usually calculated a 0.05 or 0.95 quantile of the same.

I know a quantile function has recently been implemented in Stan, but I’m having trouble getting it to work—the function reference link is broken and I haven’t totally parsed the long GH issues discussing its development.

I tried writing a quick model to explore how the quantile function works, but can’t even get that to work. So, this is a two-part question:

- HOW DOES QUANTILE() WORK IN STAN?

My toy model:

```
data {
real probs;
int np;
int ny;
}
parameters {
matrix<lower=0>[np, ny] N;
}
model{
for(p in 1:np){
for(y in 1:ny){
N[p,y] ~ normal(10000, 3000);
}
}
}
generated quantities {
real quant_out[ny];
for(y in 1:ny){
quant_out = quantile(N[,y], probs);
}
}
```

Returns this error that seems to imply that quantile() doesn’t return anything:

```
A returning function was expected but an undeclared identifier 'quantile' was supplied.
```

And for extra credit…

2. HAS ANYONE WRITTEN A USER-DEFINED FUNCTION FOR CALCULATING WEIGHTED QUANTILES IN STAN?

This function would take as arguments a real probability / quantile, a vector or 1D array of data, and a vector or 1D array of weights (same dimensions as the data). I understand there are multiple ways to calculate weighted quantiles, and I’m not too picky about the exact method, since I’ll explore sensitivity to it regardless.