Построение таблицы результатов регрессии - PullRequest
0 голосов
/ 18 февраля 2019

Я пытаюсь построить таблицу результатов регрессии, и я застрял.Я получаю сообщение об ошибке:

Ошибка в summary(mod)$coefficients[vars, "Estimate"]: индекс за пределами.

Все эти модели запущены и помечены так.Как я хочу, чтобы моя таблица выглядела так:

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

мои функции для извлечения ключевых результатов регрессии из оценочной модели

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

построение таблицы

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
}

вывод таблицы результатов выборки

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