# 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':   3045 obs. of  8 variables:
##  $ tick          : int  9 25 29 30 45 62 71 84 87 93 ...
##  $ id            : int  723 5259 9190 723 9190 3899 4534 3899 723 4534 ...
##  $ age           : int  44 48 77 44 77 75 78 75 44 78 ...
##  $ race          : chr  "Hispanic" "Black" "White" "Hispanic" ...
##  $ female        : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ alc_use_status: int  1 3 1 1 1 1 0 1 1 0 ...
##  $ smoking_status: chr  "Current" "Current" "Never" "Current" ...
##  $ event_type    : chr  "Incarceration" "Incarceration" "Incarceration" "Release" ...
##  - attr(*, ".internal.selfref")=<externalptr>
dim(incarceration_release_dt)
## [1] 3045    8
incarceration_release_dt[,.N]
## [1] 3045
# 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] 1540
str(incarceration_dt)
## Classes 'data.table' and 'data.frame':   1540 obs. of  8 variables:
##  $ tick          : int  9 25 29 62 71 87 97 102 107 113 ...
##  $ id            : int  723 5259 9190 3899 4534 723 9177 3418 537 8766 ...
##  $ age           : int  44 48 77 75 78 44 30 67 61 47 ...
##  $ race          : chr  "Hispanic" "Black" "White" "White" ...
##  $ female        : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ alc_use_status: int  1 3 1 1 0 1 1 0 3 1 ...
##  $ smoking_status: chr  "Current" "Current" "Never" "Never" ...
##  $ event_type    : chr  "Incarceration" "Incarceration" "Incarceration" "Incarceration" ...
##  - attr(*, ".internal.selfref")=<externalptr>
dim(incarceration_dt)
## [1] 1540    8
release_dt[,.N]
## [1] 1505
str(release_dt)
## Classes 'data.table' and 'data.frame':   1505 obs. of  8 variables:
##  $ tick          : int  30 45 84 93 102 109 158 158 218 225 ...
##  $ id            : int  723 9190 3899 4534 723 9177 5259 1469 5259 3453 ...
##  $ age           : int  44 77 75 78 44 30 48 54 48 71 ...
##  $ race          : chr  "Hispanic" "White" "White" "Black" ...
##  $ female        : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ alc_use_status: int  1 1 1 0 1 1 3 0 3 1 ...
##  $ smoking_status: chr  "Current" "Never" "Never" "Current" ...
##  $ event_type    : chr  "Release" "Release" "Release" "Release" ...
##  - attr(*, ".internal.selfref")=<externalptr>
dim(release_dt)
## [1] 1505    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: Hispanic   220 0.142857143
## 2:    Black   976 0.633766234
## 3:    White   342 0.222077922
## 4:    Asian     2 0.001298701
incarceration_dt[, .(Count = .N, 
                     Proportion = .N/nrow(incarceration_dt)), 
                 by = .(female)]
##    female Count Proportion
## 1:      0  1497 0.97207792
## 2:      1    43 0.02792208
incarceration_dt[, .(Count = .N, 
                     Proportion = .N/nrow(incarceration_dt)), 
                 by = .(alc_use_status)]
##    alc_use_status Count Proportion
## 1:              1   632 0.41038961
## 2:              3   452 0.29350649
## 3:              0   397 0.25779221
## 4:              2    59 0.03831169
incarceration_dt[, .(Count = .N, 
                     Proportion = .N/nrow(incarceration_dt)), 
                 by = .(smoking_status)]
##    smoking_status Count Proportion
## 1:        Current  1263 0.82012987
## 2:          Never   175 0.11363636
## 3:         Former   102 0.06623377
# 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: 29   586  41    White      0              1        Current Incarceration
## 2: 30  6858  83    White      0              0        Current Incarceration
## 3: 55   388  42    Black      0              0        Current Incarceration
## 4: 74  1069  78    Black      0              3         Former Incarceration
## 5: 81  5656  57 Hispanic      0              1        Current Incarceration
## 6: 84 10111  56 Hispanic      0              0        Current Incarceration
dim(first_incarceration_dt)
## [1] 559   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:    White   252 0.450805009
## 2:    Black   183 0.327370304
## 3: Hispanic   123 0.220035778
## 4:    Asian     1 0.001788909
first_incarceration_dt[, .(Count = .N, 
                           Proportion = .N/n_first_inc_agents), 
                       by = .(female)]
