r - 有没有办法在 R 中合并回归摘要列表?
问题描述
我模拟了各种大小和“形状”的对数伽马数据,然后将伽马和对数正态模型拟合到这些模拟数据。
这是我的相关代码:
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) 的 row[1] 与 lapply(model,glance) 的 row[1] , lapply(model, tidy) 的 row[2] 与lapply(model,glance) 等的第 [2] 行,即使这些列表中的每一个的行都是不同维度的小标题。
我怎样才能最好地做到这一点?有没有更简单的方法来实现我想要的?
编辑:我已经设法通过单元素列表列表获得偏差残差。仍然不确定如何将这些合并到 AIC 值等。
解决方案
解决此问题的方法是首先查看:
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
推荐阅读
- java - 插入后第一个元素的双端队列返回 null
- azure - Microsoft Graph API:获取用户组织
- python-3.x - Python,Keras基于预测的自定义损失函数,不是输入
- javascript - 无法使用正则表达式在模式之间提取字符串
- python - 在熊猫数据框中读取具有可变列的文本文件
- assembly - RiscV 跳转 (j, jal) 到错误地址(偏移量 2)
- ios - 为什么动画中的图片不能显示?
- javascript - 在我的网站上进行地理定位的解决方案
- javascript - 如何防止 runtime.lastError 错误消息出现在控制台中?
- swift - 如何转换字符串中的任何数字Amount