|
| 1 | +#= |
| 2 | +# Interaction Estimation |
| 3 | +
|
| 4 | +In this example we aim to estimate the average interaction effect of two, potentially correlated, |
| 5 | +treatment variables `T1` and `T2` on an outcome `Y`. |
| 6 | +
|
| 7 | +## Data Generating Process |
| 8 | +
|
| 9 | +Let's consider the following structural causal model where the shaded nodes represent the observed variables. |
| 10 | +
|
| 11 | + |
| 12 | +
|
| 13 | +In other words, only one confounding variable is observed (`W1`). This would be a major problem if we wanted to estimate the |
| 14 | +average treatment effect of `T1` or `T2` on `Y` separately. However, here, we are interested in interactions and thus `W1` is |
| 15 | +a sufficient adjustment set. This artificial situation is ubiquitous in genetics, where two main sources of confounding exist. |
| 16 | +Ancestry, can be estimated (here `W1`) and linkage disequilibrium is usually more challenging to address (here `W2`). |
| 17 | +
|
| 18 | +Let us first define some helper functions and import some libraries. |
| 19 | +=# |
| 20 | +using Distributions |
| 21 | +using Random |
| 22 | +using DataFrames |
| 23 | +using Statistics |
| 24 | +using CategoricalArrays |
| 25 | +using TMLE |
| 26 | +using CairoMakie |
| 27 | +using MLJXGBoostInterface |
| 28 | +using MLJ |
| 29 | +using MLJLinearModels |
| 30 | +Random.seed!(123) |
| 31 | + |
| 32 | +μT(w) = [sum(w), sum(w)] |
| 33 | + |
| 34 | +μY(t, w) = 1 + 10t[1] - 3t[2] * t[1] * w |
| 35 | + |
| 36 | +#= |
| 37 | +We assume that `W1` and `W2`, the confounding variables, follow a uniform distribution. |
| 38 | +=# |
| 39 | + |
| 40 | +generate_W(n) = rand(Uniform(0, 1), 2, n) |
| 41 | + |
| 42 | +#= |
| 43 | +`T1` and `T2` are generated via a copula method through a multivariate normal to induce some statistical dependence (via σ). |
| 44 | +=# |
| 45 | + |
| 46 | +function generate_T(W, n; σ=0.5, threshold=0) |
| 47 | + covariance = [ |
| 48 | + 1. σ |
| 49 | + σ 1. |
| 50 | + ] |
| 51 | + T = zeros(Bool, 2, n) |
| 52 | + for i in 1:n |
| 53 | + dTi = MultivariateNormal(μT(W[:, i]), covariance) |
| 54 | + T[:, i] = rand(dTi) .> threshold |
| 55 | + end |
| 56 | + return T |
| 57 | +end |
| 58 | + |
| 59 | +#= |
| 60 | +Finally, `Y` is generated through a simple linear model with an interaction term. |
| 61 | +=# |
| 62 | + |
| 63 | +function generate_Y(T, W1, n; σY=1) |
| 64 | + Y = zeros(Float64, n) |
| 65 | + for i in 1:n |
| 66 | + dY = Normal(μY(T[:, i], W1[i]), σY) |
| 67 | + Y[i] = rand(dY) |
| 68 | + end |
| 69 | + return Y |
| 70 | +end |
| 71 | + |
| 72 | +#= |
| 73 | +Importantly, the average interaction effect between `T1` and `T2` is thus ``-3 \mathbb{E}[W] = -1.5``. |
| 74 | +
|
| 75 | +We will generate a full dataset with the following function. |
| 76 | +=# |
| 77 | + |
| 78 | +function generate_dataset(;n=1000, σ=0.5, threshold=0., σY=1) |
| 79 | + |
| 80 | + W = generate_W(n) |
| 81 | + T = generate_T(W, n; σ=σ, threshold=threshold) |
| 82 | + |
| 83 | + W = permutedims(W) |
| 84 | + W1 = W[:, 1] |
| 85 | + W2 = W[:, 2] |
| 86 | + |
| 87 | + Y = generate_Y(T, W1, n; σY=σY) |
| 88 | + |
| 89 | + T = permutedims(T) |
| 90 | + T1 = categorical(T[:, 1]) |
| 91 | + T2 = categorical(T[:, 2]) |
| 92 | + |
| 93 | + return DataFrame(W1=W1, W2=W2, T1=T1, T2=T2, Y=Y) |
| 94 | +end |
| 95 | + |
| 96 | +dataset = generate_dataset() |
| 97 | + |
| 98 | +first(dataset, 5) |
| 99 | +#= |
| 100 | +Let's verify that each treatment level is sufficiently present in the dataset (≈positivity). |
| 101 | +=# |
| 102 | + |
| 103 | +combine(groupby(dataset, [:T1, :T2]), proprow => :JOINT_TREATMENT_FREQ) |
| 104 | + |
| 105 | +#= |
| 106 | +And that `T1` and `T2` are indeed correlated. |
| 107 | +=# |
| 108 | + |
| 109 | +treatment_correlation(dataset) = cor(unwrap.(dataset.T1), unwrap.(dataset.T2)) |
| 110 | +@assert treatment_correlation(dataset) > 0.2 #hide |
| 111 | +treatment_correlation(dataset) |
| 112 | + |
| 113 | +#= |
| 114 | +## Estimation |
| 115 | +
|
| 116 | +We can now proceed to estimation using TMLE and default (linear) models. |
| 117 | +
|
| 118 | +Interactions are defined via the `AIE` function (note that we only set `W1` as a confounder). |
| 119 | +=# |
| 120 | + |
| 121 | +Ψ = AIE( |
| 122 | + outcome=:Y, |
| 123 | + treatment_values= ( |
| 124 | + T1=(case=1, control=0), |
| 125 | + T2=(case=1, control=0) |
| 126 | + ), |
| 127 | + treatment_confounders = [:W1] |
| 128 | +) |
| 129 | +linear_models = default_models(G=LogisticClassifier(lambda=0), Q_continuous=LinearRegressor()) |
| 130 | +estimator = TMLEE(models=linear_models, weighted=true) |
| 131 | +result, _ = estimator(Ψ, dataset; verbosity=0) |
| 132 | +@assert pvalue(significance_test(result, -1.5)) > 0.05 #hide |
| 133 | +significance_test(result) |
| 134 | + |
| 135 | +#= |
| 136 | +The true effect size is thus covered by our confidence interval. |
| 137 | +
|
| 138 | +## Varying levels of correlation |
| 139 | +
|
| 140 | +We now vary the correlation level between `T1` and `T2` to observe how it affects the estimation results. |
| 141 | +First, let's see how the parameter σ affects the correlation between `T1` and `T2`. |
| 142 | +=# |
| 143 | + |
| 144 | +function plot_correlations(;σs = 0.1:0.1:1, n=1000, threshold=0., σY=1.) |
| 145 | + fig = Figure() |
| 146 | + ax = Axis(fig[1, 1], xlabel="σ", ylabel="Correlation(T1, T2)") |
| 147 | + correlations = map(σs) do σ |
| 148 | + dataset = generate_dataset(;n=n, σ=σ, threshold=threshold, σY=σY) |
| 149 | + return treatment_correlation(dataset) |
| 150 | + end |
| 151 | + scatter!(ax, σs, correlations, color=:blue) |
| 152 | + return fig |
| 153 | +end |
| 154 | + |
| 155 | +σs = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 0.99] |
| 156 | +plot_correlations(;σs=σs, n=10_000) |
| 157 | + |
| 158 | +#= |
| 159 | +As expected, the correlation between `T1` and `T2` increases with σ. Let's see how this affects estimation, |
| 160 | +for this, we will vary both the dataset size and the correlation level. |
| 161 | +=# |
| 162 | + |
| 163 | +function estimate_across_correlation_levels(σs; n=1000, estimator=TMLEE(weighted=true)) |
| 164 | + results = [] |
| 165 | + for σ in σs |
| 166 | + dataset = generate_dataset(n=n, σ=σ) |
| 167 | + result, _ = estimator(Ψ, dataset; verbosity=0) |
| 168 | + push!(results, result) |
| 169 | + end |
| 170 | + Ψ̂s = TMLE.estimate.(results) |
| 171 | + errors = last.(confint.(significance_test.(results))) .- Ψ̂s |
| 172 | + return Ψ̂s, errors |
| 173 | +end |
| 174 | + |
| 175 | +function estimate_across_sample_sizes_and_correlation_levels(ns, σs; estimator=TMLEE(models=linear_models, weighted=true)) |
| 176 | + results = [] |
| 177 | + for n in ns |
| 178 | + Ψ̂s, errors = estimate_across_correlation_levels(σs; n=n, estimator=estimator) |
| 179 | + push!(results, (Ψ̂s, errors)) |
| 180 | + end |
| 181 | + return results |
| 182 | +end |
| 183 | + |
| 184 | +function plot_across_sample_sizes_and_correlation_levels(results, ns, σs; title="Estimation via TMLE (GLMs)") |
| 185 | + fig = Figure(size=(800, 800)) |
| 186 | + for (index, n) in enumerate(ns) |
| 187 | + Ψ̂s, errors = results[index] |
| 188 | + ax = if n == last(ns) |
| 189 | + Axis(fig[index, 1], xlabel="σ", ylabel="AIE\n(n=$n)") |
| 190 | + else |
| 191 | + Axis(fig[index, 1], ylabel="AIE\n(n=$n)", xticklabelsvisible=false) |
| 192 | + end |
| 193 | + errorbars!(ax, σs, Ψ̂s, errors, color = :blue, whiskerwidth = 10) |
| 194 | + scatter!(ax, σs, Ψ̂s, color=:red, markersize=10) |
| 195 | + hlines!(ax, [-1.5], color=:green, linestyle=:dash) |
| 196 | + end |
| 197 | + Label(fig[0, :], title; tellwidth=false, fontsize=24) |
| 198 | + return fig |
| 199 | +end |
| 200 | + |
| 201 | +ns = [100, 1000, 10_000, 100_000, 1_000_000] |
| 202 | +σs = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 0.999] |
| 203 | +results = estimate_across_sample_sizes_and_correlation_levels(ns, σs; estimator=TMLEE(models=linear_models, weighted=true)) |
| 204 | +plot_across_sample_sizes_and_correlation_levels(results, ns, σs; title="Estimation via TMLE (GLMs)") |
| 205 | + |
| 206 | +#= |
| 207 | +First, notice that only extreme correlations (>0.9) tend to blow up the size of the confidence intervals. This implies that statistical power may be limited in such circumstances. |
| 208 | +
|
| 209 | +Furthermore, and perhaps unexpectedly, coverage decreases as sample size grows for larger correlations. Since we have used simple linear models until now, |
| 210 | +this could be due to model misspecification. We can verify this by using a more flexible modelling strategy. Here we will use XGBoost |
| 211 | +(with tree_method=`hist` to speed things up a little). Because this model is prone to overfitting we will also use cross-validation (this will take a few minutes). |
| 212 | +=# |
| 213 | + |
| 214 | +xgboost_estimator = TMLEE( |
| 215 | + models=default_models(G=XGBoostClassifier(tree_method="hist"), Q_continuous=XGBoostRegressor(tree_method="hist")), |
| 216 | + weighted=true, |
| 217 | + resampling=StratifiedCV(nfolds=3) |
| 218 | +) |
| 219 | +xgboost_results = estimate_across_sample_sizes_and_correlation_levels(ns, σs, estimator=xgboost_estimator) |
| 220 | +plot_across_sample_sizes_and_correlation_levels(xgboost_results, ns, σs; title="Estimation via TMLE (XGboost)") |
| 221 | + |
| 222 | +#= |
| 223 | +As expected, XGBoost improves estimation performance in the asymptotic regime, furthermore, |
| 224 | +the correlation between `T1` and `T2` seems harmless (except when σ > 0.9 as before). |
| 225 | +=# |
0 commit comments