---
title: "Figure S5 data source eLife submission"
output: html_document
---

```{r message = FALSE, warning = FALSE}
library(easyGgplot2)
#ggplot2.multiplot(..., plotlist=NULL, cols=2)
library(easyGgplot2)
library(lme4)
library(merTools)
combinedData <- read.csv("~/Desktop/Temperature study/combined_data.csv")

combinedData$time_HR_imputed <- combinedData$time_HR
combinedData$time_HR_imputed[is.na(combinedData$time_HR_imputed)] <- 12

dataVeterans  <- read.csv('~/Desktop/Temperature study/veterans_processed.csv')
dataNHANES    <- read.csv('~/Desktop/Temperature study/NHANES_processed.csv')
ST_data 	  <- read.csv('~/Desktop/Temperature study/STRIDE_processed.csv')

dataVeterans$race <- factor(as.character(dataVeterans$race),levels = c("white", "black"))
combinedData$race [combinedData$race=="asian"]<- "other"
combinedData$race <- factor(combinedData$race,levels = c("white", "black","other" ))
dataNHANES$race[dataNHANES$race == "unknown"] <- NA
dataNHANES$race <- factor(dataNHANES$race,levels = c("white", "black","other" ))
ST_data$race [ST_data$race=="Asian"]<- "Other"
ST_data$race <- factor(ST_data$race,levels = c("White", "Black","Other"))
ST_data$exam_month<-factor (ST_data$exam_month)

dataVeterans$state_type <- factor(dataVeterans$state_type, levels = c("very cold", "cold","moderately_cold","moderate","warm", "hot"))

dataNHANES$exammonth <-as.factor(dataNHANES$exammonth)
dataVeterans$exammonth <-as.factor(dataVeterans$exammonth)


state_temps <- read.csv("~/Desktop/Temperature study/state_temps.csv")
state_codes <- read.csv("~/Desktop/Temperature study/state_codes.csv")
state_codes$Code <- factor(as.character(state_codes$Code), levels=levels(dataVeterans$state))
state_temps$state_code <- state_codes$Code[match(as.character(state_temps$state),as.character(state_codes$State))]

dataVeterans$ambient_temp <- (state_temps$value[match(paste(dataVeterans$state,(dataVeterans$examyear * 100 + as.numeric(dataVeterans$exammonth))), paste(state_temps$state_code,state_temps$date))] - 32) / 1.8

ST_data$state <- "CA"
  
ST_data$ambient_temp <- (state_temps$value[match(paste(ST_data$state,(ST_data$exam_year * 100 + as.numeric(ST_data$exam_month))), paste(state_temps$state_code,state_temps$date))] - 32) / 1.8

```

Month effect analysis

Figure 1-figure supplement 5: Effect of the month of temperature measurement.
Mixed effect predictions and confidence intervals of the month during which the measurement was taken on the measured temperature. A. White men in UACWV, B. white men in NHANES I, C. white men in STRIDE, D. white women in NHANES I, and E. white women in STRIDE

```{r message = FALSE, warning = FALSE}
library(arm)
lmerST_data_month <-lmer(temp_C~ age_years + weight_KG + height_CM + (1 | exam_month), data= ST_data [ST_data$GENDER == "Male" & ST_data$race == "White",])
summary(lmerST_data_month)

lmerST_data_month_re <- data.frame(re=as.numeric(ranef(lmerST_data_month)$exam_month[,1]), se=as.numeric(se.ranef(lmerST_data_month)$exam_month[,1]))
lmerST_data_month_re$low <- lmerST_data_month_re$re - 1.96 * lmerST_data_month_re$se
lmerST_data_month_re$high <- lmerST_data_month_re$re + 1.96 * lmerST_data_month_re$se
lmerST_data_month_re$month <- factor(c("January","February","March","April","May","June","July","August","September","October","November","December"),levels=c("January","February","March","April","May","June","July","August","September","October","November","December"))

lmerST_data_month_re$sex <- "men"
lmerST_data_month_re$race <- "white"
lmerST_data_month_re$period <- "STRIDE"

ggplot(data=lmerST_data_month_re,aes(x=month, y=re, color = "blue")) + geom_point() + geom_linerange(aes(ymin=low, ymax=high)) + labs(title="Month effect on temperature in white men STRIDE", x="", y="Temperature Difference, C", colour="", fill="") + theme(legend.position="none", axis.text.x = element_text(angle = 30, hjust = 1))

```

