【问题标题】:Is there a way to merge lists of regression summaries in R?有没有办法在 R 中合并回归摘要列表?
【发布时间】:2020-06-13 03:59:33
【问题描述】:

我模拟了各种大小和“形状”的对数伽马数据,然后将伽马和对数正态模型拟合到这些模拟数据。

这是我的相关代码:

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)]
  )
} 

我现在的问题是我想在一个表中生成这些回归结果的并排比较,其中我的设计矩阵的每一行 [1] 对应于函数输出的第一行,并且再次为第 [2] 行,一直到第 [40] 行。

理想情况下,它看起来像

尺寸 |阿尔法 |总结伽玛glm |摘要对数正态glm

总共 40 行,每个大小和 alpha 组合一个,以便最容易地解释结果。

基本上,我只想合并 design.matrix 和摘要。

不幸的是,生成 glm 摘要的数据框一直很困难,我无法找到一种方法来逐行合并这些结果,就像它想要的那样。

我已经看到,使用 lapply、tidy 和 Glance 为我提供了我想要的每个摘要的所有信息,但是这两个都给我留下了一个数据框列表,并且逐行组合它们也躲过了我。

如果我要使用这种方法,我仍然想将 lapply(model, tidy) 的第 [1] 行与 lapply(model,glance) 的第 [1] 行、lapply(model, tidy) 与 lapply(model,glance) 的行 [2] 等,即使每个列表的行都是不同维度的小标题。

我怎样才能最好地做到这一点?有没有更简单的方法来实现我想要的?

编辑:我已经设法通过单元素列表的列表获得偏差残差。仍然不确定如何将这些合并到 AIC 值等。

【问题讨论】:

  • summary of models 本身就是许多组件的列表。您想将所有这些信息保存在表格的一列中吗?
  • 代码报错。什么是b_0b_1
  • 您需要确定您想要的摘要对象的哪些部分。摘要对象是一个复杂的对象,因此您需要将其部分丢弃。
  • @Parfait 我已经添加了我想保留的每个摘要中的特定组件。
  • @IRTFM 我已经更新了帖子,以便清楚我想要的值。不过,我仍在努力提取所有这些内容。尤其是每个 glm 摘要中的偏差残差。

标签: r merge lapply


【解决方案1】:

考虑使用Map 的元素循环(包装到mapply)构建数据框列表,并在每次迭代中运行这两个模型,然后将summary 的所需组件提取到数据框:

定义方法

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致电

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)

输出

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
...

【讨论】:

    【解决方案2】:

    解决这个问题的方法是首先查看:

    str(gm_glog[[1]] 
    

    ....并确定您想要的项目的名称:

    对于截距和斜坡:

    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
    

    对于 AIC 和偏差残值:

    这是一种返回矩阵的方法,该矩阵的列是 AIC(在列表项“aic”中找到)和剩余偏差(在列表项“deviance”中找到)值。与 R 从具有一致数量元素的 sapply 调用返回的内容一样,结果位于结果矩阵的列中,您可以转置以获得与您的设计矩阵一致的部分:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-24
      • 2021-11-01
      • 1970-01-01
      • 2017-12-11
      相关资源
      最近更新 更多