In mid-August 2022 the Governor of North Dakota unveiled a plan to change the tax structure of their state from a multi-tiered tax structure to a flat tax. I used this as an opportunity to develop a structure to analyze changes is tax rates and how they would affect various income levels.
I wanted to develop code that would allow me to enter in the current and proposed rates and calculate the tax owed across a sequence of incomes for each income level. I also want this code to be repeatable simply if there are any other tax changes in the future so it would be easy to change the income and rate levels without needing to overhaul the code.
After attempting a few different methods I settled on the idea of building a function that could be used with a version of apply()
to pass a vector into it and calculate off the income levels. The problem I ran into at this point was the calculating of the tax. After consulting google about it I found a Stack Overflow exchange that described this exact situation. They suggested using a While() loop
to calculate the income at each step. I found the idea straight forward and set out to build the function using this method. With some modification to the code in the stack overflow I found a method that worked great.
tax<- function(inc, brackets, rates) {
nbrackets<- length(brackets)
if(inc<=brackets[1])
return(0)
i<-2
cumtax<-0
while(i <=nbrackets){
if(inc>brackets[i-1]&& inc <brackets[i])
return(cumtax + rates[i-1]*(inc-brackets[i-1]))
else
cumtax<-cumtax+rates[i-1]*(brackets[i]-brackets[i-1])
i<- i+1
}
cumtax +rates[nbrackets]*(inc - brackets[nbrackets])
}
To use the function I created 4 data frames of tax data. The each of the four data frames included tax rate schedules for one of the four situations I was calculating: Current and Proposed rate schedules for each Single and Married Filing Jointly returns.
mfjtaxcur<-sapply(income,tax,brackets=mfjcur$xmin, rates=mfjcur$xrate)
These four tax calculations I then joined togethter in a single frame, totaltax
, and added comparative calculations to them.
tottax<-data.frame(income=income,
filing_type = "Single",
current=sgltaxcur,
proposed=sgltaxnew)%>%
union(data.frame(income=income,
filing_type="Married",
current=mfjtaxcur,
proposed=mfjtaxnew))%>%
mutate(change = proposed-current,
current_eff = round(current/income,4) ,
proposed_eff = round(proposed/income,4),
change_eff = proposed_eff-current_eff,
change_pct = change/current
)
I was then able to visualize the data into two visualizations.