---
title: "Final THYROCHEM report - REPORT - tables"
author: "Xanthi D. Andrianou - Water and Health Lab"
output:  
  html_document:
    number_sections: yes
    theme: flatly
    toc: yes
  word_document: default
Date: August 22, 2016
---
```{r setup, cache=FALSE, include=FALSE}
options(width = 90)
```

This report is based on the full analysis of the THYROCHEM dataset including both the Cypriot and Romanian data.

# Preliminaries
```{r Packages, echo=FALSE, include=FALSE, warning=TRUE}
#CHANGE 160822 - LOAD EACH PACKAGE SEPARATELY
library("ggplot2")
library("dplyr")
library("data.table")
library("Hmisc")
library("stargazer")
library("psych")
library("plyr")
library("corrplot")
library("tableone")
library("sjPlot")
library("corrplot")
library("ICC")
library("irr")
library("coefplot")
library("car")
library("knitr")

#pkg<-c("ggplot2","dplyr","data.table","Hmisc","stargazer","psych","plyr","tableone", "sjPlot","corrplot","ICC", "irr","coefplot","car","knitr")
#out<-lapply(pkg, function(x) {
#  if(!require(x,character.only = TRUE)) #install.packages(x,character.only = TRUE)
#}
#)
```
## Packages used
The R packages used in the analysis are the following: "ggplot2","dplyr","data.table","Hmisc","stargazer","psych","plyr","tableone", "sjPlot","corrplot","ICC", "irr","coefplot","car","knitr"
Complete information on the versions and the exact citations are provided in the end of the report.
---

## Raw data files
The raw data files used for the analysis are the following:

* Cyprus
  + questionnaire - CY_20150714_Questionnaire_DF.csv
  + BPA results - CY_BPA_spot_rawdata_DF.csv and CY_BPA_morn_rawdata_DF.csv
  + Iodine results (incl. creatinine) - CY_creat_iodine_DF.csv
  
* Romania
  + questionnaire - RO_Questionnaire_150711_DF.csv
  + BPA results - RO_BPA_rawdata.csv
  + Iodine results - RO_creat_iodine_DF.csv


```{r VariableLists, include=FALSE, hide=TRUE, warning=FALSE}
#variables from the whole questionnaire that will be recoded for the analysis
quest_vars<-c(
  "CODE", "status", "country", 
  #demographics
  "age", "weight", "height", "marstat", "edu", "tsh", "ft4", "antitg", "antitpo", "smokst", "alccons",
  #drinking water from 20l
  "dwh20l", "glh20l", "dwbh20l", "glbh20lb", "dwa20l", "gla20l", "dwba20l", "glba20lb", "ref20l", "reffr20l",
  #microware use
  "MwaveFrw",
  #canned food
  "tommc", "tunamc", "cornmc", "stdrmc", "vegmc", "fishmc", "meatmc", "frtmc", "cnotmc",
  #PCPs
  "shmpwc", "conrwc", "bylnwc", "srglwc", "hdspwc", "hrfmwc", "hrdewc", "hrsywc", "nlphwc", "lppnwc", "fecmwc",
  #knowledge
  "natknowl", "chemknowl",
  #cleaning
  "dshwshm", "dshwshsd", "mopm", "mopd", "toilm", "toild", "bthrm", "bthrd", "winm", "wind", "othcm", "othcd",
  #chlorine use
  "dshwshchl", "mopchl", "toilchl", "bthrchl", "winchl", "othcchl", "lauweek",
  #perfume
  "perfwc",
  #deodorant
  "liqdeowc", "soldeowc", "sprdeowc",
  #cosmetics
  "mkupw", "lpstw", "eylnw", "rougw", "mascw", "mkremw")

#the variables of the questionnaire that are to be used in the analysis
quest_analysis_vars<-c("CODE", "status", "country", "age", "weight", "heightM", "BMI", "BMI_cat", "marstat", "edu", "tsh", "ft4", "antitg", "antitpo", "smokst", "alccons",
                       "daily20lglass", #drinking water from 20l 
                       "MwaveFrw", #microware use
                       "weekPORTIONScanned", #canned food
                       "weekMINScleaning", #cleaning
                       "weekPORTIONSpcps", #PCPs
                       "perfwc", #perfume
                       "weekPORTIONSdeo", #deodorant
                       "weekPORTIONScosm"#cosmetics
                       )

#demographic variables
demo_vars<-c("age","weight", "heightM", "BMI", "BMI_cat", "smokst", "alccons", "marstat", "edu")
#exposures from the questionnaire
expo_vars<-c("daily20lglass", #drinking water from 20l
             "MwaveFrw", #microware use
             "weekPORTIONScanned", #canned food
             "weekMINScleaning", #cleaning
             "weekPORTIONSpcps", #PCPs
             "perfwc", #perfume
             "weekPORTIONSdeo", #deodorant
             "weekPORTIONScosm"#cosmetics
             )
#hormones
horm_vars_iod_sp<-c("tsh", "ft4","iod_ugPERlt_spot")

#hormones and antibodies
horm_vars_cat<-c("tsh_cat","ft4_cat", "antitg", "antitpo")

#urine measurement variables - incl. morning and spot
urine_levs_vars<-c("BPF_ngPERlt_spot","BPA_ngPERlt_spot","ClBPA_ngPERlt_spot","Cl35_2BPA_ngPERlt_spot", "Cl33_2BPA_ngPERlt_spot","ClxBPA_ngPERlt_spot","BPF_ngPERg_spot","BPA_ngPERg_spot","ClBPA_ngPERg_spot","Cl35_2BPA_ngPERg_spot", "Cl33_2BPA_ngPERg_spot","ClxBPA_ngPERg_spot",
                   "BPF_ngPERlt_morn","BPA_ngPERlt_morn","ClBPA_ngPERlt_morn","Cl35_2BPA_ngPERlt_morn", "Cl33_2BPA_ngPERlt_morn","ClxBPA_ngPERlt_morn","BPF_ngPERg_morn","BPA_ngPERg_morn","ClBPA_ngPERg_morn","Cl35_2BPA_ngPERg_morn", "Cl33_2BPA_ngPERg_morn","ClxBPA_ngPERg_morn")

ln_vars_spot<-c("ln_BPF_ngPERlt_spot","ln_BPA_ngPERlt_spot", "ln_ClBPA_ngPERlt_spot","ln_Cl35_2BPA_ngPERlt_spot", "ln_Cl33_2BPA_ngPERlt_spot","ln_ClxBPA_ngPERlt_spot", "ln_BPF_ngPERg_spot","ln_BPA_ngPERg_spot", "ln_ClBPA_ngPERg_spot","ln_Cl35_2BPA_ngPERg_spot", "ln_Cl33_2BPA_ngPERg_spot","ln_ClxBPA_ngPERg_spot")

ln_vars_morn<-c("ln_BPF_ngPERlt_morn","ln_BPA_ngPERlt_morn", "ln_ClBPA_ngPERlt_morn","ln_Cl35_2BPA_ngPERlt_morn", "ln_Cl33_2BPA_ngPERlt_morn","ln_ClxBPA_ngPERlt_morn", "ln_BPF_ngPERg_morn","ln_BPA_ngPERg_morn", "ln_ClBPA_ngPERg_morn","ln_Cl35_2BPA_ngPERg_morn", "ln_Cl33_2BPA_ngPERg_morn","ln_ClxBPA_ngPERg_morn")

```

## Variable lists
The variables were all taken from the raw data. Questionnaire variables were summarized to be used in the summary analysis of the exposures. Variables that were not directly measured such as BMI or the creatinine-adjustment of the urinary concentrations were created during the analysis.
Specific variable lists were created and used in the present script for efficiency.

List of the different combinations of the variables used in the analysis:

* quest_vars - all the variables extracted from the questionnaire and used in the analysis

* quest_analysis_vars - questionnaire variables used in the final analysis

* demo_vars - demographics and anthropometric characteristics

* expo_vars - summary of the "questionniare exposures"

* horm_vars_iod_sp - hormones and spot  iodine (ng/L) (all continuous)

* horm_vars_cat - categorical variables of the hormone levels and the antibodies 
 + _Note: the categorization of the hormone leves was done separately for Cyprus and Romania since the normal levels differ in the two countries._ 

* urine_levs_vars - ng/L urine levels both spot and morning  (raw, ng/L and ng/g)

* urine_levs_vars_av - urine levels average levels {derived with the function (spot+morn)/2} 

* ln_vars_spot - ln-transformed ng/L and ng/g - spot

* ln_vars_morn - ln-transformed ng/L and ng/g - morning

* ln_vars_av - ln-transformed average ng/L and ng/g - (ln of the average not average of the ln_spot and ln_morn)

* quant_vars - quantiles of the BPF, BPA and ClxBPA (ng/L)

## Functions

General functions

* creatine_adj -- creatinine adjustment (ng/L concentrations divided by the creatinine) _Note: iodine concentrations were already adjusted for creatinine and the units were ug/L and ug/g (unadjusted and adjusted, respectively)
* spot_morn_av -- average of spot and morning - applied to the ng/L and the ng/g

* gm_func -- geometric mean
* gsd_func -- geometric standard deviation

* break_quant -- breaking the quantiles (0,0.25,0.50,0.75,1) - including the lowest bound

* t_test_unpaired_table -- table for the results of the unpaired t-test - applied to the ln-transformed ng/L and ng/g concentrations, hormones etc.
* wilcox_test_unpaired_table -- table for the results of the unpaired Wilcoxon test - applied to the raw ng/L and ng/g concentrations, hormones etc.

```{r GenFunctions, echo=FALSE, include=FALSE, warning=FALSE}
### Functions
#make the creatinine-adjustment concentrations for the BPA etc.
creatine_adj<-function(ngPERlt_conc,creatinine_gPERlt){
  ngPERlt_conc/creatinine_gPERlt}
spot_morn_av<-function(spot,morn){
  (spot+morn)/2}

gm_func<-function(x){geometric.mean(x, na.rm = TRUE)}
gsd_func <- function(x, na.rm=TRUE){exp(sd(log(x), na.rm=na.rm))}
expx <- function(x) (exp(x))

break_quant<-function (var){cut(var, breaks=quantile(var, c(0,0.25,0.50,0.75,1), na.rm=TRUE), 
                                  include.lowest = TRUE)}
t_test_unpaired_table<-function (variable, group) {unlist(t.test(log(variable)~group)[c("estimate","p.value","statistic")])}

wilcox_test_unpaired_table<-function (variable, group) {unlist(wilcox.test(variable~group)[c("p.value","statistic")])}
```

Logistic regression functions

_Νotes: 
The logistic regression fuctions were used separately for each country and the pooled samples.

All the concentration variables were used in the models log-transformed (to the natural logarithm) including the hormones, iodine and creatinine.

* _Univariate regressions_
    + All variables (incl. those recoded from the questionnaires) were included in univariate models 

* _Multivariate regressions_
    + LR1: adjusted for age, BMI, tsh and ft4
    + LR2: adjusted for age, BMI, tsh, ft4 and spot iodine

