Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Pt.1 create df1 - all mortality ratios over time
- #======================================================
- set.seed(5)
- df1 <- data.frame(matrix(ncol=7,nrow=300))
- colnames(df1) <- c("Trt","Day","PercentSurv","Status","V1","V2","V3")
- df1$Trt <- rep(1:50, each=6)
- df1$Day <- rep(c(0,2,4,6,8,10), times=50)
- for(i in df1$Trt){
- df1$PercentSurv[df1$Trt %in% c(i)] <-
- c("100",sort(sample(1:100,size=5),decreasing=TRUE))
- df1$V1[df1$Trt %in% c(i)] <- sample(12:30,size=1)
- df1$V2[df1$Trt %in% c(i)] <- sample(10:40,size=1)
- df1$V3[df1$Trt %in% c(i)] <- sample(1:5,size=1)
- }
- df1$PercentSurv <- as.numeric(df1$PercentSurv)
- df1$Status[df1$PercentSurv > 15] <- as.numeric(1)
- df1$Status[df1$PercentSurv <=15] <-as.numeric(2) '
- # Pt.2 create df2 - time-to-event with variables only
- #==========================================================
- df2 <- data.frame(matrix(ncol = 6,nrow=50))
- colnames(df2) <- c("Trt", "Time", "Status","V1","V2","V3")
- df2$Trt <- c(1:50)
- for(i in df2$Trt){
- df2$Status[df2$Trt %in% c(i)] <- ifelse( min( df1$PercentSurv[df1$Trt
- %in% c(i)]) <= 15,2,1)
- df2$Time[df2$Trt %in% c(i)] <- ifelse(df2$Status[df2$Trt %in%
- c(i)]=="2", df1$Day[df1$Trt %in% c(i)]
- [min(which(df1$PercentSurv[df1$Trt %in% c(i)]<=15))],10)
- df2$V1[df2$Trt %in% c(i)] <- min(df1$V1[df1$Trt %in% c(i)])
- df2$V2[df2$Trt %in% c(i)] <- min(df1$V2[df1$Trt %in% c(i)])
- df2$V3[df2$Trt %in% c(i)] <- min(df1$V3[df1$Trt %in% c(i)])
- }
- df2$Time <- as.numeric(df2$Time)
- # Pt.3 use survival analysis on variables in df2 - cox test
- #=========================================================
- install.packages("survival")
- install.packages("survminer")
- library("survival")
- library("survminer")
- coxmodel <- coxph(Surv(Time, Status) ~ V1 + V2 + V3, data=df2)
- summary(coxmodel)
- ggsurvplot(survfit(coxmodel),data=df2, color = "#2E9FDF",
- ggtheme = theme_minimal())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement