Mortgage Calculator (and Amortization Charts) with R

[This article was first published on R-Chart, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


Mortgage rates have been at historic lows recently.  The rates are posted various places online along with simple mortgage calculators.  Such calculators illustrate the payment schedule for a mortgage based upon selected terms. But with less than a dozen lines of R code, you can do a far more sophisticated analysis.

Mortgage Calculation Function
Rather than reinvent the wheel, you can work with this nice R function by Thomas Girke (Associate Professor of Bioinformatics over at UC Riverside).  At the R prompt, you can grab it from its home online by calling source:

source(“http://faculty.ucr.edu/~tgirke/Documents/R_BioCond/My_R_Scripts/mortgage.R”)




This loads the function and outputs a helpful description of the function:



The monthly mortgage payments and amortization rates can be calculted with the mortgage() function like this: 

        mortgage(P=500000, I=6, L=30, amort=T, plotData=T)
                P = principal (loan amount)
                I = annual interest rate
                L = length of the loan in years 

So keep in mind that there is a huge amount of R code available online: 
are just the beginning.  


Instant R Graphical User Interfaces

Rather than simply calling the function directly, you can quickly construct a GUI input widget using the fgui library.

library(fgui)
gui(mortgage)


With this trivial invocation, a window pops up.






Not terribly fancy, but about the simplest way you will ever be able to construct a GUI!  In this case a mortgage amount of $90,000 for 10 years at 3.75% is illustrated.  


After entering these values, click OK to actually call the function.  This results in a  good deal of interesting output.  Close the pop up window and look at the R Console:



Monthly payment: $900.5512 (stored in monthPay)


                        Total cost: $108066.1



As indicated in this message, an R object named monthPay contains the amount of the monthly payment and can be used in subsequent R commands and calculations.  You also are greeted with a graph illustrating annual interest and payments as a stacked bar chart.

Plenty of useful information!  But that’s not all…


Beyond the Basics
You might have noticed a number of messages regarding data stored in R objects.  This is where the power of R exceeds that of any standard mortgage calculator.  These objects can serve as input to other calculations or charting operations.


The aDFmonth object contains amortization data for each month, while aDFyear contains annual information. In the following example, a new data frame is created from the monthly data that does not include the amortization information and plot it using ggplot2.  (The amortization data is a significantly different scale and better viewed independently).

library(ggplot2)
DF=melt(aDFmonth[-1], id.vars=’Year’)

ggplot(DF, aes(x=Year,y=value, group=variable)) + geom_line() + facet_wrap(~ variable, ncol=1)
You can quickly manipulate the data frame to view amortization information instead.  Use the exact same ggplot call (though the facet_wrap is removed below as unnecessary for a single variable)  to create a chart scaled to fit the values relevant to the amortization.

DF=melt(aDFmonth[c(1,5)], id.vars=’Year’)
ggplot(DF, aes(x=Year,y=value, group=variable))+ geom_line() 


The limits of calculations and visualizations available in a web calculator or Excel are reached pretty quickly.  R provides the means to create relatively full featured solutions in only a few lines of code. 

To leave a comment for the author, please follow the link and comment on their blog: R-Chart.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)