英文:
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 "Predicted Values on x Values", so that it is its own row like we have in the "Observations" row. Is there a way to do this?
Code to generate regression data:
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 output with table:
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("", "", "", ""), # add empty list element
c("Predict when x=1",mod1.pred1,mod2.pred1),
c("Predict when x=2",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("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")
option 2:
Edit the html before it is save to an html file. For this, instead of type = "text"
you specify type = "html"
to match your output file format.
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("", "", "", ""), # add empty list element
c("Predict when x=1",mod1.pred1,mod2.pred1),
c("Predict when x=2",mod1.pred2,mod2.pred2)),
add.lines.separator = c(1) # add separator after fourth element)
)
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")
option 3:
Insert the horizontal rule into your list of 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("", "", "", ""), # add empty list element
c("Predict when x=1",mod1.pred1,mod2.pred1),
c("Predict when x=2",mod1.pred2,mod2.pred2)),
add.lines.separator = c(1) # add separator after fourth element)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论