library(pacman)
p_load("car","tidyverse","reshape2", "gridExtra", "data.table", "janitor",
       "kableExtra", "knitr",  "here","tidyselect","scales","haven", 
       "ggpubr","Hmisc","psych","arsenal","sjPlot","stargazer",
       "readxl",  "miceadds","lme4","gam","missRanger","mice","broom.mixed",
       "gtsummary","xlsx","emmeans","glmmTMB","patchwork","naniar","multcomp","writexl") 
## package 'xlsx' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\smithcat\AppData\Local\Temp\RtmpO822Kw\downloaded_packages
#sessionInfo()

Survey Rounds: R1 – Adult survey May 2016-Feb 2017 R2 – Respiratory survey R1; 2017-18 R3 – Respiratory survey R2; 2021 R4 – Respiratory survey R3; June-October 2023

1. Import data

dta_long <- readRDS(here::here("Data", "dta_long_reg.rds")) 

imputed_dta<-readRDS(here::here("Data","imputed_long.rds"))

imputed_observed <- readRDS( here::here("Data","imputed_observed.rds"))

2. Descriptive summaries

2.1 Functions

Control <- tableby.control(
  test = F,
  total = T,
  digits=1,
  digits.pct=1,
  digits.p=3,
  numeric.test = "kwt", cat.test = "fe",
  numeric.stats = c("meansd", "medianq1q3", "range", "Nmiss2"),
  cat.stats = c("countpct", "Nmiss2"),
  ordered.stats=c("countpct", "Nmiss2"),
  stats.labels = list(
    meansd = "Mean (SD)",
    medianq1q3 = "Median (Q1, Q3)",
    range = "Min - Max",
    Nmiss2 = "Missing"
  )
)

2.2 Baseline: Participant characteristics

table<- tableby( Morwell ~    Base_firePM_TotalDiary + Base_Age +  gender +  Base_Education + 
                   Base_IRSAD_Score + Base_Employment + Base_Work_Exposed_2grp + Base_Work_Exposed_3grp + 
                   as.factor(Base_Asthma) + as.factor(Base_COPD) + Base_Asthma_COPD + 
                   Base_Asthma_medicine + Base_smokestatus+ Base_packyears ,  
  data = dta_long[dta_long$Time == "R1",], 
  control = Control,
  test=T
)
 
summary(table,
  title = "Table #. Distribution of Baseline characteristics at Adult survey"
)
Table #. Distribution of Baseline characteristics at Adult survey
Sale (N=173) Morwell (N=346) Total (N=519) p value
Mean exposure to fire-related PM2.5 < 0.001
   Mean (SD) 0.1 (0.3) 15.3 (11.5) 10.2 (11.8)
   Median (Q1, Q3) 0.0 (0.0, 0.0) 11.7 (7.2, 18.3) 7.2 (0.0, 14.0)
   Min - Max 0.0 - 2.7 0.1 - 56.0 0.0 - 56.0
   Missing 0 0 0
RECODE of AGE_LAST_BIRTHDAY (A2. What was your age last birthday?) 0.284
   Mean (SD) 54.3 (15.8) 52.7 (16.2) 53.2 (16.1)
   Median (Q1, Q3) 55.0 (43.0, 66.0) 53.5 (40.0, 65.0) 54.0 (41.0, 65.5)
   Min - Max 20.0 - 86.0 20.0 - 88.0 20.0 - 88.0
   Missing 0 0 0
gender 0.107
   Male 62 (35.8%) 151 (43.6%) 213 (41.0%)
   Female 111 (64.2%) 195 (56.4%) 306 (59.0%)
   Missing 0 0 0
Base_Education 0.208
   up to Year 10 34 (19.8%) 85 (24.9%) 119 (23.2%)
   Year 11-12 31 (18.0%) 72 (21.1%) 103 (20.1%)
   Post-secondary 107 (62.2%) 184 (54.0%) 291 (56.7%)
   Missing 1 5 6
Index of Relative Socio-economic Advantage & Disadvantage - Score < 0.001
   Mean (SD) 905.3 (53.5) 848.5 (100.6) 867.4 (91.7)
   Median (Q1, Q3) 900.0 (858.0, 962.0) 854.0 (806.0, 892.0) 868.0 (818.0, 926.5)
   Min - Max 756.0 - 972.0 554.0 - 1027.0 554.0 - 1027.0
   Missing 0 0 0
Base_Employment 0.021
   Employed 88 (51.8%) 154 (44.8%) 242 (47.1%)
   Other 68 (40.0%) 132 (38.4%) 200 (38.9%)
   Unemployed/unable 14 (8.2%) 58 (16.9%) 72 (14.0%)
   Missing 3 2 5
Base_Work_Exposed_2grp 0.446
   Not exposed 109 (63.0%) 205 (59.2%) 314 (60.5%)
   Exposed 64 (37.0%) 141 (40.8%) 205 (39.5%)
   Missing 0 0 0
Base_Work_Exposed_3grp 0.006
   Not exposed 109 (63.0%) 205 (59.2%) 314 (60.5%)
   Coal mine exposed 8 (4.6%) 46 (13.3%) 54 (10.4%)
   Exposed, not coal mine 56 (32.4%) 95 (27.5%) 151 (29.1%)
   Missing 0 0 0
as.factor(Base_Asthma) 0.061
   0 105 (61.0%) 180 (52.2%) 285 (55.1%)
   1 67 (39.0%) 165 (47.8%) 232 (44.9%)
   Missing 1 1 2
as.factor(Base_COPD) 0.479
   0 168 (97.1%) 330 (95.4%) 498 (96.0%)
   1 5 (2.9%) 16 (4.6%) 21 (4.0%)
   Missing 0 0 0
Base_Asthma_COPD 0.076
   no 102 (59.3%) 176 (51.0%) 278 (53.8%)
   yes 70 (40.7%) 169 (49.0%) 239 (46.2%)
   Missing 1 1 2
Base_Asthma_medicine 0.071
   no 127 (73.8%) 228 (65.9%) 355 (68.5%)
   yes 45 (26.2%) 118 (34.1%) 163 (31.5%)
   Missing 1 0 1
Base_smokestatus 0.516
   Non-smoker 89 (51.4%) 175 (50.6%) 264 (50.9%)
   Ex-smoker 62 (35.8%) 114 (32.9%) 176 (33.9%)
   Current smoker 22 (12.7%) 57 (16.5%) 79 (15.2%)
   Missing 0 0 0
Baseline cigarette packyears 0.671
   Mean (SD) 9.8 (16.3) 8.5 (14.6) 8.9 (15.2)
   Median (Q1, Q3) 0.0 (0.0, 13.9) 0.0 (0.0, 12.0) 0.0 (0.0, 13.0)
   Min - Max 0.0 - 78.0 0.0 - 100.0 0.0 - 100.0
   Missing 2 9 11
smokers <- dta_long %>% filter(Time == "R1" & Base_smokestatus!= "Non-smoker")
table<- tableby( Morwell ~     Base_packyears ,  
  data = smokers, 
  control = Control,
  test=T
)
 
summary(table,
  title = "Table #. Distribution of Cigaretter pack years at baseline for smokers"
)
Table #. Distribution of Cigaretter pack years at baseline for smokers
Sale (N=84) Morwell (N=171) Total (N=255) p value
Baseline cigarette packyears 0.212
   Mean (SD) 20.5 (18.3) 17.7 (16.9) 18.6 (17.4)
   Median (Q1, Q3) 15.0 (5.8, 29.5) 13.5 (4.7, 25.0) 14.2 (5.0, 27.0)
   Min - Max 0.0 - 78.0 0.0 - 100.0 0.0 - 100.0
   Missing 2 9 11

3. Mixed effects logistic regression

outcomes <- c("Wheeze_l12","Chest_tightness_l12","Shortness_breath_l12","Shortness_breath_rest_l12","Sneeze_l12",
            "Cough_3months", "Phelgm_3months")

outcome_labels <- c("Wheeze in last year","Chest tightness in last year","Woken by dyspnoea in last year","Dyspnoea while resting in last year","Nasal symptoms without cold in last year","Chronic cough", "Chronic Phelgm")

3.1 UNWEIGHTED regression

3.1a Regresion Function

