Advertisement
Guest User

Untitled

a guest
Jun 7th, 2018
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. p = .06
  2. n_events = 40
  3. t_max = 1000
  4. n_pop = 1e3
  5. t_death = matrix(nrow = n_pop, ncol = 1)
  6. for(i in 1:n_pop){
  7. dat = replicate(n_events, sample(0:1, t_max, replace = T, prob = c(1-p, p)))
  8. t_death[i] = which(apply(apply(dat, 2, function(x) cumsum(x) > 0), 1, all))[1]
  9. }
  10.  
  11. hist(t_death, breaks = seq(0, max(t_death) + 5, by = 5),
  12. freq = F, col = "Grey", xlab = "Age",
  13. main = c("Age at Death", paste0("Mean = ", mean(t_death))))
  14.  
  15. lines(2:max(t_death), diff((1 - (1 - p)^(1:max(t_death)))^n_events), lwd = 3, col = "Red")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement