Hi all,
I’m trying to fit a non-linear growth model to some data with Stan (R code below). It seems pretty straightforward but I can’t get reasonable parameter estimates. Also I’m getting warning messages about divergent transitions and exceedance of the tree-depth.
I would greatly appreciate some advice.
Thanks,
Maarten
library(rstan)
dat <- list(
  "N" = 14,
  "x" = c(18, 19, 33, 39, 41, 41, 42, 44, 48, 57, 80, 81, 88, 93),
  "Y" = c(0.80, 1.16, 1.55, 2.22, 2.76, 4.63, 5.41, 6.93, 4.55, 5.17, 6.42, 6.78, 8.92, 5.99)
   )
nlm <- nls(Y ~ a*(1-exp(-b*x))^c, data = dat,
           start = list(a = 1, b = 0.01, c = 1))
plot(dat$x, dat$Y)
lines(dat$x, predict(nlm))
print(nlm)
stanmodel <- "
    data {
        int<lower=0> N; 
        real<lower=0> x[N]; 
        real<lower=0> Y[N];
    }
    parameters {
        real alpha; 
        real beta;  
        real gamma;
        real<lower=0> tau; 
    }
    transformed parameters {
        real<lower=0> sigma; 
        real<lower=0> m[N];
        real<lower=0> a;
        real<lower=0> b;
        real<lower=0> c;
        a = exp(alpha);
        b = exp(beta);
        c = exp(gamma);
        for (i in 1:N) 
            m[i] = a*(1-exp(-b*x[i]))^c;
        sigma = 1 / sqrt(tau);
    } 
    model {
        // priors
        alpha ~ normal(0.0, 1000); 
        beta ~ normal(0.0, 1000); 
        gamma ~ normal(0.0, 1000); 
        tau ~ gamma(.0001, .0001); 
        Y ~ normal(m, sigma); // likelihood
    }
    generated quantities{
        real Y_mean[N]; 
        real Y_pred[N]; 
        for(i in 1:N){
            Y_mean[i] = a*(1-exp(-b*x[i]))^c;  // Posterior parameter distribution of the mean
            Y_pred[i] = normal_rng(Y_mean[i], sigma);  // Posterior predictive distribution
        }
    }
    "
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
fit <- stan(model_code = stanmodel,
            model_name = "GrowthCurve",
            data = dat)
print(fit)