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
can calculate Instalment Payments, but the annoying thing is that my result is different from the financial function IRR
in MS Excel.
For example, you borrow 3600 from the bank, the administrative fee is 0.006*3600
, equal principal instalments in 24 months, so every month you have to pay 3600*0.006+3600/24=171.6
.
The cost you incur is cal_irr(3600,0.006,240) = 0.01104071
per month, but in Excel I got 1.1054657%
. What is wrong with my R code?
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