2013-01-10 54 views
0

我正在嘗試使用R計算非週期性現金流的IRR(內部收益率)(類似於名爲XIRR的Excel函數)。唯一的問題是它不起作用。我當然可以用在搞清楚一些幫助什麼是走錯了..使用R計算XIRR

R>cashflow <- c(275,275,275,275,275,275,275,275,275,275,275,275,416.833333333333,416.833333333333,416.833333333333,416.833333333333,416.833333333333,416.833333333333,416.833333333333,416.833333333333,416.833333333333,416.833333333333,416.833333333333,416.833333333333,452.666666666667,452.666666666667,452.666666666667,452.666666666667,452.666666666667,452.666666666667,452.666666666667,452.666666666667,452.666666666667,452.666666666667,452.666666666667,452.666666666667,534.583333333333,534.583333333333,534.583333333333,534.583333333333,534.583333333333,534.583333333333,534.583333333333,534.583333333333,534.583333333333,534.583333333333,534.583333333333,534.583333333333,-9246.969046) 
R>dates <- c("1997-01-31","1997-02-28","1997-03-31","1997-04-30","1997-05-31","1997-06-30","1997-07-31","1997-08-31","1997-09-30","1997-10-31","1997-11-30","1997-12-31","1998-01-31","1998-02-28","1998-03-31","1998-04-30","1998-05-31","1998-06-30","1998-07-31","1998-08-31","1998-09-30","1998-10-31","1998-11-30","1998-12-31","1999-01-31","1999-02-28","1999-03-31","1999-04-30","1999-05-31","1999-06-30","1999-07-31","1999-08-31","1999-09-30","1999-10-31","1999-11-30","1999-12-31","2000-01-31","2000-02-29","2000-03-31","2000-04-30","2000-05-31","2000-06-30","2000-07-31","2000-08-31","2000-09-30","2000-10-31","2000-11-30","2000-12-31","2001-01-31") 

R>irr <- xirr(cashflow, dates) 

它拋出的錯誤:

Error in while (pchg >= tol & abs(fval) > tol & iter <= itmax) { : missing value where TRUE/FALSE needed

這裏的功能:

xirr <- function(cf, dates) { 

# Secant method. 
secant <- function(par, fn, tol=1.e-07, itmax = 100, trace=FALSE, ...) { 
    # par = a starting vector with 2 starting values 
    # fn = a function whose first argument is the variable of interest 
    if (length(par) != 2) stop("You must specify a starting parameter vector of length 2") 
    p.2 <- par[1] 
    p.1 <- par[2] 
    f <- rep(NA, length(par)) 
    f[1] <- fn(p.1, ...) 
    f[2] <- fn(p.2, ...) 
    iter <- 1 
    pchg <- abs(p.2 - p.1) 
    fval <- f[2] 
    if (trace) cat("par: ", par, "fval: ", f, "\n") 
    while (pchg >= tol & abs(fval) > tol & iter <= itmax) { 
     p.new <- p.2 - (p.2 - p.1) * f[2]/(f[2] - f[1]) 
     pchg <- abs(p.new - p.2) 
     fval <- fn(p.new, ...) 
     p.1 <- p.2 
     p.2 <- p.new 
     f[1] <- f[2] 
     f[2] <- fval 
     iter <- iter + 1 
     if (trace) cat("par: ", p.new, "fval: ", fval, "\n") 
    } 
    list(par = p.new, value = fval, iter=iter) 
} 

# Net present value. 
npv <- function(irr, cashflow, times) sum(cashflow/(1 + irr)^times) 

times <- as.numeric(difftime(dates, dates[1], units="days"))/365.24 

r <- secant(par=c(0,0.1), fn=npv, cashflow=cf, times=times) 

return(r$par) 
} 
+3

在這種情況下,嘗試單步調試你的函數(這是調試的基本技巧)。經過56次迭代後,'p.2 == - Inf'使得'fval == NaN',並且R不能爲你的條件'abs(fval)> tol'指定一個真值。 –

回答

0

都讓我感謝你作出這一職位,因爲我一直在尋找這樣一個功能相當長的一段第一。 仍然你的功能不能控制NaN。你應該在while循環改變表達式[fval]

fval <- ifelse(is.na(fn(p.new, ...)),1,fn(p.new, ...)) 

希望它能幫助。