【发布时间】:2020-11-11 15:42:06
【问题描述】:
我有一个循环,目前可以在 R 中测试具有一个结果的多次曝光。
下面的代码测试结果 y 与 exp1、exp2 和 exp3 的关联。
我的问题是,测试 y、y1、y2、y3、y4 的相同曝光关联的最佳/有效方法是什么?我正在尝试为多重曝光和多重结果运行 glm。而不是我为 5 个结果复制循环 5 次。
# Build data --------------------------------------------------------------
amino_df <- data.frame(y = rbinom(100, 1, 0.5), y2 = rbinom(100, 1, 0.3), y3 = rbinom(100, 1, 0.2), y4 = rbinom(100, 1, 0.22),
exp1 = rnorm(100), exp2 = rnorm(100), exp3 = rnorm(100))
# Observational estimates unadjusted -------------------------------------------------
exp <- c("exp1", "exp2", "exp3")
obs_results <- data.frame()
for (i in seq_along(exp))
{
mod <- as.formula(sprintf("y ~ %s", exp[i]))
glmmodel <- glm(formula = mod, family = binomial, data = amino_df)
obs_results[i,1] <- names(coef(glmmodel))[2]
obs_results[i,2] <- exp(glmmodel$coefficients[2])
obs_results[i,3] <- summary(glmmodel)$coefficients[2,2]
obs_results[i,4] <- summary(glmmodel)$coefficients[2,4]
obs_results[i,5] <- exp(confint.default(glmmodel)[2,1])
obs_results[i,6] <- exp(confint.default(glmmodel)[2,2])
colnames(obs_results) <- c("exposure","OR", "SE", "P_value", "95_CI_LOW","95_CI_HIGH")
}
【问题讨论】:
-
您是否针对每个 exp 变量测试每个 y?即
y~exp1, y~exp2,y~exp3, y2~exp1, y2~exp2, y2~exp3,...?