### Install medRCT from GitHub Source: https://github.com/t0ngchen/medrct/blob/master/README.md Use the remotes package to install the latest stable version of medRCT from the GitHub repository. ```r remotes::install_github("T0ngChen/medRCT") ``` -------------------------------- ### Initialize medRCT Analysis Source: https://github.com/t0ngchen/medrct/blob/master/README.md Load the package and set a random seed to ensure reproducibility of the simulation results. ```r # Load the medRCT package library(medRCT) # Set a seed for reproducibility set.seed(2025) ``` -------------------------------- ### Configure Bootstrap Parameters Source: https://context7.com/t0ngchen/medrct/llms.txt Adjust bootstrap resampling settings or disable bootstrapping to obtain point estimates only. ```r # Custom bootstrap configuration with percentile CIs med_boot_perc <- medRCT( dat = LSACdata, exposure = "sep", outcome = "child_mh", mediators = c("parent_mh", "preschool_att"), intermediate_confs = "fam_stress", confounders = c("child_sex", "child_atsi", "mat_cob", "mat_engl", "mat_age"), intervention_type = "shift_k", effect_measure = "RD", bootstrap = TRUE, boot_args = list( R = 500, # Number of bootstrap replicates stype = "i", # Bootstrap statistic type ci.type = "perc" # Percentile confidence intervals ), mcsim = 200 ) # Run without bootstrapping for point estimates only med_no_boot <- medRCT( dat = LSACdata, exposure = "sep", outcome = "child_mh", mediators = c("parent_mh", "preschool_att"), intermediate_confs = "fam_stress", confounders = c("child_sex", "child_atsi", "mat_cob", "mat_engl", "mat_age"), intervention_type = "shift_k", effect_measure = "RD", bootstrap = FALSE, # No bootstrapping - point estimates only mcsim = 200 ) # View point estimates only med_no_boot$est ``` -------------------------------- ### Load and Explore LSAC Dataset Source: https://context7.com/t0ngchen/medrct/llms.txt Load the included simulated dataset and inspect its structure. ```r # Load and explore the included dataset data(LSACdata) # Dataset structure str(LSACdata) #> 'data.frame': 5107 obs. of 11 variables: #> $ child_sex : num 0 NA NA NA 1 1 ... #> $ child_atsi : num 1 0 0 0 0 0 ... #> $ mat_cob : num 0 0 0 0 0 0 ... #> $ mat_engl : num 0 0 0 0 0 0 ... #> $ mat_age : num 1 NA NA NA 1 1 ... #> $ sep : num 0 0 0 0 1 0 ... # Exposure: socioeconomic position #> $ fam_stress : num 0 NA NA NA 0 1 ... # Intermediate confounder #> $ parent_mh : num 0 NA NA NA 0 1 ... # Mediator 1: parental mental health #> $ preschool_att : num 1 0 0 0 0 0 ... # Mediator 2: preschool attendance #> $ child_mh : num 0 0 1 0 1 1 ... # Binary outcome: child mental health #> $ child_SDQscore: num 8.92 7.35 12.82 ... # Continuous outcome: SDQ score ``` -------------------------------- ### Launch Interactive Model Assessment Source: https://context7.com/t0ngchen/medrct/llms.txt Use the Shiny application to validate regression models and configure parameters before running the full analysis. ```r # Launch Shiny app for model assessment if (interactive()) { medRCT_shiny(data = LSACdata) } ``` -------------------------------- ### Inspect Dataset and Define Confounders Source: https://github.com/t0ngchen/medrct/blob/master/README.md View the initial rows of the dataset and define the vector of confounders to be used in the analysis. ```R head(LSACdata) # Define confounders for the analysis confounders <- c("child_sex", "child_atsi", "mat_cob", "mat_engl", "mat_age") ``` -------------------------------- ### Simple Mediation Analysis Without Intermediate Confounders Source: https://context7.com/t0ngchen/medrct/llms.txt Performs mediation analysis when there are no exposure-induced mediator-outcome confounders. Set 'intermediate_confs' to NULL. Requires specifying data, exposure, outcome, mediators, confounders, and intervention type. ```r med_simple <- medRCT( dat = LSACdata, exposure = "sep", outcome = "child_mh", mediators = c("parent_mh", "preschool_att"), intermediate_confs = NULL, # No intermediate confounders confounders = c("child_sex", "child_atsi", "mat_cob", "mat_engl", "mat_age"), intervention_type = "all", effect_measure = "RD", bootstrap = TRUE, boot_args = list(R = 100, stype = "i", ci.type = "norm"), mcsim = 200 ) ``` ```r summary(med_simple) ``` -------------------------------- ### Analyze Continuous Outcome with Shift-k Intervention Source: https://context7.com/t0ngchen/medrct/llms.txt Analyzes a continuous outcome using the difference in means as the effect measure, with a 'shift_k' intervention type. Requires specifying data, exposure, outcome, mediators, intermediate confounders, confounders, and effect measure. ```r med_continuous <- medRCT( dat = LSACdata, exposure = "sep", outcome = "child_SDQscore", # Continuous outcome variable mediators = c("parent_mh", "preschool_att"), intermediate_confs = "fam_stress", confounders = c("child_sex", "child_atsi", "mat_cob", "mat_engl", "mat_age"), intervention_type = "shift_k", effect_measure = "Diff", # Difference in means for continuous outcome bootstrap = TRUE, boot_args = list(R = 100, stype = "i", ci.type = "norm"), mcsim = 200 ) ``` ```r summary(med_continuous) ``` -------------------------------- ### Customize Exposure-Confounder Interactions Source: https://context7.com/t0ngchen/medrct/llms.txt Control the inclusion of exposure-confounder interactions in regression models using the interactions_XC argument. ```r # No exposure-confounder interactions med_no_int <- medRCT( dat = LSACdata, exposure = "sep", outcome = "child_mh", mediators = c("parent_mh", "preschool_att"), intermediate_confs = "fam_stress", confounders = c("child_sex", "child_atsi", "mat_cob", "mat_engl", "mat_age"), interactions_XC = "none", # Remove all exposure-confounder interactions intervention_type = "shift_k", effect_measure = "RD", bootstrap = TRUE, mcsim = 200 ) # Custom interaction specification med_custom_int <- medRCT( dat = LSACdata, exposure = "sep", outcome = "child_mh", mediators = c("parent_mh", "preschool_att"), intermediate_confs = "fam_stress", confounders = c("child_sex", "child_atsi", "mat_cob", "mat_engl", "mat_age"), interactions_XC = "sep:child_sex + sep:mat_age", # Specific interactions only intervention_type = "shift_k", effect_measure = "RD", bootstrap = TRUE, mcsim = 200 ) ``` -------------------------------- ### Perform Single Mediator Analysis Source: https://context7.com/t0ngchen/medrct/llms.txt Execute a mediation analysis with a single mediator using the shift_k intervention type. ```r med_single <- medRCT( dat = LSACdata, exposure = "sep", outcome = "child_mh", mediators = "parent_mh", # Single mediator intermediate_confs = "fam_stress", confounders = c("child_sex", "child_atsi", "mat_cob", "mat_engl", "mat_age"), intervention_type = "shift_k", # Only option for single mediator effect_measure = "RD", bootstrap = TRUE, boot_args = list(R = 100, stype = "i", ci.type = "norm"), mcsim = 200 ) #> Only able to estimate the effect type 'shift_k' with a single mediator. summary(med_single) ``` -------------------------------- ### Load Data and Estimate Interventional Effects with medRCT Source: https://context7.com/t0ngchen/medrct/llms.txt Loads the LSACdata dataset and estimates various interventional mediation effects using the medRCT function. Requires specifying confounders, mediators, and intervention types. Bootstrapping is enabled for confidence intervals. ```r library(medRCT) # Load included simulated dataset based on Longitudinal Study of Australian Children data(LSACdata) head(LSACdata) #> child_sex child_atsi mat_cob mat_engl mat_age sep fam_stress parent_mh #> 1 0 1 0 0 1 0 0 0 #> 2 NA 0 0 0 NA 0 NA NA #> 3 NA 0 0 0 NA 0 NA NA #> 4 NA 0 0 0 NA 0 NA NA #> 5 1 0 0 0 1 1 0 0 #> 6 1 0 0 0 1 0 1 1 #> preschool_att child_mh child_SDQscore #> 1 1 0 8.924660 #> 2 0 0 7.349826 #> 3 0 1 12.824643 #> 4 0 0 6.611369 #> 5 0 1 10.329341 #> 6 0 1 13.552515 # Define baseline confounders confounders <- c("child_sex", "child_atsi", "mat_cob", "mat_engl", "mat_age") # Define intermediate (exposure-induced) confounders intermediate_confs <- "fam_stress" # Set seed for reproducibility set.seed(2025) # Estimate all types of interventional effects med_res <- medRCT( dat = LSACdata, exposure = "sep", # Binary exposure: socioeconomic position outcome = "child_mh", # Binary outcome: child mental health problems mediators = c("parent_mh", "preschool_att"), # Two mediators of interest intermediate_confs = intermediate_confs, # Exposure-induced confounder confounders = confounders, # Baseline confounders interactions_XC = "all", # Include all exposure-confounder interactions intervention_type = "all", # Estimate all three effect types effect_measure = "RD", # Risk difference for binary outcome bootstrap = TRUE, # Enable bootstrapping for CIs boot_args = list(R = 200, stype = "i", ci.type = "norm"), mcsim = 200 # Monte Carlo simulations ) #> Assumed causal order for estimating effect of type 'shift_k_order': parent_mh, preschool_att #> Conducting complete case analysis, 2499 observations were excluded due to missing data. ``` -------------------------------- ### Estimate Interventional Effects Source: https://github.com/t0ngchen/medrct/blob/master/README.md Run the medRCT function to estimate interventional effects. Note that incomplete records are removed by default and a higher number of Monte Carlo simulations is recommended for production. ```R med_res <- medRCT( dat = LSACdata, exposure = "sep", outcome = "child_mh", mediators = c("parent_mh", "preschool_att"), intermediate_confs = "fam_stress", # intermediate confounders confounders = confounders, bootstrap = TRUE, intervention_type = "shift_k", mcsim = 50 ) ``` -------------------------------- ### Estimate Shift-k Intervention Effects Source: https://context7.com/t0ngchen/medrct/llms.txt Estimates interventional effects for shifting individual mediator distributions without considering flow-on effects to other mediators. This is faster for computation. Requires specifying data, exposure, outcome, mediators, intermediate confounders, confounders, and intervention type. ```r med_shift_k <- medRCT( dat = LSACdata, exposure = "sep", outcome = "child_mh", mediators = c("parent_mh", "preschool_att"), intermediate_confs = "fam_stress", confounders = c("child_sex", "child_atsi", "mat_cob", "mat_engl", "mat_age"), intervention_type = "shift_k", # Only estimate shift_k effects effect_measure = "RD", bootstrap = TRUE, boot_args = list(R = 100, stype = "i", ci.type = "norm"), mcsim = 200 ) ``` ```r summary(med_shift_k) ``` -------------------------------- ### Calculate Risk Ratio Effect Measure Source: https://context7.com/t0ngchen/medrct/llms.txt Calculates the risk ratio as the effect measure for binary outcomes. This is an alternative to risk difference. Requires specifying data, exposure, outcome, mediators, intermediate confounders, confounders, and effect measure. ```r med_rr <- medRCT( dat = LSACdata, exposure = "sep", outcome = "child_mh", mediators = c("parent_mh", "preschool_att"), intermediate_confs = "fam_stress", confounders = c("child_sex", "child_atsi", "mat_cob", "mat_engl", "mat_age"), intervention_type = "shift_k", effect_measure = "RR", # Risk ratio for binary outcome bootstrap = TRUE, boot_args = list(R = 100, stype = "i", ci.type = "norm"), mcsim = 200 ) ``` ```r summary(med_rr) ``` -------------------------------- ### View Mediation Analysis Results Source: https://context7.com/t0ngchen/medrct/llms.txt Displays a summary of the mediation analysis results, including estimated indirect effects, direct effects, and expected outcomes in different trial arms. This is typically used after running a mediation analysis model. ```r summary(med_res) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.