在R的Stargazer包中,如何为生成的输出表添加一条分隔线?

huangapple go评论70阅读模式
英文:

In the Stargazer package in R, how can one add a line separator for the resulting output table?

问题

我在`R`的`stargazer`中有两个回归要报告,使用`add.lines()`在末尾添加了一个预测表格。我的表格目前如下:

[![在这里输入图片描述][1]][1]

但我想在“Predicted Values on x Values”下方添加一行,使其成为自己的行,就像我们在“Observations”行中所示。有办法做到这一点吗?

生成回归数据的代码:

```R
x <- 1:100
y <- rnorm(100, 4*x, 5)
mod1 <- lm(y ~ x)
mod2 <- lm(y ~ 1)
se1 <- summary(mod1)$coef[2,2]
se2 <- summary(mod2)$coef[1,2]
mod1.pred1 <- predict(mod1, newdata=data.frame(x=1))
mod2.pred1 <- predict(mod2, newdata=data.frame(x=1))
mod1.pred2 <- predict(mod1, newdata=data.frame(x=2))
mod2.pred2 <- predict(mod2, newdata=data.frame(x=2))

带有表格的Stargazer输出:

stargazer(mod1, mod2, se = list(se1, se2), out="Results.html", notes="Two Models", keep.stat="n", type = "text", 
      table.layout ="ldmc#-t-s-a=n",
      add.lines = list(
        c("Predicted Values on x Values"),
        c("", "", "", ""), # 添加一个空列表元素
        c("Predict when x=1",mod1.pred1,mod2.pred1),
        c("Predict when x=2",mod1.pred2,mod2.pred2)),
      add.lines.separator = c(1) # 在第四个元素后添加分隔符

<details>
<summary>英文:</summary>

I have two regressions to report in `stargazer` in `R`, with `add.lines()` adding a predicted table at the end. My table currently looks like:

[![enter image description here][1]][1]

but I want to add a line right below &quot;Predicted Values on x Values&quot;, so that it is its own row like we have in the &quot;Observations&quot; row. Is there a way to do this?



Code to generate regression data:

    x &lt;- 1:100
    y &lt;- rnorm(100, 4*x, 5)
    mod1 &lt;- lm(y ~ x)
    mod2 &lt;- lm(y ~ 1)
    se1 &lt;- summary(mod1)$coef[2,2]
    se2 &lt;- summary(mod2)$coef[1,2]
    mod1.pred1 &lt;- predict(mod1, newdata=data.frame(x=1))
    mod2.pred1 &lt;- predict(mod2, newdata=data.frame(x=1))
    mod1.pred2 &lt;- predict(mod1, newdata=data.frame(x=2))
    mod2.pred2 &lt;- predict(mod2, newdata=data.frame(x=2))

Stargazer output with table:

    stargazer(mod1, mod2, se = list(se1, se2), out=&quot;Results.html&quot;, notes=&quot;Two Models&quot;, keep.stat=&quot;n&quot;, type = &quot;text&quot;, 
          table.layout =&quot;ldmc#-t-s-a=n&quot;,
          add.lines = list(
            c(&quot;Predicted Values on x Values&quot;),
            c(&quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;), # add empty list element
            c(&quot;Predict when x=1&quot;,mod1.pred1,mod2.pred1),
            c(&quot;Predict when x=2&quot;,mod1.pred2,mod2.pred2)),
          add.lines.separator = c(1) # add separator after fourth element)


  [1]: https://i.stack.imgur.com/3Uv0Hm.png


</details>


# 答案1
**得分**: 6

**选项 1:**

编辑HTML输出文件

```R
star = readLines("Results.html")
newline.pos = grep('Predicted Values on x Values', star)
star = c(star[1:newline.pos], star[newline.pos -1], star[-(1:newline.pos)])
writeLines(star, "Results.html")

选项 2:

在保存为HTML文件之前编辑HTML。为此,将type = "text"更改为与输出文件格式匹配的type = "html"

