Skip to content

Commit

Permalink
Add intuition
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Lundberg committed Jun 28, 2024
1 parent 6f6766e commit c7f68f7
Show file tree
Hide file tree
Showing 22 changed files with 726 additions and 249 deletions.
2 changes: 2 additions & 0 deletions _quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ website:
left:
- href: index.qmd
text: Home
- href: intuition.qmd
text: Build Intuition
- href: data.qmd
text: Simulate Data
- href: outcomemodeling.qmd
Expand Down
Binary file added assets/simplematching.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/simpleoutcomemodeling.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/simplesetting.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/simpleweighting.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 17 additions & 6 deletions data.qmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "Generate Data"
title: "Simulate Data"
---

The code below will generate a dataset of $n = 100$ observations. Each observation contains several observed variables:
Expand All @@ -15,13 +15,24 @@ Each observation also contains outcomes that we know only because the data are s
* `Y0` The potential outcome under control
* `Y1` The potential outcome under treatment

To run this code, you will need the `dplyr` package. If you don't have it, first run the line `install.packages("dplyr")` in your R console.
To run this code, you will need the `dplyr` package. If you don't have it, first run the line `install.packages("dplyr")` in your R console. Then, add this line to your R script to load the package.

```{r}
```{r, message = F, warning = F}
library(dplyr)
n <- 100
data <- tibble(L1 = rnorm(n),
L2 = rnorm(n)) |>
```

If you want your simulation to match our numbers exactly, add a line to set your seed.

```{r}
set.seed(90095)
```

```{r}
n <- 500
data <- tibble(
L1 = rnorm(n),
L2 = rnorm(n)
) |>
# Generate potential outcomes as functions of L
mutate(Y0 = rnorm(n(), mean = L1 + L2, sd = 1),
Y1 = rnorm(n(), mean = Y0 + 1, sd = 1)) |>
Expand Down
Binary file added docs/assets/simplematching.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/simpleoutcomemodeling.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/simplesetting.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/simpleweighting.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 25 additions & 27 deletions docs/data.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">


<title>causalestimators - Generate Data</title>
<title>causalestimators - Simulate Data</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
Expand Down Expand Up @@ -120,6 +120,10 @@
<li class="nav-item">
<a class="nav-link" href="./index.html" rel="" target="">
<span class="menu-text">Home</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="./intuition.html" rel="" target="">
<span class="menu-text">Build Intuition</span></a>
</li>
<li class="nav-item">
<a class="nav-link active" href="./data.html" rel="" target="" aria-current="page">
Expand Down Expand Up @@ -156,7 +160,7 @@

<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title">Generate Data</h1>
<h1 class="title">Simulate Data</h1>
</div>


Expand Down Expand Up @@ -184,35 +188,29 @@ <h1 class="title">Generate Data</h1>
<li><code>Y0</code> The potential outcome under control</li>
<li><code>Y1</code> The potential outcome under treatment</li>
</ul>
<p>To run this code, you will need the <code>dplyr</code> package. If you don’t have it, first run the line <code>install.packages("dplyr")</code> in your R console.</p>
<p>To run this code, you will need the <code>dplyr</code> package. If you don’t have it, first run the line <code>install.packages("dplyr")</code> in your R console. Then, add this line to your R script to load the package.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(dplyr)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>
Attaching package: 'dplyr'</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>The following objects are masked from 'package:stats':

filter, lag</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>The following objects are masked from 'package:base':