```{r RegFunctions, echo=FALSE, include=FALSE, warning=FALSE}
#regression functions 
#Logistic regression outcome: TND

#regression functions
# univariate - crude estimates
LR_status01_x1<-function(var, dataframe) {
  formula    <- as.formula(paste("status01 ~ ", var))
  res.logist <- glm(formula, data = dataframe, family = binomial(link=logit))
}#no country - with creatinine - either spot or morning

# univariate and creatinine - CY, RO and pooled
LR_status01_x1_spot_CR<-function(var, dataframe) {
  formula    <- as.formula(paste("status01 ~ ln_creat_gPERlt_spot +", var))
  res.logist <- glm(formula, data = dataframe, family = binomial(link=logit))
}

# multivatiare for spot ng/L and creatinine - CY, RO and pooled
LR1_status01_multix_spot_CR<-function(var, dataframe) {
  formula    <- as.formula(paste("status01 ~ age + BMI + ln_tsh + ln_ft4 + ln_creat_gPERlt_spot +", var))
  res.logist <- glm(formula, data = dataframe, family = binomial(link=logit))
}

LR2_status01_multix_spot_CR<-function(var, dataframe) {
  formula    <- as.formula(paste("status01 ~ age + BMI + ln_tsh + ln_ft4 + ln_iod_ugPERlt_spot + ln_creat_gPERlt_spot +", var))
  res.logist <- glm(formula, data = dataframe, family = binomial(link=logit))
}

#models with country as covariate and creatinine adjustment - only spot levels

# univariate and creatinine - CY, RO and pooled
# used only for the demo, questionnaire etc.
LR_status01_x1_country<-function(var, dataframe) {
  formula    <- as.formula(paste("status01 ~ country +", var))
  res.logist <- glm(formula, data = dataframe, family = binomial(link=logit))
}
# used with the concentrations
LR_status01_x1_country_CR<-function(var, dataframe) {
  formula    <- as.formula(paste("status01 ~ country + ln_creat_gPERlt_spot +", var))
  res.logist <- glm(formula, data = dataframe, family = binomial(link=logit))
}

# multivatiare for spot ng/L and creatinine - CY, RO and pooled
LR1_status01_multix_country_CR<-function(var, dataframe) {
  formula    <- as.formula(paste("status01 ~ country + age + BMI + ln_tsh + ln_ft4 + ln_creat_gPERlt_spot +", var))
  res.logist <- glm(formula, data = dataframe, family = binomial(link=logit))
}

LR2_status01_multix_country_CR<-function(var, dataframe) {
  formula    <- as.formula(paste("status01 ~ country + age + BMI + ln_tsh + ln_ft4 + ln_iod_ugPERlt_spot + ln_creat_gPERlt_spot +", var))
  res.logist <- glm(formula, data = dataframe, family = binomial(link=logit))
}

```
```{r DataImport, echo=FALSE, include=FALSE, warning=FALSE}
###CY data
#Questionnaire
CY_questionnaire<-read.csv("CY_20150714_Questionnaire_DF.csv", header=TRUE)
str(CY_questionnaire)

#taking just the variables we need -- see variables.r script
CY_quest<-CY_questionnaire[, c(quest_vars)]
str(CY_quest)

#Get BPA results - add ClxBPA and make creatinine adjustment 
#spot
CY_BPA_spot<-read.csv("CY_BPA_spot_rawdata_DF.csv", header=TRUE)
#sum of the BPA analogs - ClxBPA_ngPERlt_spot
CY_BPA_spot$ClxBPA_ngPERlt_spot<-with(CY_BPA_spot, ClBPA_ngPERlt_spot+Cl35_2BPA_ngPERlt_spot+Cl33_2BPA_ngPERlt_spot)
str(CY_BPA_spot)

#morn
CY_BPA_morn<-read.csv("CY_BPA_morn_rawdata_DF.csv", header=TRUE)
#sum of the BPA analogs - ClxBPA_ngPERlt_morn
CY_BPA_morn$ClxBPA_ngPERlt_morn<-with(CY_BPA_morn, ClBPA_ngPERlt_morn+Cl35_2BPA_ngPERlt_morn+Cl33_2BPA_ngPERlt_morn)
str(CY_BPA_morn)

#merging spot and morning BPA
CY_all_BPA<-merge(CY_BPA_spot, CY_BPA_morn)
str(CY_all_BPA)
summary(CY_all_BPA)

#Get the iodine data - incl. the creatinine adjustment of iodine
#and the creatinine concentrations
CY_iod<-read.csv("CY_creat_iodine_DF.csv", header=TRUE)
str(CY_iod)

#Merging all the dfs with the measurements
CY_meas<-merge(CY_all_BPA, CY_iod)
#take only the ngPERlt, creatinine and ngPERg concentrations  NOTE: iodine also taken - 
CY_meas_SHRT<-CY_meas[grep("_ngPERlt|_ngPERg|_gPERlt|iod|CODE",colnames(CY_meas))]

#creatinine adjustment - SPOT
CY_meas_SHRT$BPF_ngPERg_spot<-with(CY_meas_SHRT, creatine_adj(BPF_ngPERlt_spot, creat_gPERlt_spot))
CY_meas_SHRT$BPA_ngPERg_spot<-with(CY_meas_SHRT, creatine_adj(BPA_ngPERlt_spot, creat_gPERlt_spot))
CY_meas_SHRT$ClBPA_ngPERg_spot<-with(CY_meas_SHRT, creatine_adj(ClBPA_ngPERlt_spot, creat_gPERlt_spot))
CY_meas_SHRT$Cl35_2BPA_ngPERg_spot<-with(CY_meas_SHRT, creatine_adj(Cl35_2BPA_ngPERlt_spot, creat_gPERlt_spot))
CY_meas_SHRT$Cl33_2BPA_ngPERg_spot<-with(CY_meas_SHRT, creatine_adj(Cl33_2BPA_ngPERlt_spot, creat_gPERlt_spot))
CY_meas_SHRT$ClxBPA_ngPERg_spot<-with(CY_meas_SHRT, creatine_adj(ClxBPA_ngPERlt_spot, creat_gPERlt_spot))

#creatinine adjustment - MORN
CY_meas_SHRT$BPF_ngPERg_morn<-with(CY_meas_SHRT, creatine_adj(BPF_ngPERlt_morn, creat_gPERlt_morn))
CY_meas_SHRT$BPA_ngPERg_morn<-with(CY_meas_SHRT, creatine_adj(BPA_ngPERlt_morn, creat_gPERlt_morn))
CY_meas_SHRT$ClBPA_ngPERg_morn<-with(CY_meas_SHRT, creatine_adj(ClBPA_ngPERlt_morn, creat_gPERlt_morn))
CY_meas_SHRT$Cl35_2BPA_ngPERg_morn<-with(CY_meas_SHRT, creatine_adj(Cl35_2BPA_ngPERlt_morn, creat_gPERlt_morn))
CY_meas_SHRT$Cl33_2BPA_ngPERg_morn<-with(CY_meas_SHRT, creatine_adj(Cl33_2BPA_ngPERlt_morn, creat_gPERlt_morn))
CY_meas_SHRT$ClxBPA_ngPERg_morn<-with(CY_meas_SHRT, creatine_adj(ClxBPA_ngPERlt_morn, creat_gPERlt_morn))

str(CY_meas_SHRT)
summary(CY_meas_SHRT)

###Get the RO data
RO_questionnaire<-read.csv("RO_Questionnaire_150711_DF.csv", header=TRUE)
str(RO_questionnaire)
#taking just the variables we need -- see variables.r script
RO_quest<-RO_questionnaire[, c(quest_vars)]
str(RO_quest)

RO_iod<-read.csv("RO_creat_iodine_DF.csv", header=TRUE)
str(RO_iod)

RO_BPA<-read.csv("RO_BPA_rawdata.csv", header=TRUE)
#sum of the BPA analogs - ClxBPA_ngPERlt_spot
RO_BPA$ClxBPA_ngPERlt_spot<-with(RO_BPA, ClBPA_ngPERlt_spot+Cl35_2BPA_ngPERlt_spot+Cl33_2BPA_ngPERlt_spot)

RO_meas<-merge(RO_BPA, RO_iod)
#take only the ngPERlt, creatinine and ngPERg concentrations (the latter are only available for iodine) - NOTE: iodine also taken 
RO_meas_SHRT<-RO_meas[grep("_ngPERg|_ngPERlt|_gPERlt|iod_|CODE",colnames(RO_meas))]

#make the creatinine-adjusted concentrations for the BPA etc.

RO_meas_SHRT$BPF_ngPERg_spot<-with(RO_meas_SHRT, creatine_adj(BPF_ngPERlt_spot, creat_gPERlt_spot))
RO_meas_SHRT$BPA_ngPERg_spot<-with(RO_meas_SHRT, creatine_adj(BPA_ngPERlt_spot, creat_gPERlt_spot))
RO_meas_SHRT$ClBPA_ngPERg_spot<-with(RO_meas_SHRT, creatine_adj(ClBPA_ngPERlt_spot, creat_gPERlt_spot))
RO_meas_SHRT$Cl35_2BPA_ngPERg_spot<-with(RO_meas_SHRT, creatine_adj(Cl35_2BPA_ngPERlt_spot, creat_gPERlt_spot))
RO_meas_SHRT$Cl33_2BPA_ngPERg_spot<-with(RO_meas_SHRT, creatine_adj(Cl33_2BPA_ngPERlt_spot, creat_gPERlt_spot))
RO_meas_SHRT$ClxBPA_ngPERg_spot<-with(RO_meas_SHRT, creatine_adj(ClxBPA_ngPERlt_spot, creat_gPERlt_spot))

str(RO_meas_SHRT)
summary(RO_meas_SHRT)
#merge the measurements
meas_SHRT<-rbind.fill(CY_meas_SHRT, RO_meas_SHRT)
summary(meas_SHRT)

##Merge the questionnaires to make the clean up and handle the variables and split them up again
questionnaires<-rbind(CY_quest, RO_quest)
#str(questionnaires)
```

## Questionnaire variable recoding
List of the final external exposure measurement variables are: 

* Water consumption from 20L PC bottles
* Weekly use of microwave (with plastic container?)
* Weekly consumption of canned food (number of portions)
* Duration of cleaning activities (as proxy to chlorine exposures)
* Weekly use of PCPs (sum of: shampoo, conditioner, body lotion, shower gel, hair dye, hair foam, hair spray, nail polish, lip care products, face cream) 
* Weekly use of perfume and deodorant
* Weekly use of perfume and deodorant and weekly use of cosmetics (makeup, lipstick, eyeliner, rouge, mascara, makeup remover).

```{r QuestVarRecoding, echo=FALSE, include=FALSE,  hide=TRUE}
#'Missing values handling
questionnaires[questionnaires==999]<-NA

questionnaires[, c("liqdeowc","soldeowc","mkremw")][questionnaires[, c("liqdeowc","soldeowc","mkremw")]==99]<- NA
questionnaires[, c("marstat","edu","smokst","alccons","ref20l","reffr20l")][questionnaires[, c("marstat","edu","smokst","alccons","ref20l","reffr20l")]==9]<- NA

#make BMI variable
questionnaires$heightM=(questionnaires$height/100)
questionnaires$BMI<-round(questionnaires$weight/(questionnaires$heightM^2), digits=2)
#add BMI categories
questionnaires$BMI_cat=(cut(questionnaires$BMI, breaks=c(0,18.4,24.9,29.9,Inf), 
                      labels=c("Underweight","Normal","Overweight","Obese")))

#making the categorical variables as factors and renaming them
questionnaires$marstat<-factor(questionnaires$marstat)
questionnaires$marstat<-revalue(questionnaires$marstat, c("9"="NA","1"="Single","2"="Married","3"="Divorced","4"="Widow","5"="Other"))

questionnaires$edu<-factor(questionnaires$edu)
questionnaires$edu<-revalue(questionnaires$edu, c("9"="NA","1"="Primary","2"="Secondary","3"="University","4"="University","5"="Other"))

questionnaires$smokst<-factor(questionnaires$smokst)
questionnaires$smokst<-revalue(questionnaires$smokst, c("9"="NA","0"="Never","1"="Currently","2"="Past"))

questionnaires$alccons<-factor(questionnaires$alccons)
questionnaires$alccons<-revalue(questionnaires$alccons, c("9"="NA","0"="Never/Rarely","1"="Weekend","2"="Often"))

questionnaires$antitg<-factor(questionnaires$antitg)
questionnaires$antitg<-revalue(questionnaires$antitg, c("9"="NA","0"="Negative","1"="Positive"))

questionnaires$antitpo<-factor(questionnaires$antitpo)
questionnaires$antitpo<-revalue(questionnaires$antitpo, c("9"="NA","0"="Negative","1"="Positive"))

questionnaires$ref20l<-factor(questionnaires$ref20l)
questionnaires$ref20l<-revalue(questionnaires$ref20l, c("9"="NA","0"="No","1"="Yes"))

questionnaires$reffr20l<-factor(questionnaires$reffr20l)
questionnaires$reffr20l<-revalue(questionnaires$reffr20l, c("9"="NA","0"="0to10times","1"="10to20times","2"="21to50times","3"="morethan50times"))

#YES/NO - factor variables that are continuous taken to be changed to factors
quest_YN_cat<-which(names(questionnaires)%in%c(
  "ref20l",
  #use of chlorine - categorical
  "dshwshchl","mopchl","toilchl","bthrchl","winchl","othcchl"))

questionnaires[, quest_YN_cat] <- lapply(questionnaires[, quest_YN_cat], as.factor)

#functions for quest recoding
daily20lglass<-function(x){
  with(x, ((dwh20l*glh20l)+(dwbh20l*glbh20lb)+(dwa20l*gla20l)+(dwba20l*glba20lb))/7)}

weekPORTIONScanned<-function(x){
  with(x, ((tommc+tunamc+cornmc+stdrmc+vegmc+fishmc+meatmc+frtmc+cnotmc)/4))}

dailyMINScleaning<-function(x){
  with(x, ((dshwshm*dshwshsd)+(mopm*mopd)+(toilm*toild)+(bthrm*bthrd)+(winm*wind)+(othcm*othcd))/30)}

weekMINScleaning<-function(x){
  with(x, ((dshwshm*dshwshsd)+(mopm*mopd)+(toilm*toild)+(bthrm*bthrd)+(winm*wind)+(othcm*othcd))/4)}

dailyPORTIONSpcps<-function(x){
  with(x, ((shmpwc+conrwc+bylnwc+srglwc+hdspwc+hrfmwc+ hrdewc+hrsywc+nlphwc+lppnwc+fecmwc+liqdeowc+soldeowc+sprdeowc)/7))}

weekPORTIONSpcps<-function(x){
  with(x, ((shmpwc+conrwc+bylnwc+srglwc+hdspwc+hrfmwc+ hrdewc+hrsywc+nlphwc+lppnwc+fecmwc)))}

weekPORTIONSdeo<-function(x){
  with(x, (liqdeowc+soldeowc+sprdeowc))}

dailyPORTIONScosm<-function(x){
  with(x, ((mkupw+lpstw+eylnw+rougw+mascw+mkremw)/7))}

weekPORTIONScosm<-function(x){
  with(x, (mkupw+lpstw+eylnw+rougw+mascw+mkremw))}
####

#summary variable for the water
questionnaires$daily20lglass<-daily20lglass(questionnaires)
#weekly consuption of canned food
questionnaires$weekPORTIONScanned<-weekPORTIONScanned(questionnaires)
#cleaning duration day
questionnaires$dailyMINScleaning<-dailyMINScleaning(questionnaires)
#cleaning duration weekly
questionnaires$weekMINScleaning<-weekMINScleaning(questionnaires)
#daily use of PCPs - incl. deodorant
questionnaires$dailyPORTIONSpcps<-dailyPORTIONSpcps(questionnaires)
#pcps use weekly - excl. deodorant
questionnaires$weekPORTIONSpcps<-weekPORTIONSpcps(questionnaires)
#deodorant weekly use
questionnaires$weekPORTIONSdeo<-weekPORTIONSdeo(questionnaires)
#daily use of cosmetics
questionnaires$dailyPORTIONScosm<-dailyPORTIONScosm(questionnaires)
#weekly use of cosmetics
questionnaires$weekPORTIONScosm<-weekPORTIONScosm(questionnaires)

#Get a short version of the questionnaire  
questionnaires_SHRT<-questionnaires[,c(quest_analysis_vars)] 

#split by country to be used separately in the respective analyses
CY_quest_SHRT<-questionnaires_SHRT[which(questionnaires_SHRT$country=='CY'),]
summary(CY_quest_SHRT)
RO_quest_SHRT<-questionnaires_SHRT[which(questionnaires_SHRT$country=="RO"),]
summary(RO_quest_SHRT)
```
``` {r DFs_merge, echo=FALSE, include=FALSE}
#merge the measurements with the questionnaire
CY_SHRT<-merge(CY_quest_SHRT, CY_meas_SHRT)
RO_SHRT<-merge(RO_quest_SHRT, RO_meas_SHRT)

SHRT<-rbind.fill(CY_SHRT, RO_SHRT)
```

```{r BoxQQplotsShapiroCODE, echo=FALSE, warning=FALSE}
#normality test
#get only numeric variables of the measurement_data df to perform the shapiro test
norm_test_df<-sapply(SHRT, is.numeric)
norm_test_df2<-SHRT[,norm_test_df]
df.shapiro<-lapply(norm_test_df2,shapiro.test)

#Check for normality - using boxplots and QQplots
#get the raw urine variables
raw_vars<-SHRT[ ,c("CODE", urine_levs_vars)]

#melt the raw markers dataframe to  make the QQplots
raw_vars_melt<-melt(raw_vars) #the status used as id variables
raw_vars_melt$ln_value<-log(raw_vars_melt$value)

raw_vars_ggplot<-ggplot(data=raw_vars_melt)+
  facet_wrap(~variable, scales="free",ncol=6)+
  theme(text=element_text(size=12),title=element_text(size=16,face="bold"))
#get the raw hormone variables
horm_raw_vars<-SHRT[ ,c("CODE", c(horm_vars_iod_sp,"iod_ugPERlt_morn","iod_ugPERg_spot","iod_ugPERg_morn"))]
horm_raw_vars_melt<-melt(horm_raw_vars)
horm_raw_vars_melt$ln_value<-log(horm_raw_vars_melt$value)

horm_raw_vars_ggplot<-ggplot(data=horm_raw_vars_melt)+
  facet_wrap(~variable, scales="free",ncol=2)+
  theme(text=element_text(size=12),title=element_text(size=16,face="bold"))
```

```{r BoxQQplotsFIG, echo=FALSE, include=FALSE, fig.width=12, fig.height=8, warning=FALSE}

kable(t(sapply(df.shapiro, '[', c("statistic","p.value"))), caption="Shapiro test for normality results.")
writeLines("Boxplots for the compounds measured in urine\n")
raw_vars_ggplot+geom_boxplot(mapping = aes(x=variable, y=value))+ggtitle("Non-transformed variables")
raw_vars_ggplot+stat_qq(aes(sample=value))+ggtitle("Non-transformed variables")
raw_vars_ggplot+geom_boxplot(mapping = aes(x=variable, y=ln_value))+ggtitle("Log-transformed variables")
raw_vars_ggplot+stat_qq(aes(sample=ln_value))+ggtitle("Log-transformed variables")
writeLines("Boxplots for the hormones and iodine levels")
horm_raw_vars_ggplot+geom_boxplot(mapping = aes(x=variable, y=value))+ggtitle("Non-transformed variables")
horm_raw_vars_ggplot+stat_qq(aes(sample=value))+ggtitle("Non-transformed variables")
horm_raw_vars_ggplot+geom_boxplot(mapping = aes(x=variable, y=ln_value))+ggtitle("Log-transformed variables")
horm_raw_vars_ggplot+stat_qq(aes(sample=ln_value))+ggtitle("Log-transformed variables")
```

```{r SHRT_additional_cleanup, echo=FALSE, include=FALSE, warning=FALSE}
#Make a big dataframe with the CY_SHRT and RO_SHRT to ln-transform the urine variables add the status01 variable and use in the stats where the whole population is needed
#SHRT will be separated again as CY_log_SHRT and RO_log_SHRT to be used in the regressions and other stats where log variables are necessary

#add the status01
SHRT$status01[SHRT$status=="CA"]<-"1"
SHRT$status01[SHRT$status=="CO"]<-"0"
SHRT$status01<-as.factor(SHRT$status01)
#make the ln-transformed variables
SHRT$ln_BPF_ngPERlt_spot<-log(SHRT$BPF_ngPERlt_spot)
SHRT$ln_BPA_ngPERlt_spot<-log(SHRT$BPA_ngPERlt_spot)
SHRT$ln_ClBPA_ngPERlt_spot<-log(SHRT$ClBPA_ngPERlt_spot)
SHRT$ln_Cl35_2BPA_ngPERlt_spot<-log(SHRT$Cl35_2BPA_ngPERlt_spot)
SHRT$ln_Cl33_2BPA_ngPERlt_spot<-log(SHRT$Cl33_2BPA_ngPERlt_spot)
SHRT$ln_ClxBPA_ngPERlt_spot<-log(SHRT$ClxBPA_ngPERlt_spot)

SHRT$ln_BPF_ngPERg_spot<-log(SHRT$BPF_ngPERg_spot)
SHRT$ln_BPA_ngPERg_spot<-log(SHRT$BPA_ngPERg_spot)
SHRT$ln_ClBPA_ngPERg_spot<-log(SHRT$ClBPA_ngPERg_spot)
SHRT$ln_Cl35_2BPA_ngPERg_spot<-log(SHRT$Cl35_2BPA_ngPERg_spot)
SHRT$ln_Cl33_2BPA_ngPERg_spot<-log(SHRT$Cl33_2BPA_ngPERg_spot)
SHRT$ln_ClxBPA_ngPERg_spot<-log(SHRT$ClxBPA_ngPERg_spot)

SHRT$ln_BPF_ngPERlt_morn<-log(SHRT$BPF_ngPERlt_morn)
SHRT$ln_BPA_ngPERlt_morn<-log(SHRT$BPA_ngPERlt_morn)
SHRT$ln_ClBPA_ngPERlt_morn<-log(SHRT$ClBPA_ngPERlt_morn)
SHRT$ln_Cl35_2BPA_ngPERlt_morn<-log(SHRT$Cl35_2BPA_ngPERlt_morn)
SHRT$ln_Cl33_2BPA_ngPERlt_morn<-log(SHRT$Cl33_2BPA_ngPERlt_morn)
SHRT$ln_ClxBPA_ngPERlt_morn<-log(SHRT$ClxBPA_ngPERlt_morn)

SHRT$ln_BPF_ngPERg_morn<-log(SHRT$BPF_ngPERg_morn)
SHRT$ln_BPA_ngPERg_morn<-log(SHRT$BPA_ngPERg_morn)
SHRT$ln_ClBPA_ngPERg_morn<-log(SHRT$ClBPA_ngPERg_morn)
SHRT$ln_Cl35_2BPA_ngPERg_morn<-log(SHRT$Cl35_2BPA_ngPERg_morn)
SHRT$ln_Cl33_2BPA_ngPERg_morn<-log(SHRT$Cl33_2BPA_ngPERg_morn)
SHRT$ln_ClxBPA_ngPERg_morn<-log(SHRT$ClxBPA_ngPERg_morn)

SHRT$ln_tsh<-log(SHRT$tsh)
SHRT$ln_ft4<-log(SHRT$ft4)

SHRT$ln_iod_ugPERlt_spot<-log(SHRT$iod_ugPERlt_spot)
SHRT$ln_iod_ugPERg_spot<-log(SHRT$iod_ugPERg_spot)
SHRT$ln_iod_ugPERlt_morn<-log(SHRT$iod_ugPERlt_morn)
SHRT$ln_iod_ugPERg_morn<-log(SHRT$iod_ugPERg_morn)

SHRT$ln_creat_gPERlt_spot<-log(SHRT$creat_gPERlt_spot)
SHRT$ln_creat_gPERlt_morn<-log(SHRT$creat_gPERlt_morn)

summary(SHRT)

CY_log_SHRT<-SHRT[which(SHRT$country=='CY'),]
#summary(CY_log_SHRT)
RO_log_SHRT<-SHRT[which(SHRT$country=="RO"),]
#summary(RO_log_SHRT)
```

```{r CountryDFs_DEMO, echo=FALSE, include=FALSE, warning=TRUE}
#CY
#categories for TSH and fT4
CY_SHRT$tsh_cat=(cut(CY_SHRT$tsh, breaks=c(0,0.399,3.999,Inf),
                  labels=c("<0.4","0.4-4",">4")))
CY_SHRT$ft4_cat=(cut(CY_SHRT$ft4, breaks=c(0,9.999,23.999,Inf), 
                  labels=c("<10","10-24",">24")))
#tables with the descriptives overall and by status
CY_demo_overall<-CreateTableOne(vars=demo_vars, data=CY_SHRT)
CY_demo_by_status<-CreateTableOne(vars=demo_vars, strata=c("status"), data=CY_SHRT)

CY_expo_overall<-CreateTableOne(vars=expo_vars, data=CY_SHRT)
CY_expo_by_status<-CreateTableOne(vars=expo_vars, strata=c("status"), data=CY_SHRT)

###WORKING ON RO
#categories for TSH and fT4
RO_SHRT$tsh_cat=(cut(RO_SHRT$tsh, breaks=c(0,0.2799,4.1999,Inf),
                     labels=c("<0.27","0.27-4.2",">4.2")))
RO_SHRT$ft4_cat=(cut(RO_SHRT$ft4, breaks=c(0,10.699,22.699,Inf), 
                     labels=c("<10.6","10.6-22.7",">22.7")))
#tables with the descriptives overall and by status
RO_demo_overall<-CreateTableOne(vars=demo_vars, data=RO_SHRT)
RO_demo_by_status<-CreateTableOne(vars=demo_vars,strata=c("status"), data=RO_SHRT)

RO_expo_overall<-CreateTableOne(vars=expo_vars, data=RO_SHRT)
RO_expo_by_status<-CreateTableOne(vars=expo_vars, strata=c("status"), data=RO_SHRT)

CY1=print(CY_demo_overall)
CY1_status=print(CY_demo_by_status, exact= c("BMI_cat","alccons", "marstat","edu"))
CY2=print(CY_expo_overall, nonnormal = expo_vars)
CY2_status=print(CY_expo_by_status, nonnormal = expo_vars)

RO1=print(RO_demo_overall)
RO1_status=print(RO_demo_by_status, exact= c("BMI_cat","alccons", "marstat","edu"))#
RO2=print(RO_expo_overall, nonnormal = expo_vars)
RO2_status=print(RO_expo_by_status, nonnormal = expo_vars)
```

```{r CountryDFs_HORM, echo=FALSE, include=FALSE}
#CY
#note: the CY hormones descriptive table included also the iodine for the morning samples and the comparisons
CY_horm_table<-CreateContTable(c(horm_vars_iod_sp, "iod_ugPERlt_morn","iod_ugPERg_spot", "iod_ugPERg_morn"),data=CY_SHRT)
CY_horm_table_by_status<-CreateContTable(c(horm_vars_iod_sp, "iod_ugPERlt_morn","iod_ugPERg_spot", "iod_ugPERg_morn"),strata=c("status"),data=CY_SHRT)

#added to make table 1 -160822
CY_horm_table_spot<-CreateContTable(c(horm_vars_iod_sp, "iod_ugPERg_spot"),data=CY_SHRT)

CY_horm_table_by_status_spot<-CreateContTable(c(horm_vars_iod_sp, "iod_ugPERg_spot"),strata=c("status"),data=CY_SHRT)

CY_horm_cat_table<-CreateCatTable(horm_vars_cat,data=CY_SHRT)
CY_horm_cat_table_by_status<-CreateCatTable(horm_vars_cat,strata=c("status"),data=CY_SHRT)

#RO
RO_horm_table<-CreateContTable(c(horm_vars_iod_sp,"iod_ugPERg_spot"),data=RO_SHRT)
RO_horm_table_by_status<-CreateContTable(c(horm_vars_iod_sp,"iod_ugPERg_spot"),strata=c("status"),data=RO_SHRT)

RO_horm_cat_table<-CreateCatTable(horm_vars_cat,data=RO_SHRT)
RO_horm_cat_table_by_status<-CreateCatTable(horm_vars_cat,strata=c("status"),data=RO_SHRT)
```
# Cyprus 
```{r CY_hormdescr, echo=FALSE, include=FALSE}
CY_horm_kable=print(CY_horm_table, nonnormal = c(horm_vars_iod_sp, "iod_ugPERlt_morn","iod_ugPERg_spot", "iod_ugPERg_morn"))
CY_horm_status_kable=print(CY_horm_table_by_status, nonnormal = c(horm_vars_iod_sp, "iod_ugPERlt_morn","iod_ugPERg_spot", "iod_ugPERg_morn"))

#added to make table 1
CY_horm_spot_kable=print(CY_horm_table_spot, nonnormal = c(horm_vars_iod_sp, "iod_ugPERg_spot"))

CY_horm_status_spot_kable=print(CY_horm_table_by_status_spot, nonnormal = c(horm_vars_iod_sp, "iod_ugPERg_spot"))
CY_horm_cat_kable=print(CY_horm_cat_table)
CY_horm_cat_status_kable=print(CY_horm_cat_table_by_status)

```
```{r CY_HormPlots, fig.width=6, fig.height=3,echo=FALSE, include=TRUE, fig.show='asis', warning=FALSE}
#hormone boxplots
CY_TSH<-ggplot(CY_SHRT, aes(factor(status), tsh, fill=status))
CY_TSH+geom_boxplot()+
  labs(x="Status", y="TSH, mIU/L", title="TSH levels (CY)")+
  geom_hline(aes(yintercept=0.4), color="green")+
  geom_hline(aes(yintercept=4), color="green")+ 
  theme(text = element_text(size=20))

CY_FT4<-ggplot(CY_SHRT, aes(factor(status), ft4,fill=status))
CY_FT4+geom_boxplot()+
  labs(x="Status", y="FT4, pmol/L", title="FT4 levels (CY)")+
  geom_hline(aes(yintercept=10), color="orange")+
  geom_hline(aes(yintercept=24), color="orange")+ 
  theme(text = element_text(size=20))
```

