带有 pglm 模型的 R Stargazer - 在 plm 模型中转换二项式 pglm 模型
R Stargazer with pglm model - convert binominal pglm model in plm model
我正在使用 stargazer
创建我的 plm 汇总表。
library(plm)
library(pglm)
data("Unions", package = "pglm")
anb1 <- plm(wage ~ union + exper + rural, Unions, model = "random", method = "bfgs")
stargazer(anb1)
很遗憾,stargazer 不支持 pglm 模型。我正在寻找有关如何绘制具有二进制因变量的 pglm 模型结果的解决方案,因为以下 stargazer 调用不适用于 pglm 模型。
anb2 <- pglm(union ~ wage + exper + rural, Unions, family = "binomial",
model = "random", method = "bfgs")
stargazer(anb2)
除了提取每个摘要项然后分别格式化之外,还有什么替代方法吗?
结果的class是:
[1] "maxLik" "maxim" "list"
一种可能的解决方案是执行以下操作。
anb2 <- pglm(union ~ wage + exper + rural, Unions, family = "binomial",
model = "random", method = "bfgs")
model = summary(anb2)
加载或安装以下库
library(dplyr)
library(xtable)
library('gtools')
使用协变量的名称创建一个向量
var = c('Intercept', 'wage', 'exper', 'ruralyes', 'sigma')
然后
model_summary = model$estimate %>% as.data.frame() %>%
mutate(term = var, Estimate = round(Estimate, 2), SE = round(`Std. error`, 2), p.value = stars.pval(`Pr(> t)`)) %>%
select(term, Estimate, SE, p.value)
> model_summary
term Estimate SE p.value
1 Intercept -2.86 0.23 ***
2 wage 0.12 0.02 ***
3 exper -0.06 0.02 *
4 ruralyes 0.09 0.25
5 sigma 4.30 0.23 ***
然后你可以在 data.frame
上使用 xtable
library(xtable)
xtable(model_summary)
另一个可能的(但不完全令人满意的)解决方案。
library(plm)
library(pglm)
library(stargazer)
data("Unions", package = "pglm")
anb2 <- pglm(union ~ wage + exper + rural, Unions, family = "binomial",
model = "random", method = "bfgs")
# A "fake" model
anb0 <- plm(union ~ wage + exper + rural, Unions, family = "binomial",
model = "random", method = "bfgs")
tstats <- summary(anb2)$estimate[,3][-5]
pvs <- summary(anb2)$estimate[,4][-5]
SEs <- summary(anb2)$estimate[,2][-5]
coefs <- summary(anb2)$estimate[,1][-5]
stargazer(anb0, type="text", coef=list(coefs), se=list(SEs),
p = list(pvs), omit.stat="all")
这是stargazer
生成的table:
====================================
Dependent variable:
---------------------------
union
------------------------------------
wage 0.122***
(0.024)
exper -0.058**
(0.023)
ruralyes 0.092
(0.249)
Constant -2.857***
(0.235)
====================================
====================================
Note: *p<0.1; **p<0.05; ***p<0.01
这里有一个简单的提取函数,可以使 texreg 与 pglm 一起工作:
extract.pglm <- function (model, include.nobs = TRUE, include.loglik = TRUE, ...) {
s <- summary(model, ...)
coefficient.names <- rownames(s$estimate)
coefficients <- s$estimate[, 1]
standard.errors <- s$estimate[, 2]
significance <- s$estimate[, 4]
loglik.value <- s$loglik
n <- nrow(model$model)
gof <- numeric()
gof.names <- character()
gof.decimal <- logical()
if (include.loglik == TRUE) {
gof <- c(gof, loglik.value)
gof.names <- c(gof.names, "Log-Likelihood")
gof.decimal <- c(gof.decimal, TRUE)
}
if (include.nobs == TRUE) {
gof <- c(gof, n)
gof.names <- c(gof.names, "Num. obs.")
gof.decimal <- c(gof.decimal, FALSE)
}
tr <- createTexreg(coef.names = coefficient.names, coef = coefficients,
se = standard.errors, pvalues = significance, gof.names = gof.names,
gof = gof, gof.decimal = gof.decimal)
return(tr)
}
为了使此代码工作,您还应该注册该函数,以便它在调用 extract
时默认处理 pglm maxLik
对象:
setMethod("extract", signature = className("maxLik", "maxLik"),
definition = extract.pglm)
之后,您可以将 texreg 与 pglm 一起使用,就像与 plm 或 texreg 支持的其他模型一样。
texreg
(1.36.24) 的新版本在 GitHub 上可用(很快在 CRAN 上)并且 pglm class 已添加。
这是一个更简单的解决方案。截至 2019 年 6 月 25 日,Stargazer 仍然不支持 pglm,但 coeftest 这样做只是通过 coeftest 将模型传递给 stargazer。
(另请注意自@giamcomo 以来 pglm 中数据对象名称的更改)
library(plm)
library(pglm)
library(lmtest)
library(stargazer)
data("UnionWage", package = "pglm")
anb2 <- pglm(union ~ wage + exper + rural, UnionWage, family = "binomial",
model = "random", method = "bfgs")
stargazer(anb2)
summary(anb2)
stargazer(coeftest(anb2), type="text")
这是输出
> stargazer(anb2)
% Error: Unrecognized object type.
>
> summary(anb2)
--------------------------------------------
Maximum Likelihood estimation
BFGS maximization, 35 iterations
Return code 0: successful convergence
Log-Likelihood: -1655.034
5 free parameters
Estimates:
Estimate Std. error t value Pr(> t)
(Intercept) -3.43651 0.29175 -11.779 < 2e-16 ***
wage 0.82896 0.15014 5.521 3.37e-08 ***
exper -0.06590 0.02318 -2.843 0.00447 **
ruralyes 0.07558 0.24866 0.304 0.76116
sigma 4.26050 0.22606 18.847 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
--------------------------------------------
>
> stargazer(coeftest(anb2), type="text")
====================================
Dependent variable:
---------------------------
------------------------------------
wage 0.83***
(0.15)
exper -0.07***
(0.02)
ruralyes 0.08
(0.25)
sigma 4.26***
(0.23)
Constant -3.44***
(0.29)
====================================
====================================
Note: *p<0.1; **p<0.05; ***p<0.01
>
我正在使用 stargazer
创建我的 plm 汇总表。
library(plm)
library(pglm)
data("Unions", package = "pglm")
anb1 <- plm(wage ~ union + exper + rural, Unions, model = "random", method = "bfgs")
stargazer(anb1)
很遗憾,stargazer 不支持 pglm 模型。我正在寻找有关如何绘制具有二进制因变量的 pglm 模型结果的解决方案,因为以下 stargazer 调用不适用于 pglm 模型。
anb2 <- pglm(union ~ wage + exper + rural, Unions, family = "binomial",
model = "random", method = "bfgs")
stargazer(anb2)
除了提取每个摘要项然后分别格式化之外,还有什么替代方法吗? 结果的class是:
[1] "maxLik" "maxim" "list"
一种可能的解决方案是执行以下操作。
anb2 <- pglm(union ~ wage + exper + rural, Unions, family = "binomial",
model = "random", method = "bfgs")
model = summary(anb2)
加载或安装以下库
library(dplyr)
library(xtable)
library('gtools')
使用协变量的名称创建一个向量
var = c('Intercept', 'wage', 'exper', 'ruralyes', 'sigma')
然后
model_summary = model$estimate %>% as.data.frame() %>%
mutate(term = var, Estimate = round(Estimate, 2), SE = round(`Std. error`, 2), p.value = stars.pval(`Pr(> t)`)) %>%
select(term, Estimate, SE, p.value)
> model_summary
term Estimate SE p.value
1 Intercept -2.86 0.23 ***
2 wage 0.12 0.02 ***
3 exper -0.06 0.02 *
4 ruralyes 0.09 0.25
5 sigma 4.30 0.23 ***
然后你可以在 data.frame
上使用xtable
library(xtable)
xtable(model_summary)
另一个可能的(但不完全令人满意的)解决方案。
library(plm)
library(pglm)
library(stargazer)
data("Unions", package = "pglm")
anb2 <- pglm(union ~ wage + exper + rural, Unions, family = "binomial",
model = "random", method = "bfgs")
# A "fake" model
anb0 <- plm(union ~ wage + exper + rural, Unions, family = "binomial",
model = "random", method = "bfgs")
tstats <- summary(anb2)$estimate[,3][-5]
pvs <- summary(anb2)$estimate[,4][-5]
SEs <- summary(anb2)$estimate[,2][-5]
coefs <- summary(anb2)$estimate[,1][-5]
stargazer(anb0, type="text", coef=list(coefs), se=list(SEs),
p = list(pvs), omit.stat="all")
这是stargazer
生成的table:
====================================
Dependent variable:
---------------------------
union
------------------------------------
wage 0.122***
(0.024)
exper -0.058**
(0.023)
ruralyes 0.092
(0.249)
Constant -2.857***
(0.235)
====================================
====================================
Note: *p<0.1; **p<0.05; ***p<0.01
这里有一个简单的提取函数,可以使 texreg 与 pglm 一起工作:
extract.pglm <- function (model, include.nobs = TRUE, include.loglik = TRUE, ...) {
s <- summary(model, ...)
coefficient.names <- rownames(s$estimate)
coefficients <- s$estimate[, 1]
standard.errors <- s$estimate[, 2]
significance <- s$estimate[, 4]
loglik.value <- s$loglik
n <- nrow(model$model)
gof <- numeric()
gof.names <- character()
gof.decimal <- logical()
if (include.loglik == TRUE) {
gof <- c(gof, loglik.value)
gof.names <- c(gof.names, "Log-Likelihood")
gof.decimal <- c(gof.decimal, TRUE)
}
if (include.nobs == TRUE) {
gof <- c(gof, n)
gof.names <- c(gof.names, "Num. obs.")
gof.decimal <- c(gof.decimal, FALSE)
}
tr <- createTexreg(coef.names = coefficient.names, coef = coefficients,
se = standard.errors, pvalues = significance, gof.names = gof.names,
gof = gof, gof.decimal = gof.decimal)
return(tr)
}
为了使此代码工作,您还应该注册该函数,以便它在调用 extract
时默认处理 pglm maxLik
对象:
setMethod("extract", signature = className("maxLik", "maxLik"),
definition = extract.pglm)
之后,您可以将 texreg 与 pglm 一起使用,就像与 plm 或 texreg 支持的其他模型一样。
texreg
(1.36.24) 的新版本在 GitHub 上可用(很快在 CRAN 上)并且 pglm class 已添加。
这是一个更简单的解决方案。截至 2019 年 6 月 25 日,Stargazer 仍然不支持 pglm,但 coeftest 这样做只是通过 coeftest 将模型传递给 stargazer。
(另请注意自@giamcomo 以来 pglm 中数据对象名称的更改)
library(plm)
library(pglm)
library(lmtest)
library(stargazer)
data("UnionWage", package = "pglm")
anb2 <- pglm(union ~ wage + exper + rural, UnionWage, family = "binomial",
model = "random", method = "bfgs")
stargazer(anb2)
summary(anb2)
stargazer(coeftest(anb2), type="text")
这是输出
> stargazer(anb2)
% Error: Unrecognized object type.
>
> summary(anb2)
--------------------------------------------
Maximum Likelihood estimation
BFGS maximization, 35 iterations
Return code 0: successful convergence
Log-Likelihood: -1655.034
5 free parameters
Estimates:
Estimate Std. error t value Pr(> t)
(Intercept) -3.43651 0.29175 -11.779 < 2e-16 ***
wage 0.82896 0.15014 5.521 3.37e-08 ***
exper -0.06590 0.02318 -2.843 0.00447 **
ruralyes 0.07558 0.24866 0.304 0.76116
sigma 4.26050 0.22606 18.847 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
--------------------------------------------
>
> stargazer(coeftest(anb2), type="text")
====================================
Dependent variable:
---------------------------
------------------------------------
wage 0.83***
(0.15)
exper -0.07***
(0.02)
ruralyes 0.08
(0.25)
sigma 4.26***
(0.23)
Constant -3.44***
(0.29)
====================================
====================================
Note: *p<0.1; **p<0.05; ***p<0.01
>