Week 4 Assignment 2
Robert B.
March 4, 2017
Do you want to know what your mortgate payments will be, and how long it will take to page off the loan?
Have you wonder why the interest paid on a loan is at least almost double the payment amount?
It seems unbelievable the number of payments one needs to make to pay off the loan.
Here, we will show you a way to increase your savings in the next slides.
The Mortgage Payment Calculator provides flexibility to change the house price, down payment, loan type and interest rate. The left side of the web page provides input field, drop down list and sliding bar to allow easily enter the desire values. Here is an snippet of the code that is used to build the application.
library(shiny);library(plotly);library(httpuv); library(webshot)
shinyApp(
ui = fluidPage( sidebarLayout( sidebarPanel(
numericInput('Price', 'Home Price', 120000),
numericInput('Down', 'Down Payment', 20000),
selectInput('loan', 'Loan Type', c('30 Year Fixed','20 Year Fixed','15 Year Fixed')),
sliderInput("Rate","Interest Rate:",min = 1.0, max = 20.0, value = 4.0),
submitButton("Submit")
),
mainPanel( plotOutput('plot1', height = "400px") )
)),
server = function(input, output, session) {
output$plot1 <- renderPlotly({
bal <- input$Price - input$Down
if (input$loan == '30 Year Fixed') { dur <- 30 * 12
} else if ( input$loan == '25 Year Fixed') { dur <- 20 * 12
} else if ( input$loan == '15 Year Fixed') { dur <- 15 * 12
}
apr <- input$Rate/100/12
pmt <- bal*((apr*(1+apr)^dur)/(((1+apr)^dur)-1))
pYear <- 12
int <- apr*bal
df <- matrix(NA, nrow =dur, ncol = 8)
colnames(df)<- c("pNo","pAmt","int","prAmt","lBal","prAc","intAc","pmtAc")
df[1,] <- c(pNo=1,pAmt=pmt, int=int,prAmt=pmt-int,
lBal=bal-int, prAc=pmt-int, intAc=int,pmtAc=pmt)
for (i in c(2:dur)) {
df[i,1] <- i; df[i,2] <- pmt;df[i,3] <- apr*df[i-1,5]
df[i,4] <- pmt - df[i,3]; df[i,5] <- df[i-1,5]-df[i,4];
df[i,6] <- df[i-1,6]+df[i,4]; df[i,7] <- df[i-1,7]+df[i,3]
df[i,8] <- df[i-1,8]+df[i,2]
if (df[i,5] < 0) break;
}
df <- as.data.frame(df)
p<-plot_ly(df, x = ~pNo, y = ~pmtAc, name = 'Interest',
type = 'scatter', mode = 'none', fill = 'tozeroy', fillcolor = '#F5FF8D')%>%
add_trace(y = ~prAc, name = 'Principal', fillcolor = '#50CB86') %>%
add_lines(y= ~lBal, name="Balance", fill= FALSE, mode='lines',fillcolor='black')%>%
layout(title = 'Loan Payment Amortization',
xaxis = list(title = "Monthly Payments", showgrid = FALSE),
yaxis = list(title = "Currency", showgrid = FALSE))
})
})
Check the app to visualize these savings.
This impresive graph shows the variables that play a big role on a mortgage loan.