0

I'm attempting to build a regression results table and I'm stuck. I'm getting the error:

Error in summary(mod)$coefficients[vars, "Estimate"] : subscript out of bounds.

I have all these models run and labeled as so. What I want my table to look like:

 |          |  model1L |  model2L |  model3L |  model1P |  model2P |  model3P |
 |----------|----------|----------|----------|----------|----------|----------|
 |price     |   coef1L |   coef2L |   coef3L |   coef1P |   coef2P |   coef3P |
 |          |     sd1L |     sd2L |     sd3L |     sd1P |     sd2P |     sd3P |
 |promoflag |   coef1L |   coef2L |   coef3L |   coef1P |   coef2P |   coef3P |
 |          |     sd1L |     sd2L |     sd3L |     sd1P |     sd2P |     sd3P |

my functions to extract key regression results from an estimated model

model_list = c("model1L","model2L","model3L", "model1P", "model2P", "model3P")
vars = c("price","promoflag")

building the table

results_table1 = function(model_list, vars) {
  # build leftmost column of results table  
  outrec = c()
  for (j in 1:length(vars)) {
    outrec = c(outrec,sprintf("%s",vars[j]))
    outrec = c(outrec,"")
  }
  outrec = c(outrec,"R^2")
  outrec = c(outrec,"Observations")
  outdf = as.data.frame(outrec) 
  # process each model
  for (i in 1:length(model_list)) {
    # extract estimates for this model
    mod = eval(parse(text=model_list[i]))
    estimates = summary(mod)$coefficients[vars,"Estimate"]
    ses = summary(mod)$coefficients[vars,"Std. Error"]
    pvals = summary(mod)$coefficients[vars,"Pr(>|t|)"]
    # process each parameter of interest
    outrec = c()
    for (j in 1:length(vars)) {
      # set significance stars
      star = ""
      if (pvals[j] <= .05) {star = "*"}
      if (pvals[j] <= .01) {star = "**"}
      if (pvals[j] <= .001) {star = "***"}
      # output estimate and std err
      outrec = c(outrec,sprintf("%.4f%s",estimates[j],star))
      outrec = c(outrec,sprintf("(%.4f)",ses[j]))
    }
    # add R^2, # of observations to output
    outrec = c(outrec,sprintf("%.4f",summary(mod)$r.squared[1]))
    outrec = c(outrec,sprintf("%d",nobs(mod)))
    outdf = cbind(outdf,outrec)
  }
  # set column names to model names
  names(outdf) = c("",model_list)
  outdf
}

outputting the sample results table

model_list = c("model1L", "model2L", "model3L", "model1P", "model2P",     "model3P")
vars = c("price", "promoflag")
outdf = results_table1(model_list, vars)
library(knitr)
kable(outdf,align='c')
TylerH
  • 20,799
  • 66
  • 75
  • 101
whhpssh
  • 1
  • 1
  • This might be helpful: https://rpubs.com/tjmahr/prettytables_2015 – JasonAizkalns Feb 18 '19 at 16:06
  • Also, check out the [SO thread](https://stackoverflow.com/questions/15031338/subscript-out-of-bounds-general-definition-and-solution) for more info on your error message. – Andrew Feb 18 '19 at 16:10
  • Maybe package `stargarzer`may help you – LocoGris Feb 18 '19 at 16:35
  • the package 'broom' is also a helpful way to get model results into a dataframe – Mike Feb 18 '19 at 16:44
  • Hi Andrew, thank you! I tried their debug method and I get this: > options(error=recover) > reach_full_in <- reachability(krack_full, 'in') Error in reachability(krack_full, "in") : could not find function "reachability" No suitable frames for recover() – whhpssh Feb 18 '19 at 16:48
  • 1
    I appreciate the package suggestions! We are supposed to figure out how to produce this using code and not packages though – whhpssh Feb 18 '19 at 16:49

0 Answers0