# Analyze incarceration data (This is from the agent log, there is a separate incarceartion log)

rm(list=ls())


# Load R environment ---------

renv::activate()


# Load packages ---------

library(here)
## here() starts at /users/akhann16/code/cadre/data-analysis-plotting/Simulated-Data-Analysis/r
library(data.table)
library(yaml)
library(ggplot2)


# Read RDS file ------------

incarceration_release_log_env <- 
  readRDS("/users/akhann16/code/cadre/data-analysis-plotting/Simulated-Data-Analysis/r/incarceration-log-analysis/rds-outs/incarceration_log_env.RDS")

names(incarceration_release_log_env)
## [1] "incarceration_release_dt"
# Load data ------------

incarceration_release_dt <- incarceration_release_log_env[["incarceration_release_dt"]]


# Explore data ------------

str(incarceration_release_dt)
## Classes 'data.table' and 'data.frame':   2853 obs. of  8 variables:
##  $ tick          : int  1 2 10 24 38 72 81 89 106 106 ...
##  $ id            : int  5012 2438 1095 1095 5308 5012 1336 1336 3118 2438 ...
##  $ age           : int  23 84 22 22 33 23 33 33 77 84 ...
##  $ race          : chr  "Black" "White" "White" "White" ...
##  $ female        : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ alc_use_status: int  1 1 3 3 1 1 0 0 1 1 ...
##  $ smoking_status: chr  "Current" "Current" "Never" "Never" ...
##  $ event_type    : chr  "Incarceration" "Incarceration" "Incarceration" "Release" ...
##  - attr(*, ".internal.selfref")=<externalptr>
dim(incarceration_release_dt)
## [1] 2853    8
incarceration_release_dt[,.N]
## [1] 2853
# Filter incarceration and release data ------------

incarceration_dt <- incarceration_release_dt[event_type=="Incarceration",,]
release_dt <- incarceration_release_dt[event_type=="Release"]

incarceration_dt[,.N]
## [1] 1440
str(incarceration_dt)
## Classes 'data.table' and 'data.frame':   1440 obs. of  8 variables:
##  $ tick          : int  1 2 10 38 81 106 122 125 140 145 ...
##  $ id            : int  5012 2438 1095 5308 1336 3118 236 6435 4586 9402 ...
##  $ age           : int  23 84 22 33 33 77 64 52 31 22 ...
##  $ race          : chr  "Black" "White" "White" "Hispanic" ...
##  $ female        : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ alc_use_status: int  1 1 3 1 0 1 0 0 0 3 ...
##  $ smoking_status: chr  "Current" "Current" "Never" "Current" ...
##  $ event_type    : chr  "Incarceration" "Incarceration" "Incarceration" "Incarceration" ...
##  - attr(*, ".internal.selfref")=<externalptr>
dim(incarceration_dt)
## [1] 1440    8
release_dt[,.N]
## [1] 1413
str(release_dt)
## Classes 'data.table' and 'data.frame':   1413 obs. of  8 variables:
##  $ tick          : int  24 72 89 106 123 128 158 167 180 218 ...
##  $ id            : int  1095 5012 1336 2438 5308 3118 4586 6726 6044 8395 ...
##  $ age           : int  22 23 33 84 33 77 31 69 34 62 ...
##  $ race          : chr  "White" "Black" "Black" "White" ...
##  $ female        : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ alc_use_status: int  3 1 0 1 1 1 0 1 0 0 ...
##  $ smoking_status: chr  "Never" "Current" "Current" "Current" ...
##  $ event_type    : chr  "Release" "Release" "Release" "Release" ...
##  - attr(*, ".internal.selfref")=<externalptr>
dim(release_dt)
## [1] 1413    8
# Analyze state distributions for all incarceration events ------------

n_inc_events <- length(incarceration_dt$id)


incarceration_dt[, .(Count = .N, 
                     Proportion = .N/nrow(incarceration_dt)), 
                 by = .(race)]
