我如何绘制一个保持一个变量常量的函数?
我想从y轴上的函数和x轴上的利率绘制“monthPay”。所有变量都将保持不变。
我相信有一个功能可以做到这一点,但我不记得它的名字。
mortgage <- function(P=500000, I=6, L=30) {
J <- I/(12 * 100)
N <- 12 * L
M <- P*J/(1-(1+J)^(-N))
monthPay <- M
# Calculate Amortization for each Month
Pt <- P # current principal or amount of the loan
currP <- NULL
while(Pt>=0) {
H <- Pt * J # this is the current monthly interest
C <- M - H # this is your monthly payment minus your monthly interest, so it is the amount of principal you pay for that month
Q <- Pt - C # this is the new balance of your principal of your loan
Pt <- Q # sets P equal to Q and goes back to step 1. The loop continues until the value Q (and hence P) goes to zero
currP <- c(currP, Pt)
}#End Pt Calc
monthP <- c(P, currP[1:(length(currP)-1)])-currP
aDFmonth <<- data.frame(
Amortization=c(P, currP[1:(length(currP)-1)]),
Monthly_Payment=monthP+c((monthPay-monthP)[1:(length(monthP)-1)],0),
Monthly_Principal=monthP,
Monthly_Interest=c((monthPay-monthP)[1:(length(monthP)-1)],0),
Year=sort(rep(1:ceiling(N/12), 12))[1:length(monthP)]
)#end aDFmonth
aDFyear <- data.frame(
Amortization=tapply(aDFmonth$Amortization, aDFmonth$Year, max),
Annual_Payment=tapply(aDFmonth$Monthly_Payment, aDFmonth$Year, sum),
Annual_Principal=tapply(aDFmonth$Monthly_Principal, aDFmonth$Year, sum),
Annual_Interest=tapply(aDFmonth$Monthly_Interest, aDFmonth$Year, sum),
Year=as.vector(na.omit(unique(aDFmonth$Year)))
)#end aDFyear
return(list(monthPay, aDFmonth,aDFyear ))
}
temp<-mortgage(P=500000, I=6, L=30)
temp[[1]] #the var. for the y-axis.