# Rewriting hierarchical model from WinBugs. Help needed

Hi
I am completely new to Stan so please bear with me:-)
I have created (and run) a model in WinBugs. Since WinBugs doesn’t cut it anymore I wish to switch to Stan. I am currently attempting to run my model in Stan.
What I have is 600 crabs that were measured (length=L0) and thrown back in the sea. These crabs have been recaptured 1,2 or 3 times, indicated by variable J ( J[1] = first recapture and so on). The length at recapture is L. In the model I wish to create a Von Bertalanffy growth curve based on
-length at tagging (L0)
-length at recapture (L)
-time elapsed between tagging and recapture (T).

To do so we need to estimate two parameters, Linf and k. Since we have no way of measuring the age of the individual, the age of each individual has to be estimated also.

THE CODE I USED IN WINBUGS IS:

``````{
#PROCESS
for( i in 1 : N) {
pred.L0[i]<-Linf*(1-exp(-k*(age[i])))  #predicted length at release
L0[i]~dnorm(pred.L0[i],prec.L0)

for(j in 1:J[i]){
pred.L[i,j]<-Linf*(1-exp(-k*(age[i]+ t[i,j]))) #length at jth recapture
L[i,j]~dnorm(pred.L[i,j],prec.L)
}}
#PRIORS
for( i in 1 : N) {
age[i]~dlnorm(log.centre,prec.age)
}

#HYPER PRIORS
Linf~dnorm(200,.002)  #population mean
k~dbeta(1,1)  #population mean
prec.L0<-1/(sd.L0*sd.L0)
sd.L0~dunif(0,100)
prec.L<-prec.L0
centre~dunif(0,200)
log.centre<-log(centre)
prec.age~dgamma(.01,.01)
p.age~dlnorm(log.centre,prec.age)
p.Linf~dnorm(200,.002)
p.k~dbeta(1,1)
}
``````

STAN SCRIPT
I’ve been trying to rewrite the script and this is how far I’ve gotten

``````data {
int<lower=0> N;
int<lower=0> T;
real L0;
real L;
}
parameters{
real Linf;
real k;
real pLinf;
real pk;
real sdL0;
real centre;
real page;
real precage;
real age;
}

transformed parameters{
real logcentre;
real precL0;
real precL;

logcentre=log(centre);
precL0=1/(sdL0*sdL0);
precL=precL0;
}

model{
Linf ~ normal(200,.002);
k ~beta(1,1);
pLinf ~ normal(200,.002);
pk ~ beta(1,1);
sdL0 ~ uniform(0,100);
centre~uniform(0,200);
page~lognormal(logcentre,precage);
precage ~ gamma(.01,.01);
age~lognormal(logcentre,precage);

for (i in 1:N)
predL0[i]=Linf*(1-exp(-k*(age[i])))  //predicted length at release
L0[i]~dnorm(predL0[i],precL0[i])

for(j in 1:J[i]){
pred.L[i,j]<-Linf*(1-exp(-k*(age[i]+ t[i,j]))) //length at jth recapture
L[i,j]~dnorm(pred.L[i,j],prec.L)
}
``````

I think I’m on my way but the following message stopped me in my track:

``````SYNTAX ERROR, MESSAGE(S) FROM PARSER:

Indexed expression must have at least as many dimensions as number of indexes supplied:
indexed expression dimensionality = 0; indexes supplied = 1
error in 'model_Growth2' at line 43, column 33
-------------------------------------------------
41:
42: for (i in 1:N)
43: predL0[i]=Linf*(1-exp(-k*(age[i])))  //predicted length at release
^
44: L0[i]~dnorm(predL0[i],precL0[i])
-------------------------------------------------

Error in stanc(model_code = paste(program, collapse = "\n"), model_name = model_cppname,  :
failed to parse Stan model 'Growth2' due to the above error.
``````

How do I solve this? I appreciate any help and if you see any other mistakes in my code, feel free to point them out to me:-)

This will get you one step further. The error message is pointing you to the use of `age`. It’s declared as `real age`, which is a scalar–a single value. You’re trying to index it like an array.

My quick suggestion: start simple and build up. It’s easier to debug that way.

1 Like

Am I to remove “real” in front of age? If so, am I to remove it from the other parameters also? Doesn’t “real” just tell you that your dealing with continous numbers, not integer?

Never mind, I understand now:-)

1 Like

Thank you for you help, it fixed the problem. But now i got a new one.

SYNTAX ERROR, MESSAGE(S) FROM PARSER:

## unknown variable in assignment; lhs variable=predL0 error in ‘model_Growth2’ at line 44, column 11

``````42:
43: for (n in 1:N)
44: predL0[n]=Linf*(1-exp(-k*(age[n])))  //predicted length at release
^
45: L0[n]~dnorm(predL0[i],precL0[n])
``````

PARSER EXPECTED:
Error in stanc(model_code = paste(program, collapse = “\n”), model_name = model_cppname, :
failed to parse Stan model ‘Growth2’ due to the above error.

My script now looks like this:
data {
int<lower=0> N;
int<lower=0> T;
real L0;
real L;
}

parameters{
real Linf;
real k;
real pLinf;
real pk;
real sdL0;
real centre;
real page;
real precage;
real age[N];

}

transformed parameters{
real logcentre;
real precL0;
real precL;

logcentre=log(centre);
precL0=1/(sdL0*sdL0);
precL=precL0;
}

model{
Linf ~ normal(200,.002);
k ~beta(1,1);
pLinf ~ normal(200,.002);
pk ~ beta(1,1);
sdL0 ~ uniform(0,100);
centre~uniform(0,200);
page~lognormal(logcentre,precage);
precage ~ gamma(.01,.01);
age~lognormal(logcentre,precage);

for (n in 1:N)
predL0[n]=Linf*(1-exp(-k*(age[n]))); //predicted length at release
L0[n]~dnorm(predL0[i],precL0[n]);

for(j in 1:J){
pred.L[i,j]<-Linf*(1-exp(-k*(age[i]+ t[i,j]))); //length at jth recapture
L[i,j]~dnorm(pred.L[i,j],prec.L);
}