bayes.table: Put results of MCMC sampling into a nicely formatted table...

Description Usage Arguments Value Examples

View source: R/bayes_table.r

Description

Put results of MCMC sampling into a nicely formatted table for Latex, HTML, or plaint text. It processes MCMC output and the outcome data. This function is built on top of Philip Leifeld's texreg package.

Usage

1
2
3
4
5
bayes.table(datalist, ylist, yreplist = NULL, include.nobs = TRUE,
  include.rsquared = FALSE, include.eff.size = TRUE,
  include.geweke = TRUE, custom.coef.map = NULL,
  custom.model.names = NULL, HPDI = TRUE, HPDI.prob = 0.95,
  output = "latex", caption = "")

Arguments

datalist

a list of coda::mcmc.list objects from different models

ylist

a list of vectors of the instances of a dependent variable

yreplist

(optional) a list coda::mcmc.list object with the samples of predicted values

include.nobs

TRUE if number of observations should be included, FALSE otherwisr

include.eff.size

TRUE if effective size of MCMC chain should be included, FALSE otherwise

include.geweke

if TRUE, Geweke diagnostic is calculated for all the chains, and the largest value is included

custom.coef.map

list of new names for the variables, should be in the form list("oldname1"="newname1", "oldname2"="newname2"). Variables that are not in this mapping are not included. This is passed directly to texreg function

custom.model.names

a vector of model labels. This is passed directly to texreg function

HPDI

if TRUE, Highest Probability Density Intervals are shown instead of standard errors. Default is TRUE

HPDI.prob

probability for HPD intervals. Defauld is 0.95

output

output format: can be latex, html, or word. Default is latex

caption

a caption for the table. This is passed directly to texreg function

include.rquared

TRUE if quasi-bayesian r-squared should be included, FALSE otherwise

Value

a character string that contains Late, HTML, or plain text for the rable

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
library(rjags)
library(coda)
library(texreg)
library(bayestable)

## Generating some fake data
N <- 100
x1 <- rnorm(N)
x2 <- rnorm(N)
beta <-c (0.3,-0.3)
y <- 0.1 + beta[1]*x1 + beta[2]*x2 + rnorm(N)

## Define three JAGS models
code1<-"
  model{
    for (i in 1:N){
      y[i] ~ dnorm(mu[i], sigma)
      mu[i] <- a
    }
   a ~ dnorm(0, 0.001)
   sigma <- pow(tau, -2)
   tau ~ dunif(0,100)
}"

code2<-"
 model{
   for (i in 1:N){
     y[i] ~ dnorm(mu[i], sigma)
     mu[i]<-a+beta1*x1[i]
   }
   a ~ dnorm(0, 0.001)
   beta1 ~ dnorm(0,0.001)
   sigma <- pow(tau, -2)
   tau ~ dunif(0,100)
}"

code3<-"
  model{
   for (i in 1:N){
    y[i] ~ dnorm(mu[i], sigma)
    mu[i]<-a + beta1*x1[i] + beta2*x2[i]
  }
  a ~ dnorm(0, 0.001)
  beta1 ~ dnorm(0,0.001)
  beta2 ~ dnorm(0,0.001)
  sigma <- pow(tau, -2)
  tau ~ dunif(0,100)
}"
## connecting to the models
model1.spec<-textConnection(code1)
model2.spec<-textConnection(code2)
model3.spec<-textConnection(code3)

## compiling the code into JAGS models
jags1<-jags.model(model1.spec, data = list('N'=N, 'y'=y), quiet = T)
jags2<-jags.model(model2.spec, data = list('x1'=x1,'N'=N, 'y'=y), quiet = T)
jags3<-jags.model(model3.spec, data = list('x1'=x1,'x2'=x2,'N'=N, 'y'=y), quiet = T)

## Sampling regression parameters
samples1<-coda.samples(jags1, variable.names = c("a"), n.iter=1000,nchain=4)
samples2<-coda.samples(jags2, variable.names = c("a", "beta1"), n.iter=1000,nchain=4)
samples3<-coda.samples(jags3, variable.names = c("a", "beta1", "beta2"),n.iter=1000,nchain=4)

## sampling fitted values
samples1.rep<-coda.samples(jags1, variable.names = c("mu"), n.iter=1000,nchain=4)
samples2.rep<-coda.samples(jags2, variable.names = c("mu"), n.iter=1000,nchain=4)
samples3.rep<-coda.samples(jags3, variable.names = c("mu"), n.iter=1000,nchain=4)

#' ## creating a list of samples from three different models
datalist<-list(samples1, samples2, samples3)

## creating a list of outcomes for different models (here we used the same set of outcomes)
ylist<-list(y,y,y)

## creating a list of samples of fitted values from different models
yreplist<-list(samples1.rep, samples2.rep, samples3.rep)

## generating table
bayes.table(datalist, ylist, yreplist,
           custom.coef.map = list("a" = "Intercept",
                                   "beta1" = "GDP",
                                   "beta2" = "Polity"),
           include.rsquared = T, HPDI.prob = 0.97)

ananyevm/bayestable documentation built on May 5, 2019, 2:41 a.m.