I have this function which calculates internal rates of returns for non-periodic cashflows..
xirr <- function(cf, t) {
npv <- function(cf, t, r) sum(cf/((r)^(t/365.25)))
irr <- uniroot(f=npv, interval=c(0,10), cf=cf, t=t, maxiter=100)$root - 1
return(irr)
}
The problem is, the mathematical assumptions of this formula are not satisfied if the return on the cashflows are less than -100%. I need the function to NOT STOP (throw an error) even if these assumptions aren't fulfilled. I need it to return 0 or something when this happens. At present..
mycashflow <- c(2000, 2000, 3000,4000, -10000)
mydates <-as.integer( c(0,101,200,300,400))
xirr(mycashflow, mydates)
..it throws the error :
Error in uniroot(f = npv, interval = c(0, 10), cf = cf, t = t, maxiter = 100) :
f.lower = f(lower) is NA