我们模型的几个参数未能在WinBUGS中收敛

时间:2018-05-02 09:50:11

标签: r winbugs stan

我建立了贝叶斯模型,其中包括文献中的一些方程。我想根据监测数据同时模拟十个参数(P,R,Rear,K,R20,mu,a,b,tau和sigma)。该模型在语法上是正确的,加载数据并编译得很好。我更新了1000000.它可以给出每个参数的结果。

但我们模型的三个参数(mu,a和b)似乎总是无法在WinBUGS中收敛。当我只模拟这三个参数时,这些参数也无法收敛。

问题在于我不知道如何调整它。我试图多次改变一些参数的pior分布。但没用。我也不知道如何估计模型,因为DIC结果显示“未定义的实际结果”。

我搜索了我们的网站,发现还有一个类似的问题。

我得到了以下答案: “WinBUGS已有几年历史了,自上次更新以来已有8年了!我认为你应该忘记它,因为有几个更好的选择。 您可以在Jags或Stan中尝试几乎相同的代码,其中两者都可以通过rJags和RStan在R中使用。 Stan特别重要,因为它使用的MCMC在WinBUGS没有的许多情况下会聚。 “ WinBUGS Weibull Network Meta-Analysis

然而,我也是Jags或Stan的新手。我不知道如何将其更改为STAN代码。

我很感激任何关于如何调整这个(模型结构,方程,pior分布,初始值等等)的建议,以使其正常运行。 另外,我想用R进行分析。

提前致谢。

这是我的模特

    model; 
{ 
for( i in 1 : N ) {
lambda[i] ~ dnorm(u[i],tau)  }

for( i in 1 : N ) {
u[i] <- P[i] - R[i] + Rear[i]
P[i] <- mu * IZ[i]  
IZ[i] <- I[i] * exp(e[i])  
e[i] <- (-1) * Kd[i]
Kd[i] <- a + b * log(Tur[i])

R[i] <- R20 * pow(1.047,T[i] - 20)
Rear[i] <- K * (Os[i] - O[i])
Os[i] <- 14.62 - 0.3671 * T[i] + (0.004497 * T[i]) * T[i] - 0.966 * Sa[i] + (0.00205 * Sa[i]) * T[i] + (2.739E-4 * Sa[i]) * Sa[i]

T[i] ~ dnorm( 0.0,1.0E-6)
I[i] ~ dnorm( 0.0,1.0E-6) 
Tur[i] ~ dnorm(10, 0.1)I(0,)  
Sa[i] ~ dnorm( 0.0,1.0E-6)
O[i] ~ dnorm( 0.0,1.0E-6)  
  }


tau ~ dgamma(0.001,0.001)
R20 ~ dnorm( 0.0,1.0E-6)
K ~ dnorm( 0.0,1.0E-6)
mu ~ dnorm( 0.0,1.0E-6)
b ~ dnorm( 0.0,1.0E-6)
a ~ dnorm( 0.0,1.0E-6)
sigma <- 1 / sqrt(tau) 
} 

这是我的数据

list(lambda=c(0.3, 0, 0.03, 0.12,   0.13,   0.12,   0.03,   0.27,   0.29,   0.02,   0.2,    0.25,   0.26,   0.15,   0.16,   0.74,   0.3,    0.4,    0.4,    0.28,   0.15,   -0.15,  -0.07,  0.02,   -0.13,  -0.3,   -0.26,  -0.36,  -0.26,  -0.28,  -0.26,  -0.32,  -0.18,  -0.29,  -0.27,  -0.09,  -0.32,  -0.21,  -0.18,  -0.16,  -0.23,  -0.18,  -0.16,  -0.13,  -0.18,  -0.07,  -0.15,  -0.11,  -0.03,  0.01,   0.03,   0.12,   -0.07,  0.12,   0.08,   0.18,   0.24,   0.3,    0.08,   0.35,   0.27,   0.29,   0.02,   0.2,    0.25,   0.26,   0.24,   0.2,    0.2,    -0.09,  -0.2,   -0.16,  -0.16,  -0.08,  -0.06,  -0.17,  -0.32,  -0.14,  -0.18,  -0.32,  -0.16,  -0.28,  -0.04,  -0.27,  -0.15,  -0.06,  -0.15,  -0.11,  -0.15,  -0.11,  -0.03,  -0.25,  -0.02,  -0.06,  -0.16,  -0.08,  0.08),
  T=c(27.45,27.56,27.33,27.39,27.61,27.61,28,28.4,28.78,29,29.5,29.94,30,30.22,30.56,31,31.4,31.8,31.9,30.82,30.15,30.25,30.25,29.86,29.94,29.76,29.52,29.26,29.02,28.8,28.59,28.33,28.09,27.85,27.63,27.46,27.31,27.17,27.04,26.91,26.77,26.63,26.52,26.41,26.29,26.13,26.01,25.88,25.79,25.79,25.77,25.75,25.76,25.86,25.8,25.85,26.01,26.15,26.27,26.49,26.74,27,27.56,27.87,27.83,28.02,28.36,28.67,29.11,29.45,29.55,29.35,28.67,28.52,28.05,27.98,27.91,27.55,27.26,26.99,26.72,26.46,26.21,25.98,25.71,25.44,25.23,25.01,24.79,24.6,24.41,24.23,24.05,23.88,23.73,23.59,23.48),
I=c(24,52,141,138,179,193,148,306,210,423,306,258,235,385,157,382,598,517,427,603,279,196,235,124,132,87,42,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,20,24,52,141,138,179,193,148,306,210,423,306,258,235,385,157,382,598,517,427,603,279,196,235,124,132,87,42,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,20,59,90,109,112,86,143,206,248,244,260,384,361,460,563,398,204,510,614,608,588,523,430,332,246,161,73,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,22,90),
 Sa=c(2.67,2.67,2.68,2.68,2.67,2.67,2.67,2.67,2.67,2.67,2.67,2.67,2.67,2.68,2.67,2.67,2.67,2.67,2.67,2.67,2.67,2.67,2.67,2.68,2.68,2.67,2.67,2.67,2.67,2.67,2.67,2.67,2.67,2.67,2.68,2.67,2.67,2.67,2.68,2.67,2.67,2.67,2.68,2.67,2.67,2.67,2.68,2.67,2.68,2.67,2.68,2.67,2.68,2.71,2.68,2.6,2.68,2.68,2.68,2.68,2.69,2.69,2.69,2.69,2.68,2.68,2.69,2.68,2.7,2.69,2.69,2.695,2.697,2.699,2.701,2.703,2.69,2.7,2.7,2.69,2.7,2.7,2.69,2.7,2.69,2.7,2.7,2.7,2.7,2.7,2.7,2.7,2.7,2.69,2.69,2.7,2.7),
 O=c(6.23,6.53,6.53,6.56,6.68,6.81,6.93,6.96,7.23,7.52,7.54,7.74,7.99,8.25,8.4,8.56,9.3,9.6,10,10.4,10.68,10.83,10.68,10.61,10.63,10.5,10.2,9.94,9.58,9.32,9.04,8.78,8.46,8.28,7.99,7.72,7.63,7.31,7.1,6.92,6.76,6.53,6.35,6.19,6.06,5.88,5.81,5.66,5.55,5.52,5.53,5.56,5.68,5.61,5.73,5.81,5.99,6.23,6.53,6.61,6.96,7.23,7.52,7.54,7.74,7.99,8.25,8.49,8.69,8.89,8.8,8.6,8.44,8.28,8.2,8.14,7.97,7.65,7.51,7.33,7.01,6.85,6.57,6.53,6.26,6.11,6.05,5.9,5.79,5.64,5.53,5.5,5.25,5.23,5.17,5.01,4.93),   

N=97)

