library(survival)
library(randomForestSRC)
# 生成模拟数据
set.seed(123)
n <- 200
time <- rexp(n, rate = 0.1)
status <- rbinom(n, size = 1, prob = 0.7)
var1 <- rnorm(n)
var2 <- rnorm(n)
var3 <- rnorm(n)
data1 <- data.frame(time = time, status = status, var1 = var1, var2 = var2, var3 = var3)
# 定义模型列表
models <- list(
cox = function(data) {
fit <- survival::coxph(Surv(time,status) ~ .,data=data)
sum<-summary(fit)[["coefficients"]][,5]
canshu<-names(sum)
result<-list(fit=fit,canshu=canshu)
return(result)
},
rsf=function(data){
fit<-rfsrc(Surv(time,status) ~ .,data=data1)
canshu<-var.select(object=fit,
method="md",
conservative="low")$md.obj$topvars.1se
result<-list(fit=fit,canshu=canshu)
return(result)
}
)
# 列举所有模型组合(考虑顺序)
model<-c("cox","rsf")
all_combinations <- list()
library(gtools)
for (n in 1:length(model)) {
permutations <- permutations(2,n,v=model)
mat_list <- apply(permutations, 1, function(row) paste(row, collapse = ","))
mat_vector_list <- lapply(mat_list, function(str) unlist(strsplit(str, ",")))
all_combinations <- c(all_combinations, mat_vector_list)
}
model_combinations<-all_combinations
# 循环遍历不同模型组合
selected_vars_final <- list()
for (i in 1:length(model_combinations)) {
comb <- model_combinations[[i]]
selected_vars <- NULL
# 循环遍历每个模型类型
data1<-lung
for (model_name in comb) {
i=1
if (grep(model_name,comb)==1) {
# 根据前一步的选择变量建立模型并筛选变量
result <- models[[model_name]](data1)
cat("第一步:",model_name,"---",result$canshu,"\n")
} else {
vc=paste("c(", paste(sprintf('"%s"', selected_vars), collapse = ","), ")", sep = "")
cat("纳入第二步的因素:",model_name,"---",vc,"\n")
selected_data <- data.frame(data1[,eval(parse(text = vc))],
data1[,c("time","status")])
result <- models[[model_name]](selected_data)
}
# 更新选定变量
selected_vars <- result$canshu
}
selected_vars_final[[paste(comb, collapse = "_")]] <- selected_vars
}
print(selected_vars_final)
R语言生存分析算法的简单组合
weixin_493202632023-08-19 11:24
相关推荐
数据分析能量站16 小时前
目标检测-R-CNNmatlabgoodboy19 小时前
数据分析帮做spss数据代分析stata实证python统计R语言eviews处理biomooc1 天前
R 语言 | 绘图的文字格式(绘制上标、下标、斜体、文字标注等)Tiger Z3 天前
R 语言科研绘图第 6 期 --- 散点图-基础LvManBa3 天前
R 常用的内置软件包及功能介绍新知图书4 天前
R语言的数据结构-数据框新知图书5 天前
R语言的字符串操作新知图书6 天前
R语言函数简介还没想好1236 天前
mlr3机器学习AUC的置信区间提取拓端研究室TRL7 天前
R语言混合模型回归GBTM群组轨迹模型绘图可视化研究