Thank you for the response @sbfnk. I tried your code on the MRE I provided here and unfortunately it still throws the same error:
# generate data
set.seed(10)
x <- 1:250
r <- rnorm(x, mean = 0, sd = x^-1)
c0 <- 2
a <- 0.01
b <- 0.07
k <- 0.01
y <- c0 * exp(-(a * x - b/k * (exp(-k * x) - 1))) + r
# write Stan code
stan_code <- "
data {
int n; // Number of observations
vector[n] y; // Response variable
vector[n] x; // Predictor variable
}
parameters {
real<lower=0> sigma; // Standard deviation
real<lower=0> a; // Intercept
real<lower=0> tau; // Precision of random step
vector[n] k; // Decay rate
}
model {
// Priors
sigma ~ exponential( 1 );
a ~ lognormal( log(2) , 0.05 );
k[1] ~ lognormal( log(0.08) , 0.1 );
tau ~ lognormal( log(0.01) , 0.1 );
// Random walk
for ( i in 2:n ) {
k[i] ~ normal(k[i - 1], tau);
}
// Prediction function
vector[n] mu; // Mean prediction for y
for( i in 1:n ) {
mu[i] = a * exp(-k[i] * x[i]); // Calculate mu using updated k
}
// Likelihood
y ~ normal(mu, sigma);
}
"
# draw samples using RStan
require(rstan)
require(tidybayes)
rw.mod <- stan(model_code = stan_code,
data = compose_data(df), iter = 1e4,
chains = 1, cores = parallel::detectCores())
[1] "Error : Initialization failed."
error occurred during calling the sampler; sampling not done