setwd("D:/charls/")
library(lme4)
library(lmerTest)
library(haven)
library(tidyverse)
library(lm.beta)
library(tableone)
library(charlsMAX)
library(httr)
base <- read.csv("base.csv")

glm_effect <- function(model, pollutant_var,name) {
  result <- tidy(model, conf.int = TRUE, exponentiate = TRUE) %>%
    filter(term == pollutant_var) %>%
    mutate(
      Pollutant = case_when(
        grepl("PM2.5", pollutant_var) ~ "PM2.5",
        grepl("NO2", pollutant_var) ~ "NO2",
        grepl("SO2", pollutant_var) ~ "SO2"
      ),
      value = name,
      OR_CI = sprintf("%.3f (%.3f–%.3f)", estimate, conf.low, conf.high),
      Significance = case_when(
        p.value < 0.001 ~ "***",
        p.value < 0.01 ~ "**",
        p.value < 0.05 ~ "*",
        TRUE ~ ""
      )
    )
  return(result)
}
#PM2.5
PM2.5 <- glm(
  blood ~ PM2.5_Q + age + sex + marriage + house + activity + smoke + drink + health + sleep + depression + BMI, 
  family = binomial,
  data = base
)

#PM2.5+NO2
PM2.5_NO2 <- glm(
  blood ~ PM2.5_Q + NO2_Q + age + sex + marriage + house + activity + smoke + drink + health + sleep + depression + BMI, 
  family = binomial,
  data = base
)

#PM2.5+SO2
PM2.5_SO2 <- glm(
  blood ~ PM2.5_Q + SO2_Q + age + sex + marriage + house + activity + smoke + drink + health + sleep + depression + BMI, 
  family = binomial,
  data = base
)

#PM2.5+NO2+SO2
PM2.5_NO2_SO2 <- glm(
  blood ~ PM2.5_Q + NO2_Q + SO2_Q + age + sex + marriage + house + activity + smoke + drink + health + sleep + depression + BMI, 
  family = binomial,
  data = base
)

PM2.5_result <- glm_effect(PM2.5, "PM2.5_Q","PM2.5")
PM2.5_NO2_result <- glm_effect(PM2.5_NO2, "PM2.5_Q","PM2.5+NO2")
PM2.5_SO2_result <- glm_effect(PM2.5_SO2, "PM2.5_Q","PM2.5+SO2")
PM2.5_NO2_SO2_result <- glm_effect(PM2.5_NO2_SO2, "PM2.5_Q","PM2.5+NO2+SO2")

#NO2
NO2 <- glm(
  blood ~ NO2_Q + age + sex + marriage + house + activity + smoke + drink + health + sleep + depression + BMI, 
  family = binomial,
  data = base
)

#NO2+PM2.5
NO2_PM2.5 <- glm(
  blood ~ NO2_Q + PM2.5_Q + age + sex + marriage + house + activity + smoke + drink + health + sleep + depression + BMI, 
  family = binomial,
  data = base
)

#NO2+SO2
NO2_SO2 <- glm(
  blood ~ NO2_Q + SO2 + age + sex + marriage + house + activity + smoke + drink + health + sleep + depression + BMI, 
  family = binomial,
  data = base
)

#NO2+PM2.5+SO2
NO2_PM2.5_SO2 <- glm(
  blood ~ NO2_Q + PM2.5_Q + SO2_Q + age + sex + marriage + house + activity + smoke + drink + health + sleep + depression + BMI, 
  family = binomial,
  data = base
)

NO2_result <- glm_effect(NO2, "NO2_Q","NO2")
NO2_PM2.5_result <- glm_effect(NO2_PM2.5, "NO2_Q","NO2+PM2.5")
NO2_SO2_result <- glm_effect(NO2_SO2, "NO2_Q","NO2+SO2")
NO2_PM2.5_SO2_result <- glm_effect(NO2_PM2.5_SO2, "NO2_Q","NO2+PM2.5+SO2")

#SO2
SO2 <- glm(
  blood ~ SO2_Q + age + sex + marriage + house + activity + smoke + drink + health + sleep + depression + BMI, 
  family = binomial,
  data = base
)

#SO2+PM2.5
SO2_PM2.5 <- glm(
  blood ~ SO2_Q + PM2.5_Q + age + sex + marriage + house + activity + smoke + drink + health + sleep + depression + BMI, 
  family = binomial,
  data = base
)

#SO2+NO2
SO2_NO2 <- glm(
  blood ~ SO2_Q + NO2_Q + age + sex + marriage + house + activity + smoke + drink + health + sleep + depression + BMI, 
  family = binomial,
  data = base
)

#SO2+PM2.5+NO2
SO2_PM2.5_NO2 <- glm(
  blood ~ SO2_Q + PM2.5 + NO2 + age + sex + marriage + house + activity + smoke + drink + health + sleep + depression + BMI, 
  family = binomial,
  data = base
)

SO2_result <- glm_effect(SO2, "SO2_Q","SO2")
SO2_PM2.5_result <- glm_effect(SO2_PM2.5, "SO2_Q","SO2+PM2.5")
SO2_NO2_result <- glm_effect(SO2_NO2, "SO2_Q","SO2+NO2")
SO2_PM2.5_NO2_result <- glm_effect(SO2_PM2.5_NO2, "SO2_Q","SO2+PM2.5+NO2")

#合并结果
pollution_result <- bind_rows(
  PM2.5_result, PM2.5_NO2_result, PM2.5_SO2_result,PM2.5_NO2_SO2_result,
  NO2_result, NO2_PM2.5_result, NO2_SO2_result,NO2_PM2.5_SO2_result,
  SO2_result, SO2_PM2.5_result, SO2_NO2_result,SO2_PM2.5_NO2_result
) 
pollution_result <- pollution_result %>%transmute(name = value,
                                                  `OR（95%CI）`= OR_CI,
                                                  P = p.value)