```{r CY_UrineMeasDescriptivesCODE, echo=FALSE, include=FALSE,  hide=TRUE,warning=FALSE}
#Urine measurements analysis
#CY
CY_urine_lev_table<-CreateContTable(c(urine_levs_vars), funcAdditional = list(gm=gm_func, gsd=gsd_func), data=CY_SHRT)
CY_Urine_lev_table_by_status<-CreateContTable(c(urine_levs_vars), funcAdditional = list(gm=gm_func, gsd=gsd_func), strata=c("status"), data=CY_SHRT)

CY_U1=print(CY_urine_lev_table, nonnormal= c(urine_levs_vars))
CY_U2=print(CY_Urine_lev_table_by_status, nonnormal=c(urine_levs_vars))

CY_urine_lev_table_spot<-CreateContTable(urine_levs_vars[c(1:12)], funcAdditional = list(gm=gm_func, gsd=gsd_func), data=CY_SHRT)
CY_Urine_lev_table_spot_by_status<-CreateContTable(urine_levs_vars[c(1:12)], funcAdditional = list(gm=gm_func, gsd=gsd_func), strata=c("status"), data=CY_SHRT)

CY_U1_spot=print(CY_urine_lev_table_spot, nonnormal= c(urine_levs_vars))
CY_U2_spot=print(CY_Urine_lev_table_spot_by_status, nonnormal=c(urine_levs_vars))
```
```{r SampleTypeReprod, echo=FALSE, include=FALSE, warning=FALSE}
###ONLY FOR CY
#PAIRED COMPARISON - WITH T-TEST - BETWEEN THE MORNING AND THE SPOT SAMPLES
#ICC CALCULATION
#------------------
ln_BPF_df_ngPERlt<-CY_log_SHRT[ ,c("status","CODE","ln_BPF_ngPERlt_spot","ln_BPF_ngPERlt_morn")]
ln_BPF_df_ngPERlt_melt<-melt(ln_BPF_df_ngPERlt, value.name="ln_BPF_ngPERlt", variable.name="BPF1_sample_type")

ln_BPA_df_ngPERlt<-CY_log_SHRT[ ,c("status","CODE","ln_BPA_ngPERlt_spot","ln_BPA_ngPERlt_morn")]
ln_BPA_df_ngPERlt_melt<-melt(ln_BPA_df_ngPERlt, value.name="ln_BPA_ngPERlt", variable.name="BPA1_sample_type")

ln_ClBPA_df_ngPERlt<-CY_log_SHRT[ ,c("status","CODE","ln_ClBPA_ngPERlt_spot","ln_ClBPA_ngPERlt_morn")]
ln_ClBPA_df_ngPERlt_melt<-melt(ln_ClBPA_df_ngPERlt, value.name="ln_ClBPA_ngPERlt", variable.name="ClBPA1_sample_type")

ln_Cl35_2BPA_df_ngPERlt<-CY_log_SHRT[ ,c("status","CODE","ln_Cl35_2BPA_ngPERlt_spot","ln_Cl35_2BPA_ngPERlt_morn")]
ln_Cl35_2BPA_df_ngPERlt_melt<-melt(ln_Cl35_2BPA_df_ngPERlt, value.name="ln_Cl35_2BPA_ngPERlt", variable.name="Cl35_2BPA1_sample_type")

ln_Cl33_2BPA_df_ngPERlt<-CY_log_SHRT[ ,c("status","CODE","ln_Cl33_2BPA_ngPERlt_spot","ln_Cl33_2BPA_ngPERlt_morn")]
ln_Cl33_2BPA_df_ngPERlt_melt<-melt(ln_Cl33_2BPA_df_ngPERlt, value.name="ln_Cl33_1BPA_ngPERlt", variable.name="Cl33_2BPA1_sample_type")

ln_ClxBPA_df_ngPERlt<-CY_log_SHRT[ ,c("status","CODE","ln_ClxBPA_ngPERlt_spot","ln_ClxBPA_ngPERlt_morn")]
ln_ClxBPA_df_ngPERlt_melt<-melt(ln_ClxBPA_df_ngPERlt, value.name="ln_ClxBPA_ngPERlt", variable.name="ClxBPA1_sample_type")


ln_BPF_df_ngPERg<-CY_log_SHRT[ ,c("status","CODE","ln_BPF_ngPERg_spot","ln_BPF_ngPERg_morn")]
ln_BPF_df_ngPERg_melt<-melt(ln_BPF_df_ngPERg, value.name="ln_BPF_ngPERg", variable.name="BPF2_sample_type")

ln_BPA_df_ngPERg<-CY_log_SHRT[ ,c("status","CODE","ln_BPA_ngPERg_spot","ln_BPA_ngPERg_morn")]
ln_BPA_df_ngPERg_melt<-melt(ln_BPA_df_ngPERg, value.name="ln_BPA_ngPERg", variable.name="BPA2_sample_type")

ln_ClBPA_df_ngPERg<-CY_log_SHRT[ ,c("status","CODE","ln_ClBPA_ngPERg_spot","ln_ClBPA_ngPERg_morn")]
ln_ClBPA_df_ngPERg_melt<-melt(ln_ClBPA_df_ngPERg, value.name="ln_ClBPA_ngPERg", variable.name="ClBPA2_sample_type")

ln_Cl35_2BPA_df_ngPERg<-CY_log_SHRT[ ,c("status","CODE","ln_Cl35_2BPA_ngPERg_spot","ln_Cl35_2BPA_ngPERg_morn")]
ln_Cl35_2BPA_df_ngPERg_melt<-melt(ln_Cl35_2BPA_df_ngPERg, value.name="ln_Cl35_2BPA_ngPERg", variable.name="Cl35_2BPA2_sample_type")

ln_Cl33_2BPA_df_ngPERg<-CY_log_SHRT[ ,c("status","CODE","ln_Cl33_2BPA_ngPERg_spot","ln_Cl33_2BPA_ngPERg_morn")]
ln_Cl33_2BPA_df_ngPERg_melt<-melt(ln_Cl33_2BPA_df_ngPERg, value.name="ln_Cl33_2BPA_ngPERg", variable.name="Cl33_2BPA2_sample_type")

ln_ClxBPA_df_ngPERg<-CY_log_SHRT[ ,c("status","CODE","ln_ClxBPA_ngPERg_spot","ln_ClxBPA_ngPERg_morn")]
ln_ClxBPA_df_ngPERg_melt<-melt(ln_ClxBPA_df_ngPERg, value.name="ln_ClxBPA_ngPERg", variable.name="ClxBPA2_sample_type")

ICC_df<-cbind(ln_BPF_df_ngPERlt_melt, ln_BPA_df_ngPERlt_melt, ln_ClBPA_df_ngPERlt_melt, ln_Cl35_2BPA_df_ngPERlt_melt, ln_Cl33_2BPA_df_ngPERlt_melt,  ln_ClxBPA_df_ngPERlt_melt, ln_BPF_df_ngPERg_melt, ln_BPA_df_ngPERg_melt, ln_ClBPA_df_ngPERg_melt, ln_Cl35_2BPA_df_ngPERg_melt, ln_Cl33_2BPA_df_ngPERg_melt, ln_ClxBPA_df_ngPERg_melt)

ICC_df$sample_type[ICC_df$BPF1_sample_type=="ln_BPF_ngPERlt_spot"]<-"spot"
ICC_df$sample_type[ICC_df$BPF1_sample_type=="ln_BPF_ngPERlt_morn"]<-"morning"

ICC_df2<-ICC_df[,c(1,2,4,8,12,16,20,24,28,32,36,40,44,48,49)]

ICCest_participant_S<-t(sapply(ICC_df2[c(-1,-2,-15)], function(x) unlist(ICCest(ICC_df2$CODE, x, alpha=0.05, CI.type="S")[c("ICC","LowerCI","UpperCI")])))

```
```{r PairedTests,echo=FALSE, include=TRUE,  hide=TRUE, warning=FALSE}
#Paired t_tests
paired_t_test<-
  function (var1, var2) {unlist(t.test(var1, var2, paired = TRUE, na.rm=TRUE)[c("method","p.value")])}

#ngPERlt
BPF_ngPERlt<-paired_t_test(CY_log_SHRT$ln_BPF_ngPERlt_spot, CY_log_SHRT$ln_BPF_ngPERlt_morn)
BPA_ngPERlt<-paired_t_test(CY_log_SHRT$ln_BPA_ngPERlt_spot, CY_log_SHRT$ln_BPA_ngPERlt_morn)
ClBPA_ngPERlt<-paired_t_test(CY_log_SHRT$ln_ClBPA_ngPERlt_spot, CY_log_SHRT$ln_ClBPA_ngPERlt_morn)
Cl35_2BPA_ngPERlt<-paired_t_test(CY_log_SHRT$ln_Cl35_2BPA_ngPERlt_spot, CY_log_SHRT$ln_Cl35_2BPA_ngPERlt_morn)
Cl33_2BPA_ngPERlt<-paired_t_test(CY_log_SHRT$ln_Cl33_2BPA_ngPERlt_spot, CY_log_SHRT$ln_Cl33_2BPA_ngPERlt_morn)
ClxBPA_ngPERlt<-paired_t_test(CY_log_SHRT$ln_ClxBPA_ngPERlt_spot, CY_log_SHRT$ln_ClxBPA_ngPERlt_morn)
#ngPERg
BPF_ngPERg<-paired_t_test(CY_log_SHRT$ln_BPF_ngPERg_spot, CY_log_SHRT$ln_BPF_ngPERg_morn)
BPA_ngPERg<-paired_t_test(CY_log_SHRT$ln_BPA_ngPERg_spot, CY_log_SHRT$ln_BPA_ngPERg_morn)
ClBPA_ngPERg<-paired_t_test(CY_log_SHRT$ln_ClBPA_ngPERg_spot, CY_log_SHRT$ln_ClBPA_ngPERg_morn)
Cl35_2BPA_ngPERg<-paired_t_test(CY_log_SHRT$ln_Cl35_2BPA_ngPERg_spot, CY_log_SHRT$ln_Cl35_2BPA_ngPERg_morn)
Cl33_2BPA_ngPERg<-paired_t_test(CY_log_SHRT$ln_Cl33_2BPA_ngPERg_spot, CY_log_SHRT$ln_Cl33_2BPA_ngPERg_morn)
ClxBPA_ngPERg<-paired_t_test(CY_log_SHRT$ln_ClxBPA_ngPERg_spot, CY_log_SHRT$ln_ClxBPA_ngPERg_morn)

CY_pooled_paired_t_test<-rbind(BPF_ngPERlt, BPA_ngPERlt, ClBPA_ngPERlt, Cl35_2BPA_ngPERlt, Cl33_2BPA_ngPERlt, ClxBPA_ngPERlt,
                               BPF_ngPERg, BPA_ngPERg, ClBPA_ngPERg, Cl35_2BPA_ngPERg, Cl33_2BPA_ngPERg, ClxBPA_ngPERg)

#separate cases and controls
CA_CY_log_SHRT<-CY_log_SHRT[which(CY_log_SHRT$status=='CA'),]
CO_CY_log_SHRT<-CY_log_SHRT[which(CY_log_SHRT$status=='CO'),]
```

## Table S4

```{r CY_UrineMeasDescriptives_paired_ICC, echo=FALSE, include=TRUE, warning=FALSE}
#printing the descriptives
kable(summary(CY_urine_lev_table), caption="CY - Summary of the urinary levels (all participants)")

#printing the ICC and paired t-tests
writeLines("Note: All the t-tests have been applied to the ln-transformed concentrations")
kable(cbind(CY_pooled_paired_t_test, ICCest_participant_S), caption = "Paired t-test p-values and ICC - spot and morning samples - CY (pooled population) - Table S4")
```

# Romania

```{r RO_hormdescr, echo=FALSE, include=FALSE}
RO_horm_kable=print(RO_horm_table, nonnormal = c(horm_vars_iod_sp,"iod_ugPERg_spot"))
RO_horm_status_kable=print(RO_horm_table_by_status, nonnormal = c(horm_vars_iod_sp,"iod_ugPERg_spot"))
RO_horm_cat_kable=print(RO_horm_cat_table)
RO_horm_cat_status_kable=print(RO_horm_cat_table_by_status)

```

```{r RO_HormPlots, fig.width=6, fig.height=3,echo=FALSE, include=TRUE, fig.show='asis', warning=FALSE}
RO_TSH<-ggplot(RO_SHRT, aes(factor(status), tsh, fill=status))
RO_TSH+geom_boxplot()+
  labs(x="Status", y="TSH, mIU/L", title="TSH levels (RO)")+
  geom_hline(aes(yintercept=0.27), color="green")+
  geom_hline(aes(yintercept=4.2), color="green")+ 
  theme(text = element_text(size=20))

RO_FT4<-ggplot(RO_SHRT, aes(factor(status), ft4,fill=status))
RO_FT4+geom_boxplot()+
  labs(x="Status", y="FT4, pmol/L", title="FT4 levels (RO)")+
  geom_hline(aes(yintercept=10.6), color="orange")+
  geom_hline(aes(yintercept=22.7), color="orange")+ 
  theme(text = element_text(size=20))
```

```{r RO_UrineMeasDescriptivesCODE, echo=FALSE, include=FALSE,  hide=TRUE,warning=FALSE}
#Urine measurements analysis
RO_urine_lev_table<-CreateContTable(urine_levs_vars,funcAdditional = list(gm=gm_func, gsd=gsd_func), data=RO_SHRT)
RO_Urine_lev_table_by_status<-CreateContTable(urine_levs_vars,funcAdditional = list(gm=gm_func, gsd=gsd_func), strata=c("status"), data=RO_SHRT)

RO_U1=print(RO_urine_lev_table, nonnormal=c(urine_levs_vars))#kable(RO_U1, caption="RO - Summary of the urinary levels (all participants)")
RO_U2=print(RO_Urine_lev_table_by_status, nonnormal=c(urine_levs_vars))#kable(RO_U2, caption="RO - Summary of the urinary levels (by status)")
```

# Working on the both countries (pooled population)

```{r PooledPopulation, echo=FALSE, include=FALSE, hide=TRUE, warning=FALSE}
#table with the descriptives overall and by status
SHRT_demo_overall<-CreateTableOne(vars=demo_vars, data=SHRT)
SHRT_demo_by_status<-CreateTableOne(vars=demo_vars, strata=c("status"), data=SHRT)
SHRT_demo_by_country<-CreateTableOne(vars=demo_vars, strata=c("country"), data=SHRT)

#table with the descriptives overall and by status
SHRT_expo_overall<-CreateTableOne(vars=expo_vars, data=SHRT)
SHRT_expo_by_status<-CreateTableOne(vars=expo_vars, strata=c("status"), data=SHRT)
SHRT_expo_by_country<-CreateTableOne(vars=expo_vars, strata=c("country"), data=SHRT)

#separate cases and controls
CA_SHRT<-SHRT[which(SHRT$status=='CA'),]
CO_SHRT<-SHRT[which(SHRT$status=="CO"),]

CreateTableOne(data=CA_SHRT)
CreateTableOne(data=CO_SHRT)

#Hormone and urine levels descriptives pooled population and by status (in the pooled)
horm_table<-CreateContTable(c(horm_vars_iod_sp,"iod_ugPERg_spot"),data=SHRT)
horm_table_by_status<-CreateContTable(c(horm_vars_iod_sp,"iod_ugPERg_spot"),strata=c("status"),data=SHRT)
horm_table_by_country<-CreateContTable(c(horm_vars_iod_sp,"iod_ugPERg_spot"),strata=c("country"),data=SHRT)

horm_cat_table<-CreateCatTable(horm_vars_cat,data=SHRT)
horm_cat_table_by_status<-CreateCatTable(horm_vars_cat,strata=c("status"),data=SHRT)
horm_cat_table_by_country<-CreateCatTable(horm_vars_cat,strata=c("country"),data=SHRT)


urine_lev_table<-CreateContTable(urine_levs_vars[c(1:12)],funcAdditional = list(gm=gm_func, gsd=gsd_func), data=SHRT)
urine_lev_status_table<-CreateContTable(urine_levs_vars [c(1:12)],funcAdditional = list(gm=gm_func, gsd=gsd_func), strata=c("status"),data=SHRT)
urine_lev_country_table<-CreateContTable(urine_levs_vars [c(1:12)],funcAdditional = list(gm=gm_func, gsd=gsd_func), strata=c("country"),data=SHRT)

### making tables by status with country comparison
#for the cases
CASES_country_table<-CreateContTable(urine_levs_vars[c(1:12)],funcAdditional = list(gm=gm_func, gsd=gsd_func), strata=c("country"), data=CA_SHRT)

#for the controls
CONTROLS_country_table<-CreateContTable(urine_levs_vars [c(1:12)],funcAdditional = list(gm=gm_func, gsd=gsd_func), strata=c("country"),data=CO_SHRT)
```

## Descriptives and comparisons - demographics, hormones, iodine and urinary levels  of BPA, BPF, ClxBPA
```{r descrtables, echo=FALSE, include=FALSE, hide=TRUE}
SH1=print(SHRT_demo_overall)
SH1_status=print(SHRT_demo_by_status, exact= c("BMI_cat","alccons", "marstat","edu"))
SH1_country=print(SHRT_demo_by_country, exact= c("BMI_cat","alccons", "marstat","edu"))

SH2=print(SHRT_expo_overall, nonnormal = expo_vars)
SH2_status=print(SHRT_expo_by_status, nonnormal = expo_vars)
SH2_country=print(SHRT_expo_by_country, nonnormal = expo_vars)

SH3=print(horm_table, nonnormal =c( horm_vars_iod_sp, "iod_ugPERg_spot"))
SH3_status=print(horm_table_by_status, nonnormal =c(horm_vars_iod_sp, "iod_ugPERg_spot"))
SH3_country=print(horm_table_by_country, nonnormal =c(horm_vars_iod_sp, "iod_ugPERg_spot"))

SH4=print(horm_cat_table)
SH4_status=print(horm_cat_table_by_status)
SH4_country=print(horm_cat_table_by_country)

SH5=print(urine_lev_table, nonnormal=c(urine_levs_vars))
SH5_status=print(urine_lev_status_table, nonnormal=c(urine_levs_vars))
SH5_country=print(urine_lev_country_table, nonnormal=c(urine_levs_vars))

SH6=print(CASES_country_table, nonnormal=c(urine_levs_vars))
SH7=print(CONTROLS_country_table, nonnormal=c(urine_levs_vars))
```

### Table 1, Table S1, Table S3

```{r SHRTComparisonsPRINT, echo=FALSE, include=TRUE, warning=FALSE}
#Overall table 1
kable(cbind(SH1, SH1_status, CY1, CY1_status, RO1, RO1_status), caption="Demographics (all study population - by status and in the two countries - Table 1)")

kable(cbind(SH3, SH3_status, CY_horm_spot_kable, CY_horm_status_spot_kable, RO_horm_kable, RO_horm_status_kable), caption="Hormone and iodine (all study population - by status and in the two countries) - Table 1")

kable(cbind(SH5, SH5_status, CY_U1_spot, CY_U2_spot, RO_U1, RO_U2), caption="BPA, BPF, ClxBPA (all study population - by status and in the two countries) - Table 1") 

kable(cbind(SH2,SH2_status, CY2, CY2_status, RO2, RO2_status), caption="Determinants of exposure (all study population - by status and in the two countries) - Table S1")
kable(cbind(SH4, SH4_status, SH4_country), caption="Antibodies (all study population) - Table S2")
kable(SH5_country, caption="BPA, BPF, ClxBPA - by country - Table S3")

kable(SH1_country, caption="Demographics by country")
kable(SH2_country, caption="Determinants of exposure by country")
kable(SH3_country, caption="Hormones and iodine levels by country")
```

