Skip to content

Commit cf90fd9

Browse files
Merge pull request #57 from r-causal/code-examples
2 parents 427ad28 + db69625 commit cf90fd9

File tree

9 files changed

+60
-16
lines changed

9 files changed

+60
-16
lines changed

slides/pdf/07-using-pscores.pdf

10.6 KB
Binary file not shown.

slides/raw/07-using-pscores.html

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ <h2>Matching in R (ATM)</h2>
705705
<h2><em>Your Turn 1</em></h2>
706706
<div class="cell" data-layout-align="center">
707707
<div class="cell-output-display">
708-
<div class="countdown" id="timer_f26e5c3e" data-update-every="1" tabindex="0" style="right:0;bottom:0;">
708+
<div class="countdown" id="timer_3820d331" data-update-every="1" tabindex="0" style="right:0;bottom:0;">
709709
<div class="countdown-controls"><button class="countdown-bump-down"></button><button class="countdown-bump-up">+</button></div>
710710
<code class="countdown-time"><span class="countdown-digits minutes">10</span><span class="countdown-digits colon">:</span><span class="countdown-digits seconds">00</span></code>
711711
</div>
@@ -725,65 +725,82 @@ <h2>Propensity scores</h2>
725725
<h2>Target estimands: ATE</h2>
726726
<p>Average Treatment Effect (ATE)</p>
727727
<p><span class="math display">\[\Large w_{ATE} = \frac{Z_i}{p_i} + \frac{1-Z_i}{1 - p_i}\]</span></p>
728+
<div class="cell" data-layout-align="center">
729+
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1"></a>(Z <span class="sc">/</span> p) <span class="sc">+</span> ((<span class="dv">1</span> <span class="sc">-</span> Z) <span class="sc">/</span> (<span class="dv">1</span> <span class="sc">-</span> p))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
730+
</div>
728731
</section>
729732
<section id="target-estimands-att-atc" class="slide level2" data-background-color="#23373B">
730733
<h2>Target estimands: ATT &amp; ATC</h2>
731734
<div class="fragment">
732735
<p>Average Treatment Effect Among the Treated (ATT) <span class="math display">\[\Large w_{ATT} = \frac{p_i Z_i}{p_i} + \frac{p_i (1-Z_i)}{1-p_i}\]</span></p>
736+
<div class="cell" data-layout-align="center">
737+
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1"></a>((p <span class="sc">*</span> Z) <span class="sc">/</span> p) <span class="sc">+</span> ((p <span class="sc">*</span> (<span class="dv">1</span> <span class="sc">-</span> Z)) <span class="sc">/</span> (<span class="dv">1</span> <span class="sc">-</span> p))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
733738
</div>
734-
<div class="fragment">
739+
</div>
740+
</section>
741+
<section id="target-estimands-att-atc-1" class="slide level2" data-background-color="#23373B">
742+
<h2>Target estimands: ATT &amp; ATC</h2>
735743
<p>Average Treatment Effect Among the Controls (ATC) <span class="math display">\[\Large w_{ATC} = \frac{(1-p_i)Z_i}{p_i} + \frac{(1-p_i)(1-Z_i)}{(1-p_i)}\]</span></p>
744+
<div class="cell" data-layout-align="center">
745+
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1"></a>(((<span class="dv">1</span> <span class="sc">-</span> p) <span class="sc">*</span> Z) <span class="sc">/</span> p) <span class="sc">+</span> (((<span class="dv">1</span> <span class="sc">-</span> p) <span class="sc">*</span> (<span class="dv">1</span> <span class="sc">-</span> Z)) <span class="sc">/</span> (<span class="dv">1</span> <span class="sc">-</span> p))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
736746
</div>
737747
</section>
738748
<section id="target-estimands-atm-ato" class="slide level2" data-background-color="#23373B">
739749
<h2>Target estimands: ATM &amp; ATO</h2>
740750
<div class="fragment">
741751
<p>Average Treatment Effect Among the Evenly Matchable (ATM) <span class="math display">\[\Large w_{ATM} = \frac{\min \{p_i, 1-p_i\}}{Z_ip_i + (1-Z_i)(1-p_i)}\]</span></p>
752+
<div class="cell" data-layout-align="center">
753+
<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1"></a><span class="fu">pmin</span>(p, <span class="dv">1</span> <span class="sc">-</span> p) <span class="sc">/</span> (Z <span class="sc">*</span> p <span class="sc">+</span> (<span class="dv">1</span> <span class="sc">-</span> Z) <span class="sc">*</span> (<span class="dv">1</span> <span class="sc">-</span> p))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
742754
</div>
743-
<div class="fragment">
755+
</div>
756+
</section>
757+
<section id="target-estimands-atm-ato-1" class="slide level2" data-background-color="#23373B">
758+
<h2>Target estimands: ATM &amp; ATO</h2>
744759
<p>Average Treatment Effect Among the Overlap Population <span class="math display">\[\Large w_{ATO} = (1-p_i)Z_i + p_i(1-Z_i)\]</span></p>
760+
<div class="cell" data-layout-align="center">
761+
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1"></a>(<span class="dv">1</span> <span class="sc">-</span> p) <span class="sc">*</span> Z <span class="sc">+</span> p <span class="sc">*</span> (<span class="dv">1</span> <span class="sc">-</span> Z)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
745762
</div>
746763
</section>
747764
<section id="histogram-of-propensity-scores" class="slide level2">
748765
<h2>Histogram of propensity scores</h2>
749766

