Last active
November 28, 2017 05:08
-
-
Save emhart/c22e3c3b008e8b921004362350a490bf to your computer and use it in GitHub Desktop.
Housing model simulation to help me decide if we should buy or not...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Define terms to understand investment potentional | |
### This function will simulate scenarios to compare renting to buying and allow us to make an informed financial decision | |
## Parameters of the model (some are hard coded but can still be changed) | |
## r - Annual appreciation rate | |
## term - Term you want to own the house for (in months) | |
## house_cos - cost of the house in dollars | |
## ir - Interest rate of my mortgage | |
## closing - total closing costs | |
## sr - cost of selling the house, expressed as a percentage that will be paid in comission | |
## aou - Amount over/under for final sale (on top of annual appreciation rate, gets a bit confusing so best to set to 0) | |
## rinvest - Amount of annual appreciation of an investment expressed as a percentage | |
## tax_penalty - how much annually will I save in taxes if I buy instead of rent, essentially an annual penalty for renting | |
housing_comp <- function(r = 0.04, | |
term = 100, | |
house_cost = 1000000, | |
closing = 13000, | |
ir = 3.375, | |
sr = 0.055, | |
aou = 0, | |
rinvest = 0.05, | |
savings_rate = 0.9, | |
tax_penalty = 10000){ | |
### Set the property tax rate | |
prop_tax_rate <- .0125 | |
### Set monthly taxes | |
monthly_taxes <- (prop_tax_rate * house_cost) / 12 | |
### Loan principal | |
principal <- .8*house_cost | |
val <- house_cost | |
### My downpayment | |
mydp <- .1*house_cost | |
for(x in 1:term){ | |
val <- val*(1+(r/12)) | |
} | |
### Calculate the amount of principal you have in the home | |
mort <- mortgage(principal,ir) | |
## principal paid | |
ppaid <- sum(mort$Monthly_Principal[1:term]) | |
## interest paid | |
ipaid <- sum(mort$Monthly_Interest[1:term]) | |
sold_amount <- (1+aou)*val | |
### Calculate the total costs of buying the house (closing and selling costs) | |
costs <- ((sold_amount*sr) + closing) | |
### calculate the total profit | |
profit <- sold_amount - house_cost | |
### Calculate landed cut | |
to_landed <- (.1*house_cost)+(.25*profit) | |
### How much the bank takes back | |
to_bank <- principal - ppaid | |
### Calculate my profit | |
my_profit <- sold_amount - (to_bank + to_landed + costs) | |
### Calculate a simple ROI | |
roi <- (my_profit / mydp) - 1 | |
### Add in some benchmarks | |
### What was my rent | |
rent<- 3250*term | |
### Here I'll calculate how much I'm going to save if I keep renting | |
## Set a savings rate (fraction of the difference between mortage and rent that we'd likely save instead of blowing on travel or amazon) | |
savings_rate <- .9 | |
### Set what my all in monthly payment would be (taxes, mortagage, insurance) | |
monthly_mortgage_pay <- monthly_taxes + mort[1,2] + (.00022* house_cost) | |
monthly_savings <- (monthly_mortgage_pay - 3250) * savings_rate | |
### How much I could theoretically have invented initially | |
invest <- mydp + closing | |
### Calculate my investment potential savings | |
for(x in 1:term){ | |
invest <- (invest*(1+(rinvest/12)) ) + monthly_savings | |
} | |
cat("costs: ", costs,"\n") | |
cat("total profit: ",profit,"\n") | |
cat("to bank: ",to_bank,"\n") | |
cat("to landed: ",to_landed,"\n") | |
cat("my profit: ",my_profit,"\n") | |
cat("my house roi: ",roi,"\n") | |
cat("my investment: ",invest,"\n") | |
#cat("my investment roi: ", (invest / (mydp+closing)) - 1 ) | |
cat("my rent costs: ", rent,"\n") | |
cat("my interest: ", ipaid,"\n") | |
### Term for tax savings thrown in to the rent | |
total_cost_living_own <- my_profit - ((monthly_taxes*term) + ipaid) | |
total_cost_living_rent <- invest - (rent + ((tax_penalty/12) * term)) | |
cat("Owning total cost of living for ",term,"months: ", total_cost_living_own,"\n") | |
cat("Renting total cost of living for ",term,"months: ", total_cost_living_rent,"\n") | |
### Now how much money will I have when I walk away? | |
cat("If I buy I'll have ", my_profit, " after livining in my own house for ",(term/12), " years \n") | |
cat("If I rent I'll have ", invest, " after livining in a rented house for ",(term/12), " years \n") | |
out <- list("own cost" = total_cost_living_own, "rent cost" = total_cost_living_rent, "my profit" = my_profit, "own roi" = roi) | |
return(out) | |
} | |
### Function to calculate basic mortage payment in the US | |
## P = principal, the initial amount of the loan | |
## I = the annual interest rate (from 1 to 100 percent) | |
## L = length, the length (in years) of the loan, or at least the length over which the loan is amortized. | |
mortgage <- function(P=500000, I=6, L=30, amort=T) { | |
J <- I/(12 * 100) | |
N <- 12 * L | |
M <- P*J/(1-(1+J)^(-N)) | |
monthPay <<- M | |
# Calculate Amortization for each Month | |
if(amort==T) { | |
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) | |
} | |
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)] | |
) | |
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))) | |
) | |
aDFyear <- aDFyear | |
} | |
return(aDFmonth) | |
} | |
### Now lets make some plots | |
library(ggplot2) | |
term <- 1:100 | |
r = c(.01,.02,.03,.04,.05) | |
rinvest = c(.02,.04,.06,.08,.1) | |
out_mat <- matrix(ncol=5, nrow=0) | |
### buying is flagged as 1 | |
### renting is flagged as 0 just so I can use a matrix | |
### format is cost, term , r , rinvest, own_flag | |
for(i in term){ | |
for(j in 1:length(r)){ | |
for(k in 1:length(rinvest)){ | |
temp_est <- housing_comp(r=r[j], rinvest= rinvest[k], term = i, house_cost = 750000) | |
out_mat <- rbind(out_mat, c(temp_est$`own cost`,r[j],rinvest[k],i,1 )) | |
out_mat <- rbind(out_mat, c(temp_est$`rent cost`,r[j],rinvest[k],i,0 )) | |
} | |
} | |
} | |
out_df <- data.frame(out_mat) | |
colnames(out_df)<- c('cost', 'r' , 'rinvest', 'term' , 'own_flag') | |
out_df$own_flag <- replace(out_df$own_flag, out_df$own_flag == 1,"Buy") | |
out_df$own_flag <- replace(out_df$own_flag, out_df$own_flag == 0,"Rent") | |
ggplot(out_df,aes(x = term,y=cost, group = own_flag, colour=own_flag)) + facet_grid(r~rinvest)+ geom_path() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment