Have you ever thought of borrowing some money? A common case is when you have to borrow money for buying a house, which is called a mortgage. Wikipedia (‘Mortgage loan’ entry) goes into much more detail about the definition than what I’ll cover.
One of the aspects you have to evaluate when considering a loan such as a mortgage is how much interest you will be charged and how long the loan will be. Those two determine your monthly payment.
That is, given the loan amount, you can compare different loans that offer different rates and/or loan lengths. There are many tools out there that allow you to do so and are generally called mortgage calculators. Although the more detailed term is amortization calculator. From wikipedia(‘Amortization calculator’ entry) we find the so called annuity formula which helps you calculate your monthly payment.
My use case
However, a month or so ago I got curious about comparing some loans which had a different twist than normal. Given some circumstances, I wanted to compare some loans where the loaner is willing to receive payments every few months yet with interests being compounded (that is, calculated) on a different time frame. Obviously, the easiest would be to compound interest at the same frequency as payments are made. Plus, probably most banks would not offer such flexible loans.
Being interested in
R code, shiny (RStudio Inc.) apps and trying out integrating rCharts (Vaidyanathan, 2013) with shiny, I ended up coding my own mortgage calculator which you can find here (Simple Mortage Calculator). The code is publicly available at GitHub.
It has a couple of simple inputs:
- Amount to borrow
- Interest rate
- Loan duration
- Payment frequency
- How frequently interests are compounded
- Month of the first payment
The option to begin the loan now but accept the first payment much later is there also because of the complicated use case I had in mind. Again, that is a feature a bank will most likely not offer. But it’s something I needed to take into consideration.
Going back to the app, I tried explaining all the inputs as much as possible. The output is relatively straight forward.
First, there is a line with focus D3 interactive plot (Line Chart with View Finder) which shows the principal (the amount you owe) over months as well as how much you’ve paid already. The bottom panel allows you to zoom into a specific time range as shown below.
Next, the information is shown in more detail as an interactive data table (DataTables) 12 months at a time. The interactive part makes it very easy to search for a specific month and look at the state of the loan.
Finally, you can download the amortization table in CSV format for your records.
PS You can also access the app via shinyapps at this url. It is the first time I’ve deployed an app there as I wanted to test shinyapps out.
The experience of coding the shiny app (Simple Mortage Calculator) was interesting as I did learn a couple of new things. The same was true for figuring out the calculations for the more flexible options.
Finally, but not least, the shiny app was useful for my use case and was informative for comparing different loan options. Hopefully it will be useful for other users!
Citations made with
knitcitations (Boettiger, 2014).
- Mortgage loan - Wikipedia, the free encyclopedia. Wikipedia http://en.wikipedia.org/wiki/Mortgage_loan
- Amortization calculator - Wikipedia, the free encyclopedia. Wikipedia http://en.wikipedia.org/wiki/Amortization_calculator
- C, (2010) Mortgage Calculator (and Amortization Charts) with R. R-Chart http://www.r-chart.com/2010/11/mortgage-calculator-and-amortization.html
- BioStatMatt, Mortgage Refinance Calculator | BioStatMatt. BioStatMatt http://biostatmatt.com/archives/1908
- Compound Interest Calculator. http://www.calculator.net/compound-interest-calculator.html
- Compound Interest. http://math.about.com/library/weekly/aa042002a.htm
- Simple Mortgage Calculator. http://glimmer.rstudio.com/lcolladotor/mortgage/
- Novus Partners, Line Chart With View Finder - NVD3. http://nvd3.org//examples/lineWithFocus.html
- DataTables (table plug-in for jQuery). DataTables (jQuery plug-in) https://datatables.net/
- Carl Boettiger, (2014) knitcitations: Citations for knitr markdown files. http://CRAN.R-project.org/package=knitcitations
- Ramnath Vaidyanathan, (2013) rCharts: Interactive Charts using Polycharts.js.
- RStudio , Inc. , (2014) shiny: Web Application Framework for R. http://CRAN.R-project.org/package=shiny
## R version 3.1.0 (2014-04-10) ## Platform: x86_64-apple-darwin10.8.0 (64-bit) ## ## locale: ##  en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 ## ## attached base packages: ##  stats graphics grDevices utils datasets methods base ## ## other attached packages: ##  knitcitations_0.5-0 bibtex_0.3-6 knitr_1.5.27 ## ## loaded via a namespace (and not attached): ##  codetools_0.2-8 digest_0.6.4 evaluate_0.5.3 formatR_0.10 ##  httr_0.3 RCurl_1.95-4.1 stringr_0.6.2 tools_3.1.0 ##  XML_3.98-1.1 xtable_1.7-3