Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export result of glht to LaTeX in R

Tags:

r

latex

I want to export the result of a glht object in R into a LaTeX table.

For example with the library "stargazer" one can produce a pretty formatted LaTeX table of a lme object.

I want have a automatically created LaTeX table from the output of the summary of the glht object, such as the summary created with

>summary(glht(dataModel))
Linear Hypotheses:
                                                                        Estimate Std.     Error z value Pr(>|z|)    
Group1 - Group2 == 0   -0.14007    0.01589  -8.813   <0.001 "***"
Group1 - Group3 == 0    -0.09396    0.01575  -5.965   <0.001 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 
(Adjusted p values reported -- single-step method)

I'm aware of libraries like stargazer, xtable, texreg, reporttools, memisc and apsrtable, but none of them does the job for glht :(

any hints on whether there's a library for that?

like image 950
b00tsy Avatar asked Oct 12 '13 12:10

b00tsy


2 Answers

You might find the answer right in the code example below:

multcomp:::print.summary.glht 

x<-glht(...)    
pq<-summary(x)$test

mtests <- cbind(pq$coefficients, pq$sigma, pq$tstat, pq$pvalues)
error <- attr(pq$pvalues, "error")
pname <- switch(x$alternativ, 
                 less = paste("Pr(<", ifelse(x$df ==0, "z", "t"), ")", sep = ""), 
                 greater = paste("Pr(>", ifelse(x$df == 0, "z", "t"), ")", sep = ""), 
                 two.sided = paste("Pr(>|", ifelse(x$df == 0, "z", "t"), "|)", sep = ""))                                                                   
colnames(mtests) <- c("Estimate", "Std. Error", ifelse(x$df ==0, "z value", "t value"), pname)

xtable(mtests)
like image 143
RNewbie Avatar answered Oct 15 '22 12:10

RNewbie


Rich from R-help gave the helping clue:

The trick for latexing glht objects is recognizing that they are very complex. It is necessary to isolate the part you want first, then the latex() function in Hmisc works very well.

This example is based on one of the examples in ?glht

library(Hmisc)
library(multcomp)

### set up a one-way ANOVA
amod <- aov(breaks ~ tension, data = warpbreaks)

### set up all-pair comparisons for factor `tension'
### using a symbolic description (`type' argument
### to `contrMat()')
amod.glht <- glht(amod, linfct = mcp(tension = "Tukey"))

latex(confint(amod.glht)$confint, dec=3)

Well that doesn't print exactly what summary(amod.glht) would print, yet, but latex() is the missing function I was looking for

like image 2
b00tsy Avatar answered Oct 15 '22 10:10

b00tsy