Hi,

I’m a STAN beginner and I’m trying to model the following problem: Given a arbitary 1D function (here a zickzick like function) with unknown shift in x direction, I try to build model that evaluates the probability of the possible shifts

Here is my code

```
library(rstan)
# simulate the data
x <- seq(-10,10,0.01)
zickzack <- function(x, off) {
if (abs(x-off) >= 0 & abs(x-off) < pi) {
return(0.5*(pi-abs(x-off)))
}
else {
return(0)
}
}
y <- sapply(x,zickzack,off=5)
y_obs = y + rnorm(length(y),0,0.3)
model_code="
functions {
real zz(real x, real off) {
if ( (fabs(x-off) >= 0) && (fabs(x-off) < pi()) )
return 0.5*(pi()-fabs(x-off));
else
return 0;
}
}
data{
vector[2001] y;
vector[2001] x;
}
parameters{
real off;
real<lower=0> sigma;
}
model{
real mu;
sigma ~ exponential( 1 );
off ~ normal( 0 , 10 );
mu = zz(x,off);
y ~ normal( mu , sigma );
}
"
mymodel <- stan(model_code=model_code, data=list(y=y_obs,x=x))
```

This is how the data looks like (whereas the shift of the red function in x direction is unknown):

Currently, I’m struggeling getting my function working in stan. If I try to fit the model I get the error:

Can I somehow tell stan to apply the function elementwise? Or do I need to change the function that it works with vectors?

Best,

Thorsten