library("data.table")
library("ggplot2")

rm(list=ls())

# uses the trying-fixes branch
#setwd("~/data/akhann16/cadre_simulated_data/output_20231106_082519") # with transitions
setwd("~/data/akhann16/cadre_simulated_data/output_20231106_082826") # frozen alc transitions (still the post-release )

## trying-fixes branch WITHOUT POST_RELEASE ALCOHOL STATE REASSIGNMENT
#setwd("~/data/nrousell/cadre_simulated_data//output_20231128_194046")
getwd()
## [1] "/oscar/data/akhann16/akhann16/cadre_simulated_data/output_20231106_082826"
list.files()
## [1] "agent_log.csv"   "counts_log.csv"  "network_log.csv" "parameters.txt"
dt <- fread("agent_log.csv")
dt
##            tick    id age     race female alc_use_status smoking_status
##        1:     1  2600  84    White      1              1          Never
##        2:     1     0  48 Hispanic      0              1          Never
##        3:     1     1  47 Hispanic      0              2          Never
##        4:     1     2  54    Black      1              0          Never
##        5:     1     3  22 Hispanic      1              1          Never
##       ---                                                              
## 10965660: 10950 15659  55    White      1              1          Never
## 10965661: 10950 15660  25    White      1              1          Never
## 10965662: 10950 15661  23 Hispanic      1              0          Never
## 10965663: 10950 15662  65    White      0              1          Never
## 10965664: 10950 15663  30    White      1              1          Never
##           last_incarceration_tick last_release_tick
##        1:                      -1                -1
##        2:                      -1                -1
##        3:                      -1                -1
##        4:                      -1                -1
##        5:                      -1                -1
##       ---                                          
## 10965660:                      -1                -1
## 10965661:                      -1                -1
## 10965662:                      -1                -1
## 10965663:                      -1                -1
## 10965664:                      -1                -1
##           current_incarceration_status entry_at_tick exit_at_tick
##        1:                            0             0            1
##        2:                            0             0           -1
##        3:                            0             0           -1
##        4:                            0             0           -1
##        5:                            0             0           -1
##       ---                                                        
## 10965660:                            0         10940           -1
## 10965661:                            0         10946           -1
## 10965662:                            0         10948           -1
## 10965663:                            0         10948           -1
## 10965664:                            0         10948           -1
##           n_incarcerations n_releases n_smkg_stat_trans n_alc_use_stat_trans
##        1:                0          0                 0                    0
##        2:                0          0                 0                    0
##        3:                0          0                 0                    0
##        4:                0          0                 0                    0
##        5:                0          0                 0                    0
##       ---                                                                   
## 10965660:                0          0                 0                    0
## 10965661:                0          0                 0                    0
## 10965662:                0          0                 0                    0
## 10965663:                0          0                 0                    0
## 10965664:                0          0                 0                    0
n_ticks = max(dt$tick)


# remove exit logs as they aren't relevant here
no_exits <- dt[exit_at_tick == -1]
n_agents = no_exits[tick == 1, .N]

## Alcohol rates over entire population
alc_use_rates <- no_exits[,.N,by=.(tick,alc_use_status)]
alc_use_rates$N = alc_use_rates$N / n_agents

ggplot(data=alc_use_rates, aes(x=tick,y=N, color=alc_use_status)) + 
  geom_point()

## alcohol rates over never-incarcerated population for first 1000 ticks
never_incarcerated_ids <- no_exits[(tick == 1001) & (n_incarcerations == 0)]$id
alive_at_model_start <- no_exits[tick == 1]$id
never_incarcerated_ids <- never_incarcerated_ids[never_incarcerated_ids %in% alive_at_model_start]

never_incarcerated_alc_use <- no_exits[(tick <= 1001) & (id %in% never_incarcerated_ids), .N, by=.(tick, alc_use_status)]
never_incarcerated_alc_use$N = never_incarcerated_alc_use$N / length(never_incarcerated_ids)

