Are there any R packages that will (a) Let you put models in adjacent columns, and (b) stack regressions in different rows? I have seen one possible solution (R stargazer stack several regression models in rows under each other) but that doesn't quite solve the problem because it includes all model coefficients and adds other junk.
An example of what I'm trying to create is below. The rows are dependent variables and the columns are model specifications:
I can create one row of what I want by estimating the models in columns 1, 2, 3 for a single dependent variable:
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')
How about this. I wrote a little function that would take lists of models and produce the required output.
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}
Created on 2022-02-04 by the reprex package (v2.0.1)