我已使用summary()
包中的mediation
函数,并希望将结果转换为data.frame
(因此我可以在将其保存为CSV之前进行更多操作文件)。
这是summary(mediation)
输出的示例。
我在tidy
包和普通broom
函数中尝试了data.frame()
函数但没有成功。
答案 0 :(得分:1)
更新(忘记下面的丑陋的事情)
extract_mediation_summary <- function (x) {
clp <- 100 * x$conf.level
isLinear.y <- ((class(x$model.y)[1] %in% c("lm", "rq")) ||
(inherits(x$model.y, "glm") && x$model.y$family$family ==
"gaussian" && x$model.y$family$link == "identity") ||
(inherits(x$model.y, "survreg") && x$model.y$dist ==
"gaussian"))
printone <- !x$INT && isLinear.y
if (printone) {
smat <- c(x$d1, x$d1.ci, x$d1.p)
smat <- rbind(smat, c(x$z0, x$z0.ci, x$z0.p))
smat <- rbind(smat, c(x$tau.coef, x$tau.ci, x$tau.p))
smat <- rbind(smat, c(x$n0, x$n0.ci, x$n0.p))
rownames(smat) <- c("ACME", "ADE", "Total Effect", "Prop. Mediated")
} else {
smat <- c(x$d0, x$d0.ci, x$d0.p)
smat <- rbind(smat, c(x$d1, x$d1.ci, x$d1.p))
smat <- rbind(smat, c(x$z0, x$z0.ci, x$z0.p))
smat <- rbind(smat, c(x$z1, x$z1.ci, x$z1.p))
smat <- rbind(smat, c(x$tau.coef, x$tau.ci, x$tau.p))
smat <- rbind(smat, c(x$n0, x$n0.ci, x$n0.p))
smat <- rbind(smat, c(x$n1, x$n1.ci, x$n1.p))
smat <- rbind(smat, c(x$d.avg, x$d.avg.ci, x$d.avg.p))
smat <- rbind(smat, c(x$z.avg, x$z.avg.ci, x$z.avg.p))
smat <- rbind(smat, c(x$n.avg, x$n.avg.ci, x$n.avg.p))
rownames(smat) <- c("ACME (control)", "ACME (treated)",
"ADE (control)", "ADE (treated)", "Total Effect",
"Prop. Mediated (control)", "Prop. Mediated (treated)",
"ACME (average)", "ADE (average)", "Prop. Mediated (average)")
}
colnames(smat) <- c("Estimate", paste(clp, "% CI Lower", sep = ""),
paste(clp, "% CI Upper", sep = ""), "p-value")
smat
}
并且,在行动中:
extract_mediation_summary(summary(med.out))
## Estimate 95% CI Lower 95% CI Upper p-value
## ACME (control) 0.083271207 0.03938198 0.1334100 0.00
## ACME (treated) 0.083228279 0.03685293 0.1328788 0.00
## ADE (control) 0.009957454 -0.10992815 0.1705339 0.90
## ADE (treated) 0.009914526 -0.12182752 0.1781260 0.90
## Total Effect 0.093185734 -0.03049831 0.2458085 0.16
## Prop. Mediated (control) 0.807209060 -7.10847324 8.8136165 0.16
## Prop. Mediated (treated) 0.820068951 -6.45663184 8.2852297 0.16
## ACME (average) 0.083249743 0.03910592 0.1322914 0.00
## ADE (average) 0.009935990 -0.11587783 0.1743300 0.90
## Prop. Mediated (average) 0.813639005 -6.78255254 8.5494231 0.16
丑陋,但确实有效:
library(mediation)
library(sandwich)
data("framing")
med.fit <- lm(emo ~ treat + age + educ + gender + income, data = framing)
out.fit <- glm(cong_mesg ~ emo + treat + age + educ + gender + income,
data = framing, family = binomial("probit"))
med.out <- mediate(med.fit, out.fit, treat = "treat", mediator = "emo",
robustSE = TRUE, sims = 100)
# extract summary
library(purrr)
summary(med.out) %>%
capture.output() %>%
discard(`==`, "") -> lines
lines[which(grepl("^ ", lines)):(which(grepl("^Sample", lines))-1)] %>%
sub("^ ", "med.out", .) %>%
gsub(" 95%", "_95%", .) %>%
gsub("CI ", "ci_", .) %>%
sub(" \\(", "_(", .) %>%
sub("p-", "p_", .) %>%
sub(" ", "_", ., fixed=TRUE) %>%
textConnection() %>%
read.table(header=TRUE) %>%
setNames(sub("_$", "", colnames(.))) %>%
dplyr::mutate(med.out=sub("\\.|_$", "", med.out),
med.out=gsub("_", " ", med.out))
## med.out Estimate_95. ci_Lower_95. ci_Upper p_value
## 1 ACME (control) 0.0873 0.0382 0.1593 0.00
## 2 ACME (treated) 0.0876 0.0434 0.1586 0.00
## 3 ADE (control) 0.0115 -0.0869 0.1169 0.86
## 4 ADE (treated) 0.0117 -0.0986 0.1238 0.86
## 5 Total Effect 0.0991 -0.0118 0.2160 0.10
## 6 Prop Mediated (control) 0.7862 -4.2247 6.1448 0.10
## 7 Prop Mediated (treated) 0.8044 -3.7360 5.6117 0.10
## 8 ACME (average) 0.0875 0.0409 0.1556 0.00
## 9 ADE (average) 0.0116 -0.0932 0.1204 0.86
## 10 Prop Mediated (average) 0.7953 -3.9803 5.8783 0.10