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