2014-02-13 61 views
0

内部收益率(IRR)或经济收益率(ERR)是用于衡量和比较投资收益率的资本预算的回报率。R和MS Excel之间IRR计算的不同结果

我写了一些R代码里面来计算回报率(IRR)内部收益率是这样的:

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不同在MS Excel中。

例如,您从银行借3600,管理费是0.006*3600,等于24个月的本金分期付款,所以每个月您都必须支付3600*0.006+3600/24=171.6

您发生的费用是每月cal_irr(3600,0.006,240) = 0.01104071,但在Excel中,我得到了1.1054657%。我的R代码有什么问题?

enter image description here

回答

3

您unirooting找到小数字,这可能会导致耐受性的问题。尝试:

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 
0

如果你有一个CF,看起来像这样:

> cal_cash <- function(amount,fee,duration) c(amount,rep(-1*(amount*fee+amount/duration),duration)) 
> cal_cash(3600,0.006,24) 
[1] 3600.0 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 
[20] -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 

然后可以很容易地使用financial包来计算IRR:

> require(financial) 
> cf(cal_cash(3600,0.006,24))$irr 
[1] -185.755352 1.105466