# 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':   2427 obs. of  8 variables:
##  $ tick          : int  60 76 80 106 114 114 128 129 132 144 ...
##  $ id            : int  4866 5738 4866 4226 6245 7796 7796 8538 4226 7785 ...
##  $ age           : int  20 69 20 33 72 83 83 47 33 53 ...
##  $ race          : chr  "White" "Black" "White" "Hispanic" ...
##  $ female        : int  0 0 0 1 0 0 0 0 1 0 ...
##  $ alc_use_status: int  3 3 3 3 1 1 1 1 3 3 ...
##  $ smoking_status: chr  "Current" "Current" "Current" "Never" ...
##  $ event_type    : chr  "Incarceration" "Incarceration" "Release" "Incarceration" ...
##  - attr(*, ".internal.selfref")=<externalptr>
dim(incarceration_release_dt)
## [1] 2427    8
incarceration_release_dt[,.N]
## [1] 2427
# 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] 1227
str(incarceration_dt)
## Classes 'data.table' and 'data.frame':   1227 obs. of  8 variables:
##  $ tick          : int  60 76 106 114 114 129 144 146 171 174 ...
##  $ id            : int  4866 5738 4226 6245 7796 8538 7785 4861 8538 8638 ...
##  $ age           : int  20 69 33 72 83 47 53 82 47 66 ...
##  $ race          : chr  "White" "Black" "Hispanic" "Hispanic" ...
##  $ female        : int  0 0 1 0 0 0 0 0 0 0 ...
##  $ alc_use_status: int  3 3 3 1 1 1 3 1 1 3 ...
##  $ smoking_status: chr  "Current" "Current" "Never" "Current" ...
##  $ event_type    : chr  "Incarceration" "Incarceration" "Incarceration" "Incarceration" ...
##  - attr(*, ".internal.selfref")=<externalptr>
dim(incarceration_dt)
## [1] 1227    8
release_dt[,.N]
## [1] 1200
str(release_dt)
## Classes 'data.table' and 'data.frame':   1200 obs. of  8 variables:
##  $ tick          : int  80 128 132 151 167 206 214 221 225 244 ...
##  $ id            : int  4866 7796 4226 8538 7785 7785 6245 8091 4861 2906 ...
##  $ age           : int  20 83 33 47 53 53 72 29 83 39 ...
##  $ race          : chr  "White" "Black" "Hispanic" "Black" ...
##  $ female        : int  0 0 1 0 0 0 0 0 0 0 ...
##  $ alc_use_status: int  3 1 3 1 3 3 1 1 1 3 ...
##  $ smoking_status: chr  "Current" "Current" "Never" "Current" ...
##  $ event_type    : chr  "Release" "Release" "Release" "Release" ...
##  - attr(*, ".internal.selfref")=<externalptr>
dim(release_dt)
## [1] 1200    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:    White   311 0.253463733
## 2:    Black   744 0.606356968
## 3: Hispanic   164 0.133659332
## 4:    Asian     8 0.006519967
incarceration_dt[, .(Count = .N, 
                     Proportion = .N/nrow(incarceration_dt)), 
                 by = .(female)]
##    female Count Proportion
## 1:      0  1184 0.96495518
## 2:      1    43 0.03504482
incarceration_dt[, .(Count = .N, 
                     Proportion = .N/nrow(incarceration_dt)), 
                 by = .(alc_use_status)]
##    alc_use_status Count Proportion
## 1:              3   447 0.36430318
## 2:              1   580 0.47269764
## 3:              2   108 0.08801956
## 4:              0    92 0.07497963
incarceration_dt[, .(Count = .N, 
                     Proportion = .N/nrow(incarceration_dt)), 
                 by = .(smoking_status)]
