### Example Usage of CMAverse Package Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/simexreg.html This example demonstrates how to load the CMAverse library and clear the environment. It is typically used for setting up an analysis session. ```R if (FALSE) { rm(list=ls()) library(CMAverse) } ``` -------------------------------- ### Basic Usage of svymultinom Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/svymultinom.html Demonstrates how to load the CMAverse library and prepare for using the svymultinom function. This example is conditional and typically run in an interactive session. ```R if (FALSE) { rm(list = ls()) library(CMAverse) } ``` -------------------------------- ### Install CMAverse Package Source: https://github.com/bs1125/cmaverse/blob/master/docs/index.html Installs the latest version of the CMAverse package from GitHub. Ensure you have the devtools package installed. ```r devtools::install_github("BS1125/CMAverse") ``` -------------------------------- ### Example Usage of cmest_multistate Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/cmest_multistate.html Demonstrates how to call the cmest_multistate function with specified data, parameters, and model settings. Ensure the CMAverse library is loaded before use. ```R if (FALSE) { library(CMAverse) multistate_out <- cmest_multistate(data = sc_data, s = s_vec, multistate_seed = 1, exposure = 'A', mediator = 'M', outcome = 'S', yevent = "ind_S", mevent = "ind_M", basec = c("C1", "C2"), basecval = c("C1" = "1", "C2" = as.character(mean(sc_data$C2))), astar="0", a="1", nboot=1, EMint=F, bh_method = "breslow") multistate_out } ``` -------------------------------- ### Perform Sensitivity Analysis and Plot Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/ggcmsens.html This example demonstrates how to perform a sensitivity analysis using cmest and then prepare the data for plotting with ggcmsens. It sets up a statistical model with imputation and bootstrap inference. ```R naive <- cmest(data = cma2020, model = "rb", outcome = "contY", mediator = c("M1", "M2"), basec = c("C1", "C2"), EMint = TRUE, mreg = list("logistic", "multinomial"), yreg = "linear", astar = 0, a = 1, mval = list(0, "M2_0"), estimation = "imputation", inference = "bootstrap", nboot = 10) ``` -------------------------------- ### GLM for Initial Model Setup Source: https://github.com/bs1125/cmaverse/blob/master/docs/articles/multiple_imputation.html This snippet shows the initial generalized linear model call and its summary statistics, likely used for setting up the imputation process or as a baseline. It includes model call, deviance residuals, and coefficients. ```R ## Call: glm(formula = M ~ A + C1 + C2, family = binomial(), data = getCall(x$reg.output[[3L]]$mreg[[1L]])$data, weights = getCall(x$reg.output[[3L]]$mreg[[1L]])$weights) Deviance Residuals: Min 1Q Median 3Q Max -3.2771 0.1135 0.1611 0.2478 0.5803 Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) 0.1175 0.6424 0.183 0.85487 A 1.7076 0.1451 11.765 < 2e-16 *** C1 2.3329 0.6511 3.583 0.00034 *** C2 0.9053 0.1351 6.699 2.1e-11 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 (Dispersion parameter for binomial family taken to be 1) Null deviance: 2279.3 on 9999 degrees of freedom Residual deviance: 2054.9 on 9996 degrees of freedom AIC: 2062.9 Number of Fisher Scoring iterations: 7 ``` -------------------------------- ### Case-Control Mediation Analysis Setup Source: https://github.com/bs1125/cmaverse/blob/master/docs/articles/case_control.html Sets up and runs a case-control mediation analysis with a rare outcome. Specifies logistic regression for both mediator and outcome, and uses parametric function estimation with delta method inference. ```R res_yrare <- cmest(data = data, model = "rb", casecontrol = TRUE, yrare = TRUE, outcome = "Y", exposure = "A", mediator = "M", basec = c("C1", "C2"), EMint = TRUE, mreg = list("logistic"), yreg = "logistic", astar = 0, a = 1, mval = list(1), estimation = "paramfunc", inference = "delta") summary(res_yrare) ``` -------------------------------- ### Weighting-based Mediation Analysis Setup Source: https://github.com/bs1125/cmaverse/blob/master/docs/articles/multiple_mediators.html This snippet demonstrates how to set up a weighting-based causal mediation analysis with multiple mediators (M1, M2) and interaction terms. It specifies logistic regression for outcome and exposure, and uses bootstrap for inference. ```R res_wb <- cmest(data = data, model = "wb", outcome = "Y", exposure = "A", mediator = c("M1", "M2"), basec = c("C1", "C2"), EMint = TRUE, ereg = "logistic", yreg = "logistic", astar = 0, a = 1, mval = list(0, 2), estimation = "imputation", inference = "bootstrap", nboot = 2) ``` -------------------------------- ### ggcmsens Function Call Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/ggcmsens.html Example of calling the ggcmsens function with specified parameters for sensitivity analysis. Ensure the 'cmsens.html' is accessible and parameters are correctly defined. ```r x <- [cmsens](cmsens.html)(object = naive, sens = "me", MEmethod = "rc", MEvariable = "C1", MEvartype = "con", MEerror = [c](https://rdrr.io/r/base/c.html)(0.1, 0.2)) ``` -------------------------------- ### Plot causal effect estimates with flipped coordinates Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/ggcmest.html This example shows how to use ggcmest with coord_flip to reverse the axes, which can be useful for certain types of plots. ```r ggcmest(x) + coord_flip(xlim = NULL, ylim = NULL, expand = TRUE, clip = "on") ``` -------------------------------- ### G-Formula Model Setup Source: https://github.com/bs1125/cmaverse/blob/master/docs/articles/post_exposure_confounding.html Sets up a causal mediation analysis using the g-formula approach with specified outcome, exposure, mediator, and covariates. It configures regression models for outcome, mediator, and post-exposure covariates, and specifies estimation and inference methods. ```R res_gformula <- cmest(data = data, model = "gformula", outcome = "Y", exposure = "A", mediator = "M", basec = c("C1", "C2"), postc = "L", EMint = TRUE, mreg = list("logistic"), yreg = "logistic", postcreg = list("linear"), astar = 0, a = 1, mval = list(1), estimation = "imputation", inference = "bootstrap", nboot = 2) ``` -------------------------------- ### Multiple Mediator Mediation Analysis (rb model, bootstrap inference) Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/cmest.html Conducts mediation analysis with multiple mediators using the 'rb' model. This example uses bootstrap for inference and specifies different regression models for each mediator. Note: 10 boots are used for illustration; a higher number is recommended for actual analysis. ```R library(CMAverse) # multiple-mediator case with rb # 10 boots are used for illustration exp3 <- cmest(data = cma2020, model = "rb", outcome = "contY", exposure = "A", mediator = c("M1", "M2"), basec = c("C1", "C2"), EMint = TRUE, mreg = list("logistic", "multinomial"), yreg = "linear", astar = 0, a = 1, mval = list(0, "M2_0"), estimation = "imputation", inference = "bootstrap", nboot = 10, boot.ci.type = "bca") ``` -------------------------------- ### Simulate Data with Multiple Mediator Types Source: https://github.com/bs1125/cmaverse/blob/master/docs/articles/multiple_mediators.html Simulates a dataset with a continuous confounder (C1), a binary confounder (C2), a binary exposure (A), a count mediator (M1), a categorical mediator (M2), and a binary outcome (Y). This setup is useful for testing mediation analysis with diverse variable types. ```R set.seed(1) expit <- function(x) exp(x)/(1+exp(x)) n <- 10000 C1 <- rnorm(n, mean = 1, sd = 0.1) C2 <- rbinom(n, 1, 0.6) A <- rbinom(n, 1, expit(0.2 + 0.5*C1 + 0.1*C2)) M1 <- rpois(n, exp(1 - 2*A + 0.5*C1 + 0.8*C2)) linpred1 <- 0.1 + 0.1*A + 0.4*M1 - 0.5*C1 + 0.1*C2 linpred2 <- 0.4 + 0.2*A - 0.1*M1 - C1 + 0.5*C2 probm0 <- 1 / (1 + exp(linpred1) + exp(linpred2)) probm1 <- exp(linpred1) / (1 + exp(linpred1) + exp(linpred2)) probm2 <- exp(linpred2) / (1 + exp(linpred1) + exp(linpred2)) M2 <- factor(sapply(1:n, FUN = function(x) sample(c(0, 1, 2), size = 1, replace = TRUE, prob=c(probm0[x], probm1[x], probm2[x])))) Y <- rbinom(n, 1, expit(1 + 0.8*A - 1.8*M1 + 0.5*(M2 == 1) + 0.8*(M2 == 2) + 0.5*A*M1 - 0.4*A*(M2 == 1) - 1.4*A*(M2 == 2) + 0.3*C1 - 0.6*C2)) data <- data.frame(A, M1, M2, Y, C1, C2) ``` -------------------------------- ### Rcreg Example: Linear Regression with Measurement Error Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/rcreg.html Demonstrates simulating data for a linear regression model with a measurement error variable, fitting a naive model, and then applying the rcreg function for corrected analysis. Includes examples of extracting coefficients, variance-covariance matrix, residual standard deviation, formula, family, predictions, model frame, updating the model, and summary. ```R if (FALSE) { [rm](https://rdrr.io/r/base/rm.html)(list\]=[ls](https://rdrr.io/r/base/ls.html)()) [library](https://rdrr.io/r/base/library.html)([CMAverse](https://bs1125.github.io/CMAverse/)) # 2 boots are used for illustration # lm n <- 1000 x1 <- [rnorm](https://rdrr.io/r/stats/Normal.html)(n, mean = 5, sd = 3) x2_true <- [rnorm](https://rdrr.io/r/stats/Normal.html)(n, mean = 2, sd = 1) error1 <- [rnorm](https://rdrr.io/r/stats/Normal.html)(n, mean = 0, sd = 0.5) x2_error <- x2_true + error1 x3 <- [rbinom](https://rdrr.io/r/stats/Binomial.html)(n, size = 1, prob = 0.4) y <- 1 + 2 * x1 + 4 * x2_true + 2 * x3 + [rnorm](https://rdrr.io/r/stats/Normal.html)(n, mean = 0, sd = 2) data <- [data.frame](https://rdrr.io/r/base/data.frame.html)(x1 = x1, x2_true = x2_true, x2_error = x2_error, x3 = x3, y = y) reg_naive <- [lm](https://rdrr.io/r/stats/lm.html)(y ~ x1 + x2_error + x3, data = data) reg_true <- [lm](https://rdrr.io/r/stats/lm.html)(y ~ x1 + x2_true + x3, data = data) reg_rc <- rcreg(reg = reg_naive, data = data, MEvariable = "x2_error", MEerror = 0.5, variance = TRUE, nboot = 2) [coef](https://rdrr.io/r/stats/coef.html)(reg_rc) [vcov](https://rdrr.io/r/stats/vcov.html)(reg_rc) [sigma](https://rdrr.io/r/stats/sigma.html)(reg_rc) [formula](https://rdrr.io/r/stats/formula.html)(reg_rc) [family](https://rdrr.io/r/stats/family.html)(reg_rc) [predict](https://rdrr.io/r/stats/predict.html)(reg_rc, newdata = data[1, ]) reg_rc_model <- [model.frame](https://rdrr.io/r/stats/model.frame.html)(reg_rc) reg_rc_update <- [update](https://rdrr.io/r/stats/update.html)(reg_rc, data = data, weights = [rep](https://rdrr.io/r/base/rep.html)(1, n)) reg_rc_summ <- [summary](https://rdrr.io/r/base/summary.html)(reg_rc) #glm n <- 1000 x1 <- [rnorm](https://rdrr.io/r/stats/Normal.html)(n, mean = 0, sd = 1) x2_true <- [rnorm](https://rdrr.io/r/stats/Normal.html)(n, mean = 1, sd = 1) error1 <- [rnorm](https://rdrr.io/r/stats/Normal.html)(n, mean = 0, sd = 0.5) x2_error <- x2_true + error1 x3 <- [rbinom](https://rdrr.io/r/stats/Binomial.html)(n, size = 1, prob = 0.4) linearpred <- 1 + 0.3 * x1 - 0.5 * x2_true - 0.2 * x3 py <- [exp](https://rdrr.io/r/base/Log.html)(linearpred) / (1 + [exp](https://rdrr.io/r/base/Log.html)(linearpred)) y <- [rbinom](https://rdrr.io/r/stats/Binomial.html)(n, size = 1, prob = py) data <- [data.frame](https://rdrr.io/r/base/data.frame.html)(x1 = x1, x2_true = x2_true, x2_error = x2_error, x3 = x3, y = y) reg_naive <- [glm](https://rdrr.io/r/stats/glm.html)(y ~ x1 + x2_error + x3, data = data, family = [binomial](https://rdrr.io/r/stats/family.html)("logit")) reg_true <- [glm](https://rdrr.io/r/stats/glm.html)(y ~ x1 + x2_true + x3, data = data, family = [binomial](https://rdrr.io/r/stats/family.html)("logit")) reg_rc <- rcreg(reg = reg_naive, data = data, MEvariable = "x2_error", MEerror = 0.5, variance = TRUE, nboot = 2) } ``` -------------------------------- ### Update simexreg Model Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/simexreg.html Use update() to modify and refit a simexreg model. This function is useful for iterating on model specifications without starting from scratch. ```R update(simexreg) ``` -------------------------------- ### print.summary.svymultinom Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/svymultinom.html Prints the summary of a svymultinom object with specified precision. ```APIDOC ## print.summary.svymultinom ### Description Prints the summary of a svymultinom object nicely with specified digits. ### Usage ```r print(x, digits = 4, ...) ``` ### Arguments * `x` (summary.svymultinom): An object of class 'summary.svymultinom'. * `digits` (numeric): Minimal number of significant digits. See `print.default`. * `...`: Additional arguments. ``` -------------------------------- ### Summary of Initial Regression Model Source: https://github.com/bs1125/cmaverse/blob/master/docs/articles/multiple_imputation.html This output shows the summary of an initial regression model, likely before imputation, including coefficients, standard errors, and significance codes. ```R ## -3.2892 0.1105 0.1631 0.2451 0.5819 ## ## Coefficients: ## Estimate Std. Error z value Pr(>|z|) ## (Intercept) 0.1842 0.6422 0.287 0.774268 ## A 1.7317 0.1457 11.886 < 2e-16 *** ## C1 2.2255 0.6499 3.424 0.000617 *** ## C2 0.9759 0.1358 7.184 6.75e-13 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## (Dispersion parameter for binomial family taken to be 1) ## ## Null deviance: 2286.6 on 9999 degrees of freedom ## Residual deviance: 2052.2 on 9996 degrees of freedom ## AIC: 2060.2 ## ## Number of Fisher Scoring iterations: 7 ``` -------------------------------- ### Categorical Measurement Error Analysis Source: https://github.com/bs1125/cmaverse/blob/master/docs/articles/quickstart.html Performs a SIMEX regression for categorical variables with measurement error. This snippet shows the setup and results for a model with a single variable 'A' measured with error. ```R simexreg(reg = getCall(x$sens[[2L]]$reg.output$mreg[[1L]])$reg, formula = M2 ~ A + C1 + C2, data = getCall(x$sens[[2L]]$reg.output$mreg[[1L]])$data, MEvariable = "A", MEvartype = "categorical", MEerror = c(0.95, 0.05, 0.05, 0.95), variance = TRUE, lambda = c(0.5, 1, 1.5, 2), B = 5, weights = getCall(x$sens[[2L]]$reg.output$mreg[[1L]])$weights) ## Naive coefficient estimates: ## (Intercept) A C1 C2 ## 0.003446339 0.828694328 -0.984453987 0.892199087 ## ## Naive var-cov estimates: ## (Intercept) A C1 C2 ## (Intercept) 0.30025921 -0.07438249 -0.08381839 -0.17194314 ## A -0.07438249 0.20860406 -0.00266003 -0.01458923 ## C1 -0.08381839 -0.00266003 0.08577848 -0.01275232 ## C2 -0.17194314 -0.01458923 -0.01275232 0.26197192 ## ## Variable measured with error: ## A ## Measurement error: ## 0 1 ## 0 0.95 0.05 ## 1 0.05 0.95 ## ## Error-corrected results: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 0.1671 0.5520 0.303 0.7628 ## A1 0.6279 0.5216 1.204 0.2317 ## C1 -0.9362 0.2899 -3.230 0.0017 ** ## C2 0.7649 0.5134 1.490 0.1396 ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ``` ```R simexreg(reg = getCall(x$sens[[2L]]$reg.output$mreg[[2L]])$reg, formula = M2 ~ A + C1 + C2, data = getCall(x$sens[[2L]]$reg.output$mreg[[2L]])$data, MEvariable = "A", MEvartype = "categorical", MEerror = c(0.9, 0.1, 0.1, 0.9), variance = TRUE, lambda = c(0.5, 1, 1.5, 2), B = 5, weights = getCall(x$sens[[2L]]$reg.output$mreg[[2L]])$weights) ## Naive coefficient estimates: ## (Intercept) A C1 C2 ## 1.6402566 0.2901364 0.6208107 2.0833115 ## ## Naive var-cov estimates: ## (Intercept) A C1 C2 ## (Intercept) 0.06573913 -0.018924346 -0.0173562931 -0.0381103530 ## A -0.01892435 0.047073002 0.0032931500 -0.0062472874 ## C1 -0.01735629 0.003293150 0.0141472682 0.0003964488 ## C2 -0.03811035 -0.006247287 0.0003964488 0.0559647177 ## ## Variable measured with error: ## A ## Measurement error: ## 0 1 ## 0 0.9 0.1 ## 1 0.1 0.9 ## ## Error-corrected results: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 1.5985 0.2803 5.702 1.30e-07 *** ## A 0.3403 0.3206 1.062 0.291 ## C1 0.6341 0.1211 5.235 9.71e-07 *** ## C2 2.0890 0.2390 8.741 7.49e-14 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ``` -------------------------------- ### Summary Method for cmsens.me Objects Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/cmsens.html S3 method for summarizing sensitivity analysis results related to measurement error. ```R summary(object, ...) ``` -------------------------------- ### Summarize simexreg Results Nicely Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/simexreg.html Use summary() to obtain a detailed summary of the simexreg object. This includes coefficient estimates, standard errors, p-values, and other diagnostic statistics. ```R summary(simexreg) ``` -------------------------------- ### Causal Mediation Analysis with Error Correction Source: https://github.com/bs1125/cmaverse/blob/master/docs/articles/measurement_error.html Performs causal mediation analysis using the `cmest` function. This is useful for estimating direct and indirect effects when there might be measurement error. Ensure the `cmest` package is installed and loaded. ```R res_true <- cmest(data = data, model = "rb", outcome = "Y", exposure = "A", mediator = "M", basec = c("C1", "C2"), EMint = TRUE, mreg = list("logistic"), yreg = "logistic", astar = 0, a = 1, mval = list(1), estimation = "paramfunc", inference = "delta") ``` -------------------------------- ### Prepare Data for svymultinom Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/svymultinom.html Generates synthetic data with independent variables (x1, x2, x3), probabilities for three categories (py1, py2, py3), a categorical outcome variable (y), and survey weights (w). This data is then formatted into a data frame for use with svymultinom. ```R n <- 1000 x1 <- rnorm(n, mean = 0, sd = 1) x2 <- rnorm(n, mean = 1, sd = 1) x3 <- rbinom(n, size = 1, prob = 0.4) linearpred1 <- 1 + 0.3 * x1 - 0.5 * x2 - 0.2 * x3 linearpred2 <- 2 + 1 * x1 - 2 * x2 - 1 * x3 py2 <- exp(linearpred1) / (1 + exp(linearpred1) + exp(linearpred2)) py3 <- exp(linearpred2) / (1 + exp(linearpred1) + exp(linearpred2)) py1 <- 1 - py2 - py3 y <- sapply(1:n, function(x) sample(size = 1, c(1:3), prob = c(py1[x], py2[x], py3[x]))) w <- ifelse(x3 == 0, 0.4, 0.6) data <- data.frame(x1 = x1, x2 = x2, x3 = x3, y = y) ``` -------------------------------- ### Print Summary of simexreg Nicely Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/simexreg.html Use print(summary.simexreg) to display the summary of a simexreg object in a nicely formatted way. This provides a concise overview of the model's performance and results. ```R print(summary.simexreg) ``` -------------------------------- ### Simulate and Prepare Data for Multistate Model Source: https://github.com/bs1125/cmaverse/blob/master/docs/articles/multistate_model.html This R code simulates data and prepares it for a multistate model. It involves generating event times, merging data, and selecting relevant covariates. Ensure 'gen_srv' function and necessary variables (c1, c2, c3, c4, data_23, data) are defined. ```R for(i in 1:dim(data_23)[1]){ data_23_tem$id[i] = data_23$id[i] repeat { time_test = gen_srv(n = 1, lambda = 0.8, beta = c(as.numeric(c1), c2, as.numeric(c3), as.numeric(c4)), X = data_23[i, c("A.3", "M.3", "C1.3","C2.3")]) # exit if the condition is met if (time_test > data_23[i,"M.3"]) break } data_23_tem$new_y[i] = time_test } data_temp = merge(data, data_23_tem, by = "id", all = T) # modify M and S data_temp$S[which(data_temp$ind_M == 1)] = data_temp$new_y[which(data_temp$ind_M == 1)] data_temp$M[which(data_temp$ind_M == 0)] = data_temp$S[which(data_temp$ind_M == 0)] data_final = data_temp data_final$A = as.factor(data_final$A) sc_data = data_final %>% dplyr::select(id,A,M,S,ind_M,ind_S,C1,C2) # generate time to censoring and update event indicator time_to_censor = runif(n, 0, 2*max(sc_data$S)) sc_data$ind_S = ifelse(sc_data$S > time_to_censor, 0, 1) sc_data$A = factor(sc_data$A) sc_data$C1 = factor(sc_data$C1) ``` -------------------------------- ### cmdag() Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/index.html Generates a Directed Acyclic Graph (DAG) visualization. ```APIDOC ## cmdag() ### Description DAG Visualization ### Usage cmdag() ### Details This function is used for visualizing Directed Acyclic Graphs, likely in the context of causal inference. ### Value A visualization object. ``` -------------------------------- ### Direct Counterfactual Imputation Estimation with Bootstrap Inference Source: https://github.com/bs1125/cmaverse/blob/master/docs/articles/single_mediator.html Use this snippet to estimate direct counterfactuals with imputation and bootstrap inference. Ensure 'cmest' is installed and data is properly formatted. Set 'nboot' to a sufficiently large number for reliable inference. ```r res_gformula <- cmest(data = data, model = "gformula", outcome = "Y", exposure = "A", mediator = "M", basec = c("C1", "C2"), EMint = TRUE, mreg = list("logistic"), yreg = "logistic", astar = 0, a = 1, mval = list(1), estimation = "imputation", inference = "bootstrap", nboot = 2) summary(res_gformula) ``` -------------------------------- ### print.simexreg Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/simexreg.html Prints a summary of a simexreg object. ```APIDOC ## print.simexreg ### Description Prints a summary of a simexreg object. ### Method `print` (S3 method for simexreg) ### Parameters #### Arguments - **x** (simexreg) - An object of class `simexreg`. - **...** - Additional arguments. ``` -------------------------------- ### print.summary.simexreg Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/simexreg.html Prints a summary of the summary of a simexreg object. ```APIDOC ## print.summary.simexreg ### Description Prints a summary of the summary of a simexreg object. ### Method `print` (S3 method for summary.simexreg) ### Parameters #### Arguments - **x** (summary.simexreg) - An object of class `summary.simexreg`. - **digits** (integer) - Minimal number of significant digits. See `print.default`. - **...** - Additional arguments. ``` -------------------------------- ### Simulate Data with Post-exposure Confounding Source: https://github.com/bs1125/cmaverse/blob/master/docs/articles/post_exposure_confounding.html Simulates a dataset for mediation analysis with a continuous baseline confounder, a binary baseline confounder, a binary exposure, a continuous mediator-outcome confounder affected by the exposure, a binary mediator, and a binary outcome. This setup is crucial for testing methods that account for exposure-dependent confounders. ```R set.seed(1) expit <- function(x) exp(x)/(1+exp(x)) n <- 10000 C1 <- rnorm(n, mean = 1, sd = 0.1) C2 <- rbinom(n, 1, 0.6) A <- rbinom(n, 1, expit(0.2 + 0.5*C1 + 0.1*C2)) L <- rnorm(n, mean = 1 + A - C1 - 0.5*C2, sd = 0.5) M <- rbinom(n, 1, expit(1 + 2*A - L + 1.5*C1 + 0.8*C2)) Y <- rbinom(n, 1, expit(-3 - 0.4*A - 1.2*M + 0.5*A*M - 0.5*L + 0.3*C1 - 0.6*C2)) data <- data.frame(A, M, Y, C1, C2, L) ``` -------------------------------- ### Single-Mediator Analysis (Continuous Outcome) Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/cmest_rb.html Example of using cmest_rb for a single-mediator case with a continuous outcome, without exposure-mediator interaction. Requires specifying outcome, exposure, mediator, base covariates, regression models for outcome and mediator, estimation method, inference method, and specific values for exposure and mediator. ```R if (FALSE) { library(CMAverse) # single-mediator case without exposure-mediator interaction exp1 <- cmest_rb(data = cma2020, outcome = "contY", exposure = "A", mediator = "M1", basec = c("C1", "C2"), EMint = FALSE, yreg = "linear", mreg = list("logistic"), estimation = "paramfunc", inference = "delta", astar = 0, a = 1, mval = list(1)) summary(exp1) } ``` -------------------------------- ### Extract Model Information Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/svymultinom.html Demonstrates how to extract coefficients, variance-covariance matrix, formula, and model frame from a fitted svymultinom object. ```R coef(reg) vcov(reg) formula(reg) model.frame(reg) ``` -------------------------------- ### Simulate Data for Causal Analysis Source: https://github.com/bs1125/cmaverse/blob/master/docs/articles/quickstart.html Simulates a dataset with a binary exposure, two mediators (binary and continuous), a continuous outcome, and two baseline confounders. This data is used for demonstrating the package's functionalities. ```r 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) ``` -------------------------------- ### Multiple Mediator Mediation Analysis (ne model, bootstrap inference) Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/cmest.html Performs mediation analysis with multiple mediators using the 'ne' (natural effect) model. This example uses bootstrap for inference and explicitly defines the outcome regression model using a glm formula. Requires specifying outcome, exposure, mediators, base covariates, reference values for exposure and mediator, and estimation/inference methods. ```R library(CMAverse) # multiple-mediator case with ne exp4 <- cmest(data = cma2020, model = "ne", outcome = "contY", EMint = TRUE, exposure = "A", mediator = c("M1", "M2"), basec = c("C1", "C2"), yreg = glm(contY ~ A + M1 + M2 + A*M1 + A*M2 + C1 + C2, family = gaussian, data = cma2020), astar = 0, a = 1, mval = list(0, "M2_0"), estimation = "imputation", inference = "bootstrap", nboot = 10) ``` -------------------------------- ### Summarize svymultinom Model Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/svymultinom.html Provides a summary of the fitted svymultinom model, including coefficients, standard errors, and significance tests. ```R summary(reg) ``` -------------------------------- ### summary.svymultinom Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/svymultinom.html Generates a summary of the svymultinom object, providing a concise overview of the model results. ```APIDOC ## summary.svymultinom ### Description Summarizes the results of a svymultinom object nicely. ### Usage ```r summary(object, ...) ``` ### Arguments * `object` (svymultinom): An object of class 'svymultinom'. * `...`: Additional arguments. ### Value A summary object that can be printed using `print.summary.svymultinom`. ``` -------------------------------- ### Print Method for Summary of cmsens.me Objects Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/cmsens.html S3 method for printing the summary of measurement error sensitivity analysis results. ```R print(x, digits = 4, ...) ``` -------------------------------- ### Mediator Regression Analysis (Initial Dataset) Source: https://github.com/bs1125/cmaverse/blob/master/docs/articles/multiple_imputation.html This snippet shows the mediator regression model for the initial dataset before imputation. It provides a baseline for comparison. ```R ## Mediator regressions: ## Call: glm(formula = M ~ A + C1 + C2, family = binomial(), data = getCall(x$reg.output[[1L]]$mreg[[1L]])$data, weights = getCall(x$reg.output[[1L]]$mreg[[1L]])$weights) Deviance Residuals: Min 1Q Median 3Q Max -3.2820 0.1137 0.1633 0.2434 0.5946 Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) -0.03518 0.64316 -0.055 0.956376 A 1.66962 0.14384 11.607 < 2e-16 *** C1 2.48028 0.65225 3.803 0.000143 *** C2 0.94592 0.13553 6.980 2.96e-12 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 (Dispersion parameter for binomial family taken to be 1) Null deviance: 2030.4 on 9999 degrees of freedom Residual deviance: 1973.2 on 9994 degrees of freedom AIC: 1985.2 Number of Fisher Scoring iterations: 7 ``` -------------------------------- ### print.svymultinom Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/svymultinom.html Prints the results of a svymultinom object in a user-friendly format. ```APIDOC ## print.svymultinom ### Description Prints the results of a svymultinom object nicely. ### Usage ```r print(x, ...) ``` ### Arguments * `x` (svymultinom): An object of class 'svymultinom'. * `...`: Additional arguments. ### Methods * `print(summary.svymultinom)`: Print summary of `svymultinom` nicely. ``` -------------------------------- ### Extract Information from simexreg Object (Linear Model) Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/simexreg.html Demonstrates how to extract coefficients, variance-covariance matrix, standard deviation, formula, family, predictions, model frame, updated model, and summary from a simexreg object fitted to a linear model. ```R coef(reg_simex) vcov(reg_simex) sigma(reg_simex) formula(reg_simex) family(reg_simex) predict(reg_simex, newdata = data[1, ]) reg_simex_model <- model.frame(reg_simex) reg_simex_update <- update(reg_simex, data = data, weights = rep(1, n)) reg_simex_summ <- summary(reg_simex) ``` -------------------------------- ### Print simexreg Results Nicely Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/simexreg.html Use print() to display the results of a simexreg object in a nicely formatted way. This function provides a user-friendly output of the model's key information. ```R print(simexreg) ``` -------------------------------- ### Predict Probabilities with svymultinom Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/svymultinom.html Generates predicted probabilities for the first observation in the dataset using the fitted svymultinom model. ```R predict(reg, newdata = data[1, ], type = "probs") ``` -------------------------------- ### Accessing Bootstrap Summary Source: https://github.com/bs1125/cmaverse/blob/master/docs/articles/multistate_model.html Retrieves the summary of bootstrapped RD, SD, and TE estimates, including percentiles, from the cmest_multistate function output. ```r sc_data_result$bootstrap_summary ``` -------------------------------- ### Data Simulation for Multistate Model Source: https://github.com/bs1125/cmaverse/blob/master/docs/articles/multistate_model.html Simulates a dataset for a multistate model, including a binary exposure, time-to-event mediator and outcome, and baseline covariates. Assumes exponential time-to-event distributions. ```R # set up coefficients # transition 1 (A to M) a1 = -1.9 a2 = 0.2 a3 = 0.5 # transition 2 (A to S) b1 = 1 b3 = -0.5 b4 = 0.3 # transition 3 (M to S) c1 = 0.55 c2 = -0.15 c3 = -0.1 c4 = -0.2 set.seed(8) # build a function to generate time-to-event data gen_srv <- function(n, lambda, beta, X){ X = as.matrix(X) beta = as.matrix(beta, ncol=1) time = -log(runif(n)) / (lambda * exp(X %*% beta)) return(time) } n <- 2000 A = sample(c(0,1),replace=TRUE, size=n, c(0.5,0.5)) #binary exposure C1 = sample(c(0,1),replace=TRUE, size=n,c(0.6, 0.4)) #binary baseline covariate C2 = rnorm(n, mean = 1, sd = 1) #continuous baseline covariate id=c(1:n) full = data.frame(id,A,C1,C2) M = gen_srv(n=n, lambda = 1.5, beta = c(a1,a2,a3), X=data.frame(A,C1,C2)) #time to event mediator S = gen_srv(n=n, lambda = 1, beta = c(b1,b3,b4), X=data.frame(A,C1,C2)) #time to event outcome data = data.frame(id = c(1:n), M = M, S = S) # indicator for event data$ind_M = ifelse(data$M <= data$S, 1, 0) data$ind_S = 1 data <- merge(data,full , by = "id") # modify S distribution trans_matrix = transMat(x = list(c(2, 3), c(3), c()), names = c("A", "M", "S")) covs = c("A","M", "C1","C2") pre_data = msprep(time = c(NA, "M", "S"), status = c(NA, "ind_M", "ind_S"), data = data, trans = trans_matrix, keep = covs) pre_data = expand.covs(pre_data, covs, append = TRUE, longnames = FALSE) # resample for T < S data_23 = pre_data[which(pre_data$trans == 3),] data_23_tem = data.frame(id = rep(NA,dim(data_23)[1]), new_y = rep(NA,dim(data_23)[1])) paste("# to resample is ", nrow(data_23)) ``` -------------------------------- ### Run SIMEX for Continuous Measurement Error Source: https://github.com/bs1125/cmaverse/blob/master/docs/articles/measurement_error.html Initiates a sensitivity analysis for a continuous variable measured with error using the SIMEX method. Requires a pre-existing naive model result (`res_naive_cont`), the name of the variable with error (`MEvariable`), its type (`MEvartype`), and the estimated measurement error (`MEerror`). ```R res_simex_cont <- cmsens(object = res_naive_cont, sens = "me", MEmethod = "simex", MEvariable = "C1_error", MEvartype = "con", MEerror = 0.05) ``` -------------------------------- ### Plot DAG with cmdag (With Base and Post-Exposure Confounders) Source: https://github.com/bs1125/cmaverse/blob/master/docs/reference/cmdag.html Use this snippet to visualize a DAG that includes both base and post-exposure confounders. Node and text colors are customizable. ```R cmdag(outcome = "Y", exposure = "A", mediator = c("M1", "M2"), basec = c("C1", "C2", "C3"), postc = c("L1", "L2"), node = FALSE, text_col = "black") ```