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/nrousell/cadre_simulated_data/output_20231128_194046"
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 0 69 Black 1 1 Former
## 2: 1 1 37 Black 1 1 Never
## 3: 1 2 76 Hispanic 1 1 Never
## 4: 1 3 41 Hispanic 0 0 Never
## 5: 1 4 81 White 1 1 Former
## ---
## 7603668: 7591 13667 30 White 0 1 Former
## 7603669: 7591 13668 73 White 1 1 Current
## 7603670: 7591 13669 83 White 0 1 Current
## 7603671: 7591 13670 47 Asian 0 1 Never
## 7603672: 7591 13671 81 White 1 1 Current
## last_incarceration_tick last_release_tick current_incarceration_status
## 1: -1 -1 0
## 2: -1 -1 0
## 3: -1 -1 0
## 4: -1 -1 0
## 5: -1 -1 0
## ---
## 7603668: -1 -1 0
## 7603669: -1 -1 0
## 7603670: -1 -1 0
## 7603671: -1 -1 0
## 7603672: -1 -1 0
## entry_at_tick exit_at_tick n_incarcerations n_releases
## 1: 0 -1 0 0
## 2: 0 -1 0 0
## 3: 0 -1 0 0
## 4: 0 -1 0 0
## 5: 0 -1 0 0
## ---
## 7603668: 7581 -1 0 0
## 7603669: 7583 -1 0 0
## 7603670: 7583 -1 0 0
## 7603671: 7585 -1 0 0
## 7603672: 7585 -1 0 0
## n_smkg_stat_trans n_alc_use_stat_trans
## 1: 0 0
## 2: 0 0
## 3: 0 0
## 4: 0 0
## 5: 0 0
## ---
## 7603668: 0 0
## 7603669: 0 0
## 7603670: 0 0
## 7603671: 0 0
## 7603672: 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.55491329
## 2: 1 0 0.34776668
## 3: 1 3 0.04813452
## 4: 1 2 0.04918550
## 5: 11 1 0.55491329
## ---
## 400: 991 2 0.04918550
## 401: 1001 1 0.55491329
## 402: 1001 0 0.34776668
## 403: 1001 3 0.04813452
## 404: 1001 2 0.04918550
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.55491329
## 2: 1 0 0.34776668
## 3: 1 3 0.04813452
## 4: 1 2 0.04918550
never_incarcerated_alc_use[tick == 1001]
## tick alc_use_status N
## 1: 1001 1 0.55491329
## 2: 1001 0 0.34776668
## 3: 1001 3 0.04813452
## 4: 1001 2 0.04918550
## 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] 57
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 1 0.43859649
## 2: 1 0 0.24561404
## 3: 1 3 0.29824561
## 4: 1 2 0.01754386
## 5: 11 1 0.43859649
## ---
## 400: 991 2 0.01754386
## 401: 1001 1 0.43859649
## 402: 1001 0 0.24561404
## 403: 1001 3 0.29824561
## 404: 1001 2 0.01754386
# "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] 272
length(unique(ticks_300_after_ids)) # 127
## [1] 272
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] 259
## [1] 272
## [1] 272
## [1] 272
## [1] 272
## [1] 272
## [1] 272
## [1] 272
## [1] 272
## [1] 272
## [1] 272
## [1] 272
## [1] 272
## [1] 272
## [1] 272
## [1] 272
## [1] 272
## [1] 272
## [1] 272
## [1] 272
## [1] 272
## [1] 272
## [1] 272
## [1] 272
## [1] 272
## [1] 272
## [1] 272
## [1] 272
## [1] 272
## [1] 272
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()
