微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

R 中的多面板回归表

如何解决R 中的多面板回归表

我正在尝试创建一个回归表,该表具有两个具有重叠但不相同的因变量的面板。对于每个面板,我想要一个标题,指示每个面板的名称以及每个回归的因变量。

# rm(list = ls())

data("mtcars")

# Regressions for panel 1
reg1 <- lm(mpg ~ cyl + disp,data = mtcars)
reg2 <- lm(cyl ~ cyl + disp,data = mtcars)

# Regressions for panel 2
reg3 <- lm(mpg ~ hp + drat,data = mtcars)
reg4 <- lm(cyl ~ hp + drat,data = mtcars)
reg5 <- lm(disp ~ hp + qsec,data = mtcars)

# Panel 1 output
panel1 <- stargazer::stargazer(
  reg1,reg2,float = TRUE,header = FALSE,model.numbers = FALSE,omit.table.layout = "n",multicolumn = FALSE,dep.var.caption = "",type = "latex",align = TRUE,digits = 2,df = FALSE,digits.extra = 2,nobs = TRUE,omit.stat = c("rsq","adj.rsq","ser") 
)

# Panel 2 output
panel2 <- 
  stargazer::stargazer(
  reg3,reg4,reg5,"ser")
)

# Plot panels together
table <- 
  starpolishr::star_panel(
  panel1,panel2,panel.names = c(
    "Panel 1","Panel 2"
  ),same.summary.stats = FALSE,same.lhs.vars = FALSE )

# Save as a .tex file
starpolishr::star_tex_write(
  starlist = table,file = paste0(here::here(),"/panel_mwes.tex"),headers = FALSE
)

代码产生以下输出

我需要改变的是1)将“面板A:面板1”移到因变量名称上方;和 2) 在“面板 B:面板 2”下方添加因变量名称。有没有什么好的方法来实现这个(在 stargazer/starpolishr 或其他方式)而不是超级黑客?

解决方法

所以这是我的 hacky 做事方式(我不会接受这个作为答案,因为必须有更好的方法来做到这一点)。

# 1. Flip the panel name and column names around
col_names  <- table[8]
panel_name <- table[10]

table[8]  <- panel_name
table[10] <- col_names

# 2. Insert custom column names below panel 2 header
table <- 
  starpolishr::star_insert_row(
    table,c("\\hline \\\\[-1.8ex] & \\multicolumn{1}{c}{mpg} & \\multicolumn{1}{c}{cyl} & \\multicolumn{1}{c}{disp} \\\\ \\hline \\\\[-1.8ex]"),insert.after = c(24)
  )

# Save output
starpolishr::star_tex_write(
  starlist = table,file = paste0(here::here(),"/panel_mwe2.tex"),headers = FALSE
)

以下输出大致是我想要实现的:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。