如何在模拟研究中使用 lbreg 立即获得系数
How to get coefficients at once using lbreg in a simulation study
我有 1000 个数据集,我有 运行 使用 logbinomial 包 (lbreg) 进行分析,但我的结果在列表中,我正在尝试将其转换为数据框。我有一个代码并将提供列表的样子,因为该列表对于 1000 个数据集的每个输出都有 23 个元素
我已经尝试了很多东西但没有转换所以我已经弄清楚系数是如何打包的但是现在我需要得到它们一次而不是做 1000 次
library(lbreg)
nsim = 1000
for(k in 1:nsim){
dat <- mydata1[mydata1$Sim == k,]
sp <- split(mydata1, mydata1$Sim)
model_list <- lapply(sp, function(dat){
tryCatch(model1 <- lbreg(y ~ x1+x2, family = binomial(link = log), data = dat),
error = function(e) e)
})
}
ok <- !sapply(model_list, inherits, 'error')
fitlist <- model_list[ok]
我试过使用
data.frame(matrix(unlist(fitlist), nrow=1000, byrow=T),stringsAsFactors=FALSE)
但是失败了。
如果我这样做,我会得到系数,但我必须达到 1000 才能得到所有系数
data.frame(coef(fitlist$`1`), coef(fitlist$`2`), coef(fitlist$`3`), coef(fitlist$`4`), coef(fitlist$`4`), coef(fitlist$`5`))
对于一个数据集,如果你想查看
https://github.com/Seanlove89/logitdata
#Using base R
do.call("rbind",lapply(fitlist, function(x) data.frame(t(coef(x)))))
#or using `purrr`
purrr::map_df(set_names(fitlist, nm=1:length(lstfit)),
~data.frame(t(coef(.x))),
.id = "model")
我有 1000 个数据集,我有 运行 使用 logbinomial 包 (lbreg) 进行分析,但我的结果在列表中,我正在尝试将其转换为数据框。我有一个代码并将提供列表的样子,因为该列表对于 1000 个数据集的每个输出都有 23 个元素
我已经尝试了很多东西但没有转换所以我已经弄清楚系数是如何打包的但是现在我需要得到它们一次而不是做 1000 次
library(lbreg)
nsim = 1000
for(k in 1:nsim){
dat <- mydata1[mydata1$Sim == k,]
sp <- split(mydata1, mydata1$Sim)
model_list <- lapply(sp, function(dat){
tryCatch(model1 <- lbreg(y ~ x1+x2, family = binomial(link = log), data = dat),
error = function(e) e)
})
}
ok <- !sapply(model_list, inherits, 'error')
fitlist <- model_list[ok]
我试过使用
data.frame(matrix(unlist(fitlist), nrow=1000, byrow=T),stringsAsFactors=FALSE)
但是失败了。
如果我这样做,我会得到系数,但我必须达到 1000 才能得到所有系数
data.frame(coef(fitlist$`1`), coef(fitlist$`2`), coef(fitlist$`3`), coef(fitlist$`4`), coef(fitlist$`4`), coef(fitlist$`5`))
对于一个数据集,如果你想查看 https://github.com/Seanlove89/logitdata
#Using base R
do.call("rbind",lapply(fitlist, function(x) data.frame(t(coef(x)))))
#or using `purrr`
purrr::map_df(set_names(fitlist, nm=1:length(lstfit)),
~data.frame(t(coef(.x))),
.id = "model")