如何从mcmc.list对象中获取均值?

时间:2014-03-07 21:18:28

标签: r mcmc r-coda

output = RBugsfit(..., coda=T, ...)输出一个mcmc.list对象,其中包含四个参数的后验分布样本及其样本后验均值。使用summary()我可以看到样本后验的意思,但我想知道如何将output中的样本后验方法检索到我程序中的变量中?谢谢!

> summary(output)

Iterations = 201:3396
Thinning interval = 5 
Number of chains = 2 
Sample size per chain = 640 

1. Empirical mean and standard deviation for each variable,
   plus standard error of the mean:

           Mean        SD  Naive SE Time-series SE
beta  1.052e+00 3.189e-02 8.914e-04      9.185e-04
df    3.849e+00 2.916e-01 8.150e-03      1.516e-02
sigma 1.056e-02 2.504e-04 6.998e-06      1.000e-05
tau   8.990e+03 4.273e+02 1.194e+01      1.710e+01

2. Quantiles for each variable:

           2.5%       25%       50%       75%     97.5%
beta  9.891e-01 1.032e+00 1.052e+00 1.073e+00 1.113e+00
df    3.304e+00 3.650e+00 3.836e+00 4.042e+00 4.450e+00
sigma 1.004e-02 1.039e-02 1.055e-02 1.072e-02 1.105e-02
tau   8.197e+03 8.700e+03 8.977e+03 9.263e+03 9.917e+03

> str(output)
List of 2
 $ : mcmc [1:640, 1:4] 1.1 1.03 1.05 1.12 1.07 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:4] "beta" "df" "sigma" "tau"
  ..- attr(*, "mcpar")= num [1:3] 201 3396 5
 $ : mcmc [1:640, 1:4] 1.03 1.04 1.06 1.06 1.07 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:4] "beta" "df" "sigma" "tau"
  ..- attr(*, "mcpar")= num [1:3] 201 3396 5
 - attr(*, "class")= chr "mcmc.list"

1 个答案:

答案 0 :(得分:1)

我通常使用两种方式:

1)使用摘要(假设输出是mcmc.list类):

s <- summary(output)
m <- s$statistics[,"Mean"]

2)自己做:

mt <- as.matrix(output)
m <- apply(mt, 2, mean)