rm(list=ls())
library("data.table")
library("ggplot2")
library("ipumsr")
## Warning: package 'ipumsr' was built under R version 4.3.1
# 2021 NHIS Smoking Data extracted using IPUMS: https://nhis.ipums.org/nhis/
ddi <- read_ipums_ddi("~/Coding/khanna-research/noah-testing/nhis_00004.xml")
data <- read_ipums_micro(ddi, data_file = "~/Coding/khanna-research/noah-testing/nhis_00004.dat")
## Use of data from IPUMS NHIS is subject to conditions including that users should cite the data appropriately. Use command `ipums_conditions()` for more details.
ddi["var_info"]
## $var_info
## # A tibble: 9 × 10
##   var_name     var_label    var_desc val_labels code_instr start   end imp_decim
##   <chr>        <chr>        <chr>    <list>     <chr>      <dbl> <dbl>     <dbl>
## 1 SAMPWEIGHT   Sample Pers… "SAMPWE… <tibble>   "SAMPWEIG…     1    12         3
## 2 AGE          Age          "AGE re… <tibble>   "AGE is a…    13    15         0
## 3 SEX          Sex          "SEX in… <tibble>    <NA>         16    16         0
## 4 RACENEW      Self-report… "RACENE… <tibble>    <NA>         17    19         0
## 5 HISPETH      Hispanic et… "HISPET… <tibble>    <NA>         20    21         0
## 6 HISPRACE     Hispanic et… "For sa… <tibble>    <NA>         22    23         0
## 7 SMOKEV       Ever smoked… "SMOKEV… <tibble>    <NA>         24    25         0
## 8 SMOKESTATUS2 Cigarette s… "For sa… <tibble>    <NA>         26    27         0
## 9 SMOKFREQNOW  Smoke every… "SMOKFR… <tibble>    <NA>         28    28         0
## # ℹ 2 more variables: var_type <chr>, rectypes <lgl>
dt <- data.table(data)
summary(dt)
##    SAMPWEIGHT           AGE              SEX           RACENEW   
##  Min.   :  680.5   Min.   :  0.00   Min.   :1.000   Min.   :100  
##  1st Qu.: 4705.9   1st Qu.: 22.00   1st Qu.:1.000   1st Qu.:100  
##  Median : 7381.2   Median : 44.00   Median :2.000   Median :100  
##  Mean   : 8622.1   Mean   : 45.13   Mean   :1.533   Mean   :198  
##  3rd Qu.:10783.8   3rd Qu.: 64.00   3rd Qu.:2.000   3rd Qu.:200  
##  Max.   :71378.0   Max.   :999.00   Max.   :9.000   Max.   :999  
##     HISPETH         HISPRACE         SMOKEV       SMOKESTATUS2  
##  Min.   :10.00   Min.   :1.000   Min.   :0.000   Min.   : 0.00  
##  1st Qu.:10.00   1st Qu.:2.000   1st Qu.:1.000   1st Qu.:11.00  
##  Median :10.00   Median :2.000   Median :1.000   Median :20.00  
##  Mean   :14.43   Mean   :2.224   Mean   :1.232   Mean   :21.18  
##  3rd Qu.:10.00   3rd Qu.:2.000   3rd Qu.:2.000   3rd Qu.:30.00  
##  Max.   :93.00   Max.   :7.000   Max.   :9.000   Max.   :90.00  
##   SMOKFREQNOW    
##  Min.   :0.0000  
##  1st Qu.:0.0000  
##  Median :0.0000  
##  Mean   :0.4473  
##  3rd Qu.:1.0000  
##  Max.   :9.0000
nrow(dt) # 37,743 people
## [1] 37743
dt <- dt[AGE >= 18]
sample_size <- nrow(dt) # 29,482

# Recode sex
possible_sex_values <- c("1", "2")
map_to <- c("M", "F")
dt[, SEX := as.character(SEX)]
dt[.(SEX = possible_sex_values, to = map_to), on = "SEX", SEX := i.to]

# remove 4 people who have smoked, yet current smoking status is unknown
dt <- dt[SMOKESTATUS2 != 40]

# remove the 886 people where smoking status is unknown
dt <- dt[SMOKESTATUS2!=90]

sample_size <- nrow(dt) # down to a sample of 28,592

# Recode Smoking status
possible_smokestatus_values <- c("10","11","12","13","20","30")
map_to <- c("CURRENT", "CURRENT", "CURRENT", "CURRENT", "FORMER", "NEVER")
dt[, SMOKESTATUS2 := as.character(SMOKESTATUS2)]
dt[.(SMOKESTATUS2 = possible_smokestatus_values, to = map_to), on="SMOKESTATUS2", SMOKESTATUS2 := i.to]

# Recode Race
possible_race_values <- c("1","2","3","4","5","6","7")
map_to <- c("Hispanic", "White", "Black", "Asian", "Other", "Other", "Other")
dt[, HISPRACE := as.character(HISPRACE)]
dt[.(HISPRACE = possible_race_values, to = map_to), on ="HISPRACE", HISPRACE := i.to]


## Verifying that values match with this analysis of the same data: https://www.cdc.gov/mmwr/volumes/72/wr/mm7218a1.htm

# total smoke proportions (weighted)
total_sampweight <- dt[, sum(SAMPWEIGHT)]
dt[, sum(SAMPWEIGHT), by=SMOKESTATUS2]
##    SMOKESTATUS2        V1
## 1:        NEVER 160737274
## 2:       FORMER  56305141
## 3:      CURRENT  28322851
dt[, sum(SAMPWEIGHT) / total_sampweight, by=SMOKESTATUS2]
##    SMOKESTATUS2        V1
## 1:        NEVER 0.6550938
## 2:       FORMER 0.2294748
## 3:      CURRENT 0.1154314
# Men / Women
men_sampweight <- dt[SEX=="M", sum(SAMPWEIGHT)]
dt[SEX=="M", sum(SAMPWEIGHT) / men_sampweight, by=SMOKESTATUS2]
##    SMOKESTATUS2        V1
## 1:        NEVER 0.5970221
## 2:       FORMER 0.2716810
## 3:      CURRENT 0.1312969
women_sampweight <- dt[SEX=="F", sum(SAMPWEIGHT)]
dt[SEX=="F", sum(SAMPWEIGHT) / women_sampweight, by=SMOKESTATUS2]
##    SMOKESTATUS2        V1
## 1:        NEVER 0.7096014
## 2:       FORMER 0.1898293
## 3:      CURRENT 0.1005694
## Extract smoking proportions for each race-sex combo in the sample

get_smoke_proportions <- function(dt, race, sex) {
  subsample_sampweight <- dt[(SEX==sex) & (HISPRACE==race), sum(SAMPWEIGHT)]
  dt[(SEX==sex) & (HISPRACE==race), sum(SAMPWEIGHT) / subsample_sampweight, by=SMOKESTATUS2]
}

get_smoke_proportions(dt, "White", "M")
##    SMOKESTATUS2        V1
## 1:        NEVER 0.5561551
## 2:       FORMER 0.3060202
## 3:      CURRENT 0.1378247
get_smoke_proportions(dt, "White", "F")
##    SMOKESTATUS2        V1
## 1:        NEVER 0.6367765
## 2:       FORMER 0.2423897
## 3:      CURRENT 0.1208338
get_smoke_proportions(dt, "Black", "M")
##    SMOKESTATUS2        V1
## 1:        NEVER 0.6957351
## 2:      CURRENT 0.1445684
## 3:       FORMER 0.1596965
get_smoke_proportions(dt, "Black", "F")
##    SMOKESTATUS2        V1
## 1:      CURRENT 0.0950610
## 2:        NEVER 0.7878936
## 3:       FORMER 0.1170454
get_smoke_proportions(dt, "Hispanic", "M")
##    SMOKESTATUS2        V1
## 1:        NEVER 0.6660289
## 2:      CURRENT 0.1028489
## 3:       FORMER 0.2311222
get_smoke_proportions(dt, "Hispanic", "F")
##    SMOKESTATUS2         V1
## 1:        NEVER 0.85094896
## 2:       FORMER 0.09588120
## 3:      CURRENT 0.05316984
get_smoke_proportions(dt, "Asian", "M")
##    SMOKESTATUS2         V1
## 1:        NEVER 0.69443814
## 2:       FORMER 0.21057858
## 3:      CURRENT 0.09498328
get_smoke_proportions(dt, "Asian", "F")
##    SMOKESTATUS2         V1
## 1:        NEVER 0.92894555
## 2:       FORMER 0.05318555
## 3:      CURRENT 0.01786890
get_smoke_proportions(dt, "Other", "M")
##    SMOKESTATUS2        V1
## 1:       FORMER 0.2753746
## 2:      CURRENT 0.1755110
## 3:        NEVER 0.5491144
get_smoke_proportions(dt, "Other", "F")
##    SMOKESTATUS2        V1
## 1:        NEVER 0.6493463
## 2:       FORMER 0.2074923
## 3:      CURRENT 0.1431614
## Calculate transition rates to keep smoking rates stable

# Cessation, defined as proportion of the population that makes at least one quit attempt in a year
# = 55.1% from https://www.cdc.gov/tobacco/data_statistics/fact_sheets/cessation/smoking-cessation-fast-facts/index.html

# Converting to a daily rate:
# The complement is 0.449 / year. Assuming each day is a Bernoulli trial and p the daily rate of cessation:
# 0.449=(1-p)^365 = 0.00219138278045028
cessation = 0.00219138278045028

get_transition_rates <- function(dt, race, sex){
  rates <- get_smoke_proportions(dt, race, sex)
  former_rate <- rates[SMOKESTATUS2=="FORMER", V1]
  current_rate <- rates[SMOKESTATUS2=="CURRENT", V1]
  # Relapse * Former = Cessation * Current --> Relapse = (Cessation * Current) / Former
  relapse = (cessation * current_rate) / former_rate
  print(paste(race, " ", sex, "Relapse:", relapse))
}

get_transition_rates(dt, "White", "M")
## [1] "White   M Relapse: 0.000986949919086459"
get_transition_rates(dt, "White", "F")
## [1] "White   F Relapse: 0.00109242737133654"
get_transition_rates(dt, "Black", "M")
## [1] "Black   M Relapse: 0.00198379277488641"
get_transition_rates(dt, "Black", "F")
## [1] "Black   F Relapse: 0.00177977939663843"
get_transition_rates(dt, "Hispanic", "M")
## [1] "Hispanic   M Relapse: 0.000975160161911163"
get_transition_rates(dt, "Hispanic", "F")
## [1] "Hispanic   F Relapse: 0.00121520652508235"
get_transition_rates(dt, "Asian", "M")
## [1] "Asian   M Relapse: 0.000988442062938252"
get_transition_rates(dt, "Asian", "F")
## [1] "Asian   F Relapse: 0.000736245252599015"
get_transition_rates(dt, "Other", "M")
## [1] "Other   M Relapse: 0.00139668519103322"
get_transition_rates(dt, "Other", "F")
## [1] "Other   F Relapse: 0.00151196727939936"