[英]Storing meta-analysis results using for loop
I am trying to conduct a meta analysis of a group of studies. 我正在尝试对一组研究进行元分析。 I am trying to figure out the code to properly output what I would like.
我试图找出代码以正确输出我想要的东西。 Essentially I am trying to run a fixed effects meta-analysis test using the
metafor
package and collect the coefficient estimates to store them inside a matrix. 本质上,我试图使用
metafor
包运行固定效果的荟萃分析测试,并收集系数估计值以将其存储在矩阵中。
I have several problems. 我有几个问题。 For example, I want to run these tests only for research that have multiple results.
例如,我只想对具有多个结果的研究运行这些测试。 So studies whose study numbers turn up more than once (see code for example).
因此,其研究编号多次出现的研究(例如,参见代码)。 When I try this using my existing code, it does not work, it spits out a number, but it isn't the right one.
当我使用现有代码尝试此操作时,它不起作用,会吐出一个数字,但它不是正确的数字。 Also, some study-numbers are larger than the actual amount of studies I have.
另外,有些学习数量大于我的实际学习数量。 In my personal dataset there is a study numbered 3500 for example.
在我的个人数据集中,有一个研究编号为3500。 When I run my loop, R spits out the results for that particular fixed effects model on the 3500th row, instead of just placing it in the next empty row.
当我运行循环时,R在第3500行上吐出该特定固定效果模型的结果,而不仅仅是将其放在下一个空行中。
I have a basic example below that anyone can run in R. 下面有一个基本示例,任何人都可以在R中运行。
library(metafor)
origdata <- data.frame(matrix(data=NA, nrow=15, ncol=3))
colnames(origdata) <- c("studynum", "Mail_b", "Mail_SE")
origdata$studynum <- c(1, 1, 1, 1, 1, 2, 3, 3, 3, 4, 7, 7, 7, 7, 7)
origdata$Mail_b <- c(1.8, 0.8, 1.2, 1, 1, 5, 3, 3, 6, 4, 5, 8, 5, 9, 2)
origdata$Mail_SE <- c(1.6, 0.8, 1.3, 1, 1, 1, 3, 2.9, 6, 4, 5, 8, 5, 8, 1)
collapsedtest <- data.frame(matrix(data=NA, nrow=5, ncol=3))
colnames(collapsedtest) <- c("studynum", "Meta_b", "Meta_SE")
collapsedtest$studynum <- unique(origdata$studynum)
for(i in unique(origdata$studynum)) {
if((table(origdata$studynum) == 1) == FALSE){
collapsedtest[i, 2] <- (coef(summary(rma(yi=Mail_b[origdata$studynum == i],
sei=Mail_SE[origdata$studynum == i],
method="FE",
data=origdata)))$estimate)
collapsedtest[i, 3] <- (coef(summary(rma(yi=Mail_b[origdata$studynum == i ],
sei=Mail_SE[origdata$studynum == i],
method="FE", data=origdata)))$ci.ub
-
(coef(summary(rma(yi=Mail_b[origdata$studynum == i],
sei=Mail_SE[origdata$studynum == i],
method="FE",
data=origdata)))$estimate)) / 1.96
} else {
collapsed[i, 2] <- origdata$Mail_b[origdata$studynum == i]
collapsed[i, 3] <- origdata$Mail_SE[origdata$studynum == i]
}
}
Consider using nrow
and adjust your row indexer with a conditional on studynum . 考虑使用
nrow
并与studynum条件调整排索引。 Also, be sure to run your rma method only once and then use resulting estimates. 另外,请确保只运行一次rma方法,然后使用结果估计。
for(i in unique(origdata$studynum)) {
coef_data <- coef(summary(rma(yi=Mail_b[origdata$studynum == i],
sei=Mail_SE[origdata$studynum == i],
method="FE",
data=origdata)))
if(nrow(origdata[origdata$studynum==i,]) > 1) {
collapsedtest[collapsedtest$studynum==i, 2] <- coef_data$estimate
collapsedtest[collapsedtest$studynum==i, 3] <- (coef_data$$ci.ub - coef_data$estimate) / 1.96
collapsedtest[collapsedtest$studynum==i, 2] <- origdata$Mail_b[origdata$studynum == i]
collapsedtest[collapsedtest$studynum==i, 3] <- origdata$Mail_SE[origdata$studynum == i]
}
}
However, you can avoid the studynum filters by using by
, the method that subsets a dataframe by factor(s) and runs subsets into an operation. 但是,可以通过使用
by
来避免studynum过滤器,该方法可以按因子对数据帧进行子集化,然后将子集运行到一个运算中。 Then after processing, row bind all subsetted dataframes with do.call
: 然后在处理之后,将所有子集的数据帧与
do.call
行绑定:
df_list <- by(origdata, origdata$studynum, function(sub){
coef_data <- coef(summary(rma(yi=Mail_b, sei=Mail_SE, method="FE", data=origdata)))
if(nrow(sub) > 1) {
df <- data.frame(studynum = sub$studynum[[1]],
Meta_b = coef_data$estimate,
Meta_SE = (coef_data$$ci.ub - coef_data$estimate) / 1.96
} else {
df <- data.frame(studynum = sub$studynum[[1]],
Meta_b = sub$Mail_b,
Meta_SE = sub$Mail_SE)
}
return(df)
})
collapsedtest <- do.call(rbind, df_list)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.