等额本息和等额本金还款在R中的实现

等额本金还款

library(tidyverse)
Eqprincipal <- function(principal, r, Loanterm){
  n <- Loanterm * 12
  r <- r / 12
  Prepay <- rep(principal / n, n)
  Pint <- rep(NA, n)
  Pint[1] <- principal * r
  RepayPrint <- rep(NA, n)
  RepayPrint[1] <- Prepay[1] + Pint[1]
  Resipr <- rep(NA, n)
  Resipr[1] <- principal - Prepay[1]
for (i in c(2:n)){
  Pint[i] <- Resipr[i - 1] * r
  RepayPrint[i] <- Prepay[i] + Pint[i]
  Resipr[i] <- principal - i * Prepay[i]
}
  result <- tibble('每月偿还本金' = Prepay, 
                   '偿还利息' = Pint,
                   '偿还本息' = RepayPrint,
                   '剩余本金' = Resipr)
  return(result)
}
a <- Eqprincipal(1000000, 0.049, 20)
# 每月偿还本金 偿还利息 偿还本息 剩余本金
# <dbl>    <dbl>    <dbl>    <dbl>
#   1        4167.    4083.    8250   995833.
# 2        4167.    4066.    8233.  991667.
# 3        4167.    4049.    8216.  987500 
# 4        4167.    4032.    8199.  983333.
# 5        4167.    4015.    8182.  979167.
# 6        4167.    3998.    8165.  975000 
# 7        4167.    3981.    8148.  970833.
# 8        4167.    3964.    8131.  966667.
# 9        4167.    3947.    8114.  962500 
# 10        4167.    3930.    8097.  958333.
# # ... with 230 more rows

等额本息还款

Eqprint <- function(principal, r, Loanterm){
  n <- Loanterm * 12
  r <- r / 12
  Pint <- rep(principal * r, n)
  Pint[1] <- principal * r
  RepayPrint <- rep(principal * r * (1 + r)^n / ((1 + r)^n - 1), n)
  Prepay <- rep(NA, n)
  Prepay[1] <- RepayPrint[1] - Pint[1]
  Resipr <- rep(NA, n)
  Resipr[1] <- principal - Prepay[1]
  for (i in c(2:n)){
    Pint[i] <- Resipr[i - 1] * r
    Prepay[i] <- RepayPrint[i] - Pint[i]
    Resipr[i] <- Resipr[i - 1] - Prepay[i]
  }
  result <- tibble('每月偿还本金' = Prepay, 
                   '偿还利息' = Pint,
                   '偿还本息' = RepayPrint,
                   '剩余本金' = Resipr)
  return(result)
}
b <- Eqprint(1000000, 0.049, 20)
# 每月偿还本金 偿还利息 偿还本息 剩余本金
# <dbl>    <dbl>    <dbl>    <dbl>
#   1        2461.    4083.    6544.  997539.
# 2        2471.    4073.    6544.  995068.
# 3        2481.    4063.    6544.  992586.
# 4        2491.    4053.    6544.  990095.
# 5        2502.    4043.    6544.  987594.
# 6        2512.    4033.    6544.  985082.
# 7        2522.    4022.    6544.  982560.
# 8        2532.    4012.    6544.  980027.
# 9        2543.    4002.    6544.  977485.
# 10        2553.    3991.    6544.  974932.
# # ... with 230 more rows

两种还款方式的利息差额计算

chae <- b$偿还利息 - a$偿还利息 
chae1 <- tibble(chae)
# # A tibble: 240 x 1
# chae
# <dbl>
# 1  0   
# 2  6.96
# 3 13.9 
# 4 20.8 
# 5 27.6 
# 6 34.4 
# 7 41.2 
# 8 47.9 
# 9 54.6 
# 10 61.2 
# # ... with 230 more rows
sum(chae)
# [1] 78624.05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值