require("data.table")
## Loading required package: data.table
agent_dt <- fread("~/Coding/khanna-research/alc-smoking-transition-use/python/output/agent_log.csv")

summary(agent_dt)
##       tick         id       smoking_status     alcohol_status    
##  Min.   :1   Min.   :   0   Length:10000       Length:10000      
##  1st Qu.:1   1st Qu.:2500   Class :character   Class :character  
##  Median :1   Median :5000   Mode  :character   Mode  :character  
##  Mean   :1   Mean   :5000                                        
##  3rd Qu.:1   3rd Qu.:7499                                        
##  Max.   :1   Max.   :9999
# number of agents
num_agents <- agent_dt[tick == 1, .N]
num_agents
## [1] 10000
# proportions of smoking statuses
agent_dt[tick == 1, .N, by=smoking_status][,"%":=N/sum(N)*100][order(smoking_status)]
##    smoking_status    N     %
## 1:        CURRENT 1083 10.83
## 2:         FORMER 2958 29.58
## 3:          NEVER 5959 59.59
# proportions of alcohol statuses
agent_dt[tick == 1, .N, by=alcohol_status][, "%":=N/sum(N)*100][order(alcohol_status)] 
##    alcohol_status    N     %
## 1:        ALCOHOL 6221 62.21
## 2:     NO ALCOHOL 3779 37.79
# "28.6% of persons as neither smoking tobacco nor using alcohol"
prob_neither <- agent_dt[tick == 1 & smoking_status!="CURRENT" & alcohol_status=='NO ALCOHOL', .N] / num_agents
prob_neither - .286
## [1] 0.0711
# "about 21.7% as using both"
prob_both <- agent_dt[tick == 1 & smoking_status=='CURRENT' & alcohol_status=='ALCOHOL', .N] / num_agents
prob_both - .217
## [1] -0.1295
# "about 65.4% as using any alcohol in the past year"
prob_alcohol <- agent_dt[tick == 1 & alcohol_status=='ALCOHOL', .N] / num_agents
prob_alcohol - .654
## [1] -0.0319
# "27.7% as using any tobacco in the past year"
prob_tobacco <- agent_dt[tick == 1 & smoking_status=='CURRENT', .N] / num_agents
prob_tobacco - .277
## [1] -0.1687
# P(A | T) = P(A & T) / P(T)
current_smokers <- agent_dt[tick == 1 & smoking_status == "CURRENT"]
prob_of_smoker <- current_smokers[,.N] / num_agents # P(T)

alc_and_smokers <- current_smokers[alcohol_status == 'ALCOHOL']

alc_given_tobacco <- prob_both / prob_of_smoker # P(A | T)
abs(alc_given_tobacco - 0.783) # difference from observed co-occurrence
## [1] 0.0249409
# P(A | !T) = P(A & !T) / P(!T)
non_smokers <- agent_dt[tick == 1 & smoking_status != "CURRENT"]
prob_of_non_smoker <- non_smokers[,.N] / num_agents # P(!T)

alc_and_non_smokers <- non_smokers[alcohol_status == 'ALCOHOL']
prob_of_alc_and_non_smoker <- alc_and_non_smokers[,.N] / num_agents # P(A & !T)

alc_given_not_tobacco <- prob_of_alc_and_non_smoker / prob_of_non_smoker # P(A | !T)
abs(alc_given_not_tobacco - 0.604) # difference from observed co-occurrence
## [1] 0.00447101