Computing Iterations in R to Calculate Population Growth Rates

I tried to calculate the population growth rate, denoted as r, which is obtained from:

 sum(e^(r*x)*lx*mx) = 1

      

I know the values ​​of x, lx and mx, but the value of r has to be iteratively retrieved to get the sum of one. This is the code I wrote (or tried), which is wrong as it returns values ​​for the sum but not for r. I do not know what happened. I would be grateful for a solution to this problem. Thank you.

lx <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0)
mx <- c(1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0)
x <- c(1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
            18, 19, 20, 21, 22)

radj = sum((exp(-r*x))*lx*mx)

for (radj in 0:1) {
  repeat { radj <- sum((exp(-r*x))*lx*mx) 
           print(radj)
           if (radj < 1) break ()} }

      

+3


source to share


1 answer


Try the following:



root <- uniroot( f = function(r) sum(exp(r*x)*lx*mx) - 1, interval = c(-1, 0))
root$root

> [1] -0.8340894

      

+5


source







All Articles