##        race Count  Proportion
## 1:    Black   719 0.499305556
## 2:    White   423 0.293750000
## 3: Hispanic   290 0.201388889
## 4:    Asian     8 0.005555556
incarceration_dt[, .(Count = .N, 
                     Proportion = .N/nrow(incarceration_dt)), 
                 by = .(female)]
##    female Count Proportion
## 1:      0  1389 0.96458333
## 2:      1    51 0.03541667
incarceration_dt[, .(Count = .N, 
                     Proportion = .N/nrow(incarceration_dt)), 
                 by = .(alc_use_status)]
##    alc_use_status Count Proportion
## 1:              1   487 0.33819444
## 2:              3   481 0.33402778
## 3:              0   417 0.28958333
## 4:              2    55 0.03819444
incarceration_dt[, .(Count = .N, 
                     Proportion = .N/nrow(incarceration_dt)), 
                 by = .(smoking_status)]
##    smoking_status Count Proportion
## 1:        Current  1125 0.78125000
## 2:          Never   206 0.14305556
## 3:         Former   109 0.07569444
# Analyze state distributions for first-time incarceration events ------------

first_incarceration_dt <- incarceration_dt[order(id, tick), .SD[1], by = id]

head(first_incarceration_dt)
##     id  tick age     race female alc_use_status smoking_status    event_type
## 1:  45 10089  61 Hispanic      0              0          Never Incarceration
## 2:  54  6305  47    White      0              1         Former Incarceration
## 3:  67  5690  56    White      0              0        Current Incarceration
## 4:  98  3943  40    White      0              1        Current Incarceration
## 5: 106  2229  79    White      0              0        Current Incarceration
## 6: 168  1157  50    White      0              0         Former Incarceration
dim(first_incarceration_dt)
## [1] 573   8
n_first_inc_agents <- length(first_incarceration_dt$id)

first_incarceration_dt[, .(Count = .N, 
                     Proportion = .N/n_first_inc_agents), 
                 by = .(race)]
##        race Count Proportion
## 1: Hispanic   121  0.2111693
## 2:    White   277  0.4834206
## 3:    Black   169  0.2949389
## 4:    Asian     6  0.0104712
first_incarceration_dt[, .(Count = .N, 
                           Proportion = .N/n_first_inc_agents), 
                       by = .(female)]
##    female Count Proportion
## 1:      0   529 0.92321117
## 2:      1    44 0.07678883
first_incarceration_dt[, .(Count = .N, 
                           Proportion = .N/n_first_inc_agents), 
                       by = .(alc_use_status)][
                         order(alc_use_status)
                         ]
##    alc_use_status Count Proportion
## 1:              0   196 0.34205934
## 2:              1   254 0.44328098
## 3:              2    18 0.03141361
## 4:              3   105 0.18324607
first_incarceration_dt[, .(Count = .N, 
                           Proportion = .N/n_first_inc_agents), 
                       by = .(smoking_status)]
##    smoking_status Count Proportion
## 1:          Never   161  0.2809773
## 2:         Former    69  0.1204188
## 3:        Current   343  0.5986038
# Analyze state distributions of repeat incarceration events ------------

