# 相加交互作用的通用函数，先运行这个函数
MUM_Interaction_svyglm <- function(model, coef.index){

  theta1 <- coef(model)[coef[1]] # 0.05515819
  theta2 <- coef(model)[coef[2]] # 0.1048913
  theta3 <- coef(model)[coef[3]] # 0.06646235
   
  theta1.se <- SE(model)[coef[1]]
  theta2.se <- SE(model)[coef[2]]
  theta3.se <- SE(model)[coef[3]]

  cov.mat <- vcov(model)
  h1 <- exp(theta1 + theta2 + theta3) - exp(theta1)
  h2 <- exp(theta1 + theta2 + theta3) - exp(theta2)
  h3 <- exp(theta1 + theta2 + theta3)
  reri.var <- (h1^2 * theta1.se^2) + (h2^2 * theta2.se^2) + 
    (h3^2 * theta3.se^2) + (2 * h1 * h2 * cov.mat[coef[1], 
                                                  coef[2]]) + (2 * h1 * h3 * cov.mat[coef[1], coef[3]]) + 
    (2 * h2 * h3 * cov.mat[coef[2], coef[3]])
  reri.se <- sqrt(reri.var)
  reri.p <- exp(theta1 + theta2 + theta3) - exp(theta1) - 
    exp(theta2) + 1 # 注意，这里是 OR
  reri.l <- reri.p - (z * reri.se)
  reri.u <- reri.p + (z * reri.se)
  reri <- data.frame(est = reri.p, lower = reri.l, upper = reri.u)
  mult.p <- as.numeric(exp(theta3))
  mult.ci <- suppressMessages(confint(object = model, parm = coef[3]))
  mult.l <- as.numeric(exp(mult.ci[1]))
  mult.u <- as.numeric(exp(mult.ci[2]))
  multiplicative <- data.frame(est = mult.p, lower = mult.l, 
                               upper = mult.u)
  cov.mat <- vcov(model)
  h1 <- ((exp(theta1 + theta2 + theta3) - exp(theta1))/(exp(theta1 + 
                                                              theta2 + theta3))) - ((exp(theta1 + theta2 + theta3) - 
                                                                                       exp(theta1) - exp(theta2) + 1)/(exp(theta1 + theta2 + 
                                                                                                                             theta3)))
  h2 <- ((exp(theta1 + theta2 + theta3) - exp(theta2))/(exp(theta1 + 
                                                              theta2 + theta3))) - ((exp(theta1 + theta2 + theta3) - 
                                                                                       exp(theta1) - exp(theta2) + 1)/(exp(theta1 + theta2 + 
                                                                                                                             theta3)))
  h3 <- 1 - ((exp(theta1 + theta2 + theta3) - exp(theta1) - 
                exp(theta2) + 1)/exp(theta1 + theta2 + theta3))
  apab.var <- (h1^2 * theta1.se^2) + (h2^2 * theta2.se^2) + 
    (h3^2 * theta3.se^2) + (2 * h1 * h2 * cov.mat[coef[1], 
                                                  coef[2]]) + (2 * h1 * h3 * cov.mat[coef[1], coef[3]]) + 
    (2 * h2 * h3 * cov.mat[coef[2], coef[3]])
  apab.se <- sqrt(apab.var)
  apab.p <- (exp(theta1 + theta2 + theta3) - exp(theta1) - 
               exp(theta2) + 1)/exp(theta1 + theta2 + theta3)
  apab.l <- apab.p - (z * apab.se)
  apab.u <- apab.p + (z * apab.se)
  apab <- data.frame(est = apab.p, lower = apab.l, upper = apab.u)
  s.p <- (exp(theta1 + theta2 + theta3) - 1)/(exp(theta1) + 
                                                exp(theta2) - 2)
  cov.mat <- vcov(model)
  if (class(model)[1] == "glm" & class(model)[2] == "lm" & 
      s.p < 0) {
    warning(paste("Point estimate of synergy index (S) is less than zero (", 
                  round(s.p, digits = 2), ").\n  Confidence intervals cannot be calculated using the delta method. Consider re-parameterising as linear odds model.", 
                  sep = ""))
  }
  if (class(model)[1] == "clogit" & class(model)[2] == 
      "coxph" & s.p < 0) {
    warning(paste("Point estimate of synergy index (S) is less than zero (", 
                  round(s.p, digits = 2), ").\n  Confidence intervals cannot be calculated using the delta method. Consider re-parameterising as linear odds model.", 
                  sep = ""))
  }
  if (class(model)[1] == "geeglm" & class(model)[2] == 
      "gee" & s.p < 0) {
    warning(paste("Point estimate of synergy index (S) is less than zero (", 
                  round(s.p, digits = 2), ").\n  Confidence intervals cannot be calculated using the delta method. Consider re-parameterising as linear odds model.", 
                  sep = ""))
  }
  if (class(model)[1] == "glmerMod" & s.p < 0) {
    warning(paste("Point estimate of synergy index (S) is less than zero (", 
                  round(s.p, digits = 2), ").\n  Confidence intervals cannot be calculated using the delta method. Consider re-parameterising as linear odds model.", 
                  sep = ""))
  }
  h1 <- ((exp(theta1 + theta2 + theta3))/(exp(theta1 + 
                                                theta2 + theta3) - 1)) - (exp(theta1)/(exp(theta1) + 
                                                                                         exp(theta2) - 2))
  h2 <- ((exp(theta1 + theta2 + theta3))/(exp(theta1 + 
                                                theta2 + theta3) - 1)) - (exp(theta2)/(exp(theta1) + 
                                                                                         exp(theta2) - 2))
  h3 <- exp(theta1 + theta2 + theta3)/(exp(theta1 + theta2 + 
                                             theta3) - 1)
  lns.var <- h1^2 * theta1.se^2 + h2^2 * theta2.se^2 + 
    h3^2 * theta3.se^2 + (2 * h1 * h2 * cov.mat[coef[2], 
                                                coef[1]]) + (2 * h1 * h3 * cov.mat[coef[3], coef[1]]) + 
    (2 * h2 * h3 * cov.mat[coef[3], coef[2]])
  lns.se <- sqrt(lns.var)
  lns.p <- log(s.p)
  lns.l <- lns.p - (z * lns.se)
  lns.u <- lns.p + (z * lns.se)
  s.l <- exp(lns.l)
  s.u <- exp(lns.u)
  s <- data.frame(est = s.p, lower = s.l, upper = s.u)
  rval <- list(reri = reri, apab = apab, s = s, multiplicative = multiplicative)
  
  return(rval)
}
model <- F10
coef.index <- c(2,3,10)
# coef.index 是指，关注的交互变量在模型中的序号，以 F10，为例，则是 2，3，10
MUM_Interaction_svyglm(F10, coef.index = c(2,3,10))

