Search code examples
rsurvival

Calculate expected number of survivors over time with a known death rate


With a total number of N, a daily death rate: p1. I would like to calculate the number of survivors after certain days.

day1 : N - N*p1 
day2 : (N - N*p1) -  (N - N*p1)*p1
day3 : ((N - N*p1) -  (N - N*p1)*p1) - ((N - N*p1) -  (N - N*p1)*p1)*p1
...

This is what I have done so far to start to get the number of day 1. Suggestions would be appreciated.

df <- data.frame(day = c(1:30))  
N <- 1000
p1 <- 0.06
apply(df,1, function(x) N-N*p1) 

Solution

  • Basically, you can use the compound interest equation.

    foo = function(N, p1, d){
        for (i in 1:d){
            N = N - N * p1
        }
        N
    }
    
    foo(1000, 0.06, 30)
    

    OR

    bar = function(N, p1, d){
        N * (1 - p1)^d
    }
    
    bar(1000, 0.06, 1:30)
    

    OR if you just want a plot

    curve(1000 * (1 - 0.06) ^ x, 0, 30, n = 31)