##    smoking_status Count Proportion
## 1:        Current   991 0.80766096
## 2:          Never   167 0.13610432
## 3:         Former    69 0.05623472
# 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:  51  620  42 Black      0              1        Current Incarceration
## 2:  56 1601  64 Black      0              3          Never Incarceration
## 3:  58 2782  65 Black      0              1        Current Incarceration
## 4:  71 5702  57 White      0              3        Current Incarceration
## 5: 115 8132  65 White      0              3        Current Incarceration
## 6: 140 7020  63 Black      0              3        Current Incarceration
dim(first_incarceration_dt)
## [1] 538   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:    Black   172 0.31970260
## 2:    White   252 0.46840149
## 3: Hispanic   107 0.19888476
## 4:    Asian     7 0.01301115
first_incarceration_dt[, .(Count = .N, 
                           Proportion = .N/n_first_inc_agents), 
                       by = .(female)]
##    female Count Proportion
## 1:      0   498 0.92565056
## 2:      1    40 0.07434944
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    49 0.09107807
## 2:              1   282 0.52416357
## 3:              2    51 0.09479554
## 4:              3   156 0.28996283
first_incarceration_dt[, .(Count = .N, 
                           Proportion = .N/n_first_inc_agents), 
                       by = .(smoking_status)]
##    smoking_status Count Proportion
## 1:        Current   341 0.63382900
## 2:          Never   151 0.28066914
## 3:         Former    46 0.08550186
# 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:   51   900  43    Black      0              1        Current Incarceration
##   2:   51   919  43    Black      0              1        Current Incarceration
##   3:   51  1717  45    Black      0              1        Current Incarceration
##   4:   51  2744  48    Black      0              1        Current Incarceration
##   5:   51  5148  54    Black      0              1        Current Incarceration
##   6:   51  5442  55    Black      0              1        Current Incarceration
##   7:   51  5819  56    Black      0              1        Current Incarceration
##   8:   58  3885  68    Black      0              1        Current Incarceration
##   9:   58  4267  69    Black      0              0        Current Incarceration
##  10:   58  4738  70    Black      0              0        Current Incarceration
##  11:  140  7524  65    Black      0              3        Current Incarceration
##  12:  140  8045  66    Black      0              2        Current Incarceration
##  13:  191  8977  82 Hispanic      0              1        Current Incarceration
##  14:  195  5514  66    White      0              3        Current Incarceration
##  15:  195  5732  67    White      0              3        Current Incarceration
##  16:  271  7793  47    Black      0              3          Never Incarceration
##  17:  271  8147  48    Black      0              3          Never Incarceration
##  18:  284  6253  57 Hispanic      0              1        Current Incarceration
##  19:  325  3850  73    Black      0              3        Current Incarceration
##  20:  325  4422  74    Black      0              1        Current Incarceration
##  21:  325  4485  75    Black      0              1        Current Incarceration
##  22:  325  4540  75    Black      0              1        Current Incarceration
##  23:  325  4783  75    Black      0              1        Current Incarceration
##  24:  325  4922  76    Black      0              1        Current Incarceration
##  25:  325  5245  77    Black      0              1        Current Incarceration
##  26:  325  5650  78    Black      0              1        Current Incarceration
##  27:  325  6217  79    Black      0              1        Current Incarceration
##  28:  325  7387  82    Black      0              1        Current Incarceration
##  29:  325  7502  83    Black      0              1        Current Incarceration
##  30:  325  7925  84    Black      0              1        Current Incarceration
##  31:  458  1950  31 Hispanic      0              3        Current Incarceration
##  32: 1138  2653  83    Black      0              1        Current Incarceration
##  33: 1165   941  33    Black      0              3        Current Incarceration
##  34: 1165  1012  33    Black      0              3        Current Incarceration
##  35: 1165  1249  34    Black      0              3        Current Incarceration
##  36: 1165  1522  34    Black      0              3        Current Incarceration
##  37: 1165  1836  35    Black      0              3        Current Incarceration
##  38: 1165  2414  37    Black      0              3        Current Incarceration
##  39: 1165  2663  38    Black      0              3        Current Incarceration
##  40: 1165  2770  38    Black      0              3        Current Incarceration
##  41: 1165  2922  38    Black      0              3        Current Incarceration
##  42: 1165  2946  38    Black      0              3        Current Incarceration
##  43: 1165  3287  39    Black      0              3        Current Incarceration
##  44: 1165  3367  40    Black      0              3        Current Incarceration
##  45: 1165  3436  40    Black      0              3        Current Incarceration
##  46: 1165  3867  41    Black      0              3        Current Incarceration
##  47: 1165  4571  43    Black      0              3        Current Incarceration
##  48: 1165  4745  43    Black      0              3        Current Incarceration
##  49: 1165  5706  46    Black      0              1        Current Incarceration
##  50: 1165  6452  48    Black      0              1        Current Incarceration
##  51: 1165  7592  51    Black      0              1        Current Incarceration
##  52: 1165  7722  51    Black      0              1        Current Incarceration
##  53: 1177  5966  81    White      0              3        Current Incarceration
##  54: 1179  9464  58 Hispanic      0              1        Current Incarceration
##  55: 1256  7363  54 Hispanic      0              0        Current Incarceration
##  56: 1343  6827  73    Black      0              0        Current Incarceration
##  57: 1386  1813  40    Black      0              3        Current Incarceration
##  58: 1386  1965  40    Black      0              3        Current Incarceration
##  59: 1386  2083  41    Black      0              3        Current Incarceration
##  60: 1386  2146  41    Black      0              3        Current Incarceration
##  61: 1386  3031  43    Black      0              1        Current Incarceration
##  62: 1399  8392  56    White      0              3        Current Incarceration
##  63: 1399  8470  56    White      0              3        Current Incarceration
##  64: 1399  8577  57    White      0              3        Current Incarceration
##  65: 1399  9365  59    White      0              3        Current Incarceration
##  66: 1413  6581  63    Black      0              1        Current Incarceration
##  67: 1413  7357  65    Black      0              1         Former Incarceration
##  68: 1449  4390  38    Black      1              1        Current Incarceration
##  69: 1479  2730  38    White      0              1        Current Incarceration
##  70: 1637  6808  40    Black      0              3        Current Incarceration
##  71: 1637  7830  43    Black      0              1        Current Incarceration
##  72: 1637  7900  43    Black      0              1        Current Incarceration
##  73: 1637  8028  43    Black      0              1        Current Incarceration
##  74: 1637 10709  51    Black      0              1        Current Incarceration
##  75: 1637 10752  51    Black      0              1        Current Incarceration
##  76: 1744  5822  60    White      0              0        Current Incarceration
##  77: 1773  3054  65 Hispanic      0              1        Current Incarceration
##  78: 1802   293  34    White      0              3        Current Incarceration
##  79: 1802   427  34    White      0              3        Current Incarceration
##  80: 1832  3841  53 Hispanic      0              3        Current Incarceration
##  81: 1832  6857  61 Hispanic      0              1         Former Incarceration
##  82: 2028  4804  56    Black      0              3        Current Incarceration
##  83: 2028  4933  56    Black      0              0        Current Incarceration
##  84: 2074  3673  29    Black      0              1         Former Incarceration
##  85: 2074  4145  30    Black      0              1        Current Incarceration
##  86: 2074  4893  32    Black      0              1        Current Incarceration
##  87: 2074  5181  33    Black      0              1        Current Incarceration
##  88: 2074  7137  38    Black      0              1        Current Incarceration
##  89: 2074  7355  39    Black      0              1        Current Incarceration
##  90: 2074  7648  40    Black      0              1         Former Incarceration
##  91: 2108 10699  75    White      0              1        Current Incarceration
##  92: 2380  4288  41    White      0              0        Current Incarceration
##  93: 2402  9166  81    Black      0              1        Current Incarceration
##  94: 2462  6710  77    Black      0              3        Current Incarceration
##  95: 2462  6784  77    Black      0              3        Current Incarceration
##  96: 2462  7396  78    Black      0              3        Current Incarceration
##  97: 2462  7480  79    Black      0              2        Current Incarceration
##  98: 2462  7611  79    Black      0              2        Current Incarceration
##  99: 2847  1461  74    Black      0              1        Current Incarceration
## 100: 2847  1552  74    Black      0              1        Current Incarceration
##        id  tick age     race female alc_use_status smoking_status    event_type
dim(repeat_incarceration_dt)
## [1] 689   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:    Black   572 0.830188679
## 2: Hispanic    57 0.082728592
## 3:    White    59 0.085631350
## 4:    Asian     1 0.001451379
repeat_incarceration_dt[, .(Count = .N, 
                            Proportion = .N/n_repeat_inc_events), 
                        by = .(female)]