intersect, setdiff, setequal, union</code></pre>
<p>If you want your simulation to match our numbers exactly, add a line to set your seed.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">90095</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>n <span class="ot">&lt;-</span> <span class="dv">100</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>data <span class="ot">&lt;-</span> <span class="fu">tibble</span>(<span class="at">L1 =</span> <span class="fu">rnorm</span>(n),</span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a> <span class="at">L2 =</span> <span class="fu">rnorm</span>(n)) <span class="sc">|&gt;</span></span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> <span class="co"># Generate potential outcomes as functions of L</span></span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Y0 =</span> <span class="fu">rnorm</span>(<span class="fu">n</span>(), <span class="at">mean =</span> L1 <span class="sc">+</span> L2, <span class="at">sd =</span> <span class="dv">1</span>),</span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a> <span class="at">Y1 =</span> <span class="fu">rnorm</span>(<span class="fu">n</span>(), <span class="at">mean =</span> Y0 <span class="sc">+</span> <span class="dv">1</span>, <span class="at">sd =</span> <span class="dv">1</span>)) <span class="sc">|&gt;</span></span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a> <span class="co"># Generate treatment as a function of L</span></span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">propensity_score =</span> <span class="fu">plogis</span>(<span class="sc">-</span><span class="dv">2</span> <span class="sc">+</span> L1 <span class="sc">+</span> L2)) <span class="sc">|&gt;</span></span>
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">A =</span> <span class="fu">rbinom</span>(<span class="fu">n</span>(), <span class="dv">1</span>, propensity_score)) <span class="sc">|&gt;</span></span>
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a> <span class="co"># Generate factual outcome</span></span>
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Y =</span> <span class="fu">case_when</span>(A <span class="sc">==</span> <span class="dv">0</span> <span class="sc">~</span> Y0,</span>
<span id="cb5-12"><a href="#cb5-12" aria-hidden="true" tabindex="-1"></a> A <span class="sc">==</span> <span class="dv">1</span> <span class="sc">~</span> Y1))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell">
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>n <span class="ot">&lt;-</span> <span class="dv">500</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>data <span class="ot">&lt;-</span> <span class="fu">tibble</span>(</span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a> <span class="at">L1 =</span> <span class="fu">rnorm</span>(n),</span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a> <span class="at">L2 =</span> <span class="fu">rnorm</span>(n)</span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a>) <span class="sc">|&gt;</span></span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a> <span class="co"># Generate potential outcomes as functions of L</span></span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Y0 =</span> <span class="fu">rnorm</span>(<span class="fu">n</span>(), <span class="at">mean =</span> L1 <span class="sc">+</span> L2, <span class="at">sd =</span> <span class="dv">1</span>),</span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a> <span class="at">Y1 =</span> <span class="fu">rnorm</span>(<span class="fu">n</span>(), <span class="at">mean =</span> Y0 <span class="sc">+</span> <span class="dv">1</span>, <span class="at">sd =</span> <span class="dv">1</span>)) <span class="sc">|&gt;</span></span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a> <span class="co"># Generate treatment as a function of L</span></span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">propensity_score =</span> <span class="fu">plogis</span>(<span class="sc">-</span><span class="dv">2</span> <span class="sc">+</span> L1 <span class="sc">+</span> L2)) <span class="sc">|&gt;</span></span>
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">A =</span> <span class="fu">rbinom</span>(<span class="fu">n</span>(), <span class="dv">1</span>, propensity_score)) <span class="sc">|&gt;</span></span>
<span id="cb3-12"><a href="#cb3-12" aria-hidden="true" tabindex="-1"></a> <span class="co"># Generate factual outcome</span></span>
<span id="cb3-13"><a href="#cb3-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Y =</span> <span class="fu">case_when</span>(A <span class="sc">==</span> <span class="dv">0</span> <span class="sc">~</span> Y0,</span>
<span id="cb3-14"><a href="#cb3-14" aria-hidden="true" tabindex="-1"></a> A <span class="sc">==</span> <span class="dv">1</span> <span class="sc">~</span> Y1))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>A simulation is nice because the answer is known. In this simulation, the conditional average causal effect of <code>A</code> on <code>Y</code> equals 1 at any value of <code>L1</code> and <code>L_2</code>.</p>

Expand Down
7 changes: 6 additions & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@
<li class="nav-item">
<a class="nav-link active" href="./index.html" rel="" target="" aria-current="page">
<span class="menu-text">Home</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="./intuition.html" rel="" target="">
<span class="menu-text">Build Intuition</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="./data.html" rel="" target="">
Expand Down Expand Up @@ -135,7 +139,8 @@ <h1 class="title">Causal Estimators</h1>

</header>

<p>This page leads a tutorial on causal estimators. See the first page for code to generate data, and choose any of the subsequent pages to practice applying a causal estimator.</p>
<p>This page offers a tutorial on causal estimators. We first designed this page for the Summer Institute in Computational Social Science at UCLA in June 2024.</p>
<p>See the first page for intuition about causal estimators. The next page provides code to simulate data. You can choose any of the subsequent pages in any order to practice applying causal estimators.</p>



Expand Down
Loading

0 comments on commit c7f68f7

Please sign in to comment.