从复制函数中提取值

时间:2016-10-05 19:24:42

标签: r replication extraction

我是R的新手,正在使用R2OpenBUGS进行MCMC模拟。我的模型有效,我尝试有效地进行多次迭代。这是我多次迭代的代码。

mcmc.s=function(n1,m1,k1,al1,be1,R.x,n2,m2,k2,al2,be2,R.y) 
{
  tq.x=replicate(n1, aa(k1,al1,be1)) #produces data
  tq.y=replicate(n2, aa(k2,al2,be2)) #produces data

  x=pfF(n1,m1,k1,tq.x) #extracts samples from data
  y=pfF(n2,m2,k2,tq.y) #extracts samples from data

  x=c(x) #Need data inputed as a list
  y=c(y) #Need data inputed as a list

  datatemp<- list( "x","y","R.x","k1","m1","R.y","k2","m2") #these are just my inputs
  aaa<-rgamma(1, shape=4.5,rate=52) #prior
  bbb<-rgamma(1, shape=4.5,rate=52) #prior
  ccc<-rgamma(1, shape=20.5,rate=3.5) #prior

  initstemp<-function(){list(a=aaa,b=bbb,c=ccc)} #assigns priors to parameters in model
  bugstemp = bugs(data=datatemp,inits=initstemp,parameters.to.save=c("a","b","c"),n.iter= 50000, model.file=mtemp,
                  n.chains=3, n.burnin=40000,n.thin=1,codaPkg =F, debug=F)
  out<-bugstemp$summary
  acalc<-out["a","mean"];bcalc<-out["b","mean"];ccalc<-out["c","mean"] #line not necessary
  return(out)
}

iter=2
out=replicate(iter,mcmc.s(n1,m1,k1,al1,be1,R.x,n2,m2,k2,al2,be2,R.y))
out

结果如下:

, , 1

                mean         sd        2.5%      25%       50%      75%      97.5%     Rhat n.eff
a         0.07904428 0.03935658  0.02335975  0.05045  0.072255  0.10020  0.1738025 1.001631  2900
b         0.03557510 0.01966588  0.00946790  0.02168  0.031765  0.04493  0.0842925 1.001946  2000
c         7.05248063 2.62808107  3.24794998  5.19200  6.603000  8.43600 13.3402499 1.001574  5600
deviance 61.80275800 2.58984760 58.86000000 59.93000 61.130000 62.98000 68.6200000 1.006417   720

, , 2

               mean         sd        2.5%     25%      50%     75%     97.5%     Rhat n.eff
a         0.1992745 0.08962024  0.06359975  0.1340  0.18575  0.2506  0.413400 1.002039  1800
b         0.1648684 0.07519227  0.05357925  0.1103  0.15280  0.2070  0.341905 1.001840  2200
c         7.2121513 1.92005357  3.90300000  5.8610  7.05700  8.4000 11.320250 1.001200  8000
deviance 47.8683650 2.54986184 44.99000000 46.0300 47.22000 48.9900 54.530000 1.001291  5800

有没有办法提取参数a,b和c的均值?我想在模拟函数中使用for循环,如:

acalc<-numeric(length=iter)
bcalc<-numeric(length=iter)
ccalc<-numeric(length=iter)
for (i in 1:iter){acalc[i]<-out["a","mean"];bcalc[i]<-out["b","mean"];ccalc[i]<-out["c","mean"]}

但这似乎不起作用。关于如何提取这些信息的任何想法都会非常有帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

我其实只是解决了自己的问题。迭代结果中的每个值都可以用:out [some number]提取。例如:

out[1]=0.07904428, out[2]=0.03557510, out[3]=7.05248063 , out[4]=61.80275800, out[5]=0.03935658

因此,“out”就像一个大的矢量“蜿蜒”通过我的所有数据。一旦它“蜿蜒”通过我的第一次迭代结果,它就转移到我的第二次迭代。因为每次迭代将始终具有相同的维度,所以我想要提取的每个值总是远离我的起点36个位置。这种实现使我有可能创建一个for循环,使我只能得到我想要的值。

#This is for acalc
for (i in 1:length(out)){acalc[i]<-out[(i-1)*36+1]}
acalc <- as.numeric(na.omit(acalc))

#This is for bcalc
for (i in 1:length(out)){bcalc[i]<-out[(i-1)*36+2]}
bcalc <- na.omit(bcalc)

#This is for ccalc
for (i in 1:length(out)){ccalc[i]<-out[(i-1)*36+3]}
ccalc <- na.omit(ccalc)

for循环导致向量等于out结果的长度,但它只填充了每次迭代中与参数对应的值。这就是我使用na.omit摆脱NA的原因。

感谢任何花时间审阅我的问题的人。