regre_table_uw <- function(fomular,data,imputed_data,outcome,table_labels){
  
  model <-  glmmTMB(as.formula(fomular),data,
                        family = binomial(link = "logit")) 
  print(summary(model))
  
    Coef1 <- data.frame(coefficients(summary(model))$cond)%>%
          mutate(variables  = rownames(.)) %>%
          filter(variables != "(Intercept)") %>%
          mutate(Coef = format(round(exp(Estimate), digits = 2), nsmall = 2)) %>%
          mutate('95% CI' = paste0(format(round( exp(Estimate - 1.96*Std..Error) , digits = 2),nsmall = 2),", " ,
                                 format(round( exp(Estimate + 1.96*Std..Error) , digits = 2),nsmall = 2))) %>%
          mutate('p-value' = format(round(2*(1 - pnorm(abs(z.value))), digits = 3 ), nsmall = 3)) %>%
          mutate('p-value' = ifelse(`p-value` == "0.000","<0.001",`p-value`))
  

  imputed_model <- lapply(imputed_data, function(x) glmmTMB(as.formula(fomular),x,  
                                                         family = binomial(link = "logit")))
  
  pooled_fit <- pool(imputed_model)
 
  Coef2 <- data.frame(summary(pooled_fit)) %>%
          filter(term != "(Intercept)") %>%
          mutate('Imputed Coef ' = format(round(exp(estimate), digits = 2), nsmall = 2))  %>%
          mutate('Imputed 95% CI ' = paste0(format(round( exp(estimate - 1.96*std.error) , digits = 2),nsmall = 2)," , " ,
                                 format(round( exp(estimate + 1.96*std.error) , digits = 2),nsmall = 2))) %>%
          mutate('p-value ' = format(round(p.value, digits = 3 ), nsmall = 3)) %>%
          mutate('Imputed p-value ' = ifelse(`p-value ` == "0.000","<0.001",`p-value `))
  

   finaltable <- cbind(dplyr::select(Coef1,variables,Coef,'95% CI','p-value'),
                      dplyr::select(Coef2,`Imputed Coef `,`Imputed 95% CI `,`Imputed p-value `))
  
   finaltable <- table_labels %>%
    dplyr::left_join(finaltable, by = "variables") %>%
    dplyr::select(-variables) %>% 
    mutate(Outcome=outcome)

  
   list(finaltable)
  
  
}

