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

问题描述 投票:0回答:1

我在

stargazer
中的
R
中报告了两个回归,
add.lines()
在最后添加了一个预测表。我的桌子目前看起来像:

但我想在“x 值的预测值”正下方添加一行,以便它是它自己的行,就像我们在“观察”行中一样。有没有办法做到这一点?

生成回归数据的代码:

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("", "", "", ""), # 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)
r stargazer
1个回答
0
投票

假设我理解正确,您想在“x 值的预测值”下方添加一条水平线,这里有一个选项:

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")

请注意,如果您指定

type = "text"
而不是
type = "html"
来匹配您的输出文件格式,您也可以避免从文件中读取 html。在那种情况下你可以简单地使用

star = 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("", "", "", ""), # 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")
© www.soinside.com 2019 - 2024. All rights reserved.