## Logistic regressions
```{r PooledRegressions_UNI, echo=FALSE, include=TRUE, results='asis', warning=FALSE}
#Regressions
#univariate - simple
demo_vars_uni_models<-lapply(c(demo_vars[-5]),
                                function(x) LR_status01_x1(x, SHRT))
expo_vars_uni_models<-lapply(c(expo_vars),
                                function(x) LR_status01_x1(x, SHRT))
horm_vars_iod_sp_uni_models<-lapply(c("ln_tsh","ln_ft4","ln_iod_ugPERlt_spot"),
                                function(x) LR_status01_x1(x, SHRT))

ln_vars_spot_uni_models_CR<-lapply(c(ln_vars_spot[c(2,1,6)]),
                                   function(x) LR_status01_x1_spot_CR(x, SHRT))

#univariate - simple AND country adjustment
demo_vars_uni_country_models<-lapply(c(demo_vars[-5]),
                             function(x) LR_status01_x1_country(x, SHRT))
expo_vars_uni_country_models<-lapply(c(expo_vars),
                             function(x) LR_status01_x1_country(x, SHRT))
horm_vars_iod_sp_uni_country_models<-lapply(c("ln_tsh","ln_ft4","ln_iod_ugPERlt_spot"),
                             function(x) LR_status01_x1_country(x, SHRT))

ln_vars_spot_uni_country_models_CR<-lapply(c(ln_vars_spot[c(2,1,6)]),
                                function(x) LR_status01_x1_country_CR(x, SHRT))
```

### Table S6, Table S7

* Demographics - table S6 - A
`r sjt.glm(c(demo_vars_uni_models),group.pred = FALSE, showLogLik = TRUE, showAIC = TRUE, showFamily = TRUE, no.output = TRUE)$knitr`

* Questionnaire exposures - table S6 - A
`r sjt.glm(c(expo_vars_uni_models),group.pred = FALSE, showLogLik = TRUE, showAIC = TRUE, showFamily = TRUE, no.output = TRUE)$knitr`

* Hormone levels and iodine - table S6 - A
`r sjt.glm(c(horm_vars_iod_sp_uni_models),group.pred = FALSE, showLogLik = TRUE, showAIC = TRUE, showFamily = TRUE, no.output = TRUE)$knitr`

Study-site adjusted univariate logistic regression 

* Demographics - table S6 - B
`r sjt.glm(c(demo_vars_uni_country_models),group.pred = FALSE, showLogLik = TRUE, showAIC = TRUE, showFamily = TRUE, no.output = TRUE)$knitr`

* Questionnaire exposures - table S6 - B
`r sjt.glm(c(expo_vars_uni_country_models),group.pred = FALSE, showLogLik = TRUE, showAIC = TRUE, showFamily = TRUE, no.output = TRUE)$knitr`

* Hormone levels and iodine - table S6 - B
`r sjt.glm(c(horm_vars_iod_sp_uni_country_models),group.pred = FALSE, showLogLik = TRUE, showAIC = TRUE, showFamily = TRUE, no.output = TRUE)$knitr`

* BPF, BPA and ClxBPA - table S7
`r sjt.glm(c(ln_vars_spot_uni_country_models_CR),group.pred = FALSE, showLogLik = TRUE, showAIC = TRUE, showFamily = TRUE, no.output = TRUE)$knitr`

```{r 5to95df, echo=FALSE, include=FALSE}
#function for the 5-95 percentiles 
gettherange<-function(x){ 
  top=quantile(x, 0.95 ,na.rm=TRUE)
  bottom=quantile(x, 0.05 ,na.rm=TRUE)
  x[x>top|x<bottom]=NA
  return(x)} 

BPA_5to95_SHRT_0<-SHRT
BPA_5to95_SHRT_0$ln_BPA_ngPERlt_spot<-with(SHRT, gettherange(ln_BPA_ngPERlt_spot))
BPA_5to95_SHRT<-BPA_5to95_SHRT_0[complete.cases(BPA_5to95_SHRT_0[,c("ln_BPA_ngPERlt_spot")]),]

BPF_5to95_SHRT_0<-SHRT
BPF_5to95_SHRT_0$ln_BPF_ngPERlt_spot<-with(SHRT, gettherange(ln_BPF_ngPERlt_spot))
BPF_5to95_SHRT<-BPF_5to95_SHRT_0[complete.cases(BPF_5to95_SHRT_0[,c("ln_BPF_ngPERlt_spot")]),]

ClxBPA_5to95_SHRT_0<-SHRT
ClxBPA_5to95_SHRT_0$ln_ClxBPA_ngPERlt_spot<-with(SHRT, gettherange(ln_ClxBPA_ngPERlt_spot))
ClxBPA_5to95_SHRT<-ClxBPA_5to95_SHRT_0[complete.cases(ClxBPA_5to95_SHRT_0[,c("ln_ClxBPA_ngPERlt_spot")]),]

```

```{r PooledRegressions_MULTI, echo=FALSE, include=TRUE, results='asis', warning=FALSE}
#Multivariate regressions
#pooled population
LR1_multix_spot_models_CR<-lapply(c(ln_vars_spot[c(2,1,6)]),
                                  function(x) LR1_status01_multix_spot_CR(x, SHRT))           
                               
LR2_multix_spot_models_CR<-lapply(c(ln_vars_spot[c(2,1,6)]),
                                  function(x) LR2_status01_multix_spot_CR(x, SHRT))  

#country-adj
LR1_multix_country_spot_models_CR<-lapply(c(ln_vars_spot[c(2,1,6)]),
                               function(x) LR1_status01_multix_country_CR(x, SHRT))

LR2_multix_country_spot_models_CR<-lapply(c(ln_vars_spot[c(2,1,6)]),
                               function(x) LR2_status01_multix_country_CR(x, SHRT))

```

### Table 3

BPF, BPA and ClxBPA - table 3 - crude estimates
`r sjt.glm(c(ln_vars_spot_uni_models_CR),group.pred = FALSE, showLogLik = TRUE, showAIC = TRUE, showFamily = TRUE, no.output = TRUE)$knitr`


(A1) Adjusted for: age, BMI, TSH, FT4 - table 3
`r sjt.glm(c(LR1_multix_spot_models_CR),group.pred = FALSE, showLogLik = TRUE, showAIC = TRUE, showFamily = TRUE, no.output = TRUE)$knitr`
(A2) Adjusted for: study site, age, BMI, TSH, FT4 - table 3 
`r sjt.glm(c(LR1_multix_country_spot_models_CR),group.pred = FALSE, showLogLik = TRUE, showAIC = TRUE, showFamily = TRUE, no.output = TRUE)$knitr`
(B1) Adjusted for: age, BMI, TSH, FT4, spot iodine (μg/L)  - table 3
`r sjt.glm(c(LR2_multix_spot_models_CR),group.pred = FALSE, showLogLik = TRUE, showAIC = TRUE, showFamily = TRUE, no.output = TRUE)$knitr`
(B2) Adjusted for: study site age, BMI, TSH, FT4, spot iodine (μg/L) - table 3
`r sjt.glm(c(LR2_multix_country_spot_models_CR),group.pred = FALSE, showLogLik = TRUE, showAIC = TRUE, showFamily = TRUE, no.output = TRUE)$knitr` 

### Table S8

```{r 5to95_BPA_PooledRegressions_MULTI, echo=FALSE, include=TRUE, results='asis', warning=FALSE}
#Multivariate regressions
#pooled population
LR1_5to95_BPA_multix_spot_models_CR<-lapply(c(ln_vars_spot[c(2)]),
                                  function(x) LR1_status01_multix_spot_CR(x, BPA_5to95_SHRT))           
                               
LR2_5to95_BPA_multix_spot_models_CR<-lapply(c(ln_vars_spot[c(2)]),
                                  function(x) LR2_status01_multix_spot_CR(x, BPA_5to95_SHRT))  

#country-adj
LR1_5to95_BPA_multix_country_spot_models_CR<-lapply(c(ln_vars_spot[c(2)]),
                               function(x) LR1_status01_multix_country_CR(x, BPA_5to95_SHRT))

LR2_5to95_BPA_multix_country_spot_models_CR<-lapply(c(ln_vars_spot[c(2)]),
                               function(x) LR2_status01_multix_country_CR(x, BPA_5to95_SHRT))

```

(A1) Adjusted for: age, BMI, TSH, FT4 - table S8 - BPA
`r sjt.glm(c(LR1_5to95_BPA_multix_spot_models_CR),group.pred = FALSE, showLogLik = TRUE, showAIC = TRUE, showFamily = TRUE, no.output = TRUE)$knitr`
(A2) Adjusted for: study site, age, BMI, TSH, FT4 - table S8 - BPA
`r sjt.glm(c(LR1_5to95_BPA_multix_country_spot_models_CR),group.pred = FALSE, showLogLik = TRUE, showAIC = TRUE, showFamily = TRUE, no.output = TRUE)$knitr`
(B1) Adjusted for: age, BMI, TSH, FT4, spot iodine (μg/L) - table S8 - BPA
`r sjt.glm(c(LR2_5to95_BPA_multix_spot_models_CR),group.pred = FALSE, showLogLik = TRUE, showAIC = TRUE, showFamily = TRUE, no.output = TRUE)$knitr`
(B2) Adjusted for: study site age, BMI, TSH, FT4, spot iodine (μg/L) - table S8 - BPA
`r sjt.glm(c(LR2_5to95_BPA_multix_country_spot_models_CR),group.pred = FALSE, showLogLik = TRUE, showAIC = TRUE, showFamily = TRUE, no.output = TRUE)$knitr` 

```{r 5to95_BPF_PooledRegressions_MULTI, echo=FALSE, include=TRUE, results='asis', warning=FALSE}
#Multivariate regressions
#pooled population
LR1_5to95_BPF_multix_spot_models_CR<-lapply(c(ln_vars_spot[c(1)]),
                                  function(x) LR1_status01_multix_spot_CR(x, BPF_5to95_SHRT))           
                               
LR2_5to95_BPF_multix_spot_models_CR<-lapply(c(ln_vars_spot[c(1)]),
                                  function(x) LR2_status01_multix_spot_CR(x, BPF_5to95_SHRT))  

#country-adj
LR1_5to95_BPF_multix_country_spot_models_CR<-lapply(c(ln_vars_spot[c(1)]),
                               function(x) LR1_status01_multix_country_CR(x, BPF_5to95_SHRT))

LR2_5to95_BPF_multix_country_spot_models_CR<-lapply(c(ln_vars_spot[c(1)]),
                               function(x) LR2_status01_multix_country_CR(x, BPF_5to95_SHRT))
```

(A1) Adjusted for: age, BMI, TSH, FT4 - table S8 - BPF
`r sjt.glm(c(LR1_5to95_BPF_multix_spot_models_CR),group.pred = FALSE, showLogLik = TRUE, showAIC = TRUE, showFamily = TRUE, no.output = TRUE)$knitr`
(A2) Adjusted for: study site, age, BMI, TSH, FT4 - table S8 - BPF
`r sjt.glm(c(LR1_5to95_BPF_multix_country_spot_models_CR),group.pred = FALSE, showLogLik = TRUE, showAIC = TRUE, showFamily = TRUE, no.output = TRUE)$knitr`
(B1) Adjusted for: age, BMI, TSH, FT4, spot iodine (μg/L) - table S8 - BPF
`r sjt.glm(c(LR2_5to95_BPF_multix_spot_models_CR),group.pred = FALSE, showLogLik = TRUE, showAIC = TRUE, showFamily = TRUE, no.output = TRUE)$knitr`
(B2) Adjusted for: study site age, BMI, TSH, FT4, spot iodine (μg/L) - table S8 - BPF
`r sjt.glm(c(LR2_5to95_BPF_multix_country_spot_models_CR),group.pred = FALSE, showLogLik = TRUE, showAIC = TRUE, showFamily = TRUE, no.output = TRUE)$knitr` 

```{r 5to95_ClxBPA_PooledRegressions_MULTI, echo=FALSE, include=TRUE, results='asis', warning=FALSE}
#Multivariate regressions
#pooled population
LR1_5to95_ClxBPA_multix_spot_models_CR<-lapply(c(ln_vars_spot[c(6)]),
                                  function(x) LR1_status01_multix_spot_CR(x, ClxBPA_5to95_SHRT))           
                               
LR2_5to95_ClxBPA_multix_spot_models_CR<-lapply(c(ln_vars_spot[c(6)]),
                                  function(x) LR2_status01_multix_spot_CR(x, ClxBPA_5to95_SHRT))  

#country-adj
LR1_5to95_ClxBPA_multix_country_spot_models_CR<-lapply(c(ln_vars_spot[c(6)]),
                               function(x) LR1_status01_multix_country_CR(x, ClxBPA_5to95_SHRT))

LR2_5to95_ClxBPA_multix_country_spot_models_CR<-lapply(c(ln_vars_spot[c(6)]),
                               function(x) LR2_status01_multix_country_CR(x, ClxBPA_5to95_SHRT))
```

(A1) Adjusted for: age, BMI, TSH, FT4 - table S8 - ClxBPA
`r sjt.glm(c(LR1_5to95_ClxBPA_multix_spot_models_CR),group.pred = FALSE, showLogLik = TRUE, showAIC = TRUE, showFamily = TRUE, no.output = TRUE)$knitr`
(A2) Adjusted for: study site, age, BMI, TSH, FT4 - table S8 - ClxBPA
`r sjt.glm(c(LR1_5to95_ClxBPA_multix_country_spot_models_CR),group.pred = FALSE, showLogLik = TRUE, showAIC = TRUE, showFamily = TRUE, no.output = TRUE)$knitr`
(B1) Adjusted for: age, BMI, TSH, FT4, spot iodine (μg/L) - table S8 - ClxBPA
`r sjt.glm(c(LR2_5to95_ClxBPA_multix_spot_models_CR),group.pred = FALSE, showLogLik = TRUE, showAIC = TRUE, showFamily = TRUE, no.output = TRUE)$knitr`
(B2) Adjusted for: study site age, BMI, TSH, FT4, spot iodine (μg/L) - table S8 - ClxBPA
`r sjt.glm(c(LR2_5to95_ClxBPA_multix_country_spot_models_CR),group.pred = FALSE, showLogLik = TRUE, showAIC = TRUE, showFamily = TRUE, no.output = TRUE)$knitr` 

# Determinants of exposure by compound in the whole study population

