Zhiqiang Wang
Zhiqiang Wang

Reputation: 6769

Improve efficiency of fitting many models

Thanks to those who helped me with previous questions about coefficients and p values from many models. Now, I can get all coefficients, p values, and AIC values from many models.

md <- "mpg ~ cyl" 
xlist <- c("disp", "hp", "am")
n <- length(xlist)
comb_lst <- unlist(lapply(1:n, function(x) combn(xlist, x, simplify = F)), recursive = F)
md_lst <- lapply(comb_lst, function(x) paste(md, "+", paste(x, collapse = "+")))

coefs <- unlist(lapply(md_lst, function(x) lm(as.formula(x),data = mtcars)$coe[2]))
pvalues <- unlist(lapply(md_lst, function(x) summary(lm(as.formula(x), data = mtcars))$coe["cyl", 4]))
aic <- unlist(lapply(md_lst, function(x) AIC(lm(as.formula(x), data = mtcars))))

As you can see from the above code, each of the last 3 lines will fit many models once independently. This means that the code will fit the same set of models 3 times. This can be time consuming with large data and many variables. My question is how to fit all the models once, and then pick up coefficients, p values and AIC values later.

Upvotes: 2

Views: 158

Answers (3)

Cole
Cole

Reputation: 11255

This uses the ability to put a FUN into the combn() call. As with the other answers, it only does the linear regression once.

md <- "mpg ~ cyl" 
xlist <- c("disp", "hp", "am")

all_models <- unlist(
                lapply(seq_along(xlist),
                       function (k) {
                         combn(xlist,
                               k,
                               FUN = function (x) {
                                 form <- formula(paste(md, '+', paste(x, collapse = '+')))
                                 eval(bquote(lm(.(form), data = mtcars)   ))
                                 },
                               simplify = F
                         )
                         }
                       )
                ,recursive = F
                )

coefs <- sapply(all_models, function(x) x$coe[2])
pvalues <- sapply(all_models, function(x) summary(x)$coe["cyl", 4])
aic <- sapply(all_models, function(x) AIC(x))

Upvotes: 2

NelsonGon
NelsonGon

Reputation: 13319

For convenience, I have used a function from a package I wrote. It can be avoided by using summary as shown in another answer.

We can do:

models <- lapply(md_lst, function(x) do.call(lm, list(formula =x,
                                                      data=mtcars)) )
sapply(models, function(x) { cbind(coef(x),
 manymodelr::extract_model_info(x, "p_value")["cyl"], AIC(x) )})

I don't want to use packages(manymodelr)

sapply(models, function(x) {
  cbind(coef(x),coef(summary(x))[,4]["cyl"],
        AIC(x)
        )})

This gives us a list of matrices where each column represents the coefficients, p values and AIC respectively.

Results(truncated)

   [[1]]
                       [,1]       [,2]     [,3]
    (Intercept) 34.66099474 0.03366495 167.1456
    cyl         -1.58727681 0.03366495 167.1456
    disp        -0.02058363 0.03366495 167.1456

    [[2]]
                      [,1]         [,2]     [,3]
    (Intercept) 36.9083305 0.0004803752 169.5618
    cyl         -2.2646936 0.0004803752 169.5618
    hp          -0.0191217 0.0004803752 169.5618

    [[3]]
                     [,1]        [,2]     [,3]
    (Intercept) 34.522443 1.28456e-07 167.2191
    cyl         -2.500958 1.28456e-07 167.2191
    am           2.567035 1.28456e-07 167.2191

    [[4]]
                       [,1]      [,2]     [,3]
    (Intercept) 34.18491917 0.1349044 168.0184
    cyl         -1.22741994 0.1349044 168.0184
    disp        -0.01883809 0.1349044 168.0184
    hp          -0.01467933 0.1349044 168.0184

Upvotes: 3

kath
kath

Reputation: 7724

You can do:

models <- lapply(md_lst, function(x) lm(as.formula(x), data = mtcars))
coefs <- unlist(lapply(models, function(x) x$coef[2]))
pvalues <- unlist(lapply(models, function(x) summary(x)$coef["cyl", 4]))
aic <- unlist(lapply(models, function(x) AIC(x)))

Upvotes: 2

Related Questions