p1 <- confounder_data |>ggplot(aes(x, y)) +geom_point(alpha = .2) +geom_smooth(method ="lm", se =FALSE, color ="black") +facet_wrap(~"not adjusting for `q`\n(biased)")p2 <- confounder_data |>ggplot(aes(x, y, color = q)) +geom_point(alpha = .2) +geom_smooth(method ="lm", se =FALSE) +facet_wrap(~"adjusting for `q`\n(unbiased)")p1 + p2
Corr
lares::corr_cross(confounder_data)
correlation(confounder_data)
# Correlation Matrix (pearson-method)
Parameter1 | Parameter2 | r | 95% CI | t(998) | p
---------------------------------------------------------------------
x | y | -0.54 | [-0.58, -0.49] | -20.26 | < .001***
p-value adjustment method: Holm (1979)
Observations: 1000
Model
# Not Adjust## y ~ xconfounder_data.fit.y_x <-lm(y ~ x, data = confounder_data)## y ~ q confounder_data.fit.y_q <-lm(y ~ q, data = confounder_data)## x ~ qconfounder_data.fit.x_q <-lm(x ~ q, data = confounder_data)# Adjusted: y ~ x + q confounder_data.fit.y_xq <-lm(y ~ x + q, data = confounder_data)
set.seed(123)x <-rnorm(n)linear_pred <-2* x +rnorm(n)prob <-1/ (1+exp(-linear_pred))q <-rbinom(n, size =1, prob = prob)y <-2* q +rnorm(n)mediator_data <-tibble(x, y, q =as.factor(q))
Plot
Code
p1 <- mediator_data |>ggplot(aes(x, y)) +geom_point(alpha = .2) +geom_smooth(method ="lm", se =FALSE, color ="black") +facet_wrap(~"not adjusting for `q`\n(total effect)")p2 <- mediator_data |>ggplot(aes(x, y, color = q)) +geom_point(alpha = .2) +geom_smooth(method ="lm", se =FALSE) +facet_wrap(~"adjusting for `q`\n(direct effect)")p1 + p2
Corr
lares::corr_cross(mediator_data)
correlation(mediator_data)
# Correlation Matrix (pearson-method)
Parameter1 | Parameter2 | r | 95% CI | t(998) | p
------------------------------------------------------------------
x | y | 0.41 | [0.35, 0.46] | 14.04 | < .001***
p-value adjustment method: Holm (1979)
Observations: 1000
Model
# Not Adjust## y ~ xmediator_data.fit.y_x <-lm(y ~ x, data = mediator_data)## y ~ q mediator_data.fit.y_q <-lm(y ~ q, data = mediator_data)## x ~ qmediator_data.fit.x_q <-lm(x ~ q, data = mediator_data)# Adjusted: y ~ x + q mediator_data.fit.y_xq <-lm(y ~ x + q, data = mediator_data)
set.seed(1)x <-rnorm(n)y <-rnorm(n)linear_pred <-2* x +3* y +rnorm(n)prob <-1/ (1+exp(-linear_pred))q <-rbinom(n, size =1, prob = prob)collider_data <-tibble(x, y, q =as.factor(q))
Plot
Code
p1 <- collider_data |>ggplot(aes(x, y)) +geom_point(alpha = .2) +geom_smooth(method ="lm", se =FALSE, color ="black") +facet_wrap(~"not adjusting for `q`\n(unbiased)")p2 <- collider_data |>ggplot(aes(x, y, color = q)) +geom_point(alpha = .2) +geom_smooth(method ="lm", se =FALSE) +facet_wrap(~"adjusting for `q`\n(biased)")p1 + p2
Corr
lares::corr_cross(collider_data)
correlation(collider_data)
# Correlation Matrix (pearson-method)
Parameter1 | Parameter2 | r | 95% CI | t(998) | p
-------------------------------------------------------------------
x | y | 6.40e-03 | [-0.06, 0.07] | 0.20 | 0.840
p-value adjustment method: Holm (1979)
Observations: 1000
Model
# Not Adjust## y ~ xcollider_data.fit.y_x <-lm(y ~ x, data = collider_data)## y ~ q collider_data.fit.y_q <-lm(y ~ q, data = collider_data)## x ~ qcollider_data.fit.x_q <-lm(x ~ q, data = collider_data)# Bias: y ~ x + q collider_data.fit.y_xq <-lm(y ~ x + q, data = collider_data)