Is there a way to merge lists of regression summaries in R?

Viewed 508

I have simulated log-gamma data of various sizes and 'shapes', and then fit a gamma and lognormal model to these simulated data.

Here is my relevant code:

gm_glog <- function(size.i, alpha.i) {
  x_i <- runif(size.i, 0, 1)    # draw a sample of size 'size'
  y.true <- exp(b_0 + b_1*x_i)  # produce log gamma data
  y_i <- rgamma(size.i, rate = alpha.i/y.true, shape = alpha.i) # random gamma sample

  # Gamma Model
  log_gamma_model <- glm(y_i ~ x_i, family = Gamma(link = "log"),
                         control = glm.control(maxit=100, trace = TRUE),
                         start = c(0.1, 0.2))      
  log_gamma_summ <- summary(log_gamma_model)

  # Lognormal Model
  log_norm_model <- glm(y_i ~ x_i, family = gaussian(link = "log"), 
                        control = glm.control(maxit=500, trace = TRUE), 
                        start = c(0.1, 0.2))      
  log_norm_summ <- summary(log_norm_model)

  # DATA FRAME BUILD
  data.frame(size = size.i, 
             alpha = alpha.i,

             gamma_mod_int = log_gamma_summ$coefficients["(Intercept)", "Estimate"],
             gamma_mod_est = log_gamma_summ$coefficients["x_i", "Estimate"],
             gamma_mod_aic = log_gamma_summ$aic,
             gamma_mod_dev = log_gamma_summ$deviance.resid[length(log_gamma_summ$deviance.resid)],
             gamma_mod_shape = MASS::gamma.shape(log_gamma_model)$alpha,

             norm_mod_int = log_norm_summ$coefficients["(Intercept)", "Estimate"],
             norm_mod_est = log_norm_summ$coefficients["x_i", "Estimate"],
             norm_mod_aic = log_norm_summ$aic,
             norm_mod_dev = log_norm_summ$deviance.resid[length(log_norm_summ$deviance.resid)]
  )
} 

My issue now is that I want to produce a side-by-side comparison of these regression results in a single table, in which each row[1] of my design matrix corresponds to the first row of the function output, and again for row[2], all the way to row[40].

Ideally, it would look like

size | alpha | summary gamma glm | summary lognormal glm

with 40 rows total, one for each combination of size and alpha, for easiest interpretation of the results.

Essentially, I just want to merge design.matrix & the summaries.

Unfortunately, producing a data frame of glm summaries has been difficult and I cannot find a way to merge these results, row by row, like it would like to.

I have seen that using lapply, tidy, and glance gave me all of the information that I want for each of these summaries, but both of these leave me with a list of data frames, and combining them row by row has also eluded me.

If I were to use this method, I would still like to combine row[1] of lapply(model, tidy) with row[1] of lapply(model, glance), row[2] of lapply(model, tidy) with row[2] of lapply(model, glance), etc, even though the rows of each of these lists are tibbles of different dimensions.

How can I best do this? Is there an easier way to achieve what I want?

Edit: I have managed to get the deviance residuals with a list of single-element lists. Still not sure how I can merge these to the AIC values etc.

2 Answers

Consider building a list of data frames using element wise loop of Map (wrapper to mapply) and run both models in each iteration and then extract needed components of summary to data frame:

Defined Method

log_models <- function(size.i, alpha.i) {
  x_i <- runif(size.i, 0, 1)    # draw a sample of size 'size'
  y.true <- exp(b_0 + b_1*x_i)  # produce log gamma data
  y_i <- rgamma(size.i, rate = alpha.i/y.true, shape = alpha.i) # random gamma sample

  # Gamma Model
  log_gamma_model <- glm(y_i ~ x_i, family = Gamma(link = "log"),
                         control = glm.control(maxit=100, trace = TRUE),
                         start = c(0.1, 0.2))      
  log_gamma_summ <- summary(log_gamma_model)

  # Lognormal Model
  log_norm_model <- glm(y_i ~ x_i, family = gaussian(link = "log"), 
                        control = glm.control(maxit=500, trace = TRUE), 
                        start = c(0.1, 0.2))      
  log_norm_summ <- summary(log_norm_model)

  # DATA FRAME BUILD
  data.frame(size = size.i, 
             alpha = alpha.i,

             gamma_mod_int = log_gamma_summ$coefficients["(Intercept)", "Estimate"],
             gamma_mod_est = log_gamma_summ$coefficients["x_i", "Estimate"],
             gamma_mod_aic = log_gamma_summ$aic,
             gamma_mod_dev = log_gamma_summ$deviance.resid[length(log_gamma_summ$deviance.resid)],
             gamma_mod_shape = MASS::gamma.shape(log_gamma_model)$alpha,

             norm_mod_int = log_norm_summ$coefficients["(Intercept)", "Estimate"],
             norm_mod_est = log_norm_summ$coefficients["x_i", "Estimate"],
             norm_mod_aic = log_norm_summ$aic,
             norm_mod_dev = log_norm_summ$deviance.resid[length(log_norm_summ$deviance.resid)]
  )
} 

