# Error for loops rStan transformed parameters

Hello,
I’m trying to estimate the growth parameters of a fish from the capture and recapture data, I want to calculate the age by means of a loops but you leave me error in block transformed parameters

cat(file = “modelhier.stan”, "
data {
int<lower=1> N;// Sample size
int<lower=1> n;// size of id for individuals
int<lower=0> ID[n];// id for individuals
vector[N] dt;// time
vector[N] L;// individuals capture length mm
vector[N] L0;// individuals recapture length mm
}
parameters{
real<lower=0> Linf;//infinity length or max length
real<lower=0> k;//growth rate
real<lower=0> sigma[N];//error
}
transformed parameters {
vector[N] mu;//I want to estimate mean, but I need calculate individual age A[j]

``````    mu[i] = Linf*(1-exp(-k*(A[ID[i]] + dt[i])));

for(j in 1:n){
A[j] = log((L[j]-L0[j])/(L[j]-L0[j]*(exp(-k*dt[j]))))*(-1/k);
}
``````

}
model{
Linf ~ normal(60,20)T[0,];
k ~ normal(0, 0.1)T[0,];
sigma ~ normal(0,1)T[0,];
for( i in 1:N){
L[i] ~ normal(mu[i], sigma);
}
}
")

L<-dato\$A1
L0<-dato\$A0
dt<-dato\$dt
N<-length(dato\$ID)
n<-length(unique(dato\$ID))
ID<-dato\$ID

I am not sure that I understand what you are trying to do, but it looks as if the expression for `mu[i]` should either be inside the for loop or better as a one-line expression like

``````transformed data {
vector[N] log_LmL0 = log(L - L0);
}
parameters {
real<lower = 0> Linf;
real<lower = 0> k;
real<lower = 0> sigma[N];
}
transformed parameters {
vector[N] A = log_LmL0 - log(L - L0 .* exp(-k * dt)) - log(k);
vector[N] mu = Linf * (1 - exp(-k * (A[ID] + dt)));
}
``````

Then, there is another error that

``````sigma ~ normal(0, 1)[T[0, ];
``````

is not supported for real arrays. If you don’t need a normalized log-density, you can just omit the `T[0, ];` in this case because the truncated area is constant. Or if you want to, you can do

``````sigma ~ normal(0, 1); // or std_normal() in 2.19
target += -N * log(0.5); // due to the truncation
``````

Thanks bgoodri, I have changed according to what you recommend, I need to calculate the age of each individual “A [j]” in function of its id therefore I put the for loop in n, since the individuals have been recaptured and measured (but I do not know the parameter "k "), the problem is on the line transformed data {…

cat(file = “modelhier.stan”, "
data {
int<lower=1> N;// Sample size
int<lower=1> n;// size of id for individuals
int<lower=0> ID[n];// id for individuals
vector[N] dt;// time
vector[N] L;// individuals capture length mm
vector[N] L0;// individuals recapture length mm
}

``````parameters{
real<lower=0> Linf;//infinity length or max length
real<lower=0> k;//growth rate
real<lower=0> sigma[N];//error
//real<lower=0> mu[N];
}
transformed data {
vector[n] A;
for(j in 1:n){
A[j] = log((L[j]-L0[j])/(L[j]-L0[j]*(exp(-k*dt[j]))))*(-1/k);
}
``````

}
transformed parameters {
vector[N] mu;
for(i in 1:N){
mu[i] = Linf*(1-exp(-k*(A[ID[i]] + dt[i])));
}
}

model{
Linf ~ normal(60,20)T[0,];
k ~ normal(0, 0.1)T[0,];
sigma ~ normal(0,1);

``````    for( i in 1:N){
L[i] ~ normal(mu[i], sigma);
``````

}
}
")

If a function depends on `k` and `k` is declared in the parameters block, then it all needs to go into the transformed parameters block rather than transformed data. I think what I wrote before was equivalent to what you were trying to do.

Thanks once again,given the change,

cat(file = “modelhier.stan”, "
data {
int N;
vector[N] dt;
vector[N] L;
vector[N] L0;
}
transformed data {
vector[N] log_LmL0 = log(L - L0);
}
parameters {
real<lower = 0> Linf;
real<lower = 0> k;
real<lower = 0> sigma;//[N];
}
transformed parameters {
vector[N] A = -(log_LmL0 - log(L - L0 .* exp(-k * dt)))/k;
vector[N] mu = Linf * (1 - exp(-k * (A + dt)));
}
model{
Linf ~ normal(60,20)T[0,];
k ~ normal(0, 0.1)T[0,];
sigma ~ normal(0, 1);
//target += -N * log(0.5);
for( i in 1:N){
L[i] ~ normal(mu[i], sigma);
}
}
")

I get
Rejecting initial value:
Error evaluating the log probability at the initial value.

Initialization between (-2, 2) failed after 100 attempts.
[1] “Error in sampler\$call_sampler(args_list[[i]]) : Initialization failed.”
[1] “error occurred during calling the sampler; sampling not done”

I suppose that transformed parameters A , gave me log na

Its good, the problem was with log() gave nas values, I run for N observations, but not for n individuals that was repet in all meditions (N). I want to iterations for n individuals. Only this. Thanks