### Table 2
```{r BPAdeterminants_POOLED, echo=FALSE, include=TRUE, results='asis'}
# Pooled population
  # ngPERlt - model 1
BPA_ngPERlt_spot_lm4_1<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + daily20lglass, data=SHRT)
BPA_ngPERlt_spot_lm5_1<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + MwaveFrw, data=SHRT)
BPA_ngPERlt_spot_lm6_1<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + weekPORTIONScanned, data=SHRT)
BPA_ngPERlt_spot_lm7_1<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + weekMINScleaning, data=SHRT)
BPA_ngPERlt_spot_lm8_1<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + weekPORTIONSpcps, data=SHRT)
BPA_ngPERlt_spot_lm9_1<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + perfwc, data=SHRT)
BPA_ngPERlt_spot_lm10_1<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + weekPORTIONSdeo, data=SHRT)
BPA_ngPERlt_spot_lm11_1<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + weekPORTIONScosm, data=SHRT)

 # model 2
BPA_ngPERlt_spot_lm1<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country, data=SHRT)
BPA_ngPERlt_spot_lm2<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age, data=SHRT)
BPA_ngPERlt_spot_lm3<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI, data=SHRT)
BPA_ngPERlt_spot_lm4<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + daily20lglass, data=SHRT)
BPA_ngPERlt_spot_lm5<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + MwaveFrw, data=SHRT)
BPA_ngPERlt_spot_lm6<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekPORTIONScanned, data=SHRT)
BPA_ngPERlt_spot_lm7<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekMINScleaning, data=SHRT)
BPA_ngPERlt_spot_lm8<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekPORTIONSpcps, data=SHRT)
BPA_ngPERlt_spot_lm9<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + perfwc, data=SHRT)
BPA_ngPERlt_spot_lm10<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekPORTIONSdeo, data=SHRT)
BPA_ngPERlt_spot_lm11<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekPORTIONScosm, data=SHRT)

#Printing the pooled population models

stargazer(BPA_ngPERlt_spot_lm4_1,BPA_ngPERlt_spot_lm5_1, BPA_ngPERlt_spot_lm6_1, BPA_ngPERlt_spot_lm7_1, BPA_ngPERlt_spot_lm8_1, BPA_ngPERlt_spot_lm9_1, BPA_ngPERlt_spot_lm10_1, BPA_ngPERlt_spot_lm11_1, ci=TRUE, single.row = TRUE, type="html", title="Determinants of BPA exposure - linear regression analysis (table 2 - BPA - model 1", dep.var.labels = "Spot BPA (ng/L creatinine, log transformed)", dep.var.caption = "Pooled population - questionnaire exposures", report = "vcsp")

stargazer(BPA_ngPERlt_spot_lm4, BPA_ngPERlt_spot_lm5, BPA_ngPERlt_spot_lm6, BPA_ngPERlt_spot_lm7, BPA_ngPERlt_spot_lm8, BPA_ngPERlt_spot_lm9, BPA_ngPERlt_spot_lm10, BPA_ngPERlt_spot_lm11, type="html", ci=TRUE, single.row = TRUE, title="Determinants of BPA exposure - linear regression analysis (Table 2 - BPA - model 2", dep.var.labels = "Spot BPA (ng/L, log transformed)", dep.var.caption = "Pooled population - questionnaire exposures (adj. for study site, disease status, age,  BMI", report = "vcsp")
```

## Table S5

```{r 5to95_BPAdeterminants_POOLED, echo=FALSE, include=TRUE, results='asis'}
# Pooled population
  # ngPERlt - for the supplementary table
# model 2
BPA_5to95_ngPERlt_spot_lm1<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country, data=BPA_5to95_SHRT)
BPA_5to95_ngPERlt_spot_lm2<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age, data=BPA_5to95_SHRT)
BPA_5to95_ngPERlt_spot_lm3<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI, data=BPA_5to95_SHRT)
BPA_5to95_ngPERlt_spot_lm4<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + daily20lglass, data=BPA_5to95_SHRT)
BPA_5to95_ngPERlt_spot_lm5<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + MwaveFrw, data=BPA_5to95_SHRT)
BPA_5to95_ngPERlt_spot_lm6<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekPORTIONScanned, data=BPA_5to95_SHRT)
BPA_5to95_ngPERlt_spot_lm7<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekMINScleaning, data=BPA_5to95_SHRT)
BPA_5to95_ngPERlt_spot_lm8<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekPORTIONSpcps, data=BPA_5to95_SHRT)
BPA_5to95_ngPERlt_spot_lm9<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + perfwc, data=BPA_5to95_SHRT)
BPA_5to95_ngPERlt_spot_lm10<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekPORTIONSdeo, data=BPA_5to95_SHRT)
BPA_5to95_ngPERlt_spot_lm11<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekPORTIONScosm, data=BPA_5to95_SHRT)

#Printing the pooled population models

stargazer(BPA_5to95_ngPERlt_spot_lm4, BPA_5to95_ngPERlt_spot_lm5, BPA_5to95_ngPERlt_spot_lm6, BPA_5to95_ngPERlt_spot_lm7, BPA_5to95_ngPERlt_spot_lm8, BPA_5to95_ngPERlt_spot_lm9, BPA_5to95_ngPERlt_spot_lm10, BPA_5to95_ngPERlt_spot_lm11, type="html", ci=TRUE, single.row = TRUE, title="5to95 - Determinants of BPA exposure - linear regression analysis - Table S5 - BPA", dep.var.labels = "Spot BPA (ng/L, log transformed)", dep.var.caption = "Pooled population - questionnaire exposures (adj. for study site, disease status, age,  BMI", report = "vcsp")

```

```{r BPFdeterminants_POOLED, echo=FALSE, include=TRUE, results='asis'}
# Pooled population
  # ngPERlt - model 1
BPF_ngPERlt_spot_lm4_1<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + daily20lglass, data=SHRT)
BPF_ngPERlt_spot_lm5_1<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + MwaveFrw, data=SHRT)
BPF_ngPERlt_spot_lm6_1<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + weekPORTIONScanned, data=SHRT)
BPF_ngPERlt_spot_lm7_1<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + weekMINScleaning, data=SHRT)
BPF_ngPERlt_spot_lm8_1<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + weekPORTIONSpcps, data=SHRT)
BPF_ngPERlt_spot_lm9_1<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + perfwc, data=SHRT)
BPF_ngPERlt_spot_lm10_1<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + weekPORTIONSdeo, data=SHRT)
BPF_ngPERlt_spot_lm11_1<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + weekPORTIONScosm, data=SHRT)

#model 2
BPF_ngPERlt_spot_lm1<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + country, data=SHRT)
BPF_ngPERlt_spot_lm2<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age, data=SHRT)
BPF_ngPERlt_spot_lm3<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI, data=SHRT)
BPF_ngPERlt_spot_lm4<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + daily20lglass, data=SHRT)
BPF_ngPERlt_spot_lm5<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + MwaveFrw, data=SHRT)
BPF_ngPERlt_spot_lm6<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekPORTIONScanned, data=SHRT)
BPF_ngPERlt_spot_lm7<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekMINScleaning, data=SHRT)
BPF_ngPERlt_spot_lm8<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekPORTIONSpcps, data=SHRT)
BPF_ngPERlt_spot_lm9<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + perfwc, data=SHRT)
BPF_ngPERlt_spot_lm10<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekPORTIONSdeo, data=SHRT)
BPF_ngPERlt_spot_lm11<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekPORTIONScosm, data=SHRT)

#Printing the pooled population models

stargazer(BPF_ngPERlt_spot_lm4_1,BPF_ngPERlt_spot_lm5_1, BPF_ngPERlt_spot_lm6_1, BPF_ngPERlt_spot_lm7_1, BPF_ngPERlt_spot_lm8_1, BPF_ngPERlt_spot_lm9_1, BPF_ngPERlt_spot_lm10_1, BPF_ngPERlt_spot_lm11_1, ci=TRUE, single.row = TRUE, type="html", title="Determinants of BPF exposure - linear regression analysis (Table 2 - BPF - model 1)", dep.var.labels = "Spot BPF (ng/L creatinine, log transformed)", dep.var.caption = "Pooled population - questionnaire exposures", report = "vcsp")

stargazer(BPF_ngPERlt_spot_lm4, BPF_ngPERlt_spot_lm5, BPF_ngPERlt_spot_lm6, BPF_ngPERlt_spot_lm7, BPF_ngPERlt_spot_lm8, BPF_ngPERlt_spot_lm9, BPF_ngPERlt_spot_lm10, BPF_ngPERlt_spot_lm11, type="html", ci=TRUE, single.row = TRUE, title="Determinants of BPF exposure - linear regression analysis (Table 2 - BPF - model 2)", dep.var.labels = "Spot BPF (ng/L, log transformed)", dep.var.caption = "Pooled population - questionnaire exposures (adj. for study site, disease status, age,  BMI", report = "vcsp")
```

```{r 5to95_BPFdeterminants_POOLED, echo=FALSE, include=TRUE, results='asis'}
# Pooled population
  # ngPERlt - for the supplementary information
BPF_5to95_ngPERlt_spot_lm1<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + country, data=BPF_5to95_SHRT)
BPF_5to95_ngPERlt_spot_lm2<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age, data=BPF_5to95_SHRT)
BPF_5to95_ngPERlt_spot_lm3<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI, data=BPF_5to95_SHRT)
BPF_5to95_ngPERlt_spot_lm4<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + daily20lglass, data=BPF_5to95_SHRT)
BPF_5to95_ngPERlt_spot_lm5<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + MwaveFrw, data=BPF_5to95_SHRT)
BPF_5to95_ngPERlt_spot_lm6<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekPORTIONScanned, data=BPF_5to95_SHRT)
BPF_5to95_ngPERlt_spot_lm7<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekMINScleaning, data=BPF_5to95_SHRT)
BPF_5to95_ngPERlt_spot_lm8<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekPORTIONSpcps, data=BPF_5to95_SHRT)
BPF_5to95_ngPERlt_spot_lm9<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + perfwc, data=BPF_5to95_SHRT)
BPF_5to95_ngPERlt_spot_lm10<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekPORTIONSdeo, data=BPF_5to95_SHRT)
BPF_5to95_ngPERlt_spot_lm11<-lm(ln_BPF_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekPORTIONScosm, data=BPF_5to95_SHRT)

#Printing the pooled population models
stargazer(BPF_5to95_ngPERlt_spot_lm4, BPF_5to95_ngPERlt_spot_lm5, BPF_5to95_ngPERlt_spot_lm6, BPF_5to95_ngPERlt_spot_lm7, BPF_5to95_ngPERlt_spot_lm8, BPF_5to95_ngPERlt_spot_lm9, BPF_5to95_ngPERlt_spot_lm10, BPF_5to95_ngPERlt_spot_lm11, type="html", ci=TRUE, single.row = TRUE, title="5to95 - Determinants of BPF exposure - linear regression analysis - Table S5 - BPF", dep.var.labels = "Spot BPF (ng/L, log transformed)", dep.var.caption = "Pooled population - questionnaire exposures (adj. for study site, disease status, age,  BMI", report = "vcsp")
```

```{r ClxBPAdeterminants_POOLED, echo=FALSE, include=TRUE, results='asis'}
# Pooled population
  # ngPERlt - model 1
ClxBPA_ngPERlt_spot_lm4_1<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + daily20lglass, data=SHRT)
ClxBPA_ngPERlt_spot_lm5_1<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + MwaveFrw, data=SHRT)
ClxBPA_ngPERlt_spot_lm6_1<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + weekPORTIONScanned, data=SHRT)
ClxBPA_ngPERlt_spot_lm7_1<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + weekMINScleaning, data=SHRT)
ClxBPA_ngPERlt_spot_lm8_1<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + weekPORTIONSpcps, data=SHRT)
ClxBPA_ngPERlt_spot_lm9_1<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + perfwc, data=SHRT)
ClxBPA_ngPERlt_spot_lm10_1<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + weekPORTIONSdeo, data=SHRT)
ClxBPA_ngPERlt_spot_lm11_1<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + weekPORTIONScosm, data=SHRT)

# model 2
ClxBPA_ngPERlt_spot_lm1<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country, data=SHRT)
ClxBPA_ngPERlt_spot_lm2<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age, data=SHRT)
ClxBPA_ngPERlt_spot_lm3<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI, data=SHRT)
ClxBPA_ngPERlt_spot_lm4<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + daily20lglass, data=SHRT)
ClxBPA_ngPERlt_spot_lm5<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + MwaveFrw, data=SHRT)
ClxBPA_ngPERlt_spot_lm6<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekPORTIONScanned, data=SHRT)
ClxBPA_ngPERlt_spot_lm7<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekMINScleaning, data=SHRT)
ClxBPA_ngPERlt_spot_lm8<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekPORTIONSpcps, data=SHRT)
ClxBPA_ngPERlt_spot_lm9<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + perfwc, data=SHRT)
ClxBPA_ngPERlt_spot_lm10<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekPORTIONSdeo, data=SHRT)
ClxBPA_ngPERlt_spot_lm11<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekPORTIONScosm, data=SHRT)

#Printing the pooled population models

stargazer(ClxBPA_ngPERlt_spot_lm4_1,ClxBPA_ngPERlt_spot_lm5_1, ClxBPA_ngPERlt_spot_lm6_1, ClxBPA_ngPERlt_spot_lm7_1, ClxBPA_ngPERlt_spot_lm8_1, ClxBPA_ngPERlt_spot_lm9_1, ClxBPA_ngPERlt_spot_lm10_1, ClxBPA_ngPERlt_spot_lm11_1, ci=TRUE, single.row = TRUE, type="html", title="Determinants of ClxBPA exposure - linear regression analysis (Table 2 - ClxBPA - model 1)", dep.var.labels = "Spot ClxBPA (ng/L creatinine, log transformed)", dep.var.caption = "Pooled population - questionnaire exposures", report = "vcsp")

stargazer(ClxBPA_ngPERlt_spot_lm4, ClxBPA_ngPERlt_spot_lm5, ClxBPA_ngPERlt_spot_lm6, ClxBPA_ngPERlt_spot_lm7, ClxBPA_ngPERlt_spot_lm8, ClxBPA_ngPERlt_spot_lm9, ClxBPA_ngPERlt_spot_lm10, ClxBPA_ngPERlt_spot_lm11, type="html", ci=TRUE, single.row = TRUE, title="Determinants of ClxBPA exposure - linear regression analysis (Table - ClxBPA - model 2)", dep.var.labels = "Spot ClxBPA (ng/L, log transformed)", dep.var.caption = "Pooled population - questionnaire exposures (adj. for study site, disease status, age,  BMI", report = "vcsp")
```

