############################################################################# ## Function to Calculate Monthly Mortgage Payments and Amortization Tables ## ############################################################################# # Author: Thomas Girke # Last update: Feb 27, 2007 # Utility: Calculates monthly and annual loan or mortgage payments, generates amortization tables and plots the results # How to run the script: # source("http://faculty.ucr.edu/~tgirke/Documents/R_BioCond/My_R_Scripts/mortgage.R") # Definitions: # P = principal, the initial amount of the loan # I = annual interest rate # L = length of the loan in years, or at least the length over which the loan is amortized. # J = monthly interest in decimal form = I / (12 x 100) # M = monthly payment; formula: M = P * ( J / (1 - (1 + J) ^ -N)) # N = number of months over which loan is amortized = L x 12 # see also: http://www.jeacle.ie/mortgage/instructions.html source("~/Dropbox/dev/R/!ScriptsR/basicWrappers.r") schoolLoan <- function(P, intrPercent=6, years=10, monthsWithOutPayments=18, fixed=FALSE) { if (fixed) { return(loan(P, intrPercent, years, monthsWithOutPayments, T, F)) } return(loanIncreasing(P, intrPercent, years, monthsWithOutPayments)) } loan <- function(P=500000, intrPercent=6, years=30, monthsWithOutPayments=0, amort=TRUE, plotData=FALSE) { # monthsWithOutPayments, used for student loans. Interest acrues, compounded monthly. # time for Amortization begins after that cat("\n\n") I <- intrPercent L <- years #for ease of notation J <- I/(12 * 100) P <- P * ((1+J) ^ monthsWithOutPayments) # for student loans cat("Principal when amortization begins is: ", asCurr(P, 0), "\n") N <- 12 * L M <- P*J/(1-(1+J)^(-N)) monthPay <<- M cat("\nThe payments for this loan are:\n Monthly payment: ", asCurr(M), " (stored in monthPay)\n Total cost: ", asCurr(M*N), "\n\n", sep="") # Calculate Amortization for each Month if(amort) { Pt <- P # current principal or amount of the loan currP <- NULL # vector tracking Principal after each payment while(Pt>=0) { H <- Pt * J # this is the current monthly interest C <- M - H # this is your monthly payment minus your monthly interest, so it is the amount of principal you pay for that month Pt <- Pt - C # this is the new balance of your principal of your loan currP <- c(currP, Pt) } # The loop continues until the value Q (and hence P) goes to zero 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 cat("The amortization data for each of the", N, "months are stored in \"aDFmonth\".\n\n") cat("The amortization data for each of the", L, "years are stored in \"aDFyear\".\n\n") } if(plotData) { 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)) } } loanIncreasing <- function(P=500000, intrPercent=6, years=30, monthsWithOutPayments=0) { # monthsWithOutPayments, used for student loans. Interest acrues, compounded monthly. # time for Amortization begins after that cat("\n\n") I <- intrPercent L <- years #for ease of notation J <- I/(12 * 100) P <- P * ((1+J) ^ monthsWithOutPayments) # for student loans cat("Principal when amortization begins is: ", asCurr(P, 0), "\n") N <- 12 * L M <- P*J/(1-(1+J)^(-N)) monthPay <<- M totPayments <- M cat("\nThe payments for this loan are:\n Monthly payment: ", asCurr(M), " (stored in monthPay)\n Total cost: ", asCurr(M*N), "\n\n", sep="") #INCREASING counter <- 0 intervals <- years * 4 intrRates <<- I Pt <- P # current principal or amount of the loan currP <- NULL # vector tracking Principal after each payment while(Pt>=0) { #Recalculate interest every 3 months counter <- (counter + 1) if ((counter %% 3) == 0) { I <- min(21, I + round((rnorm(1)),1)) intrRates <<- c(intrRates, I) } else if ((counter %% 10) == 0) { I <- min(21, I + round(abs(rnorm(1)),1)) intrRates <<- c(intrRates, I) } # RECALCULATE J <- I/(12 * 100) N <- 12 * L M <- P*J/(1-(1+J)^(-N)) totPayments <- totPayments + M #------------------------- H <- Pt * J # this is the current monthly interest C <- M - H # this is your monthly payment minus your monthly interest, so it is the amount of principal you pay for that month Pt <- Pt - C # this is the new balance of your principal of your loan currP <- c(currP, Pt) } # The loop continues until the value Q (and hence P) goes to zero 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 cat("The amortization data for each of the", N, "months are stored in \"aDFmonth\".\n") cat("The amortization data for each of the", L, "years are stored in \"aDFyear\".\n\n") cat("Total Payments were: ", asCurr(totPayments), "\n") cat("The rates were:", intrRates, ".\n\n") } schoolLoan(amt, 09.875, 10, 18)