repeat_incarceration_dt <- incarceration_dt[order(id, tick), .SD[-1], by = id]
head(repeat_incarceration_dt, 100)
##        id  tick age     race female alc_use_status smoking_status    event_type
##   1:   67  6100  58    White      0              0        Current Incarceration
##   2:  177  2226  51    Black      0              1        Current Incarceration
##   3:  177  2593  52    Black      0              1        Current Incarceration
##   4:  177  3674  55    Black      0              1        Current Incarceration
##   5:  177  3872  56    Black      0              1        Current Incarceration
##   6:  205  2468  68    Black      0              1        Current Incarceration
##   7:  205  2645  69    Black      0              1        Current Incarceration
##   8:  205  2920  69    Black      0              1        Current Incarceration
##   9:  205  3931  72    Black      0              1        Current Incarceration
##  10:  205  4786  75    Black      0              1        Current Incarceration
##  11:  236  6902  83    Black      0              0          Never Incarceration
##  12:  254  3419  30    Black      0              1        Current Incarceration
##  13:  254  4237  33    Black      0              1        Current Incarceration
##  14:  254  4477  33    Black      0              1        Current Incarceration
##  15:  325  9455  47    Black      0              0        Current Incarceration
##  16:  325 10224  49    Black      0              0        Current Incarceration
##  17:  332 10200  73    Black      0              1        Current Incarceration
##  18:  332 10739  75    Black      0              1        Current Incarceration
##  19:  470  6522  61 Hispanic      0              2        Current Incarceration
##  20:  535  5622  78 Hispanic      0              3        Current Incarceration
##  21:  535  5916  79 Hispanic      0              3        Current Incarceration
##  22:  535  6192  80 Hispanic      0              3        Current Incarceration
##  23:  535  6450  80 Hispanic      0              3        Current Incarceration
##  24:  535  6760  81 Hispanic      0              3        Current Incarceration
##  25:  535  7604  84 Hispanic      0              3         Former Incarceration
##  26:  594  1471  60    Black      0              0        Current Incarceration
##  27:  594  2032  61    Black      0              0        Current Incarceration
##  28:  594  2184  62    Black      0              0        Current Incarceration
##  29:  594  2721  63    Black      0              0        Current Incarceration
##  30:  594  3061  64    Black      0              0        Current Incarceration
##  31:  594  3658  66    Black      0              0        Current Incarceration
##  32:  776  1229  47 Hispanic      0              3        Current Incarceration
##  33:  776  1447  48 Hispanic      0              3        Current Incarceration
##  34:  776  1472  48 Hispanic      0              3        Current Incarceration
##  35:  776  1750  49 Hispanic      0              3        Current Incarceration
##  36:  776  1821  49 Hispanic      0              3        Current Incarceration
##  37:  776  1935  49 Hispanic      0              3        Current Incarceration
##  38:  776  2682  51 Hispanic      0              3        Current Incarceration
##  39:  776  2829  52 Hispanic      0              3        Current Incarceration
##  40:  776  3758  54 Hispanic      0              3        Current Incarceration
##  41:  776  3854  55 Hispanic      0              3        Current Incarceration
##  42:  776  4052  55 Hispanic      0              3        Current Incarceration
##  43:  776  4320  56 Hispanic      0              3        Current Incarceration
##  44:  776  4809  57 Hispanic      0              3        Current Incarceration
##  45:  776  5026  58 Hispanic      0              3         Former Incarceration
##  46:  776  5578  59 Hispanic      0              3        Current Incarceration
##  47:  776  6058  61 Hispanic      0              3        Current Incarceration
##  48:  776  6273  61 Hispanic      0              3        Current Incarceration
##  49:  776  6630  62 Hispanic      0              3        Current Incarceration
##  50:  776  6706  63 Hispanic      0              3        Current Incarceration
##  51:  820  7158  50    White      0              3        Current Incarceration
##  52:  873  5334  67 Hispanic      0              1        Current Incarceration
##  53:  873  5364  67 Hispanic      0              1        Current Incarceration
##  54:  940 10483  72    Black      0              0        Current Incarceration
##  55:  940 10636  72    Black      0              0        Current Incarceration
##  56:  941  2943  79    White      0              0        Current Incarceration
##  57:  975  7475  45    Black      0              3          Never Incarceration
##  58:  975  8060  46    Black      0              3          Never Incarceration
##  59:  975  9127  49    Black      0              3          Never Incarceration
##  60:  975  9689  51    Black      0              3          Never Incarceration
##  61:  975 10821  54    Black      0              3          Never Incarceration
##  62:  981   331  40    Black      0              3        Current Incarceration
##  63:  981   378  40    Black      0              3        Current Incarceration
##  64:  981  2035  44    Black      0              3        Current Incarceration
##  65:  981  2089  45    Black      0              3        Current Incarceration
##  66:  981  2230  45    Black      0              3        Current Incarceration
##  67:  981  2330  45    Black      0              3        Current Incarceration
##  68:  981  2401  45    Black      0              3        Current Incarceration
##  69:  981  2514  46    Black      0              3        Current Incarceration
##  70:  981  3592  49    Black      0              3         Former Incarceration
##  71:  981  4019  50    Black      0              3        Current Incarceration
##  72:  981  4080  50    Black      0              3        Current Incarceration
##  73:  981  4403  51    Black      0              3        Current Incarceration
##  74:  981  4471  51    Black      0              3        Current Incarceration
##  75:  981  6917  58    Black      0              3        Current Incarceration
##  76:  981  7122  58    Black      0              3        Current Incarceration
##  77:  981  7155  58    Black      0              3        Current Incarceration
##  78:  981  7354  59    Black      0              3        Current Incarceration
##  79:  981  7573  60    Black      0              3        Current Incarceration
##  80:  981  7588  60    Black      0              3        Current Incarceration
##  81:  981  7688  60    Black      0              3        Current Incarceration
##  82:  981  7722  60    Black      0              3        Current Incarceration
##  83:  981  7879  60    Black      0              3        Current Incarceration
##  84:  981  8046  61    Black      0              3        Current Incarceration
##  85:  981  8194  61    Black      0              3        Current Incarceration
##  86:  981  9134  64    Black      0              3         Former Incarceration
##  87: 1064  5962  73    White      0              1         Former Incarceration
##  88: 1290  6033  67    White      0              3        Current Incarceration
##  89: 1290  6465  68    White      0              3        Current Incarceration
##  90: 1290  6655  68    White      0              3        Current Incarceration
##  91: 1290  6879  69    White      0              3        Current Incarceration
##  92: 1290  7055  69    White      0              3        Current Incarceration
##  93: 1290  7935  72    White      0              3        Current Incarceration
##  94: 1290  8572  73    White      0              3        Current Incarceration
##  95: 1290  8650  74    White      0              3        Current Incarceration
##  96: 1290  8835  74    White      0              3        Current Incarceration
##  97: 1322  5401  58    White      0              3        Current Incarceration
##  98: 1322  5474  58    White      0              3        Current Incarceration
##  99: 1329  3074  39    Black      0              1         Former Incarceration
## 100: 1329  3312  40    Black      0              1        Current Incarceration
##        id  tick age     race female alc_use_status smoking_status    event_type
dim(repeat_incarceration_dt)
## [1] 867   8
n_repeat_inc_events <- nrow(repeat_incarceration_dt)

