4

I'm about to write a function that yields both a screen output and a LaTeX/PDF-output of lists of regression tables and have problems with the decimal point alignment of the latter.

From this toy table regmat,

                1                 2                 3                 4                
(Intercept)     "      33.99 ***" "    3370.83 ***" "      39.42 ***" "      46.80 ***"
                "      (1.89)   " "    (260.49)   " "      (2.64)   " "      (6.37)   "
cyl             "       ---     " "       ---     " "      -1.51 ** " "       ---     "
                "       ---     " "       ---     " "      (0.42)   " "       ---     "
factor(am)1     "       ---     " "     180.92    " "       0.18    " "       0.19    "
                "       ---     " "    (139.63)   " "      (1.30)   " "      (1.49)   "
factor(cyl)6    "      -4.26 ** " "    -303.13 *  " "       ---     " "      -1.28    "
                "      (1.39)   " "    (140.73)   " "       ---     " "      (1.53)   "
factor(cyl)8    "      -6.07 ***" "    -216.37    " "       ---     " "      -1.39    "
                "      (1.65)   " "    (228.43)   " "       ---     " "      (2.16)   "
hp              "       ---     " "      -3.21 *  " "       ---     " "      -0.10 ** "
                "       ---     " "      (1.37)   " "       ---     " "      (0.03)   "
wt              "      -3.21 ***" "    -249.68 ** " "      -3.13 ** " "      -7.12 ** "
                "      (0.75)   " "     (88.56)   " "      (0.91)   " "      (2.24)   "
wt:hp           "       ---     " "       ---     " "       ---     " "       0.02 *  "
                "       ---     " "       ---     " "       ---     " "      (0.01)   "
Deviance        "    1126.05    " "11260471.88    " "    1126.05    " "    1126.05    "
Df              "      31       " "      31       " "      31       " "      31       "
Log-Likelihood  "     -73.31    " "    -217.60    " "     -73.99    " "     -67.34    "
AIC             "     156.62    " "     449.20    " "     157.99    " "     150.69    "
BIC             "     163.95    " "     459.46    " "     165.32    " "     162.41    "
Resid. deviance "     183.06    " " 1510255.92    " "     191.05    " "     126.07    "
Resid. Df       "      28       " "      26       " "      28       " "      25       "
Num. obs.       "      32       " "      32       " "      32       " "      32       "

I get a nice screen output with as.data.frame(regmat);

> as.data.frame(regmat)
                              1               2               3               4
(Intercept)           33.99 ***     3370.83 ***       39.42 ***       46.80 ***
                      (1.89)        (260.49)          (2.64)          (6.37)   
cyl                    ---             ---            -1.51 **         ---     
                       ---             ---            (0.42)           ---     
factor(am)1            ---           180.92            0.18            0.19    
                       ---          (139.63)          (1.30)          (1.49)   
factor(cyl)6          -4.26 **      -303.13 *          ---            -1.28    
                      (1.39)        (140.73)           ---            (1.53)   
factor(cyl)8          -6.07 ***     -216.37            ---            -1.39    
                      (1.65)        (228.43)           ---            (2.16)   
hp                     ---            -3.21 *          ---            -0.10 ** 
                       ---            (1.37)           ---            (0.03)   
wt                    -3.21 ***     -249.68 **        -3.13 **        -7.12 ** 
                      (0.75)         (88.56)          (0.91)          (2.24)   
wt:hp                  ---             ---             ---             0.02 *  
                       ---             ---             ---            (0.01)   
Deviance            1126.05     11260471.88         1126.05         1126.05    
Df                    31              31              31              31       
Log-Likelihood       -73.31         -217.60          -73.99          -67.34    
AIC                  156.62          449.20          157.99          150.69    
BIC                  163.95          459.46          165.32          162.41    
Resid. deviance      183.06      1510255.92          191.05          126.07    
Resid. Df             28              26              28              25       
Num. obs.             32              32              32              32       

the decimal points are all aligned, even in lines with parentheses, and the integers in the lower GOF section end before the points.

Now I want a similar LaTeX/PDF output with xtable using rmarkdown and this code,

library(xtable)
xtbl <- xtable(regmat, comment=FALSE)
align(xtbl) <- c("L{2.4cm}", rep(c("L{2cm}@{\\hskip 0.1cm}"), 4))
print(xtbl,
      tabular.environment="tabularx",
      width=".5\\textwidth",
      hline.after=c(-1, 0, 16, nrow(regmat)),
      comment=FALSE)

where the YAML looks like this

---
output: 
  pdf_document:
    includes:
      in_header: "X:/<myDir>/header.tex"
classoption: 
- a4paper
- landscape
geometry: margin=2cm
header-includes: 
- \pagenumbering{gobble}
- \usepackage{dcolumn}
---

and header.tex -- as suggested here -- looks like so,