##    female Count Proportion
## 1:      0   518 0.92665474
## 2:      1    41 0.07334526
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   184 0.32915921
## 2:              1   273 0.48837209
## 3:              2    19 0.03398927
## 4:              3    83 0.14847943
first_incarceration_dt[, .(Count = .N, 
                           Proportion = .N/n_first_inc_agents), 
                       by = .(smoking_status)]
##    smoking_status Count Proportion
## 1:        Current   349  0.6243292
## 2:         Former    58  0.1037567
## 3:          Never   152  0.2719141
# 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:   55   759  43    Black      0              0        Current Incarceration
##   2:   55  1139  44    Black      0              0        Current Incarceration
##   3:   55  1239  45    Black      0              0        Current Incarceration
##   4:   55  1634  46    Black      0              0        Current Incarceration
##   5:   55  2346  48    Black      0              0        Current Incarceration
##   6:   55  2701  49    Black      0              0        Current Incarceration
##   7:   55  2837  49    Black      0              0        Current Incarceration
##   8:   55  5399  56    Black      0              0        Current Incarceration
##   9:   55  6409  59    Black      0              0        Current Incarceration
##  10:   55  8913  66    Black      0              0        Current Incarceration
##  11:   55  9078  66    Black      0              0        Current Incarceration
##  12:   55  9406  67    Black      0              0        Current Incarceration
##  13:   55  9539  67    Black      0              0        Current Incarceration
##  14:   55  9676  68    Black      0              0        Current Incarceration
##  15:   55 10075  69    Black      0              0        Current Incarceration
##  16:   55 10172  69    Black      0              0        Current Incarceration
##  17:   55 10328  70    Black      0              0        Current Incarceration
##  18:   55 10491  70    Black      0              0        Current Incarceration
##  19:   55 10879  71    Black      0              0        Current Incarceration
##  20:  216  2588  47    Black      0              1        Current Incarceration
##  21:  216  2790  48    Black      0              1        Current Incarceration
##  22:  216  3798  51    Black      0              1         Former Incarceration
##  23:  359  1955  77    Black      0              0        Current Incarceration
##  24:  359  2199  78    Black      0              0        Current Incarceration
##  25:  359  2769  79    Black      0              0        Current Incarceration
##  26:  359  2910  80    Black      0              0        Current Incarceration
##  27:  359  3119  80    Black      0              0        Current Incarceration
##  28:  359  3535  82    Black      0              0        Current Incarceration
##  29:  359  3933  83    Black      0              0        Current Incarceration
##  30:  359  4035  83    Black      0              0        Current Incarceration
##  31:  359  4363  84    Black      0              0        Current Incarceration
##  32:  408  3704  80    Asian      0              1        Current Incarceration
##  33:  419  9327  82    White      0              3        Current Incarceration
##  34:  432   961  81    Black      0              3        Current Incarceration
##  35:  432  1205  82    Black      0              3        Current Incarceration
##  36:  432  1225  82    Black      0              3        Current Incarceration
##  37:  432  1382  82    Black      0              3        Current Incarceration
##  38:  432  1486  83    Black      0              3        Current Incarceration
##  39:  432  1675  83    Black      0              3        Current Incarceration
##  40:  432  1835  84    Black      0              3        Current Incarceration
##  41:  432  1922  84    Black      0              3        Current Incarceration
##  42:  432  1950  84    Black      0              3        Current Incarceration
##  43:  446  7575  64 Hispanic      0              0        Current Incarceration
##  44:  510  2012  33    Black      0              2        Current Incarceration
##  45:  510  2388  34    Black      0              2        Current Incarceration
##  46:  510  2542  35    Black      0              2        Current Incarceration
##  47:  510  2574  35    Black      0              2        Current Incarceration
##  48:  510  3379  37    Black      0              2        Current Incarceration
##  49:  510  3593  38    Black      0              2        Current Incarceration
##  50:  510  3830  38    Black      0              2        Current Incarceration
##  51:  510  4618  40    Black      0              2        Current Incarceration
##  52:  510  5565  43    Black      0              2        Current Incarceration
##  53:  510  5813  44    Black      0              2        Current Incarceration
##  54:  533  3802  59    White      0              1        Current Incarceration
##  55:  534 10075  47    Black      0              0        Current Incarceration
##  56:  534 10456  48    Black      0              0        Current Incarceration
##  57:  534 10651  48    Black      0              0        Current Incarceration
##  58:  537  4377  72    White      0              3          Never Incarceration
##  59:  537  5133  74    White      0              3          Never Incarceration
##  60:  603 10142  55    White      0              3        Current Incarceration
##  61:  604   959  22    Black      0              0        Current Incarceration
##  62:  604  2454  26    Black      0              0        Current Incarceration
##  63:  604  2559  27    Black      0              0        Current Incarceration
##  64:  604  4443  32    Black      0              0        Current Incarceration
##  65:  604  4583  32    Black      0              0        Current Incarceration
##  66:  604  4723  33    Black      0              0        Current Incarceration
##  67:  604  5030  33    Black      0              0        Current Incarceration
##  68:  604  7047  39    Black      0              0        Current Incarceration
##  69:  604  7760  41    Black      0              0        Current Incarceration
##  70:  604  8428  43    Black      0              0        Current Incarceration
##  71:  604  9532  46    Black      0              0        Current Incarceration
##  72:  604  9756  46    Black      0              0        Current Incarceration
##  73:  604 10377  48    Black      0              0        Current Incarceration
##  74:  604 10601  49    Black      0              0        Current Incarceration
##  75:  604 10932  50    Black      0              0        Current Incarceration
##  76:  614  1657  40    Black      0              1        Current Incarceration
##  77:  614  1863  41    Black      0              1        Current Incarceration
##  78:  723    87  44 Hispanic      0              1        Current Incarceration
##  79:  942  6364  82    White      0              1        Current Incarceration
##  80:  947  3018  75    White      0              1        Current Incarceration
##  81:  953   353  64    Black      0              0        Current Incarceration
##  82:  953  1306  66    Black      0              0        Current Incarceration
##  83:  953  3035  71    Black      0              0        Current Incarceration
##  84:  953  3498  72    Black      0              0        Current Incarceration
##  85:  953  4025  74    Black      0              0        Current Incarceration
##  86:  953  4569  75    Black      0              0         Former Incarceration
##  87:  953  5540  78    Black      0              0        Current Incarceration
##  88:  953  6011  79    Black      0              0        Current Incarceration
##  89:  953  6308  80    Black      0              0        Current Incarceration
##  90:  953  6808  82    Black      0              0        Current Incarceration
##  91:  953  7075  82    Black      0              0        Current Incarceration
##  92:  953  7269  83    Black      0              0        Current Incarceration
##  93:  953  7624  84    Black      0              0        Current Incarceration
##  94:  959  1192  49    White      0              0        Current Incarceration
##  95: 1010  4686  78 Hispanic      1              0        Current Incarceration
##  96: 1034 10820  69    White      0              1        Current Incarceration
##  97: 1078  4893  40    Black      0              1        Current Incarceration
##  98: 1078  5409  42    Black      0              1        Current Incarceration
##  99: 1078  5588  42    Black      0              1        Current Incarceration
## 100: 1078  5651  42    Black      0              1        Current Incarceration
##        id  tick age     race female alc_use_status smoking_status    event_type
dim(repeat_incarceration_dt)
## [1] 981   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   793 0.808358818
## 2:    Asian     1 0.001019368
## 3:    White    90 0.091743119
## 4: Hispanic    97 0.098878695
repeat_incarceration_dt[, .(Count = .N, 
                            Proportion = .N/n_repeat_inc_events), 
                        by = .(female)]
