Home > Blockchain >  Stacking regressions in a table?
Stacking regressions in a table?

Time:01-30

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 (table example

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')

CodePudding user response:

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(mods), by=2), 1] <- paste0("Var ", seq_along(seq(1, nrow(mods), by=2)))
  }else{
    cols[seq(1, nrow(mods), 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)
  }
}

myfun(col1_mods, col2_mods, col3_mods, 
      rn = c("MPG", "Displacement", "Horsepower", "Time to 60"), 
      output = "text")

#      DV           Spec 1  Spec 2  Spec 3  
# [1,] MPG          -2.876* -2.501* -2.138* 
# [2,]              (0.322) (0.361) (0.715) 
# [3,] Displacement 62.599* 56.621* 50.852* 
# [4,]              (5.469) (6.169) (12.242)
# [5,] Horsepower   31.958* 37.253* 40.162* 
# [6,]              (3.884) (4.234) (8.423) 
# [7,] Time to 60   -0.592* -0.979* -0.681* 
# [8,]              (0.147) (0.109) (0.208) 

myfun(col1_mods, col2_mods, col3_mods, 
      rn = c("MPG", "Displacement", "Horsepower", "Time to 60"), 
      output = "md")

# |DV           |Spec 1  |Spec 2  |Spec 3   |
# |:------------|:-------|:-------|:--------|
# |MPG          |-2.876* |-2.501* |-2.138*  |
# |             |(0.322) |(0.361) |(0.715)  |
# |Displacement |62.599* |56.621* |50.852*  |
# |             |(5.469) |(6.169) |(12.242) |
# |Horsepower   |31.958* |37.253* |40.162*  |
# |             |(3.884) |(4.234) |(8.423)  |
# |Time to 60   |-0.592* |-0.979* |-0.681*  |
# |             |(0.147) |(0.109) |(0.208)  |

myfun(col1_mods, col2_mods, col3_mods, 
      rn = c("MPG", "Displacement", "Horsepower", "Time to 60"), 
      output = "latex")

# % latex table generated in R 4.1.2 by xtable 1.8-4 package
# % Sat Jan 29 20:02:00 2022
# \begin{table}[ht]
# \centering
# \begin{tabular}{llll}
# \hline
# DV & Spec 1 & Spec 2 & Spec 3 \\ 
# \hline
# MPG & -2.876* & -2.501* & -2.138* \\ 
# & (0.322) & (0.361) & (0.715) \\ 
# Displacement & 62.599* & 56.621* & 50.852* \\ 
# & (5.469) & (6.169) & (12.242) \\ 
# Horsepower & 31.958* & 37.253* & 40.162* \\ 
# & (3.884) & (4.234) & (8.423) \\ 
# Time to 60 & -0.592* & -0.979* & -0.681* \\ 
# & (0.147) & (0.109) & (0.208) \\ 
# \hline
# \end{tabular}
# \end{table}

  •  Tags:  
  • Related