rmodel-comparison

Extracting AIC from multiple regression models


I have a few binary logistic regression models in R (over a 100). I would like to list all the individual regression models along with their AIC, Null deviance, residual deviance etc. in this format

  Model      AIC        Null deviance
     reg1      155.13        ..
     reg2      154.     
     reg3      

Is it possible to have a code that would achieve this for me avoiding the manual work

Thank you


Solution

  • You can extract the parameters of the model from the list of models using sapply function. Then aggregate these parameters in a data frame.

    Please see the code below together with simulated list of models for logistic regression.

    set.seed(123)
    
    # simulation
    # generates list of 100 models
    
    xs <-  replicate(
      n = 100, 
      expr = {
        x <- rnorm(1000)          
        z <- 1 + 2 * x        
        pr <- 1 / (1 + exp(-z))         
        y <- rbinom(1000, 1, pr)      
    
        #now feed it to glm for logistic regression
        model <- glm(y ~ x, family = "binomial")
        model
      },
      simplify = FALSE
    )
    
    # list of models created
    # now let's create a data frame
    # extracting parameter from the models
    summary(xs[[1]])
    df <- data.frame(
      model = seq_along(xs),
      aic = sapply(xs, AIC),
      null_deviance = sapply(xs, function(x) x$null.deviance),
      df_residual = sapply(xs, function(x) x$df.residual)
    )
    head(df)
    

    Output:

      model      aic null_deviance df_residual
    1     1 867.0781      1259.696         998
    2     2 853.3573      1311.370         998
    3     3 904.3534      1276.693         998
    4     4 922.6175      1296.127         998
    5     5 884.0100      1271.172         998
    6     6 878.9568      1289.871         998