750-
<img data-src="07-using-pscores_files/figure-revealjs/unnamed-chunk-14-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
767+
<img data-src="07-using-pscores_files/figure-revealjs/unnamed-chunk-19-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
751768
<section id="ate" class="slide level2">
752769
<h2>ATE</h2>
753770

754-
<img data-src="07-using-pscores_files/figure-revealjs/unnamed-chunk-15-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
771+
<img data-src="07-using-pscores_files/figure-revealjs/unnamed-chunk-20-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
755772
<section id="att" class="slide level2">
756773
<h2>ATT</h2>
757774

758-
<img data-src="07-using-pscores_files/figure-revealjs/unnamed-chunk-16-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
775+
<img data-src="07-using-pscores_files/figure-revealjs/unnamed-chunk-21-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
759776
<section id="atc" class="slide level2">
760777
<h2>ATC</h2>
761778

762-
<img data-src="07-using-pscores_files/figure-revealjs/unnamed-chunk-17-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
779+
<img data-src="07-using-pscores_files/figure-revealjs/unnamed-chunk-22-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
763780
<section id="atm" class="slide level2">
764781
<h2>ATM</h2>
765782

766-
<img data-src="07-using-pscores_files/figure-revealjs/unnamed-chunk-18-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
783+
<img data-src="07-using-pscores_files/figure-revealjs/unnamed-chunk-23-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
767784
<section id="ato" class="slide level2">
768785
<h2>ATO</h2>
769786

770-
<img data-src="07-using-pscores_files/figure-revealjs/unnamed-chunk-19-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
787+
<img data-src="07-using-pscores_files/figure-revealjs/unnamed-chunk-24-1.png" style="width:80.0%" class="r-stretch quarto-figure-center"></section>
771788
<section id="ate-in-r" class="slide level2">
772789
<h2>ATE in R <img data-src="img/hex/propensity.png" class="absolute" style="top: 0px; right: 0px; width: 140px; "></h2>
773790
<p><br></p>
774791
<p>Average Treatment Effect (ATE) <span class="math inline">\(w_{ATE} = \frac{Z_i}{p_i} + \frac{1-Z_i}{1 - p_i}\)</span></p>
775792
<div class="cell" data-layout-align="center">
776-
<div class="sourceCode cell-code" id="cb11" data-code-line-numbers="|4"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1"></a><span class="fu">library</span>(propensity)</span>
777-
<span id="cb11-2"><a href="#cb11-2"></a>df <span class="ot">&lt;-</span> propensity_model <span class="sc">|&gt;</span></span>
778-
<span id="cb11-3"><a href="#cb11-3"></a> <span class="fu">augment</span>(<span class="at">type.predict =</span> <span class="st">"response"</span>, <span class="at">data =</span> nhefs_complete) <span class="sc">|&gt;</span></span>
779-
<span id="cb11-4"><a href="#cb11-4"></a> <span class="fu">mutate</span>(<span class="at">w_ate =</span> <span class="fu">wt_ate</span>(.fitted, qsmk)) </span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
793+
<div class="sourceCode cell-code" id="cb16" data-code-line-numbers="|4"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1"></a><span class="fu">library</span>(propensity)</span>
794+
<span id="cb16-2"><a href="#cb16-2"></a>df <span class="ot">&lt;-</span> propensity_model <span class="sc">|&gt;</span></span>
795+
<span id="cb16-3"><a href="#cb16-3"></a> <span class="fu">augment</span>(<span class="at">type.predict =</span> <span class="st">"response"</span>, <span class="at">data =</span> nhefs_complete) <span class="sc">|&gt;</span></span>
796+
<span id="cb16-4"><a href="#cb16-4"></a> <span class="fu">mutate</span>(<span class="at">w_ate =</span> <span class="fu">wt_ate</span>(.fitted, qsmk)) </span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
780797
</div>
781798
</section>
782799
<section id="your-turn" class="slide level2">
783800
<h2><em>Your Turn</em></h2>
784801
<div class="cell" data-layout-align="center">
785802
<div class="cell-output-display">
786-
<div class="countdown" id="timer_7fd186af" data-update-every="1" tabindex="0" style="right:0;bottom:0;">
803+
<div class="countdown" id="timer_5ab3ca30" data-update-every="1" tabindex="0" style="right:0;bottom:0;">
787804
<div class="countdown-controls"><button class="countdown-bump-down"></button><button class="countdown-bump-up">+</button></div>
788805
<code class="countdown-time"><span class="countdown-digits minutes">10</span><span class="countdown-digits colon">:</span><span class="countdown-digits seconds">00</span></code>
789806
</div>