\usepackage{array}
\usepackage{tabularx}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\
\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\
\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\
\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{P}[1]{>{\raggedright\tabularxbackslash}p{#1}}

obtaining this failed LaTeX/PDF-table result:

enter image description here

Looks not bad, but as one can see the left-alignment starts at the first non-space character and all the white spaces seem to be ignored.

In rep(c("L{2cm}@{\\hskip 0.1cm}") I tried all of L, C, R, and P of the header.tex, but none of them aligns the values at the decimal point, i.e. doesn't seem to use the package \usepackage{dcolumn} which is written to align decimal points.

I don't care about dcolumn, though, I just find it weird that my white spaces are ignored.

How can I get the same alignment in my LaTeX-PDF-table as in the screen output shown above, either with dcolumn or by not ignoring the white spaces?

Data

regmat <- structure(c("      33.99 ***", "      (1.89)   ", "       ---     ", 
"       ---     ", "       ---     ", "       ---     ", "      -4.26 ** ", 
"      (1.39)   ", "      -6.07 ***", "      (1.65)   ", "       ---     ", 
"       ---     ", "      -3.21 ***", "      (0.75)   ", "       ---     ", 
"       ---     ", "    1126.05    ", "      31       ", "     -73.31    ", 
"     156.62    ", "     163.95    ", "     183.06    ", "      28       ", 
"      32       ", "    3370.83 ***", "    (260.49)   ", "       ---     ", 
"       ---     ", "     180.92    ", "    (139.63)   ", "    -303.13 *  ", 
"    (140.73)   ", "    -216.37    ", "    (228.43)   ", "      -3.21 *  ", 
"      (1.37)   ", "    -249.68 ** ", "     (88.56)   ", "       ---     ", 
"       ---     ", "11260471.88    ", "      31       ", "    -217.60    ", 
"     449.20    ", "     459.46    ", " 1510255.92    ", "      26       ", 
"      32       ", "      39.42 ***", "      (2.64)   ", "      -1.51 ** ", 
"      (0.42)   ", "       0.18    ", "      (1.30)   ", "       ---     ", 
"       ---     ", "       ---     ", "       ---     ", "       ---     ", 
"       ---     ", "      -3.13 ** ", "      (0.91)   ", "       ---     ", 
"       ---     ", "    1126.05    ", "      31       ", "     -73.99    ", 
"     157.99    ", "     165.32    ", "     191.05    ", "      28       ", 
"      32       ", "      46.80 ***", "      (6.37)   ", "       ---     ", 
"       ---     ", "       0.19    ", "      (1.49)   ", "      -1.28    ", 
"      (1.53)   ", "      -1.39    ", "      (2.16)   ", "      -0.10 ** ", 
"      (0.03)   ", "      -7.12 ** ", "      (2.24)   ", "       0.02 *  ", 
"      (0.01)   ", "    1126.05    ", "      31       ", "     -67.34    ", 
"     150.69    ", "     162.41    ", "     126.07    ", "      25       ", 
"      32       "), .Dim = c(24L, 4L), .Dimnames = list(c("(Intercept)", 
" ", "cyl", "   ", "factor(am)1", "     ", "factor(cyl)6", "       ", 
"factor(cyl)8", "         ", "hp", "           ", "wt", "             ", 
"wt:hp", "               ", "Deviance", "Df", "Log-Likelihood", 
"AIC", "BIC", "Resid. deviance", "Resid. Df", "Num. obs."), c("1", 
"2", "3", "4")))
Community
  • 1
  • 1
jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • I haven't used `xtable` in a while, but the answers to [this question](https://stackoverflow.com/questions/38964540/how-to-decimal-align-regression-coefficients-in-latex-table-output-in-rmarkdown) that I asked a while back might be helpful. – eipi10 Sep 22 '19 at 20:52
  • @eipi10 Actually, I referred to this question in my question. It's a different attempt and when you look closer the GOFs aren't actually aligned. – jay.sf Sep 22 '19 at 20:56
  • Ah, I missed that! There are a number of more recent packages that try to make table creation easier with R. I haven't used [`huxtable`](https://hughjonesd.github.io/huxtable/huxreg.html), but [it seems](https://hughjonesd.github.io/huxtable/reference/align.html) to have the means to align on a decimal point. – eipi10 Sep 22 '19 at 20:57
  • @eipi10 I've tried `library(huxtable); set_align(huxtable(regmat), value=".")` but that doesn't seem to work (perhaps due to the `" --- "` cells). – jay.sf Sep 22 '19 at 21:11
  • @jay.sf your problem is that your table has lots of spaces in already. huxtable attempts to respect these when it does the alignment. If you do `regmat2 <- trimws(regmat); regmat2 <- as_huxtable(regmat2); align(regmat2) <- ".";` you will get a nicer result - still not perfect, as there is no easy way to align on decimal points in either HTML or TeX. One workaround is to set the font to `"cmtt"` or another fixed width font. – dash2 Sep 23 '19 at 11:21

0 Answers0