```{r message = FALSE, warning = FALSE}

library(arm)
lmerST_data_month_F <-lmer(temp_C~ age_years + weight_KG + height_CM + (1 | exam_month), data= ST_data [ST_data$GENDER == "Female" & ST_data$race == "White",])
summary(lmerST_data_month_F)

lmerST_data_month_F_re <- data.frame(re=as.numeric(ranef(lmerST_data_month_F)$exam_month[,1]), se=as.numeric(se.ranef(lmerST_data_month_F)$exam_month[,1]))
lmerST_data_month_F_re$low <- lmerST_data_month_F_re$re - 1.96 * lmerST_data_month_F_re$se
lmerST_data_month_F_re$high <- lmerST_data_month_F_re$re + 1.96 * lmerST_data_month_F_re$se
lmerST_data_month_F_re$month <- factor(c("January","February","March","April","May","June","July","August","September","October","November","December"),levels=c("January","February","March","April","May","June","July","August","September","October","November","December"))

lmerST_data_month_F_re$sex <- "women"
lmerST_data_month_F_re$race <- "white"
lmerST_data_month_F_re$period <- "STRIDE"

ggplot(data=lmerST_data_month_F_re,aes(x=month, y=re, color = "red")) + geom_point() + geom_linerange(aes(ymin=low, ymax=high)) + labs(title="Month effect on temperature in white women in STRIDE", x="", y="Temperature Difference, C", colour="", fill="") + theme(legend.position="none", axis.text.x = element_text(angle = 30, hjust = 1))

```

```{r message = FALSE, warning = FALSE}
library(arm)
lmerVeterans_month <-lmer(temp_C~ age + weightKG + heightCM + (1 | exammonth), data= dataVeterans [dataVeterans$race == "white",])
summary(lmerVeterans_month)

lmerVeterans_month_re <- data.frame(re=as.numeric(ranef(lmerVeterans_month)$exammonth[,1]), se=as.numeric(se.ranef(lmerVeterans_month)$exammonth[,1]))
lmerVeterans_month_re$low <- lmerVeterans_month_re$re - 1.96 * lmerVeterans_month_re$se
lmerVeterans_month_re$high <- lmerVeterans_month_re$re + 1.96 * lmerVeterans_month_re$se
lmerVeterans_month_re$month <- factor(c("January","February","March","April","May","June","July","August","September","October","November","December"),levels=c("January","February","March","April","May","June","July","August","September","October","November","December"))

lmerVeterans_month_re$sex <- "men"
lmerVeterans_month_re$race <- "white"
lmerVeterans_month_re$period <- "UAVCW"
ggplot(data=lmerVeterans_month_re,aes(x=month, y=re, color = "red")) + geom_point() + geom_linerange(aes(ymin=low, ymax=high)) + labs(title="Month effect on temperature in white men UAVCW", x="", y="Temperature Difference, C", colour="", fill="") + theme(legend.position="none", axis.text.x = element_text(angle = 30, hjust = 1))
 
```


```{r message = FALSE, warning = FALSE}
 lmerNHANES_month <-lmer(temp_C ~ age + weight_KG + height_CM + (1 | exammonth), data=dataNHANES[dataNHANES$sex == "male" & dataNHANES$race == "white", ], weights=dataNHANES$sample_weights[dataNHANES$sex=="male" & dataNHANES$race == "white"])
summary(lmerNHANES_month)
 
lmerNHANES_month_re <- data.frame(re=as.numeric(ranef(lmerNHANES_month)$exammonth[
,1]), se=as.numeric(se.ranef(lmerNHANES_month)$exammonth[,1]))
lmerNHANES_month_re$low <- lmerNHANES_month_re$re - 1.96 * lmerNHANES_month_re$se
lmerNHANES_month_re$high <- lmerNHANES_month_re$re + 1.96 * lmerNHANES_month_re$se
lmerNHANES_month_re$month <- factor(c("January","February","March","April","May","
June","July","August","September","October","November","December"),levels=c("January","February","March","April","May","June","July","August","September","October","November","December"))
lmerNHANES_month_re$sex <- "men"
lmerNHANES_month_re$race <- "white"
lmerNHANES_month_re$period <- "NHANES"

ggplot(data=lmerNHANES_month_re,aes(x=month, y=re, color = "red")) + geom_point()+ geom_linerange(aes(ymin=low, ymax=high)) + labs(title="Month effect on temperature in white men NHANES", x="", y="Temperature Difference, C", colour="", fill="") + theme(legend.position="none", axis.text.x = element_text(angle = 30, hjust = 1))

```

