简体   繁体   English

在R的观星台中格式化笔记

[英]Formatting notes in R's stargazer tables

I am using stargazer package to produce (regression output) tables. 我正在使用stargazer软件包来生成(回归输出)表。 Everything working miracles until I start editing the notes. 在我开始编辑笔记之前,所有事情都是奇迹。 First : line breaks are hard, but Bryan suggests a manual solution that is not elegant, but works. 首先 :换行很难,但Bryan 提出了一种手动解决方案,该解决方案虽然不够优雅,但却可以使用。 Second I need to make it start from the very left of the table. 其次,我需要从表格的最左边开始。

Here is an example of the notes I am trying to produce from within R . 这是我尝试从R生成的音符的示例。 在此处输入图片说明

Produced by changing the original LaTeX code from: 通过将原始LaTeX代码从以下位置更改而产生:

\textit{Note:}  & \multicolumn{4}{l}{Logistic regression. Dependent variable: an indicator varible ... AND Some very long and interesting comment.} \\ 

To

   \multicolumn{5}{l} {\parbox[t]{11cm}{ \textit{Notes:} Logistic regression. Dependent variable: an indicator varible ... AND Some very long and interesting comment.}} \\

在此处输入图片说明

Editing by hand is time-consuming and error-prone. 手工编辑既费时又容易出错。 So I am looking for a way to solve this from R , where I am currently using the following: 所以我正在寻找一种方法来解决R ,我目前正在使用以下方法:

stargazer([...],
          style = "qje", notes.append = FALSE, notes.align = "l",
          notes = "\\parbox[t]{7cm}{Logistic regression. Dependent variable: an indicator 
                   varible ... AND Some very long and interesting comment.}")

stargazer returns its output invisibly as a character vector, specifically so you can post-process it. stargazer不可见地将其输出作为字符向量返回,特别是您可以对其进行后处理。 Depending what you want to change, this may involve some regex. 根据您要更改的内容,这可能涉及一些正则表达式。 Or, as in this case, if you know what your notes line should look like, you can simply replace the wrong line with what you want. 或者,在这种情况下,如果您知道便笺行应为什么样,则只需将错误的行替换为所需的行即可。 Here's a minimal reproducible example: 这是一个最小的可重现示例:

df <- data.frame(x = 1:10 + rnorm(100),
                 y = 1:10 + rnorm(100))
reg <- lm(y ~ x, data = df)

star <- stargazer(reg,
          style = "qje", notes.append = FALSE, notes.align = "l",
          notes = "This will be replaced")


note.latex <- "\\multicolumn{5}{l} {\\parbox[t]{11cm}{ \\textit{Notes:} Logistic regression. Dependent variable: an indicator varible ... AND Some very long and interesting comment.}} \\\\"
star[grepl("Note",star)] <- note.latex
cat (star, sep = "\n")

You can replace 7cm with \\textwidth, making your solution generic. 您可以将7cm替换为\\ textwidth,从而使解决方案具有通用性。

stargazer([...],
      style = "qje", notes.append = FALSE, notes.align = "l",
      notes = "\\parbox[t]{\\textwidth}{Logistic regression. Dependent variable: an indicator varible ... AND Some very long and interesting comment.}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM