在 Stargazer 中显示 Akaike 标准
Show Akaike Criteria in Stargazer
我有两个用 lm
创建的线性模型,我想将它们与 stargazer
包中的 table 进行比较。在大多数情况下,我喜欢我得到的结果。但是 Akaike 信息准则没有显示。 The docs 说我可以在 keep.stat
参数中传递 "aic"
以包含它。但它不在那里。没有错误消息。
stargazer(model1, model2, type="text", report="vc", header=FALSE,
title="Linear Models Predicting Forest Land",
keep.stat=c("aic", "rsq", "n"), omit.table.layout="n")
Linear Models Predicting Forest Land
==========================================
Dependent variable:
--------------------
forest
(1) (2)
------------------------------------------
log.MS.MIL.XPND.GD.ZS -11.948 -12.557
log.TX.VAL.AGRI.ZS.UN 2.310 2.299
log.NY.GDP.MKTP.CD 0.505
Constant 40.857 28.365
------------------------------------------
Observations 183 183
R2 0.142 0.146
==========================================
我看不出有任何理由不能包含它。在这些模型上调用全局 AIC
函数工作正常。
> AIC(model1)
[1] 1586.17
> AIC(model2)
[1] 1587.208
问题是由 stargazer:::.stargazer.wrap
中定义的 .AIC
函数给出的。
可以看出,此函数不计算 lm
模型的 AIC:
.AIC <- function(object.name) {
model.name <- .get.model.name(object.name)
if (model.name %in% c("coeftest")) {
return(NA)
}
if (model.name %in% c("lmer", "lme", "nlme", "glmer",
"nlmer", "ergm", "gls", "Gls", "lagsarlm", "errorsarlm",
"", "Arima")) {
return(as.vector(AIC(object.name)))
}
if (model.name %in% c("censReg")) {
return(as.vector(AIC(object.name)[1]))
}
if (model.name %in% c("fGARCH")) {
return(object.name@fit$ics["AIC"])
}
if (model.name %in% c("maBina")) {
return(as.vector(object.name$w$aic))
}
if (model.name %in% c("arima")) {
return(as.vector(object.name$aic))
}
else if (!is.null(.summary.object$aic)) {
return(as.vector(.summary.object$aic))
}
else if (!is.null(object.name$AIC)) {
return(as.vector(object.name$AIC))
}
return(NA)
}
.AIC
中的 .get.model.name
函数调用 .model.identify
。如果模型的组件 call
是 lm()
,那么 .model.identify
returns ls
:
if (object.name$call[1] == "lm()") {
return("ls")
}
解决方案一:使用add.lines
.
set.seed(12345)
n <- 100
df <- data.frame(y=rnorm(n), x1=rnorm(n), x2=rnorm(n))
model1 <- lm(y ~ x1, data=df)
model2 <- lm(y ~ x2, data=df)
library(stargazer)
stargazer(model1, model2, type="text", report="vc", header=FALSE,
title="Linear Models Predicting Forest Land",
keep.stat=c("rsq", "n"), omit.table.layout="n",
add.lines=list(c("AIC", round(AIC(model1),1), round(AIC(model2),1))))
输出为:
Linear Models Predicting Forest Land
=================================
Dependent variable:
--------------------
y
(1) (2)
---------------------------------
x1 0.115
x2 -0.052
Constant 0.240 0.243
---------------------------------
AIC 309.4 310.3
Observations 100 100
R2 0.011 0.002
=================================
解决方案 2:将组件 AIC
添加到模型对象中。
model1 <- lm(y ~ x1, data=df)
model2 <- lm(y ~ x2, data=df)
model1$AIC <- AIC(model1)
model2$AIC <- AIC(model2)
stargazer(model1, model2, type="text", report="vc", header=FALSE,
title="Linear Models Predicting Forest Land",
keep.stat=c("aic", "rsq", "n"), omit.table.layout="n")
输出为
Linear Models Predicting Forest Land
======================================
Dependent variable:
--------------------
y
(1) (2)
--------------------------------------
x1 0.115
x2 -0.052
Constant 0.240 0.243
--------------------------------------
Observations 100 100
R2 0.011 0.002
Akaike Inf. Crit. 309.413 310.318
======================================
我有两个用 lm
创建的线性模型,我想将它们与 stargazer
包中的 table 进行比较。在大多数情况下,我喜欢我得到的结果。但是 Akaike 信息准则没有显示。 The docs 说我可以在 keep.stat
参数中传递 "aic"
以包含它。但它不在那里。没有错误消息。
stargazer(model1, model2, type="text", report="vc", header=FALSE,
title="Linear Models Predicting Forest Land",
keep.stat=c("aic", "rsq", "n"), omit.table.layout="n")
Linear Models Predicting Forest Land
==========================================
Dependent variable:
--------------------
forest
(1) (2)
------------------------------------------
log.MS.MIL.XPND.GD.ZS -11.948 -12.557
log.TX.VAL.AGRI.ZS.UN 2.310 2.299
log.NY.GDP.MKTP.CD 0.505
Constant 40.857 28.365
------------------------------------------
Observations 183 183
R2 0.142 0.146
==========================================
我看不出有任何理由不能包含它。在这些模型上调用全局 AIC
函数工作正常。
> AIC(model1)
[1] 1586.17
> AIC(model2)
[1] 1587.208
问题是由 stargazer:::.stargazer.wrap
中定义的 .AIC
函数给出的。
可以看出,此函数不计算 lm
模型的 AIC:
.AIC <- function(object.name) {
model.name <- .get.model.name(object.name)
if (model.name %in% c("coeftest")) {
return(NA)
}
if (model.name %in% c("lmer", "lme", "nlme", "glmer",
"nlmer", "ergm", "gls", "Gls", "lagsarlm", "errorsarlm",
"", "Arima")) {
return(as.vector(AIC(object.name)))
}
if (model.name %in% c("censReg")) {
return(as.vector(AIC(object.name)[1]))
}
if (model.name %in% c("fGARCH")) {
return(object.name@fit$ics["AIC"])
}
if (model.name %in% c("maBina")) {
return(as.vector(object.name$w$aic))
}
if (model.name %in% c("arima")) {
return(as.vector(object.name$aic))
}
else if (!is.null(.summary.object$aic)) {
return(as.vector(.summary.object$aic))
}
else if (!is.null(object.name$AIC)) {
return(as.vector(object.name$AIC))
}
return(NA)
}
.AIC
中的 .get.model.name
函数调用 .model.identify
。如果模型的组件 call
是 lm()
,那么 .model.identify
returns ls
:
if (object.name$call[1] == "lm()") {
return("ls")
}
解决方案一:使用add.lines
.
set.seed(12345)
n <- 100
df <- data.frame(y=rnorm(n), x1=rnorm(n), x2=rnorm(n))
model1 <- lm(y ~ x1, data=df)
model2 <- lm(y ~ x2, data=df)
library(stargazer)
stargazer(model1, model2, type="text", report="vc", header=FALSE,
title="Linear Models Predicting Forest Land",
keep.stat=c("rsq", "n"), omit.table.layout="n",
add.lines=list(c("AIC", round(AIC(model1),1), round(AIC(model2),1))))
输出为:
Linear Models Predicting Forest Land
=================================
Dependent variable:
--------------------
y
(1) (2)
---------------------------------
x1 0.115
x2 -0.052
Constant 0.240 0.243
---------------------------------
AIC 309.4 310.3
Observations 100 100
R2 0.011 0.002
=================================
解决方案 2:将组件 AIC
添加到模型对象中。
model1 <- lm(y ~ x1, data=df)
model2 <- lm(y ~ x2, data=df)
model1$AIC <- AIC(model1)
model2$AIC <- AIC(model2)
stargazer(model1, model2, type="text", report="vc", header=FALSE,
title="Linear Models Predicting Forest Land",
keep.stat=c("aic", "rsq", "n"), omit.table.layout="n")
输出为
Linear Models Predicting Forest Land
======================================
Dependent variable:
--------------------
y
(1) (2)
--------------------------------------
x1 0.115
x2 -0.052
Constant 0.240 0.243
--------------------------------------
Observations 100 100
R2 0.011 0.002
Akaike Inf. Crit. 309.413 310.318
======================================