##    female Count  Proportion
## 1:      0   686 0.995645864
## 2:      1     3 0.004354136
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    43 0.06240929
## 2:              1   298 0.43251089
## 3:              2    57 0.08272859
## 4:              3   291 0.42235123
repeat_incarceration_dt[, .(Count = .N, 
                            Proportion = .N/n_repeat_inc_events), 
                        by = .(smoking_status)]
##    smoking_status Count Proportion
## 1:        Current   650 0.94339623
## 2:          Never    16 0.02322206
## 3:         Former    23 0.03338171
# 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   302 0.251666667
## 2:    Black   732 0.610000000
## 3: Hispanic   158 0.131666667
## 4:    Asian     8 0.006666667
release_dt[, .(Count = .N, 
                     Proportion = .N/nrow(release_dt)), 
                 by = .(female)]
##    female Count Proportion
## 1:      0  1157 0.96416667
## 2:      1    43 0.03583333
release_dt[, .(Count = .N, 
                     Proportion = .N/nrow(release_dt)), 
                 by = .(alc_use_status)]
##    alc_use_status Count Proportion
## 1:              3   436 0.36333333
## 2:              1   569 0.47416667
## 3:              2   103 0.08583333
## 4:              0    92 0.07666667
release_dt[, .(Count = .N, 
                     Proportion = .N/nrow(release_dt)), 
                 by = .(smoking_status)]
