Suppose you want to buy a $350,000 house, and you plan to get a 30-year fixed mortage (interest rate 4.87%) with a 20% down payment. What’s your monthly repayment? How much of it will go towards interest and how much will go towards the principal?
Notice the mortgage amount you are borrowing is $280,000 (80% of the house price), and there’re 360 monthly payments you need to make.
The calculation of the monthly payment involves 2 steps conceptually:
- Calculate the simple monthly rate by dividing the interest rate by 12. This is because the quoted interest rate is (almost always) an annual rate (APR).
- The mortgage amount, monthly payment amount and monthly rate are related by the present value formula for annuities. Knowing any two of them, we can easily solve for the 3rd one.
These 2 steps are carried out in the following R code block.
So every month, you’re expected to make a payment of $1480.93 back to your lender. The interest is calculated by multiplying the outstanding principal at the beginning of the month by the monthly rate, and the reducible amount of principal is obtained by subtracting interest from the monthly payment $1480.93. The following code block calculates the payment schedule (also called amortization schedule) for the first 12 payment months.
The R code I give above should be very comfortable to you. If you ask 10 R users to program a mortgage payment schedule calculator, 8 of them probably will give you something similar. However, if you understand the behavior of R environments and
<<-, you can code it up differently. In the next blog post, I’ll explain how environments work and code up the same calculator in a different fashion using
PS: I’ve put the above code pieces in a function for ease of use.