Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

cal.LOAN <- function(P=360000, I=4.

25, L=35, amort=T, plotData=T) {


J <- I/(12 * 100)
N <- 12 * L
M <- P*J/(1-(1+J)^(-N))
monthPay <<- M

if(amort==T) {
Pt <- P # current principal or amount of the loan
currP <- NULL
while(Pt>=0) {
H <- Pt * J # current monthly interest
C <- M - H # the amount of principal payed for that month
Q <- Pt - C # new balance of your principal of your loan
Pt <- Q # goes back to step 1. The loop continues until the p goes to zero
currP <- c(currP, Pt)
}
monthP <- c(P, currP[1:(length(currP)-1)])-currP
aDFmonth <<- data.frame(
Amortization=c(P, currP[1:(length(currP)-1)]),
Monthly_Payment=monthP+c((monthPay-monthP)[1:(length(monthP)-1)],0),
Monthly_Principal=monthP,
Monthly_Interest=c((monthPay-monthP)[1:(length(monthP)-1)],0),
Year=sort(rep(1:ceiling(N/12), 12))[1:length(monthP)]
)
aDFyear <- data.frame(
Amortization=tapply(aDFmonth$Amortization, aDFmonth$Year, max),
Annual_Payment=tapply(aDFmonth$Monthly_Payment, aDFmonth$Year, sum),
Annual_Principal=tapply(aDFmonth$Monthly_Principal, aDFmonth$Year, sum),
Annual_Interest=tapply(aDFmonth$Monthly_Interest, aDFmonth$Year, sum),
Year=as.vector(na.omit(unique(aDFmonth$Year)))
)
aDFyear <<- aDFyear
}
if(plotData==T) {
barplot(t(aDFyear[,c(3,4)]),
col=c("blue", "red"),
main="Annual Interest and Principal Payments",
sub="The data for this plot is stored in aDFyear.",
xlab="Years", ylab="$ Amount",
legend.text=c("Principal", "Interest"),
ylim=c(0, max(aDFyear$Annual_Payment)*1.3))
}
print(monthPay)
print(aDFyear)
}
cal.LOAN(P=360000, I=4.25, L=35)
[1] 1648.418
Amortization Annual_Payment Annual_Principal Annual_Interest Year
1 360000.00 19781.02 4569.346 15211.6747 1
2 355430.65 19781.02 4767.371 15013.6497 2
3 350663.28 19781.02 4973.978 14807.0427 3
4 345689.31 19781.02 5189.539 14591.4819 4
5 340499.77 19781.02 5414.441 14366.5791 5
6 335085.33 19781.02 5649.091 14131.9296 6
7 329436.23 19781.02 5893.910 13887.1109 7
8 323542.32 19781.02 6149.338 13631.6823 8
9 317392.99 19781.02 6415.837 13365.1841 9
10 310977.15 19781.02 6693.884 13087.1363 10
11 304283.27 19781.02 6983.982 12797.0387 11
12 297299.28 19781.02 7286.652 12494.3689 12
13 290012.63 19781.02 7602.439 12178.5820 13
14 282410.19 19781.02 7931.911 11849.1097 14
15 274478.28 19781.02 8275.662 11505.3588 15
16 266202.62 19781.02 8634.310 11146.7105 16
17 257568.31 19781.02 9008.501 10772.5192 17
18 248559.81 19781.02 9398.909 10382.1113 18
19 239160.90 19781.02 9806.237 9974.7841 19
20 229354.66 19781.02 10231.216 9549.8042 20
21 219123.45 19781.02 10674.614 9106.4066 21
22 208448.83 19781.02 11137.227 8643.7932 22
23 197311.61 19781.02 11619.889 8161.1312 23
24 185691.72 19781.02 12123.469 7657.5518 24
25 173568.25 19781.02 12648.872 7132.1483 25
26 160919.37 19781.02 13197.046 6583.9751 26
27 147722.33 19781.02 13768.975 6012.0453 27
28 133953.35 19781.02 14365.691 5415.3293 28
29 119587.66 19781.02 14988.268 4792.7531 29
30 104599.40 19781.02 15637.825 4143.1958 30
31 88961.57 19781.02 16315.532 3465.4882 31
32 72646.04 19781.02 17022.610 2758.4102 32
33 55623.43 19781.02 17760.331 2020.6892 33
34 37863.10 19781.02 18530.024 1250.9970 34
35 19333.07 19781.02 19333.073 447.9481 35

You might also like