R 语言贷款月供数据分析

#================================================================
#----------------------------------------------------------------
# 步骤 0:为各个变量赋值
# P: 本金
# m: 还款的总月份
# i: 年利率
# s: 月利率

P=25000
i=0.05
m=120
s=i/12

# step 1: 计算月份向量

t=1:m

# step 2: 计算月还款额

monthly.payment = (1+s)^m*s*P/((1+s)^m-1)

# step 3: 计算每个月应该偿还的本金额度向量

principal.paid.money.t = (1+s)^(t-1)*s*P/((1+s)^m-1)

# step 4:  计算每月未偿还的本金额度向量

principal.remaining= P*(1-((1+s)^t-1)/((1+s)^m-1))


# step 5: 用每月还款额扣除每月应偿还的本金额度向量,计算每个月还的利息

interest.paid.month.t = monthly.payment - principal.paid.money.t

# step 6: 将每个月所付出的利息累加,计算总利息

total.interest.paid=sum(interest.paid.month.t)

# step 7: 在控制台显示计算结果

monthly.payment
total.interest.paid
t
principal.paid.money.t
interest.paid.month.t
principal.remaining

# step 8: 以月份向量为横轴,以未偿还的本金额度向量为纵轴来绘图

plot(t,principal.remaining,type="l")

选自 《R 语言初学指南》

猜你喜欢

转载自www.cnblogs.com/reycg-blog/p/9013201.html