slides/raw/07-using-pscores.qmd

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@ Direct Adjustment
242242
Average Treatment Effect (ATE)
243243

244244
$$\Large w_{ATE} = \frac{Z_i}{p_i} + \frac{1-Z_i}{1 - p_i}$$
245+
```{r}
246+
#| eval: false
247+
(Z / p) + ((1 - Z) / (1 - p))
248+
```
249+
250+
245251

246252
## Target estimands: ATT & ATC {background-color="#23373B"}
247253

@@ -250,11 +256,20 @@ $$\Large w_{ATE} = \frac{Z_i}{p_i} + \frac{1-Z_i}{1 - p_i}$$
250256
Average Treatment Effect Among the Treated (ATT)
251257
$$\Large w_{ATT} = \frac{p_i Z_i}{p_i} + \frac{p_i (1-Z_i)}{1-p_i}$$
252258

253-
. . .
259+
```{r}
260+
#| eval: false
261+
((p * Z) / p) + ((p * (1 - Z)) / (1 - p))
262+
```
263+
264+
## Target estimands: ATT & ATC {background-color="#23373B"}
254265

255266
Average Treatment Effect Among the Controls (ATC)
256267
$$\Large w_{ATC} = \frac{(1-p_i)Z_i}{p_i} + \frac{(1-p_i)(1-Z_i)}{(1-p_i)}$$
257268

269+
```{r}
270+
#| eval: false
271+
(((1 - p) * Z) / p) + (((1 - p) * (1 - Z)) / (1 - p))
272+
```
258273

259274
## Target estimands: ATM & ATO {background-color="#23373B"}
260275

@@ -263,11 +278,23 @@ $$\Large w_{ATC} = \frac{(1-p_i)Z_i}{p_i} + \frac{(1-p_i)(1-Z_i)}{(1-p_i)}$$
263278
Average Treatment Effect Among the Evenly Matchable (ATM)
264279
$$\Large w_{ATM} = \frac{\min \{p_i, 1-p_i\}}{Z_ip_i + (1-Z_i)(1-p_i)}$$
265280

266-
. . .
281+
282+
```{r}
283+
#| eval: false
284+
pmin(p, 1 - p) / (Z * p + (1 - Z) * (1 - p))
285+
```
286+
287+
## Target estimands: ATM & ATO {background-color="#23373B"}
267288

268289
Average Treatment Effect Among the Overlap Population
269290
$$\Large w_{ATO} = (1-p_i)Z_i + p_i(1-Z_i)$$
270291

292+
293+
```{r}
294+
#| eval: false
295+
(1 - p) * Z + p * (1 - Z)
296+
```
297+
271298
## Histogram of propensity scores
272299

273300
```{r}
-11.3 KB
Loading
92.1 KB
Loading
92.8 KB
Loading
86.7 KB
Loading
92.7 KB
Loading
93.9 KB
Loading

0 commit comments

Comments
 (0)