Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. # Pt.1 create df1 - all mortality ratios over time
  2. #======================================================
  3. set.seed(5)
  4. df1 <- data.frame(matrix(ncol=7,nrow=300))
  5. colnames(df1) <- c("Trt","Day","PercentSurv","Status","V1","V2","V3")
  6. df1$Trt <- rep(1:50, each=6)
  7. df1$Day <- rep(c(0,2,4,6,8,10), times=50)
  8. for(i in df1$Trt){
  9. df1$PercentSurv[df1$Trt %in% c(i)] <-
  10. c("100",sort(sample(1:100,size=5),decreasing=TRUE))
  11. df1$V1[df1$Trt %in% c(i)] <- sample(12:30,size=1)
  12. df1$V2[df1$Trt %in% c(i)] <- sample(10:40,size=1)
  13. df1$V3[df1$Trt %in% c(i)] <- sample(1:5,size=1)
  14. }
  15. df1$PercentSurv <- as.numeric(df1$PercentSurv)
  16. df1$Status[df1$PercentSurv > 15] <- as.numeric(1)
  17. df1$Status[df1$PercentSurv <=15] <-as.numeric(2) '
  18.  
  19.  
  20.  
  21.  
  22. # Pt.2 create df2 - time-to-event with variables only
  23. #==========================================================
  24. df2 <- data.frame(matrix(ncol = 6,nrow=50))
  25. colnames(df2) <- c("Trt", "Time", "Status","V1","V2","V3")
  26. df2$Trt <- c(1:50)
  27. for(i in df2$Trt){
  28. df2$Status[df2$Trt %in% c(i)] <- ifelse( min( df1$PercentSurv[df1$Trt
  29. %in% c(i)]) <= 15,2,1)
  30. df2$Time[df2$Trt %in% c(i)] <- ifelse(df2$Status[df2$Trt %in%
  31. c(i)]=="2", df1$Day[df1$Trt %in% c(i)]
  32. [min(which(df1$PercentSurv[df1$Trt %in% c(i)]<=15))],10)
  33. df2$V1[df2$Trt %in% c(i)] <- min(df1$V1[df1$Trt %in% c(i)])
  34. df2$V2[df2$Trt %in% c(i)] <- min(df1$V2[df1$Trt %in% c(i)])
  35. df2$V3[df2$Trt %in% c(i)] <- min(df1$V3[df1$Trt %in% c(i)])
  36. }
  37. df2$Time <- as.numeric(df2$Time)
  38.  
  39.  
  40. # Pt.3 use survival analysis on variables in df2 - cox test
  41. #=========================================================
  42.  
  43. install.packages("survival")
  44. install.packages("survminer")
  45. library("survival")
  46. library("survminer")
  47.  
  48. coxmodel <- coxph(Surv(Time, Status) ~ V1 + V2 + V3, data=df2)
  49. summary(coxmodel)
  50. ggsurvplot(survfit(coxmodel),data=df2, color = "#2E9FDF",
  51. ggtheme = theme_minimal())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement