0

I do multiple regression by group with a loop. I extract only 1 coefficient to which I attach stars according to this coef p-value. Her is an example of my code:

for(i in 1:length(list)) {
    # Equation
    coef <- summary(lm(formula = var1 ~ var2 + var3 + var4,
                       data = subset(data.df, origin==var2[i])
                    ),
            )
    # extraction
    est     <- coef$coefficients[2,1]
    p       <- coef$coefficients[2,4]
    # Define notions for significance levels; spacing is important.
    mystars <- ifelse(p < .001, "***", 
               ifelse(p < .01 , "** ", 
               ifelse(p < .05 , "*  ",
               "   ")))
    # past stars after estimate - put it in the matrix
    est.mat[1,i] <- paste(sprintf('%.2f',est), mystars, sep = "", collapse = NULL)
    # drop useless objects
    rm(coef, est, t, p)
}

This works perfectly. Once done, I transfer my matrix est.mat into latex as follow:

print(xtable(est.mat, align = c("l","r","r","r","r","r","r"),
                              label = paste("tab:", file.name, sep = "", collapse = NULL),
                              caption = file.caption),
             type = "latex",
             size="\\normalsize",
             caption.placement = "top",
             file = paste("graphs/", file.name, ".tex", sep = "", collapse = NULL) 
     )

It works perfectly as well. The only problem is that, once printed in PDF the empty space after the stars defined in "mystar" are considered "un-existant" and therefore the coefficient numbers are not aligned, as illustrated below.

enter image description here

My question is then: How can I protect this space in "mystar" ?

TeYaP
  • 303
  • 6
  • 21

1 Answers1

1

One way to do this is to use "phantom" stars, which take up the same space as a star, but display nothing. I believe this modification of your code will do it:

mystars <- ifelse(p < .001, "***", 
           ifelse(p < .01 , "**\\phantom{*}", 
           ifelse(p < .05 , "*\\phantom{**}",
           "\\phantom{***}")))

This will always leave space for three stars, so you might want to make it a little fancier by looking at the whole column first, and choosing how many phantom stars to add based on the most stars in the column. I'll leave that to you.

Edited to add: As described in Using xtable with R and Latex, math mode in column names?, print.xtable will escape the LaTeX macro, so it will display in the resulting PDF. But you can tell it not to, using argument sanitize.text.function:

print(xtable(est.mat, align = c("l","r","r","r","r","r","r"),
                              label = paste("tab:", file.name, sep = "", collapse = NULL),
                              caption = file.caption),
             type = "latex",
             size="\\normalsize",
             caption.placement = "top",
             file = paste("graphs/", file.name, ".tex", sep = "", collapse = NULL), 
             sanitize.text.function = function(x) x
     )

This assumes that all of the table entries are legal LaTeX. If they are not, you may need a more complicated sanitization.

user2554330
  • 37,248
  • 4
  • 43
  • 90
  • The idea is correct, it is exactly what I would like. Unfortunately that solution doest not work for me. The R console displays "8.33**\\phantom{*}" which gives "8.33**\phantom{*}" on the PDF. – TeYaP Oct 30 '18 at 18:32
  • See https://stackoverflow.com/questions/14877305/using-xtable-with-r-and-latex-math-mode-in-column-names. – user2554330 Oct 30 '18 at 18:44
  • Answer edited to do this. It would have been easier if you'd included a reproducible example, hint, hint. – user2554330 Oct 30 '18 at 19:35