Skip to content

Commit

Permalink
update mediation function
Browse files Browse the repository at this point in the history
  • Loading branch information
mikenguyen13 committed Jan 23, 2024
1 parent 029c266 commit 7516a30
Show file tree
Hide file tree
Showing 27 changed files with 350 additions and 15 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ Imports:
did (>= 2.1.2),
synthdid (>= 0.0.9),
plm (>= 2.6.3),
MASS,
foreach (>= 1.5.2)
VignetteBuilder: knitr
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export(ama_theme)
export(balance_assessment)
export(balance_scatter_custom)
export(get_balanced_panel)
export(med_ind)
export(nice_tab)
export(panel_estimate)
export(plot_PanelEstimate)
Expand Down
54 changes: 54 additions & 0 deletions R/med_ind.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#' Estimate Mediation Indirect Effects
#'
#' @description
#' `med_ind` estimates the indirect effects of an independent variable on a dependent variable
#' through a mediator using Monte Carlo simulations (Selig & Preacher, 2008). It calculates the distribution of the product
#' of path coefficients (a*b) and provides confidence intervals for the indirect effect, along
#' with a ggplot histogram for visualization.
#'
#' @references
#' Selig, J. P., & Preacher, K. J. (2008, June). Monte Carlo method for assessing mediation:
#' An interactive tool for creating confidence intervals for indirect effects [Computer software].
#' Available from http://quantpsy.org/.
#'
#' @param a The regression coefficient for the effect of the independent (causal) variable on the mediator.
#' @param b The regression coefficient for the effect of the mediator on the dependent (outcome) variable.
#' @param var_a The variance of the coefficient a.
#' @param var_b The variance of the coefficient b.
#' @param cov_ab The covariance between coefficients a and b.
#' @param ci The confidence interval width for the indirect effect (default is 95 for a 95% CI).
#' @param iterations The number of iterations for the Monte Carlo simulation (default is 20000).
#' @param seed The seed for random number generation to ensure reproducibility (default is 1).
#' @param theme Custom theme that follows ggplots2 (default is AMA style)
#' @return A list containing the lower quantile, upper quantile, raw simulation data, and histogram plot of the indirect effects.
#' @examples
#' \dontrun{
#' result <- med_ind(a = 0.5, b = 0.7, var_a = 0.04, var_b = 0.05, cov_ab = 0.01)
#' result$lower_quantile
#' result$upper_quantile
#' result$plot
#' }
#' @export
med_ind <- function(a, b, var_a, var_b, cov_ab, ci = 95, iterations = 20000, seed = 1, theme = causalverse::ama_theme()) {
set.seed(seed)

acov_matrix <- matrix(c(var_a, cov_ab, cov_ab, var_b), 2, 2)

sim_data <- MASS::mvrnorm(iterations, mu = c(a, b), Sigma = acov_matrix)
ab_values <- sim_data[, 1] * sim_data[, 2]

lower_q <- quantile(ab_values, (1 - ci/100)/2)
upper_q <- quantile(ab_values, 1 - (1 - ci/100)/2)

plot <- ggplot(data.frame(ab_values), aes(x = ab_values)) +
geom_histogram(
fill = "skyblue", color = "black"
) +
labs(x = "Indirect Effect", y = "Frequency", title = "Distribution of Simulated Indirect Effects") +
geom_vline(xintercept = lower_q, color = "red", linetype = "dashed") +
geom_vline(xintercept = upper_q, color = "red", linetype = "dashed") +
geom_vline(xintercept = 0, color = "black", linetype = "solid") +
theme

list(lower_quantile = lower_q, upper_quantile = upper_q, raw_data = ab_values, plot = plot)
}
2 changes: 1 addition & 1 deletion R/synthdid_est.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ synthdid_est <-

# Remove units with missing data in a time period
if (any(is.na(data))) {
data <- as.data.table(data) %>%
data <- data.table::as.data.table(data) %>%
.[, nas := min(get(outcome_var), na.rm = TRUE), by = !!unit_id_var] %>%
na.omit() %>%
.[, nas := NULL] %>%
Expand Down
2 changes: 1 addition & 1 deletion R/synthdid_est_ate.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ synthdid_est_ate <-

# Process each adoption cohort
for (adoption_cohort in adoption_cohorts) {
cat("adoption_cohort:",adoption_cohort,"\n" )
cat("Adoption Cohort:",adoption_cohort,"\n" )

# Prepare balanced panel data for current adoption cohort
# Specifically, create balanced panel with lags and leads for all treated
Expand Down
Binary file modified causalverse_0.0.0.9000.pdf
Binary file not shown.
12 changes: 11 additions & 1 deletion docs/articles/a_introduction.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 5 additions & 7 deletions docs/articles/b_synthdid.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/articles/c_did.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ articles:
a_introduction: a_introduction.html
b_synthdid: b_synthdid.html
c_did: c_did.html
last_built: 2024-01-12T21:00Z
last_built: 2024-01-23T01:01Z

4 changes: 4 additions & 0 deletions docs/reference/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7516a30

Please sign in to comment.