【问题标题】:Compute interest rate of monthly mortgage payments using R使用 R 计算每月抵押贷款的利率
【发布时间】:2019-04-01 19:57:10
【问题描述】:

我尝试在给定总抵押贷款、期限和每月付款的情况下计算年金利率。

每月年金计算公式:

              i
J = T * --------------
        1 - (1 + i)^-n

其中 T 是总抵押贷款,i 是每月利息,J 是年金

例子:

library(purrr)
library(dplyr)

mortgage   <- 300000
#annualRate <- ??
duration   <- 360
#annuity
ann <- 1108.86

#Use brute force to find the interest rate
annuity <- function(annualRate, mortgage, duration) {
    monthRate <- annualRate / 12
    ann <- (monthRate / (1- ((1+monthRate)^(-duration)) )) * mortgage
    data.frame(rate = annualRate*100, ann = ann)
}

#Try some rates
checkRates <- seq(1,3,0.5)/100
res <- map(checkRates , annuity, mortgage, duration) %>% bind_rows()
res

#  rate       ann
#1  1.0  964.9186
#2  1.5 1035.3606
#3  2.0 1108.8584
#4  2.5 1185.3627
#5  3.0 1264.8121

使用这种蛮力技术的兴趣是2.0%

检查解决方案:

annualRate <- 2/100

monthRate <- annualRate / 12
(monthRate / (1- ((1+monthRate)^(-duration)) )) * mortgage
#[1] 1108.858

但是,我想知道是否可以在不使用蛮力的情况下计算 R 中的确切利率。

# 1108.86          i
# ------- =  --------------
# 300000     1 - (1 + i)^-360

是否可以使用 R 解这个方程? 任何建议表示赞赏。

【问题讨论】:

    标签: r math annuity


    【解决方案1】:

    我想你正在寻找uniroot。这是您的问题的示例用法:

    mortgage   <- 300000
    duration   <- 360
    ann <- 1108.86
    uniroot(function(x) mortgage * x/12 / (1 - (1+x/12)^(-duration)) - ann,
        c(1e-6,1))$root * 100
    #[1] 2.000091
    

    【讨论】:

    • 谢谢,这就是我要找的。​​span>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多