用R语言进行中介分析的代码如下
getmedre_vm2wmhv <- function(X,Y,M,C1,C2,C3,C4,C5,C6,C7){
Data_X <- data.frame(M = M, X = X, C1 = C1, C2 = C2, C3 = C3, C4 = C4, C5 = C5, C6 = C6, C7 = C7)
Data_Y <- data.frame(Y = Y,M = M, X = X, C1 = C1, C2 = C2, C3 = C3, C4 = C4, C5 = C5, C6 = C6, C7 = C7)
fitM <- lm(M ~ X + C1 + C2 + C3 + C4 + C5+ C6 + C7,data = Data_X)
fitY <- glm(Y ~ X + M + C1 + C2 + C3 + C4 + C5 + C6 + C7,data = Data_Y,family = Gamma(link = "log"))
fitMed <- mediate(fitM, fitY, treat="X", mediator="M",sims = 5000,control.value = mean(X),treat.value = mean(X)+1)
sum_fitM <- summary(fitM)
sum_fitY <- summary(fitY)
re_beta <- c(sum_fitM$coefficients[2,1],sum_fitY$coefficients[3,1],fitMed$z0,fitMed$d0,fitMed$tau.coef)
re_ci_m <- c(sum_fitM$coefficients[2,1]-1.96*sum_fitM$coefficients[2,1],
sum_fitY$coefficients[3,1]-1.96*sum_fitY$coefficients[3,1],
unname(fitMed$z0.ci[1]),
unname(fitMed$d0.ci[1]),unname(fitMed$tau.ci[1]))
re_ci_p<- c(sum_fitM$coefficients[2,1]+1.96*sum_fitM$coefficients[2,1],
sum_fitY$coefficients[3,1]+1.96*sum_fitY$coefficients[3,1],
unname(fitMed$z0.ci[2]),
unname(fitMed$d0.ci[2]),unname(fitMed$tau.ci[2]))
re_pval <- c(sum_fitM$coefficients[2,4],sum_fitY$coefficients[3,4],fitMed$z0.p,fitMed$d0.p,fitMed$tau.p)
return(rbind(re_beta,re_ci_m,re_ci_p,re_pval))
}
返回值解释
从左到右的四列,分别是pathA,pathB,pathC'.pathAB,pathc