We illustrate the general workflow of the CMAverse
package by a quick example. The general workflow is:
Plot the DAG of causal relationships using cmdag
.
Estimate causal effects and make inferences using cmest
.
Conduct sensitivity analysis for unmeasured confounding and measurement error using cmsens
.
Firstly, let's load the package.
library(CMAverse)
Next, we simulate some data and plot the DAG. The simulated dataset contains a binary exposure, a binary mediator, a continuous mediator, a continuous outcome and two baseline confounders.
set.seed(1) n <- 100 C1 <- rnorm(n, mean = 1, sd = 1) C2 <- rbinom(n, 1, 0.6) pa <- exp(0.2 - 0.5*C1 + 0.1*C2)/(1 + exp(0.2 - 0.5*C1 + 0.1*C2)) A <- rbinom(n, 1, pa) pm <- exp(1 + 0.5*A - 1.5*C1 + 0.5*C2)/ (1 + exp(1 + 0.5*A - 1.5*C1 + 0.5*C2)) M1 <- rbinom(n, 1, pm) M2 <- rnorm(n, 2 + 0.8*A - M1 + 0.5*C1 + 2*C2, 1) Y <- rnorm(n, mean = 0.5 + 0.4*A + 0.5*M1 + 0.6*M2 + 0.3*A*M1 + 0.2*A*M2 - 0.3*C1 + 2*C2, sd = 1) data <- data.frame(A, M1, M2, Y, C1, C2)
The DAG can be plotted using cmdag
.
cmdag(outcome = "Y", exposure = "A", mediator = c("M1", "M2"), basec = c("C1", "C2"), postc = NULL, node = FALSE, text_col = "black")
Then, we estimate causal effects using cmest
. We use the regression-based approach for illustration. The reference values for the exposure are set to be 0 and 1. The reference values for the two mediators are set to be 1.
est <- cmest(data = data, model = "rb", outcome = "Y", exposure = "A", mediator = c("M1", "M2"), basec = c("C1", "C2"), EMint = TRUE, mreg = list("logistic", "linear"), yreg = "linear", astar = 0, a = 1, mval = list(1, 1), estimation = "imputation", inference = "bootstrap", nboot = 20)
Summarize and plot the results:
summary(est)
ggcmest(est) + ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 30, vjust = 0.8))
Lastly, we conduct sensitivity analysis for the results. Sensitivity analysis for unmeasured confounding:
cmsens(object = est, sens = "uc")
Assume that $C_1$ was measured with error. Sensitivity analysis for measurement error using regression calibration with a set of assumed error standard deviations 0.1, 0.2 and 0.3:
me1 <- cmsens(object = est, sens = "me", MEmethod = "rc", MEvariable = "C1", MEvartype = "con", MEerror = c(0.1, 0.2, 0.3))
Summarize and plot the results:
summary(me1)
ggcmsens(me1) + ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 30, vjust = 0.8))
Then, assume that the exposure was measured with error. Sensitivity analysis for measurement error using SIMEX with two assumed misclassification matrices:
me2 <- cmsens(object = est, sens = "me", MEmethod = "simex", MEvariable = "A", MEvartype = "cat", B = 5, MEerror = list(matrix(c(0.95, 0.05, 0.05, 0.95), nrow = 2), matrix(c(0.9, 0.1, 0.1, 0.9), nrow = 2)))
Summarize and plot the results:
summary(me2)
ggcmsens(me2) + ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 30, vjust = 0.8))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.