HP63
HP63

Reputation: 13

Mortgage Cash Calculator in R

I am trying to replicate the table output from http://www.wolframalpha.com/input/?i=mortgage using R. I have the following code set up but am not sure what to do next?

mortgage_cash_flow_enginer <- function(loan_amt,loan_period,APR){
                loan_amt <- readline(prompt = "Loan Amount: ")
                loan_period <- readline(prompt = "Loan Peroid: ") 
                APR <- readline(prompt = "Annual Percentage Rate (%): ")


                pmt_numerator <- ((loan_amt)*((APR)/(100*12L)))
                morpmt_denomenator <- (1-((1+((APR)/(100*12L)))^((-1)*loan_period*12)))
                monthly_payment <- (pmt_numerator)/(pmt_denomenator)
                return(as.integer(monthly_payment))
}

Upvotes: 1

Views: 598

Answers (2)

TheSciGuy
TheSciGuy

Reputation: 1196

In case anyone was looking for the updated formula and code (http://mathworld.wolfram.com/Mortgage.html):

  mortgage_cash_flow_engineer <- function(){
  loan_amt <- as.numeric(readline(prompt = "Loan Amount ($): "))
  loan_period <- as.numeric(readline(prompt = "Loan Period (months): ") )
  APR <- (as.numeric(readline(prompt = "Annual Percentage Rate (%): "))/100)


  pmt_numerator <- ((loan_amt*(APR/12))*((1+(APR/12))^loan_period))
  pmt_denomenator <- ((1+(APR/12))^loan_period)-1
  monthly_payment <- (pmt_numerator)/(pmt_denomenator)
  return(as.numeric(monthly_payment))
}

mortgage_cash_flow_engineer()

Upvotes: 0

Hack-R
Hack-R

Reputation: 23200

Here are the problems with your code:

  1. The classes are not properly set on the input objects
  2. You didn't have an object called pmt_denomenator but you referenced it (it was called something else)

This code runs fine now:

mortgage_cash_flow_engineer <- function(){
  loan_amt <- as.numeric(readline(prompt = "Loan Amount: "))
  loan_period <- as.numeric(readline(prompt = "Loan Peroid: ") )
  APR <- as.numeric(readline(prompt = "Annual Percentage Rate (%): "))


  pmt_numerator <- ((loan_amt)*((APR)/(100*12)))
  pmt_denomenator <- (1-((1+((APR)/(100*12)))^((-1)*loan_period*12)))
  monthly_payment <- (pmt_numerator)/(pmt_denomenator)
  return(as.integer(monthly_payment))
}

mortgage_cash_flow_engineer()#200000,30,.0365

So, that fixes the programming bugs in your code, but I believe the formula you're using is still different. This is the correct formula.

Upvotes: 2

Related Questions