repeat_incarceration_dt[, .(Count = .N, 
                            Proportion = .N/n_repeat_inc_events), 
                        by = .(race)]
##        race Count  Proportion
## 1:    White   146 0.168396770
## 2:    Black   550 0.634371396
## 3: Hispanic   169 0.194925029
## 4:    Asian     2 0.002306805
repeat_incarceration_dt[, .(Count = .N, 
                            Proportion = .N/n_repeat_inc_events), 
                        by = .(female)]
##    female Count  Proportion
## 1:      0   860 0.991926182
## 2:      1     7 0.008073818
repeat_incarceration_dt[, .(Count = .N, 
                            Proportion = .N/n_repeat_inc_events), 
                        by = .(alc_use_status)][
                          order(alc_use_status)
                        ]
##    alc_use_status Count Proportion
## 1:              0   221 0.25490196
## 2:              1   233 0.26874279
## 3:              2    37 0.04267589
## 4:              3   376 0.43367935
repeat_incarceration_dt[, .(Count = .N, 
                            Proportion = .N/n_repeat_inc_events), 
                        by = .(smoking_status)]
##    smoking_status Count Proportion
## 1:        Current   782 0.90196078
## 2:          Never    45 0.05190311
## 3:         Former    40 0.04613610
# Analyze state distributions for all release events ------------

n_rel_events <- length(release_dt$id)


release_dt[, .(Count = .N, 
                     Proportion = .N/nrow(release_dt)), 
                 by = .(race)]
##        race Count  Proportion
## 1:    White   416 0.294409059
## 2:    Black   702 0.496815287
## 3: Hispanic   287 0.203113942
## 4:    Asian     8 0.005661713
release_dt[, .(Count = .N, 
                     Proportion = .N/nrow(release_dt)), 
                 by = .(female)]
##    female Count Proportion
## 1:      0  1363  0.9646143
## 2:      1    50  0.0353857
release_dt[, .(Count = .N, 
                     Proportion = .N/nrow(release_dt)), 
                 by = .(alc_use_status)]
##    alc_use_status Count Proportion
## 1:              3   471 0.33333333
## 2:              1   475 0.33616419
## 3:              0   412 0.29157820
## 4:              2    55 0.03892427
release_dt[, .(Count = .N, 
                     Proportion = .N/nrow(release_dt)), 
                 by = .(smoking_status)]
##    smoking_status Count Proportion
## 1:          Never   200 0.14154282
## 2:        Current  1107 0.78343949
## 3:         Former   106 0.07501769
# Analyze state distributions for first-time release events ------------

first_release_dt <- release_dt[order(id, tick), .SD[1], by = id]

head(first_release_dt)
##     id  tick age     race female alc_use_status smoking_status event_type
## 1:  45 10110  61 Hispanic      0              0          Never    Release
## 2:  54  6328  48    White      0              1         Former    Release
## 3:  67  5826  57    White      0              0        Current    Release
## 4:  98  3953  40    White      0              1        Current    Release
## 5: 106  2256  79    White      0              0        Current    Release
## 6: 168  1170  50    White      0              0         Former    Release
dim(first_release_dt)
## [1] 567   8
n_first_rel_agents <- length(first_release_dt$id)

first_release_dt[, .(Count = .N, 
                           Proportion = .N/n_first_rel_agents), 
                       by = .(race)]
##        race Count Proportion
## 1: Hispanic   121 0.21340388
## 2:    White   274 0.48324515
## 3:    Black   166 0.29276896
## 4:    Asian     6 0.01058201
first_release_dt[, .(Count = .N, 
                           Proportion = .N/n_first_rel_agents), 
                       by = .(female)]
##    female Count Proportion
## 1:      0   524 0.92416226
## 2:      1    43 0.07583774
first_release_dt[, .(Count = .N, 
                           Proportion = .N/n_first_rel_agents), 
                       by = .(alc_use_status)][
                         order(alc_use_status)
                       ]
##    alc_use_status Count Proportion
## 1:              0   195 0.34391534
## 2:              1   250 0.44091711
## 3:              2    18 0.03174603
## 4:              3   104 0.18342152
first_release_dt[, .(Count = .N, 
                           Proportion = .N/n_first_rel_agents), 
                       by = .(smoking_status)]
##    smoking_status Count Proportion
## 1:          Never   156  0.2751323
## 2:         Former    68  0.1199295
## 3:        Current   343  0.6049383
# Analyze state distributions of repeat release events ------------

