【发布时间】:2021-03-18 14:42:51
【问题描述】:
我正在运行一项模拟研究,我的结果存储在一个嵌套列表结构中。列表的第一级代表模型生成的不同超参数。第二层是同一模型的复制次数(改变种子)。
在下面的示例中,我列出了由两个超参数(hyperpar1 和 hyperpar2)控制的模型的输出,其中两个超参数都可以采用 2 个不同的值,从而导致生成的模型有 4 种不同的组合。此外,4 种可能的组合中的每一种都运行了两次(不同的种子),产生了 8 种可能的组合。最后,从模型的每次可能迭代中恢复了两个性能指标(metric1 和 metric2)以及模型的两个参数的值 beta = list(b1 = value, b2 = value)。
我想将此信息放入data.frame,同时保留两件事。
- 我想保留对象的类(特别是与
time_iter相关,它以给定单位测量迭代时间) - 我希望列表
betas在每次迭代中的每个组件都有一个单独的列,比如说b1和b2。
样本数据:
res <-list(
list(list(modeltype = "tree", time_iter = structure(0.7099, class = "difftime", units = "secs"),seed = 1, nobs = 75, hyperpar1 = 0.5, hyperpar2 = 0.5, metric1 = 0.4847, metric2 = 0.2576, beta = list(b1 = 0.575, b2 =0.745)),
list(modeltype = "tree", time_iter = structure(0.058 , class = "difftime", units = "secs"),seed = 2, nobs = 75, hyperpar1 = 0.5, hyperpar2 = 0.5, metric1 = 0.4013, metric2 = 0.2569, beta = list(b1 = 0.535, b2 =0.775))),
list(list(modeltype = "tree", time_iter = structure(0.046 , class = "difftime", units = "secs"),seed = 1, nobs = 75, hyperpar1 = 0.8, hyperpar2 = 0.5, metric1 = 0.4755, metric2 = 0.2988, beta = list(b1 = 0.541, b2 =0.702) ),
list(modeltype = "tree", time_iter = structure(0.0474, class = "difftime", units = "secs"),seed = 2, nobs = 75, hyperpar1 = 0.8, hyperpar2 = 0.5, metric1 = 0.2413, metric2 = 0.2147, beta = list(b1 = 0.545, b2 =0.793) )),
list(list(modeltype = "tree", time_iter = structure(0.0502, class = "difftime", units = "secs"),seed = 1, nobs = 75, hyperpar1 = 0.5, hyperpar2 = 1 , metric1 = 0.7131, metric2 = 0.5024, beta = list(b1 = 0.500, b2 =0.722) ),
list(modeltype = "tree", time_iter = structure(2.9419, class = "difftime", units = "secs"),seed = 2, nobs = 75, hyperpar1 = 0.5, hyperpar2 = 1 , metric1 = 0.4254, metric2 = 0.2824, beta = list(b1 = 0.555, b2 =0.712) )),
list(list(modeltype = "tree", time_iter = structure(0.041 , class = "difftime", units = "secs"),seed = 1, nobs = 75, hyperpar1 = 0.8, hyperpar2 = 1 , metric1 = 0.6709, metric2 = 0.4092, beta = list(b1 = 0.578, b2 =0.701) ),
list(modeltype = "tree", time_iter = structure(0.0396, class = "difftime", units = "secs"),seed = 2, nobs = 75, hyperpar1 = 0.8, hyperpar2 = 1 , metric1 = 0.4585, metric2 = 0.4115, beta = list(b1 = 0.501, b2 =0.777) )))
这是我的尝试。首先我未列出列表,然后 cbind 每个组件。如您所见,它失败得很惨。
un <- do.call(c, unlist(res, recursive=FALSE))
do.call(rbind.data.frame, un)
b1 b2
modeltype tree tree
time_iter 0.7099 0.7099
seed 1 1
nobs 75 75
hyperpar1 0.5 0.5
hyperpar2 0.5 0.5
metric1 0.4847 0.4847
metric2 0.2576 0.2576
beta 0.575 0.745
modeltype1 tree tree
time_iter1 0.058 0.058
seed1 2 2
nobs1 75 75
hyperpar11 0.5 0.5
hyperpar21 0.5 0.5
metric11 0.4013 0.4013
metric21 0.2569 0.2569
beta1 0.535 0.775
modeltype2 tree tree
time_iter2 0.046 0.046
期望的输出
structure(list(modeltype = c("tree", "tree", "tree", "tree","tree", "tree", "tree", "tree"),
time_iter = structure(c(0.7099,0.058, 0.046, 0.0474, 0.0502, 2.9419, 0.041, 0.0396),
class = "difftime", units = "secs"),
seed = c(1, 2, 1, 2, 1, 2, 1, 2),
nobs = c(75, 75, 75, 75, 75, 75, 75, 75),
hyperpar1 = c(0.5, 0.5, 0.8, 0.8, 0.5, 0.5,0.8, 0.8),
hyperpar2 = c(0.5, 0.5, 0.5, 0.5, 1, 1, 1, 1),
metric1 = c(0.4847, 0.4013, 0.4755, 0.2413, 0.7131, 0.4254, 0.6709, 0.4585),
metric2 = c(0.2576, 0.2569, 0.2988, 0.2147, 0.5024, 0.2824, 0.4092, 0.4115),
b1 = c(0.575, 0.535, 0.541, 0.545, 0.5, 0.555, 0.578, 0.501),
b2 = c(0.745, 0.775, 0.702,0.793, 0.722, 0.712, 0.701, 0.777)),
row.names = c(NA, -8L), class = "data.frame")
modeltype time_iter seed nobs hyperpar1 hyperpar2 metric1 metric2 b1 b2
1 tree 0.7099 secs 1 75 0.5 0.5 0.4847 0.2576 0.575 0.745
2 tree 0.0580 secs 2 75 0.5 0.5 0.4013 0.2569 0.535 0.775
3 tree 0.0460 secs 1 75 0.8 0.5 0.4755 0.2988 0.541 0.702
4 tree 0.0474 secs 2 75 0.8 0.5 0.2413 0.2147 0.545 0.793
5 tree 0.0502 secs 1 75 0.5 1.0 0.7131 0.5024 0.500 0.722
6 tree 2.9419 secs 2 75 0.5 1.0 0.4254 0.2824 0.555 0.712
7 tree 0.0410 secs 1 75 0.8 1.0 0.6709 0.4092 0.578 0.701
8 tree 0.0396 secs 2 75 0.8 1.0 0.4585 0.4115 0.501 0.777
最后,我想说我检查了Converting nested list to dataframe、How to convert a list consisting of vector of different lengths to a usable data frame in R?、How to convert a list consisting of vector of different lengths to a usable data frame in R? 和Convert R list to dataframe with missing/NULL elements,这些问题/答案都没有解决我的问题,因为我的示例的嵌套结构不同。
【问题讨论】: