library("data.table")
library("ggplot2")
library("reshape2")
## Warning: package 'reshape2' was built under R version 4.3.1
##
## Attaching package: 'reshape2'
## The following objects are masked from 'package:data.table':
##
## dcast, melt
rm(list=ls())
agent_dt <- fread("~/Coding/khanna-research/alc-smoking-transition-use/python/output/agent_log_7.csv")
num_agents <- agent_dt[tick == 1, .N]
num_agents
## [1] 10000
last_tick <- max(agent_dt$tick)
last_tick
## [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 1150 11.50
## 2: FORMER 1999 19.99
## 3: NEVER 6851 68.51
# proportions of AUD statuses
agent_dt[tick == 1, .N, by=AUD_status][, "%":=N/sum(N)*100][order(AUD_status)]
## AUD_status N %
## 1: CURRENT 1139 11.39
## 2: FORMER 1763 17.63
## 3: NEVER 7098 70.98
current_tobacco <- agent_dt[(tick == 1) & (smoking_status=="CURRENT")]
current_AUD <- agent_dt[(tick == 1) & (AUD_status=="CURRENT")]
both <- current_tobacco[AUD_status=="CURRENT"]
n_both <- nrow(both)
# P(T & AUD) = 0.0262612
print(paste("P(T, AUD) = ", n_both / num_agents))
## [1] "P(T, AUD) = 0.027"
# P(AUD | T) = P(AUD, T) / P(T) = 0.2283583
print(paste("P(AUD | T) = ", n_both / nrow(current_tobacco)))
## [1] "P(AUD | T) = 0.234782608695652"
# P(AUD | !T) = P(AUD, !T) / P(!T)
num_AUD_not_tobacco <- nrow(current_AUD[smoking_status != "CURRENT"])
print(paste("P(AUD | !T) = ", num_AUD_not_tobacco / (num_agents - nrow(current_tobacco))))
## [1] "P(AUD | !T) = 0.0981920903954802"
# P(FAUD | T) = P(FAUD, T) / P(T)
f_AUD_T = current_tobacco[(AUD_status == "FORMER")]
print(paste("P(FAUD | T) = ", nrow(f_AUD_T) / nrow(current_tobacco)))
## [1] "P(FAUD | T) = 0.191304347826087"
# P(FAUD | !T) = P(FAUD, !T) / P(!T)
not_T <- agent_dt[(tick == 1) & smoking_status!="CURRENT", .N]
f_AUD_not_T <- agent_dt[(tick == 1) & (smoking_status!="CURRENT") & (AUD_status=="FORMER"), .N]
print(paste("P(FAUD | !T) = ", f_AUD_not_T / not_T))
## [1] "P(FAUD | !T) = 0.174350282485876"
# proportions of smoking statuses
agent_dt[tick == last_tick, .N, by=smoking_status][,"%":=N/sum(N)*100][order(smoking_status)]
## smoking_status N %
## 1: CURRENT 1138 11.38
## 2: FORMER 2011 20.11
## 3: NEVER 6851 68.51
# proportions of AUD statuses
agent_dt[tick == last_tick, .N, by=AUD_status][, "%":=N/sum(N)*100][order(AUD_status)]
## AUD_status N %
## 1: CURRENT 1148 11.48
## 2: FORMER 1754 17.54
## 3: NEVER 7098 70.98
current_tobacco <- agent_dt[(tick == last_tick) & (smoking_status=="CURRENT")]
current_AUD <- agent_dt[(tick == last_tick) & (AUD_status=="CURRENT")]
both <- current_tobacco[AUD_status=="CURRENT"]
n_both <- nrow(both)
# P(T & AUD) = 0.0262612
print(paste("P(T, AUD) = ", n_both / num_agents))
## [1] "P(T, AUD) = 0.0285"
# P(AUD | T) = P(AUD, T) / P(T) = 0.2283583
print(paste("P(AUD | T) = ", n_both / nrow(current_tobacco)))
## [1] "P(AUD | T) = 0.250439367311072"
# P(T | AUD) = P(AUD, T) / P(AUD) = 0.2324
print(paste("P(T | AUD) = ", n_both / nrow(current_AUD)))
## [1] "P(T | AUD) = 0.248257839721254"
# P(AUD | !T) = P(AUD, !T) / P(!T)
num_AUD_not_tobacco <- nrow(current_AUD[smoking_status != "CURRENT"])
print(paste("P(AUD | !T) = ", num_AUD_not_tobacco / (num_agents - nrow(current_tobacco))))
## [1] "P(AUD | !T) = 0.0973820807944031"
smoking_counts_per_tick <- agent_dt[, .N, by=.(tick, smoking_status)]
tick <- 1:last_tick
num_current <- smoking_counts_per_tick[smoking_status == "CURRENT"]$N
num_former <- smoking_counts_per_tick[smoking_status == "FORMER"]$N
num_never <- smoking_counts_per_tick[smoking_status == "NEVER"]$N
data <- data.frame(tick, num_current, num_former)
data_long <- melt(data, id = "tick")
smoking_plot <- ggplot(data_long,
aes(x = tick,
y = value,
color = variable)) + geom_line()
smoking_plot
AUD_counts_per_tick <- agent_dt[, .N, by=.(tick, AUD_status)]
tick <- 1:last_tick
num_current_AUD <- AUD_counts_per_tick[AUD_status == "CURRENT"]$N
num_former_AUD <- AUD_counts_per_tick[AUD_status == "FORMER"]$N
num_never_AUD <- AUD_counts_per_tick[AUD_status == "NEVER"]$N
data <- data.frame(tick, num_current_AUD, num_former_AUD)
data_long <- melt(data, id = "tick")
AUD_plot <- ggplot(data_long,
aes(x = tick,
y = value,
color = variable)) + geom_line()
AUD_plot
both_per_tick <- agent_dt[(smoking_status == "CURRENT") & (AUD_status == "CURRENT"), .N, by=tick]$N
tobacco_per_tick <- agent_dt[(smoking_status == "CURRENT"), .N, by=tick]$N
not_tobacco_per_tick <- agent_dt[(smoking_status != "CURRENT"), .N, by=tick]$N
AUD_per_tick <- agent_dt[(AUD_status == "CURRENT"), .N, by=tick]$N
AUD_no_tobacco_per_tick <- agent_dt[(AUD_status=="CURRENT") & (smoking_status!="CURRENT"), .N, by=tick]$N
T_no_AUD_per_tick <- agent_dt[(AUD_status!="CURRENT") & (smoking_status=="CURRENT"), .N, by=tick]$N
not_AUD_per_tick <- agent_dt[(AUD_status!="CURRENT"), .N, by=tick]$N
#former_T_per_tick <- agent_dt[(smoking_status=="FORMER"), .N, by=tick]$N
former_T_AUD_per_tick <- agent_dt[(smoking_status=="FORMER") & (AUD_status=="CURRENT"), .N, by=tick]$N
former_T_not_AUD_per_tick <- agent_dt[(smoking_status=="FORMER") & (AUD_status!="CURRENT"), .N, by=tick]$N
ever_smokers_AUD <- agent_dt[(smoking_status!="NEVER") & (AUD_status=="CURRENT"), .N, by=tick]$N
ever_smokers_not_AUD <- agent_dt[(smoking_status!="NEVER") & (AUD_status!="CURRENT"), .N, by=tick]$N
former_AUD_T_per_tick <- agent_dt[(smoking_status=="CURRENT") & (AUD_status=="FORMER"), .N, by=tick]$N
former_AUD_not_T_per_tick <- agent_dt[(smoking_status!="CURRENT") & (AUD_status=="FORMER"), .N, by=tick]$N
ever_AUD_T <- agent_dt[(smoking_status=="CURRENT") & (AUD_status!="NEVER"), .N, by=tick]$N
ever_AUD_no_T <- agent_dt[(smoking_status!="CURRENT") & (AUD_status!="NEVER"), .N, by=tick]$N
AUD_given_T_per_tick = both_per_tick / tobacco_per_tick # P(AUD | T) = P(AUD, T) / P(T)
AUD_given_not_T_per_tick = AUD_no_tobacco_per_tick / not_tobacco_per_tick # P(AUD | !T) = P(AUD, !T) / P(!T)
T_given_AUD_per_tick = both_per_tick / AUD_per_tick # P(T | AUD) = P(AUD, T) / P(AUD)
T_given_not_AUD_per_tick = T_no_AUD_per_tick / not_AUD_per_tick # P(T | !AUD) = P(T, !AUD) / P(!AUD)
both_percentage_per_tick = both_per_tick / num_agents # P(AUD, T)
T_percentage_per_tick = tobacco_per_tick / num_agents # P(T)
AUD_percentage_per_tick = AUD_per_tick / num_agents # P(AUD)
#FS_given_AUD_per_tick = former_T_AUD_per_tick / AUD_per_tick # P(FS | AUD)
#FS_given_not_AUD_per_tick = former_T_not_AUD_per_tick / not_AUD_per_tick # P(FS | !AUD)
T_quit_rate_given_AUD = former_T_AUD_per_tick / ever_smokers_AUD
T_quit_rate_given_not_AUD = former_T_not_AUD_per_tick / ever_smokers_not_AUD
AUD_quit_rate_given_T = former_AUD_T_per_tick / ever_AUD_T # P(FA | T) / P(AUD | T)
AUD_quit_rate_given_not_T = former_AUD_not_T_per_tick / ever_AUD_no_T
data <- data.frame(tick, AUD_given_T_per_tick, T_given_AUD_per_tick, AUD_given_not_T_per_tick, T_given_not_AUD_per_tick, T_percentage_per_tick, AUD_percentage_per_tick)
data_long <- melt(data, id = "tick")
conditional_plot <- ggplot(data_long,
aes(x = tick,
y = value,
color = variable)) + geom_line()
conditional_plot
# smoking quit rate of AUD and non-AUD individuals
data_long <- melt(data.frame(tick, T_quit_rate_given_AUD, T_quit_rate_given_not_AUD), id="tick")
ggplot(data_long, aes(x=tick, y=value, color=variable)) + geom_line()
# AUD quit rate of smoking and non-smoking individuals
data_long <- melt(data.frame(tick, AUD_quit_rate_given_T, AUD_quit_rate_given_not_T), id="tick")
ggplot(data_long, aes(x=tick, y=value, color=variable)) + geom_line()
### Follow population of smokers who quit on day 1 (model needs to be
set for this)
# follow population that quits for 6 months
#agent_dt[(tick == 4), .N, by=smoking_status] # 67% should've relapsed
#agent_dt[(tick == 9), .N, by=smoking_status] # 76% should've relapsed
#agent_dt[(tick == 16), .N, by=smoking_status] # 78% should've relapsed
#agent_dt[(tick == 32), .N, by=smoking_status] # 81% should've relapsed
#agent_dt[(tick == 92), .N, by=smoking_status] # 89% should've relapsed
#agent_dt[(tick == 182), .N, by=smoking_status] # 97% should've relapsed
#agent_dt[(tick == 8), .N, by=AUD_status] # 7 days, 80% should be abstinent
#agent_dt[(tick == 15), .N, by=AUD_status] # 14 days, 63% should be abstinent
#agent_dt[(tick == 31), .N, by=AUD_status] # 1 month, 53% should be abstinent
#agent_dt[(tick == 46), .N, by=AUD_status] # 45 days, 42% should be abstinent
#agent_dt[(tick == 183), .N, by=AUD_status] # 6 months, 34% should be abstinent
#agent_dt[(tick == 366), .N, by=AUD_status] # 1 year, 16.1% should be abstinent