##    female Count  Proportion
## 1:      0   979 0.997961264
## 2:      1     2 0.002038736
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   213 0.21712538
## 2:              1   359 0.36595311
## 3:              2    40 0.04077472
## 4:              3   369 0.37614679
repeat_incarceration_dt[, .(Count = .N, 
                            Proportion = .N/n_repeat_inc_events), 
                        by = .(smoking_status)]
##    smoking_status Count Proportion
## 1:        Current   914 0.93170234
## 2:         Former    44 0.04485219
## 3:          Never    23 0.02344546
# 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: Hispanic   219 0.145514950
## 2:    White   328 0.217940199
## 3:    Black   956 0.635215947
## 4:    Asian     2 0.001328904
release_dt[, .(Count = .N, 
                     Proportion = .N/nrow(release_dt)), 
                 by = .(female)]
##    female Count Proportion
## 1:      0  1462 0.97142857
## 2:      1    43 0.02857143
release_dt[, .(Count = .N, 
                     Proportion = .N/nrow(release_dt)), 
                 by = .(alc_use_status)]
##    alc_use_status Count Proportion
## 1:              1   616 0.40930233
## 2:              0   392 0.26046512
## 3:              3   439 0.29169435
## 4:              2    58 0.03853821
release_dt[, .(Count = .N, 
                     Proportion = .N/nrow(release_dt)), 
                 by = .(smoking_status)]
