First, we will install from the .tar file and load the package.

library(boostmed)

There are two data frames in the file for testing the functions. Let’s start with the sensitivity analysis.

data("AR_data")
head(AR_data)

The sensitivity analysis is done by the function causal_med_sen. Please see the help file for more details. It outputs three things:

  1. The average causal mediation effect (ACME) under assumption of no confounding.
  2. The critial value of ACME when it’s statistically nullified.
  3. The estimation of ACME under the critical bias.
causal_med_sen(AR_data,
               x = "ar_condition",
               m = "fit_uncertainty",
               y = "return")
## $acme_no_bias
## acme_no_bias    lb 95% CI    ub 95% CI 
##   -1.1474140   -2.0204030   -0.4561891 
## 
## $critical_rho
## [1] 0.247
## 
## $acme_critical_bias
## acme_critical_bias          lb 95% CI          ub 95% CI 
##       -0.555995543       -1.248215142        0.001679047

Next, let’s try the constructed IV estimation. The function to use is causal_med_civ. We will use it with a simulated data.

data("civ_example_data")
head(civ_example_data)

The constructed IV estimation is done by the function causal_med_civ. The flow of the function is like this:

  1. Check if the constructed IV is strong using the residual from the \(M \sim D\) regression with the Breusch-Pagn test of heterskedasticity. Stop the analysis if the IV is weak.
  2. If the IV is strong, run the constructed IV analysis and report and corrected mediation estimation.
  3. Check the exogeneity of the constructed IV by testing the heteroskedasticity of the residual of \(Y \sim D + \hat{M}\), where \(\hat{M}\) is the predicted value of the mediator from the first stage regression. Note that this test is overpowered. The rejection of the null implies the IV might not be exogenous. If so, the function outputs a warning.
causal_med_civ(civ_example_data,
               x = "D",
               m = "M",
               y = "Y")
## $strength_of_constructed_IV
## Chi-square Stats          P-value 
##         432.7623           0.0000 
## 
## $exogeneity_of_constructed_IV
## Chi-square Stats          P-value 
##       0.03431541       0.98298865 
## 
## $acme_IV
## acme_constructed_IV           lb 95% CI           ub 95% CI 
##           0.5271170           0.4604242           0.5972552

The results show that the constructed IV is strong and also we cannot reject the null of homoskedasticity which implies the exogneity of the IV. The final output shows the estimated ACME and bootstrapped 95% CI.

If we try this function on the AR_data, we get an error message as the IV is too weak. Using a weak instrument may not correct the bias but increase the variance of estimation.

causal_med_civ(AR_data,
               x = "ar_condition",
               m = "fit_uncertainty",
               y = "return")
## [1] "Breusch-Pagan Test for M~D residuals:"
## Chi-square Stats          P-value 
##        0.6439754        0.4222749
## Error in causal_med_civ(AR_data, x = "ar_condition", m = "fit_uncertainty", : The constructed instrument is too weak!