# Mortgage calculator in R Shiny

2020-08-14 4 minute read

# Introduction

I recently moved out and bought my first apartment. Of course, I could not pay it entirely with my own savings, so I had to borrow money from the bank. I visited a couple of banks operating in my country and asked for a mortgage.

If you already bought your house or apartment in the past, you know how it goes: the bank analyzes your financial and personal situation and make an offer based on your propensity to repay the bank. You then either accept the offer if you are satisfied with the rate and conditions, or visit another bank if you believe you could receive a better offer. Mortgages and loans are more complicated than that of course, but let’s keep it simple here.

As I kind of like to control and keep a close eye on my personal finances (sometimes a bit too close I must admit), I knew precisely how much I could spend for my monthly mortgage repayment while still being able to cover my living expenses. However, I had no clue how much I could borrow in total for my new apartment given these housing repayments.

I knew I was not the first person in this case, so I looked online if I could find a R script which would answer my question (and potentially also give me the total cost of the housing loan, including the loan amount and the accumulated interests). I finally found a R script created a while ago by Prof. Thomas Girke.

# Mortgage calculator

The function in the script was functional and solved my main issue, but I wanted to be able to play more easily with the different settings such as the amount, the duration and the interest rate of the loan.

For this reason, I created a R Shiny app which is available here:

In the meantime, I received an Excel file from a friend working in a Belgian bank which does precisely the same task. I am not an actuary nor a expert in mortgage loan, so with his file I was able to cross check the results and edit the code accordingly.

The app greatly helped me to know the maximum amount I could borrow from the bank by playing with the three main settings of a mortgage, so it gave me a precise price limit when looking for apartments online.

Note that the app can of course be used for any loan, not only for mortgage.

# How to use the mortgage calculator?

First, you can find the mortgage calculator here.

I try to keep all my Shiny apps easy to use for everyone. However, here is how to use it in case it is not intuitive enough:

1. Enter the amount of the loan (i.e., the amount you would like to borrow, do not include downpayment)
2. Enter the annual interest rate in %
3. Enter the duration of the loan in years

On the right panel (or bottom if you use the app on mobile) you will see:

• a summary repeating the settings you entered,
• the total cost of the loan (principal and interests included), and more importantly
• the amount of the monthly payments

A plot representing the percentage attributed to the repayment of the interests and the capital is also displayed. You see that (especially in the first years of the loan), the higher the interest rate and the duration of the loan, the higher the percentage of the monthly repayments is attributed to the repayments of the interests.

Finally, the amortization table showing the remaining balance month by month is displayed after the summary and the plot. You can copy, export (in PDF, CSV or Excel) or print this amortization table for further use.

# Code of the app

Below the entire code in case you would like to enhance the app (feel free to send me your app if you happen to improve it!).

Thanks for reading. I hope this mortgage calculator helped you to play with the different settings of a mortgage, and who knows, helped you to decide which house or apartment to buy.

As always, if you have a question or a suggestion related to the topic covered in this article, please add it as a comment so other readers can benefit from the discussion.

Disclosure: Note that this application does not include investment advice or recommendations, nor a financial analysis. This application is intended for information only and you invest at your own risks. I cannot be held liable for any decision made based on the information contained in this application, nor for its use by third parties.

### Liked this post?

Get updates every time a new article is published.
No spam and unsubscribe anytime.