Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output a table with standard errors below estimates

Tags:

r

latex

sweave

So I've got some parameter estimates like so

est<-matrix(1:10,nrow=2)
colnames(est)<-c("a","b","c","d","e")

and I've got some standard errors like so

se<-matrix(seq(0.1,1,by=0.1),nrow=2)
colnames(se)<-c("a","b","c","d","e")

and I want to output a Latex (or HTML) table where each parameter estimate has its standard error in parens just below it.

The table should look something like this

    a    &   b    &  c    &  d    &  e
    1    &   3    &  5    &  7    &  9
  (0.1)  &  (0.3) & (0.5) & (0.7) & (0.9)
    2    &   4    &  6    &  8    &  10
  (0.2)  &  (0.4) & (0.6) & (0.8) & (1.0)

except, you know, in proper Latex (or HTML). How can I do this from R?

like image 610
njt Avatar asked Aug 27 '26 22:08

njt


2 Answers

If you don't mind having row labels, the texreg package can offer a solution:

# your original code:
est <- matrix(1:10, nrow = 2)
colnames(est) <- c("a", "b", "c", "d", "e")
se <- matrix(seq(0.1, 1, by = 0.1), nrow = 2)
colnames(se) <- c("a", "b", "c", "d", "e")

# add row labels:
rownames(est) <- c("row 1", "row 2")
rownames(se) <- c("row 1", "row 2")

library("texreg")

# create a texreg object:
tr <- list()
for (j in 1:ncol(est)) {
  tr[[j]] <- createTexreg(
      coef.names = rownames(est), 
      coef = est[, j], 
      se = se[, j]
  )
}

# for text output:
screenreg(tr, custom.model.names = colnames(est), 
    custom.note = "")

# for LaTeX output:
texreg(tr, custom.model.names = colnames(est), 
    custom.note = "")

# for HTML output:
htmlreg(tr, custom.model.names = colnames(est), 
    custom.note = "")

For example, the text output would look like this:

=============================================
       a       b       c       d       e     
---------------------------------------------
row 1   1.00    3.00    5.00    7.00    9.00 
       (0.10)  (0.30)  (0.50)  (0.70)  (0.90)
row 2   2.00    4.00    6.00    8.00   10.00 
       (0.20)  (0.40)  (0.60)  (0.80)  (1.00)
=============================================

You can as well omit the top, bottom and mid rules by specifying additional arguments to the screenreg function (inner.rule = "" and outer.rule = "").

Note that you should have texreg (>= 1.29.7) installed.

like image 139
Philip Leifeld Avatar answered Aug 30 '26 11:08

Philip Leifeld


Two steps:

Create matrix with data in table

M <- matrix(as.vector(rbind(as.character(est),
                            paste("(",as.vector(se),")", sep="")
                            )
             ), nrow=4)
colnames(M) <- colnames(est)

Write matrix as latex or html table:

library(xtable)
print(xtable(M),type="latex") # or type="html" 
like image 30
Wojciech Sobala Avatar answered Aug 30 '26 12:08

Wojciech Sobala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!