如何在 R 中以 LaTex 格式创建 AIC 模型选择 Table?
How to Create AIC Model Selection Table in R in LaTex format?
想要为 LaTex 格式的出版物创建 AIC 选择 table,但我似乎无法获得我想要的形式。我已经用谷歌搜索死了,很惊讶我找不到答案。我在 R 中找到了更多晦涩问题的答案。
下面是一些代码,一些 tables 我做的我并不过分热衷,底部是我想做的一般结构,但在 nice latex table 格式与 stargazer 包相同。
我尝试为两个包使用额外的参数来实现我想要的但没有成功。
##Create dummy variables
a<-1:10
b<-c(10:3,1,2)
c<-c(1,4,5,3,7,3,6,2,4,5)
##Create df
df<-data.frame(a,b,c)
##Build models
m1<-lm(a~b,data=df)
summary(m1)
m2<-lm(a~c,data=df)
m3<-lm(a~b+c,data=df)
m4<-lm(a~b*c,data=df)
##View list of AIC values
AIC(m1,m2,m3,m4)
########################CREATE AIC SELECTION TABLE
##Using MuMIn Package
library(MuMIn)
modelTABLE <- model.sel(m1,m2,m3,m4)
View(modelTABLE) ##No AIC values, just AICc, no R-squared, and model name (i.e, a~b) not present
##Using stargazer Package
library(stargazer)
test<-stargazer(m1,m2,m3,m4 ,
type = "text",
title="Regression Results",
align=TRUE,
style="default",
dep.var.labels.include=TRUE,
flip=FALSE
## ,out="models.htm"
)
View(test) ##More of a table depicting individual covariate attributes, bottom of table doesn't have AIC
###Would like a table similar to the following
Model ModelName df logLik AIC delta AICweight R2
m1 a ~ b 3 -6.111801 18.2 0 0.95 0.976
m3 a ~ b + c 4 -5.993613 20 1.8 0.05 0.976
m4 a ~ b * c 5 -5.784843 21.6 3.4 0.00 0.977
m2 a ~ c 3 -24.386821 54.8 36.6 0.00 0.068
`
model.sel
结果是 data.frame
,因此您可以修改它(添加模型名称、整数等)并使用例如导出到乳胶latex
来自 Hmisc
包。
# include R^2:
R2 <- function(x) summary(x)$r.squared
ms <- model.sel(m1, m2, m3, m4, extra = "R2")
i <- 1:4 # indices of columns with model terms
response <- "a"
res <- as.data.frame(ms)
v <- names(ms)[i]
v[v == "(Intercept)"] <- 1
# create formula-like model names:
mnames <- apply(res[, i], 1, function(x)
deparse(simplify.formula(reformulate(v[!is.na(x)], response = response))))
## OR
# mnames <- apply(res[, i], 1, function(x)
# sapply(attr(ms, "modelList"), function(x) deparse(formula(x)))
res <- cbind(model = mnames, res[, -i])
Hmisc::latex(res, file = "")
想要为 LaTex 格式的出版物创建 AIC 选择 table,但我似乎无法获得我想要的形式。我已经用谷歌搜索死了,很惊讶我找不到答案。我在 R 中找到了更多晦涩问题的答案。
下面是一些代码,一些 tables 我做的我并不过分热衷,底部是我想做的一般结构,但在 nice latex table 格式与 stargazer 包相同。
我尝试为两个包使用额外的参数来实现我想要的但没有成功。
##Create dummy variables
a<-1:10
b<-c(10:3,1,2)
c<-c(1,4,5,3,7,3,6,2,4,5)
##Create df
df<-data.frame(a,b,c)
##Build models
m1<-lm(a~b,data=df)
summary(m1)
m2<-lm(a~c,data=df)
m3<-lm(a~b+c,data=df)
m4<-lm(a~b*c,data=df)
##View list of AIC values
AIC(m1,m2,m3,m4)
########################CREATE AIC SELECTION TABLE
##Using MuMIn Package
library(MuMIn)
modelTABLE <- model.sel(m1,m2,m3,m4)
View(modelTABLE) ##No AIC values, just AICc, no R-squared, and model name (i.e, a~b) not present
##Using stargazer Package
library(stargazer)
test<-stargazer(m1,m2,m3,m4 ,
type = "text",
title="Regression Results",
align=TRUE,
style="default",
dep.var.labels.include=TRUE,
flip=FALSE
## ,out="models.htm"
)
View(test) ##More of a table depicting individual covariate attributes, bottom of table doesn't have AIC
###Would like a table similar to the following
Model ModelName df logLik AIC delta AICweight R2
m1 a ~ b 3 -6.111801 18.2 0 0.95 0.976
m3 a ~ b + c 4 -5.993613 20 1.8 0.05 0.976
m4 a ~ b * c 5 -5.784843 21.6 3.4 0.00 0.977
m2 a ~ c 3 -24.386821 54.8 36.6 0.00 0.068
`
model.sel
结果是 data.frame
,因此您可以修改它(添加模型名称、整数等)并使用例如导出到乳胶latex
来自 Hmisc
包。
# include R^2:
R2 <- function(x) summary(x)$r.squared
ms <- model.sel(m1, m2, m3, m4, extra = "R2")
i <- 1:4 # indices of columns with model terms
response <- "a"
res <- as.data.frame(ms)
v <- names(ms)[i]
v[v == "(Intercept)"] <- 1
# create formula-like model names:
mnames <- apply(res[, i], 1, function(x)
deparse(simplify.formula(reformulate(v[!is.na(x)], response = response))))
## OR
# mnames <- apply(res[, i], 1, function(x)
# sapply(attr(ms, "modelList"), function(x) deparse(formula(x)))
res <- cbind(model = mnames, res[, -i])
Hmisc::latex(res, file = "")