2

I'm using the coda package to calculate the summary statistics of my MCMC. However, there seems to be no options to convert the printed summary into a Latex table. I've tried stargazer, and coercing summary.mcmc results to a data frame. Both attempts have failed.

Here's a reproducible example:

library(coda)
mock_mcmc <- mcmc(rnorm(1000))
summary(mock_mcmc)

summary.mcmc will print out

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

          Mean             SD       Naive SE Time-series SE 
       0.03180        0.98715        0.03122        0.03368 

2. Quantiles for each variable:

    2.5%      25%      50%      75%    97.5% 
-1.89794 -0.65289  0.02952  0.67396  1.97158 

How do I output that result table into a Latex file? I understand that it is possible to calculate the summary statistics by hand, but I'm curious whether there's a convenient feature of coda that I don't know about.

jaradniemi
  • 618
  • 4
  • 15
Heisenberg
  • 8,386
  • 12
  • 53
  • 102
  • What exactly do you want in this table? You should include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output. Also be more explicit about how attempts have failed. – MrFlick Apr 13 '16 at 02:54
  • I've added an example. – Heisenberg Apr 13 '16 at 02:58

2 Answers2

0

I don't know if there's a way to do it in coda. The method below defines a method of xtable for mcmc objects by converting the object to a dataframe and then rerunning xtable.

library(coda)
library(dplyr)
library(magrittr)
library(xtable)

xtable.summary.mcmc = function(x, ...)
  x %>%
  use_series(statistics) %>%
  c(x %>%
      use_series(quantiles) ) %>%
  as.list %>%
  dplyr::as_data_frame() %>%
  xtable(...)

1000 %>% 
  rnorm %>% 
  mcmc %>% 
  summary %>%
  xtable                    
bramtayl
  • 4,004
  • 2
  • 11
  • 18
0

The key is to extract the relevant quantities from the output. To find the appropriate quantities, use the names() function in R.

library(coda)
library(xtable)

mock_mcmc = mcmc(rnorm(1000))
s = summary(mock_mcmc)

stats_table = xtable(as.data.frame(t(s$statistics)))
quant_table = xtable(as.data.frame(t(s$quantiles)))

print(stats_table, file="stats_table.tex")
print(quant_table, file="quant_table.tex")
jaradniemi
  • 618
  • 4
  • 15