it_is_a_literature - 2 years ago 81

R Question

The internal rate of return (IRR) or economic rate of return (ERR) is a rate of return used in capital budgeting to measure and compare the profitability of investments.

I wrote some R code to calculate the internal rate of return (IRR) like this:

`cal_irr <- function(amount,fee,duration) {`

cash<-c(amount,rep(-1*(amount*fee+amount/duration),duration))

NPV<-function(r){sum(cash /((1 + r) ^ (seq(along.with = cash)-1)))}

return(uniroot(NPV, c(0, 1))$root)

}

`cal_irr`

`IRR`

For example, you borrow 3600 from the bank, the administrative fee is

`0.006*3600`

`3600*0.006+3600/24=171.6`

The cost you incur is

`cal_irr(3600,0.006,240) = 0.01104071`

`1.1054657%`

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

You are unirooting to find small numbers, which can cause issues with tolerance. Try:

```
cal_irr <- function(amount,fee,duration) {
cash<-c(amount,rep(-1*(amount*fee+amount/duration),duration))
NPV<-function(r){sum(cash /((1 + r) ^ (seq(along.with = cash)-1)))}
return(uniroot(NPV, c(0, 1), tol=.0000001)$root)}
cal_irr(3600,0.006,24)
# [1] 0.01105466
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**