JAGS错误:图中未定义的祖先节点或有向循环

时间:2018-06-29 18:39:20

标签: jags

在对生物生命周期进行建模时,我遇到了JAGS的名义错误。我的数据是多年繁殖鱼类的计数。这些繁殖鱼有三种不同的年龄类别(年龄= 3,年龄= 4,年龄= 5),因此我有一个Nyears by 3计数矩阵。给定年份鱼总数的2参数非线性函数会产生幼鱼。我想估计这些参数。为了完成生命周期并通过时间将我的观察结果机械地联系起来,我还必须估算年生存率和成熟概率。

这是使用MATLAB进行的过程模拟。如果使用R,请想象一下对象的某些[[]]在下面的“()”中。

years=50;
a=50; %First parameter of nonlinear function
b=1000; %Second parameter of nonlinear function
phi=random('beta',30,15,[years+5,1]);%Annual survival
gam3=0.05;%probabilty of maturing as a 3-year old
gam4=0.5;% CONDITIONAL probabilty of maturing as a 4-year old
gam5=1;%CONDITIONAL probabilty of maturing as a 5-year old


%Starting points for data simulation
N(1,1:3)=[100,600,150];
N(2,1:3)=[100,600,150];
N(3,1:3)=[100,600,150];
N(4,2:3)=[600,150];
N(5,3)=150;

%Data simulation
for t=1:150
    S(t)=sum(N(t,:));
    juv(t)=exp(log(a)+log(S(t))-log(1+ a/b*S(t))+random('norm',2,0.1,[1,1]));%nonlinear func, w/ LogNorm error.
    N(t+3,1)=juv(t)*phi(t)*phi(t+2)*phi(t+3)*gam3;%survive first 3 years then mature
    N(t+4,2)=juv(t)*phi(t)*phi(t+2)*phi(t+3)*(1-gam3)*phi(t+4)*gam4;%survive first 3, don't mature, surive 4th then mature.
    N(t+5,3)=juv(t)*phi(t)*phi(t+2)*phi(t+3)*(1-gam3)*phi(t+4)*(1-gam4)*phi(t+5)*gam5;
end
Ni=round(N);%put the simulation results back to integers
plot(sum(N,2))

由于我喜欢MATLAB,因此我正在使用matjags.m来调用JAGS。这是初始化变量的方法:

Data = struct('N',Ni);
nchains  = 4; % How Many Chains?
nburnin  = 35000; % How Many Burn-in Samples?
nsamples = 10000;  % How Many Recorded Samples?
% To run a different recrutitment model, change the last number in the 
% lines in the loop below to match the needed number of paramters.
clear init I initStructs
for i=1:nchains
    I.a= 5+2*rand([1,1]);
    %I.b= 1000+2000*rand([1,4]);%BH
    I.b= 0.01*rand([1,1]);%RK
    I.sig_tau =  0+2*rand([1,1]);  %Log scale
    I.gam3= 0.1*rand([1,1]);
    I.gam4= 0.3 +0.4*rand([1,1]);
    I.gam5= 0.3 +0.4*rand([1,1]);
    I.phi= random('beta',10,7,[38,1]);
    I.p= drchrnd([1,1 1],1);
    init(i) = I; % init is a structure array that has the initial values for all latent variables for each chain
end
initStructs=init;

现在到重要部分-JAGS模型。由于该过程具有交错的(对角线)性质,因此我将通过在前12行中给JAGS一个起点来简化事情(类似于模拟),并且我将不使用我所使用的所有数据。模拟过。

model{
Ni[1,1]<-524
Ni[1,2]<-540
Ni[1,3]<-1474
Ni[2,1]<-365
Ni[2,2]<-2598
Ni[2,3]<-127
Ni[3,1]<-178
Ni[3,2]<-2340
Ni[3,3]<-789
Ni[4,2]<-1247
Ni[4,3]<-775
Ni[5,3]<-356

 for (t in 1:30){
  #Recruitment function
   #S[t]<-Ni[t,1]+Ni[t,2]+Ni[t,3]
   S[t]~dsum(Ni[t,1]+Ni[t,2]+Ni[t,3])
   mu[t]<-log(a)+log(S[t])-log(1+a/b*S[t]
   logsmolts[t]~dnorm(mu[t],tau)

  #Survival and Maturation
   Ni[t+3,1]<-exp(logsmolts[t])*phi[t+1]*1*phi[t+2]*1*phi[t+3]*gam3
   Ni[t+4,2]<-exp(logsmolts[t])*phi[t+1]*1*phi[t+2]*1*phi[t+3]*(1-gam3)*phi[t+4]*gam4
   Ni[t+5,3]<-exp(logsmolts[t])*phi[t+1]*1*phi[t+2]*1*phi[t+3]*(1-gam3)*phi[t+4]*(1-gam4)*phi[t+5]*1
   Sstar[t]<-trunc(sum(Ni[t,]))  
   N[t,1:3]~dmulti(p[t,1:3],Sstar[t])
  } 

#priors
a~dunif(1,100)
b~dunif(1,2000)

tau<-pow(sig.tau,-2)
sig.tau~dunif(0,4)
sig<-sqrt(1/tau)

gam3~dbeta(1,1)
gam4~dbeta(1,1)
#gam5~dbeta(1,1) #this value must be 1.

for (t in 1:30){
 p[t,1:3]~ddirch(alpha[])
}


for (t in 1:35){
 phi[t]~dbeta(1,1)
}

}   

以及JAGS的输出

  

运行时错误:第27行上的编译错误。无法解析节点   p [1,1:3]这可能是由于未定义的祖先节点或有向节点   图删除模型中的循环

0 个答案:

没有答案