star = stargazer(mod1, mod2, se = list(se1, se2), 
                 notes="Two Models", keep.stat="n", type = "html", 
            table.layout ="ldmc#-t-s-a=n",
            add.lines = list(
              c("Predicted Values on x Values"),
              c("", "", "", ""), # 添加空列表元素
              c("Predict when x=1",mod1.pred1,mod2.pred1),
              c("Predict when x=2",mod1.pred2,mod2.pred2)),
            add.lines.separator = c(1) # 在第四个元素后添加分隔符)
)

newline.pos = grep('Predicted Values on x Values', star)
star = c(star[1:newline.pos], star[newline.pos -1], star[-(1:newline.pos)])
writeLines(star, "Results.html")

选项 3:

将水平分隔线插入到add.lines列表中:

stargazer(mod1, mod2, se = list(se1, se2), 
                 out="Results.html", notes="Two Models", keep.stat="n", type = "html", 
            table.layout ="ldmc#-t-s-a=n",
            add.lines = list(
              c("Predicted Values on x Values"),
              c('<tr><td colspan="3" style="border-bottom: 1px solid black"></td></tr><tr>'),
              c("", "", "", ""), # 添加空列表元素
              c("Predict when x=1",mod1.pred1,mod2.pred1),
              c("Predict when x=2",mod1.pred2,mod2.pred2)),
            add.lines.separator = c(1) # 在第四个元素后添加分隔符)
)
英文:

Assuming I understood correctly that you want to add a horizontal rule below the words 'Predicted Values on x Values':

option 1:

Edit the html output file

star = readLines(&quot;Results.html&quot;)
newline.pos = grep(&#39;Predicted Values on x Values&#39;, star)
star = c(star[1:newline.pos], star[newline.pos -1], star[-(1:newline.pos)])
writeLines(star, &quot;Results.html&quot;)

在R的Stargazer包中,如何为生成的输出表添加一条分隔线?

option 2:

Edit the html before it is save to an html file. For this, instead of type = &quot;text&quot; you specify type = &quot;html&quot; to match your output file format.

star = stargazer(mod1, mod2, se = list(se1, se2), 
                 notes=&quot;Two Models&quot;, keep.stat=&quot;n&quot;, type = &quot;html&quot;, 
            table.layout =&quot;ldmc#-t-s-a=n&quot;,
            add.lines = list(
              c(&quot;Predicted Values on x Values&quot;),
              c(&quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;), # add empty list element
              c(&quot;Predict when x=1&quot;,mod1.pred1,mod2.pred1),
              c(&quot;Predict when x=2&quot;,mod1.pred2,mod2.pred2)),
            add.lines.separator = c(1) # add separator after fourth element)
)
            
newline.pos = grep(&#39;Predicted Values on x Values&#39;, star)
star = c(star[1:newline.pos], star[newline.pos -1], star[-(1:newline.pos)])
writeLines(star, &quot;Results.html&quot;)

option 3:

Insert the horizontal rule into your list of add.lines:

stargazer(mod1, mod2, se = list(se1, se2), 
                 out=&quot;Results.html&quot;, notes=&quot;Two Models&quot;, keep.stat=&quot;n&quot;, type = &quot;html&quot;, 
            table.layout =&quot;ldmc#-t-s-a=n&quot;,
            add.lines = list(
              c(&quot;Predicted Values on x Values&quot;),
              c(&#39;&lt;tr&gt;&lt;td colspan=&quot;3&quot; style=&quot;border-bottom: 1px solid black&quot;&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&#39;),
              c(&quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;), # add empty list element
              c(&quot;Predict when x=1&quot;,mod1.pred1,mod2.pred1),
              c(&quot;Predict when x=2&quot;,mod1.pred2,mod2.pred2)),
            add.lines.separator = c(1) # add separator after fourth element)
)

huangapple
  • 本文由 发表于 2023年2月19日 07:18:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定