Guest User

Untitled

a guest
Jan 20th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. # load library
  2. library(tidyverse)
  3.  
  4. # Function to create probability distribution of survival data (PFS)
  5. prob_dist <- function(surv, lcl, ucl, ci = 0.95, arm) {
  6.  
  7. lci <- log(-log(lcl))
  8. uci <- log(-log(ucl))
  9. lsurv <- log(-log(surv))
  10.  
  11. z <- qnorm((1 + ci)/2)
  12. lsd <- abs(lsurv - lci)/z
  13. usd <- abs(uci - lsurv)/z
  14.  
  15. sd <- (lsd + usd)/2
  16. xs <- log(-log(seq(from = 0, to = 1, length.out = 1000)))
  17.  
  18. samp <- dnorm(x = xs, mean = lsurv, sd = sd)
  19.  
  20. tibble::tibble(arm = arm, xs = exp(-exp(xs)), samp = samp)
  21. }
  22.  
  23. # Function to calculate vector of events (no or yes) for a particular survival value
  24. sim_event <- function(surv, lcl, ucl, ci = 0.95, N_sim = 1000) {
  25.  
  26. lci <- log(-log(lcl))
  27. uci <- log(-log(ucl))
  28. lsurv <- log(-log(surv))
  29.  
  30. z <- qnorm((1 + ci)/2)
  31. lsd <- abs(lsurv - lci)/z
  32. usd <- abs(uci - lsurv)/z
  33.  
  34. sd <- (lsd + usd)/2
  35.  
  36. pop_surv <- exp(-exp(rnorm(N_sim, mean = lsurv, sd = sd)))
  37.  
  38. events <- vector("numeric", N_sim)
  39.  
  40. for (i in seq_len(N_sim)) {
  41. events[i] <- sample(c(0, 1), size = 1, replace = TRUE,
  42. prob = c(pop_surv[i], 1 - pop_surv[i]))
  43. }
  44.  
  45. events
  46. }
  47.  
  48. # Function which is the main simulation and calculates the proportion of times
  49. # Arm 1 and Arm 2 achieve any event
  50. simulation <- function(s1, lcl1, ucl1, s2, lcl2, ucl2, n_sim) {
  51.  
  52. foo <- function() {
  53. ev1 <- sim_event(s1, lcl1, ucl1, 0.95, 1000)
  54. ev2 <- sim_event(s2, lcl2, ucl2, 0.95, 1000)
  55.  
  56. c(prop_01 = mean(1*((ev1 == 0) & (ev2 == 1))),
  57. prop_10 = mean(1*((ev1 == 1) & (ev2 == 0))),
  58. prop_11 = mean(1*((ev1 == 1) & (ev2 == 1))),
  59. prop_00 = mean(1*((ev1 == 0) & (ev2 == 0))))
  60. }
  61.  
  62. rerun(n_sim, foo())
  63. }
  64.  
  65. # CODES TO CREATE THE PROBABILITY DISTRIBUTION GRAPH
  66. df1 <- prob_dist(surv = 0.867, lcl = 0.826, ucl = 0.899, arm = "Arm 1")
  67. df2 <- prob_dist(surv = 0.76, lcl = 0.71, ucl = 0.802, arm = "Arm 2")
  68.  
  69. df <- bind_rows(df1, df2)
  70.  
  71. ggplot(df, aes(x = xs, y = samp, colour = arm)) +
  72. geom_line(size = 1.5) + xlim(0.65, 1) + labs(x = "PFS", y = "density", colour = "Arm", title = "There is some overlap between the PFSs") + theme_bw()
  73.  
  74. # CODES TO CREATE THE TABLE OF PROPORTIONS AND GRAPH OF PROPORTIONS
  75.  
  76. ss <- simulation(0.867, 0.826, 0.899, 0.76, 0.71, 0.802, n_sim = 500)
  77.  
  78. df_prop <- tibble::as_tibble(do.call(rbind, ss))
  79. df_prop <- df_prop %>% gather("scenario", "proportion") %>% mutate(scenario =
  80. case_when(scenario == "prop_01" ~ "Arm1 event- Arm2 event+",
  81. scenario == "prop_00" ~ "Arm1 event- Arm2 event-",
  82. scenario == "prop_10" ~ "Arm1 event+ Arm2 event-",
  83. scenario == "prop_11" ~ "Arm1 event+ Arm2 event+"))
  84.  
  85. knitr::kable(df_prop %>% group_by(scenario) %>%
  86. summarise(mean = mean(proportion),
  87. sd = sd(proportion),
  88. minimum = min(proportion),
  89. maximum = max(proportion)), digits = 4)
  90.  
  91. ggplot(df_prop, aes(x = proportion, y = ..density.., fill = scenario)) +
  92. geom_density() + labs(x = "Proportion", y = "Density", colour = "Scenario",
  93. title = "Arm 1 is better than Arm 2 in only around 20% of times.\n
  94. In 80% of times, Arm 1 is either same as Arm 2 or\n worse than Arm 2") +
  95. theme_bw() + xlim(0, 0.9)
Add Comment
Please, Sign In to add comment