Hi, @simonbrauer I found the problem, in the data block the variables were incorrectly declared, in the transformed parameters block the matrix equation was incorrect and in the model block the binomial distribution variables were also incorrect.
Thank you very much for your help, really the way you showed me was correct. Thank you so much for taking your time to help me! Below is the correct STAN code:
data
{
int<lower=1> N;
array[N] int y;
array[N] int n;
array[N] real x;
array[N] real lambda;
}
transformed data
{
vector[N] x2;
for (i in 1:N){
if (lambda[i] != 0) {
x2[i] = (pow(x[i], lambda[i]) - 1) / (lambda[i]);
}else{
x2[i] = log(x[i]);
}
}
}
parameters
{
real beta0;
real beta1;
}
transformed parameters
{
array[N] real p;
for (c in 1:N)
{
p[c] = inv_logit(beta0 + beta1 * x2[c] * lambda[c]);
}
}
model
{
beta0 ~ normal(0, 100);
beta1 ~ normal(0, 100);
for(j in 1:N)
{
y[j] ~ binomial(n[j], p[j]);
}
}
Here the R code:
library(“rstan”)
library(“ggplot2”)
library(“StanHeaders”)
options(mc.cores=4)
lambda = c(0.12,0.44,0.45,0.47,0.5,0.21,0.13,0.4,0.45,0.32,
0.23,0.3,0.23,0.15,0.38,0.26,0.11,0.17,0.5,0.19,0.15,0.14,0.22,0.17,4.9)
x = c(9.21,10.21,10.58,10.83,11.03,11.33,11.58,11.83,12.08,
12.33,12.58,12.83,13.08,13.33,13.58,13.83,14.08,14.33,
14.58,14.83,15.08,15.33,15.58,15.83,17.58)
n = c(376,200,93,120,90,88,105,111,100,93,100,108,99,106,105,117,
98,97,120,102,122,111,94,114,1049)
y = c(0,0,0,2,2,5,10,17,16,29,39,51,47,67,81,88,79,90,113,95,117,107,92,112,1049)
model = stan_model(“Model_1_A.stan”)
fit = sampling(model,list(N=25, n=n, y=y, x=x, lambda=lambda),iter=5000,chains=4)
print(fit)