提问人:nicholaspooran 提问时间:10/12/2023 最后编辑:nicholaspooran 更新时间:10/13/2023 访问量:93
如何在单变量分析中有效地循环变量?
How to effectively loop variables in univariate analysis?
问:
我写了下面的代码来运行 cox 回归并将它们输出到表中。我想对许多变量运行单变量。你能帮我使用一个循环来让它更快吗?变量有 Grade、DCIS、LVE、Tsize 等......
我写的代码如下:
CoxphToDF <- function(y) {
stopifnot(class(y) == "summary.coxph")
cbind(y[["coefficients"]],
`lower 0.95` = y[["conf.int"]][, "lower .95"],
`upper 0.95` = y[["conf.int"]][, "upper .95"])
}
Grade <- data[!is.na(data$Grade) & data$Grade != "", ]
df1 = CoxphToDF(summary(coxph(Surv(OS, OS.Event) ~ Grade, data = Grade)))
DCIS <- data[!is.na(data$DCIS) & data$DCIS != "", ]
df2 = CoxphToDF(summary(coxph(Surv(OS, OS.Event) ~ DCIS, data = DCIS)))
LVE <- data[!is.na(data$LVE) & data$LVE != "", ]
df3 = CoxphToDF(summary(coxph(Surv(OS, OS.Event) ~ LVE, data = LVE)))
Tsize <- data[!is.na(data$Tsize) & data$Tsize != "", ]
df4 = CoxphToDF(summary(coxph(Surv(OS, OS.Event) ~ Tsize, data = Tsize)))
write.table(rbind(df1,df2,df3,df4), "univariate_results.txt", sep="\t", col.names = NA)
我尝试了以下方法:
#vector of variables
variables_to_test <- c("Grade", "DCIS", "LVE", "Tsize")
#list to store the results
results_list <- list()
# Iterate
for (variable in variables_to_test) {
subset_data <- data[!is.na(data[[variable]]) & data[[variable]] != "", ]
result <- CoxphToDF(summary(coxph(Surv(OS, OS.Event) ~ ., data = subset_data)))
results_list[[variable]] <- result
}
# single data frame
results_df <- do.call(rbind, results_list)
它给了我意想不到的结果,数据框中的所有变量都没有被选中,而且结果很奇怪
答:
2赞
Friede
10/12/2023
#1
你在找吗?lapply()
#vector of variables
variables_to_test <- c("Grade", "DCIS", "LVE", "Tsize")
do.call(what = "rbind",
args = lapply(X = variables_to_test,
FUN = \(x) {
data <- data[!is.na(data[[x]])
& data[[x]] != "", ]
CoxphToDF(summary(coxph(Surv(OS, OS.Event) ~ ., data = data)))
}
)
)
请注意,每个模型拟合都会发出警告
1: In coxph.fit(X, Y, istrat, offset, init, control, weights = weights, :
Ran out of iterations and did not converge
我看不出有什么优势。考虑将结果存储在列表中,即 .do.call(what = "rbind", args = something)
list_of_sth <- lapply(...
评论
apply()
lapply()
purrr