##    smoking_status Count Proportion
## 1:        Current   969 0.80750000
## 2:          Never   163 0.13583333
## 3:         Former    68 0.05666667
# 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:  51  631  42 Black      0              1        Current    Release
## 2:  56 2240  65 Black      0              3          Never    Release
## 3:  58 2884  65 Black      0              1        Current    Release
## 4:  71 5823  57 White      0              3        Current    Release
## 5: 115 8152  65 White      0              3        Current    Release
## 6: 140 7141  64 Black      0              3        Current    Release
dim(first_release_dt)
## [1] 526   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:    Black   171 0.32509506
## 2:    White   245 0.46577947
## 3: Hispanic   103 0.19581749
## 4:    Asian     7 0.01330798
first_release_dt[, .(Count = .N, 
                           Proportion = .N/n_first_rel_agents), 
                       by = .(female)]
##    female Count Proportion
## 1:      0   486 0.92395437
## 2:      1    40 0.07604563
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    49 0.09315589
## 2:              1   277 0.52661597
## 3:              2    48 0.09125475
## 4:              3   152 0.28897338
first_release_dt[, .(Count = .N, 
                           Proportion = .N/n_first_rel_agents), 
                       by = .(smoking_status)]
##    smoking_status Count Proportion
## 1:        Current   333 0.63307985
## 2:          Never   148 0.28136882
## 3:         Former    45 0.08555133
# 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:   51   916  43    Black      0              1        Current    Release
##   2:   51  1341  44    Black      0              1        Current    Release
##   3:   51  2431  47    Black      0              1        Current    Release
##   4:   51  2778  48    Black      0              1        Current    Release
##   5:   51  5248  55    Black      0              1        Current    Release
##   6:   51  5461  55    Black      0              1        Current    Release
##   7:   51  5922  57    Black      0              1        Current    Release
##   8:   58  3940  68    Black      0              1        Current    Release
##   9:   58  4293  69    Black      0              0        Current    Release
##  10:   58  4752  70    Black      0              0        Current    Release
##  11:  140  7541  65    Black      0              3        Current    Release
##  12:  140  8100  66    Black      0              2        Current    Release
##  13:  191  9027  82 Hispanic      0              1        Current    Release
##  14:  195  5582  66    White      0              3        Current    Release
##  15:  195  5891  67    White      0              3        Current    Release
##  16:  271  7807  47    Black      0              3          Never    Release
##  17:  271  8317  48    Black      0              3          Never    Release
##  18:  284  6269  57 Hispanic      0              1        Current    Release
##  19:  325  3867  73    Black      0              3        Current    Release
##  20:  325  4446  74    Black      0              1        Current    Release
##  21:  325  4508  75    Black      0              1        Current    Release
##  22:  325  4568  75    Black      0              1        Current    Release
##  23:  325  4803  75    Black      0              1        Current    Release
##  24:  325  5101  76    Black      0              1        Current    Release
##  25:  325  5309  77    Black      0              1        Current    Release
##  26:  325  5771  78    Black      0              1        Current    Release
##  27:  325  6393  80    Black      0              1        Current    Release
##  28:  325  7418  83    Black      0              1        Current    Release
##  29:  325  7513  83    Black      0              1        Current    Release
##  30:  458  1978  31 Hispanic      0              3        Current    Release
##  31: 1138  2734  83    Black      0              1        Current    Release
##  32: 1165   961  33    Black      0              3        Current    Release
##  33: 1165  1021  33    Black      0              3        Current    Release
##  34: 1165  1259  34    Black      0              3        Current    Release
##  35: 1165  1616  35    Black      0              3        Current    Release
##  36: 1165  1979  36    Black      0              3        Current    Release
##  37: 1165  2447  37    Black      0              3        Current    Release
##  38: 1165  2724  38    Black      0              3        Current    Release
##  39: 1165  2783  38    Black      0              3        Current    Release
##  40: 1165  2930  38    Black      0              3        Current    Release
##  41: 1165  3277  39    Black      0              3        Current    Release
##  42: 1165  3295  39    Black      0              3        Current    Release
##  43: 1165  3376  40    Black      0              3        Current    Release
##  44: 1165  3617  40    Black      0              3        Current    Release
##  45: 1165  4033  41    Black      0              3        Current    Release
##  46: 1165  4651  43    Black      0              3        Current    Release
##  47: 1165  4832  44    Black      0              3        Current    Release
##  48: 1165  6342  48    Black      0              1        Current    Release
##  49: 1165  6581  48    Black      0              1        Current    Release
##  50: 1165  7602  51    Black      0              1        Current    Release
##  51: 1165  7821  52    Black      0              1        Current    Release
##  52: 1177  5983  82    White      0              3        Current    Release
##  53: 1179  9527  58 Hispanic      0              1        Current    Release
##  54: 1256  7391  54 Hispanic      0              0        Current    Release
##  55: 1343  6910  73    Black      0              0        Current    Release
##  56: 1386  1960  40    Black      0              3        Current    Release
##  57: 1386  2018  40    Black      0              3        Current    Release
##  58: 1386  2110  41    Black      0              3        Current    Release
##  59: 1386  2254  41    Black      0              3        Current    Release
##  60: 1386  3122  43    Black      0              1        Current    Release
##  61: 1399  8406  56    White      0              3        Current    Release
##  62: 1399  8576  57    White      0              3        Current    Release
##  63: 1399  8689  57    White      0              3        Current    Release
##  64: 1399  9409  59    White      0              3        Current    Release
##  65: 1413  6600  63    Black      0              1        Current    Release
##  66: 1413  7366  65    Black      0              1         Former    Release
##  67: 1449  4405  39    Black      1              1        Current    Release
##  68: 1479  2751  38    White      0              1        Current    Release
##  69: 1637  7592  42    Black      0              3        Current    Release
##  70: 1637  7851  43    Black      0              1        Current    Release
##  71: 1637  7975  43    Black      0              1        Current    Release
##  72: 1637  8053  43    Black      0              1        Current    Release
##  73: 1637 10720  51    Black      0              1        Current    Release
##  74: 1637 10778  51    Black      0              1        Current    Release
##  75: 1744  5895  60    White      0              0        Current    Release
##  76: 1773  4926  70 Hispanic      0              1        Current    Release
##  77: 1802   308  34    White      0              3        Current    Release
##  78: 1802   447  35    White      0              3        Current    Release
##  79: 1832  3969  53 Hispanic      0              3        Current    Release
##  80: 1832  6882  61 Hispanic      0              1         Former    Release
##  81: 2028  4825  56    Black      0              3        Current    Release
##  82: 2028  5053  56    Black      0              0        Current    Release
##  83: 2074  3758  29    Black      0              1         Former    Release
##  84: 2074  4285  30    Black      0              1        Current    Release
##  85: 2074  4962  32    Black      0              1        Current    Release
##  86: 2074  6750  37    Black      0              1        Current    Release
##  87: 2074  7241  38    Black      0              1        Current    Release
##  88: 2074  7522  39    Black      0              1        Current    Release
##  89: 2074  7669  40    Black      0              1         Former    Release
##  90: 2108 10862  75    White      0              1        Current    Release
##  91: 2380  4421  42    White      0              0        Current    Release
##  92: 2402  9180  81    Black      0              1        Current    Release
##  93: 2462  6738  77    Black      0              3        Current    Release
##  94: 2462  6945  77    Black      0              3        Current    Release
##  95: 2462  7417  78    Black      0              3        Current    Release
##  96: 2462  7567  79    Black      0              2        Current    Release
##  97: 2462  7774  79    Black      0              2        Current    Release
##  98: 2847  1474  74    Black      0              1        Current    Release
##  99: 2847  1678  74    Black      0              1        Current    Release
## 100: 2847  3639  79    Black      0              3        Current    Release
##        id  tick age     race female alc_use_status smoking_status event_type
dim(repeat_release_dt)
## [1] 674   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:    Black   561 0.83234421
## 2: Hispanic    55 0.08160237
## 3:    White    57 0.08456973
## 4:    Asian     1 0.00148368
repeat_release_dt[, .(Count = .N, 
                            Proportion = .N/n_repeat_rel_events), 
                        by = .(female)]
