strengejacke / sjstats

Effect size measures and significance tests
https://strengejacke.github.io/sjstats
189 stars 21 forks source link

Specifying response variable in mediation() #80

Closed jmgirard closed 6 months ago

jmgirard commented 5 years ago

I am using the zero_inflated_beta family in brms to assess mediation on two response variables: the beta component of y and the degenerate or zero-inflation component called zi. The mediation() function works great to test the mediation involving the beta component y but I can't figure out how to ask it to use the degenerate component zi as the response variable. Is this possible, or could that functionality be added?

Here is a small toy example:

library(brms)
library(sjstats)

y <- c(rbeta(n = 1000, shape1 = 3, shape2 = 6), rep(0, 200))
x <- 0.5 * y + sqrt(1 - .25) * rnorm(1200)
m <- 0.7 * x + sqrt(1 - .49) * rnorm(1200)
df <- data.frame(x, m, y)

f1 <- bf(
  y ~ x + m,
  zi ~ x + m,
  family = zero_inflated_beta
)
f2 <- bf(formula = m ~ x)
fit <- brm(
  formula = f1 + f2 + set_rescor(FALSE), 
  data = df,
  cores = 4
)

mediation(fit)
#> Causal Mediation Analysis for Stan Model
#> 
#>  Treatment: x
#>   Mediator: m
#>   Response: y
#>
#>                 Estimate    HDI (90%)
#>  Direct effect:     0.07 [ 0.02 0.12]
#>  Indirect effect:     0.00 [-0.03 0.04]
#>  Total effect:     0.08 [ 0.04 0.12]
#>
#>  Proportion mediated: 5.26% [-46.41% 56.94%]

Update: I was able to figure out how to do it using brms::posterior_samples() but I still think it would be good to add a response argument to the mediation() function.

strengejacke commented 4 years ago

mediation() is now re-implemented in our dedicated Bayes-package, bayestestR. You could install the GitHub repo from the PR, if you like to test. I'll also try out your example, the new mediation() functions has a response-argument now.