从边距创建 APA Table()
Create APA Table from margins()
我使用 R 中的边距库从线性模型计算 AME。通常我会使用 stargazer 库来创建我可以在学术论文中使用的 table。遗憾的是,这仅适用于回归对象。是否有一种有效的方法可以为从边距返回的结果生成类似的 table,例如图书馆?
感谢您的帮助!
这是一个例子:
library(stargazer)
library(margins)
x <- lm(mpg ~ cyl * hp + wt, data = mtcars)
stargazer(x, out = 'foo.html', type = 'html') # This produces the desired outcome for the linear model
m <- margins(x)
summary(m) # I like to create a similar table as above for these results
是的,stargazer
可以输出你的结果,如果你先保存它们,然后将它们传递给 stargazer。
默认情况下,如果我们传递一个普通的数据框,stargazer 将生成一个摘要table;这不是我们想要的。所以我们设置summary = FALSE
.
df<- summary(m) # I like to create a similar table as above for these results
stargazer(df, type = "text", summary = FALSE)
您可以将以上设置为 out = 'foo.html', type = 'html'
作为您的输出。以上returns:
==================================================
factor AME SE z p lower upper
--------------------------------------------------
1 cyl 0.038 0.600 0.064 0.949 -1.138 1.214
2 hp -0.046 0.015 -3.191 0.001 -0.075 -0.018
3 wt -3.120 0.661 -4.718 0.00000 -4.416 -1.824
--------------------------------------------------
使用 texreg
的一个相当老套的想法。使用基本模型 x
并使用 texreg::*reg
函数的 override .*
选项,放入 AME。当您第一次为基本模型创建表而不覆盖任何内容时,AME 表看起来很相似。
AME 应该通过前面的 NA
进行截距和交互的后续扩展(只是为了使 AME 与系数一致)。要摆脱 GOF,请使用 readLines
识别相应的行并省略它们。 cat
将代码保存到您的工作目录中的一个文件中。
sm <- rbind(NA, summary(m), NA)
library(texreg)
ame <- list(l=x, custom.model.names="AME", override.coef=sm[, 2], digits=3,
override.se=sm[, 3], override.pvalues=sm[, 5], omit.coef="\(|:",
caption="Average marginal effects")
## html version
ame.html <- do.call("htmlreg", ame)
tmp <- tempfile()
cat(ame.html, sep="\n", file=tmp)
ame.html <- readLines(tmp)
ame.html <- ame.html[-(el(grep("R<sup>2", ame.html)):grep("<tfoot>", ame.html))]
cat(ame.html, sep="\n", file="ame.html")
## latex version
ame.latex <- do.call("texreg", ame)
tmp <- tempfile()
cat(ame.latex, sep="\n", file=tmp)
ame.latex <- readLines(tmp)
ame.latex <- ame.latex[-(el(grep("R\$\^2\$", ame.latex)):grep("multicolumn", ame.latex))]
cat(ame.latex, sep="\n", file="ame.tex")
## console version
ame.screen <- do.call("screenreg", ame)
tmp <- tempfile()
cat(ame.screen, sep="\n", file=tmp)
ame.screen <- readLines(tmp)
ame.screen <- ame.screen[-(grep("---", ame.screen)[2]:(grep("\=", ame.screen)[2] - 1))]
cat(ame.screen, sep="\n")
注意:我试着让grep
通用,但您可能需要根据您的型号进行调整。
结果
(显示控制台)
=====================
AME
---------------------
cyl 0.038
(0.600)
hp -0.046 **
(0.015)
wt -3.120 ***
(0.661)
=====================
*** p < 0.001; ** p < 0.01; * p < 0.05
我使用 R 中的边距库从线性模型计算 AME。通常我会使用 stargazer 库来创建我可以在学术论文中使用的 table。遗憾的是,这仅适用于回归对象。是否有一种有效的方法可以为从边距返回的结果生成类似的 table,例如图书馆?
感谢您的帮助!
这是一个例子:
library(stargazer)
library(margins)
x <- lm(mpg ~ cyl * hp + wt, data = mtcars)
stargazer(x, out = 'foo.html', type = 'html') # This produces the desired outcome for the linear model
m <- margins(x)
summary(m) # I like to create a similar table as above for these results
是的,stargazer
可以输出你的结果,如果你先保存它们,然后将它们传递给 stargazer。
默认情况下,如果我们传递一个普通的数据框,stargazer 将生成一个摘要table;这不是我们想要的。所以我们设置summary = FALSE
.
df<- summary(m) # I like to create a similar table as above for these results
stargazer(df, type = "text", summary = FALSE)
您可以将以上设置为 out = 'foo.html', type = 'html'
作为您的输出。以上returns:
==================================================
factor AME SE z p lower upper
--------------------------------------------------
1 cyl 0.038 0.600 0.064 0.949 -1.138 1.214
2 hp -0.046 0.015 -3.191 0.001 -0.075 -0.018
3 wt -3.120 0.661 -4.718 0.00000 -4.416 -1.824
--------------------------------------------------
使用 texreg
的一个相当老套的想法。使用基本模型 x
并使用 texreg::*reg
函数的 override .*
选项,放入 AME。当您第一次为基本模型创建表而不覆盖任何内容时,AME 表看起来很相似。
AME 应该通过前面的 NA
进行截距和交互的后续扩展(只是为了使 AME 与系数一致)。要摆脱 GOF,请使用 readLines
识别相应的行并省略它们。 cat
将代码保存到您的工作目录中的一个文件中。
sm <- rbind(NA, summary(m), NA)
library(texreg)
ame <- list(l=x, custom.model.names="AME", override.coef=sm[, 2], digits=3,
override.se=sm[, 3], override.pvalues=sm[, 5], omit.coef="\(|:",
caption="Average marginal effects")
## html version
ame.html <- do.call("htmlreg", ame)
tmp <- tempfile()
cat(ame.html, sep="\n", file=tmp)
ame.html <- readLines(tmp)
ame.html <- ame.html[-(el(grep("R<sup>2", ame.html)):grep("<tfoot>", ame.html))]
cat(ame.html, sep="\n", file="ame.html")
## latex version
ame.latex <- do.call("texreg", ame)
tmp <- tempfile()
cat(ame.latex, sep="\n", file=tmp)
ame.latex <- readLines(tmp)
ame.latex <- ame.latex[-(el(grep("R\$\^2\$", ame.latex)):grep("multicolumn", ame.latex))]
cat(ame.latex, sep="\n", file="ame.tex")
## console version
ame.screen <- do.call("screenreg", ame)
tmp <- tempfile()
cat(ame.screen, sep="\n", file=tmp)
ame.screen <- readLines(tmp)
ame.screen <- ame.screen[-(grep("---", ame.screen)[2]:(grep("\=", ame.screen)[2] - 1))]
cat(ame.screen, sep="\n")
注意:我试着让grep
通用,但您可能需要根据您的型号进行调整。
结果
(显示控制台)
=====================
AME
---------------------
cyl 0.038
(0.600)
hp -0.046 **
(0.015)
wt -3.120 ***
(0.661)
=====================
*** p < 0.001; ** p < 0.01; * p < 0.05