阅读权限 255 威望 0 级论坛币 50288 个 通用积分 83.6306 学术水平 253 点 热心指数 300 点 信用等级 208 点 经验 41518 点 帖子 3256 精华 14 在线时间 766 小时 注册时间 2006-5-4 最后登录 2022-11-6
### Import original data and use the treatment as well as
### baseline BAI as z and x
newdata <- read.csv("Mediation.csv", header = TRUE)
### Generate simulation data, alphau and betau measure
### violation of sequential ignorability and gamma measures
### interaction
gendata <- function(thetaz, thetam, betax, betau, beta0,
alphaz, alphax, alpha0, gamma, alphau) {
z <- 1 - as.numeric(newdata$condition=="Control")
x.m <- newdata$b_bai
x.y <- newdata$b_bai
n <- length(z)
u <- rnorm(n)
m <- alphaz*z + alphax*x.m + gamma*x.m*z + alphau*u +
alpha0 + rnorm(n, sd = 0.2)
y <- thetaz*z + thetam*m + betax*x.y + betau*u + beta0 +
rnorm(n, sd = 0.5)
list(z, m, y, x.m, x.y)
}
library(sandwich) # for robust SE
### RPM Estimation function, return with estimates and CI
### from RPM and OLS
mymed <- function(z, m, y, x.m, x.y){
data <- na.omit(data.frame(z = z, m = m, y = y,
x.m = x.m, x.y = x.y))
modelm <- lm(m ~ z*x.m, data = data)
data1 <- data0 <- data
data1$z <- 1
data0$z <- 0
w <- as.matrix(predict(modelm, data1, type = "response")-predict(modelm, data0, type = "response"))
w1 <- (data$z-mean(data$z))
w2 <- (data$z-mean(data$z))*w
# w4, w5 is for the augmented estimating equations
w4 <- 1
w5 <- data$x.y
WW <- cbind(w1, w2, w4, w5)
# solve the equation by its explicit expression
MM <- cbind(data$z, data$m, 1, data$x.y)
XX <- crossprod(WW, MM)
YY <- crossprod(WW, data$y)
theta <- tcrossprod(solve(XX), t(YY))
# calculate y00 and then get sandwich variance estimation
y0 <- data$y-tcrossprod(MM, t(theta))
V <- tcrossprod(tcrossprod(solve(XX), WW*as.vector(y0)))
B3 <- solve(solve(crossprod(WW*as.vector(y0))))
A <- XX
V3 <- (solve(A)%*%B3%*%solve(t(A)))[1:2, 1:2]
# Calculate 95% CI
res3 <- cbind(theta[1:2], theta[1:2] + qnorm(0.025)*sqrt(diag(V3)), theta[1:2] + qnorm(0.975)*sqrt(diag(V3)))
#Indirect effect and Mediated effect via OLS
res <- lm(y~z + m + x.y, data = data)
#Compute 95% CI for indirect and mediated effect
rbind(res3, cbind(coef(res), coef(res)-1.96*sqrt(diag(vcovHC(res))), coef(res) + 1.96*sqrt(diag(vcovHC(res))))[2:3, ])
}
# one time simulation
onesim <- function(betau = betau, gamma = gamma){
thetaz <- (-1.55)
thetam <- 0.37
betax <- 0.1
beta0 <- 0
alphaz <- -2.2
alphax <- 0.9
alpha0 <- 0
alphau <- 1
simdata <- gendata(thetaz, thetam, betax, betau, beta0, alphaz, alphax, alpha0, gamma, alphau)
z <- simdata[[1]]
m <- simdata[[2]]
y <- simdata[[3]]
x.m <- simdata[[4]]
x.y <- simdata[[5]]
result <- mymed(z, m, y, x.m, x.y)
bias <- result[,1]-rep(c(thetaz, thetam), 2)
cr <- as.numeric(((result[,2]-rep(c(thetaz, thetam), 2))*(result[,3]-rep(c(thetaz, thetam), 2)))<0)
c(bias, cr)
}
# Simulation
M <- 1000
myfun <- function(betau = betau, gamma = gamma) {
res <- replicate(M, onesim(betau, gamma), simplify = TRUE)
# calculate bias and coverage rate
a1 <- apply(res, 1, mean)
# calculate sd
a2 <- apply(res, 1, sd)
out <- cbind(a1[1:4], a2[1:4], a1[5:8])
colnames(out) <- c("Bias","SD","CR")
rownames(out) <- c("RPM.Z","RPM.M","OLS.Z","OLS.M")
round(out, 2)
}
# Sequential ignorability fails and weak interaction
set.seed(111)
myfun(2, -0.1)
### NOTE: ~17 sec for 1,000 simulations on a current MacBook Pro
# Sequential ignorability fail and strong interaction
set.seed(111)
myfun(2, -0.5)
# Sequential ignorability hold and weak interaction
set.seed(111)
myfun(0, -0.1)
# Sequential ignorability hold and strong interaction
set.seed(111)
myfun(0, -0.5) 复制代码
总评分: 经验 + 100
查看全部评分