time-dependent-no-tunnel-states.R 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # We will use cycles from 0 to 100
  2. cycles <- 0:100
  3. # Create an array for the state membership
  4. state_membership <- array(NA_real_, c(length(cycles), 3), list(cycle = cycles, state = c("Healthy", "Diseased", "Dead")))
  5. state_membership[1, ] <- c(1, 0, 0)
  6. # Define the survival model parameters
  7. # - Healthy to Diseased
  8. lambda_inc <- 2e-5
  9. gamma_inc <- 2.5
  10. # - Healthy to Dead
  11. a_general <- 0.2
  12. b_general <- 1e-7
  13. # - Diseased to Dead
  14. lambda_surv <- 0.2
  15. gamma_surv <- 0.5
  16. # Calculate exponential distribution with same mean survival as Weibull
  17. # distribution for Diseased to Dead
  18. mean_surv <- lambda_surv ^ (-1 / gamma_surv) * gamma(1 + 1 / gamma_surv)
  19. rate_surv <- 1 / mean_surv
  20. # Calculate the hazard rates for the transitions out of the Healthy state
  21. healthy_haz <- array(NA_real_, c(length(cycles), 2), list(cycle = cycles, to = c("Diseased", "Dead")))
  22. healthy_haz[, 1] <- lambda_inc * ((cycles + 1) ^ gamma_inc - cycles ^ gamma_inc)
  23. healthy_haz[, 2] <- b_general / a_general * (exp(a_general * (cycles + 1)) - exp(a_general * cycles))
  24. # Create an array for transition probabilities in each cycle
  25. transition_probs <- array(NA_real_, c(length(cycles), 3), list(cycle = cycles, transition = c("Healthy->Diseased", "Healthy->Dead", "Diseased->Dead")))
  26. transition_probs[, 1:2] <- healthy_haz / rowSums(healthy_haz) * (1 - exp(-rowSums(healthy_haz)))
  27. transition_probs[, 3] <- 1 - exp(-rate_surv)
  28. # Run cohort simulation
  29. for (i in 2:length(cycles)) {
  30. # Update state membership
  31. state_membership[i, 1] <- state_membership[i - 1, 1] * (1 - sum(transition_probs[i - 1, 1:2]))
  32. state_membership[i, 2] <- state_membership[i - 1, 1] * transition_probs[i - 1, 1] +
  33. state_membership[i - 1, 2] * (1 - transition_probs[i - 1, 3])
  34. state_membership[i, 3] <- state_membership[i - 1, 1] * transition_probs[i - 1, 2] +
  35. state_membership[i - 1, 2] * transition_probs[i - 1, 3] +
  36. state_membership[i - 1, 3]
  37. }