```{r message = FALSE, warning = FALSE}
 lmNHANES_W_F_month <- lm(temp_C ~ age + weight_KG + height_CM +exammonth, data=dataNHANES[dataNHANES$sex == "female" & dataNHANES$race == "white", ], weights=dataNHANES$sample_weights[dataNHANES$sex=="female" & dataNHANES$race == "white"])
summary(lmNHANES_W_F_month)

```

```{r message = FALSE, warning = FALSE}
lmerNHANES_month_F <-lmer(temp_C ~ age + weight_KG + height_CM + (1 | exammonth),
data=dataNHANES[dataNHANES$sex == "female" & dataNHANES$race == "white", ], weights=dataNHANES$sample_weights[dataNHANES$sex=="female" & dataNHANES$race == "white"])
summary(lmerNHANES_month_F)

 
lmerNHANES_month_F_re <- data.frame(re=as.numeric(ranef(lmerNHANES_month_F)$exammonth[,1]),se=as.numeric(se.ranef(lmerNHANES_month_F)$exammonth[,1]))
lmerNHANES_month_F_re$low <- lmerNHANES_month_F_re$re - 1.96 * lmerNHANES_month_F_re$se
lmerNHANES_month_F_re$high <-lmerNHANES_month_F_re$re + 1.96 * lmerNHANES_month_F_re$se
lmerNHANES_month_F_re$month <- factor(c("January","February","March","April","May","June","July","August","September","October","November","December"),levels=c("January","February","March","April","May","June","July","August","September","October","November","December"))

lmerNHANES_month_F_re$sex <- "women"
lmerNHANES_month_F_re$race <- "white"
lmerNHANES_month_F_re$period <- "NHANES"
ggplot(data=lmerNHANES_month_F_re,aes(x=month, y=re, color = "red")) + geom_point(
) + geom_linerange(aes(ymin=low, ymax=high)) + labs(title="Month effect on temperature in white women NHANES", x="", y="Temperature Difference, C", colour="", fill=
"") + theme(legend.position="none", axis.text.x = element_text(angle = 30, hjust =
1))

```

 
```{r message = FALSE, warning = FALSE}

plot_colors <- c("#FF9233", "#29D0D0", "#81C57A", "#575757", "#8126C0", "#29D0D0" ,"#575757", "#AD2323", "#2A4BD7", "#1D6914", "#814A19", "#8126C0", "#A0A0A0", "#FFEE33", "#FFCDF3") # gg_color_hue(3)

monthlyData <- rbind(lmerNHANES_month_F_re,lmerNHANES_month_re,lmerVeterans_month_re,lmerST_data_month_re,lmerST_data_month_F_re)
monthlyData$sex <- factor(monthlyData$sex,levels=c("men","women"))
monthlyData$race <- factor(monthlyData$race,levels=c("white"))
monthlyData$period <- factor(monthlyData$period,levels=c("UAVCW","NHANES","STRIDE"))

ggplot(data=monthlyData[!is.na(monthlyData$month),],aes(x=month, y=re, color = sex)) + geom_point() + geom_linerange(aes(ymin=low, ymax=high)) + labs(title="Month effect on temperature", x="", y="Temperature Difference, C", colour="", fill="") + theme(legend.position="none", axis.text.x= element_text(angle = 60, hjust = 1)) + facet_grid(sex ~ period) + scale_colour_manual(values = plot_colors [c(9,8 )])
 

```
