1

Looking to create an AIC selection table for a publication in LaTex format, but I can not seem to get the form I want. I have googled this to death and was VERY surprised I couldn't find an answer. I've found answers to much more obscure questions in R.

Below is a bit of code, a few tables I made that I'm not overly keen on, and at the bottom is the general structure I'd like to make, but in nice latex table format as the stargazer package does.

I tried to use extra arguments for both packages to attain what I wanted but was unsuccessful.

##Create dummy variables
a<-1:10
b<-c(10:3,1,2)
c<-c(1,4,5,3,7,3,6,2,4,5)

##Create df
df<-data.frame(a,b,c)

##Build models
m1<-lm(a~b,data=df)
summary(m1)
m2<-lm(a~c,data=df)
m3<-lm(a~b+c,data=df)
m4<-lm(a~b*c,data=df)

##View list of AIC values
AIC(m1,m2,m3,m4)

########################CREATE AIC SELECTION TABLE
##Using MuMIn Package
library(MuMIn)
modelTABLE <- model.sel(m1,m2,m3,m4)
View(modelTABLE)  ##No AIC values, just AICc, no R-squared, and model name (i.e, a~b) not present

##Using stargazer Package
library(stargazer)
test<-stargazer(m1,m2,m3,m4 ,
                type = "text", 
                title="Regression Results", 
                align=TRUE,
                style="default",
                dep.var.labels.include=TRUE,
                flip=FALSE
                ## ,out="models.htm"
)

View(test)  ##More of a table depicting individual covariate attributes, bottom of table doesn't have AIC


###Would like a table similar to the following
Model  ModelName  df    logLik      AIC    delta   AICweight    R2
m1     a ~ b      3    -6.111801    18.2    0      0.95         0.976
m3     a ~ b + c  4    -5.993613    20      1.8    0.05         0.976
m4     a ~ b * c  5    -5.784843    21.6    3.4    0.00         0.977
m2     a ~ c      3    -24.386821   54.8    36.6   0.00         0.068
 `
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ctlamb
  • 131
  • 1
  • 4
  • 14

1 Answers1

1

model.sel result is a data.frame, so you can modify it (add model names, round numbers etc) and export to latex using e.g. latex from Hmisc package.

# include R^2: 
R2 <- function(x) summary(x)$r.squared
ms <- model.sel(m1, m2, m3, m4, extra = "R2")

i <- 1:4 # indices of columns with model terms
response <- "a"

res <- as.data.frame(ms)
v <- names(ms)[i]
v[v == "(Intercept)"] <- 1

# create formula-like model names:
mnames <- apply(res[, i], 1, function(x) 
     deparse(simplify.formula(reformulate(v[!is.na(x)], response = response))))
## OR
#   mnames <- apply(res[, i], 1, function(x)
#          sapply(attr(ms, "modelList"), function(x) deparse(formula(x)))

res <- cbind(model = mnames, res[, -i])
Hmisc::latex(res, file = "")
Kamil Bartoń
  • 1,482
  • 9
  • 10
  • Thanks Kamil! The second line, ms<-model.sel... produces this error when I run my example and then your script? Error in names(models) <- .makemnames(sys.call()) : 'names' attribute [5] must be the same length as the vector [4] – ctlamb Jan 28 '15 at 16:02
  • Yes, it is a bug in the current version. Update the package from R-forge: `install.packages("MuMIn", repos="http://R-Forge.R-project.org")` – Kamil Bartoń Jan 28 '15 at 16:40
  • Thank you Kamil, I got it. One last question- Is there a way to output the full formula, i.e., not a simplified model (ex. d+e+d*e, not d*e),,,I tried to remove the simplify.formula argument but that didn't work. I understand that is what this fn is doing though – ctlamb Jan 29 '15 at 03:52
  • You should check your code again. `deparse(reformulate(...))` does produce an expanded formula. `reformulate` combines column names, `c("1", "b", "c", "b:c")`, so interactions in the resulting formula do not have a shorthand form (`b*c`). – Kamil Bartoń Jan 29 '15 at 13:13