##    smoking_status Count Proportion
## 1:        Current  1235 0.82059801
## 2:          Never   172 0.11428571
## 3:         Former    98 0.06511628
# 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: 29   611  41    White      0              1        Current    Release
## 2: 30  6867  84    White      0              0        Current    Release
## 3: 55   415  42    Black      0              0        Current    Release
## 4: 74  2578  83    Black      0              3         Former    Release
## 5: 81  5760  57 Hispanic      0              1        Current    Release
## 6: 84 10251  57 Hispanic      0              0        Current    Release
dim(first_release_dt)
## [1] 545   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:    White   241 0.442201835
## 2:    Black   181 0.332110092
## 3: Hispanic   122 0.223853211
## 4:    Asian     1 0.001834862
first_release_dt[, .(Count = .N, 
                           Proportion = .N/n_first_rel_agents), 
                       by = .(female)]
##    female Count Proportion
## 1:      0   504 0.92477064
## 2:      1    41 0.07522936
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   182 0.33394495
## 2:              1   265 0.48623853
## 3:              2    19 0.03486239
## 4:              3    79 0.14495413
first_release_dt[, .(Count = .N, 
                           Proportion = .N/n_first_rel_agents), 
                       by = .(smoking_status)]
##    smoking_status Count Proportion
## 1:        Current   342 0.62752294
## 2:         Former    54 0.09908257
## 3:          Never   149 0.27339450
# 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:   55   800  44    Black      0              0        Current    Release
##   2:   55  1179  45    Black      0              0        Current    Release
##   3:   55  1304  45    Black      0              0        Current    Release
##   4:   55  1650  46    Black      0              0        Current    Release
##   5:   55  2504  48    Black      0              0        Current    Release
##   6:   55  2713  49    Black      0              0        Current    Release
##   7:   55  5003  55    Black      0              0        Current    Release
##   8:   55  5467  56    Black      0              0        Current    Release
##   9:   55  8540  65    Black      0              0        Current    Release
##  10:   55  8929  66    Black      0              0        Current    Release
##  11:   55  9102  66    Black      0              0        Current    Release
##  12:   55  9463  67    Black      0              0        Current    Release
##  13:   55  9664  68    Black      0              0        Current    Release
##  14:   55  9733  68    Black      0              0        Current    Release
##  15:   55 10113  69    Black      0              0        Current    Release
##  16:   55 10196  69    Black      0              0        Current    Release
##  17:   55 10373  70    Black      0              0        Current    Release
##  18:   55 10603  70    Black      0              0        Current    Release
##  19:   55 10903  71    Black      0              0        Current    Release
##  20:  216  2617  47    Black      0              1        Current    Release
##  21:  216  2836  48    Black      0              1        Current    Release
##  22:  216  3815  51    Black      0              1         Former    Release
##  23:  359  1979  77    Black      0              0        Current    Release
##  24:  359  2225  78    Black      0              0        Current    Release
##  25:  359  2845  80    Black      0              0        Current    Release
##  26:  359  3092  80    Black      0              0        Current    Release
##  27:  359  3137  80    Black      0              0        Current    Release
##  28:  359  3635  82    Black      0              0        Current    Release
##  29:  359  3964  83    Black      0              0        Current    Release
##  30:  359  4174  83    Black      0              0        Current    Release
##  31:  359  4371  84    Black      0              0        Current    Release
##  32:  408  3912  81    Asian      0              1        Current    Release
##  33:  419  9417  83    White      0              3        Current    Release
##  34:  432  1138  82    Black      0              3        Current    Release
##  35:  432  1221  82    Black      0              3        Current    Release
##  36:  432  1365  82    Black      0              3        Current    Release
##  37:  432  1464  83    Black      0              3        Current    Release
##  38:  432  1666  83    Black      0              3        Current    Release
##  39:  432  1725  83    Black      0              3        Current    Release
##  40:  432  1901  84    Black      0              3        Current    Release
##  41:  432  1945  84    Black      0              3        Current    Release
##  42:  446  7600  64 Hispanic      0              0        Current    Release
##  43:  510  2117  34    Black      0              2        Current    Release
##  44:  510  2536  35    Black      0              2        Current    Release
##  45:  510  2570  35    Black      0              2        Current    Release
##  46:  510  2863  36    Black      0              2        Current    Release
##  47:  510  3510  37    Black      0              2        Current    Release
##  48:  510  3697  38    Black      0              2        Current    Release
##  49:  510  3858  38    Black      0              2        Current    Release
##  50:  510  4690  41    Black      0              2        Current    Release
##  51:  510  5602  43    Black      0              2        Current    Release
##  52:  510  5821  44    Black      0              2        Current    Release
##  53:  533  3890  59    White      0              1        Current    Release
##  54:  534 10094  47    Black      0              0        Current    Release
##  55:  534 10475  48    Black      0              0        Current    Release
##  56:  534 10676  48    Black      0              0        Current    Release
##  57:  537  4396  72    White      0              3          Never    Release
##  58:  537  5410  75    White      0              3          Never    Release
##  59:  603 10217  55    White      0              3        Current    Release
##  60:  604  1519  24    Black      0              0        Current    Release
##  61:  604  2474  26    Black      0              0        Current    Release
##  62:  604  2623  27    Black      0              0        Current    Release
##  63:  604  4458  32    Black      0              0        Current    Release
##  64:  604  4659  32    Black      0              0        Current    Release
##  65:  604  4882  33    Black      0              0        Current    Release
##  66:  604  6956  39    Black      0              0        Current    Release
##  67:  604  7056  39    Black      0              0        Current    Release
##  68:  604  7775  41    Black      0              0        Current    Release
##  69:  604  8444  43    Black      0              0        Current    Release
##  70:  604  9547  46    Black      0              0        Current    Release
##  71:  604  9767  46    Black      0              0        Current    Release
##  72:  604 10391  48    Black      0              0        Current    Release
##  73:  604 10700  49    Black      0              0        Current    Release
##  74:  604 10945  50    Black      0              0        Current    Release
##  75:  614  1741  40    Black      0              1        Current    Release
##  76:  614  1890  41    Black      0              1        Current    Release
##  77:  723   102  44 Hispanic      0              1        Current    Release
##  78:  942  6389  82    White      0              1        Current    Release
##  79:  947  3100  75    White      0              1        Current    Release
##  80:  953   455  64    Black      0              0        Current    Release
##  81:  953  2245  69    Black      0              0        Current    Release
##  82:  953  3086  71    Black      0              0        Current    Release
##  83:  953  3603  73    Black      0              0        Current    Release
##  84:  953  4173  74    Black      0              0        Current    Release
##  85:  953  4592  75    Black      0              0         Former    Release
##  86:  953  5566  78    Black      0              0        Current    Release
##  87:  953  6039  79    Black      0              0        Current    Release
##  88:  953  6360  80    Black      0              0        Current    Release
##  89:  953  6969  82    Black      0              0        Current    Release
##  90:  953  7233  83    Black      0              0        Current    Release
##  91:  953  7292  83    Black      0              0        Current    Release
##  92:  959  1244  49    White      0              0        Current    Release
##  93: 1010  4694  78 Hispanic      1              0        Current    Release
##  94: 1078  4919  40    Black      0              1        Current    Release
##  95: 1078  5430  42    Black      0              1        Current    Release
##  96: 1078  5602  42    Black      0              1        Current    Release
##  97: 1078  5697  42    Black      0              1        Current    Release
##  98: 1078  6487  45    Black      0              1        Current    Release
##  99: 1243  6136  74    Black      0              0        Current    Release
## 100: 1243  6388  74    Black      0              0        Current    Release
##        id  tick age     race female alc_use_status smoking_status event_type
dim(repeat_release_dt)
## [1] 960   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   775 0.807291667
## 2:    Asian     1 0.001041667
## 3:    White    87 0.090625000
## 4: Hispanic    97 0.101041667
repeat_release_dt[, .(Count = .N, 
                            Proportion = .N/n_repeat_rel_events), 
                        by = .(female)]
##    female Count  Proportion
## 1:      0   958 0.997916667
## 2:      1     2 0.002083333
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   210   0.218750
## 2:              1   351   0.365625
## 3:              2    39   0.040625
## 4:              3   360   0.375000
repeat_release_dt[, .(Count = .N, 
                            Proportion = .N/n_repeat_rel_events), 
                        by = .(smoking_status)]
##    smoking_status Count Proportion
## 1:        Current   893 0.93020833
## 2:         Former    44 0.04583333
## 3:          Never    23 0.02395833