table 中的堆叠回归?
Stacking regressions in a table?
是否有任何 R 包可以 (a) 让您将模型放在相邻的列中,以及 (b) 在不同的行中堆叠回归?我看到了一种可能的解决方案 (R stargazer stack several regression models in rows under each other),但这并不能完全解决问题,因为它包含所有模型系数并添加了其他垃圾。
下面是我尝试创建的示例。行是因变量,列是模型规格:
我可以通过为单个因变量估计第 1、2、3 列中的模型来创建我想要的一行:
data(mtcars)
attach(mtcars)
library(stargazer)
m1 <- lm(mpg ~ cyl, mtcars)
m2 <- lm(mpg ~ cyl + disp, mtcars)
m3 <- lm(mpg ~ cyl + disp + hp, mtcars)
stargazer(m1,m2,m3, keep = c('cyl'), type = 'text')
这个怎么样。我写了一个小函数来获取模型列表并生成所需的输出。
myfun <- function(..., param_num = 2, cn = NULL, rn=NULL, output=c("text", "md", "latex")){
### ... is a list where each element is itself a list of models that
### will be used in in the corresponding column
### param_num is a vector of number isdentify the index of the
### coefficient to be used for that column
### cn is a vector of column names for the table.
### rn is a vector of names of the rows of the table
### output a string indicating the kind of output you want. If "md" (markdown), then
### the knitr package will be loaded. If "latex", then the xtable package will be loaded.
### This function requires the psre package
require(psre)
output = match.arg(output)
args <- list(...)
if(length(param_num) == 1)param_num <- rep(param_num, length(args))
b <- lapply(1:length(args), function(x)sapply(args[[x]], function(y)coef(y)[param_num[x]]))
se <- lapply(1:length(args), function(x)sapply(args[[x]], function(y)sqrt(diag(vcov(y)))[param_num[x]]))
res_df <- lapply(1:length(args), function(x)sapply(args[[x]], function(y)y$df.residual))
t <- lapply(1:length(b), function(i)b[[i]]/se[[i]])
p <- lapply(1:length(b), function(i)2*pt(abs(t[[i]]), df=res_df[[i]], lower.tail=FALSE))
cols <- sapply(1:length(b), function(i)shuffle(b[[i]], p[[i]], se[[i]]))
cols <- cbind("", cols)
if(is.null(cn)){
colnames(cols) <- c("DV", paste0("Spec ", 1:length(b)))
}else{
colnames(cols) <- cn
}
if(is.null(rn)){
cols[seq(1, nrow(cols), by=2), 1] <- paste0("Var ", seq_along(seq(1, nrow(cols), by=2)))
}else{
cols[seq(1, nrow(cols), by=2), 1] <- rn
}
if(output == "text"){
print(noquote(cols))
}
if(output == "md"){
require(knitr)
print(knitr::kable(cols))
}
if(output == "latex"){
require(xtable)
print(xtable(cols), include.rownames=FALSE)
}
}
library(mtcars)
#> Error in library(mtcars): there is no package called 'mtcars'
col1_mods <- list(lm(mpg ~ vs + cyl, data=mtcars),
lm(disp ~ vs + cyl, data=mtcars),
lm(hp ~ vs + cyl, data=mtcars),
lm(qsec ~ vs + cyl, data=mtcars))
col2_mods <- list(lm(mpg ~ vs , data=mtcars),
lm(disp ~ vs , data=mtcars),
lm(hp ~ vs , data=mtcars),
lm(qsec ~ vs , data=mtcars))
col3_mods <- list(lm(mpg ~ vs + cyl + am, data=mtcars),
lm(disp ~ vs + cyl + am, data=mtcars),
lm(hp ~ vs + cyl + am, data=mtcars),
lm(qsec ~ vs + cyl + am, data=mtcars))
myfun(col1_mods, col2_mods, col3_mods,
rn = c("MPG", "Displacement", "Horsepower", "Time to 60"),
output = "text")
#> Loading required package: psre
#> DV Spec 1 Spec 2 Spec 3
#> [1,] MPG -0.939 7.940* 1.295
#> [2,] (1.978) (1.632) (2.193)
#> [3,] Displacement 15.047 -174.693* -20.554
#> [4,] (33.561) (31.597) (37.519)
#> [5,] Horsepower -19.115 -98.365* 10.364
#> [6,] (23.649) (17.155) (25.813)
#> [7,] Time to 60 2.744* 2.640* 1.063
#> [8,] (0.751) (0.432) (0.637)
myfun(col1_mods, col2_mods, col3_mods,
rn = c("MPG", "Displacement", "Horsepower", "Time to 60"),
output = "md")
#> Loading required package: knitr
#>
#>
#> |DV |Spec 1 |Spec 2 |Spec 3 |
#> |:------------|:--------|:---------|:--------|
#> |MPG |-0.939 |7.940* |1.295 |
#> | |(1.978) |(1.632) |(2.193) |
#> |Displacement |15.047 |-174.693* |-20.554 |
#> | |(33.561) |(31.597) |(37.519) |
#> |Horsepower |-19.115 |-98.365* |10.364 |
#> | |(23.649) |(17.155) |(25.813) |
#> |Time to 60 |2.744* |2.640* |1.063 |
#> | |(0.751) |(0.432) |(0.637) |
myfun(col1_mods, col2_mods, col3_mods,
rn = c("MPG", "Displacement", "Horsepower", "Time to 60"),
output = "latex")
#> Loading required package: xtable
#>
#> Attaching package: 'xtable'
#> The following object is masked from 'package:psre':
#>
#> caption
#> % latex table generated in R 4.1.2 by xtable 1.8-4 package
#> % Fri Feb 4 09:46:07 2022
#> \begin{table}[ht]
#> \centering
#> \begin{tabular}{llll}
#> \hline
#> DV & Spec 1 & Spec 2 & Spec 3 \
#> \hline
#> MPG & -0.939 & 7.940* & 1.295 \
#> & (1.978) & (1.632) & (2.193) \
#> Displacement & 15.047 & -174.693* & -20.554 \
#> & (33.561) & (31.597) & (37.519) \
#> Horsepower & -19.115 & -98.365* & 10.364 \
#> & (23.649) & (17.155) & (25.813) \
#> Time to 60 & 2.744* & 2.640* & 1.063 \
#> & (0.751) & (0.432) & (0.637) \
#> \hline
#> \end{tabular}
#> \end{table}
由 reprex package (v2.0.1)
于 2022-02-04 创建
是否有任何 R 包可以 (a) 让您将模型放在相邻的列中,以及 (b) 在不同的行中堆叠回归?我看到了一种可能的解决方案 (R stargazer stack several regression models in rows under each other),但这并不能完全解决问题,因为它包含所有模型系数并添加了其他垃圾。
下面是我尝试创建的示例。行是因变量,列是模型规格:
我可以通过为单个因变量估计第 1、2、3 列中的模型来创建我想要的一行:
data(mtcars)
attach(mtcars)
library(stargazer)
m1 <- lm(mpg ~ cyl, mtcars)
m2 <- lm(mpg ~ cyl + disp, mtcars)
m3 <- lm(mpg ~ cyl + disp + hp, mtcars)
stargazer(m1,m2,m3, keep = c('cyl'), type = 'text')
这个怎么样。我写了一个小函数来获取模型列表并生成所需的输出。
myfun <- function(..., param_num = 2, cn = NULL, rn=NULL, output=c("text", "md", "latex")){
### ... is a list where each element is itself a list of models that
### will be used in in the corresponding column
### param_num is a vector of number isdentify the index of the
### coefficient to be used for that column
### cn is a vector of column names for the table.
### rn is a vector of names of the rows of the table
### output a string indicating the kind of output you want. If "md" (markdown), then
### the knitr package will be loaded. If "latex", then the xtable package will be loaded.
### This function requires the psre package
require(psre)
output = match.arg(output)
args <- list(...)
if(length(param_num) == 1)param_num <- rep(param_num, length(args))
b <- lapply(1:length(args), function(x)sapply(args[[x]], function(y)coef(y)[param_num[x]]))
se <- lapply(1:length(args), function(x)sapply(args[[x]], function(y)sqrt(diag(vcov(y)))[param_num[x]]))
res_df <- lapply(1:length(args), function(x)sapply(args[[x]], function(y)y$df.residual))
t <- lapply(1:length(b), function(i)b[[i]]/se[[i]])
p <- lapply(1:length(b), function(i)2*pt(abs(t[[i]]), df=res_df[[i]], lower.tail=FALSE))
cols <- sapply(1:length(b), function(i)shuffle(b[[i]], p[[i]], se[[i]]))
cols <- cbind("", cols)
if(is.null(cn)){
colnames(cols) <- c("DV", paste0("Spec ", 1:length(b)))
}else{
colnames(cols) <- cn
}
if(is.null(rn)){
cols[seq(1, nrow(cols), by=2), 1] <- paste0("Var ", seq_along(seq(1, nrow(cols), by=2)))
}else{
cols[seq(1, nrow(cols), by=2), 1] <- rn
}
if(output == "text"){
print(noquote(cols))
}
if(output == "md"){
require(knitr)
print(knitr::kable(cols))
}
if(output == "latex"){
require(xtable)
print(xtable(cols), include.rownames=FALSE)
}
}
library(mtcars)
#> Error in library(mtcars): there is no package called 'mtcars'
col1_mods <- list(lm(mpg ~ vs + cyl, data=mtcars),
lm(disp ~ vs + cyl, data=mtcars),
lm(hp ~ vs + cyl, data=mtcars),
lm(qsec ~ vs + cyl, data=mtcars))
col2_mods <- list(lm(mpg ~ vs , data=mtcars),
lm(disp ~ vs , data=mtcars),
lm(hp ~ vs , data=mtcars),
lm(qsec ~ vs , data=mtcars))
col3_mods <- list(lm(mpg ~ vs + cyl + am, data=mtcars),
lm(disp ~ vs + cyl + am, data=mtcars),
lm(hp ~ vs + cyl + am, data=mtcars),
lm(qsec ~ vs + cyl + am, data=mtcars))
myfun(col1_mods, col2_mods, col3_mods,
rn = c("MPG", "Displacement", "Horsepower", "Time to 60"),
output = "text")
#> Loading required package: psre
#> DV Spec 1 Spec 2 Spec 3
#> [1,] MPG -0.939 7.940* 1.295
#> [2,] (1.978) (1.632) (2.193)
#> [3,] Displacement 15.047 -174.693* -20.554
#> [4,] (33.561) (31.597) (37.519)
#> [5,] Horsepower -19.115 -98.365* 10.364
#> [6,] (23.649) (17.155) (25.813)
#> [7,] Time to 60 2.744* 2.640* 1.063
#> [8,] (0.751) (0.432) (0.637)
myfun(col1_mods, col2_mods, col3_mods,
rn = c("MPG", "Displacement", "Horsepower", "Time to 60"),
output = "md")
#> Loading required package: knitr
#>
#>
#> |DV |Spec 1 |Spec 2 |Spec 3 |
#> |:------------|:--------|:---------|:--------|
#> |MPG |-0.939 |7.940* |1.295 |
#> | |(1.978) |(1.632) |(2.193) |
#> |Displacement |15.047 |-174.693* |-20.554 |
#> | |(33.561) |(31.597) |(37.519) |
#> |Horsepower |-19.115 |-98.365* |10.364 |
#> | |(23.649) |(17.155) |(25.813) |
#> |Time to 60 |2.744* |2.640* |1.063 |
#> | |(0.751) |(0.432) |(0.637) |
myfun(col1_mods, col2_mods, col3_mods,
rn = c("MPG", "Displacement", "Horsepower", "Time to 60"),
output = "latex")
#> Loading required package: xtable
#>
#> Attaching package: 'xtable'
#> The following object is masked from 'package:psre':
#>
#> caption
#> % latex table generated in R 4.1.2 by xtable 1.8-4 package
#> % Fri Feb 4 09:46:07 2022
#> \begin{table}[ht]
#> \centering
#> \begin{tabular}{llll}
#> \hline
#> DV & Spec 1 & Spec 2 & Spec 3 \
#> \hline
#> MPG & -0.939 & 7.940* & 1.295 \
#> & (1.978) & (1.632) & (2.193) \
#> Displacement & 15.047 & -174.693* & -20.554 \
#> & (33.561) & (31.597) & (37.519) \
#> Horsepower & -19.115 & -98.365* & 10.364 \
#> & (23.649) & (17.155) & (25.813) \
#> Time to 60 & 2.744* & 2.640* & 1.063 \
#> & (0.751) & (0.432) & (0.637) \
#> \hline
#> \end{tabular}
#> \end{table}
由 reprex package (v2.0.1)
于 2022-02-04 创建