I am trying to replace a spreadsheet model of a project justification with an R script. I can't seem to track down R functions to calculate Internal Rate of Return and NPV? Am I missing something? NPV doesn't seem so difficult to calculate (at least for a regular series) but I am struggling to identify how to solve for IRR in R. It would be sufficient if it worked for a regular series but really useful if there was something that worked with irregular time series. cheers
Hello,
If we define IRR implicitly such that NPV(C, IRR) = 0, then we can just
write an IRR function that finds the zeros of the NPV function. Here are
two such functions:
NPV <- function(C, r) {
sum(C / (1 + r) ^ (seq(along = C) - 1))
}
IRR <- function(C) {
uniroot(NPV, c(0, 1), C = C)$root
}
Hope this helps,
Robert
-----Original Message-----
From: paul sorenson [mailto:sourceforge at metrak.com]
Sent: Wednesday, November 30, 2005 3:50 PM
To: r-help
Subject: [R] calculating IRR (accounting) in R
I am trying to replace a spreadsheet model of a project justification
with an R script.
I can't seem to track down R functions to calculate Internal Rate of
Return and NPV? Am I missing something? NPV doesn't seem so difficult
to calculate (at least for a regular series) but I am struggling to
identify how to solve for IRR in R.
It would be sufficient if it worked for a regular series but really
useful if there was something that worked with irregular time series.
cheers
______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
You can use uniroot: npv <- function(i, cf, tt = seq(along = cf)) sum(cf / (1-i)^tt) npv0 <- npv(0.1, 1:10) npv0 f <- function(i, cf, npv0) npv0 - npv(i, cf) uniroot(f, c(0.01, .99), cf = 1:10, npv0 = npv0) On 11/30/05, paul sorenson <sourceforge at metrak.com> wrote:> I am trying to replace a spreadsheet model of a project justification > with an R script. > > I can't seem to track down R functions to calculate Internal Rate of > Return and NPV? Am I missing something? NPV doesn't seem so difficult > to calculate (at least for a regular series) but I am struggling to > identify how to solve for IRR in R. > > It would be sufficient if it worked for a regular series but really > useful if there was something that worked with irregular time series. > > cheers > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
paul sorenson wrote:> I can't seem to track down R functions to calculate Internal Rate of > Return and NPV?Thanks for the answers people. Comparing the answers with what Excel pops out shows just how assumptions can vary. In particular whether the first payment is at T0 or T1. cheers -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: irr.R Url: https://stat.ethz.ch/pipermail/r-help/attachments/20051201/39ccbb00/irr.pl