```{r 5to95_ClxBPAdeterminants_POOLED, echo=FALSE, include=TRUE, results='asis'}
# Pooled population
  # ngPERlt
# for the supplementary information
ClxBPA_5to95_ngPERlt_spot_lm1<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country, data=ClxBPA_5to95_SHRT)
ClxBPA_5to95_ngPERlt_spot_lm2<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age, data=ClxBPA_5to95_SHRT)
ClxBPA_5to95_ngPERlt_spot_lm3<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI, data=ClxBPA_5to95_SHRT)
ClxBPA_5to95_ngPERlt_spot_lm4<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + daily20lglass, data=ClxBPA_5to95_SHRT)
ClxBPA_5to95_ngPERlt_spot_lm5<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + MwaveFrw, data=ClxBPA_5to95_SHRT)
ClxBPA_5to95_ngPERlt_spot_lm6<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekPORTIONScanned, data=ClxBPA_5to95_SHRT)
ClxBPA_5to95_ngPERlt_spot_lm7<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekMINScleaning, data=ClxBPA_5to95_SHRT)
ClxBPA_5to95_ngPERlt_spot_lm8<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekPORTIONSpcps, data=ClxBPA_5to95_SHRT)
ClxBPA_5to95_ngPERlt_spot_lm9<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + perfwc, data=ClxBPA_5to95_SHRT)
ClxBPA_5to95_ngPERlt_spot_lm10<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekPORTIONSdeo, data=ClxBPA_5to95_SHRT)
ClxBPA_5to95_ngPERlt_spot_lm11<-lm(ln_ClxBPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + status01 + age + BMI + weekPORTIONScosm, data=ClxBPA_5to95_SHRT)

#Printing the pooled population models

stargazer(ClxBPA_5to95_ngPERlt_spot_lm4, ClxBPA_5to95_ngPERlt_spot_lm5, ClxBPA_5to95_ngPERlt_spot_lm6, ClxBPA_5to95_ngPERlt_spot_lm7, ClxBPA_5to95_ngPERlt_spot_lm8, ClxBPA_5to95_ngPERlt_spot_lm9, ClxBPA_5to95_ngPERlt_spot_lm10, ClxBPA_5to95_ngPERlt_spot_lm11, type="html", ci=TRUE, single.row = TRUE, title="5to95 - Determinants of ClxBPA exposure - linear regression analysis - Table S5 - ClxBPA", dep.var.labels = "Spot ClxBPA (ng/L, log transformed)", dep.var.caption = "Pooled population - questionnaire exposures (adj. for study site, disease status, age,  BMI", report = "vcsp")
```

## Table S10 (additional table)

```{r BPAdeterminants_POOLED_BYSTATUS, echo=FALSE, include=TRUE, results='asis'}
# CASES
  # ngPERlt - for the supplementary table
CA_BPA_ngPERlt_spot_lm1<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country, data=CA_SHRT)
CA_BPA_ngPERlt_spot_lm2<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + age, data=CA_SHRT)
CA_BPA_ngPERlt_spot_lm3<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + age + BMI, data=CA_SHRT)
CA_BPA_ngPERlt_spot_lm4<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + age + BMI + daily20lglass, data=CA_SHRT)
CA_BPA_ngPERlt_spot_lm5<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + age + BMI + MwaveFrw, data=CA_SHRT)
CA_BPA_ngPERlt_spot_lm6<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + age + BMI + weekPORTIONScanned, data=CA_SHRT)
CA_BPA_ngPERlt_spot_lm7<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + age + BMI + weekMINScleaning, data=CA_SHRT)
CA_BPA_ngPERlt_spot_lm8<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + age + BMI + weekPORTIONSpcps, data=CA_SHRT)
CA_BPA_ngPERlt_spot_lm9<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + age + BMI + perfwc, data=CA_SHRT)
CA_BPA_ngPERlt_spot_lm10<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + age + BMI + weekPORTIONSdeo, data=CA_SHRT)
CA_BPA_ngPERlt_spot_lm11<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + age + BMI + weekPORTIONScosm, data=CA_SHRT)

#Printing the pooled population models
stargazer(CA_BPA_ngPERlt_spot_lm4, CA_BPA_ngPERlt_spot_lm5, CA_BPA_ngPERlt_spot_lm6, CA_BPA_ngPERlt_spot_lm7, CA_BPA_ngPERlt_spot_lm8, CA_BPA_ngPERlt_spot_lm9, CA_BPA_ngPERlt_spot_lm10, CA_BPA_ngPERlt_spot_lm11, type="html", ci=TRUE, single.row = TRUE, title="Determinants of BPA exposure - linear regression analysis - Table S10 (additional)", dep.var.labels = "Spot BPA (ng/L, log transformed)", dep.var.caption = "Pooled population (CASES only) - questionnaire exposures (adj. for study site, disease status, age,  BMI", report = "vcsp")

# Controls
  # ngPERlt - for the supplementary

CO_BPA_ngPERlt_spot_lm4<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + age + BMI + daily20lglass, data=CO_SHRT)
CO_BPA_ngPERlt_spot_lm5<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + age + BMI + MwaveFrw, data=CO_SHRT)
CO_BPA_ngPERlt_spot_lm6<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + age + BMI + weekPORTIONScanned, data=CO_SHRT)
CO_BPA_ngPERlt_spot_lm7<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + age + BMI + weekMINScleaning, data=CO_SHRT)
CO_BPA_ngPERlt_spot_lm8<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + age + BMI + weekPORTIONSpcps, data=CO_SHRT)
CO_BPA_ngPERlt_spot_lm9<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + age + BMI + perfwc, data=CO_SHRT)
CO_BPA_ngPERlt_spot_lm10<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + age + BMI + weekPORTIONSdeo, data=CO_SHRT)
CO_BPA_ngPERlt_spot_lm11<-lm(ln_BPA_ngPERlt_spot ~ ln_creat_gPERlt_spot + country + age + BMI + weekPORTIONScosm, data=CO_SHRT)

#Printing the pooled population models
stargazer(CO_BPA_ngPERlt_spot_lm4, CO_BPA_ngPERlt_spot_lm5, CO_BPA_ngPERlt_spot_lm6, CO_BPA_ngPERlt_spot_lm7, CO_BPA_ngPERlt_spot_lm8, CO_BPA_ngPERlt_spot_lm9, CO_BPA_ngPERlt_spot_lm10, CO_BPA_ngPERlt_spot_lm11, type="html", ci=TRUE, single.row = TRUE, title="Determinants of BPA exposure - linear regression analysis - Table S10 (additional)", dep.var.labels = "Spot BPA (ng/L, log transformed)", dep.var.caption = "Pooled population (CONTROLS only) - questionnaire exposures (adj. for study site, disease status, age,  BMI", report = "vcsp")
```

# BPA and TSH - trends

* Trend model A: univariate (only creatinine adjustment)
* Trend model B: adjustment for creatinine, age and BMI
* Trend model C: adjustment for creatinine, country, age and BMI
* Trend model D: adjustment for creatinine, country, age, BMI and disease status

```{r variablesFORtrend, echo=FALSE, include=FALSE, warning=TRUE}
#work on the dataset and cut the urine levels
SHRT$quant_ln_BPF_ngPERlt_spot<-break_quant(SHRT$ln_BPF_ngPERlt_spot)
SHRT$quant_ln_BPA_ngPERlt_spot<-break_quant(SHRT$ln_BPA_ngPERlt_spot)
SHRT$quant_ln_ClxBPA_ngPERlt_spot<-break_quant(SHRT$ln_ClxBPA_ngPERlt_spot)

#find the median of each quartile

break_quant1234<-function (var){cut(var, breaks=quantile(var, c(0,0.25,0.50,0.75,1), na.rm=TRUE), include.lowest = TRUE,labels = c(1,2,3,4))}

SHRT$quant1234_ln_BPF_ngPERlt_spot<-break_quant1234(SHRT$ln_BPF_ngPERlt_spot)
SHRT$quant1234_ln_BPA_ngPERlt_spot<-break_quant1234(SHRT$ln_BPA_ngPERlt_spot)
SHRT$quant1234_ln_ClxBPA_ngPERlt_spot<-break_quant1234(SHRT$ln_ClxBPA_ngPERlt_spot)

#make a variable where each urinary BPF, BPA and ClxBPA value has been replaced with the median of the quartile it belongs to

SHRT$SUBquant1234_ln_BPF_ngPERlt_spot[SHRT$quant1234_ln_BPF_ngPERlt_spot=="1"]<-median(SHRT$ln_BPF_ngPERlt_spot[SHRT$quant1234_ln_BPF_ngPERlt_spot=="1"], na.rm=TRUE)
SHRT$SUBquant1234_ln_BPF_ngPERlt_spot[SHRT$quant1234_ln_BPF_ngPERlt_spot=="2"]<-median(SHRT$ln_BPF_ngPERlt_spot[SHRT$quant1234_ln_BPF_ngPERlt_spot=="2"], na.rm=TRUE)
SHRT$SUBquant1234_ln_BPF_ngPERlt_spot[SHRT$quant1234_ln_BPF_ngPERlt_spot=="3"]<-median(SHRT$ln_BPF_ngPERlt_spot[SHRT$quant1234_ln_BPF_ngPERlt_spot=="3"], na.rm=TRUE)
SHRT$SUBquant1234_ln_BPF_ngPERlt_spot[SHRT$quant1234_ln_BPF_ngPERlt_spot=="4"]<-median(SHRT$ln_BPF_ngPERlt_spot[SHRT$quant1234_ln_BPF_ngPERlt_spot=="4"], na.rm=TRUE)

SHRT$SUBquant1234_ln_BPA_ngPERlt_spot[SHRT$quant1234_ln_BPA_ngPERlt_spot=="1"]<-median(SHRT$ln_BPA_ngPERlt_spot[SHRT$quant1234_ln_BPA_ngPERlt_spot=="1"], na.rm=TRUE)
SHRT$SUBquant1234_ln_BPA_ngPERlt_spot[SHRT$quant1234_ln_BPA_ngPERlt_spot=="2"]<-median(SHRT$ln_BPA_ngPERlt_spot[SHRT$quant1234_ln_BPA_ngPERlt_spot=="2"], na.rm=TRUE)
SHRT$SUBquant1234_ln_BPA_ngPERlt_spot[SHRT$quant1234_ln_BPA_ngPERlt_spot=="3"]<-median(SHRT$ln_BPA_ngPERlt_spot[SHRT$quant1234_ln_BPA_ngPERlt_spot=="3"], na.rm=TRUE)
SHRT$SUBquant1234_ln_BPA_ngPERlt_spot[SHRT$quant1234_ln_BPA_ngPERlt_spot=="4"]<-median(SHRT$ln_BPA_ngPERlt_spot[SHRT$quant1234_ln_BPA_ngPERlt_spot=="4"], na.rm=TRUE)

SHRT$SUBquant1234_ln_ClxBPA_ngPERlt_spot[SHRT$quant1234_ln_ClxBPA_ngPERlt_spot=="1"]<-median(SHRT$ln_ClxBPA_ngPERlt_spot[SHRT$quant1234_ln_ClxBPA_ngPERlt_spot=="1"], na.rm=TRUE)
SHRT$SUBquant1234_ln_ClxBPA_ngPERlt_spot[SHRT$quant1234_ln_ClxBPA_ngPERlt_spot=="2"]<-median(SHRT$ln_ClxBPA_ngPERlt_spot[SHRT$quant1234_ln_ClxBPA_ngPERlt_spot=="2"], na.rm=TRUE)
SHRT$SUBquant1234_ln_ClxBPA_ngPERlt_spot[SHRT$quant1234_ln_ClxBPA_ngPERlt_spot=="3"]<-median(SHRT$ln_ClxBPA_ngPERlt_spot[SHRT$quant1234_ln_ClxBPA_ngPERlt_spot=="3"], na.rm=TRUE)
SHRT$SUBquant1234_ln_ClxBPA_ngPERlt_spot[SHRT$quant1234_ln_ClxBPA_ngPERlt_spot=="4"]<-median(SHRT$ln_ClxBPA_ngPERlt_spot[SHRT$quant1234_ln_ClxBPA_ngPERlt_spot=="4"], na.rm=TRUE)

#separate cases and controls - once more to be used in the trend tests
CA_SHRT<-SHRT[which(SHRT$status=='CA'),]
CO_SHRT<-SHRT[which(SHRT$status=="CO"),]

quant_varsCONT<-c("SUBquant1234_ln_BPA_ngPERlt_spot", "SUBquant1234_ln_BPF_ngPERlt_spot", "SUBquant1234_ln_ClxBPA_ngPERlt_spot")
```

## Table 4

outcome: ln_TSH
dependent variables: quartiles of ln_BPF, ln_BPA, ln_ClxBPA (continuous) 

ONLY WORKING ON THE SPOT SAMPLES 
```{r 5to95df_remake, echo=FALSE, include=FALSE}
#function for the 5-95 percentiles  - REMAKING THE 5to95 DATASETS
gettherange<-function(x){ 
  top=quantile(x, 0.95 ,na.rm=TRUE)
  bottom=quantile(x, 0.05 ,na.rm=TRUE)
  x[x>top|x<bottom]=NA
  return(x)} 
BPA_5to95_SHRT_0<-SHRT
BPA_5to95_SHRT_0$ln_BPA_ngPERlt_spot<-with(SHRT, gettherange(ln_BPA_ngPERlt_spot))
BPA_5to95_SHRT<-BPA_5to95_SHRT_0[complete.cases(BPA_5to95_SHRT_0[,c("ln_BPA_ngPERlt_spot")]),]

BPF_5to95_SHRT_0<-SHRT
BPF_5to95_SHRT_0$ln_BPF_ngPERlt_spot<-with(SHRT, gettherange(ln_BPF_ngPERlt_spot))
BPF_5to95_SHRT<-BPF_5to95_SHRT_0[complete.cases(BPF_5to95_SHRT_0[,c("ln_BPF_ngPERlt_spot")]),]

ClxBPA_5to95_SHRT_0<-SHRT
ClxBPA_5to95_SHRT_0$ln_ClxBPA_ngPERlt_spot<-with(SHRT, gettherange(ln_ClxBPA_ngPERlt_spot))
ClxBPA_5to95_SHRT<-ClxBPA_5to95_SHRT_0[complete.cases(ClxBPA_5to95_SHRT_0[,c("ln_ClxBPA_ngPERlt_spot")]),]
```