Map/mapply Call

df_list <- Map(log_models, design.matrix$size, design.matrix$alpha)
# df_list <- mapply(log_models, design.matrix$size, design.matrix$alpha, SIMPLIFY=FALSE)

final_df <- do.call(rbind, df_list)

Output

final_df
#     size alpha gamma_mod_int gamma_mod_est gamma_mod_aic gamma_mod_dev gamma_mod_shape norm_mod_int norm_mod_est norm_mod_aic  norm_mod_dev
# 5      5   0.1   -2.39484838      3.808953      2.349387     1.6062347      0.25294152   -0.3943182    0.4366572     21.50163  2.2462398978
# 10    10   0.1   -0.03146698     -1.752435    -48.768787    -2.4685411      0.15839450 -769.8179792  797.7937171     16.72900  0.0073639677
# 15    15   0.1   -6.22434742     11.420125   -146.836144     2.7585789      0.11692945   -0.1601247    1.6135214    102.27202 22.0098432208
# 30    30   0.1    0.26381051      1.067361   -298.873575    -4.7725793      0.08641668    0.2565112    1.0687070    195.59417 -1.7643885736
# 51     5   0.2  -12.23809196     12.760998    -52.109115     0.0412409      0.31666275  -11.1636898   11.2453833    -48.17426  0.0006702163
# 101   10   0.2    1.51817293     -6.261376    -91.417016    -0.7455693      0.12372107   -0.4463434   -1.1394914     31.86825 -0.1580558441
# 151   15   0.2   -0.54878568      3.672312    -17.724359    -1.0910863      0.14922850   -2.7737690    6.2481058    101.48735  0.0621486528
# 301   30   0.2    0.84636917     -1.208503    -25.603596     0.1811917      0.19949756    0.6339933   -0.6533998    168.03056  0.0819567624
# 52     5   0.3   -0.45653740     -2.541001      4.907533     0.8486617      0.66655843   -0.7883221   -0.7289522     10.27774  0.4708082262
# 102   10   0.3    0.70548641     -2.790209     13.450575     0.3375955      0.54226062    1.3245745   -9.0701981     24.19732 -0.8978180162
...

The way to approach this is to first look at:

str(gm_glog[[1]] 

....and determine the names of the items you want:

For the Intercept and the slopes:

do.call( rbind, sapply(gm_glog, function(x){ x[c("coefficients")]}) )
             (Intercept)         x_i
coefficients  2.33991821 -20.7836582
coefficients 13.33466647 -31.4034737
coefficients  2.24020883  -3.1949161
coefficients -1.41151531   1.0243415
coefficients -0.81649523   1.2787418
coefficients -1.53695481   0.7518618
coefficients -4.86985066   7.5985577
snipped the rest

For the AIC and deviance residual value:

Here's a method for returning a matrix with columns that are the AIC (found in the list item "aic") and residual deviance (found in the list item "deviance") values. As always with what R returns from an sapply call with a consistent number of elements, the results are in columns of the resultant matrix and you would transpose to get some that lines up with your design matrix:

sapply(gm_glog, function(x){ x[c("deviance", "aic")]})
         [,1]      [,2]      [,3]     [,4]      [,5]     [,6]      [,7]     [,8]     [,9]      [,10]    [,11]   
deviance 17.52917  78.81847  239.01   553.7603  29.27955 58.71526  77.9131  147.4969 29.97461  39.20052 40.13341
aic      -33.49309 -77.06459 -117.259 -389.6077 2.919589 -21.44068 11.57039 67.40446 -10.87137 31.8441  19.54028
         [,12]     [,13]    [,14]    [,15]    [,16]    [,17]    [,18]    [,19]    [,20]    [,21]    [,22]    [,23]   
deviance 198.8519  7.185649 38.97136 47.7754  80.16326 6.465192 11.35418 22.99457 83.80098 5.192405 8.945869 39.36833
aic      -23.23857 7.554898 -16.0006 27.28793 63.8827  11.50956 43.5854  33.28914 58.52796 26.10081 28.88124 33.08681
         [,24]    [,25]    [,26]    [,27]    [,28]    [,29]    [,30]    [,31]    [,32]    [,33]     [,34]    [,35]   
deviance 54.99003 7.045267 14.42835 26.74579 31.64986 1.670572 3.71758  24.23743 47.28533 0.2497075 12.76083 17.40761
aic      72.41119 3.920895 34.28885 24.2481  55.23406 15.1922  28.20926 44.49589 83.13905 11.19624  41.62632 37.05153
         [,36]    [,37]    [,38]    [,39]    [,40]   
deviance 35.25456 12.10367 9.070027 34.15762 29.88891
aic      65.23201 19.17986 34.25908 33.74274 71.36175
Related