##    female Count  Proportion
## 1:      0   671 0.995548961
## 2:      1     3 0.004451039
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    43 0.06379822
## 2:              1   292 0.43323442
## 3:              2    55 0.08160237
## 4:              3   284 0.42136499
repeat_release_dt[, .(Count = .N, 
                            Proportion = .N/n_repeat_rel_events), 
                        by = .(smoking_status)]
##    smoking_status Count Proportion
## 1:        Current   636 0.94362018
## 2:          Never    15 0.02225519
## 3:         Former    23 0.03412463
# Sort data by id --------

incarceration_release_dt[order(id)]
##        tick    id age     race female alc_use_status smoking_status
##    1:   620    51  42    Black      0              1        Current
##    2:   631    51  42    Black      0              1        Current
##    3:   900    51  43    Black      0              1        Current
##    4:   916    51  43    Black      0              1        Current
##    5:   919    51  43    Black      0              1        Current
##   ---                                                              
## 2423: 10924 15168  35    Asian      0              1        Current
## 2424: 10272 15313  65 Hispanic      0              3          Never
## 2425: 10309 15313  65 Hispanic      0              3          Never
## 2426: 10539 15467  23    White      0              0        Current
## 2427: 10681 15467  24    White      0              0        Current
##          event_type
##    1: Incarceration
##    2:       Release
##    3: Incarceration
##    4:       Release
##    5: Incarceration
##   ---              
## 2423:       Release
## 2424: Incarceration
## 2425:       Release
## 2426: Incarceration
## 2427:       Release