```{r TSH_TrendModels_CONTINUOUS, echo=FALSE, include=FALSE, results='asis'}

#(A) models
ln_tsh_quantileCONT<-function(var, dataframe) {
  formula    <- as.formula(paste("ln_tsh ~ ln_creat_gPERlt_spot +", var))
  res.logist <- lm(formula, data = dataframe)
}
#(B) models
ln_tsh_quantileCONT_adj1<-function(var, dataframe) {
  formula    <- as.formula(paste("ln_tsh ~ ln_creat_gPERlt_spot + age + BMI +", var))
  res.logist <- lm(formula, data = dataframe)
}
#(C) models
ln_tsh_quantileCONT_adj2<-function(var, dataframe) {
  formula    <- as.formula(paste("ln_tsh ~ ln_creat_gPERlt_spot + country + age + BMI +", var))
  res.logist <- lm(formula, data = dataframe)
}
#(D) models
ln_tsh_quantileCONT_adj3<-function(var, dataframe) {
  formula    <- as.formula(paste("ln_tsh ~ ln_creat_gPERlt_spot + country + age + BMI + status01 +", var))
  res.logist <- lm(formula, data = dataframe)}

# pooled
univ_trendCONT_tsh<-lapply(c(quant_varsCONT),
               function(x) ln_tsh_quantileCONT(x, SHRT))

adjustment1CONT_tsh<-lapply(c(quant_varsCONT),
               function(x) ln_tsh_quantileCONT_adj1(x, SHRT))

adjustment2CONT_tsh<-lapply(c(quant_varsCONT),
              function(x) ln_tsh_quantileCONT_adj2(x, SHRT))

adjustment3CONT_tsh<-lapply(c(quant_varsCONT),
               function(x) ln_tsh_quantileCONT_adj3(x, SHRT))

#5to95 - BPA
BPA_5to95_univ_trendCONT_tsh<-lapply(c(quant_varsCONT[c(1)]),
               function(x) ln_tsh_quantileCONT(x, BPA_5to95_SHRT))

BPA_5to95_adjustment1CONT_tsh<-lapply(c(quant_varsCONT[c(1)]),
               function(x) ln_tsh_quantileCONT_adj1(x, BPA_5to95_SHRT))

BPA_5to95_adjustment2CONT_tsh<-lapply(c(quant_varsCONT[c(1)]),
              function(x) ln_tsh_quantileCONT_adj2(x, BPA_5to95_SHRT))

BPA_5to95_adjustment3CONT_tsh<-lapply(c(quant_varsCONT[c(1)]),
               function(x) ln_tsh_quantileCONT_adj3(x, BPA_5to95_SHRT))

#5to95 - BPF
BPF_5to95_univ_trendCONT_tsh<-lapply(c(quant_varsCONT[c(2)]),
               function(x) ln_tsh_quantileCONT(x, BPF_5to95_SHRT))

BPF_5to95_adjustment1CONT_tsh<-lapply(c(quant_varsCONT[c(2)]),
               function(x) ln_tsh_quantileCONT_adj1(x, BPF_5to95_SHRT))

BPF_5to95_adjustment2CONT_tsh<-lapply(c(quant_varsCONT[c(2)]),
              function(x) ln_tsh_quantileCONT_adj2(x, BPF_5to95_SHRT))

BPF_5to95_adjustment3CONT_tsh<-lapply(c(quant_varsCONT[c(2)]),
               function(x) ln_tsh_quantileCONT_adj3(x, BPF_5to95_SHRT))

#5to95 - ClxBPA
ClxBPA_5to95_univ_trendCONT_tsh<-lapply(c(quant_varsCONT[c(3)]),
               function(x) ln_tsh_quantileCONT(x, ClxBPA_5to95_SHRT))

ClxBPA_5to95_adjustment1CONT_tsh<-lapply(c(quant_varsCONT[c(3)]),
               function(x) ln_tsh_quantileCONT_adj1(x, ClxBPA_5to95_SHRT))

ClxBPA_5to95_adjustment2CONT_tsh<-lapply(c(quant_varsCONT[c(3)]),
              function(x) ln_tsh_quantileCONT_adj2(x, ClxBPA_5to95_SHRT))

ClxBPA_5to95_adjustment3CONT_tsh<-lapply(c(quant_varsCONT[c(3)]),
               function(x) ln_tsh_quantileCONT_adj3(x, ClxBPA_5to95_SHRT))

```

For each models the results of the complete dataset are presented first followed by the results of the sensitivity analysis BPA, BPF and ClxBPA where only participants with values between the 5th and the 9th percentile have been included.

(1) Univariate trend models and (2) adjustment: age + BMI - quantiles CONT

* Pooled sample

```{r printTSHtrend1, echo=FALSE, include=TRUE, results='asis'}
#Complete dataset
stargazer(c(univ_trendCONT_tsh), ci=TRUE, single.row = TRUE, title="TSH and BPF, BPA or ClxBPA - trend analysis - Pooled population - Table 4", dep.var.labels = "TSH (mIU/L, log-transformed)", dep.var.caption = "A - Creatinine (g/L, log-transformed)", type="html", report = "vcsp")
#Sensitivity
stargazer(c(BPA_5to95_univ_trendCONT_tsh), ci=TRUE, single.row = TRUE, title="Sensitivity analysis results: TSH and BPF, BPA or ClxBPA - trend analysis - Pooled population - BPA: 5to95 - Table 4", dep.var.labels = "TSH (mIU/L, log-transformed)", dep.var.caption = "A - Creatinine (g/L, log-transformed)", type="html", report = "vcsp")

stargazer(c(BPF_5to95_univ_trendCONT_tsh), ci=TRUE, single.row = TRUE, title="Sensitivity analysis results: TSH and BPF, BPF or ClxBPA - trend analysis - Pooled population - BPF: 5to95 - Table 4", dep.var.labels = "TSH (mIU/L, log-transformed)", dep.var.caption = "A - Creatinine (g/L, log-transformed)", type="html", report = "vcsp")

stargazer(c(ClxBPA_5to95_univ_trendCONT_tsh), ci=TRUE, single.row = TRUE, title="Sensitivity analysis results: TSH and ClxBPA, BPF or ClxBPA - trend analysis - Pooled population - ClxBPA_5to95 - Table 4", dep.var.labels = "TSH (mIU/L, log-transformed)", dep.var.caption = "A - Creatinine (g/L, log-transformed)", type="html", report = "vcsp")

#Complete dataset
stargazer(c(adjustment1CONT_tsh), ci=TRUE, single.row = TRUE, title="TSH and BPF, BPA or ClxBPA - trend analysis - Pooled population - Table 4", dep.var.labels = "TSH (mIU/L, log-transformed)", dep.var.caption = "B - Age, BMI and creatinine (g/L, log-transformed)", type="html", report = "vcsp")
#Sensitivity
stargazer(c(BPA_5to95_adjustment1CONT_tsh), ci=TRUE, single.row = TRUE, title="Sensitivity analysis results: TSH and BPF, BPA or ClxBPA - trend analysis - Pooled population - BPA: 5to95- Table 4", dep.var.labels = "TSH (mIU/L, log-transformed)", dep.var.caption = "B - Age, BMI and creatinine (g/L, log-transformed)", type="html", report = "vcsp")

stargazer(c(BPF_5to95_adjustment1CONT_tsh), ci=TRUE, single.row = TRUE, title="Sensitivity analysis results: TSH and BPF, BPF or ClxBPA - trend analysis - Pooled population - BPF: 5to95 - Table 4", dep.var.labels = "TSH (mIU/L, log-transformed)", dep.var.caption = "B - Age, BMI and creatinine (g/L, log-transformed)", type="html", report = "vcsp")

stargazer(c(ClxBPA_5to95_adjustment1CONT_tsh), ci=TRUE, single.row = TRUE, title="Sensitivity analysis results: TSH and BPF, BPF or ClxBPA - trend analysis - Pooled population - ClxBPA: 5to95 - Table 4", dep.var.labels = "TSH (mIU/L, log-transformed)", dep.var.caption = "B - Age, BMI and creatinine (g/L, log-transformed)", type="html", report = "vcsp")

```

(3) Adjustment: country + age + BMI and (4) adjustment: country + age + BMI and + status (only for the pooled sample) - quantiles CONT  
 
* Pooled sample

```{r printTSHtrend2, echo=FALSE, include=TRUE, results='asis'}
#Whole dataset
stargazer(c(adjustment2CONT_tsh), ci=TRUE, single.row = TRUE, title="TSH and BPF, BPA or ClxBPA - trend analysis - Pooled population - Table 4", dep.var.labels = "TSH (mIU/L, log-transformed)", dep.var.caption = "(C) Age, BMI, study site_R and creatinine", type="html", report = "vcsp")
#senstivity analysis
stargazer(c(BPA_5to95_adjustment2CONT_tsh), ci=TRUE, single.row = TRUE, title="Sensitivity analysis results: TSH and BPF, BPA or ClxBPA - trend analysis - Pooled population - BPA: 5to95 - Table 4", dep.var.labels = "TSH (mIU/L, log-transformed)", dep.var.caption = "(C) Age, BMI, study site and creatinine", type="html", report = "vcsp")

stargazer(c(BPF_5to95_adjustment2CONT_tsh), ci=TRUE, single.row = TRUE, title="Sensitivity analysis results: TSH and BPF, BPA or ClxBPA - trend analysis - Pooled population - BPF_5to95 - Table 4", dep.var.labels = "TSH (mIU/L, log-transformed)", dep.var.caption = "(C) Age, BMI, study site and creatinine", type="html", report = "vcsp")

stargazer(c(ClxBPA_5to95_adjustment2CONT_tsh), ci=TRUE, single.row = TRUE, title="Sensitivity analysis results: TSH and BPF, BPA or ClxBPA - trend analysis - Pooled population - ClxBPA_5to95 - Table 4", dep.var.labels = "TSH (mIU/L, log-transformed)", dep.var.caption = "(C) Age, BMI, study site and creatinine", type="html", report = "vcsp")

#Whole dataset
stargazer(c(adjustment3CONT_tsh), ci=TRUE, single.row = TRUE, title="TSH and BPF, BPA or ClxBPA - trend analysis - Pooled population - Table 4", dep.var.labels = "TSH (mIU/L, log-transformed)", dep.var.caption = "(D) Age, BMI, study site_R, disease status[case] (and creatinine)", type="html", report = "vcsp")
#senstivity analysis
stargazer(c(BPA_5to95_adjustment3CONT_tsh), ci=TRUE, single.row = TRUE, title="Sensitivity analysis results: TSH and BPF, BPA or ClxBPA - trend analysis - Pooled population - BPA_5to95 - Table 4", dep.var.labels = "TSH (mIU/L, log-transformed)", dep.var.caption = "(D) Age, BMI, study site, disease status[case] (and creatinine)", type="html", report = "vcsp")

stargazer(c(BPF_5to95_adjustment3CONT_tsh), ci=TRUE, single.row = TRUE, title="Sensitivity analysis results: TSH and BPF, BPA or ClxBPA - trend analysis - Pooled population - BPF_5to95 - Table 4", dep.var.labels = "TSH (mIU/L, log-transformed)", dep.var.caption = "(D) Age, BMI, study site, disease status[case] (and creatinine)", type="html", report = "vcsp")

stargazer(c(ClxBPA_5to95_adjustment3CONT_tsh), ci=TRUE, single.row = TRUE, title="Sensitivity analysis results: TSH and BPF, BPA or ClxBPA - trend analysis - Pooled population - ClxBPA_5to95 - Table 4", dep.var.labels = "TSH (mIU/L, log-transformed)", dep.var.caption = "(D) Age, BMI, study site, disease status[case] (and creatinine)", type="html", report = "vcsp")
```

## Additional trend analysis  for fT4

### Table S9

Linear regression
outcome: ln_ft4
dependent variables: quartiles of ln_BPF, ln_BPA, ln_ClxBPA (continuous) 

```{r fT4_TrendModels_CONTINUOUS, echo=FALSE, include=FALSE, results='asis'}
## fT4
ln_ft4_quantileCONT<-function(var, dataframe) {
  formula    <- as.formula(paste("ln_ft4 ~ ln_creat_gPERlt_spot +", var))
  res.logist <- lm(formula, data = dataframe)
}

ln_ft4_quantileCONT_adj1<-function(var, dataframe) {
  formula    <- as.formula(paste("ln_ft4 ~ ln_creat_gPERlt_spot + age + BMI +", var))
  res.logist <- lm(formula, data = dataframe)
}

ln_ft4_quantileCONT_adj2<-function(var, dataframe) {
  formula    <- as.formula(paste("ln_ft4 ~ ln_creat_gPERlt_spot + country + age + BMI +", var))
  res.logist <- lm(formula, data = dataframe)
}

ln_ft4_quantileCONT_adj3<-function(var, dataframe) {
  formula    <- as.formula(paste("ln_ft4 ~ ln_creat_gPERlt_spot + country + age + BMI + status01 +", var))
  res.logist <- lm(formula, data = dataframe)}

# pooled
univ_trendCONT_ft4<-lapply(c(quant_varsCONT),
               function(x) ln_ft4_quantileCONT(x, SHRT))

adjustment1CONT_ft4<-lapply(c(quant_varsCONT),
               function(x) ln_ft4_quantileCONT_adj1(x, SHRT))

adjustment2CONT_ft4<-lapply(c(quant_varsCONT),
              function(x) ln_ft4_quantileCONT_adj2(x, SHRT))

adjustment3CONT_ft4<-lapply(c(quant_varsCONT),
               function(x) ln_ft4_quantileCONT_adj3(x, SHRT))

```

```{r printfT4trends, include=TRUE, results='asis'}
#CORRECTION 160822 - REMOVED THE ARGUMENT FOR "EXTERNAL" OUTPUT ADDED THE ", report = "vcsp""
stargazer(c(univ_trendCONT_ft4, adjustment1CONT_ft4), type="html", ci=TRUE, single.row = TRUE, title="fT4 and BPF, BPA or ClxBPA - trend analysis - Pooled population - Table S9", report = "vcsp")

stargazer(c(adjustment2CONT_ft4, adjustment3CONT_ft4), type="html", ci=TRUE, title="fT4 and BPF, BPA or ClxBPA - trend analysis - Pooled population - Table S9", single.row = TRUE, report = "vcsp")

```

## Plots between BPA, TSH and iodine
```{r TSH_BPA_plots, echo=FALSE, include=TRUE}
ggplot(SHRT, aes(x=iod_ugPERlt_spot, y=BPA_ngPERlt_spot)) +
    geom_point(shape=1) +    # Use hollow circles
    geom_smooth(method=lm,   # Add linear regression line
                se=FALSE)+xlim(0,600)+ylim(0,50000)+ggtitle("Pooled sample")  

ggplot(SHRT, aes(x=iod_ugPERg_spot, y=BPA_ngPERg_spot)) +
    geom_point(shape=1) +    # Use hollow circles
    geom_smooth(method=lm,   # Add linear regression line
                se=FALSE)+xlim(0,600)+ylim(0,50000)+ggtitle("Pooled sample")   

ggplot(CY_SHRT, aes(x=iod_ugPERlt_spot, y=BPA_ngPERlt_spot)) +
    geom_point(shape=1) +    # Use hollow circles
    geom_smooth(method=lm,   # Add linear regression line
                se=FALSE)+xlim(0,600)+ylim(0,50000)+ggtitle("CY")  

ggplot(CY_SHRT, aes(x=iod_ugPERg_spot, y=BPA_ngPERg_spot)) +
    geom_point(shape=1) +    # Use hollow circles
    geom_smooth(method=lm,   # Add linear regression line
                se=FALSE)+xlim(0,600)+ylim(0,50000)+ggtitle("CY") 

ggplot(RO_SHRT, aes(x=iod_ugPERlt_spot, y=BPA_ngPERlt_spot)) +
    geom_point(shape=1) +    # Use hollow circles
    geom_smooth(method=lm,   # Add linear regression line
                se=FALSE)+xlim(0,600)+ylim(0,50000)+ggtitle("RO")

ggplot(RO_SHRT, aes(x=iod_ugPERg_spot, y=BPA_ngPERg_spot)) +
    geom_point(shape=1) +    # Use hollow circles
    geom_smooth(method=lm,   # Add linear regression line
                se=FALSE)+xlim(0,600)+ylim(0,50000)+ggtitle("RO") 

```

R and package versions used

```{r sessionInfo, include=TRUE,  hide=TRUE, echo=TRUE, results='markup'}
sessionInfo()
```