-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_confounding.qmd
224 lines (167 loc) · 6.42 KB
/
find_confounding.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
---
title: "Identifying Confounders"
format:
html:
code-fold: true
code-summary: 'Show The Code'
---
One of the many subtasks when performing a prediction scheme is adjusting for mediating and confounding variables. But what is a mediator, what is a confounder, and why include thhem? Counfounder and Mediators can be illustrated using a causal diagram (From [this Wikipedia page](https://en.wikipedia.org/wiki/Confounding)):
![](https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Comparison_confounder_mediator.svg/2560px-Comparison_confounder_mediator.svg.png)
As you can see, a confounding variable is something that confounds or 'confuses' the relationship between an exposure and an outcome. The relationship between an exposure X and an outcome Y is influenced by a confounding variable Z. A mediating variable connects an exposure to an outcome where otherwise would not be. The relationship between an exposure X and an outcome Y is influenced by a mediating variable Z.
Here, I will show how MCCV can be used to identify confounders and mediators.
::: {.panel-tabset}
# Confounding
**Generate dataset with an effect X, outcome Y, and a confounder Z variables**
The random variable Z generates X and Y. Any correlation between X and Y is therefore spurious
```{python}
import numpy as np
N=100
Z1 = np.random.normal(loc=0,scale=1,size=N)
Z2 = np.random.normal(loc=1,scale=1,size=N)
Z = np.concatenate([Z1,Z2])
import scipy as sc
Y = np.concatenate([sc.stats.bernoulli.rvs(z,size=1) for z in (Z - min(Z)) / (max(Z) - min(Z))])
X = Z + np.random.normal(loc=0,scale=1,size=len(Z))
```
```{r}
df <- tibble::tibble(
X = reticulate::py$X,
Y = reticulate::py$Y,
Z = reticulate::py$Z
)
df[['Y']] <- factor(df$Y,levels=c(0,1))
df
GGally::ggpairs(df)
```
**Employ MCCV: Predict Y from X, Y from Z, Y from X and Z**
```{python}
import pandas as pd
df = pd.DataFrame(data={'X' : X,'Y' : Y,'Z' : Z})
df.index.name = 'pt'
X = df.loc[:,['X']]
Y = df.loc[:,['Y']]
Z = df.loc[:,['Z']]
XZ = df.loc[:,['X','Z']]
```
```{python}
import mccv
mccv_YXobj = mccv.mccv(num_bootstraps=200,n_jobs=2)
mccv_YXobj.set_X(X)
mccv_YXobj.set_Y(Y)
mccv_YXobj.run_mccv()
mccv_YZobj = mccv.mccv(num_bootstraps=200,n_jobs=2)
mccv_YZobj.set_X(Z)
mccv_YZobj.set_Y(Y)
mccv_YZobj.run_mccv()
mccv_YXZobj = mccv.mccv(num_bootstraps=200,n_jobs=2)
mccv_YXZobj.set_X(XZ)
mccv_YXZobj.set_Y(Y)
mccv_YXZobj.run_mccv()
```
```{python}
f_imp_dfs = dict()
f_imp_dfs['YXobj'] = \
mccv_YXobj.mccv_data['Feature Importance']
f_imp_dfs['YZobj'] = \
mccv_YZobj.mccv_data['Feature Importance']
f_imp_dfs['YXZobj'] = \
mccv_YXZobj.mccv_data['Feature Importance']
```
**Visualize feature importances**
```{r}
library(ggplot2)
f_imp_plot <- function(x,title){
ggplot(x,aes(feature,importance,color=feature)) +
geom_boxplot(alpha=0) +
geom_point(position=position_jitter(width=0.2)) +
scale_color_manual(values=c("X" = "indianred",
"Y" = "skyblue",
"Z" = "black",
"Intercept" = "gray")) +
theme_bw() +
labs(title=title)
}
library(patchwork)
(f_imp_plot( reticulate::py$f_imp_dfs$YXobj,"Y ~ X" ) +
f_imp_plot( reticulate::py$f_imp_dfs$YZobj,"Y ~ Z" ) ) /
f_imp_plot( reticulate::py$f_imp_dfs$YXZobj,"Y ~ X + Z" )
```
In this example, Z is confounding the relationship between X and Y. The 'Y \~ X' model shows that X is very important in predicting Y. However, when Z is included in the model 'Y \~ X + Z', you see that Z remains very important but X is no longer important for predicting Y. This toy example had Z causing Y and Z causing X, and any relationship between X and Y was spurious. Therefore, including X and Z as predictors showed only the importance of Z in predicting Y.
# Mediation
**Generate dataset with an effect X, outcome Y, and a mediator Z variables**
The random variable X generates Z which generates and Y. Any correlation between X and Y is therefore spurious
```{python}
import numpy as np
N=100
X1 = np.random.normal(loc=0,scale=1,size=N)
X2 = np.random.normal(loc=1,scale=1,size=N)
X = np.concatenate([X1,X2])
import scipy as sc
Z = X + np.random.normal(loc=0,scale=1,size=len(X))
Y = np.concatenate([sc.stats.bernoulli.rvs(z,size=1) for z in (Z - min(Z)) / (max(Z) - min(Z))])
```
```{r}
df <- tibble::tibble(
X = reticulate::py$X,
Y = reticulate::py$Y,
Z = reticulate::py$Z
)
df[['Y']] <- factor(df$Y,levels=c(0,1))
df
GGally::ggpairs(df)
```
**Employ MCCV: Predict Y from X, Y from Z, Y from X and Z**
```{python}
import pandas as pd
df = pd.DataFrame(data={'X' : X,'Y' : Y,'Z' : Z})
df.index.name = 'pt'
X = df.loc[:,['X']]
Y = df.loc[:,['Y']]
Z = df.loc[:,['Z']]
XZ = df.loc[:,['X','Z']]
```
```{python}
import mccv
mccv_YXobj = mccv.mccv(num_bootstraps=200,n_jobs=2)
mccv_YXobj.set_X(X)
mccv_YXobj.set_Y(Y)
mccv_YXobj.run_mccv()
mccv_YZobj = mccv.mccv(num_bootstraps=200,n_jobs=2)
mccv_YZobj.set_X(Z)
mccv_YZobj.set_Y(Y)
mccv_YZobj.run_mccv()
mccv_YXZobj = mccv.mccv(num_bootstraps=200,n_jobs=2)
mccv_YXZobj.set_X(XZ)
mccv_YXZobj.set_Y(Y)
mccv_YXZobj.run_mccv()
```
```{python}
f_imp_dfs = dict()
f_imp_dfs['YXobj'] = \
mccv_YXobj.mccv_data['Feature Importance']
f_imp_dfs['YZobj'] = \
mccv_YZobj.mccv_data['Feature Importance']
f_imp_dfs['YXZobj'] = \
mccv_YXZobj.mccv_data['Feature Importance']
```
**Visualize feature importances**
```{r}
library(ggplot2)
f_imp_plot <- function(x,title){
ggplot(x,aes(feature,importance,color=feature)) +
geom_boxplot(alpha=0) +
geom_point(position=position_jitter(width=0.2)) +
scale_color_manual(values=c("X" = "indianred",
"Y" = "skyblue",
"Z" = "black",
"Intercept" = "gray")) +
theme_bw() +
labs(title=title)
}
library(patchwork)
(f_imp_plot( reticulate::py$f_imp_dfs$YXobj,"Y ~ X" ) +
f_imp_plot( reticulate::py$f_imp_dfs$YZobj,"Y ~ Z" ) ) /
f_imp_plot( reticulate::py$f_imp_dfs$YXZobj,"Y ~ X + Z" )
```
In this example, Z is a mediating relationship between X and Y. The 'Y \~ X' model shows that X is very important in predicting Y. However, when Z is included in the model 'Y \~ X + Z', you see that Z remains very important but X is no longer important for predicting Y. This toy example had Z causing Y and X relating to Y through Z, and any relationship between X and Y was mediated through Z. Therefore, including X and Z as predictors showed only the importance of Z in predicting Y.
:::