R/Financial_Functions.R

Defines functions RATE PMT

PMT <- function(principal, t, apr, alt_apr, k_id = NULL, df = NULL, output = 'dataframe') {
  
  #----- Setup
  if (is.null(df)) {
    df <- data.frame(Principal = principal, Term = t, APR = apr, Alt_APR = alt_apr)
  } else {
    df <- as.data.frame(df)
    df$Principal <- df[, principal]
    df$Term <- df[, t]
    df$APR <- df[, apr]
    
    if (is.numeric(alt_apr)) {
      df$Alt_APR <- alt_apr
    } else {
      df$Alt_APR <- df[, alt_apr]
    }
    
    if (!is.null(k_id)) {
      df$k_ID <- df[, k_id]
    }
    
  }
  
  #----- SF
  #-- Monthly Interest Rate
  df$r <- (1 + df$APR)^(1/12) - 1
  
  #-- SF Repayment Amount
  df$Payment <- ((df$r*df$Principal)*((1+df$r)^df$Term)) / (((1+df$r)^df$Term) - 1)
  
  #-- Alt Monthly Interest Rate
  df$r_alt <- (1 + df$Alt_APR)^(1/12) - 1
  
  #-- Alt Repayment Amount
  df$Alt_Payment <- ((df$r_alt*df$Principal) * ((1+df$r_alt)^df$Term)) / (((1+df$r_alt)^df$Term) - 1)
  
  #-- Savings
  df$Periodic_Savings <- df$Alt_Payment - df$Payment
  df$Total_Savings <- df$Periodic_Savings*df$Term
  
  #-- Total Repayment
  df$Total_Repayment <- df$Payment*df$Term
  
  #----- Fixes
  #-- k_id
  if (is.null(k_id)) {
    df$k_ID <- 1:nrow(df)
  }
  
  #-- NA Values
  df <- df %>% 
    mutate(
      Principal = ifelse(is.na(Principal), 0, Principal),
      Total_Repayment = ifelse(is.na(Total_Repayment), 0, Total_Repayment),
      Payment = ifelse(is.na(Payment), 0, Payment),
      Term = ifelse(is.na(Term), 0, Term),
      APR = ifelse(is.na(APR), 0, APR),
      Alt_Payment = ifelse(is.na(Alt_Payment), 0, Alt_Payment),
      Alt_APR = ifelse(is.na(Alt_APR), 0, Alt_APR),
      Periodic_Savings = ifelse(is.na(Periodic_Savings), 0, Periodic_Savings),
      Total_Savings = ifelse(is.na(Total_Savings), 0, Total_Savings)
    )
  
  #----- Output
  #-- List
  if (output == 'list') {
    ds_output <- list(
      k_ID = df$k_ID,
      Principal = df$Principal,
      Total_Repayment = df$Total_Repayment,
      Payment = df$Payment,
      Term = df$Term,
      APR = df$APR,
      Alt_Payment = df$Alt_Payment,
      Alt_APR = df$Alt_APR,
      Periodic_Savings = df$Periodic_Savings,
      Total_Savings = df$Total_Savings
    )
  }
  
  #-- Dataframe
  if (output == 'dataframe') {
    ds_output <- df %>% 
      select(
        Principal,
        Total_Repayment,
        Payment,
        Term,
        APR,
        Alt_Payment,
        Alt_APR,
        Periodic_Savings,
        Total_Savings
      )
  }
  
  return(ds_output)
}

#----------------------------------------------------------------------------------------------------#

RATE <- function(principal, t, repayment) {
  
  #----- Setup
  f <- function(i,n,a) {
    a - (1-(1+i)^(-n))/i
  }
  
  if (t * repayment < principal) {
    x <- -1
  } else {
    x <- uniroot(f, 
                 interval = c(1e-9, 10000),
                 t,
                 principal/repayment,
                 tol = 1e-6)$root
  }
  
  return(x)
  
}
Ehsan-F/R-Mixtape documentation built on June 24, 2020, 12:22 a.m.