list(mu=0.5, a=1, b=2, K=0.5, R20=1, tau=1) 

部分结果

ode statistics 
node     mean    sd  MC error   2.5%    median  97.5%   start   sample
mu  713.5   620.3   17.57   0.07538 590.0   2162.0  4001    996000   

node     mean    sd  MC error   2.5%    median  97.5%   start   sample
a   8.924   2.585   0.08136 0.6363  9.707   11.45   4001    996000

迭代的时间序列

enter image description here enter image description here

输入模型的变量的快速摘要:

T : water temperature
I: solar irradiance at water surface
Sa: water salinity
O: disolved oxygen

(P, R, Rear, K, R20, mu, a, b, tau, and sigma)
P: primary prodution 
R: ecosystem respiration
Rear: molecular diffusivity of dissolved oxygen across the air–water interface
K: gas transfer velocity parmeter
R20: respiration at T= 20℃
mu: the slope of the photosynthesis–irradiance relationship at low light conditions
a and b: regression coefficients between turbidity and irradiance attenuation coefficient
tau: the precision?
sigma: the standard error?

1 个答案:

答案 0 :(得分:1)

在Stan语言中会写这样的东西,我想

data {
  int<lower=1> N;
  vector[N] lambda;
  vector[N] T;
  vector[N] I;
  vector[N] Sa;
  vector[N] O;
}
parameters {
  vector<lower=0>[N] Tur;
  real<lower=0> sigma;
  real R20;
  real K;
  real mu;
  real a;
  real b;
}
model {
  vector[N] u;
  for (i in 1:N) {
    real Os = 14.62 - 0.3671 * T[i] + (0.004497 * T[i]) * T[i] - 0.966 * Sa[i] + (0.00205 * Sa[i]) * T[i] + (2.739E-4 * Sa[i]) * Sa[i];
    real Rear = K * (Os - O[i]);
    real R = R20 * 1.047^(T[i] - 20);
    real Kd = a + b * log(Tur[i]);
    real e = -1 * Kd;
    real IZ = I[i] * exp(e);
    real P = mu * IZ;
    u[i] = P - R + Rear;
  }
  target += normal_lpdf(lambda | u, sigma);

  target += normal_lpdf(Tur | 10, 3.162278); // do not need to truncate
  // other priors in the form of target += distribution_lpdf( | , );
}

但是,您的BUGS模型中存在一些令人困惑的事情。首先,您有TISaO的抽样语句,即使这些语句显然是数据,而采样语句只涉及常量。常量与Stan中的参数提议无关或哪些提案被接受,因此您可以省略所有这些。如果您需要随机抽取它们以便在一些较大的人口中进行预测,您可以在Stan中使用generated quantities块在Stan程序结束时执行此操作,或者您可以在R中执行此操作。

其次,你正在使用的这些先验 - 虽然在BUGS模型中很常见 - 与推理是对立的。对于这样的模型(以及许多其他模型),您将不得不表达您对这些参数的实际信念,而不是说空洞的事情,例如mu介于-1000之间的可能性为三分之二和斯坦有许多recommended先验。另外,请记住,Stan中正态分布的参数化是根据期望值和标准偏差(而不是精度)来实现的。

另请参阅Stan用户manualFAQ的附录B.如果您使用更好的先验,它可能会起作用,但要注意第一次尝试时可能会收到的警告信息(表明您定义的后验分布不利于采样)。