I am trying to implement a simple amortization schedule using tidyverse instead of writing loops. I am assuming there is a way to do this using tidyverse. The closest help I got to getting this done was
The problem I am trying to solve is as below
For Month 1,
LoanRem = Loan
Beyond Month 1
LoanRem = Loan - Cumulative_Sum_Previous(Principal_Payments)
PrinPay[i] = A + ExtraPrinPay[i] - Int * ( Loan - (PrinPay[1] + .. PrinPay[i-1] )
n = <constant> length of the payment scheme
A
(Monthly Payment), Int
(Monthly Interest rate), Loan
(Initial loan amount) are all constants
ExtraPrinPay
is a vector of length of length n
.
The closest link I got help which solves a slightly different problem was from this linkRecursive function using dplyr
Cost <- 200000;
Down <- 0.2 * Cost;
Loan <- Cost - Down;
Int <- 0.04 / 12;
TermY<- 6;
Term <- TermY;
A <- Loan * Int * ( 1 + Int )^(Term) / ( ( 1 + Int )^(Term) - 1.0 );
LoanRem <- function(x,y,Epp,A=A,Int=Int,Loan=Loan) {
return(A + Epp - Int * Loan - int * x);
}
# LoanRem = if_else(Mon==1,Loan,Loan-lag(LiqGain,1)),
# PrinPay = A + Curr(ExtraPrinPay) - Int * Loan - int * cumsum_1(PrinPay),
data <- data.frame(Mon=seq(1,6,1),ExtraPrinPay=0) %>%
mutate(PrinPay=accumulate2(PrinPay,,Epp=ExtraPrinPay,.f=LoanRem,.init = 0))
I get the error
Error in mutate_impl(.data, dots) :
Evaluation error: object 'PrinPay' not found.
In addition: Warning message:
package ‘bindrcpp’ was built under R version 3.4.4
Can some one help me