Testing Modified UI Calculator

2023-04-06

#Packages needed to complie the document
library(reticulate)
library(magrittr) # needs to be run every time you start R and want to use %>%
library(dplyr)    # alternatively, this also loads %>%
library(purrr)
#Setting up the directory
setwd("D:/UGA Coursework/RATA Work/Calculator/ui_calculator-master/")

#The YAML export of the environment we used is in the source folder.
use_python("C:\\Users\\mr_no\\AppData\\Local\\Programs\\Python\\PYTHON~1\\python.exe")

ui_calculator <- file.path("ui_calculator","ui_calculator_new.py")

ui_calculator= source_python(ui_calculator)


#Example of weekly benefits calculated
income_data <- read.csv("example_annual_new.csv")
#Icome data is same for year A and B (I am using strg year on purpose but the code works with numeric version of year as well)
head(income_data,6)
##   state year   wage weeks_worked
## 1    IN    A   1600            8
## 2    AZ    A  88000           52
## 3    NV    A  35000           52
## 4    GA    A   2500            3
## 5    CO    A   2400           45
## 6    MN    A 120000            8
tail(income_data,6)
##    state year   wage weeks_worked
## 7     IN    B   1600            8
## 8     AZ    B  88000           52
## 9     NV    B  35000           52
## 10    GA    B   2500            3
## 11    CO    B   2400           45
## 12    MN    B 120000            8
# Max, Min in State thresholds are changed for this example
head(state_rules,6)
##   state year wage_concept        rate intercept minimum maximum inc_thresh
## 1    AK    A  annual_wage 0.008000000        36      56     370          0
## 2    AL    A         2hqw 0.019230769         0      45     275          0
## 3    AR    A  annual_wage 0.009615385         0      81     451          0
## 4    AZ    A          hqw 0.040000000         0     187     240          0
## 5    CA    A          hqw 0.038461538         0      40     450          0
## 6    CO    A  annual_wage 0.009615385         0      25     618      58344
tail(state_rules,6)
##     state year wage_concept       rate intercept minimum maximum inc_thresh
## 103    VA    B         2hqw 0.02000000         0    1596    1764          0
## 104    VT    B         2hqw 0.02222222         0    1608    1899          0
## 105    WA    B         2hqw 0.01925000         0    1724    2176          0
## 106    WI    B          hqw 0.04000000         0    1590    1756          0
## 107    WV    B  annual_wage 0.01057692         0    1560    1810          0
## 108    WY    B          hqw 0.04000000         0    1572    1894          0
income_data <- income_data %>% mutate(weekly_earnings = wage / weeks_worked,
                                      q1_earnings = weeks_worked - 39,
                                      q2_earnings = weeks_worked - 26,
                                      q3_earnings = weeks_worked - 13,
                                      q4_earnings = weeks_worked) %>%
  mutate_at(vars(matches("q[1-4]_earnings")), ~ case_when(
    .x > 13 ~ 13 * weekly_earnings,
    .x < 0 ~ 0,
    TRUE ~ .x * weekly_earnings))


print(income_data)
##    state year   wage weeks_worked weekly_earnings q1_earnings q2_earnings
## 1     IN    A   1600            8       200.00000           0      0.0000
## 2     AZ    A  88000           52      1692.30769       22000  22000.0000
## 3     NV    A  35000           52       673.07692        8750   8750.0000
## 4     GA    A   2500            3       833.33333           0      0.0000
## 5     CO    A   2400           45        53.33333         320    693.3333
## 6     MN    A 120000            8     15000.00000           0      0.0000
## 7     IN    B   1600            8       200.00000           0      0.0000
## 8     AZ    B  88000           52      1692.30769       22000  22000.0000
## 9     NV    B  35000           52       673.07692        8750   8750.0000
## 10    GA    B   2500            3       833.33333           0      0.0000
## 11    CO    B   2400           45        53.33333         320    693.3333
## 12    MN    B 120000            8     15000.00000           0      0.0000
##    q3_earnings q4_earnings
## 1       0.0000   1600.0000
## 2   22000.0000  22000.0000
## 3    8750.0000   8750.0000
## 4       0.0000   2500.0000
## 5     693.3333    693.3333
## 6       0.0000 120000.0000
## 7       0.0000   1600.0000
## 8   22000.0000  22000.0000
## 9    8750.0000   8750.0000
## 10      0.0000   2500.0000
## 11    693.3333    693.3333
## 12      0.0000 120000.0000
income_data %>% mutate(benefits_amount =
  calc_weekly_state_quarterly(q1_earnings,
                              q2_earnings,
                              q3_earnings,
                              q4_earnings,
                              state,
                              weeks_worked,year) %>% map_dbl(1))
##    state year   wage weeks_worked weekly_earnings q1_earnings q2_earnings
## 1     IN    A   1600            8       200.00000           0      0.0000
## 2     AZ    A  88000           52      1692.30769       22000  22000.0000
## 3     NV    A  35000           52       673.07692        8750   8750.0000
## 4     GA    A   2500            3       833.33333           0      0.0000
## 5     CO    A   2400           45        53.33333         320    693.3333
## 6     MN    A 120000            8     15000.00000           0      0.0000
## 7     IN    B   1600            8       200.00000           0      0.0000
## 8     AZ    B  88000           52      1692.30769       22000  22000.0000
## 9     NV    B  35000           52       673.07692        8750   8750.0000
## 10    GA    B   2500            3       833.33333           0      0.0000
## 11    CO    B   2400           45        53.33333         320    693.3333
## 12    MN    B 120000            8     15000.00000           0      0.0000
##    q3_earnings q4_earnings benefits_amount
## 1       0.0000   1600.0000               0
## 2   22000.0000  22000.0000             240
## 3    8750.0000   8750.0000             350
## 4       0.0000   2500.0000               0
## 5     693.3333    693.3333               0
## 6       0.0000 120000.0000             740
## 7       0.0000   1600.0000               0
## 8   22000.0000  22000.0000            1723
## 9    8750.0000   8750.0000            1552
## 10      0.0000   2500.0000               0
## 11    693.3333    693.3333               0
## 12      0.0000 120000.0000            1626