Computing iterations in R to calculate the rate of population increase

97 views Asked by At

I´ve tried to calculate the rate of population increase, denoted as r, which is obtained from:

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

I know the values of x, lx, and mx, but r value should be iteratively obtained to get a sum of one. This is the code I wrote (or tried to), which it´s not correct as it returns values for the sum but not for r. I don´t know what´s wrong. I´ll appreciate any clue to solve this. Thanks.

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 ()} }
1

There are 1 answers

0
bergant On

Try this:

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

> [1] -0.8340894