never_incarcerated_alc_use
##      tick alc_use_status          N
##   1:    1              1 0.54971189
##   2:    1              2 0.05165008
##   3:    1              0 0.35285490
##   4:    1              3 0.04578313
##   5:   11              1 0.54971189
##  ---                               
## 400:  991              3 0.04578313
## 401: 1001              1 0.54971189
## 402: 1001              2 0.05165008
## 403: 1001              0 0.35285490
## 404: 1001              3 0.04578313
ggplot(data=never_incarcerated_alc_use, aes(x=tick,y=N, color=alc_use_status)) + 
  geom_point()

# no change in the never-incarcerated population with a fixed population
never_incarcerated_alc_use[tick == 1]
##    tick alc_use_status          N
## 1:    1              1 0.54971189
## 2:    1              2 0.05165008
## 3:    1              0 0.35285490
## 4:    1              3 0.04578313
never_incarcerated_alc_use[tick == 1001]
##    tick alc_use_status          N
## 1: 1001              1 0.54971189
## 2: 1001              2 0.05165008
## 3: 1001              0 0.35285490
## 4: 1001              3 0.04578313
## alcohol rates for first 1000 in agents with history of incarceration by tick 1000
incarceration_history_ids <- no_exits[(tick == 1001) & (n_incarcerations > 0)]$id

incarceration_history_ids <- no_exits[(tick == 1) & (id %in% incarceration_history_ids)]$id
length(incarceration_history_ids)
## [1] 66
incarcerated_alc_use <- no_exits[(tick <= 1001) & (id %in% incarceration_history_ids), .N, by=.(tick, alc_use_status)]
incarcerated_alc_use$N = incarcerated_alc_use$N / length(incarceration_history_ids)

incarcerated_alc_use
##      tick alc_use_status          N
##   1:    1              3 0.27272727
##   2:    1              2 0.03030303
##   3:    1              1 0.39393939
##   4:    1              0 0.30303030
##   5:   11              3 0.27272727
##  ---                               
## 400:  991              2 0.01515152
## 401: 1001              0 0.34848485
## 402: 1001              1 0.53030303
## 403: 1001              3 0.10606061
## 404: 1001              2 0.01515152
# "transitions" are abrupt--whenever an agent is released from incarceration
ggplot(data=incarcerated_alc_use, aes(x=tick,y=N, color=alc_use_status)) + 
  geom_point()

## AUD rate after incarceration
released <- no_exits[(current_incarceration_status != 1) & (last_release_tick > 0) & (n_releases == 1)]
released[,ticks_since_release := tick-last_release_tick]

ticks_300_after_ids <- released[(ticks_since_release > 299) & (ticks_since_release < 309)]$id
length(ticks_300_after_ids) # 127
## [1] 430
length(unique(ticks_300_after_ids)) # 127
## [1] 430
fixed_released_pop <- released[id %in% ticks_300_after_ids]

bin_size = 10
time_horizon = 300
bin_start <- seq(0, time_horizon-bin_size, bin_size)
bin_end <- seq(bin_size, time_horizon, bin_size)
bins <- data.frame(bin_start, bin_end)

x <- c()

for (row in 1:nrow(bins)) {
  b_start <- bins$bin_start[row]
  b_end <- bins$bin_end[row]
  within_bin <- fixed_released_pop[(ticks_since_release >= b_start) & (ticks_since_release < b_end)]
  total = nrow(within_bin)
  print(total)
  current_alc = within_bin[alc_use_status==3, .N / total]
  x <- append(x, current_alc)
}
## [1] 373
## [1] 430
## [1] 430
## [1] 430
## [1] 430
## [1] 430
## [1] 430
## [1] 430
## [1] 430
## [1] 430
## [1] 430
## [1] 430
## [1] 430
## [1] 430
## [1] 430
## [1] 430
## [1] 430
## [1] 430
## [1] 430
## [1] 430
## [1] 430
## [1] 430
## [1] 430
## [1] 430
## [1] 430
## [1] 430
## [1] 430
## [1] 430
## [1] 430
## [1] 430
plot_df <- data.frame(bin_start,x)

# this graph shows that agents with a history of incarceration are also not transitioning,
# they only re-assign states (if update_alc_use_post_release is called)
# * first point is higher as the sample is slightly smaller
ggplot(plot_df, mapping = aes(x = bin_start + 5, y = x)) + 
  geom_point()