user3284469
user3284469

Reputation:

How to get the mean from mcmc.list object?

output = RBugsfit(..., coda=T, ...) output a mcmc.list object, which contains samples of the posterior distributions of four parameters, and their sample posterior means. Using summary() I can see the sample posterior means, but I wonder how to retrieve the sample posterior means from output into a variable in my program? Thanks!

> 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"

Upvotes: 3

Views: 3664

Answers (1)

Tomas
Tomas

Reputation: 59515

I usualy use two ways:

1) using a summary (assuming output is of class mcmc.list):

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

2) do it myself:

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

Upvotes: 1

Related Questions