setwd("D:/charls/")
library(lme4)
library(lmerTest)
library(haven)
library(tidyverse)
library(lm.beta)
library(tableone)
library(charlsMAX)
library(httr)
base_move <- read.csv("base_move.csv")

#滞后一年
model_PM2.5_1 <- glm(
  blood ~ PM2.5_1_Q + prove + age + sex + marriage + house + activity + smoke + drink + health + sleep + depression + BMI + temperature, 
  family = binomial,
  data = base_move
)

model_NO2_1 <- glm(
  blood ~ NO2_1_Q + prove + age + sex + marriage + house + activity + smoke + drink + health + sleep + depression + BMI + temperature, 
  family = binomial,
  data = base_move
)

model_SO2_1 <- glm(
  blood ~ SO2_1_Q + prove + age + sex + marriage + house + activity + smoke + drink + health + sleep + depression + BMI + temperature, 
  family = binomial,
  data = base_move
)

#当前年混杂区域
model_PM2.5_move <- glm(
  blood ~ PM2.5_Q + prove + age + sex + marriage + house + activity + smoke + drink + health + sleep + depression + BMI + temperature, 
  family = binomial,
  data = base_move
)

model_NO2_move <- glm(
  blood ~ NO2_Q + prove + age + sex + marriage + house + activity + smoke + drink + health + sleep + depression + BMI + temperature, 
  family = binomial,
  data = base_move
)

model_SO2_move <- glm(
  blood ~ SO2_Q + prove + age + sex + marriage + house + activity + smoke + drink + health + sleep + depression + BMI + temperature, 
  family = binomial,
  data = base_move
)

#当前年
model_PM2.5 <- glm(
  blood ~ PM2.5_Q + age + sex + marriage + house + activity + smoke + drink + health + sleep + depression + BMI, 
  family = binomial,
  data = base_move
)

model_NO2 <- glm(
  blood ~ NO2_Q + age + sex + marriage + house + activity + smoke + drink + health + sleep + depression + BMI, 
  family = binomial,
  data = base_move
)

model_SO2 <- glm(
  blood ~ SO2_Q + age + sex + marriage + house + activity + smoke + drink + health + sleep + depression + BMI, 
  family = binomial,
  data = base_move
)

library(dplyr)
library(broom)
library(ggplot2)

# ---- 自定义函数：提取GLM模型结果 ----
extract_glm_effect <- function(model, pollutant_var, time_label) {
  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"
      ),
      Time = time_label,
      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)
}

# ---- 提取所有模型结果 ----
# 当前年模型
res_pm25_now <- extract_glm_effect(model_PM2.5, "PM2.5_Q", "Current year")
res_no2_now <- extract_glm_effect(model_NO2, "NO2_Q", "Current year")
res_so2_now <- extract_glm_effect(model_SO2, "SO2_Q", "Current year")

# 当前年混杂区域模型
res_pm25_move <- extract_glm_effect(model_PM2.5_move, "PM2.5_Q", "Confounding area and temperature")
res_no2_move <- extract_glm_effect(model_NO2_move, "NO2_Q", "Confounding area and temperature")
res_so2_move <- extract_glm_effect(model_SO2_move, "SO2_Q", "Confounding area and temperature")

# 滞后1年模型
res_pm25_lag1 <- extract_glm_effect(model_PM2.5_1, "PM2.5_1_Q", "One-year lag")
res_no2_lag1 <- extract_glm_effect(model_NO2_1, "NO2_1_Q", "One-year lag")
res_so2_lag1 <- extract_glm_effect(model_SO2_1, "SO2_1_Q", "One-year lag")

# 合并结果
plot_data <- bind_rows(
  res_pm25_now, res_no2_now, res_so2_now,
  res_pm25_move, res_no2_move, res_so2_move,
  res_pm25_lag1, res_no2_lag1, res_so2_lag1
) %>%
  mutate(
    Pollutant = factor(Pollutant, levels = c("PM2.5", "NO2", "SO2")),
    Time = factor(Time, levels = c("Current year","Confounding area and temperature", "One-year lag"))
  )

ggplot(plot_data, aes(x = Pollutant, y = estimate, color = Time)) +
  geom_point(
    aes(shape = Time), 
    size = 5, 
    position = position_dodge(width = 0.6)
  ) +
  geom_errorbar(
    aes(ymin = conf.low, ymax = conf.high),
    width = 0.2,
    linewidth = 1.2,
    position = position_dodge(width = 0.6)
  ) +
  geom_hline(yintercept = 1, linetype = "dashed", color = "grey40") +
  scale_y_log10(
    breaks = c(1, 1.2, 1.5, 2), 
    limits = c(1, max(plot_data$conf.high) * 1.2)
  ) +
  scale_color_manual(values = c("Current year" = "#D55E00", "Confounding area and temperature" = "#2ca02c", "One-year lag" = "#0072B2")) +
  scale_shape_manual(values = c("Current year" = 16, "Confounding area and temperature" = 18, "One-year lag" = 17)) +
  labs(
    x = NULL, 
    y = "Odds Ratio (95% CI)",
    color = "",
    shape = ""
  ) +
  theme_minimal(base_size = 14) +
  theme(
    legend.position = "top",
    legend.box = "horizontal",
    panel.grid.major.x = element_blank(),
    axis.text.x = element_text(face = "bold", size = 12),
    plot.title = element_text(hjust = 0.5, face = "bold")
  )