repeat_release_dt <- release_dt[order(id, tick), .SD[-1], by = id]
head(repeat_release_dt, 100)
##        id  tick age     race female alc_use_status smoking_status event_type
##   1:   67  6214  58    White      0              0        Current    Release
##   2:  177  2360  51    Black      0              1        Current    Release
##   3:  177  2666  52    Black      0              1        Current    Release
##   4:  177  3697  55    Black      0              1        Current    Release
##   5:  177  3930  56    Black      0              1        Current    Release
##   6:  205  2482  68    Black      0              1        Current    Release
##   7:  205  2668  69    Black      0              1        Current    Release
##   8:  205  3129  70    Black      0              1        Current    Release
##   9:  205  4075  73    Black      0              1        Current    Release
##  10:  205  4805  75    Black      0              1        Current    Release
##  11:  236  7056  83    Black      0              0          Never    Release
##  12:  254  3431  30    Black      0              1        Current    Release
##  13:  254  4297  33    Black      0              1        Current    Release
##  14:  254  4489  33    Black      0              1        Current    Release
##  15:  325  9476  47    Black      0              0        Current    Release
##  16:  325 10252  49    Black      0              0        Current    Release
##  17:  332 10322  74    Black      0              1        Current    Release
##  18:  332 10844  75    Black      0              1        Current    Release
##  19:  470  6664  61 Hispanic      0              2        Current    Release
##  20:  535  5774  79 Hispanic      0              3        Current    Release
##  21:  535  6054  79 Hispanic      0              3        Current    Release
##  22:  535  6353  80 Hispanic      0              3        Current    Release
##  23:  535  6558  81 Hispanic      0              3        Current    Release
##  24:  535  6775  81 Hispanic      0              3        Current    Release
##  25:  535  7614  84 Hispanic      0              3         Former    Release
##  26:  594  1576  60    Black      0              0        Current    Release
##  27:  594  2041  61    Black      0              0        Current    Release
##  28:  594  2283  62    Black      0              0        Current    Release
##  29:  594  2915  64    Black      0              0        Current    Release
##  30:  594  3075  64    Black      0              0        Current    Release
##  31:  594  3790  66    Black      0              0        Current    Release
##  32:  776  1394  48 Hispanic      0              3        Current    Release
##  33:  776  1454  48 Hispanic      0              3        Current    Release
##  34:  776  1552  48 Hispanic      0              3        Current    Release
##  35:  776  1784  49 Hispanic      0              3        Current    Release
##  36:  776  1843  49 Hispanic      0              3        Current    Release
##  37:  776  1972  50 Hispanic      0              3        Current    Release
##  38:  776  2691  52 Hispanic      0              3        Current    Release
##  39:  776  2954  52 Hispanic      0              3        Current    Release
##  40:  776  3838  55 Hispanic      0              3        Current    Release
##  41:  776  4011  55 Hispanic      0              3        Current    Release
##  42:  776  4188  56 Hispanic      0              3        Current    Release
##  43:  776  4451  56 Hispanic      0              3        Current    Release
##  44:  776  4832  57 Hispanic      0              3        Current    Release
##  45:  776  5107  58 Hispanic      0              3         Former    Release
##  46:  776  5603  59 Hispanic      0              3        Current    Release
##  47:  776  6076  61 Hispanic      0              3        Current    Release
##  48:  776  6411  62 Hispanic      0              3        Current    Release
##  49:  776  6651  62 Hispanic      0              3        Current    Release
##  50:  776  6725  63 Hispanic      0              3        Current    Release
##  51:  820  7257  50    White      0              3        Current    Release
##  52:  873  5359  67 Hispanic      0              1        Current    Release
##  53:  873  5381  67 Hispanic      0              1        Current    Release
##  54:  940 10632  72    Black      0              0        Current    Release
##  55:  940 10653  72    Black      0              0        Current    Release
##  56:  941  2970  80    White      0              0        Current    Release
##  57:  975  7492  45    Black      0              3          Never    Release
##  58:  975  8337  47    Black      0              3          Never    Release
##  59:  975  9137  49    Black      0              3          Never    Release
##  60:  975  9856  51    Black      0              3          Never    Release
##  61:  975 10892  54    Black      0              3          Never    Release
##  62:  981   351  40    Black      0              3        Current    Release
##  63:  981  2006  44    Black      0              3        Current    Release
##  64:  981  2084  45    Black      0              3        Current    Release
##  65:  981  2221  45    Black      0              3        Current    Release
##  66:  981  2327  45    Black      0              3        Current    Release
##  67:  981  2340  45    Black      0              3        Current    Release
##  68:  981  2409  45    Black      0              3        Current    Release
##  69:  981  2674  46    Black      0              3        Current    Release
##  70:  981  3608  49    Black      0              3         Former    Release
##  71:  981  4031  50    Black      0              3        Current    Release
##  72:  981  4201  50    Black      0              3        Current    Release
##  73:  981  4422  51    Black      0              3        Current    Release
##  74:  981  6548  57    Black      0              3        Current    Release
##  75:  981  6954  58    Black      0              3        Current    Release
##  76:  981  7140  58    Black      0              3        Current    Release
##  77:  981  7229  59    Black      0              3        Current    Release
##  78:  981  7544  59    Black      0              3        Current    Release
##  79:  981  7585  60    Black      0              3        Current    Release
##  80:  981  7672  60    Black      0              3        Current    Release
##  81:  981  7707  60    Black      0              3        Current    Release
##  82:  981  7810  60    Black      0              3        Current    Release
##  83:  981  8044  61    Black      0              3        Current    Release
##  84:  981  8090  61    Black      0              3        Current    Release
##  85:  981  8205  61    Black      0              3        Current    Release
##  86: 1064  5980  73    White      0              1         Former    Release
##  87: 1290  6059  67    White      0              3        Current    Release
##  88: 1290  6599  68    White      0              3        Current    Release
##  89: 1290  6710  68    White      0              3        Current    Release
##  90: 1290  6886  69    White      0              3        Current    Release
##  91: 1290  7166  70    White      0              3        Current    Release
##  92: 1290  7960  72    White      0              3        Current    Release
##  93: 1290  8613  74    White      0              3        Current    Release
##  94: 1290  8668  74    White      0              3        Current    Release
##  95: 1290  9128  75    White      0              3        Current    Release
##  96: 1322  5414  58    White      0              3        Current    Release
##  97: 1322  5618  59    White      0              3        Current    Release
##  98: 1329  3088  39    Black      0              1         Former    Release
##  99: 1329  3352  40    Black      0              1        Current    Release
## 100: 1329  3428  40    Black      0              1        Current    Release
##        id  tick age     race female alc_use_status smoking_status event_type
dim(repeat_release_dt)
## [1] 846   8
n_repeat_rel_events <- nrow(repeat_release_dt)