#### 既往代码-可以不采用了 ####
interaction_coef <- coef(F10)["depression.group3Depression:sleep.disorderSleep Disorder"]
interaction_se <- sqrt(diag(vcov(F10)))["depression.group3Depression:sleep.disorderSleep Disorder"]

# 获取depression.group3和sleep.disorder的系数估计值和标准误差
depression_coef <- coef(F10)["depression.group3Depression"]
depression_se <- sqrt(diag(vcov(F10)))["depression.group3Depression"]
sleep_disorder_coef <- coef(F10)["sleep.disorderSleep Disorder"]
sleep_disorder_se <- sqrt(diag(vcov(F10)))["sleep.disorderSleep Disorder"]

# 计算相对过量风险（RERI）
RERI <- interaction_coef - depression_coef - sleep_disorder_coef
# 计算RERI的95%置信区间
RERI_se <- sqrt(interaction_se^2 + depression_se^2 + sleep_disorder_se^2)
RERI_CI <- RERI + qnorm(c(0.025, 0.975)) * RERI_se

# 计算相互作用归因比例（API）
API <- (interaction_coef - depression_coef - sleep_disorder_coef) / interaction_coef
# 计算API的95%置信区间
API_se <- sqrt((interaction_se / interaction_coef)^2 + (depression_se / depression_coef)^2 + (sleep.disorder_se / sleep_disorder_coef)^2)
API_CI <- API + qnorm(c(0.025, 0.975)) * API_se

# 计算协同作用指数（SI）
SI <- (interaction_coef - depression_coef - sleep_disorder_coef) / (depression_coef + sleep_disorder_coef - interaction_coef)
# 计算SI的95%置信区间
SI_se <- sqrt((interaction_se / (depression_coef + sleep_disorder_coef - interaction_coef))^2 + (depression_se / (depression_coef + sleep_disorder_coef - interaction_coef))^2 + (sleep_disorder_se / (depression_coef + sleep_disorder_coef - interaction_coef))^2)
SI_CI <- SI + qnorm(c(0.025, 0.975)) * SI_se
