R

时间:2016-06-21 02:19:40

标签: r

我正在尝试使用R复制http://www.wolframalpha.com/input/?i=mortgage的表输出。我已设置以下代码但不确定下一步该怎么做?

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))
}

2 个答案:

答案 0 :(得分:2)

以下是您的代码存在的问题:

  1. 未在输入对象上正确设置类
  2. 您没有名为pmt_denomenator的对象,但您引用了它(它被称为其他内容)
  3. 此代码现在运行良好:

    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
    

    因此,它修复了代码中的编程错误,但我相信您使用的公式仍然不同。这是correct formula

答案 1 :(得分:0)

万一有人在寻找更新的公式和代码(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()