3.1b Run unweighted regression models

  results <- list()

  #loop though all outcomes,
  for (i in 1:length(outcomes)) {
    #outcome variable
    outcome <- outcomes[i]

   #regression table
   fomular <- paste0( outcome, " ~ firePM_cent10per10*Time + age_per10 + gender +
                                   education + employmentstatus + Base_IRSAD_Score_per100 + Base_Work_Exposed_2grp +
                                   Base_Asthma_COPD + Base_smokestatus + Base_lnpackyears +
                                  (1 | RESIDENT_ID)")

     table_labels <- tibble(Variable = c("PM2.5 per 10, centred(10)","Round 2","Round 3","Round 4","Age per 10yrs",
                                     "Female", "Post-secondary education","In paid employment",
                                     "IRSAD Score per 100","Work exposed","Pre-mine fire Asthma or COPD",
                                     "Ex-smoker", "Current smoker","ln(cigarette packyears + 1)",
                                     "Pm2.5:Round 2","Pm2.5:Round 3",
                                     "Pm2.5:Round 4"),
                      variables = c("firePM_cent10per10","TimeR2","TimeR3","TimeR4","age_per10",
                                     "genderFemale", "educationPost-secondary","employmentstatusPaid employment",
                                     "Base_IRSAD_Score_per100","Base_Work_Exposed_2grpExposed","Base_Asthma_COPDyes",
                                     "Base_smokestatusEx-smoker",
                                     "Base_smokestatusCurrent smoker","Base_lnpackyears",
                                     "firePM_cent10per10:TimeR2","firePM_cent10per10:TimeR3",
                                     "firePM_cent10per10:TimeR4"))

      ## Results
      model <- regre_table_uw(fomular,dta_long,imputed_dta,outcome,table_labels)
      results[[outcome]] <- model


  }

  results_combined <- map_dfr(results, bind_rows)
  results<-results_combined %>%
    dplyr::select(Outcome,Variable,Coef,'95% CI','p-value',`Imputed Coef `,
                                                  `Imputed 95% CI `,`Imputed p-value `)

# write_xlsx(results,here::here("Results","All519_unweighted.xlsx"))
# saveRDS( results, here::here("Results","All519_unweighted.rds"))

results<-readRDS(here::here("Results","All519_unweighted.rds"))


# for (i in 1:length(outcomes)) {
#
# print(knitr::kable(results[results$Outcome==outcomes[i], ],format="html") %>%  kable_styling(full_width = TRUE, bootstrap_options = c("striped")))
# }

3.1c Linear function

lincom_table_uw <- function(fomular,imputed_data,outcome,labels){
  
 mod<- lapply(imputed_data, function(x) multcomp::glht(glmmTMB(as.formula(fomular),x,  
                                                         family = binomial(link = "logit")), 
                        linfct = 
                          c("firePM_cent10per10 = 0", 
                            "firePM_cent10per10 + firePM_cent10per10:TimeR2 = 0",
                            "firePM_cent10per10 + firePM_cent10per10:TimeR3 = 0",
                            "firePM_cent10per10 + firePM_cent10per10:TimeR4 = 0"
                            )
                )
           )   

pooled_lfct <- pool(mod)


  Coef <- data.frame(summary(pooled_lfct)) %>%
          mutate('Imputed Coef ' = format(round(exp(estimate), digits = 2), nsmall = 2))  %>%
          mutate('Imputed 95% CI ' = paste0(format(round( exp(estimate - 1.96*std.error) , digits = 2),nsmall = 2)," , " ,
                                 format(round( exp(estimate + 1.96*std.error) , digits = 2),nsmall = 2))) %>%
          mutate('p-value ' = format(round(p.value, digits = 3 ), nsmall = 3)) %>%
          mutate('Imputed p-value ' = ifelse(`p-value ` == "0.000","<0.001",`p-value `))
  

   finaltable <- Coef %>% dplyr::select(contrast,`Imputed Coef `,`Imputed 95% CI `,`Imputed p-value `)
  
   finaltable <- table_labels %>%
    dplyr::left_join(finaltable, by = "contrast") %>%
    dplyr::select(-contrast) %>% 
    mutate(Outcome=outcome)

  
   list(finaltable)
  
  
}

3.1d Run linear combinations

  lin_results <- list()

  #loop though all outcomes,
  for (i in 1:length(outcomes)) {
    #outcome variable
    outcome <- outcomes[i]

   #regression table
   fomular <- paste0( outcome, " ~ firePM_cent10per10*Time + age_per10 + gender +
                                   education + employmentstatus + Base_IRSAD_Score_per100 + Base_Work_Exposed_2grp +
                                   Base_Asthma_COPD + Base_smokestatus + Base_lnpackyears +
                                  (1 | RESIDENT_ID)")

     table_labels <- tibble(Contrast = c("PM2.5 per 10, R1","PM2.5 R1 + Pm2.5:Round 2",
                                         "PM2.5 R1 + Pm2.5:Round 3","PM2.5 R1 + Pm2.5:Round 4"),
                      contrast = c("firePM_cent10per10","firePM_cent10per10 + firePM_cent10per10:TimeR2",
                                   "firePM_cent10per10 + firePM_cent10per10:TimeR3",
                                   "firePM_cent10per10 + firePM_cent10per10:TimeR4"))

      ## Results
      model <- lincom_table_uw(fomular,imputed_dta,outcome,table_labels)
      lin_results[[outcome]] <- model


  }

  lin_combined <- map_dfr(lin_results, bind_rows)
  lin_res<-lin_combined %>%
    dplyr::select(Outcome,Contrast,`Imputed Coef `,`Imputed 95% CI `,`Imputed p-value `)
 
# write_xlsx(lin_res,here::here("Results","lin_all519_unweighted.xlsx"))
# saveRDS( lin_res, here::here("Results","lin_all519_unweighted.rds"))
 


lin_results<-readRDS(here::here("Results","lin_all519_unweighted.rds"))


# for (i in 1:length(outcomes)) {
# 
# print(knitr::kable(results[results$Outcome==outcomes[i], ],format="html") %>%  kable_styling(full_width = TRUE, bootstrap_options = c("striped")))
#  }

3.1e Sensitivity analyses: Observed imputed UNweighted linear combinations

lin_results <- list()

#loop though all outcomes,
for (i in 1:length(outcomes)) {
  #outcome variable
  outcome <- outcomes[i]

 #regression table
 fomular <- paste0( outcome, " ~ firePM_cent10per10*Time + age_per10 + gender +
                                 education + employmentstatus + Base_IRSAD_Score_per100 + Base_Work_Exposed_2grp +
                                 Base_Asthma_COPD + Base_smokestatus + Base_lnpackyears +
                                (1 | RESIDENT_ID)")

   table_labels <- tibble(Contrast = c("PM2.5 per 10, R1","PM2.5 R1 + Pm2.5:Round 2",
                                       "PM2.5 R1 + Pm2.5:Round 3","PM2.5 R1 + Pm2.5:Round 4"),
                    contrast = c("firePM_cent10per10","firePM_cent10per10 + firePM_cent10per10:TimeR2",
                                 "firePM_cent10per10 + firePM_cent10per10:TimeR3",
                                 "firePM_cent10per10 + firePM_cent10per10:TimeR4"))

    ## Results
    model <- lincom_table_uw(fomular,imputed_observed,outcome,table_labels)
    lin_results[[outcome]] <- model


}

lin_combined <- map_dfr(lin_results, bind_rows)
lin_res<-lin_combined %>%
  dplyr::select(Outcome,Contrast,`Imputed Coef `,`Imputed 95% CI `,`Imputed p-value `)


# write_xlsx(lin_res,here::here("Results","lin_observed_unweighted.xlsx"))
# saveRDS( lin_res, here::here("Results","lin_observed_unweighted.rds"))


lin_results<-readRDS(here::here("Results","lin_observed_unweighted.rds"))


# for (i in 1:length(outcomes)) {
# 
# print(knitr::kable(results[results$Outcome==outcomes[i], ],format="html") %>%  kable_styling(full_width = TRUE, bootstrap_options = c("striped")))
# }

3.1f UNWeighted Stratified

3.1f.1 By Pre-fire Asthma

imputed_asthma <- list()

 for (i in 1:50) {
imputed_asthma[[i]] <-imputed_dta[[i]] %>%
  filter(Base_Asthma=="Yes")
}



  lin_results <- list()

  #loop though all outcomes,
  for (i in 1:length(outcomes)) {
    #outcome variable
    outcome <- outcomes[i]

   #regression table
   fomular <- paste0( outcome, " ~ firePM_cent10per10*Time + age_per10 + gender +
                                   education + employmentstatus + Base_IRSAD_Score_per100 + Base_Work_Exposed_2grp +
                                    Base_smokestatus + Base_lnpackyears +
                                  (1 | RESIDENT_ID)")

     table_labels <- tibble(Contrast = c("PM2.5 per 10, R1","PM2.5 R1 + Pm2.5:Round 2",
                                         "PM2.5 R1 + Pm2.5:Round 3","PM2.5 R1 + Pm2.5:Round 4"),
                      contrast = c("firePM_cent10per10","firePM_cent10per10 + firePM_cent10per10:TimeR2",
                                   "firePM_cent10per10 + firePM_cent10per10:TimeR3",
                                   "firePM_cent10per10 + firePM_cent10per10:TimeR4"))

      ## Results
      model <- lincom_table_uw(fomular,imputed_asthma,outcome,table_labels)
      lin_results[[outcome]] <- model


  }

  lin_combined <- map_dfr(lin_results, bind_rows)
  lin_res<-lin_combined %>%
    dplyr::select(Outcome,Contrast,`Imputed Coef `,`Imputed 95% CI `,`Imputed p-value `)

# write_xlsx(lin_res,here::here("Results","lin_all519_unw_strata_Asthma.xlsx"))
# saveRDS( lin_res, here::here("Results","lin_all519_unw_strata_Asthma.rds"))

3.1f.2 No Pre-fire Asthma

imputed_NONasthma <- list()

 for (i in 1:50) {
imputed_NONasthma[[i]] <-imputed_dta[[i]] %>%
  filter(Base_Asthma=="No")
}


  lin_results <- list()

  #loop though all outcomes,
  for (i in 1:length(outcomes)) {
    #outcome variable
    outcome <- outcomes[i]

   #regression table
   fomular <- paste0( outcome, " ~ firePM_cent10per10*Time + age_per10 + gender +
                                   education + employmentstatus + Base_IRSAD_Score_per100 + Base_Work_Exposed_2grp +
                                    Base_smokestatus + Base_lnpackyears +
                                  (1 | RESIDENT_ID)")

     table_labels <- tibble(Contrast = c("PM2.5 per 10, R1","PM2.5 R1 + Pm2.5:Round 2",
                                         "PM2.5 R1 + Pm2.5:Round 3","PM2.5 R1 + Pm2.5:Round 4"),
                      contrast = c("firePM_cent10per10","firePM_cent10per10 + firePM_cent10per10:TimeR2",
                                   "firePM_cent10per10 + firePM_cent10per10:TimeR3",
                                   "firePM_cent10per10 + firePM_cent10per10:TimeR4"))

      ## Results
      model <- lincom_table_uw(fomular,imputed_NONasthma,outcome,table_labels)
      lin_results[[outcome]] <- model


  }

  lin_combined <- map_dfr(lin_results, bind_rows)
  lin_res<-lin_combined %>%
    dplyr::select(Outcome,Contrast,`Imputed Coef `,`Imputed 95% CI `,`Imputed p-value `)

# write_xlsx(lin_res,here::here("Results","lin_all519_unw_strata_NONAsthma.xlsx"))
# saveRDS( lin_res, here::here("Results","lin_all519_unw_strata_NONAsthma.rds"))

4 WEIGHTED regression

4.1 Regresion Function WEIGHTED

regre_table <- function(fomular,data,imputed_data,outcome,table_labels){
  
  model <-  glmmTMB(as.formula(fomular),data,weights=Base_sample_weight,
                        family = binomial(link = "logit")) 
  print(summary(model))
  
    Coef1 <- data.frame(coefficients(summary(model))$cond)%>%
          mutate(variables  = rownames(.)) %>%
          filter(variables != "(Intercept)") %>%
          mutate(Coef = format(round(exp(Estimate), digits = 2), nsmall = 2)) %>%
          mutate('95% CI' = paste0(format(round( exp(Estimate - 1.96*Std..Error) , digits = 2),nsmall = 2),", " ,
                                 format(round( exp(Estimate + 1.96*Std..Error) , digits = 2),nsmall = 2))) %>%
          mutate('p-value' = format(round(2*(1 - pnorm(abs(z.value))), digits = 3 ), nsmall = 3)) %>%
          mutate('p-value' = ifelse(`p-value` == "0.000","<0.001",`p-value`))
  

  imputed_model <- lapply(imputed_data, function(x) glmmTMB(as.formula(fomular),x,weights=Base_sample_weight,  
                                                         family = binomial(link = "logit")))
  
  pooled_fit <- pool(imputed_model)
 
  Coef2 <- data.frame(summary(pooled_fit)) %>%
          filter(term != "(Intercept)") %>%
          mutate('Imputed Coef ' = format(round(exp(estimate), digits = 2), nsmall = 2))  %>%
          mutate('Imputed 95% CI ' = paste0(format(round( exp(estimate - 1.96*std.error) , digits = 2),nsmall = 2)," , " ,
                                 format(round( exp(estimate + 1.96*std.error) , digits = 2),nsmall = 2))) %>%
          mutate('p-value ' = format(round(p.value, digits = 3 ), nsmall = 3)) %>%
          mutate('Imputed p-value ' = ifelse(`p-value ` == "0.000","<0.001",`p-value `))
  

   finaltable <- cbind(dplyr::select(Coef1,variables,Coef,'95% CI','p-value'),
                      dplyr::select(Coef2,`Imputed Coef `,`Imputed 95% CI `,`Imputed p-value `))
  
   finaltable <- table_labels %>%
    dplyr::left_join(finaltable, by = "variables") %>%
    dplyr::select(-variables) %>% 
    mutate(Outcome=outcome)

  
   list(finaltable)
  
  
}

4.2 Weighted Linear function

lincom_table <- function(fomular,imputed_data,outcome,labels){
  
 mod<- lapply(imputed_data, function(x) multcomp::glht(glmmTMB(as.formula(fomular),x, weights=Base_sample_weight,
                                                         family = binomial(link = "logit")), 
                        linfct = 
                          c("firePM_cent10per10 = 0", 
                            "firePM_cent10per10 + firePM_cent10per10:TimeR2 = 0",
                            "firePM_cent10per10 + firePM_cent10per10:TimeR3 = 0",
                            "firePM_cent10per10 + firePM_cent10per10:TimeR4 = 0"
                            )
                )
           )   

pooled_lfct <- pool(mod)


  Coef <- data.frame(summary(pooled_lfct)) %>%
          mutate('Imputed Coef ' = format(round(exp(estimate), digits = 2), nsmall = 2))  %>%
          mutate('Imputed 95% CI ' = paste0(format(round( exp(estimate - 1.96*std.error) , digits = 2),nsmall = 2)," , " ,
                                 format(round( exp(estimate + 1.96*std.error) , digits = 2),nsmall = 2))) %>%
          mutate('p-value ' = format(round(p.value, digits = 3 ), nsmall = 3)) %>%
          mutate('Imputed p-value ' = ifelse(`p-value ` == "0.000","<0.001",`p-value `))
  

   finaltable <- Coef %>% dplyr::select(contrast,`Imputed Coef `,`Imputed 95% CI `,`Imputed p-value `)
  
   finaltable <- table_labels %>%
    dplyr::left_join(finaltable, by = "contrast") %>%
    dplyr::select(-contrast) %>% 
    mutate(Outcome=outcome)

  
   list(finaltable)
  
  
}

4.5c Run weighted regression

  results <- list()

  #loop though all outcomes,
  for (i in 1:length(outcomes)) {
    #outcome variable
    outcome <- outcomes[i]

   #regression table
   fomular <- paste0( outcome, " ~ firePM_cent10per10*Time + age_per10 + gender +
                                   education + employmentstatus + Base_IRSAD_Score_per100 + Base_Work_Exposed_2grp +
                                   Base_Asthma_COPD +  Base_smokestatus + Base_lnpackyears +
                                  (1 | RESIDENT_ID)")

     table_labels <- tibble(Variable = c("PM2.5 per 10, centred(10)","Round 2","Round 3","Round 4","Age per 10yrs",
                                     "Female", "Post-secondary education","In paid employment",
                                     "IRSAD Score per 100","Work exposed","Pre-mine fire Asthma or COPD",
                                     "Ex-smoker", "Current smoker","ln(cigarette packyears + 1)",
                                     "Pm2.5:Round 2","Pm2.5:Round 3",
                                     "Pm2.5:Round 4"),
                      variables = c("firePM_cent10per10","TimeR2","TimeR3","TimeR4","age_per10",
                                     "genderFemale", "educationPost-secondary","employmentstatusPaid employment",
                                     "Base_IRSAD_Score_per100","Base_Work_Exposed_2grpExposed","Base_Asthma_COPDyes",
                                     "Base_smokestatusEx-smoker",
                                     "Base_smokestatusCurrent smoker","Base_lnpackyears",
                                     "firePM_cent10per10:TimeR2","firePM_cent10per10:TimeR3",
                                     "firePM_cent10per10:TimeR4"))

      ## Results
      model <- regre_table(fomular,dta_long,imputed_dta,outcome,table_labels)
      results[[outcome]] <- model


  }

  results_combined <- map_dfr(results, bind_rows)
  results<-results_combined %>%
    dplyr::select(Outcome,Variable,Coef,'95% CI','p-value',`Imputed Coef `,
                                                  `Imputed 95% CI `,`Imputed p-value `)

# write_xlsx(results,here::here("Results","All519_weighted.xlsx"))
# saveRDS( results, here::here("Results","All519_weighted.rds"))
#
results<-readRDS(here::here("Results","All519_weighted.rds"))

# for (i in 1:length(outcomes)) {
# 
# print(knitr::kable(results[results$Outcome==outcomes[i], ],format="html") %>%  kable_styling(full_width = TRUE, bootstrap_options = c("striped")))
#  }
# 

4.5d Run weighted linear combinations

  lin_results <- list()

  #loop though all outcomes,
  for (i in 1:length(outcomes)) {
    #outcome variable
    outcome <- outcomes[i]

   #regression table
   fomular <- paste0( outcome, " ~ firePM_cent10per10*Time + age_per10 + gender +
                                   education + employmentstatus + Base_IRSAD_Score_per100 + Base_Work_Exposed_2grp +
                                   Base_Asthma_COPD +  Base_smokestatus + Base_lnpackyears +
                                  (1 | RESIDENT_ID)")

     table_labels <- tibble(Contrast = c("PM2.5 per 10, R1","PM2.5 R1 + Pm2.5:Round 2",
                                         "PM2.5 R1 + Pm2.5:Round 3","PM2.5 R1 + Pm2.5:Round 4"),
                      contrast = c("firePM_cent10per10","firePM_cent10per10 + firePM_cent10per10:TimeR2",
                                   "firePM_cent10per10 + firePM_cent10per10:TimeR3",
                                   "firePM_cent10per10 + firePM_cent10per10:TimeR4"))

      ## Results
      model <- lincom_table(fomular,imputed_dta,outcome,table_labels)
      lin_results[[outcome]] <- model


  }

  lin_combined <- map_dfr(lin_results, bind_rows)
  lin_res<-lin_combined %>%
    dplyr::select(Outcome,Contrast,`Imputed Coef `,`Imputed 95% CI `,`Imputed p-value `)

# write_xlsx(lin_res,here::here("Results","lin_all519_weighted.xlsx"))
# saveRDS( lin_res, here::here("Results","lin_all519_weighted.rds"))
 
lin_results<-readRDS(here::here("Results","lin_all519_weighted.rds"))

# for (i in 1:length(outcomes)) {
# 
# print(knitr::kable(results[results$Outcome==outcomes[i], ],format="html") %>%  kable_styling(full_width = TRUE, bootstrap_options = c("striped")))
# }

5. Interaction p-values

5.1 Unweighted Interaction tests

p_load("mitml")

int_test <- list()

for (i in 1:length(outcomes)) {

  outcome <- outcomes[i]

  mod1 <- lapply(imputed_dta, function(x) glmmTMB(as.formula(paste0( outcome, " ~ firePM_cent10per10*Time + age_per10 + gender +
                                   education + employmentstatus + Base_IRSAD_Score_per100 + Base_Work_Exposed_2grp +
                                   Base_Asthma_COPD +  Base_smokestatus + Base_lnpackyears +
                                  (1 | RESIDENT_ID)")),x,family = binomial(link = "logit")))


  mod2 <- lapply(imputed_dta, function(x) glmmTMB(as.formula(paste0( outcome, " ~ firePM_cent10per10 + Time + age_per10 + gender +
                                   education + employmentstatus + Base_IRSAD_Score_per100 + Base_Work_Exposed_2grp +
                                   Base_Asthma_COPD +  Base_smokestatus + Base_lnpackyears +
                                  (1 | RESIDENT_ID)")),x,family = binomial(link = "logit")))

  interaction_p <- testModels(mod1,mod2)

  tst <- as.tibble(interaction_p$test) %>%
         mutate(Outcome = outcome )

    int_test[[outcome]] <- tst


  }

  test_combined <- map_dfr(int_test, bind_rows)

 p_load("writexl")

#  write_xlsx(test_combined,here::here("Results","All519_unweighted_interactiontests.xlsx"))
#  saveRDS( test_combined, here::here("Results","All519_unweighted_interactiontests.rds"))


interaction <-  readRDS(here::here("Results","All519_unweighted_interactiontests.rds")) %>% 
  dplyr::select(Outcome, F.value,df1,df2,`P(>F)`) %>%
  mutate(Fvalue = format(round(F.value, digits = 2),nsmall=2),
         pvalue = format(round(`P(>F)`,digits=3),nsmall=3)) %>% 
   mutate(pvalue = ifelse(pvalue == "0.000","<0.001",pvalue)) %>% 
  dplyr::select(Outcome, Fvalue,df1,df2,pvalue) 
  


#print(knitr::kable(interaction,format="html") %>% kable_styling(full_width = TRUE, bootstrap_options = c("striped")))

5.3 Weighted Interaction tests

int_test <- list()

for (i in 1:length(outcomes)) {

  outcome <- outcomes[i]

  mod1 <- lapply(imputed_dta, function(x) glmmTMB(as.formula(paste0( outcome, " ~ firePM_cent10per10*Time + age_per10 + gender +
                                   education + employmentstatus + Base_IRSAD_Score_per100 + Base_Work_Exposed_2grp +
                                   Base_Asthma_COPD +  Base_smokestatus + Base_lnpackyears +
                                  (1 | RESIDENT_ID)")),x,weights=Base_sample_weight,
                                                         family = binomial(link = "logit")))


  mod2 <- lapply(imputed_dta, function(x) glmmTMB(as.formula(paste0( outcome, " ~ firePM_cent10per10 + Time + age_per10 + gender +
                                   education + employmentstatus + Base_IRSAD_Score_per100 + Base_Work_Exposed_2grp +
                                   Base_Asthma_COPD + Base_smokestatus + Base_lnpackyears +
                                  (1 | RESIDENT_ID)")),x,weights=Base_sample_weight,
                                                         family = binomial(link = "logit")))

  interaction_p <- testModels(mod1,mod2)

  tst <- as.tibble(interaction_p$test) %>%
         mutate(Outcome = outcome )

    int_test[[outcome]] <- tst


  }

  test_combined <- map_dfr(int_test, bind_rows)

 # write_xlsx(test_combined,here::here("Results","All519_weighted_interactiontests.xlsx"))
#  saveRDS( test_combined, here::here("Results","All519_weighted_interactiontests.rds"))


interaction <-  readRDS(here::here("Results","All519_weighted_interactiontests.rds")) %>% 
  dplyr::select(Outcome, F.value,df1,df2,`P(>F)`) %>%
  mutate(Fvalue = format(round(F.value, digits = 2),nsmall=2),
         pvalue = format(round(`P(>F)`,digits=3),nsmall=3)) %>% 
   mutate(pvalue = ifelse(pvalue == "0.000","<0.001",pvalue)) %>% 
  dplyr::select(Outcome, Fvalue,df1,df2,pvalue) 
  


#print(knitr::kable(interaction,format="html") %>% kable_styling(full_width = TRUE, bootstrap_options = c("striped")))