在purrr coxph结果之后,将子组添加到CSV输出文件-R版本4.1.2(2021-11-01)
下面的代码通过子组分配数据框,并将结果打印到offonentiation之后的CSV文件。我想将子组名称添加为最后一列,但不确定该怎么做。任何帮助将不胜感激。虚构的示例数据代码如下。
library(survival)
library(purrr)
mydata <- read.table(header=T,
text="age Sex survival out_stroke out_cancer
out_respiratory id tstart tstop region
51 1 1.419178082 2 1 1 1 0 50 1
60 2 5 1 2 2 2 0 50 1
49 1 1.082191781 2 2 2 3 0 50 2
83 2 0.038356164 1 1 2 4 0 50 2
68 1 0.77260274 2 1 2 5 0 50 1
30 2 -0 2 1 2 6 50 0 2
44 1 2.336986301 1 2 1 7 0 100 1
76 2 1.271232877 1 2 2 8 0 100 2")
mydata$Sex<-ifelse(mydata$Sex==1, "Male", "Female")
mydata$Sex <- factor(mydata$Sex, levels = c("Female","Male"))
mydata$Sex = relevel(mydata$Sex, ref = "Female")
outcomes <- names(mydata[4:6])
cov <- c("region: ", "age: ")
cov_name<-c("region + age")
writeLines(c("OUTCOME COVARIATE HR LCI UCI SE NEVENT SUBGROUP"), "HR_new.csv")
lapply(split(mydata, mydata$Sex), function(y)
purrr::map(outcomes, function(x) {
f <- as.formula(paste("Surv(survival, event=", x, ") ~ ",cov_name))
model <- coxph(f, y)
model$call$formula <- f
s <- summary(model)
cat(paste0(substring(x,5,40),' ',cov, apply(s$coefficients, 1,
function(x) {
paste0(" ", round(exp(x[1]), 2),
' ', round(exp(x[1] - 1.96 * x[3]), 2),
' ', round(exp(x[1] + 1.96 * x[3]), 2),
" ", round((x[3]), 4)," ",
" ", summary(model)$nevent)}),
collapse = '\n'), '\n', sep = '', file = paste0('HR_new.csv'),
append = TRUE)
invisible(model)
})
)
但我想要以下内容,其中显示了最后一列中的子组
The code below splits the dataframe by subgroup and prints the results to a csv file after exponentiation. I would like to add the subgroup name as a final column but am not sure how to do that. Any help would be appreciated. Code for made up sample data is below.
library(survival)
library(purrr)
mydata <- read.table(header=T,
text="age Sex survival out_stroke out_cancer
out_respiratory id tstart tstop region
51 1 1.419178082 2 1 1 1 0 50 1
60 2 5 1 2 2 2 0 50 1
49 1 1.082191781 2 2 2 3 0 50 2
83 2 0.038356164 1 1 2 4 0 50 2
68 1 0.77260274 2 1 2 5 0 50 1
30 2 -0 2 1 2 6 50 0 2
44 1 2.336986301 1 2 1 7 0 100 1
76 2 1.271232877 1 2 2 8 0 100 2")
mydata$Sex<-ifelse(mydata$Sex==1, "Male", "Female")
mydata$Sex <- factor(mydata$Sex, levels = c("Female","Male"))
mydata$Sex = relevel(mydata$Sex, ref = "Female")
outcomes <- names(mydata[4:6])
cov <- c("region: ", "age: ")
cov_name<-c("region + age")
writeLines(c("OUTCOME COVARIATE HR LCI UCI SE NEVENT SUBGROUP"), "HR_new.csv")
lapply(split(mydata, mydata$Sex), function(y)
purrr::map(outcomes, function(x) {
f <- as.formula(paste("Surv(survival, event=", x, ") ~ ",cov_name))
model <- coxph(f, y)
model$call$formula <- f
s <- summary(model)
cat(paste0(substring(x,5,40),' ',cov, apply(s$coefficients, 1,
function(x) {
paste0(" ", round(exp(x[1]), 2),
' ', round(exp(x[1] - 1.96 * x[3]), 2),
' ', round(exp(x[1] + 1.96 * x[3]), 2),
" ", round((x[3]), 4)," ",
" ", summary(model)$nevent)}),
collapse = '\n'), '\n', sep = '', file = paste0('HR_new.csv'),
append = TRUE)
invisible(model)
})
)
But I would like the following which shows the subgroup in the last column
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)