2

I am using stargazer to extract some regression tables in latex. I would to know if it exists a way to label variables once for all without having to re-define it through "covariate.labels = ..." every time. I tried the library expss (and Hmisc), such as:

library(expss)
library(stargazer)

df <- data.frame(replicate(2,sample(0:1,100,rep=TRUE)))

df = apply_labels(df,
                  X1 =  "label x1",
                  X2 = " label x2")

ols <- lm(formula = X1 ~ X2,
          data= df)

stargazer(ols, 
          # covariate.labels = NULL, 
          title = "Regression Results",
          label = "tab:test",
          out="test.tex")

without success... any suggestion?

Gregory Demin
  • 4,596
  • 2
  • 20
  • 20
TeYaP
  • 303
  • 6
  • 21
  • Could you provide example of your code? Generally speaking, you can try `use_labels` from expss `package`. In many cases it can utilize labels for functions which doesn't apply it. – Gregory Demin Oct 26 '18 at 12:08
  • executing the code above I obtain X2 as covariate var name while I would like to have "label x2"! – TeYaP Oct 26 '18 at 13:02

1 Answers1

1

Solution with expss use_labels:

library(expss)
library(stargazer)

df <- data.frame(replicate(2,sample(0:1,100,rep=TRUE)))

df = apply_labels(df,
                  X1 =  "label x1",
                  X2 = " label x2")

ols <- use_labels(df, lm(formula = X1 ~ X2))

res <- stargazer(ols, 
          # covariate.labels = NULL, 
          title = "Regression Results",
          label = "tab:test",
          out="test.tex")

# quick and dirty workaround for removing backticks  
remove_backticks = function(text){
    text = gsub("([^A-z]+)`", "\\1", text, perl = TRUE)
    text = gsub("`([^A-z]+)", "\\1", text, perl = TRUE)
    text = gsub("(^`)|(`$)", "", text, perl = TRUE)
    text
}


res = remove_backticks(res)

writeLines(res, "test.tex")
Gregory Demin
  • 4,596
  • 2
  • 20
  • 20
  • doen't works for me! it gives me the following message: "Error: `x` and `labels` must be same type" – TeYaP Oct 26 '18 at 13:55
  • @TeYaP It is conflict with some other package - I suppose with `haven` or `labelled`. Try to restart you R session and `expss` should be loaded after the all other packages. – Gregory Demin Oct 26 '18 at 14:00
  • Thanks, I had indeed a conflict issue. Your solution is now partially working. The label included in the latex extraction is : ‘ label x2‘ . Does it exists a way to delete the quote marks? And, if I may ask another question: do you know another way, more aesthetic, to produce similar results, maybe using other libraries? – TeYaP Oct 26 '18 at 14:31
  • @TeYaP I've added function for removing backticks. It's rather quick and dirty but should work. You also take a look at `arsenal` package which provides some facilities for regression output with labels. – Gregory Demin Oct 26 '18 at 15:53
  • Thanks for everything ! – TeYaP Oct 26 '18 at 17:34
  • @GregoryDemin This works so well, thank you. Is there a way to remove the backticks if the output of stargazer we are looking for is directly to html? eg `stargazer(ols, title = "Regression Results", label = "tab:test", type = "html", out = "table1.html") %>% remove_backticks()` – Jeremy K. May 31 '20 at 07:35
  • 1
    @JeremyK. `remove_backticks` also works for HTML. You need to save result: `writeLines(res, "test.html")` – Gregory Demin May 31 '20 at 22:23
  • This works well for numerical variables, but how can we make it work for categorical variables? – bixiou Feb 11 '22 at 15:34
  • @bixiou You need to convert categorical variables to factors with `factor` or `as.factor` before linear regression. – Gregory Demin Feb 13 '22 at 16:24