repeat_release_dt[, .(Count = .N, 
                            Proportion = .N/n_repeat_rel_events), 
                        by = .(race)]
##        race Count  Proportion
## 1:    White   142 0.167848700
## 2:    Black   536 0.633569740
## 3: Hispanic   166 0.196217494
## 4:    Asian     2 0.002364066
repeat_release_dt[, .(Count = .N, 
                            Proportion = .N/n_repeat_rel_events), 
                        by = .(female)]
##    female Count  Proportion
## 1:      0   839 0.991725768
## 2:      1     7 0.008274232
repeat_release_dt[, .(Count = .N, 
                            Proportion = .N/n_repeat_rel_events), 
                        by = .(alc_use_status)][
                          order(alc_use_status)
                        ]
##    alc_use_status Count Proportion
## 1:              0   217 0.25650118
## 2:              1   225 0.26595745
## 3:              2    37 0.04373522
## 4:              3   367 0.43380615
repeat_release_dt[, .(Count = .N, 
                            Proportion = .N/n_repeat_rel_events), 
                        by = .(smoking_status)]
##    smoking_status Count Proportion
## 1:        Current   764 0.90307329
## 2:          Never    44 0.05200946
## 3:         Former    38 0.04491726
# Sort data by id --------

incarceration_release_dt[order(id)]
##        tick    id age     race female alc_use_status smoking_status
##    1: 10089    45  61 Hispanic      0              0          Never
##    2: 10110    45  61 Hispanic      0              0          Never
##    3:  6305    54  47    White      0              1         Former
##    4:  6328    54  48    White      0              1         Former
##    5:  5690    67  56    White      0              0        Current
##   ---                                                              
## 2849: 10926 14679  43    Black      0              1        Current
## 2850: 10361 14715  37 Hispanic      1              1        Current
## 2851: 10373 14715  37 Hispanic      1              1        Current
## 2852: 10761 15310  35    White      0              0        Current
## 2853: 10925 15310  36    White      0              0        Current
##          event_type
##    1: Incarceration
##    2:       Release
##    3: Incarceration
##    4:       Release
##    5: Incarceration
##   ---              
## 2849:       Release
## 2850: Incarceration
## 2851:       Release
## 2852: Incarceration
## 2853:       Release