Estimated Tax Calc

Tax impacts need to be estimated when planning scenarios.  Often a fixed percentage is used.  To get a more accurate number, it would be better to actually incorporate tax tables.

Code below accomplishes that and can be brought into calculations as desired.  There is no implied tax advice in the code nor should it be used for actual filing purposes.  Also it assumes married filing jointly.

Load  the following function and associated tax break info (good as of 2018).  Only CA included not any other state.

############################
#### Tax Estimate Code
####
#################################
crawler=seq(1,50)
########################## Tax Brackets Fed and CA
taxRates<-list()
taxRates$Fed$Breaks<-c(0, 19050, 77401, 165001, 315001, 400001, 600001 )
taxRates$Fed$Rate<-c(.10, .12, .22, .24, .32, .35, .37 )
taxRates$Fed$Deduction=24000
taxRates$Fed
"$Breaks
[1] 0 19050 77401 165001 315001 400001 600001
$Rate
[1] 0.10 0.12 0.22 0.24 0.32 0.35 0.37
"
taxRates$CA$Breaks<-c(0, 16030, 38003, 59979, 83259, 105225, 537501, 644999, 1000001, 1074997)
taxRates$CA$Rate<-c(.01, .02, .04, .06, .08, .093, .103, .113, .123, .133)
taxRates$CA
"# $Breaks
# [1] 0 16030 38003 59979 83259 105225 537501 644999 1000001 1074997
# $Rate
# [1] 0.010 0.020 0.040 0.060 0.080 0.093 0.103 0.113 0.123 0.133
"
calcFedAndStateTax <- function(taxableInc, roth=0, env = parent.frame()) {
retList<-list()
retList$TaxableIncome=taxableInc
taxable = taxableInc + roth - taxRates$Fed$Deduction
if (taxable <= 0) {
taxable = 0
retList$taxFed = retList$taxCA = 0
return(retList) 
}
## FedTax
y <<- 0
lim<-max(which(taxRates$Fed$Breaks<=taxable))
if ( lim > 1) {
sapply(crawler[2:lim], function(x) y<<-y+(taxRates$Fed$Breaks[x]-taxRates$Fed$Breaks[x-1])*taxRates$Fed$Rate[x-1])
## need to allow for taxInc over last Break 
if ( length(is.infinite(which(taxRates$Fed$Breaks>taxable))) == 0 ) #<< will return 0 else +ve number 
{
#just use last item 'lim' from first test above - get excess and apply last rate
y<<-y+(taxable - taxRates$Fed$Breaks[lim])*taxRates$Fed$Rate[lim]

} else {
# do regular i.e., found bracket
lim<-min(which(taxRates$Fed$Breaks>taxable))
limF=lim
#print (sprintf("Taxable Amount is = %.2f --> %.0f --> %.2f --> Tax %.2f", taxable, lim,taxRates$Fed$Breaks[lim-1], y ))
y<<-y+(taxable - taxRates$Fed$Breaks[lim-1])*taxRates$Fed$Rate[lim-1]
}
} else y <<- taxable * taxRates$Fed$Rate[1]
retList$taxFed <- y

##### CA Tax
y <<- 0
lim<-max(which(taxRates$CA$Breaks<=taxable))
if ( lim > 1) {
sapply(crawler[2:lim], function(x) y<<-y+(taxRates$CA$Breaks[x]-taxRates$CA$Breaks[x-1])*taxRates$CA$Rate[x-1])
## need to allow for taxInc over last Break 
if ( length(is.infinite(which(taxRates$CA$Breaks>taxable))) == 0 ) #<< will return 0 else +ve number 
{
#just use last item 'lim' from first test above - get excess and apply last rate
y<<-y+(taxable - taxRates$CA$Breaks[lim])*taxRates$CA$Rate[lim]

} else {
# do regular i.e., found bracket
lim<-min(which(taxRates$CA$Breaks>taxable))
y<<-y+(taxable - taxRates$CA$Breaks[lim-1])*taxRates$CA$Rate[lim-1]
}
} else y <<- taxable * taxRates$CA$Rate[1]
#iraSeg$taxCA[indx] <<- y
retList$taxCA <- y
#print (sprintf("Taxable Amount is = %.2f Fed = %.2f CA = %.2f lim=%.0f", taxable, iraSeg$taxFed[indx] , iraSeg$taxCA[indx], limF ))
return(retList) 
}

Then using any value for TESTAMT, i.e., the adjusted gross income just call function as below:

TESTAMT=125000
estTax<- calcFedAndStateTax(taxableInc=TESTAMT)
estTax

$TaxableIncome
[1] 125000

$taxFed
[1] 14098.9

$taxCA
[1] 4294.88