<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>econometrics.blog</title>
<link>https://www.econometrics.blog/</link>
<atom:link href="https://www.econometrics.blog/index.xml" rel="self" type="application/rss+xml"/>
<description>Puzzles, intuitions, and sundry economic tricks.</description>
<generator>quarto-1.10.18</generator>
<lastBuildDate>Fri, 14 Aug 2026 00:00:00 GMT</lastBuildDate>
<item>
  <title>Econometrics Puzzler #4: Rescaling the Reduced Form</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/econometrics-puzzler-4-rescaling-the-reduced-form/</link>
  <description><![CDATA[ 




<p>Consider the simplest textbook instrumental variables (IV) model with one endogenous regressor <img src="https://latex.codecogs.com/png.latex?X"> and a single valid instrument <img src="https://latex.codecogs.com/png.latex?Z">. You are given the estimated slope coefficient <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cgamma%7D"> from the reduced form regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?Z"> along with its standard error <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Chat%7B%5Cgamma%7D)">. You are also given the estimated slope coefficient <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cpi%7D"> from the first stage regression of <img src="https://latex.codecogs.com/png.latex?X"> on <img src="https://latex.codecogs.com/png.latex?Z">. The IV estimate equals <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cgamma%7D/%20%5Chat%7B%5Cpi%7D">. Does its standard error equal <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Chat%7B%5Cgamma%7D)/%5Chat%7B%5Cpi%7D">?</p>
<section id="solution" class="level2 page-columns page-full" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="solution"><span class="header-section-number">1</span> Solution</h2>
<div class="solution callout callout-style-simple callout-note no-icon callout-titled" title="Solution">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Solution
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Here’s why it’s tempting to answer <em>yes</em>. If <img src="https://latex.codecogs.com/png.latex?Y"> is measured in dollars and <img src="https://latex.codecogs.com/png.latex?Z"> is measured in grams, then <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cgamma%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Chat%7B%5Cgamma%7D)"> are both measured in dollars per gram. Now suppose that you wanted to express both quantities in dollars per <em>kilogram</em> rather than dollars per gram. No problem: simply multiply each by 1000. The IV logic from above <em>looks</em> analogous: the IV estimator is simply a rescaled version of the reduced form. So does the same relationship hold for the standard error? Well, there’s an equally tempting reason to say <em>no</em>. Since <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cpi%7D"> is estimated, rather than a known constant, dividing by it should <em>add</em> sampling variability of its own. This reasoning suggests that <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Chat%7B%5Cgamma%7D)/%5Chat%7B%5Cpi%7D"> should be <em>too small</em>. So which is it? When in doubt, simulate:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1848</span>)</span>
<span id="cb1-2">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span></span>
<span id="cb1-3">z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(n)</span>
<span id="cb1-4">S <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span id="cb1-5">              <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">byrow =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(mvtnorm)</span>
<span id="cb1-8">errors <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rmvnorm</span>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> S)</span>
<span id="cb1-9">u <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> errors[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb1-10">v <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> errors[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb1-11"></span>
<span id="cb1-12">pi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span></span>
<span id="cb1-13">beta <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span></span>
<span id="cb1-14"></span>
<span id="cb1-15">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> pi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> z <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> v</span>
<span id="cb1-16">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> beta <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> u</span>
<span id="cb1-17"></span>
<span id="cb1-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ivreg)</span>
<span id="cb1-19"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-20"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(broom)</span>
<span id="cb1-21"></span>
<span id="cb1-22">get_coef_se <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(results, varname) {</span>
<span id="cb1-23">  results <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-24">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tidy</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-25">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(term <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> varname) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-26">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(estimate, std.error)</span>
<span id="cb1-27">}</span>
<span id="cb1-28"></span>
<span id="cb1-29">iv <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ivreg</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> z) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_coef_se</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x'</span>)</span>
<span id="cb1-31"></span>
<span id="cb1-32">first_stage <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> z) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_coef_se</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'z'</span>)</span>
<span id="cb1-34"></span>
<span id="cb1-35">reduced_form <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> z) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-36">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_coef_se</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'z'</span>)</span></code></pre></div></div>
</div>
<p>The IV slope estimate is exactly equal to the ratio of reduced form and first stage slopes, as it should be:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">est_by_hand =</span> reduced_form<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> first_stage<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate,</span>
<span id="cb2-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">est_ivreg =</span> iv<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>est_by_hand   est_ivreg 
   1.160068    1.160068 </code></pre>
</div>
</div>
<p>but the IV <em>standard error</em> does not share the same proportionality relationship to the reduced form standard error:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se_by_hand =</span> reduced_form<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>std.error <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> first_stage<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate,</span>
<span id="cb4-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se_ivreg =</span> iv<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>std.error)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>se_by_hand   se_ivreg 
 0.3407198  0.1861929 </code></pre>
</div>
</div>
<p>So the answer is <em>no</em>, but it seems that <em>both</em> of the tempting intuitions have failed us here: the naive standard error isn’t too small; it’s nearly <em>twice</em> as large as the correct one. What’s going on?</p>
<p>To answer this question, we’ll start by writing down the structural equation <img src="https://latex.codecogs.com/png.latex?Y%20=%20%5Cbeta%20X%20+%20U"> and the first stage equation <img src="https://latex.codecogs.com/png.latex?X%20=%20%5Cpi%20Z%20+%20V">.<sup>1</sup> Combining them gives the reduced form: <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0AY%20&amp;=%20%5Cbeta%20X%20+%20U%20=%20%5Cbeta(%5Cpi%20Z%20+%20V)%20+%20U%5C%5C%0A&amp;=%20(%5Cbeta%5Cpi)%20Z%20%20+%20(%5Cbeta%20V%20+%20U)%5C%5C%0A&amp;=%20%5Cgamma%20Z%20+%20%5Cvarepsilon%0A%5Cend%7Baligned%7D%0A"> where <img src="https://latex.codecogs.com/png.latex?%5Cgamma%20%5Cequiv%20%5Cbeta%20%5Cpi"> and <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon%20%5Cequiv%20%5Cbeta%20V%20+%20U">. Now, the IV estimator can be expanded according to <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Chat%7B%5Cbeta%7D%20&amp;=%20(%5Cunderline%7BZ%7D'%5Cunderline%7BX%7D)%5E%7B-1%7D(%5Cunderline%7BZ%7D'%5Cunderline%7BY%7D)%20=%20(%5Cunderline%7BZ%7D'%5Cunderline%7BX%7D)%5E%7B-1%7D%5Cleft%5B%5Cunderline%7BZ%7D'(%5Cbeta%20%5Cunderline%7BX%7D%20+%20%5Cunderline%7BU%7D)%5Cright%5D%5C%5C%0A&amp;=%20%5Cbeta%20+%20(%5Cunderline%7BZ%7D'%5Cunderline%7BX%7D)%5E%7B-1%7D(%5Cunderline%7BZ%7D'%5Cunderline%7BU%7D)%0A%5Cend%7Baligned%7D%0A"> where <img src="https://latex.codecogs.com/png.latex?%5Cunderline%7BZ%7D'"> denotes the row vector <img src="https://latex.codecogs.com/png.latex?(Z_1,%20%5Cdots,%20Z_n)"> and analogously for <img src="https://latex.codecogs.com/png.latex?%5Cunderline%7BY%7D,%20%5Cunderline%7BX%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Cunderline%7BU%7D">. Thus, subtracting <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> from both sides and multiplying by <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7Bn%7D">, <img src="https://latex.codecogs.com/png.latex?%0A%5Csqrt%7Bn%7D(%5Chat%7B%5Cbeta%7D%20-%20%5Cbeta)%20=%20%5Cleft(%5Cfrac%7B%5Cunderline%7BZ%7D'%5Cunderline%7BX%7D%7D%7Bn%7D%5Cright)%5E%7B-1%7D%5Cleft(%5Cfrac%7B%5Cunderline%7BZ%7D'%5Cunderline%7BU%7D%7D%7B%5Csqrt%7Bn%7D%7D%5Cright)%20%5Cxrightarrow%7Bd%7D%20%5Cfrac%7B1%7D%7B%5Cmathbb%7BE%7D(ZX)%7D%20%20%5Ctimes%20N%5Cleft(0,%20%5Cmathbb%7BE%7D%5BZ%5E2U%5E2%5D%5Cright)%0A"> by the central limit theorem. Proceeding similarly for the reduced form estimator: <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Chat%7B%5Cgamma%7D%20&amp;=%20(%5Cunderline%7BZ%7D'%5Cunderline%7BZ%7D)%5E%7B-1%7D(%5Cunderline%7BZ%7D'%5Cunderline%7BY%7D)%20=%20(%5Cunderline%7BZ%7D'%5Cunderline%7BZ%7D)%5E%7B-1%7D%5Cleft%5B%5Cunderline%7BZ%7D'(%5Cgamma%20%5Cunderline%7BZ%7D%20+%20%5Cunderline%7B%5Cvarepsilon%7D)%5Cright%5D%5C%5C%0A&amp;=%20%5Cgamma%20+%20(%5Cunderline%7BZ%7D'%5Cunderline%7BZ%7D)%5E%7B-1%7D(%5Cunderline%7BZ%7D'%5Cunderline%7B%5Cvarepsilon%7D)%0A%5Cend%7Baligned%7D%0A"> and therefore, <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Csqrt%7Bn%7D(%5Chat%7B%5Cgamma%7D%20-%20%5Cgamma)%20=%20%5Cleft(%20%5Cfrac%7B%5Cunderline%7BZ%7D'%5Cunderline%7BZ%7D%7D%7Bn%7D%5Cright)%5E%7B-1%7D%5Cleft(%20%5Cfrac%7B%5Cunderline%7BZ%7D'%5Cunderline%7B%5Cvarepsilon%7D%7D%7B%5Csqrt%7Bn%7D%7D%5Cright)%20%5Cxrightarrow%7Bd%7D%20%5Cfrac%7B1%7D%7B%5Cmathbb%7BE%7D(Z%5E2)%7D%20%5Ctimes%20N(0,%20%5Cmathbb%7BE%7D%5BZ%5E2%5Cvarepsilon%5E2%5D)%0A%5Cend%7Baligned%7D%0A"> To keep the math simple, I’ll assume that <img src="https://latex.codecogs.com/png.latex?Z"> is <em>independent</em> of both <img src="https://latex.codecogs.com/png.latex?U"> and <img src="https://latex.codecogs.com/png.latex?V">, as in the simulation above.<sup>2</sup> Under this assumption <img src="https://latex.codecogs.com/png.latex?Z"> is <em>also</em> independent of <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon"> and thus <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cmathbb%7BE%7D(Z%5E2%20%5Cvarepsilon%5E2)%20&amp;=%20%5Cmathbb%7BE%7D(Z%5E2)%5Cmathbb%7BE%7D(%5Cvarepsilon%5E2)%20=%20%5Csigma_z%5E2%20%5Csigma_%5Cvarepsilon%5E2%5C%5C%0A%5Cmathbb%7BE%7D(Z%5E2%20U%5E2)%20&amp;=%20%5Cmathbb%7BE%7D(Z%5E2)%5Cmathbb%7BE%7D(U%5E2)%20=%20%5Csigma_z%5E2%20%5Csigma_u%5E2%0A%5Cend%7Baligned%7D%0A"> since <img src="https://latex.codecogs.com/png.latex?Z,%20%5Cvarepsilon"> and <img src="https://latex.codecogs.com/png.latex?U"> are mean zero. Thus, the asymptotic variances of the IV and reduced form estimators are given by <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Ctext%7BAVAR%7D(%5Chat%7B%5Cbeta%7D)%20&amp;=%20%5Cleft(%5Cfrac%7B1%7D%7B%5Cmathbb%7BE%7D%5BZX%5D%7D%5Cright)%5E2%20%5Cmathbb%7BE%7D(Z%5E2%20U%5E2)%20=%20%5Cleft(%5Cfrac%7B1%7D%7B%5Cpi%20%5Csigma_z%5E2%7D%5Cright)%5E2%20%5Csigma_z%5E2%20%5Csigma_u%5E2%20=%20%5Cfrac%7B%5Csigma_u%5E2%7D%7B%5Cpi%5E2%20%5Csigma_z%5E2%7D%5C%5C%0A%5Ctext%7BAVAR%7D(%5Chat%7B%5Cgamma%7D)%20&amp;=%20%5Cleft(%5Cfrac%7B1%7D%7B%5Cmathbb%7BE%7D%5BZ%5E2%5D%7D%5Cright)%5E2%20%5Cmathbb%7BE%7D(Z%5E2%20%5Cvarepsilon%5E2)%20=%20%5Cleft(%20%5Cfrac%7B1%7D%7B%5Csigma_z%5E2%7D%5Cright)%5E2%20%5Csigma_z%5E2%20%5Csigma_%5Cvarepsilon%5E2%20=%20%5Cfrac%7B%5Csigma_%5Cvarepsilon%5E2%7D%7B%5Csigma_z%5E2%7D%0A%5Cend%7Baligned%7D%0A"> since the first stage slope coefficient <img src="https://latex.codecogs.com/png.latex?%5Cpi"> equals <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5BZX%5D/%20%5Ctext%7BVar%7D(Z)">. And now we have our answer! Taking square roots gives <img src="https://latex.codecogs.com/png.latex?%0A%5Csqrt%7B%5Ctext%7BAVAR%7D(%5Chat%7B%5Cbeta%7D)%7D%20=%20%5Cfrac%7B%5Csigma_u%7D%7B%5Cpi%20%5Csigma_z%7D,%20%5Cquad%0A%5Csqrt%7B%5Ctext%7BAVAR%7D(%5Chat%7B%5Cgamma%7D)%7D%20=%20%5Cfrac%7B%5Csigma_%5Cvarepsilon%7D%7B%5Csigma_z%7D%0A"> The respective standard errors are <em>estimators</em> of these quantities, divided by <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7Bn%7D">. Fundamentally, the standard error of the IV estimator is governed by the variation in the <em>structural error</em> <img src="https://latex.codecogs.com/png.latex?U"> while that of the reduced form estimator is governed by the variation in the <em>reduced form error</em> <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon">. It follows that <img src="https://latex.codecogs.com/png.latex?%0A%5Csqrt%7B%5Ctext%7BAVAR%7D(%5Chat%7B%5Cbeta%7D)%7D%20=%20%5Cleft(%5Cfrac%7B%5Csigma_u%7D%7B%5Csigma_%5Cvarepsilon%7D%5Cright)%20%5Ccdot%20%5Cfrac%7B%5Csqrt%7B%5Ctext%7BAVAR%7D(%5Chat%7B%5Cgamma%7D)%7D%7D%7B%5Cpi%7D%0A"> so there <strong>really is</strong> a factor of <img src="https://latex.codecogs.com/png.latex?%5Cpi"> in the relationship between the IV and reduced form standard errors! The trouble is that there is a second factor, <img src="https://latex.codecogs.com/png.latex?%5Csigma_u/%5Csigma_%5Cvarepsilon">, and this <em>does not</em> equal one. Instead, <img src="https://latex.codecogs.com/png.latex?%0A%5Csigma_%5Cvarepsilon%5E2%20=%20%5Ctext%7BVar%7D(%5Cbeta%20V%20+%20U)%20=%20%5Cbeta%5E2%20%5Csigma_v%5E2%20+%20%5Csigma_u%5E2%20+%202%5Cbeta%20%5C,%5Ctext%7BCov%7D(U,V)%0A"> so that <img src="https://latex.codecogs.com/png.latex?%5Csigma_%5Cvarepsilon%5E2%20-%20%5Csigma_u%5E2%20=%20%5Cbeta%5E2%20%5Csigma_v%5E2%20+%202%5Cbeta%20%5Ctext%7BCov%7D(U,V)">. The first term in the difference, <img src="https://latex.codecogs.com/png.latex?%5Cbeta%5E2%20%5Csigma_v%5E2">, is always positive but the second is negative whenever the sign of <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> disagrees with that of <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(U,V)">. This means that <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7B%5Ctext%7BAVAR%7D(%5Chat%7B%5Cbeta%7D)%7D"> can be either larger or smaller than <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7B%5Ctext%7BAVAR%7D(%5Chat%7B%5Cgamma%7D)%7D/%5Cpi"> depending on <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> and on the variances and covariance of <img src="https://latex.codecogs.com/png.latex?(U,V)">. In our simulation <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> and <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(U,V)"> are both positive, so <img src="https://latex.codecogs.com/png.latex?%5Csigma_%5Cvarepsilon%20%3E%20%5Csigma_u">. This is why the naive standard error was too large.</p>
<p>What about our second intuition? Expand <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cgamma%7D"> again, but this time substitute the <em>structural</em> equation <img src="https://latex.codecogs.com/png.latex?Y%20=%20%5Cbeta%20X%20+%20U"> in place of the reduced form: <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Chat%7B%5Cgamma%7D%20&amp;=%20(%5Cunderline%7BZ%7D'%5Cunderline%7BZ%7D)%5E%7B-1%7D(%5Cunderline%7BZ%7D'%5Cunderline%7BY%7D)%20=%20(%5Cunderline%7BZ%7D'%5Cunderline%7BZ%7D)%5E%7B-1%7D%5Cleft%5B%5Cunderline%7BZ%7D'(%5Cbeta%20%5Cunderline%7BX%7D%20+%20%5Cunderline%7BU%7D)%5Cright%5D%5C%5C%0A&amp;=%20%5Cbeta%20(%5Cunderline%7BZ%7D'%5Cunderline%7BZ%7D)%5E%7B-1%7D(%5Cunderline%7BZ%7D'%5Cunderline%7BX%7D)%20+%20(%5Cunderline%7BZ%7D'%5Cunderline%7BZ%7D)%5E%7B-1%7D(%5Cunderline%7BZ%7D'%5Cunderline%7BU%7D)%5C%5C%0A&amp;=%20%5Cbeta%20%5Chat%7B%5Cpi%7D%20+%20(%5Cunderline%7BZ%7D'%5Cunderline%7BZ%7D)%5E%7B-1%7D(%5Cunderline%7BZ%7D'%5Cunderline%7BU%7D)%0A%5Cend%7Baligned%7D%0A"> where <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cpi%7D%20=%20(%5Cunderline%7BZ%7D'%5Cunderline%7BZ%7D)%5E%7B-1%7D(%5Cunderline%7BZ%7D'%5Cunderline%7BX%7D)"> is the first stage OLS slope. This identity holds in <em>every sample</em>. Whatever sampling error <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cpi%7D"> contains is carried along in the term <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20%5Chat%7B%5Cpi%7D">, so dividing through by <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cpi%7D"> replaces it with the constant <img src="https://latex.codecogs.com/png.latex?%5Cbeta">: <img src="https://latex.codecogs.com/png.latex?%0A%5Chat%7B%5Cbeta%7D%20=%20%5Cfrac%7B%5Chat%7B%5Cgamma%7D%7D%7B%5Chat%7B%5Cpi%7D%7D%20=%20%5Cbeta%20+%20%5Cfrac%7B(%5Cunderline%7BZ%7D'%5Cunderline%7BZ%7D)%5E%7B-1%7D(%5Cunderline%7BZ%7D'%5Cunderline%7BU%7D)%7D%7B%5Chat%7B%5Cpi%7D%7D%20=%20%5Cbeta%20+%20%5Cfrac%7B%5Cunderline%7BZ%7D'%5Cunderline%7BU%7D%7D%7B%5Cunderline%7BZ%7D'%5Cunderline%7BX%7D%7D%0A"> The first stage error is now in the denominator, <img src="https://latex.codecogs.com/png.latex?%5Cunderline%7BZ%7D'%5Cunderline%7BX%7D%20=%20%5Cpi%20%5Cunderline%7BZ%7D'%5Cunderline%7BZ%7D%20+%20%5Cunderline%7BZ%7D'%5Cunderline%7BV%7D">, which is of course still random. But the rescaling that sets up the central limit theorem divides it by <img src="https://latex.codecogs.com/png.latex?n">, and <img src="https://latex.codecogs.com/png.latex?%5Cunderline%7BZ%7D'%5Cunderline%7BX%7D/n"> converges in probability to <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(ZX)">, a constant. Its reciprocal is precisely the <img src="https://latex.codecogs.com/png.latex?1/%5Cmathbb%7BE%7D(ZX)"> multiplying the normal limit in our expression for <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7Bn%7D(%5Chat%7B%5Cbeta%7D%20-%20%5Cbeta)"> above.</p>
<p>Finally, we can check to see that our calculations are correct by comparing them to the simulation results from above. The standard errors reported by <code>lm</code> and <code>ivreg</code> are finite-sample counterparts of our asymptotic standard errors, so the same relationship should hold with <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Csigma%7D_u"> and <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Csigma%7D_%5Cvarepsilon"> in place of <img src="https://latex.codecogs.com/png.latex?%5Csigma_u"> and <img src="https://latex.codecogs.com/png.latex?%5Csigma_%5Cvarepsilon">. The reduced form residuals <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cvarepsilon%7D_i"> and the first stage residuals <img src="https://latex.codecogs.com/png.latex?%5Chat%7BV%7D_i"> give us the structural residuals, since <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cvarepsilon%7D_i%20-%20%5Chat%7B%5Cbeta%7D%5Chat%7BV%7D_i%20=%20Y_i%20-%20%5Chat%7B%5Cbeta%7DX_i%20=%20%5Chat%7BU%7D_i">:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">beta_hat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> reduced_form<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> first_stage<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate</span>
<span id="cb6-2">eps_hat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">residuals</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> z))</span>
<span id="cb6-3">u_hat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> eps_hat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> beta_hat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">residuals</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> z))</span>
<span id="cb6-4"></span>
<span id="cb6-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se_by_hand =</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(u_hat) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(eps_hat)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span></span>
<span id="cb6-6">    reduced_form<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>std.error <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> first_stage<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate,</span>
<span id="cb6-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se_ivreg =</span> iv<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>std.error)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>se_by_hand   se_ivreg 
 0.1861929  0.1861929 </code></pre>
</div>
</div>
</div>
</div>
</div>


<div class="no-row-height column-margin column-container"><div id="fn2"><p><sup>2</sup>&nbsp;For more on the various notions of “independence” in econometrics, see <a href="https://www.econometrics.blog/post/why-econometrics-is-confusing-part-ii-the-independence-zoo/">this post</a>.</p></div></div><div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;To keep the math simple, I implicitly assume here that <img src="https://latex.codecogs.com/png.latex?(Y,X,Z)"> are mean zero so that there is no need to include an intercept in either equation. Equivalently, we could simply subtract the mean from each random variable before proceeding.</p></div></div></section>


 ]]></description>
  <category>econometrics</category>
  <category>causal inference</category>
  <category>puzzler</category>
  <guid>https://www.econometrics.blog/post/econometrics-puzzler-4-rescaling-the-reduced-form/</guid>
  <pubDate>Fri, 14 Aug 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Econometrics Puzzler #3: I’ve Got the Power</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/econometrics-puzzler-3-ive-got-the-power/</link>
  <description><![CDATA[ 




<p>Today’s challenge is to do a power calculation <em>in your head</em>. No paper, no pencils, no normal tables, and no statistical software packages are allowed!</p>
<p>Suppose you observe <img src="https://latex.codecogs.com/png.latex?n"> independent draws from each of two normally distributed populations. For simplicity, assume that both populations have a common variance <img src="https://latex.codecogs.com/png.latex?%5Csigma%5E2">, assumed known. You plan to carry out a test of the null hypothesis that the population means are equal, <img src="https://latex.codecogs.com/png.latex?H_0%5Ccolon%20%5Cmu_1%20=%20%5Cmu_2">, against the two-sided alternative with significance level <img src="https://latex.codecogs.com/png.latex?%5Calpha%20=%200.05">. How many observations <img src="https://latex.codecogs.com/png.latex?n"> do you need from each population so that the approximate power of your test will be 0.8?</p>
<p>In case it’s been a while since you’ve thought about power calculations, remember that statistical power is the probability of rejecting the null <em>given that it is false</em>. As such, your answer will depend on <img src="https://latex.codecogs.com/png.latex?%5Csigma%5E2">, <img src="https://latex.codecogs.com/png.latex?%5Cmu_1">, and <img src="https://latex.codecogs.com/png.latex?%5Cmu_2">.</p>
<p>Until a few months ago, I would have protested that this puzzler is completely unfair. How am I supposed to invert the normal CDF in my head?! But it turns out that, at least in this special case, there is a simple and accurate approximation that’s worth committing to memory. Before I show it to you, take out a pencil and paper — the ban is lifted for this part — and see if you can discover it for yourself.</p>
<section id="solution" class="level2 page-columns page-full" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="solution"><span class="header-section-number">1</span> Solution</h2>
<div class="solution callout callout-style-simple callout-note no-icon callout-titled" title="Solution">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Solution
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<section id="lehrs-rule" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="lehrs-rule"><span class="header-section-number">2</span> Lehr’s Rule</h2>
<p>In a short note from <em>Statistics in Medicine</em>, <a href="https://doi.org/10.1002/sim.4780110811">Lehr (1992)</a> suggested that researchers who regularly design randomized controlled trials should commit the following rule-of-thumb to memory:</p>
<blockquote class="blockquote">
<p>For a two-sample, two-tailed t-test with alpha level 0.05, the following relation yields an approximate power of 0.80 <img src="https://latex.codecogs.com/png.latex?%0An%20=%2016%20s%5E2%20/%20d%5E2%0A"></p>
</blockquote>
<p>Re-writing this expression slightly to match the notation from above and make it slightly easier to remember gives <img src="https://latex.codecogs.com/png.latex?%0An%20=%20%5Cfrac%7B16%7D%7B%5CDelta%5E2%7D,%20%5Cquad%20%5CDelta%20%5Cequiv%20%5Cfrac%7B%5Cmu_1%20-%20%5Cmu_2%7D%7B%5Csigma%7D%0A"> The quantity that I have called <img src="https://latex.codecogs.com/png.latex?%5CDelta"> is a unitless measure of <em>effect size</em>: it compares the difference between populations, <img src="https://latex.codecogs.com/png.latex?%5Cmu_1%20-%20%5Cmu_2">, to the variability within each population, <img src="https://latex.codecogs.com/png.latex?%5Csigma">.<sup>1</sup> Notice that the sample size required is inversely proportional to the <em>square</em> of the effect size. This means that very large samples are needed to detect small effects. For <img src="https://latex.codecogs.com/png.latex?%5CDelta%20=%201"> (a large effect size) we only need 16 observations per group but for <img src="https://latex.codecogs.com/png.latex?%5CDelta%20=%200.1"> (a very small effect size) we need a whopping 1600 observations per group!</p>
</section>
<section id="why-does-it-work" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="why-does-it-work"><span class="header-section-number">3</span> Why does it work?</h2>
<p>Here’s a proof of Lehr’s rule. Let <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D_1"> and <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D_2"> be the sample means corresponding to each population. Then <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D_1%20%5Csim%20%5Ctext%7BN%7D(%5Cmu_1,%20%5Csigma%5E2/n)"> independently of <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D_2%20%5Csim%20%5Ctext%7BN%7D(%5Cmu_2,%20%5Csigma%5E2/n)"> and our test statistic can be written as <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0AT%20&amp;%5Cequiv%20%5Cfrac%7B%5Cbar%7BX%7D_1%20-%20%5Cbar%7BX%7D_2%7D%7B%5Csqrt%7B2%5Csigma%5E2%20/%20n%7D%7D%20=%5Cfrac%7B(%5Cbar%7BX%7D_1%20-%20%5Cbar%7BX%7D_2)%20-%20(%5Cmu_1%20-%20%5Cmu_2)%7D%7B%5Csqrt%7B2%5Csigma%5E2%20/%20n%7D%7D%20%20+%20%5Cfrac%7B%5Cmu_1%20-%20%5Cmu_2%7D%7B%5Csqrt%7B2%20%5Csigma%5E2/n%7D%7D%5C%5C%0A&amp;=%20Z%20+%20%5CDelta%20%5Csqrt%7Bn/2%7D%0A%5Cend%7Baligned%7D%0A"> where <img src="https://latex.codecogs.com/png.latex?Z%20%5Csim%20%5Ctext%7BN%7D(0,1)">. Now let <img src="https://latex.codecogs.com/png.latex?c"> be the critical value for our two-sided test with significance level <img src="https://latex.codecogs.com/png.latex?%5Calpha">. We reject the null if <img src="https://latex.codecogs.com/png.latex?%7CT%7C%20%3E%20c">, in other words if <img src="https://latex.codecogs.com/png.latex?T%3Ec"> or <img src="https://latex.codecogs.com/png.latex?T%20%3C%20-%20c">. Thus the power of our test is <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Ctext%7BPower%7D%20&amp;=%20%5Cmathbb%7BP%7D(Z%20+%20%5CDelta%20%5Csqrt%7Bn/2%7D%20%3E%20c)%20+%20%5Cmathbb%7BP%7D(Z%20+%20%5CDelta%20%5Csqrt%7Bn%20/%202%7D%20%3C%20-c)%5C%5C%0A&amp;=%201%20-%20%5CPhi(c%20-%20%5CDelta%20%5Csqrt%7Bn%20/%202%7D)%20+%20%5CPhi(-c%20-%20%5CDelta%20%5Csqrt%7Bn%20/%202%7D)%0A%5Cend%7Baligned%7D%0A"> where <img src="https://latex.codecogs.com/png.latex?%5CPhi(%5Ccdot)"> denotes the CDF of a standard normal random variable. Rearranging, <img src="https://latex.codecogs.com/png.latex?%0A1%20-%20%5Ctext%7BPower%7D%20=%20%5CPhi(c%20-%20%5CDelta%20%5Csqrt%7Bn%20/%202%7D)%20-%20%5CPhi(-c%20-%20%5CDelta%20%5Csqrt%7Bn/2%7D)%0A"> It turns out that the right hand side of this expression would be <em>completely unchanged</em> if we replaced <img src="https://latex.codecogs.com/png.latex?%5CDelta"> with <img src="https://latex.codecogs.com/png.latex?-%5CDelta">, so we can assume without loss of generality that <img src="https://latex.codecogs.com/png.latex?%5CDelta"> is positive.<sup>2</sup> This makes intuitive sense: power should depend on the <em>difference of means</em> but it should not depend on which we labeled “1” and which we labeled “2”, which determines the sign of <img src="https://latex.codecogs.com/png.latex?%5CDelta">.</p>
<p>Now comes the approximation. The second term on the right hand side of our expression for <img src="https://latex.codecogs.com/png.latex?1%20-%20%5Ctext%7BPower%7D"> is <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(T%20%3C%20-c)">. Since <img src="https://latex.codecogs.com/png.latex?%5CDelta"> is <em>positive</em>, this is the probability of rejecting the null <em>in the wrong direction</em>. In other words, it is the probability of rejecting <img src="https://latex.codecogs.com/png.latex?%5Cmu_1%20=%20%5Cmu_2"> when <img src="https://latex.codecogs.com/png.latex?%5Cmu_1%20%3E%20%5Cmu_2"> by observing a test statistic that takes on a large <em>negative</em> value. We will approximate this probability by <strong>setting it equal to zero</strong>. For a test with a reasonable amount of power–remember that we are targeting 80% power–this is a very good approximation.<sup>3</sup> If you worry that there’s something circular in my logic here, fear not: we will return to this point at the end! But for the moment, we will assume that it’s safe to write <img src="https://latex.codecogs.com/png.latex?%0A1%20-%20%5Ctext%7BPower%7D%20%5Capprox%20%5CPhi(c%20-%20%5CDelta%20%5Csqrt%7Bn%20/%202%7D)%20.%0A"> Applying the standard normal quantile function <img src="https://latex.codecogs.com/png.latex?%5CPhi%5E%7B-1%7D"> to both sides gives <img src="https://latex.codecogs.com/png.latex?%0A%5CPhi%5E%7B-1%7D(1%20-%20%5Ctext%7BPower%7D)%20%20%5Capprox%20c%20-%20%5CDelta%20%5Csqrt%7Bn%20/%202%7D%0A"> and rearranging the result gives <img src="https://latex.codecogs.com/png.latex?%0An%20%5Capprox%202%20%5Cleft%5B%5Cfrac%7Bc%20-%20%5CPhi%5E%7B-1%7D(1%20-%20%5Ctext%7BPower%7D)%7D%7B%5CDelta%7D%5Cright%5D%5E2.%0A"> Finally, substituting <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BPower%7D%20=%200.8"> and <img src="https://latex.codecogs.com/png.latex?c%20=%201.96">, <img src="https://latex.codecogs.com/png.latex?%0An%20%5Capprox%202%20%5Cleft%5B%5Cfrac%7B1.96%20-%20%5CPhi%5E%7B-1%7D(0.2)%7D%7B%5CDelta%7D%20%5Cright%5D%5E2%20%5Capprox%202%5Cleft%5B%20%5Cfrac%7B1.96%20-%20(-0.84)%7D%7B%5CDelta%7D%20%5Cright%5D%5E2%20%5Capprox%20%5Cfrac%7B15.7%7D%7B%5CDelta%5E2%7D.%0A"> We obtain Lehr’s rule by rounding <img src="https://latex.codecogs.com/png.latex?15.7"> up to <img src="https://latex.codecogs.com/png.latex?16">.</p>
<p>Now what about that <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(T%20%3C%20-%20c)"> term we discarded above? Is it really negligible? Substituting <img src="https://latex.codecogs.com/png.latex?c%20=%201.96"> and <img src="https://latex.codecogs.com/png.latex?%5CPhi%5E%7B-1%7D(1%20-%20%5Ctext%7BPower%7D)%20=%20-0.84"> into our intermediate step while solving for <img src="https://latex.codecogs.com/png.latex?n"> above gives <img src="https://latex.codecogs.com/png.latex?%5CDelta%20%5Csqrt%7Bn/2%7D%20=%201.96%20+%200.84%20=%202.8">. This automatically holds for any value of <img src="https://latex.codecogs.com/png.latex?%5CDelta"> since Lehr’s rule automatically increases <img src="https://latex.codecogs.com/png.latex?n"> to offset a smaller effect size. Thus, the discarded term equals <img src="https://latex.codecogs.com/png.latex?%5CPhi(-1.96%20-%202.8)%20=%20%5CPhi(-4.76)%20%5Capprox%2010%5E%7B-6%7D"> so it is perfectly safe to neglect it!</p>
<p>Notice that we used two approximations in this derivation, but both of them are <em>conservative</em>. In other words, Lehr’s rule slightly <em>overstates</em> the required sample size to achieve 80% power in this example.</p>
</section>
</div>
</div>
</div>


<div class="no-row-height column-margin column-container"><div id="fn3"><p><sup>3</sup>&nbsp;For a test with low power, on the other hand, rejections <em>in the wrong direction</em> present a serious problem, leading to the notion of a “Type S” error as described by <a href="https://ditraglia.com/erm/Gelman-Carlin-2014.pdf">Gelman &amp; Carlin (2014)</a>.</p></div></div><div class="no-row-height column-margin column-container"><div id="fn2"><p><sup>2</sup>&nbsp;To see why substitute <img src="https://latex.codecogs.com/png.latex?-%5CDelta"> in place of <img src="https://latex.codecogs.com/png.latex?%5CDelta"> and use the symmetry of the normal distribution. In particular, apply <img src="https://latex.codecogs.com/png.latex?%5CPhi(-x)%20=%201%20-%20%5CPhi(x)"> <em>twice</em>.</p></div></div><div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;This is the population version of <a href="https://en.wikipedia.org/wiki/Effect_size#Cohen%27s_d">Cohen’s <img src="https://latex.codecogs.com/png.latex?d"></a>. Be careful not to confuse it with the <img src="https://latex.codecogs.com/png.latex?d"> in Lehr’s formula, which rather confusingly denotes the <em>raw</em> difference of means <img src="https://latex.codecogs.com/png.latex?%5Cmu_1%20-%20%5Cmu_2">.</p></div></div></section>


 ]]></description>
  <category>statistics</category>
  <category>puzzler</category>
  <guid>https://www.econometrics.blog/post/econometrics-puzzler-3-ive-got-the-power/</guid>
  <pubDate>Thu, 06 Aug 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Complex Step Differentiation</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/complex-step-differentiation/</link>
  <description><![CDATA[ 




<p>Sometimes we need a good approximation to the derivative <img src="https://latex.codecogs.com/png.latex?f'(x)"> of a real-valued function <img src="https://latex.codecogs.com/png.latex?f"> at some real value <img src="https://latex.codecogs.com/png.latex?x">. So here’s a fun fact that you may not know. If <img src="https://latex.codecogs.com/png.latex?%5CDelta"> is a small positive number and <img src="https://latex.codecogs.com/png.latex?f"> can be evaluated at a complex argument, then <img src="https://latex.codecogs.com/png.latex?%0Af'(x)%20%5Capprox%20%5Cfrac%7B%5Ctext%7BIm%7D%5Bf(x%20+%20%5CDelta%20i)%5D%7D%7B%5CDelta%7D%0A"> where <img src="https://latex.codecogs.com/png.latex?i"> is the imaginary unit and <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BIm%7D(z)"> denotes the imaginary part of a complex number. This unexpected but highly accurate approximation is called <strong>complex step differentiation</strong>. The method dates back to <a href="https://epubs.siam.org/doi/10.1137/0704019">Lyness and Moler (1967)</a>; <a href="https://researchrepository.wvu.edu/faculty_publications/426/">Squire and Trapp (1998)</a> give a concise modern exposition.</p>
<p>To see why it works, Taylor-expand <img src="https://latex.codecogs.com/png.latex?f(x%20+%20%5CDelta%20i)"> around <img src="https://latex.codecogs.com/png.latex?x"> yielding <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0Af(x%20+%20%5CDelta%20i)%20&amp;=%20f(x)%20+%20f'(x)%5C;%20i%5CDelta%20+%20f''(x)%20%5Cfrac%7B(i%5CDelta)%5E2%7D%7B2!%7D%20+%20f'''(x)%20%5Cfrac%7B(i%5CDelta)%5E3%7D%7B3!%7D%20+%20%5Ccdots%5C%5C%0A&amp;=%20%5Cleft%5Bf(x)%20-%20f''(x)%20%5Cfrac%7B%5CDelta%5E2%7D%7B2%7D%5Cright%5D+%20%5Cleft%5Bf'(x)%5C;%20%5CDelta%20-%20f'''(x)%20%5Cfrac%7B%5CDelta%5E3%7D%7B3!%7D%5Cright%5Di%20+%20%5Ccdots%0A%5Cend%7Baligned%7D%0A"> since <img src="https://latex.codecogs.com/png.latex?i%5E2%20=%20-1"> and <img src="https://latex.codecogs.com/png.latex?i%5E3%20=%20-i">. Taking the imaginary part and dividing by <img src="https://latex.codecogs.com/png.latex?%5CDelta">, <img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7B%5Ctext%7BIm%7D%5Bf(x%20+%20%5CDelta%20i)%5D%7D%7B%5CDelta%7D%20=%20f'(x)%20-%20f'''(x)%20%5Cfrac%7B%5CDelta%5E2%7D%7B3!%7D%20+%20%5Ccdots%0A"> so complex step differentiation approximates <img src="https://latex.codecogs.com/png.latex?f'(x)"> to order <img src="https://latex.codecogs.com/png.latex?O(%5CDelta%5E2)">.</p>
<p>Sure it’s a cute trick. But why go to the trouble of introducing complex numbers? Complex step differentiation turns out to be much better behaved from a numerical perspective than the simple difference approximation <img src="https://latex.codecogs.com/png.latex?%0Af'(x)%20%5Capprox%20%5Cfrac%7Bf(x%20+%20%5CDelta)%20-%20f(x)%7D%7B%5CDelta%7D%0A"> or the symmetric difference <img src="https://latex.codecogs.com/png.latex?%0Af'(x)%20%5Capprox%20%5Cfrac%7Bf(x%20+%20%5CDelta)%20-%20f(x%20-%20%5CDelta)%7D%7B2%5CDelta%7D.%0A"> In the remainder of this post, I’ll explain why. Before beginning, I’ll start with a brief overview of complex numbers in R. If this material is already familiar to you, skip ahead to the following section.</p>
<section id="complex-numbers-in-r" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="complex-numbers-in-r"><span class="header-section-number">1</span> Complex Numbers in R</h2>
<p>R provides basic functionality for working with complex numbers, documented in the helpful <code>?complex</code>. The imaginary unit in R is denoted by <code>1i</code>. Don’t try <code>i</code> by itself because that won’t work:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">i <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># this doesn't work</span></span></code></pre></div></div>
<div class="cell-output cell-output-error">
<pre><code>Error:
! object 'i' not found</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>i <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># this works</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0+1i</code></pre>
</div>
</div>
<p>To write a complex number, simply use <code>+</code> to separate the real and imaginary parts, e.g.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>i</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3+4i</code></pre>
</div>
</div>
<p>A common error when learning R is to write code with “implied multiplication” e.g.&nbsp;<code>4x</code> (wrong) rather than <code>4 * x</code> (right). But don’t be tempted to try <code>4 * i</code> since, again, <code>i</code> is <em>not</em> the imaginary unit in R:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> i <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># this doesn't work </span></span></code></pre></div></div>
<div class="cell-output cell-output-error">
<pre><code>Error:
! object 'i' not found</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>i <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># this works</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0+4i</code></pre>
</div>
</div>
<p>To demonstrate R’s basic functionality for working with complex numbers, let’s specify a complex number <img src="https://latex.codecogs.com/png.latex?z"> in Cartesian coordinates: <img src="https://latex.codecogs.com/png.latex?z%20=%203%20+%204i">. The real part is <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BRe%7D(z)%20=%203">, the imaginary part is <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BIm%7D(z)%20=%204">, the modulus is <img src="https://latex.codecogs.com/png.latex?%7Cz%7C%20=%20%5Csqrt%7B3%5E2%20+%204%5E2%7D%20=%205">, and the complex conjugate is <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bz%7D%20=%203%20-%204i">. All of these operations are available in R:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>i</span>
<span id="cb11-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Re</span>(z)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Im</span>(z)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 4</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Mod</span>(z)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 5</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(z) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># same as Mod(z) for a complex number z</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 5</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Conj</span>(z)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3-4i</code></pre>
</div>
</div>
<p>All of the basic operations <code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>, and <code>^</code> work on complex numbers and are vectorized</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1">w <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>i</span>
<span id="cb21-2">z <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> w</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0+3i</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">z <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> w</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 6+5i</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1">z <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> w</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -5-15i</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1">z <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> w</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -1.3-0.9i</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1">z<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -117+44i</code></pre>
</div>
</div>
<p>as are functions such as <code>log()</code>, <code>exp()</code>, <code>sin()</code>, <code>cos()</code>, etc. If you supply them a complex input, they will return a complex output; if you supply them a real input, they will return a real output. This behavior explains a common gotcha: <code>sqrt(-1)</code> doesn’t work because there’s no real number whose square root is <code>-1</code> but <code>sqrt(-1 + 0i)</code> does work:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># this doesn't work</span></span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning in sqrt(-1): NaNs produced</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] NaN</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>i)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># this works</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0+1i</code></pre>
</div>
</div>
<p>Another common gotcha is trying to apply functions like <code>max()</code> and <code>min()</code> or operators like <code>&lt;</code> and <code>&gt;</code> to complex numbers. Since <a href="https://en.wikipedia.org/wiki/Ordered_field">complex numbers aren’t ordered</a>, this doesn’t work:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(z, w))</span></code></pre></div></div>
<div class="cell-output cell-output-error">
<pre><code>Error in `min()`:
! invalid 'type' (complex) of argument</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1">z <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> w</span></code></pre></div></div>
<div class="cell-output cell-output-error">
<pre><code>Error in `z &lt; w`:
! invalid comparison with complex values</code></pre>
</div>
</div>
<p>but testing for equality / non-equality does work:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1">z <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> w</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] FALSE</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb42" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1">z <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> w</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
</div>
</section>
<section id="complex-step-differentiation-in-action" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="complex-step-differentiation-in-action"><span class="header-section-number">2</span> Complex Step Differentiation in Action</h2>
<p>Let’s test out complex step differentiation with a simple example. Let <img src="https://latex.codecogs.com/png.latex?f(x)%20=%20x%5E%7B9/2%7D"> and suppose we want to compute <img src="https://latex.codecogs.com/png.latex?f'(1.5)">. This one is easy to compute analytically: <img src="https://latex.codecogs.com/png.latex?%0Af'(x)%20=%20%5Cfrac%7B9%7D%7B2%7D%20x%5E%7B7/2%7D%20%5Cimplies%20f'(1.5)%20=%204.5%20%5Ctimes(1.5)%5E%7B3.5%7D%0A"> so we can check how accurate different numerical approximations turn out to be. Computing the derivative directly gives</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb44" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb44-1">direct <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.5</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</span>)</span>
<span id="cb44-2">direct</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 18.60081</code></pre>
</div>
</div>
<p>Now we’ll compare this value against three numerical derivatives: the “simple difference” approach, the “symmetric difference” approach, and the complex step approach.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb46" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb46-1">f <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.5</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the function to differentiate</span></span>
<span id="cb46-2"></span>
<span id="cb46-3">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the point where we'll evaluate f'(x)</span></span></code></pre></div></div>
</div>
<p>Since R functions are vectorized, we can evaluate the quality of each approximation over many values of <img src="https://latex.codecogs.com/png.latex?%5CDelta"> at once by setting up a vector of progressively smaller positive values:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb47" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb47-1">delta <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>)</span>
<span id="cb47-2"></span>
<span id="cb47-3">simple <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f</span>(x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> delta) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f</span>(x)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> delta</span>
<span id="cb47-4"></span>
<span id="cb47-5">symmetric <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f</span>(x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> delta) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f</span>(x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> delta)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> delta)</span>
<span id="cb47-6"></span>
<span id="cb47-7">complex_step <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Im</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f</span>(x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> delta <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>i)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> delta</span></code></pre></div></div>
</div>
<p>Now we’ll make two tables: the first containing raw results—the approximate value of <img src="https://latex.codecogs.com/png.latex?f'(x)">—and the second containing the relative error of the approximation in percentage points. When reading the first table, recall from above that direct calculation gives <img src="https://latex.codecogs.com/png.latex?f'(1.5)%20%5Capprox%2018.60081">.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb48" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb48-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(delta, simple, symmetric, complex_step) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb48-2">  knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: right;">delta</th>
<th style="text-align: right;">simple</th>
<th style="text-align: right;">symmetric</th>
<th style="text-align: right;">complex_step</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">1e-01</td>
<td style="text-align: right;">20.89450</td>
<td style="text-align: right;">18.72139</td>
<td style="text-align: right;">18.48027</td>
</tr>
<tr class="even">
<td style="text-align: right;">1e-02</td>
<td style="text-align: right;">18.81903</td>
<td style="text-align: right;">18.60202</td>
<td style="text-align: right;">18.59961</td>
</tr>
<tr class="odd">
<td style="text-align: right;">1e-03</td>
<td style="text-align: right;">18.62253</td>
<td style="text-align: right;">18.60082</td>
<td style="text-align: right;">18.60080</td>
</tr>
<tr class="even">
<td style="text-align: right;">1e-04</td>
<td style="text-align: right;">18.60298</td>
<td style="text-align: right;">18.60081</td>
<td style="text-align: right;">18.60081</td>
</tr>
<tr class="odd">
<td style="text-align: right;">1e-05</td>
<td style="text-align: right;">18.60103</td>
<td style="text-align: right;">18.60081</td>
<td style="text-align: right;">18.60081</td>
</tr>
<tr class="even">
<td style="text-align: right;">1e-06</td>
<td style="text-align: right;">18.60083</td>
<td style="text-align: right;">18.60081</td>
<td style="text-align: right;">18.60081</td>
</tr>
<tr class="odd">
<td style="text-align: right;">1e-07</td>
<td style="text-align: right;">18.60081</td>
<td style="text-align: right;">18.60081</td>
<td style="text-align: right;">18.60081</td>
</tr>
<tr class="even">
<td style="text-align: right;">1e-08</td>
<td style="text-align: right;">18.60081</td>
<td style="text-align: right;">18.60081</td>
<td style="text-align: right;">18.60081</td>
</tr>
<tr class="odd">
<td style="text-align: right;">1e-09</td>
<td style="text-align: right;">18.60081</td>
<td style="text-align: right;">18.60081</td>
<td style="text-align: right;">18.60081</td>
</tr>
<tr class="even">
<td style="text-align: right;">1e-10</td>
<td style="text-align: right;">18.60081</td>
<td style="text-align: right;">18.60081</td>
<td style="text-align: right;">18.60081</td>
</tr>
<tr class="odd">
<td style="text-align: right;">1e-11</td>
<td style="text-align: right;">18.60077</td>
<td style="text-align: right;">18.60081</td>
<td style="text-align: right;">18.60081</td>
</tr>
<tr class="even">
<td style="text-align: right;">1e-12</td>
<td style="text-align: right;">18.60201</td>
<td style="text-align: right;">18.60245</td>
<td style="text-align: right;">18.60081</td>
</tr>
<tr class="odd">
<td style="text-align: right;">1e-13</td>
<td style="text-align: right;">18.58069</td>
<td style="text-align: right;">18.58513</td>
<td style="text-align: right;">18.60081</td>
</tr>
<tr class="even">
<td style="text-align: right;">1e-14</td>
<td style="text-align: right;">18.56293</td>
<td style="text-align: right;">18.56293</td>
<td style="text-align: right;">18.60081</td>
</tr>
<tr class="odd">
<td style="text-align: right;">1e-15</td>
<td style="text-align: right;">20.42810</td>
<td style="text-align: right;">20.42810</td>
<td style="text-align: right;">18.60081</td>
</tr>
</tbody>
</table>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb49" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb49-1">get_rel_error <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x, truth) <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> truth) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(truth)</span>
<span id="cb49-2"></span>
<span id="cb49-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(</span>
<span id="cb49-4">  delta,</span>
<span id="cb49-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">simple =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_rel_error</span>(simple, direct),</span>
<span id="cb49-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">symmetric =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_rel_error</span>(symmetric, direct),</span>
<span id="cb49-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">complex_step =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_rel_error</span>(complex_step, direct)</span>
<span id="cb49-8">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb49-9">  knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)))</span></code></pre></div></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: right;">delta</th>
<th style="text-align: right;">simple</th>
<th style="text-align: right;">symmetric</th>
<th style="text-align: right;">complex_step</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">1e-01</td>
<td style="text-align: right;">12.3311</td>
<td style="text-align: right;">0.6483</td>
<td style="text-align: right;">0.6480</td>
</tr>
<tr class="even">
<td style="text-align: right;">1e-02</td>
<td style="text-align: right;">1.1732</td>
<td style="text-align: right;">0.0065</td>
<td style="text-align: right;">0.0065</td>
</tr>
<tr class="odd">
<td style="text-align: right;">1e-03</td>
<td style="text-align: right;">0.1167</td>
<td style="text-align: right;">0.0001</td>
<td style="text-align: right;">0.0001</td>
</tr>
<tr class="even">
<td style="text-align: right;">1e-04</td>
<td style="text-align: right;">0.0117</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0000</td>
</tr>
<tr class="odd">
<td style="text-align: right;">1e-05</td>
<td style="text-align: right;">0.0012</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0000</td>
</tr>
<tr class="even">
<td style="text-align: right;">1e-06</td>
<td style="text-align: right;">0.0001</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0000</td>
</tr>
<tr class="odd">
<td style="text-align: right;">1e-07</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0000</td>
</tr>
<tr class="even">
<td style="text-align: right;">1e-08</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0000</td>
</tr>
<tr class="odd">
<td style="text-align: right;">1e-09</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0000</td>
</tr>
<tr class="even">
<td style="text-align: right;">1e-10</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0000</td>
</tr>
<tr class="odd">
<td style="text-align: right;">1e-11</td>
<td style="text-align: right;">0.0003</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0000</td>
</tr>
<tr class="even">
<td style="text-align: right;">1e-12</td>
<td style="text-align: right;">0.0064</td>
<td style="text-align: right;">0.0088</td>
<td style="text-align: right;">0.0000</td>
</tr>
<tr class="odd">
<td style="text-align: right;">1e-13</td>
<td style="text-align: right;">0.1082</td>
<td style="text-align: right;">0.0843</td>
<td style="text-align: right;">0.0000</td>
</tr>
<tr class="even">
<td style="text-align: right;">1e-14</td>
<td style="text-align: right;">0.2037</td>
<td style="text-align: right;">0.2037</td>
<td style="text-align: right;">0.0000</td>
</tr>
<tr class="odd">
<td style="text-align: right;">1e-15</td>
<td style="text-align: right;">9.8237</td>
<td style="text-align: right;">9.8237</td>
<td style="text-align: right;">0.0000</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>The simple difference approach is clearly less accurate than both the symmetric difference and complex step approach, particularly at larger values of <img src="https://latex.codecogs.com/png.latex?%5CDelta">. And while it may seem that there’s not much to choose between these latter two approximations, look carefully at what happens as <img src="https://latex.codecogs.com/png.latex?%5CDelta"> gets smaller. Eventually the relative error of the simple and symmetric difference approaches <strong>starts to increase</strong>! For example, the symmetric difference approximation is better with <img src="https://latex.codecogs.com/png.latex?%5CDelta%20=%200.1"> (relative error <img src="https://latex.codecogs.com/png.latex?%3C1%5C%25">) than it is at <img src="https://latex.codecogs.com/png.latex?%5CDelta%20=%2010%5E%7B-15%7D"> (relative error <img src="https://latex.codecogs.com/png.latex?%5Capprox%2010%5C%25">).</p>
<p>How can this be? The derivative is <em>defined</em> as the limit of the simple difference approximation as <img src="https://latex.codecogs.com/png.latex?%5CDelta%20%5Crightarrow%200">. So how can the approximation get <em>worse</em> if <img src="https://latex.codecogs.com/png.latex?%5CDelta"> becomes smaller?</p>
</section>
<section id="catastrophic-cancellation" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="catastrophic-cancellation"><span class="header-section-number">3</span> Catastrophic Cancellation</h2>
<p>The culprit is <a href="https://en.wikipedia.org/wiki/Catastrophic_cancellation">catastrophic cancellation</a>. As explained in an <a href="https://www.econometrics.blog/post/street-fighting-numerical-analysis-part-1/">earlier post</a>, computers cannot represent all real numbers exactly. Instead they use <a href="https://ngrok.com/blog/quantization#how-do-computers-store-numbers">floating point numbers</a> as an approximation. So when we compute a difference like <img src="https://latex.codecogs.com/png.latex?f(x%20+%20%5CDelta)%20-%20f(x)"> what we’re <em>really</em> computing is the difference of two approximations. The problem is that even when <img src="https://latex.codecogs.com/png.latex?A"> is a very good approximation to <img src="https://latex.codecogs.com/png.latex?f(x%20+%20%5CDelta)"> and <img src="https://latex.codecogs.com/png.latex?B"> is a very good approximation to <img src="https://latex.codecogs.com/png.latex?f(x)">, the difference <img src="https://latex.codecogs.com/png.latex?A%20-%20B"> may be a <em>poor approximation</em> to <img src="https://latex.codecogs.com/png.latex?f(x%20+%20%5CDelta)%20-%20f(x)">. This is an unfortunate property of subtraction, nicely illustrated in <a href="https://www.johndcook.com/blog/2025/07/20/interest-tech-note/">this post</a> by John D. Cook.</p>
<p>A double in R is accurate to around 16 decimal places. For small values of <img src="https://latex.codecogs.com/png.latex?%5CDelta">, <img src="https://latex.codecogs.com/png.latex?f(x%20+%20%5CDelta)"> and <img src="https://latex.codecogs.com/png.latex?f(x)"> agree in nearly all of those digits. Subtracting them eliminates the digits that match, leaving behind what is effectively rounding noise. Dividing by <img src="https://latex.codecogs.com/png.latex?%5CDelta"> then magnifies this noise. For example, since <img src="https://latex.codecogs.com/png.latex?f'(x)"> in our example is around <img src="https://latex.codecogs.com/png.latex?18.60081"> when we compute <img src="https://latex.codecogs.com/png.latex?f(x%20+%2010%5E%7B-15%7D)%20-%20f(x)"> we should obtain a result of approximately <img src="https://latex.codecogs.com/png.latex?1.860081%5Ctimes%2010%5E%7B-14%7D"> but instead we get</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb50" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb50-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%.20f"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f</span>(x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-15</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f</span>(x))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "0.00000000000002042810"</code></pre>
</div>
</div>
<p>The first couple of digits are in the ballpark, but the remaining ones are just noise.<br>
The symmetric difference formula has the same problem because it too relies on a subtraction of two approximate values in the numerator:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb52" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb52-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%.20f"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f</span>(x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-15</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f</span>(x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-15</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "0.00000000000004085621"</code></pre>
</div>
</div>
<p>Here we’d expect a result of around <img src="https://latex.codecogs.com/png.latex?3.720162%20%5Ctimes%2010%5E%7B-14%7D"> and, again, every digit after the first two is just noise.</p>
<p>In contrast, complex step differentiation <em>has no subtraction in the numerator</em>. This means that it is not subject to catastrophic cancellation. As <img src="https://latex.codecogs.com/png.latex?%5CDelta"> becomes smaller, the approximation just keeps improving—down to roughly machine precision. The cost is that <img src="https://latex.codecogs.com/png.latex?f"> must be <a href="https://en.wikipedia.org/wiki/Analytic_function">analytic</a> at <img src="https://latex.codecogs.com/png.latex?x"> and evaluable at complex inputs. Most smooth functions you’ll encounter qualify, but those involving <img src="https://latex.codecogs.com/png.latex?%7Cx%7C">, <img src="https://latex.codecogs.com/png.latex?%5Cmax">, <img src="https://latex.codecogs.com/png.latex?%5Cmin">, or indicator functions generally don’t.</p>
</section>
<section id="dont-roll-your-own" class="level2" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="dont-roll-your-own"><span class="header-section-number">4</span> Don’t Roll Your Own</h2>
<p>In this post I computed the complex step derivative by hand. That’s useful for understanding the method, but it’s a bad idea in practice. Whenever you can, you should rely on high-quality existing libraries to implement numerical methods. Numerical analysis is a deeply complicated subject and we are but lowly econometricians! Fortunately for us, the <code>grad()</code> function from the <code>numDeriv</code> R package implements complex step differentiation as one of its three methods:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb54" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb54-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(numDeriv)</span>
<span id="cb54-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grad</span>(f, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'complex'</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 18.60081</code></pre>
</div>
</div>
<p>So the next time you need to differentiate something numerically, remember the value in making things more <em>complex</em> than they need to be!</p>


</section>

 ]]></description>
  <category>computing</category>
  <guid>https://www.econometrics.blog/post/complex-step-differentiation/</guid>
  <pubDate>Sun, 26 Apr 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>A Makeover for econometrics.blog</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/makeover-for-econometrics-blog/</link>
  <description><![CDATA[ 




<p>Today <a href="https://www.econometrics.blog/">econometrics.blog</a> got a long-overdue update from <a href="https://yihui.org/blogdown/">blogdown</a> to <a href="https://quarto.org">Quarto</a>. Thanks to <a href="https://www.anthropic.com/product/claude-code">Claude Code</a>, the transition was seamless: all existing links are preserved, along with the <a href="https://utteranc.es">Utterances</a> comment sections.</p>
<p>During the upgrade I also corrected an embarrassingly large number of typos across the site and added privacy-respecting site analytics using <a href="https://goatcounter.com">GoatCounter</a>. To find out whether econometrics has gone viral at long last, check out the <a href="https://econometrics.goatcounter.com">public dashboard</a>. Where possible, references to the econometrics literature now point to a paper’s <a href="https://ideas.repec.org">IDEAS/RePEc</a> entry, where you can find citation information, a link to the journal website, and any freely-available working paper versions.</p>
<p>I hope you enjoy the new look. If you notice anything that’s not working properly, please let me know by submitting a <a href="https://github.com/fditraglia/econometrics.blog/issues">GitHub issue</a>.</p>



 ]]></description>
  <category>meta</category>
  <guid>https://www.econometrics.blog/post/makeover-for-econometrics-blog/</guid>
  <pubDate>Thu, 09 Apr 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Chris Sims - RIP</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/chris-sims-rip/</link>
  <description><![CDATA[ 




<p>I was saddened to hear of Chris Sims’s passing yesterday. Although I’m not a macroeconometrician, his work has strongly influenced the way I think about econometrics. I covered his famous <a href="https://www.econometrics.blog/post/sims-and-uhlig-1991-replication/">helicopter tour</a> paper on this blog a while back. Some of my other favorites are unpublished notes or slides from his <a href="https://www.princeton.edu/~sims">website</a>, many of them with a philosophical bent. <a href="http://sims.princeton.edu/yftp/IV/IV.pdf">Thinking about instrumental variables</a> is a paper I read in grad school that really clarified why things can go so badly wrong in IV estimation. I read <a href="http://sims.princeton.edu/yftp/UndrstndgNnBsns/GewekeBookChpter.pdf">Understanding Non-Bayesians</a> for the first time a couple of years ago and wished I had read it sooner. It articulates a view of Bayesian econometrics that I find particularly compelling. <a href="http://sims.princeton.edu/yftp/WassermanExmpl/WassermanR4a.pdf">Robins-Wasserman, Round N</a> and <a href="http://sims.princeton.edu/yftp/SharpEmet/SharpEmet.pdf">Sharp Econometrics</a> have also shaped the approach I’m taking in some <a href="https://laurayuliu.com/research/BDML_DL/BDML.pdf">recent work with Laura Liu</a>.</p>
<p>I didn’t know Chris well, but I met him a couple of times early in my career. One of those meetings is particularly memorable. I was invited to give a seminar at Princeton on relatively short notice and was feeling apprehensive. The material that I had ready to present was preliminary and a little unusual: it included elements of my paper on <a href="http://ditraglia.com/pdf/DiTraglia-Garcia-Jimeno-2019.pdf">disciplining beliefs</a> as well as some more traditional results that eventually made their way into <a href="http://ditraglia.com/pdf/binary-regressor-final.pdf">this paper</a>.</p>
<p>When I saw that my first meeting of the day was with a Nobel laureate whom I’d never met before, I was even more nervous! But Chris was great; within a couple of minutes I felt completely comfortable talking with him and was genuinely surprised to see how interested he was in hearing my views on econometrics. I don’t mean to say that my views were particularly insightful or interesting. I think Chris just genuinely enjoyed engaging with people about econometrics and puzzling through the issues that he found interesting and important.</p>
<p>Near the beginning of my seminar, someone in the audience asked me a slightly pointed question—not rude but definitely skeptical of my approach. For whatever reason, I got flustered and flubbed my response. I couldn’t seem to articulate my point, and then bungled my response to a follow-up question as well. Just when I started to worry that my seminar was going off the rails, Chris chimed in and clarified the point I had been struggling to articulate. I remember feeling incredibly relieved, like someone had just pressed a reset button for my talk. It was just a small thing, but it meant a lot to me at a time in my career when I was feeling anything but confident about my ideas and my work.</p>



 ]]></description>
  <category>meta</category>
  <guid>https://www.econometrics.blog/post/chris-sims-rip/</guid>
  <pubDate>Sun, 15 Mar 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Overlapping Confidence Intervals: Part II</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/overlapping-confidence-intervals-part-ii/</link>
  <description><![CDATA[ 




<p>In my earlier post on <a href="https://www.econometrics.blog/post/overlapping-confidence-intervals/">Overlapping Confidence Intervals</a> I asked what we can learn from the overlap, or lack thereof, between confidence intervals for two population means constructed using independent samples. To recap: if the individual confidence intervals for groups A and B <em>do not</em> overlap, there must be a statistically significant difference between the population means for the two groups. In other words, the interval for the difference of means will not include zero. If the individual intervals <em>do overlap</em>, on the other hand, anything goes. The interval for the difference of means may or may not include zero. Indeed, it’s even possible for the individual intervals for both A and B to include zero while the interval for their difference does not!</p>
<p>In <a href="https://www.econometrics.blog/post/overlapping-confidence-intervals/">Part I</a> we found a way to rephrase our statistical problem about overlapping confidence intervals as a familiar geometry problem involving <em>right triangles</em>. We then solved this geometry problem using the Pythagorean Theorem and Triangle Inequality. To build the connection between confidence intervals and right triangles, however, we assumed that our two estimators were <em>uncorrelated</em>. Unfortunately this assumption fails in many interesting real-world applications. Today we’ll ask what happens to our earlier conclusions about overlapping intervals if we allow for correlation.</p>
<section id="a-word-about-notation" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="a-word-about-notation"><span class="header-section-number">1</span> A Word About Notation</h2>
<p>While I phrased my first post about overlapping intervals in terms of sample and population means for two groups, the idea is general. Nothing substantive changes if we replace the parameters <img src="https://latex.codecogs.com/png.latex?(%5Cmu_A,%20%5Cmu_B)"> with <img src="https://latex.codecogs.com/png.latex?(%5Calpha,%20%5Cbeta)">, the estimators <img src="https://latex.codecogs.com/png.latex?(%5Cbar%7BA%7D,%20%5Cbar%7BB%7D)"> with <img src="https://latex.codecogs.com/png.latex?(%5Chat%7B%5Calpha%7D,%20%5Chat%7B%5Cbeta%7D)">, and the standard errors <img src="https://latex.codecogs.com/png.latex?%5Cleft(%5Ctext%7BSE%7D(%5Cbar%7BA%7D),%20%5Ctext%7BSE%7D(%5Cbar%7BB%7D)%5Cright)"> with <img src="https://latex.codecogs.com/png.latex?%5Cleft(%5Ctext%7BSE%7D(%5Chat%7B%5Calpha%7D),%20%5Ctext%7BSE%7D(%5Chat%7B%5Cbeta%7D)%5Cright)">. As long as the two estimators are uncorrelated and (approximately) normally distributed, the results from <a href="https://www.econometrics.blog/post/overlapping-confidence-intervals/">Part I</a> apply to the individual intervals for <img src="https://latex.codecogs.com/png.latex?%5Calpha"> and <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> versus the interval for the difference <img src="https://latex.codecogs.com/png.latex?%5Calpha-%5Cbeta">. Today we will ask what happens when the estimators are potentially <em>correlated</em>. To make it clear that our results are general, I’ll use the more agnostic <img src="https://latex.codecogs.com/png.latex?(%5Calpha,%5Cbeta)"> notation throughout.</p>
</section>
<section id="a-motivating-example" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="a-motivating-example"><span class="header-section-number">2</span> A Motivating Example</h2>
<p>Consider a randomized controlled trial with <em>two active treatments</em>. In our paper on <a href="https://ditraglia.com/pdf/pawn-paper.pdf">pawn lending</a>, for example, my co-authors and I compare default rates between borrowers assigned to the <em>status quo</em> pawn contract (control), a new <em>structured</em> contract (Treatment A), and a <em>choice arm</em> (Treatment B) in which they were free to choose whichever contract they preferred. To learn the causal effect of the structured contract, we compare the mean default rates of borrowers who received Treatment A against the corresponding rate in the control group. Call this difference of means <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Calpha%7D">. Similarly, to learn the effect of choice, we make the analogous comparison between Treatment B and the control group. Call this difference of means <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cbeta%7D">. Now suppose you’re reading a paper that reports both of these estimators and their standard errors. To find out which treatment is more effective, you need to compare <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Calpha%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cbeta%7D">, but these estimators <em>must</em> be correlated because they both involve the control group average: <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Chat%7B%5Calpha%7D%20&amp;=%20%5Ctext%7B(Treatment%20A%20mean)%7D%20-%20%5Ctext%7B(Control%20Group%20mean)%7D%5C%5C%0A%5Chat%7B%5Cbeta%7D%20&amp;=%20%5Ctext%7B(Treatment%20B%20mean)%7D%20-%20%5Ctext%7B(Control%20Group%20mean)%7D.%0A%5Cend%7Baligned%7D%0A"> Granted, if you had access to the raw data, you could easily solve this problem without worrying about the correlation. In the difference <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Calpha%7D%20-%20%5Chat%7B%5Cbeta%7D">, the control group mean cancels out <img src="https://latex.codecogs.com/png.latex?%0A%5Chat%7B%5Calpha%7D%20-%20%5Chat%7B%5Cbeta%7D%20=%20%5Ctext%7B(Treatment%20A%20mean)%7D%20-%20%5Ctext%7B(Treatment%20B%20mean)%7D.%0A"> So if we <em>had</em> the raw data for treatments A and B we’d be back to a familiar independent samples comparison of means, as in <a href="https://www.econometrics.blog/post/overlapping-confidence-intervals/">Part I</a>. But if you’re reading a paper that only reports <img src="https://latex.codecogs.com/png.latex?(%5Chat%7B%5Calpha%7D,%20%5Chat%7B%5Cbeta%7D)"> and their standard errors, you <em>cannot</em> directly calculate the standard error for the difference. The common variation from the control group mean is baked into the way both <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Chat%7B%5Calpha%7D)"> and <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Chat%7B%5Cbeta%7D)"> are computed, even though this variation is <em>irrelevant</em> for <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Chat%7B%5Calpha%7D%20-%20%5Chat%7B%5Cbeta%7D)">.</p>
</section>
<section id="allowing-correlation" class="level2 page-columns page-full" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="allowing-correlation"><span class="header-section-number">3</span> Allowing Correlation</h2>
<p>So how do we compute <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Chat%7B%5Calpha%7D%20-%20%5Chat%7B%5Cbeta%7D)"> if the two estimators are correlated? Recall that the <em>standard error</em> is the standard deviation of that estimator’s sampling distribution, and a standard deviation is merely the square root of the corresponding variance.<sup>1</sup> Using the <a href="https://www.econometrics.blog/post/random-variables-cheatsheet/">properties of variance and covariance</a>, <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BVar%7D(%5Chat%7B%5Calpha%7D%20-%20%5Chat%7B%5Cbeta%7D)%20=%20%5Ctext%7BVar%7D(%5Chat%7B%5Calpha%7D)%20+%20%5Ctext%7BVar%7D(%5Chat%7B%5Cbeta%7D)%20-%202%5Ctext%7BCov%7D(%5Chat%7B%5Calpha%7D,%20%5Chat%7B%5Cbeta%7D).%0A"> Defining <img src="https://latex.codecogs.com/png.latex?%5Crho%20%5Cequiv%20%5Ctext%7BCorr%7D(%5Chat%7B%5Calpha%7D,%5Chat%7B%5Cbeta%7D)">, it follows that <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BSE%7D(%5Chat%7B%5Calpha%7D%20-%20%5Chat%7B%5Cbeta%7D)%5E2%20=%20%5Ctext%7BSE%7D(%5Chat%7B%5Calpha%7D)%5E2%20+%20%5Ctext%7BSE%7D(%5Chat%7B%5Cbeta%7D)%5E2%20-%202%5Crho%20%5Ccdot%20%5Ctext%7BSE%7D(%5Chat%7B%5Calpha%7D)%20%5Ccdot%20%5Ctext%7BSE%7D(%5Chat%7B%5Cbeta%7D).%0A"> If <img src="https://latex.codecogs.com/png.latex?%5Crho%20=%200"> this reduces to our formula from <a href="https://www.econometrics.blog/post/overlapping-confidence-intervals/">Part I</a>: <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Chat%7B%5Calpha%7D%20-%20%5Chat%7B%5Cbeta%7D)%5E2%20=%20%5Ctext%7BSE%7D(%5Chat%7B%5Calpha%7D)%5E2%20+%20%5Ctext%7BSE%7D(%5Chat%7B%5Cbeta%7D)%5E2"> so we can equate the LHS with the length of the hypotenuse of a right triangle whose legs have lengths <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Chat%7B%5Calpha%7D)"> and <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Chat%7B%5Cbeta%7D)">. If <img src="https://latex.codecogs.com/png.latex?%5Crho%20%5Cneq%200">, however, this connection to the Pythagorean Theorem no longer holds. Nevertheless, there are <em>still triangles</em> hiding in this standard error formula! To reveal them, we need a more general theorem about triangles.</p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;Many people, including most econometricians, reserve the term <em>standard error</em> for an <em>estimate</em> of this standard deviation. I prefer to call this estimate the <em>estimated standard error</em>. Clearly my convention is better and everyone should adopt it :)</p></div></div></section>
<section id="the-law-of-cosines" class="level2 page-columns page-full" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="the-law-of-cosines"><span class="header-section-number">4</span> The Law of Cosines</h2>
<p>Consider a triangle whose sides have lengths <img src="https://latex.codecogs.com/png.latex?a,b"> and <img src="https://latex.codecogs.com/png.latex?c">. Let <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> be the angle between the sides whose lengths are <img src="https://latex.codecogs.com/png.latex?a"> and <img src="https://latex.codecogs.com/png.latex?b">. Then by the <em>Law of Cosines</em><br>
<img src="https://latex.codecogs.com/png.latex?%0Ac%5E2%20=%20a%5E2%20+%20b%5E2%20-%202%5Ccos(%5Ctheta)%20%5Ccdot%20ab%0A"> This equality holds for <em>any triangle</em>. For a right triangle whose legs have lengths <img src="https://latex.codecogs.com/png.latex?a"> and <img src="https://latex.codecogs.com/png.latex?b">, we have <img src="https://latex.codecogs.com/png.latex?%5Ctheta%20=%2090%C2%B0"> so the Law of Cosines reduces to the Pythagorean Theorem <img src="https://latex.codecogs.com/png.latex?%0Ac%5E2%20=%20a%5E2%20+%20b%5E2.%0A"> When <img src="https://latex.codecogs.com/png.latex?%5Ctheta%20%5Cneq%2090%C2%B0">, the “correction term” <img src="https://latex.codecogs.com/png.latex?-2%5Ccos(%5Ctheta)%5Ccdot%20ab"> shows how the length of <img src="https://latex.codecogs.com/png.latex?c"> differs from that of a right triangle with legs of lengths <img src="https://latex.codecogs.com/png.latex?a"> and <img src="https://latex.codecogs.com/png.latex?b">. When <img src="https://latex.codecogs.com/png.latex?%5Ctheta%20%3C%2090%C2%B0"> the cosine is positive so the correction term <em>shortens</em> <img src="https://latex.codecogs.com/png.latex?c">; when <img src="https://latex.codecogs.com/png.latex?%5Ctheta%20%3E%2090%C2%B0"> the cosine is negative so the correction term <em>lengthens</em> <img src="https://latex.codecogs.com/png.latex?c">. Regardless of the angle <img src="https://latex.codecogs.com/png.latex?%5Ctheta">, however, the Triangle Inequality still holds: <img src="https://latex.codecogs.com/png.latex?c%20%3C%20a%20+%20b"> because the shortest distance between two points is a straight line.<sup>2</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn2"><p><sup>2</sup>&nbsp;Here I assume that this is a <em>genuine triangle</em> rather than three points on the same line.</p></div></div></section>
<section id="from-geometry-to-statistics" class="level2 page-columns page-full" data-number="5">
<h2 data-number="5" class="anchored" data-anchor-id="from-geometry-to-statistics"><span class="header-section-number">5</span> From Geometry to Statistics</h2>
<p>The cosine of an angle is always between negative one and one. Can you think of anything else that shares this property? That’s right: correlation! So let’s put the Law of Cosines and our standard error formula from above side-by-side: <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0Ac%5E2%20&amp;=%20a%5E2%20+%20b%5E2%20-%202%5Ccos(%5Ctheta)%20%5Ccdot%20ab%5C%5C%20%5C%5C%0A%5Ctext%7BSE%7D(%5Chat%7B%5Calpha%7D%20-%20%5Chat%7B%5Cbeta%7D)%5E2%20&amp;=%20%5Ctext%7BSE%7D(%5Chat%7B%5Calpha%7D)%5E2%20+%20%5Ctext%7BSE%7D(%5Chat%7B%5Cbeta%7D)%5E2%20-%202%5Crho%20%5Ccdot%20%5Ctext%7BSE%7D(%5Chat%7B%5Calpha%7D)%20%5Ccdot%20%5Ctext%7BSE%7D(%5Chat%7B%5Cbeta%7D).%0A%5Cend%7Baligned%7D%0A"> The analogy is <em>perfect</em>. We can view <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Chat%7B%5Calpha%7D)"> and <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Chat%7B%5Cbeta%7D)"> as the lengths of two sides of a triangle and <img src="https://latex.codecogs.com/png.latex?%5Crho"> as the cosine of the angle between these sides. This makes <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Chat%7B%5Calpha%7D%20-%20%5Chat%7B%5Cbeta%7D)"> the length of the third side, indicated in blue in the following diagram. When <img src="https://latex.codecogs.com/png.latex?%5Crho"> is <em>positive</em>, the standard error of the difference is <em>smaller</em> than it would be under independence; if <img src="https://latex.codecogs.com/png.latex?%5Crho"> is <em>negative</em>, the standard error of the difference is <em>larger</em> than it would be under independence.</p>
<div class="cell page-columns page-full">
<div class="cell-output-display page-columns page-full">
<div class="page-columns page-full">
<figure class="figure page-columns page-full">
<p class="page-columns page-full"><img src="https://www.econometrics.blog/post/overlapping-confidence-intervals-part-ii/index_files/figure-html/unnamed-chunk-2-1.svg" class="img-fluid figure-img column-page" width="1056"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="the-grand-finale" class="level2" data-number="6">
<h2 data-number="6" class="anchored" data-anchor-id="the-grand-finale"><span class="header-section-number">6</span> The Grand Finale</h2>
<p>Since we’ve equated <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Chat%7B%5Calpha%7D%20-%20%5Chat%7B%5Cbeta%7D)">, <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Chat%7B%5Calpha%7D)"> and <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Chat%7B%5Cbeta%7D)"> with the sides of a triangle, the Triangle Inequality gives <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BSE%7D(%5Chat%7B%5Calpha%7D%20-%20%5Chat%7B%5Cbeta%7D)%20%3C%20%5Ctext%7BSE%7D(%5Chat%7B%5Calpha%7D)%20+%20%5Ctext%7BSE%7D(%5Chat%7B%5Cbeta%7D)%0A"> assuming that <img src="https://latex.codecogs.com/png.latex?%7C%5Crho%7C%20%3C%201">. And now we’re on familiar ground. Let <img src="https://latex.codecogs.com/png.latex?z"> be the appropriate quantile of a normal distribution, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?z%20%5Capprox%202"> for a 95% confidence interval. Just as we argued in <a href="https://www.econometrics.blog/post/overlapping-confidence-intervals/">Part I</a>, the individual confidence intervals for <img src="https://latex.codecogs.com/png.latex?%5Calpha"> and <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> overlap precisely when <img src="https://latex.codecogs.com/png.latex?(%5Chat%7B%5Calpha%7D%20-%20%5Chat%7B%5Cbeta%7D)/z%20%3C%20%5Ctext%7BSE%7D(%5Chat%7B%5Calpha%7D)%20+%20%5Ctext%7BSE%7D(%5Chat%7B%5Cbeta%7D)"> and there is a significant difference between the two parameters when <img src="https://latex.codecogs.com/png.latex?(%5Chat%7B%5Calpha%7D%20-%20%5Chat%7B%5Cbeta%7D)/z%20%3E%20%5Ctext%7BSE%7D(%5Chat%7B%5Calpha%7D%20-%20%5Chat%7B%5Cbeta%7D)">. The condition for overlap <em>and</em> a significant difference is <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BSE%7D(%5Chat%7B%5Calpha%7D%20-%20%5Chat%7B%5Cbeta%7D)%20%3C%20%5Cfrac%7B%5Chat%7B%5Calpha%7D%20-%20%5Chat%7B%5Cbeta%7D%7D%7Bz%7D%20%3C%20%5Ctext%7BSE%7D(%5Chat%7B%5Calpha%7D)%20+%20%5Ctext%7BSE%7D(%5Chat%7B%5Cbeta%7D)%0A"> which holds by the Triangle Inequality. So we’re back to exactly the same situation we were in when <img src="https://latex.codecogs.com/png.latex?%5Crho%20=%200">! As long as <img src="https://latex.codecogs.com/png.latex?%5Crho%20%5Cneq%20-1,%201"> the same results concerning confidence interval overlap apply when <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Calpha%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cbeta%7D"> are correlated as when they are uncorrelated:</p>
<ol type="1">
<li>Overlap doesn’t tell us <em>anything</em> about whether there is a significant difference, but</li>
<li>a lack of overlap implies that there <em>must</em> be a significant difference.</li>
</ol>


</section>


 ]]></description>
  <category>statistics</category>
  <guid>https://www.econometrics.blog/post/overlapping-confidence-intervals-part-ii/</guid>
  <pubDate>Sat, 22 Nov 2025 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Overlapping Confidence Intervals</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/overlapping-confidence-intervals/</link>
  <description><![CDATA[ 




<p>Perhaps you’ve seen a claim like this in an applied paper: “the estimated effect for Group A is statistically significant, but the estimated effect for Group B is not; this treatment helps As but not Bs.” But this reasoning is <em>flawed</em>.</p>
<p>To see why, consider the following example from <a href="https://sites.stat.columbia.edu/gelman/research/published/signif4.pdf">Gelman &amp; Stern</a>. We have data from two independent samples: Group A and Group B. For Group A our estimated effect is 25 with a standard error of 10, yielding an approximate 95% confidence interval of <img src="https://latex.codecogs.com/png.latex?25%20%5Cpm%2020"> or <img src="https://latex.codecogs.com/png.latex?(5,%2045)">. This interval does not include zero, so the effect for Group A is statistically significant at the 5% level. For Group B our estimated effect is 10 with a standard error of 10, yielding a confidence interval of <img src="https://latex.codecogs.com/png.latex?10%20%5Cpm%2020"> or <img src="https://latex.codecogs.com/png.latex?(-10,%2030)">. This interval <em>does</em> include zero, so the effect for Group B is not statistically significant at the 5% level. But there is no statistically significant difference between the groups: the difference of means is <img src="https://latex.codecogs.com/png.latex?25%20-%2010%20=%2015"> but the standard error for the difference is <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7B10%5E2%20+%2010%5E2%7D%20=%20%5Csqrt%7B200%7D%20%5Capprox%2014.14">. Thus, the 95% confidence interval for the difference is <img src="https://latex.codecogs.com/png.latex?15%20%5Cpm%2028"> or <img src="https://latex.codecogs.com/png.latex?(-13,%2043)">, which comfortably includes zero.</p>
<p>To quote the title of the aforementioned paper: <strong>The Difference Between “Significant” and “Not Significant” is not Itself Statistically Significant.</strong> Meditate on this lesson and repeat it ten times before going to bed every night.</p>
<p>After you’ve done this, I have a puzzle for you to ponder. In our example from above, the intervals for A and B <em>overlap</em> and the difference is <em>not significant</em>. Is this a general rule? In other words, does overlap in the two intervals imply no significant difference between the groups? While we’re at it, what about the opposite case? If the two intervals <em>do not</em> overlap, does that mean that there <em>is</em> a significant difference between the groups?</p>
<section id="setting-the-stage" class="level2 page-columns page-full" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="setting-the-stage"><span class="header-section-number">1</span> Setting the Stage</h2>
<section id="intervals-for-a-b-and-their-difference." class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="intervals-for-a-b-and-their-difference.">Intervals for A, B and their difference.</h3>
<p>Let <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BA%7D"> be our estimator of the population mean <img src="https://latex.codecogs.com/png.latex?%5Cmu_A"> for Group A, and let <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Cbar%7BA%7D)"> be its standard error. Similarly, let <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BB%7D"> be our estimator of the population mean <img src="https://latex.codecogs.com/png.latex?%5Cmu_B"> for Group B, with standard error <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Cbar%7BB%7D)">. If the samples we used to construct <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BA%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BB%7D"> are independent, then <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(%5Cbar%7BA%7D,%20%5Cbar%7BB%7D)%20=%200"> and <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D)%20=%20%5Csqrt%7B%5Ctext%7BSE%7D(%5Cbar%7BA%7D)%5E2%20+%20%5Ctext%7BSE%7D(%5Cbar%7BB%7D)%5E2%7D"> as in the example from above. If <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BA%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BB%7D"> are approximately normally distributed, say by appealing to the central limit theorem, then we can construct 95% confidence intervals for <img src="https://latex.codecogs.com/png.latex?%5Cmu_A">, <img src="https://latex.codecogs.com/png.latex?%5Cmu_B"> as follows <img src="https://latex.codecogs.com/png.latex?%5Cmu_A%20%5Ccolon%20%5Cquad%20%5Cbar%7BA%7D%20%5Cpm%202%20%5Ctimes%20%5Ctext%7BSE%7D(%5Cbar%7BA%7D),%20%5Cquad%20%5Cquad%20%5Cmu_B%5Ccolon%20%5Cquad%20%5Cbar%7BB%7D%20%5Cpm%202%20%5Ctimes%20%5Ctext%7BSE%7D(%5Cbar%7BB%7D)."> Similarly, we can construct a confidence interval for the difference in means <img src="https://latex.codecogs.com/png.latex?%5Cmu_A%20-%20%5Cmu_B">, namely <img src="https://latex.codecogs.com/png.latex?%0A(%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D)%20%5Cpm%202%20%5Ctimes%20%5Ctext%7BSE%7D(%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D).%0A"> More generally, to construct an approximate <img src="https://latex.codecogs.com/png.latex?(1%20-%20%5Calpha)%20%5Ctimes%20100%5C%25"> confidence interval, we would replace the 2 above with the appropriate quantile of a standard normal distribution.<sup>1</sup> Below I’ll call this quantile <img src="https://latex.codecogs.com/png.latex?z"> for short.</p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;Granted, 2 is <em>slightly</em> larger than <img src="https://latex.codecogs.com/png.latex?%5Ctexttt%7Bqnorm%7D(0.975)">, but do you really want to multiply by 1.96 in your head?</p></div></div></section>
<section id="when-is-the-difference-significant" class="level3">
<h3 class="anchored" data-anchor-id="when-is-the-difference-significant">When is the difference significant?</h3>
<p>The difference between <img src="https://latex.codecogs.com/png.latex?%5Cmu_A"> and <img src="https://latex.codecogs.com/png.latex?%5Cmu_B"> is statistically significant at the <img src="https://latex.codecogs.com/png.latex?%5Calpha%20%5Ctimes%20100%5C%25"> level if the confidence interval for <img src="https://latex.codecogs.com/png.latex?%5CDelta"> does not include zero, i.e.&nbsp;if <img src="https://latex.codecogs.com/png.latex?%7C%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D%7C%20%3E%20z%20%5Ccdot%20%5Ctext%7BSE%7D(%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D)">. But working with absolute values will quickly become tedious, so without loss of generality, let’s assume <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BA%7D%20%5Cgeq%20%5Cbar%7BB%7D">. If this doesn’t hold, we can always relabel the two groups so it does hold. Then the condition for a significant difference becomes <img src="https://latex.codecogs.com/png.latex?(%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D)/z%20%3E%20%5Ctext%7BSE%7D(%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D)."></p>
</section>
<section id="when-do-the-intervals-overlap" class="level3">
<h3 class="anchored" data-anchor-id="when-do-the-intervals-overlap">When do the intervals overlap?</h3>
<p>Again, without loss of generality, we can assume that <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BA%7D%5Cgeq%20%5Cbar%7BB%7D">. Now think about what it would mean for the two confidence intervals to overlap. The center of the interval for <img src="https://latex.codecogs.com/png.latex?%5Cmu_A"> is to the right of the center of the interval for <img src="https://latex.codecogs.com/png.latex?%5Cmu_B"> since <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BA%7D%20%5Cgeq%20%5Cbar%7BB%7D">. So for the two intervals to overlap, the <em>lower confidence limit</em> of the <img src="https://latex.codecogs.com/png.latex?%5Cmu_A"> interval must be to the left of the <em>upper confidence limit</em> of the <img src="https://latex.codecogs.com/png.latex?%5Cmu_B"> interval. This figure illustrates the logic using <img src="https://latex.codecogs.com/png.latex?z%20=%202">.</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/overlapping-confidence-intervals/index_files/figure-html/unnamed-chunk-2-1.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>From the figure, we see that the two intervals overlap if <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BA%7D%20-%202%20%5Ccdot%20%5Ctext%7BSE%7D(%5Cbar%7BA%7D)%20%3C%20%5Cbar%7BB%7D%20+%202%20%5Ccdot%20%5Ctext%7BSE%7D(%5Cbar%7BB%7D)"> Rearranging, and using the generic quantile <img src="https://latex.codecogs.com/png.latex?z">, this becomes <img src="https://latex.codecogs.com/png.latex?(%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D)/z%20%3C%20%5Ctext%7BSE%7D(%5Cbar%7BA%7D)%20+%20%5Ctext%7BSE%7D(%5Cbar%7BB%7D)."></p>
</section>
</section>
<section id="case-i-overlapping-intervals-for-a-and-b" class="level2 page-columns page-full" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="case-i-overlapping-intervals-for-a-and-b"><span class="header-section-number">2</span> Case I: Overlapping Intervals for A and B</h2>
<section id="formalizing-the-question" class="level3">
<h3 class="anchored" data-anchor-id="formalizing-the-question">Formalizing the Question</h3>
<p>Can the confidence intervals for A and B overlap despite there being a significant difference of means between the two groups? Using the results from above, this question is equivalent to asking whether it’s possible for both of these inequalities to hold at the same time:</p>
<ol type="1">
<li>Overlapping CIs: <img src="https://latex.codecogs.com/png.latex?(%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D)/z%20%3C%20%5Ctext%7BSE%7D(%5Cbar%7BA%7D)%20+%20%5Ctext%7BSE%7D(%5Cbar%7BB%7D)"></li>
<li>Significant Difference: <img src="https://latex.codecogs.com/png.latex?(%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D)/z%20%3E%20%5Ctext%7BSE%7D(%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D)"></li>
</ol>
<p>So the question becomes: can we find values of <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BA%7D">, <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BB%7D">, <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Cbar%7BA%7D)">, and <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Cbar%7BB%7D)"> such that <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D)%20%3C%20%5Cfrac%7B%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D%7D%7Bz%7D%20%3C%20%5Ctext%7BSE%7D(%5Cbar%7BA%7D)%20+%20%5Ctext%7BSE%7D(%5Cbar%7BB%7D)?"></p>
</section>
<section id="lets-talk-about-triangles" class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="lets-talk-about-triangles">Let’s talk about triangles!</h3>
<p>For just a moment, forget that we’re talking about statistics and cast your mind back to <em>high school geometry</em>. There are two facts I’d like you to recall:</p>
<section id="the-pythagorean-theorem" class="level4">
<h4 class="anchored" data-anchor-id="the-pythagorean-theorem">1: The Pythagorean Theorem</h4>
<p>If <img src="https://latex.codecogs.com/png.latex?a"> and <img src="https://latex.codecogs.com/png.latex?b"> are the lengths of the legs of a right triangle, then the length <img src="https://latex.codecogs.com/png.latex?c"> of the hypotenuse satisfies <img src="https://latex.codecogs.com/png.latex?c%20=%20%5Csqrt%7Ba%5E2%20+%20b%5E2%7D">.</p>
</section>
<section id="the-triangle-inequality" class="level4 page-columns page-full">
<h4 class="anchored" data-anchor-id="the-triangle-inequality">2: The Triangle Inequality</h4>
<p>For <em>any triangle</em> with sides <img src="https://latex.codecogs.com/png.latex?a">, <img src="https://latex.codecogs.com/png.latex?b">, and <img src="https://latex.codecogs.com/png.latex?c">, we have <img src="https://latex.codecogs.com/png.latex?c%20%5Cleq%20a%20+%20b">.<sup>2</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn2"><p><sup>2</sup>&nbsp;The triangle inequality just says that “the shortest distance between two points in a Euclidean plane is a straight line.” If we’re working with a genuine triangle, then the vertices <em>cannot</em> lie on the same line. So traveling from <img src="https://latex.codecogs.com/png.latex?x"> to <img src="https://latex.codecogs.com/png.latex?z"> via <img src="https://latex.codecogs.com/png.latex?y"> always covers a greater distance than going straight from <img src="https://latex.codecogs.com/png.latex?x"> to <img src="https://latex.codecogs.com/png.latex?z">.</p></div></div><p>So how do these two facts help us? Consider a right triangle whose legs have lengths <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Cbar%7BA%7D)"> and <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Cbar%7BB%7D)">. By the Pythagorean Theorem, the hypotenuse of this triangle has length <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7B%5Ctext%7BSE%7D(%5Cbar%7BA%7D)%5E2%20+%20%5Ctext%7BSE%7D(%5Cbar%7BB%7D)%5E2%7D%20=%20%5Ctext%7BSE%7D(%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D)%20"> and by the triangle inequality, we have: <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D)%20%3C%20%5Ctext%7BSE%7D(%5Cbar%7BA%7D)%20+%20%5Ctext%7BSE%7D(%5Cbar%7BB%7D)."> We can read this inequality from the following figure: travelling along the dashed red path covers a distance of <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Cbar%7BA%7D)%20+%20%5Ctext%7BSE%7D(%5Cbar%7BB%7D)">, while travelling along the solid blue path the shorter distance of <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D)">.</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/overlapping-confidence-intervals/index_files/figure-html/unnamed-chunk-3-1.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="the-solution" class="level3">
<h3 class="anchored" data-anchor-id="the-solution">The Solution</h3>
<p>The question we set out to answer is whether we can find values that satisfy the inequality: <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D)%20%3C%20%5Cfrac%7B%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D%7D%7Bz%7D%20%3C%20%5Ctext%7BSE%7D(%5Cbar%7BA%7D)%20+%20%5Ctext%7BSE%7D(%5Cbar%7BB%7D)."> Since the right-hand side is <em>always</em> strictly larger than the left-hand side, the answer is <em>yes</em>. Let’s try it out using a simple example. Suppose that <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Cbar%7BA%7D)%20=%20%5Ctext%7BSE%7D(%5Cbar%7BB%7D)%20=%2010"> as in the example from above, and consider a 95% confidence interval so that <img src="https://latex.codecogs.com/png.latex?z%20%5Capprox%202">. Then the inequality becomes <img src="https://latex.codecogs.com/png.latex?%0A20%20%5Csqrt%7B2%7D%20%3C%20%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D%20%3C%2040.%0A"> Since <img src="https://latex.codecogs.com/png.latex?20%20%5Csqrt%7B2%7D%20%5Capprox%2028.28"> we have a whole range of values for <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D"> that will do the trick. For example, <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BA%7D-%5Cbar%7BB%7D%20=%2030"> will do the trick. So if <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Cbar%7BA%7D)%20=%20%5Ctext%7BSE%7D(%5Cbar%7BB%7D)%20=%2010">, <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BA%7D%20=%2040"> and <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BB%7D%20=%2010">, the 95% CIs for A and B overlap but there <em>is</em> a significant difference between the two groups. This is the opposite of Gelman &amp; Stern’s example.</p>
<p>Our inequality from above depends only the difference between <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BA%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BB%7D">, not on the individual value of each sample mean. So if we keep the same standard errors as before but set <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BA%7D%20=%2015"> and <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BB%7D%20=%20-15">, so the difference of means remains 30, we obtain the same result: overlapping intervals with a significant difference between the two groups. Notice anything interesting about this example? The interval for A is <img src="https://latex.codecogs.com/png.latex?(-5,35)"> while the interval for B is <img src="https://latex.codecogs.com/png.latex?(-35,%205)">. So <em>both intervals include zero</em> but there is <em>still</em> a significant difference between them! If all we know is that two intervals overlap, we can’t say <em>anything</em> about the significance of the difference.</p>
</section>
</section>
<section id="case-ii-intervals-for-a-and-b-that-dont-overlap" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="case-ii-intervals-for-a-and-b-that-dont-overlap"><span class="header-section-number">3</span> Case II: Intervals for A and B that Don’t Overlap</h2>
<p>Now for the easy one. Since <img src="https://latex.codecogs.com/png.latex?(%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D)/z%20%3C%20%5Ctext%7BSE%7D(%5Cbar%7BA%7D)%20+%20%5Ctext%7BSE%7D(%5Cbar%7BB%7D)."> holds if and only if the intervals overlap, we merely reverse the inequality to get a condition for intervals that <em>do not overlap</em>, namely <img src="https://latex.codecogs.com/png.latex?(%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D)/z%20%3E%20%5Ctext%7BSE%7D(%5Cbar%7BA%7D)%20+%20%5Ctext%7BSE%7D(%5Cbar%7BB%7D)."> Appending what we learned above from our right triangle diagram gives <img src="https://latex.codecogs.com/png.latex?(%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D)/z%20%3E%20%5Ctext%7BSE%7D(%5Cbar%7BA%7D)%20+%20%5Ctext%7BSE%7D(%5Cbar%7BB%7D)%3E%20%5Ctext%7BSE%7D(%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D)."> So if the intervals <em>do not</em> overlap, then <img src="https://latex.codecogs.com/png.latex?(%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D)/z"> must be <em>greater than</em> <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Cbar%7BA%7D%20-%20%5Cbar%7BB%7D)">, which is precisely the condition for a significant difference between the two groups.</p>
</section>
<section id="in-summary" class="level2" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="in-summary"><span class="header-section-number">4</span> In Summary</h2>
<p>In an independent samples problem where the two individual confidence intervals overlap, there <em>may or may not</em> be a significant difference between the groups. Even if the two intervals both contain zero, there <em>could still</em> be a significant difference between them. If the two intervals <em>do not overlap</em> then we can conclude that there <em>is</em> a significant difference. But if you only take one lesson away from this post it should be this one: the difference between significant and not significant is not itself significant. If you want to carry out inference for a difference, you need to construct the standard error for that difference.</p>


</section>


 ]]></description>
  <category>statistics</category>
  <guid>https://www.econometrics.blog/post/overlapping-confidence-intervals/</guid>
  <pubDate>Sat, 15 Nov 2025 00:00:00 GMT</pubDate>
</item>
<item>
  <title>A Good Instrument is a Bad Control: Part II</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/a-good-instrument-is-a-bad-control-part-ii/</link>
  <description><![CDATA[ 




<p>At a recent seminar dinner the conversation drifted to causal inference, and I mentioned my dream of one day producing a Lady Gaga parody music video called “Bad Control”.<sup>1</sup> A lively discussion of bad controls ensued, during which I offered one of my favorite examples: <a href="https://www.econometrics.blog/post/a-good-instrument-is-a-bad-control/">a good instrument is a bad control</a>. To summarize that earlier post: including a valid instrumental variable as a <em>control</em> variable can only amplify the bias on the coefficient for our endogenous regressor of interest. When used as a control, the instrument “soaks up” the good (exogenous) variation in the endogenous regressor, leaving behind only the bad (endogenous) variation. This is the opposite of what happens in an instrumental variables regression, where we use the instrument to <em>extract</em> only the good variation in the endogenous regressor. More generally, a “bad control” is a covariate that we <em>shouldn’t adjust for</em> when using a <a href="https://www.econometrics.blog/post/how-to-do-regression-adjustment/">selection-on-observables</a> approach to causal inference.</p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;I have a very rich inner life.</p></div></div><p>Upon hearing my IV example, my colleague immediately asked “but what about the coefficient on the <em>instrument</em> itself?” This is a great question and one I hadn’t thought about before. Today I’ll give you my answer.</p>
<p>This post is a sequel, so you may find it helpful to glance at my <a href="https://www.econometrics.blog/post/a-good-instrument-is-a-bad-control/">earlier post</a> before reading further. At the very end of the post I’ll rely on a few basic ideas about directed acyclic graphs (DAGs). If this material is unfamiliar, you may find my <a href="https://www.treatment-effects.com/basics/">treatment effects slides</a> helpful. With these caveats, I’ll do my best to keep this post relatively self-contained.</p>
<section id="recap-of-part-i" class="level2 page-columns page-full" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="recap-of-part-i"><span class="header-section-number">1</span> Recap of Part I</h2>
<p>Suppose that <img src="https://latex.codecogs.com/png.latex?X"> is our endogenous regressor of interest in the linear causal model <img src="https://latex.codecogs.com/png.latex?Y%20=%20%5Calpha%20+%20%5Cbeta%20X%20+%20U"> where <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(X,U)%20%5Cneq%200"> but <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(Z,U)%20=%200">, and where <img src="https://latex.codecogs.com/png.latex?Z"> is an instrumental variable that is correlated with <img src="https://latex.codecogs.com/png.latex?X">. Now consider the population linear regression of <img src="https://latex.codecogs.com/png.latex?Y"> on both <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Z">, namely <img src="https://latex.codecogs.com/png.latex?%0AY%20=%20%5Cgamma_0%20+%20%5Cgamma_X%20X%20+%20%5Cgamma_Z%20Z%20+%20%5Ceta%0A"> where the error term <img src="https://latex.codecogs.com/png.latex?%5Ceta"> satisfies <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(X,%5Ceta)%20=%20%5Ctext%7BCov%7D(Z,%5Ceta)%20=%20%5Cmathbb%7BE%7D(%5Ceta)%20=%200"> <a href="https://www.econometrics.blog/post/why-econometrics-is-confusing-part-1-the-error-term/">by construction</a>. Further define the population linear regression of <img src="https://latex.codecogs.com/png.latex?X"> on <img src="https://latex.codecogs.com/png.latex?Z">, namely <img src="https://latex.codecogs.com/png.latex?%0AX%20=%20%5Cpi_0%20+%20%5Cpi_Z%20Z%20+%20V%0A"> where the error term <img src="https://latex.codecogs.com/png.latex?V"> satisfies <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(Z,V)%20=%20%5Cmathbb%7BE%7D(V)%20=%200"> <a href="https://www.econometrics.blog/post/why-econometrics-is-confusing-part-1-the-error-term/">by construction</a>. Finally, define the population linear regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X"> as <img src="https://latex.codecogs.com/png.latex?%0AY%20=%20%5Cdelta_0%20+%20%5Cdelta_X%20X%20+%20%5Cepsilon,%20%5Cquad%20%5Ctext%7BCov%7D(X,%5Cepsilon)%20=%20%5Cmathbb%7BE%7D(%5Cepsilon)%20=%200.%0A"> Using this notation, the result from <a href="https://www.econometrics.blog/post/a-good-instrument-is-a-bad-control/">my earlier post</a> can be written as <img src="https://latex.codecogs.com/png.latex?%0A%5Cdelta_X%20=%20%5Cbeta%20+%20%5Cfrac%7B%5Ctext%7BCov%7D(X,U)%7D%7B%5Ctext%7BVar%7D(X)%7D,%20%5Cquad%20%5Ctext%7Band%7D%20%5Cquad%20%5Cgamma_X%20=%20%5Cbeta%20+%20%5Cfrac%7B%5Ctext%7BCov%7D(X,U)%7D%7B%5Ctext%7BVar%7D(V)%7D.%0A"> To understand what this tells us, notice that, using the “first-stage” regression of <img src="https://latex.codecogs.com/png.latex?X"> on <img src="https://latex.codecogs.com/png.latex?Z">, we can write <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BVar%7D(V)%20%5Cequiv%20%5Ctext%7BVar%7D(X%20-%20%5Cpi_0%20-%20%5Cpi_Z%20Z)%20=%20%5Ctext%7BVar%7D(X)%20-%20%5Cpi_Z%5E2%20%5Ctext%7BVar%7D(Z).%0A"> This shows that whenever <img src="https://latex.codecogs.com/png.latex?Z"> is a relevant instrument <img src="https://latex.codecogs.com/png.latex?(%5Cpi_Z%20%5Cneq%200)">, we must have <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVar%7D(V)%20%3C%20%5Ctext%7BVar%7D(X)">. It follows that <img src="https://latex.codecogs.com/png.latex?%5Cgamma_X"> is <em>more biased</em> than <img src="https://latex.codecogs.com/png.latex?%5Cdelta_X">: adding <img src="https://latex.codecogs.com/png.latex?Z"> as a control regressor only makes our estimate of the effect of <img src="https://latex.codecogs.com/png.latex?X"> <em>worse</em>!<sup>2</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn2"><p><sup>2</sup>&nbsp;The right way to learn <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> by regressing <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X"> and “something else” is the control function approach described in <a href="https://www.econometrics.blog/post/three-ways-of-thinking-about-instrumental-variables/">this post</a>. Rather than adding <img src="https://latex.codecogs.com/png.latex?Z">, we add <img src="https://latex.codecogs.com/png.latex?V%20=%20X%20-%20%5Cpi_0%20-%20%5Cpi_Z%20Z"> as a control.</p></div></div></section>
<section id="what-about-gamma_z" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="what-about-gamma_z"><span class="header-section-number">2</span> What about <img src="https://latex.codecogs.com/png.latex?%5Cgamma_Z">?</h2>
<p>So if <img src="https://latex.codecogs.com/png.latex?Z"> soaks up the <em>good variation</em> in <img src="https://latex.codecogs.com/png.latex?X">, what about the coefficient <img src="https://latex.codecogs.com/png.latex?%5Cgamma_Z"> on the instrument <img src="https://latex.codecogs.com/png.latex?Z">? Perhaps this coefficient contains some useful information about the causal effect of <img src="https://latex.codecogs.com/png.latex?X"> on <img src="https://latex.codecogs.com/png.latex?Y">? To find out, we’ll use the <a href="https://www.econometrics.blog/post/two-fwl-theorems-for-the-price-of-one/">FWL Theorem</a> as follows: <img src="https://latex.codecogs.com/png.latex?%0A%5Cgamma_Z%20=%20%5Cfrac%7B%5Ctext%7BCov%7D(Y,%5Ctilde%7BZ%7D)%7D%7B%5Ctext%7BVar%7D(%5Ctilde%7BZ%7D)%7D%0A"> where <img src="https://latex.codecogs.com/png.latex?Z%20=%20%5Clambda_0%20+%20%5Clambda_X%20X%20+%20%5Ctilde%7BZ%7D"> is the population linear regression of <img src="https://latex.codecogs.com/png.latex?Z"> on <img src="https://latex.codecogs.com/png.latex?X">. This is the <em>reverse</em> of the first-stage regression of <img src="https://latex.codecogs.com/png.latex?X"> on <img src="https://latex.codecogs.com/png.latex?Z"> described above. Here the error term <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BZ%7D"> satisfies <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(%5Ctilde%7BZ%7D)%20=%20%5Ctext%7BCov%7D(%5Ctilde%7BZ%7D,%20X)%20=%200"> <a href="https://www.econometrics.blog/post/why-econometrics-is-confusing-part-1-the-error-term/">by construction</a>. Substituting the causal model gives <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BCov%7D(Y,%20%5Ctilde%7BZ%7D)%20=%20%5Ctext%7BCov%7D(%5Calpha%20+%20%5Cbeta%20X%20+%20U,%20%5Ctilde%7BZ%7D)%20=%20%5Cbeta%20%5Ctext%7BCov%7D(X,%5Ctilde%7BZ%7D)%20+%20%5Ctext%7BCov%7D(U,%5Ctilde%7BZ%7D)%20=%20%5Ctext%7BCov%7D(U,%20%5Ctilde%7BZ%7D)%0A"> since <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(X,%5Ctilde%7BZ%7D)%20=%200"> by construction. Now, substituting the definition of <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BZ%7D">, <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BCov%7D(U,%20%5Ctilde%7BZ%7D)%20=%20%5Ctext%7BCov%7D(U,%20Z%20-%20%5Clambda_0%20-%20%5Clambda_X%20X)%20=%20%5Ctext%7BCov%7D(U,Z)%20-%20%5Clambda_X%20%5Ctext%7BCov%7D(U,X)%20=%20-%5Clambda_X%20%5Ctext%7BCov%7D(X,U)%0A"> since <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(U,Z)%20=%200"> by assumption. We can already see that <img src="https://latex.codecogs.com/png.latex?%5Cgamma_Z"> is <em>not going to help us</em> learn about <img src="https://latex.codecogs.com/png.latex?%5Cbeta">. First of all, the term containing <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> vanished; second of all, the term that remained is polluted by the endogeneity of <img src="https://latex.codecogs.com/png.latex?X">, namely <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(X,U)">.</p>
<p>Still, let’s see if we can get a clean expression for <img src="https://latex.codecogs.com/png.latex?%5Cgamma_Z">. So far we have calculated the numerator of the FWL expression, showing that <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(Y,%5Ctilde%7BZ%7D)%20=%20-%5Clambda_X%20%5Ctext%7BCov%7D(X,U)">. The next step is to calculate <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVar%7D(%5Ctilde%7BZ%7D)">: <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BVar%7D(%5Ctilde%7BZ%7D)%20=%20%5Ctext%7BVar%7D(Z%20-%20%5Clambda_0%20-%20%5Clambda_X%20X)%20=%20%5Ctext%7BVar%7D(Z)%20+%20%5Clambda_X%5E2%20%5Ctext%7BVar%7D(X)%20-%202%5Clambda_X%20%5Ctext%7BCov%7D(X,Z).%0A"> Since <img src="https://latex.codecogs.com/png.latex?%5Clambda_X%20%5Cequiv%20%5Ctext%7BCov%7D(X,Z)/%5Ctext%7BVar%7D(X)">, our expression for <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVar%7D(%5Ctilde%7BZ%7D)"> simplifies to <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BVar%7D(%5Ctilde%7BZ%7D)%20=%20%5Ctext%7BVar%7D(Z)%20-%20%5Clambda_X%20%5Ctext%7BCov%7D(X,Z)%0A"> so we have discovered that: <img src="https://latex.codecogs.com/png.latex?%0A%5Cgamma_Z%20=%20%5Cfrac%7B-%5Clambda_X%20%5Ctext%7BCov%7D(X,U)%7D%7B%5Ctext%7BVar%7D(Z)%20-%20%5Clambda_X%20%5Ctext%7BCov%7D(X,Z)%7D.%0A"></p>
<p>Call me old-fashioned, but I <em>really</em> don’t like having <img src="https://latex.codecogs.com/png.latex?%5Clambda_X"> in that expression. I’d feel much happier if we could find a way to re-write this in terms of the more familiar IV first-stage coefficient <img src="https://latex.codecogs.com/png.latex?%5Cpi_Z">. Let’s give it a try! Let’s use my favorite trick of <em>multiplying by one</em>: <img src="https://latex.codecogs.com/png.latex?%0A%5Clambda_X%20%5Cequiv%20%5Cfrac%7B%5Ctext%7BCov%7D(X,Z)%7D%7B%5Ctext%7BVar%7D(X)%7D%20=%20%5Cfrac%7B%5Ctext%7BCov%7D(X,Z)%7D%7B%5Ctext%7BVar%7D(X)%7D%20%5Ccdot%20%5Cfrac%7B%5Ctext%7BVar%7D(Z)%7D%7B%5Ctext%7BVar%7D(Z)%7D%20=%20%5Cpi_Z%20%5Ccdot%20%5Cfrac%7B%5Ctext%7BVar%7D(Z)%7D%7B%5Ctext%7BVar%7D(X)%7D.%0A"> Substituting for <img src="https://latex.codecogs.com/png.latex?%5Clambda_X"> gives <img src="https://latex.codecogs.com/png.latex?%0A%5Cgamma_Z%20=%20%5Cfrac%7B-%5Cpi_Z%20%5Cfrac%7B%5Ctext%7BVar%7D(Z)%7D%7B%5Ctext%7BVar%7D(X)%7D%20%5Ctext%7BCov%7D(X,U)%7D%7B%5Ctext%7BVar%7D(Z)%20-%20%5Cpi_Z%20%5Cfrac%7B%5Ctext%7BVar%7D(Z)%7D%7B%5Ctext%7BVar%7D(X)%7D%20%5Ctext%7BCov%7D(X,Z)%7D%20=%20%5Cfrac%7B-%5Cpi_Z%20%5Ctext%7BCov%7D(X,U)%7D%7B%5Ctext%7BVar%7D(X)%20-%20%5Cpi_Z%5E2%20%5Ctext%7BVar%7D(Z)%7D.%0A"> We can simplify this even further by substituting <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVar%7D(V)%20=%20%5Ctext%7BVar%7D(X)%20-%20%5Cpi_Z%5E2%20%5Ctext%7BVar%7D(Z)"> from above to obtain <img src="https://latex.codecogs.com/png.latex?%0A%5Cgamma_Z%20=%20-%5Cpi_Z%20%5Cfrac%7B%5Ctext%7BCov%7D(X,U)%7D%7B%5Ctext%7BVar%7D(V)%7D.%0A"> And now we recognize something from above: <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(X,U)/%5Ctext%7BVar%7D(V)"> was the <em>bias</em> of <img src="https://latex.codecogs.com/png.latex?%5Cgamma_X"> relative to the true causal effect <img src="https://latex.codecogs.com/png.latex?%5Cbeta">! This means we can also write <img src="https://latex.codecogs.com/png.latex?%5Cgamma_Z%20=%20-%5Cpi_Z%20(%5Cgamma_X%20-%20%5Cbeta)">.</p>
</section>
<section id="a-little-simulation" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="a-little-simulation"><span class="header-section-number">3</span> A Little Simulation</h2>
<p>We seem to be doing an awful lot of algebra on this blog lately. To make sure that we haven’t made any silly mistakes, let’s check our work using a little simulation experiment taken from my <a href="https://www.econometrics.blog/post/a-good-instrument-is-a-bad-control/#a-simulation-example">earlier post</a>. Spoiler alert: everything checks out!</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1234</span>)</span>
<span id="cb1-2">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e5</span></span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulate instrument (z)</span></span>
<span id="cb1-5">z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(n)</span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulate error terms (u, v)</span></span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(mvtnorm)</span>
<span id="cb1-9">Rho <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, </span>
<span id="cb1-10">                <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">byrow =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb1-11">errors <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rmvnorm</span>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> Rho)</span>
<span id="cb1-12"></span>
<span id="cb1-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulate linear causal model</span></span>
<span id="cb1-14">u <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> errors[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb1-15">v <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> errors[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb1-16">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> z <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> v</span>
<span id="cb1-17">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> u</span>
<span id="cb1-18"></span>
<span id="cb1-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Regression of y on x and z</span></span>
<span id="cb1-20">gamma <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> z) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coefficients</span>()</span>
<span id="cb1-22"></span>
<span id="cb1-23">gamma</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>(Intercept)           x           z 
 -0.5471213   1.5018705  -0.3981116 </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># First-stage regression of x on z</span></span>
<span id="cb3-2">pi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> z) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coefficients</span>()</span>
<span id="cb3-4"></span>
<span id="cb3-5">pi</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>(Intercept)           z 
  0.5020338   0.7963889 </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compare two different expressions for gamma_Z to the estimate itself</span></span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gamma_z =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unname</span>(gamma[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]),</span>
<span id="cb5-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">version1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unname</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cov</span>(x, u) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">var</span>(v)),</span>
<span id="cb5-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">version2 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unname</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>pi[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (gamma[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb5-5">)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>   gamma_z   version1   version2 
-0.3981116 -0.4024918 -0.3996841 </code></pre>
</div>
</div>
</section>
<section id="making-sense-of-this-result" class="level2 page-columns page-full" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="making-sense-of-this-result"><span class="header-section-number">4</span> Making Sense of This Result</h2>
<p>So far all we’ve done is horrible, tedious algebra and a little simulation to check that it’s correct. But in fact there’s some very interesting intuition for the results we’ve obtained, intuition that is <em>deeply connected</em> to the idea of a bad control in a directed acyclic graph (DAG).</p>
<p>In the model we’ve described above, <img src="https://latex.codecogs.com/png.latex?Z"> has a causal effect on <img src="https://latex.codecogs.com/png.latex?Y">. This is because <img src="https://latex.codecogs.com/png.latex?Z"> causes <img src="https://latex.codecogs.com/png.latex?X"> which in turn causes <img src="https://latex.codecogs.com/png.latex?Y">. Because <img src="https://latex.codecogs.com/png.latex?Z"> is an instrument, its <em>only</em> effect on <img src="https://latex.codecogs.com/png.latex?Y"> goes through <img src="https://latex.codecogs.com/png.latex?X">. The unobserved confounder <img src="https://latex.codecogs.com/png.latex?U"> is a common cause of <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y"> but is unrelated to <img src="https://latex.codecogs.com/png.latex?Z">. Even if you’re not familiar with DAGs, you will probably find this diagram relatively intuitive:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggdag)</span>
<span id="cb7-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb7-3"></span>
<span id="cb7-4">iv_dag <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dagify</span>(</span>
<span id="cb7-5">  Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> U,</span>
<span id="cb7-6">  X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Z <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> U,</span>
<span id="cb7-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">coords =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb7-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Z =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">X =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">U =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>),</span>
<span id="cb7-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Z =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">X =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">U =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb7-10">  )</span>
<span id="cb7-11">)</span>
<span id="cb7-12"></span>
<span id="cb7-13">iv_dag <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggdag</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_econblog_void</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/a-good-instrument-is-a-bad-control-part-ii/index_files/figure-html/unnamed-chunk-3-1.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>In the figure, an arrow from <img src="https://latex.codecogs.com/png.latex?A"> to <img src="https://latex.codecogs.com/png.latex?B"> means that <img src="https://latex.codecogs.com/png.latex?A"> is a cause of <img src="https://latex.codecogs.com/png.latex?B">. A causal path is a sequence of arrows that “obeys one-way signs” and leads from <img src="https://latex.codecogs.com/png.latex?A"> to <img src="https://latex.codecogs.com/png.latex?B">. Because there is a directed path from <img src="https://latex.codecogs.com/png.latex?Z"> to <img src="https://latex.codecogs.com/png.latex?Y">, we say that <img src="https://latex.codecogs.com/png.latex?Z"> is a cause of <img src="https://latex.codecogs.com/png.latex?Y">. To see this using our regression equations from above, substitute the IV first-stage into the linear causal model to obtain <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0AY%20&amp;=%20%5Calpha%20+%20%5Cbeta%20X%20+%20U%20=%20%5Calpha%20+%20%5Cbeta%20(%5Cpi_0%20+%20%5Cpi_Z%20Z%20+%20V)%20+%20U%5C%5C%0A&amp;=%20(%5Calpha%20+%20%5Cbeta%20%5Cpi_0)%20+%20%5Cbeta%20%5Cpi_Z%20Z%20+%20(%5Cbeta%20V%20+%20U).%0A%5Cend%7Baligned%7D%0A"> This gives us a linear equation with <img src="https://latex.codecogs.com/png.latex?Y"> on the left-hand side and <img src="https://latex.codecogs.com/png.latex?Z"> <em>alone</em> on the right-hand side. This is called the “reduced-form” regression. Since <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(Z,U)=0"> by assumption and <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(Z,V)%20=%200"> by construction, the reduced-form is a <em>bona fide</em> population linear regression. That means that regressing <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?Z"> will indeed give us a slope that equals <img src="https://latex.codecogs.com/png.latex?%5Cpi_Z%20%5Ctimes%20%5Cbeta">. To see why the slope is a product, recall that <img src="https://latex.codecogs.com/png.latex?%5Cpi_Z"> is the causal effect of <img src="https://latex.codecogs.com/png.latex?Z"> on <img src="https://latex.codecogs.com/png.latex?X">, the <img src="https://latex.codecogs.com/png.latex?Z%5Crightarrow%20X"> arrow in the diagram, while <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> is the causal effect of <img src="https://latex.codecogs.com/png.latex?X"> on <img src="https://latex.codecogs.com/png.latex?Y">, the <img src="https://latex.codecogs.com/png.latex?X%20%5Crightarrow%20Y"> arrow in the diagram. Because the only way <img src="https://latex.codecogs.com/png.latex?Z"> can influence <img src="https://latex.codecogs.com/png.latex?Y"> is through <img src="https://latex.codecogs.com/png.latex?X">, it makes sense that the causal effect of <img src="https://latex.codecogs.com/png.latex?Z"> on <img src="https://latex.codecogs.com/png.latex?Y"> is the <em>product</em> of these two effects.</p>
<p>So now we see that the reduced-form coefficient <img src="https://latex.codecogs.com/png.latex?%5Cpi_Z%20%5Cbeta"> is indeed a causal effect. How does this relate to <img src="https://latex.codecogs.com/png.latex?%5Cgamma_Z">? Remember that <img src="https://latex.codecogs.com/png.latex?%5Cgamma_Z"> was the coefficient on <img src="https://latex.codecogs.com/png.latex?Z"> in a regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?Z"> and <img src="https://latex.codecogs.com/png.latex?X">, in other words a regression that <em>adjusted</em> for <img src="https://latex.codecogs.com/png.latex?X">. So is adjusting for <img src="https://latex.codecogs.com/png.latex?X"> the right call? Absolutely not! There are no back-door paths between <img src="https://latex.codecogs.com/png.latex?Z"> and <img src="https://latex.codecogs.com/png.latex?Y">.<sup>3</sup> This means that we <em>don’t have to adjust</em> for anything to learn the causal effect of <img src="https://latex.codecogs.com/png.latex?Z"> on <img src="https://latex.codecogs.com/png.latex?Y">. In fact adjusting for <img src="https://latex.codecogs.com/png.latex?X"> is a mistake for <em>two different reasons</em>.</p>
<div class="no-row-height column-margin column-container"><div id="fn3"><p><sup>3</sup>&nbsp;The rest of this post relies on some DAG basics. If anything here is unfamiliar, check out my <a href="https://www.treatment-effects.com/basics/">treatment effects slides</a>.</p></div></div><p>First, <img src="https://latex.codecogs.com/png.latex?X"> is a mediator on the path <img src="https://latex.codecogs.com/png.latex?Z%20%5Crightarrow%20X%20%5Crightarrow%20Y">. If there were no confounding, i.e.&nbsp;if <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(X,U)%20=%200"> so there is no <img src="https://latex.codecogs.com/png.latex?U%5Crightarrow%20X"> arrow, adjusting for <img src="https://latex.codecogs.com/png.latex?X"> would <em>block</em> the only causal path from <img src="https://latex.codecogs.com/png.latex?Z"> to <img src="https://latex.codecogs.com/png.latex?Y">. We can see this in our equations from above. Suppose that <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(X,U)%20=%200">. Then we have <img src="https://latex.codecogs.com/png.latex?%5Cgamma_X%20=%20%5Cbeta"> but <img src="https://latex.codecogs.com/png.latex?%5Cgamma_Z%20=%200">! There was a dead giveaway in our derivation: the formula for <img src="https://latex.codecogs.com/png.latex?%5Cgamma_Z"> doesn’t depend on <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> at all.</p>
<p>Second, because there <em>is</em> confounding, adjusting for <img src="https://latex.codecogs.com/png.latex?X"> creates a spurious association between <img src="https://latex.codecogs.com/png.latex?Z"> and <img src="https://latex.codecogs.com/png.latex?Y"> through the back-door path <img src="https://latex.codecogs.com/png.latex?Z%20%5Crightarrow%20X%20%5Cleftarrow%20U%20%5Crightarrow%20Y">. Because <img src="https://latex.codecogs.com/png.latex?X"> is a collider on the path <img src="https://latex.codecogs.com/png.latex?Z%20%5Crightarrow%20X%20%5Cleftarrow%20U%20%5Crightarrow%20Y">, this path starts out <em>closed</em>. Adjusting for <img src="https://latex.codecogs.com/png.latex?X"> <em>opens</em> this back-door path, creating a spurious association between <img src="https://latex.codecogs.com/png.latex?Z"> and <img src="https://latex.codecogs.com/png.latex?Y">. To see why this is the case, suppose that <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%200">. In this case there is <em>no causal effect</em> of <img src="https://latex.codecogs.com/png.latex?X"> on <img src="https://latex.codecogs.com/png.latex?Y"> and hence no causal effect of <img src="https://latex.codecogs.com/png.latex?Z"> on <img src="https://latex.codecogs.com/png.latex?Y">. But if <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(X,U)%20%5Cneq%200">, then we have <img src="https://latex.codecogs.com/png.latex?%5Cgamma_Z%20%5Cneq%200">!</p>
<p>So if you want to learn the causal effect of <img src="https://latex.codecogs.com/png.latex?Z"> on <img src="https://latex.codecogs.com/png.latex?Y">, it’s not just that <img src="https://latex.codecogs.com/png.latex?X"> is a <strong>bad control</strong>; it’s a doubly bad control! Without adjusting for <img src="https://latex.codecogs.com/png.latex?X">, everything is fine: the reduced-form regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?Z"> gives us exactly what we’re after.<sup>4</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn4"><p><sup>4</sup>&nbsp;Here I assume that we’re interested in the <img src="https://latex.codecogs.com/png.latex?Z%5Crightarrow%20Y"> causal effect. To obtain the <img src="https://latex.codecogs.com/png.latex?X%5Crightarrow%20Y"> effect we would need to use an instrumental variables regression.</p></div></div></section>
<section id="epilogue" class="level2" data-number="5">
<h2 data-number="5" class="anchored" data-anchor-id="epilogue"><span class="header-section-number">5</span> Epilogue</h2>
<p>When I showed this post to another colleague he asked me whether there is any way to learn about <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> by <em>combining</em> <img src="https://latex.codecogs.com/png.latex?%5Cgamma_Z"> and <img src="https://latex.codecogs.com/png.latex?%5Cgamma_X">. The answer is no: the regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Z"> alone doesn’t contain enough information. Since <img src="https://latex.codecogs.com/png.latex?%0A%5Cgamma_Z%20=%20-%5Cpi_Z%20%5Cfrac%7B%5Ctext%7BCov%7D(X,U)%7D%7B%5Ctext%7BVar%7D(V)%7D%20%5Cquad%20%5Ctext%7Band%7D%20%5Cquad%20%5Cgamma_X%20=%20%5Cbeta%20+%20%5Cfrac%7B%5Ctext%7BCov%7D(X,U)%7D%7B%5Ctext%7BVar%7D(V)%7D%0A"> we can rearrange to obtain the following expression for <img src="https://latex.codecogs.com/png.latex?%5Cbeta">: <img src="https://latex.codecogs.com/png.latex?%0A%5Cbeta%20=%20%5Cgamma_X%20+%20%5Cfrac%7B%5Cgamma_Z%7D%7B%5Cpi_Z%7D%0A"> which we can verify in our little simulation example as follows:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">gamma[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> gamma[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>pi[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>       x 
1.001975 </code></pre>
</div>
</div>
<p>Thus, in order to solve for <img src="https://latex.codecogs.com/png.latex?%5Cbeta">, we need to run the first-stage regression to learn <img src="https://latex.codecogs.com/png.latex?%5Cpi_Z">.</p>


</section>


 ]]></description>
  <category>econometrics</category>
  <category>causal inference</category>
  <guid>https://www.econometrics.blog/post/a-good-instrument-is-a-bad-control-part-ii/</guid>
  <pubDate>Thu, 28 Aug 2025 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Two FWL Theorems for the Price of One</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/two-fwl-theorems-for-the-price-of-one/</link>
  <description><![CDATA[ 




<p>The result that I prefer to call <a href="https://www.econometrics.blog/post/how-to-do-regression-adjustment/#fnref2">Yule’s Rule</a>, more commonly known as the “Frisch-Waugh-Lovell (FWL) theorem”, shows how to calculate the regression slope coefficient for <strong>one predictor</strong> by carrying out additional “auxiliary” regressions that adjust for <strong>all other predictors</strong>. You’ve probably encountered this result if you’ve studied introductory econometrics. But it may surprise you to learn that there are actually <em>two</em> variants of the FWL theorem, each with its pros and cons. Today we’ll take a look at the less familiar version and then circle back to understand what makes the more familiar one a textbook staple.</p>
<section id="simulation-example" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="simulation-example"><span class="header-section-number">1</span> Simulation Example</h2>
<p>Let’s start with a little simulation. First we’ll generate 5000 observations of predictors <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?W"> from a joint normal distribution with standard deviations of one, means of zero, and a correlation of 0.5.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1066</span>)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(mvtnorm)</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulate linear regression with two predictors: X and W</span></span>
<span id="cb1-5">covariance_matrix <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(</span>
<span id="cb1-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), </span>
<span id="cb1-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb1-8">)</span>
<span id="cb1-9"></span>
<span id="cb1-10">n_sims <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span></span>
<span id="cb1-11"></span>
<span id="cb1-12">x_w <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rmvnorm</span>(</span>
<span id="cb1-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> n_sims,  </span>
<span id="cb1-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), </span>
<span id="cb1-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> covariance_matrix</span>
<span id="cb1-16">)</span>
<span id="cb1-17"></span>
<span id="cb1-18">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> x_w[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb1-19">w <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> x_w[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span></code></pre></div></div>
</div>
<p>Next we’ll simulate the outcome variable <img src="https://latex.codecogs.com/png.latex?Y"> where the true coefficient on <img src="https://latex.codecogs.com/png.latex?X"> is one and the true coefficient on <img src="https://latex.codecogs.com/png.latex?W"> is -1, adding standard normal errors.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> w <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(n_sims)</span></code></pre></div></div>
</div>
<p>Now we’ll run the “auxiliary regressions”. The first one regresses <img src="https://latex.codecogs.com/png.latex?X"> on <img src="https://latex.codecogs.com/png.latex?W"> and saves the residuals. Call these residuals <code>x_tilde</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Residuals from regression of X on W</span></span>
<span id="cb3-2">x_tilde <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> w) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">residuals</span>()</span></code></pre></div></div>
</div>
<p>The next one regresses <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?W"> and saves the residuals. Call these residuals <code>y_tilde</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Residuals from regression of Y on W</span></span>
<span id="cb4-2">y_tilde <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> w) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">residuals</span>()</span></code></pre></div></div>
</div>
<p>To make the code that follows a little simpler, I’ll also create a helper function that runs a linear regression and returns the coefficients after stripping away any variable names.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">get_coef <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(formula) {</span>
<span id="cb5-2">  formula <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span>  </span>
<span id="cb5-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb5-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb5-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unname</span>() </span>
<span id="cb5-6">}</span></code></pre></div></div>
</div>
<p>Now we’re ready to compare some regressions! The “long regression” is a standard linear regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?W">. The “FWL Standard” is a regression of <code>y_tilde</code> on <code>x_tilde</code>. In other words, it regresses the residuals of <img src="https://latex.codecogs.com/png.latex?Y"> on the residuals of <img src="https://latex.codecogs.com/png.latex?X">. The FWL as it is usually encountered in textbooks implies that we should recover the same coefficient on <img src="https://latex.codecogs.com/png.latex?X"> in “Long Regression” and in “FWL Standard”, and indeed the simulation bears this out.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb6-2">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Long Regression"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_coef</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> w)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb6-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FWL Standard"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_coef</span>(y_tilde <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x_tilde <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], </span>
<span id="cb6-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FWL Alternative"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_coef</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x_tilde)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb6-5">)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Long Regression    FWL Standard FWL Alternative 
      0.9937046       0.9937046       0.9937046 </code></pre>
</div>
</div>
<p>But now take a look at “FWL” alternative: this is a regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <code>x_tilde</code>. Compared to the standard FWL approach, this version <em>does not</em> residualize <img src="https://latex.codecogs.com/png.latex?Y"> with respect to <img src="https://latex.codecogs.com/png.latex?W">. But it still gives us <em>exactly</em> the same coefficient on <img src="https://latex.codecogs.com/png.latex?X"> as the other two regressions. That leaves us with two unanswered questions:</p>
<ol type="1">
<li>Why does the “alternative” FWL approach work?</li>
<li><em>Given</em> that the alternative approach works, why does anyone ever teach the “standard” version?</li>
</ol>
<p>In the rest of this post we’ll answer both questions using simple algebra and the properties of linear regression. There are lots of deep ideas here, but there’s no need to bring out the big matrix algebra guns to explain them.</p>
</section>
<section id="a-bit-of-notation" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="a-bit-of-notation"><span class="header-section-number">2</span> A Bit of Notation</h2>
<p>First we need a bit of notation. I find it a bit simpler to work with population linear regressions rather than sample regressions, but the ideas are the same either way. So if you prefer to put “hats” on everything and work with sums rather than expectations and covariances, be my guest!</p>
<p>First we’ll define the “Long Regression” as a <a href="https://www.econometrics.blog/post/why-econometrics-is-confusing-part-1-the-error-term/">population linear regression</a> of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?W">, namely <img src="https://latex.codecogs.com/png.latex?%0AY%20=%20%5Cbeta_0%20+%20%5Cbeta_X%20X%20+%20%5Cbeta_W%20W%20+%20U,%20%5Cquad%20%5Cmathbb%7BE%7D(U)%20=%20%5Ctext%7BCov%7D(X,U)%20=%20%5Ctext%7BCov%7D(W,U)=0.%0A"> Next I’ll define two additional population linear regressions: first the regression of <img src="https://latex.codecogs.com/png.latex?X"> on <img src="https://latex.codecogs.com/png.latex?W"> <img src="https://latex.codecogs.com/png.latex?%0AX%20=%20%5Cgamma_0%20+%20%5Cgamma_W%20W%20+%20%5Ctilde%7BX%7D,%20%5Cquad%20%5Cmathbb%7BE%7D(%5Ctilde%7BX%7D)%20=%20%5Ctext%7BCov%7D(W,%5Ctilde%7BX%7D)=0%0A"> and second the regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?W"> <img src="https://latex.codecogs.com/png.latex?%0AY%20=%20%5Cdelta_0%20+%20%5Cdelta_W%20W%20+%20%5Ctilde%7BY%7D,%20%5Cquad%20%5Cmathbb%7BE%7D(%5Ctilde%7BY%7D)%20=%20%5Ctext%7BCov%7D(W,%5Ctilde%7BY%7D)=0.%0A"> I’ve already linked to a post making this point, but it bears repeating: all of the properties of the error terms <img src="https://latex.codecogs.com/png.latex?U">, <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BX%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BY%7D"> that I’ve stated here hold <em>by construction</em>. They are not assumptions; they are merely <a href="https://www.econometrics.blog/post/why-econometrics-is-confusing-part-1-the-error-term/">what defines an error term</a> in a population linear regression.</p>
</section>
<section id="why-does-the-alternative-fwl-approach-work" class="level2 page-columns page-full" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="why-does-the-alternative-fwl-approach-work"><span class="header-section-number">3</span> Why does the “alternative” FWL approach work?</h2>
<p>As mentioned in the discussion of our simulation experiment from above, the standard FWL theorem says that a regression of <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BY%7D"> on <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BX%7D"> with no intercept gives us <img src="https://latex.codecogs.com/png.latex?%5Cbeta_X">, while the <em>alternative</em> version says that a regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BX%7D"> with an intercept also gives us <img src="https://latex.codecogs.com/png.latex?%5Cbeta_X">. It is the second claim that we’ll prove now.<sup>1</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;Fear not: we’ll return to the first claim soon!</p></div></div><p>The alternative FWL theorem claims that <img src="https://latex.codecogs.com/png.latex?%5Cbeta_X%20=%20%5Ctext%7BCov%7D(Y,%5Ctilde%7BX%7D)/%5Ctext%7BVar%7D(%5Ctilde%7BX%7D)">. Since <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BX%7D"> is uncorrelated with <img src="https://latex.codecogs.com/png.latex?W"> by construction, we can <a href="https://github.com/fditraglia/random-variables-cheatsheet/blob/main/random-variables-cheatsheet.pdf">expand the numerator</a> as follows: <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BCov%7D(Y,%5Ctilde%7BX%7D)%20=%20%5Ctext%7BCov%7D(%5Cbeta_0%20+%20%5Cbeta_X%20X%20+%20%5Cbeta_W%20W%20+%20U,%20%5Ctilde%7BX%7D)%20=%20%5Cbeta_X%20%5Ctext%7BCov%7D(X,%5Ctilde%7BX%7D)%20+%20%5Ctext%7BCov%7D(U,%5Ctilde%7BX%7D).%0A"> But since <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BX%7D%20=%20(X%20-%20%5Cgamma_0%20-%20%5Cgamma_W%20W)"> we also have <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BCov%7D(U,%20%5Ctilde%7BX%7D)%20=%20%5Ctext%7BCov%7D(U,%20X%20-%20%5Cgamma_0%20-%20%5Cgamma_W%20W)%20=%20%5Ctext%7BCov%7D(U,X)%20-%20%5Cgamma_W%20%5Ctext%7BCov%7D(U,W)%20=%200%0A"> since <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?W"> are uncorrelated with <img src="https://latex.codecogs.com/png.latex?U"> by construction. So to prove our original claim it suffices to show that <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(X,%5Ctilde%7BX%7D)%20=%20%5Ctext%7BVar%7D(%5Ctilde%7BX%7D)">. To see why this holds, first write <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BCov%7D(X,%20%5Ctilde%7BX%7D)%20=%20%5Ctext%7BCov%7D(X,%20X%20-%20%5Cgamma_0%20-%20%5Cgamma_W%20W)%20=%20%5Ctext%7BVar%7D(X)%20-%20%5Cgamma_W%20%5Ctext%7BCov%7D(X,W).%0A"> using <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(X,X)%20=%20%5Ctext%7BVar%7D(X)">. Next, expand <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVar%7D(%5Ctilde%7BX%7D)"> as follows: <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BVar%7D(%5Ctilde%7BX%7D)%20=%20%5Ctext%7BVar%7D(X%20-%20%5Cgamma_0%20-%20%5Cgamma_W%20W)%20=%20%5Ctext%7BVar%7D(X)%20+%20%5Cgamma_W%5E2%20%5Ctext%7BVar%7D(W)%20-%202%20%5Cgamma_W%20%5Ctext%7BCov%7D(X,W).%0A"> and then subtract <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(X,%5Ctilde%7BX%7D)"> from <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVar%7D(%5Ctilde%7BX%7D)">: <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BVar%7D(%5Ctilde%7BX%7D)%20-%20%5Ctext%7BCov%7D(X,%5Ctilde%7BX%7D)%20=%20%5Cgamma_W%20%5Cleft%5B%20%5Cgamma_W%20%5Ctext%7BVar%7D(W)%20-%20%5Ctext%7BCov%7D(X,W)%20%5Cright%5D.%0A"> This shows that <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVar%7D(%5Ctilde%7BX%7D)"> and <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(X,%5Ctilde%7BX%7D)"> are equal if and only if <img src="https://latex.codecogs.com/png.latex?%5Cgamma_W%20%5Ctext%7BVar%7D(W)%20=%20%5Ctext%7BCov%7D(X,W)">. But since <img src="https://latex.codecogs.com/png.latex?%5Cgamma_W"> is the coefficient from the regression of <img src="https://latex.codecogs.com/png.latex?X"> on <img src="https://latex.codecogs.com/png.latex?W">, we already know that <img src="https://latex.codecogs.com/png.latex?%5Cgamma_W%20=%20%5Ctext%7BCov%7D(X,W)/%5Ctext%7BVar%7D(W)">! With a bit of algebra using the properties of covariance and the definition of a population linear regression, we’ve shown that the alternative FWL theorem holds.</p>
</section>
<section id="whats-different-about-the-usual-fwl-theorem" class="level2" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="whats-different-about-the-usual-fwl-theorem"><span class="header-section-number">4</span> What’s different about the “usual” FWL theorem?</h2>
<p>At this point you may be wondering why anyone teaches the “usual” version of the FWL theorem at all. If that extra short regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?W"> isn’t needed to learn <img src="https://latex.codecogs.com/png.latex?%5Cbeta_X">, why bother?</p>
<p>To answer this question, we’ll start by re-writing the long regression two different ways. First, we’ll substitute <img src="https://latex.codecogs.com/png.latex?X%20=%20%5Cgamma_0%20+%20%5Cgamma_W%20W%20+%20%5Ctilde%7BX%7D"> into the long regression and re-arrange, yielding <img src="https://latex.codecogs.com/png.latex?%0AY%20=%20(%5Cbeta_0%20+%20%5Cbeta_X%20%5Cgamma_0)%20+%20%5Cbeta_X%20%5Ctilde%7BX%7D%20+%20(%5Cbeta_W%20+%20%5Cbeta_X%20%5Cgamma_W)%20W%20+%20%20U.%0A"> Next we’ll substitute <img src="https://latex.codecogs.com/png.latex?Y%20=%20%5Cdelta_0%20+%20%5Cdelta_W%20W%20+%20%5Ctilde%7BY%7D"> on the left-hand side of the preceding equation and rearrange to isolate <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BY%7D">. This leaves us with <img src="https://latex.codecogs.com/png.latex?%0A%5Ctilde%7BY%7D%20=%20(%5Cbeta_0%20+%20%5Cbeta_X%20%5Cgamma_0%20-%20%5Cdelta_0)%20+%20%5Cbeta_X%20%5Ctilde%7BX%7D%20+%20(%5Cbeta_W%20+%20%5Cbeta_X%20%5Cgamma_W%20-%20%5Cdelta_W)%20W%20+%20U.%0A"> Now we have two expressions, each with <img src="https://latex.codecogs.com/png.latex?%5Cbeta_X%20%5Ctilde%7BX%7D"> as one of the terms on the right-hand side and <img src="https://latex.codecogs.com/png.latex?U"> as another. Notice that both expressions have an intercept and a term in which <img src="https://latex.codecogs.com/png.latex?W"> is multiplied by a constant. What’s more, the intercepts are closely related across the two equations, as are the <img src="https://latex.codecogs.com/png.latex?W"> coefficients. I’m now going to make a bold assertion: the intercept and <img src="https://latex.codecogs.com/png.latex?W"> coefficient in the second expression, the <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BY%7D"> one, are <strong>both equal to zero</strong> <img src="https://latex.codecogs.com/png.latex?%0A%5Cbeta_0%20+%20%5Cbeta_X%20%5Cgamma_0%20-%20%5Cdelta_0%20=%200,%20%5Cquad%20%5Ctext%7Band%7D%20%5Cquad%20%5Cbeta_W%20+%20%5Cbeta_X%20%5Cgamma_W%20-%20%5Cdelta_W%20=%200.%0A"> Perhaps you don’t believe me, but just for the moment <em>suppose that I’m correct</em>. In this case it would immediately follow that <img src="https://latex.codecogs.com/png.latex?%0A%5Cbeta_0%20+%20%5Cbeta_X%20%5Cgamma_0%20=%20%5Cdelta_0,%20%5Cquad%20%5Ctext%7Band%7D%20%5Cquad%20%5Cbeta_W%20+%20%5Cbeta_X%20%5Cgamma_W%20=%20%5Cdelta_W%0A"> leaving us with two simple linear regressions, namely <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0AY%20&amp;=%20%5Cdelta_0%20+%20%5Cbeta_X%20%5Ctilde%7BX%7D%20+%20(%5Cbeta_W%20W%20+%20U)%5C%5C%0A%5Ctilde%7BY%7D%20&amp;=%20%5Cbeta_X%20%5Ctilde%7BX%7D%20+%20U.%0A%5Cend%7Baligned%7D%0A"> We’re tantalizingly close to unraveling the mystery of why the “usual” FWL theorem is so popular. But first we need to verify my bold claim from the previous paragraph. To do so, we’ll fall back on our old friend: the <em>omitted variable bias formula</em>, also known as the regression anatomy formula: <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cdelta_W%20&amp;%5Cequiv%20%5Cfrac%7B%5Ctext%7BCov%7D(Y,W)%7D%7B%5Ctext%7BVar%7D(W)%7D%20=%20%5Cfrac%7B%5Ctext%7BCov%7D(%5Cbeta_0%20+%20%5Cbeta_X%20X%20+%20%5Cbeta_W%20W%20+%20U,%20W)%7D%7B%5Ctext%7BVar%7D(W)%7D%20=%20%5Cfrac%7B%5Cbeta_W%20%5Ctext%7BVar%7D(W)%20+%20%5Cbeta_X%20%5Ctext%7BCov%7D(X,W)%7D%7B%5Ctext%7BVar%7D(W)%7D%5C%5C%0A&amp;=%20%5Cbeta_W%20+%20%5Cbeta_X%20%5Cfrac%7B%5Ctext%7BCov%7D(X,W)%7D%7B%5Ctext%7BVar%7D(W)%7D%20=%20%5Cbeta_W%20+%20%5Cbeta_X%20%5Cgamma_W.%0A%5Cend%7Baligned%7D%0A"> Thus, <img src="https://latex.codecogs.com/png.latex?%5Cbeta_W%20+%20%5Cbeta_X%20%5Cgamma_W%20-%20%5Cdelta_W%20=%200"> as claimed. One down, one more to go. By definition, <img src="https://latex.codecogs.com/png.latex?%5Cdelta_0%20=%20%5Cmathbb%7BE%7D(Y)%20-%20%5Cdelta_W%20%5Cmathbb%7BE%7D(W)">. Substituting the long regression for <img src="https://latex.codecogs.com/png.latex?Y">, we have <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cdelta_0%20&amp;=%20%5Cmathbb%7BE%7D(%5Cbeta_0%20+%20%5Cbeta_X%20X%20+%20%5Cbeta_W%20W%20+%20U)%20-%20%5Cdelta_W%20%5Cmathbb%7BE%7D(W)%5C%5C%0A&amp;=%20%5Cbeta_0%20+%20%5Cbeta_X%20%5Cmathbb%7BE%7D(X)%20+%20(%5Cbeta_W%20-%20%5Cdelta_W)%20%5Cmathbb%7BE%7D(W)%0A%5Cend%7Baligned%7D%0A"> by the linearity of expectation and the fact that <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(U)%20=%200"> by construction. Now, we’re <em>trying to show</em> that <img src="https://latex.codecogs.com/png.latex?%5Cdelta_0%20=%20%5Cbeta_0%20+%20%5Cbeta_X%20%5Cgamma_0">. Substituting for <img src="https://latex.codecogs.com/png.latex?%5Cgamma_0"> in this expression gives <img src="https://latex.codecogs.com/png.latex?%0A%5Cbeta_0%20+%20%5Cbeta_X%20%5Cgamma_0%20=%20%5Cbeta_0%20+%20%5Cbeta_X%20%5B%5Cmathbb%7BE%7D(X)%20-%20%5Cgamma_W%20%5Cmathbb%7BE%7D(W)%5D%20=%20%5Cbeta_0%20+%20%5Cbeta_X%20%5Cmathbb%7BE%7D(X)%20-%20%5Cbeta_X%20%5Cgamma_W%20%5Cmathbb%7BE%7D(W).%0A"> Inspecting our work so far, we see that the two alternative expressions for <img src="https://latex.codecogs.com/png.latex?%5Cdelta_0"> will be equal precisely when <img src="https://latex.codecogs.com/png.latex?%5Cbeta_X%20%5Cgamma_W%20=%20%5Cdelta_W%20-%20%5Cbeta_W">. But re-arranging this gives <img src="https://latex.codecogs.com/png.latex?%5Cdelta_W%20=%20%5Cbeta_W%20+%20%5Cbeta_X%20%5Cgamma_W">, which we already proved above using the omitted variables bias formula!</p>
</section>
<section id="taking-stock" class="level2" data-number="5">
<h2 data-number="5" class="anchored" data-anchor-id="taking-stock"><span class="header-section-number">5</span> Taking Stock</h2>
<p>That was a lot of algebra, so let’s spend some time thinking about the results. We showed that <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0AY%20&amp;=%20%5Cdelta_0%20+%20%5Cbeta_X%20%5Ctilde%7BX%7D%20+%20(%5Cbeta_W%20W%20+%20U)%5C%5C%0A%5Ctilde%7BY%7D%20&amp;=%20%5Cbeta_X%20%5Ctilde%7BX%7D%20+%20U.%0A%5Cend%7Baligned%7D%0A"> Now, if you’ll permit me, I’d like to re-write that first equality as <img src="https://latex.codecogs.com/png.latex?%0AY%20=%20%5Cdelta_0%20+%20%5Cbeta_X%20%5Ctilde%7BX%7D%20+%20V,%20%5Cquad%20%5Ctext%7Bwhere%20%7D%20V%20%5Cequiv%20%5Cbeta_W%20W%20+%20U.%0A"> Since <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BX%7D"> is uncorrelated with <img src="https://latex.codecogs.com/png.latex?U">, as explained above, and since <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(U)%20=%200"> by construction, it follows that <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BY%7D%20=%20%5Cbeta_X%20%5Ctilde%7BX%7D%20+%20U"> is a <em>bona fide</em> population linear regression model. If we regress <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BY%7D"> on <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BX%7D"> the slope coefficient will be <img src="https://latex.codecogs.com/png.latex?%5Cbeta_X"> and the error term will be <img src="https://latex.codecogs.com/png.latex?U">. This regression corresponds to the <em>standard</em> FWL theorem. Notice that it has an intercept of <em>zero</em> and an error term that is <em>identical</em> to that of the long regression. We can verify this using our simulation experiment from above as follows:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Standard FWL has same residuals as long regression</span></span>
<span id="cb8-2">u_hat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">resid</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> w))</span>
<span id="cb8-3">u_tilde <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">resid</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(y_tilde <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x_tilde <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb8-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all.equal</span>(u_hat, u_tilde)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Standard FWL has an intercept of zero (to machine precision!)</span></span>
<span id="cb10-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(y_tilde <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x_tilde))[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># fit with intercept; check it's (numerically) 0</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code> (Intercept) 
8.273433e-17 </code></pre>
</div>
</div>
<p>So what about <img src="https://latex.codecogs.com/png.latex?Y%20=%20%5Cdelta_0%20+%20%5Cbeta_X%20%5Ctilde%7BX%7D%20+%20V">? This is the regression that corresponds to the <em>alternative</em> FWL theorem. Since <img src="https://latex.codecogs.com/png.latex?V%20=%20%5Cbeta_W%20W%20+%20U"> and <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BX%7D"> is uncorrelated with both <img src="https://latex.codecogs.com/png.latex?U"> and <img src="https://latex.codecogs.com/png.latex?W">, this too is a population regression. But unless <img src="https://latex.codecogs.com/png.latex?%5Cbeta_W%20=%200">, it has a <em>different error term</em>. In other words, <img src="https://latex.codecogs.com/png.latex?V%20%5Cneq%20U">. Moreover, this regression <em>includes an intercept</em> that is not in general zero. Again we can verify this using our simulation example from above:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Alternative FWL has different residuals than long regression</span></span>
<span id="cb12-2">v_hat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">resid</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x_tilde))</span>
<span id="cb12-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all.equal</span>(u_hat, v_hat)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Mean relative difference: 0.4905107"</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Alternative FWL has a non-zero intercept</span></span>
<span id="cb14-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x_tilde))[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>(Intercept) 
  0.4878453 </code></pre>
</div>
</div>
</section>
<section id="the-punchline" class="level2" data-number="6">
<h2 data-number="6" class="anchored" data-anchor-id="the-punchline"><span class="header-section-number">6</span> The Punchline</h2>
<p>If your goal is <em>merely</em> to learn <img src="https://latex.codecogs.com/png.latex?%5Cbeta_X">, then either version of the FWL theorem will do the trick and the alternative version is <em>simpler</em> because it only involves one auxiliary regression instead of two. But if you want to ensure that you end up with the same <em>error term</em> as in the original long regression, then you need to use the <em>standard</em> version of the FWL theorem. This is crucial for the purposes of <em>inference</em> because the properties of the error term determine the standard errors of your estimates.</p>


</section>


 ]]></description>
  <category>econometrics</category>
  <guid>https://www.econometrics.blog/post/two-fwl-theorems-for-the-price-of-one/</guid>
  <pubDate>Thu, 14 Aug 2025 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Econometrics Puzzler #2: Fitting a Regression with Fitted Values</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/econometrics-puzzler-2-fitting-a-regression-with-fitted-values/</link>
  <description><![CDATA[ 




<p>Suppose I run a simple linear regression of an outcome variable on a predictor variable. If I save the fitted values from this regression and then run a <em>second</em> regression of the outcome variable on the fitted values, what will I get? For extra credit: how will the R-squared from the second regression compare to that from the first regression?</p>
<section id="example-height-and-handspan" class="level2 page-columns page-full" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="example-height-and-handspan"><span class="header-section-number">1</span> Example: Height and Handspan</h2>
<p>Here’s a simple example: a regression of height, measured in inches, on handspan, measured in centimeters.<sup>1</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;In case you don’t know what handspan is: stretch out your dominant hand, and measure from the tip of your thumb to the tip of your pinky finger. This is your handspan. I collected this dataset from many years of <a href="https://ditraglia.com/Econ103Public">Econ 103</a> classes at UPenn.</p></div></div><div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(broom)</span>
<span id="cb1-3">dat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'https://ditraglia.com/data/height-handspan.csv'</span>)</span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(dat, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> height, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> handspan)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Height (in)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Handspan (cm)"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/econometrics-puzzler-2-fitting-a-regression-with-fitted-values/index_files/figure-html/unnamed-chunk-2-1.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fit the regression</span></span>
<span id="cb2-2">reg1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(height <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> handspan, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dat)</span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tidy</span>(reg1)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  &lt;chr&gt;          &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;    &lt;dbl&gt;
1 (Intercept)    40.9     1.67        24.5 9.19e-76
2 handspan        1.27    0.0775      16.3 3.37e-44</code></pre>
</div>
</div>
<p>As expected, bigger people are bigger in all dimensions, on average, so we see a positive relationship between handspan and height. Now let’s save the fitted values from this regression and run a second regression of height on the fitted values:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">dat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> reg1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb4-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">augment</span>(dat)</span>
<span id="cb4-3">reg2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(height <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> .fitted, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dat)</span>
<span id="cb4-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tidy</span>(reg2)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 2 × 5
  term         estimate std.error statistic   p.value
  &lt;chr&gt;           &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;
1 (Intercept) -1.51e-13    4.17   -3.62e-14 1.000e+ 0
2 .fitted      1.00e+ 0    0.0612  1.63e+ 1 3.37 e-44</code></pre>
</div>
</div>
<p>The intercept isn’t <em>quite</em> zero, but it’s about as close as we can reasonably expect to get on a computer and the slope is <em>exactly</em> one. Now how about the R-squared? Let’s check:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glance</span>(reg1)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 12
  r.squared adj.r.squared sigma statistic  p.value    df logLik   AIC   BIC
      &lt;dbl&gt;         &lt;dbl&gt; &lt;dbl&gt;     &lt;dbl&gt;    &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
1     0.452         0.450  3.02      267. 3.37e-44     1  -822. 1650. 1661.
# ℹ 3 more variables: deviance &lt;dbl&gt;, df.residual &lt;int&gt;, nobs &lt;int&gt;</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glance</span>(reg2)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 12
  r.squared adj.r.squared sigma statistic  p.value    df logLik   AIC   BIC
      &lt;dbl&gt;         &lt;dbl&gt; &lt;dbl&gt;     &lt;dbl&gt;    &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
1     0.452         0.450  3.02      267. 3.37e-44     1  -822. 1650. 1661.
# ℹ 3 more variables: deviance &lt;dbl&gt;, df.residual &lt;int&gt;, nobs &lt;int&gt;</code></pre>
</div>
</div>
<p>The R-squared values from the two regressions are <em>identical</em>! Surprised? Now’s your last chance to think it through on your own before I give my solution.</p>
</section>
<section id="solution" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="solution"><span class="header-section-number">2</span> Solution</h2>
<div class="solution callout callout-style-simple callout-note no-icon callout-titled" title="Solution">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Solution
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Suppose we wanted to choose <img src="https://latex.codecogs.com/png.latex?%5Calpha_0"> and <img src="https://latex.codecogs.com/png.latex?%5Calpha_1"> to minimize <img src="https://latex.codecogs.com/png.latex?%5Csum_%7Bi=1%7D%5En%20(Y_i%20-%20%5Calpha_0%20-%20%5Calpha_1%20%5Cwidehat%7BY%7D_i)%5E2"> where <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7BY%7D_i%20=%20%5Cwidehat%7B%5Cbeta%7D_0%20+%20%5Cwidehat%7B%5Cbeta%7D_1%20X_i">. This is equivalent to minimizing <img src="https://latex.codecogs.com/png.latex?%0A%5Csum_%7Bi=1%7D%5En%20%5Cleft%5BY_i%20-%20(%5Calpha_0%20+%20%5Calpha_1%20%5Cwidehat%7B%5Cbeta%7D_0)%20-%20(%5Calpha_1%5Cwidehat%7B%5Cbeta%7D_1)X_i%5Cright%5D%5E2.%0A"> By construction <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7B%5Cbeta%7D_0"> and <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7B%5Cbeta%7D_1"> minimize <img src="https://latex.codecogs.com/png.latex?%5Csum_%7Bi=1%7D%5En%20(Y_i%20-%20%5Cbeta_0%20-%20%20%5Cbeta_1%20X_i)%5E2">, so unless <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7B%5Calpha%7D_0%20=%200"> and <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7B%5Calpha%7D_1%20=%201"> we’d have a contradiction!</p>
<p>Similar reasoning explains why the R-squared values for the two regressions are the same. The R-squared of a regression equals <img src="https://latex.codecogs.com/png.latex?1%20-%20%5Ctext%7BSS%7D_%7B%5Ctext%7Bresidual%7D%7D%20/%20%5Ctext%7BSS%7D_%7B%5Ctext%7Btotal%7D%7D"> <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BSS%7D_%7B%5Ctext%7Btotal%7D%7D%20=%20%5Csum_%7Bi=1%7D%5En%20(Y_i%20-%20%5Cbar%7BY%7D)%5E2,%5Cquad%0A%5Ctext%7BSS%7D_%7B%5Ctext%7Bresidual%7D%7D%20=%20%5Csum_%7Bi=1%7D%5En%20(Y_i%20-%20%5Cwidehat%7BY%7D_i)%5E2%0A"> The total sum of squares is the same for both regressions because they have the same outcome variable. The residual sum of squares is the same because <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7B%5Calpha%7D_0%20=%200"> and <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7B%5Calpha%7D_1%20=%201"> together imply that both regressions have the same fitted values.</p>
<p>Here I focused on the case of a simple linear regression, one with a single predictor variable, but the same basic idea holds in general.</p>
</div>
</div>
</div>


</section>


 ]]></description>
  <category>econometrics</category>
  <category>puzzler</category>
  <guid>https://www.econometrics.blog/post/econometrics-puzzler-2-fitting-a-regression-with-fitted-values/</guid>
  <pubDate>Thu, 24 Jul 2025 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Econometrics Puzzler #1: To Instrument or Not?</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/econometrics-puzzler-1-to-instrument-or-not/</link>
  <description><![CDATA[ 




<p>Welcome to the first installment of the <em>Econometrics Puzzler</em>, a new series of shorter posts that will test and strengthen your econometric intuition.<sup>1</sup> Here’s the format: I’ll pose a question that requires only introductory econometrics knowledge, but has an unexpected answer. The idea is for you to ponder the question before reading my solution. Many of these questions are based on common misconceptions that come up year-after-year in my econometrics teaching. I hope you’ll find them both challenging and enlightening. Today we’ll revisit everyone’s favorite example: Angrist &amp; Krueger’s 1991 paper on the returns to education.<sup>2</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;When I wrote this puzzler, it was literally the first in the series, but now it’s only <em>technically</em> the first: I realized that an <a href="https://www.econometrics.blog/post/is-it-better-to-improve-sensitivity-or-specificity/">older post</a> was really a puzzler in disguise and decided to bring it into the series as “puzzler zero”!</p></div><div id="fn2"><p><sup>2</sup>&nbsp;I’m sick of this example too, but the point of this puzzler is to get you thinking about instrumental variables; using an example that most people know will get us to the punch line faster.</p></div></div><section id="to-instrument-or-not-to-instrument" class="level2 page-columns page-full" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="to-instrument-or-not-to-instrument"><span class="header-section-number">1</span> To Instrument or Not to Instrument?</h2>
<p>Suppose I want to predict someone’s wage as accurately as possible using a linear model–that is, I want my predictions to be as close as they can be to the actual wages. (In fact we will predict the <em>log</em> of wage.) I observe a representative sample of workers that includes their log wage <img src="https://latex.codecogs.com/png.latex?Y_i"> and years of schooling <img src="https://latex.codecogs.com/png.latex?X_i">. I could use an OLS regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X"> to make my predictions, but years of schooling are the classic example of an endogenous regressor; they’re correlated with myriad unobserved causes of wages, like “ability” and family background. Fortunately, I also have a valid and relevant instrument: quarter of birth <img src="https://latex.codecogs.com/png.latex?Z_i"> is correlated with years of schooling and (supposedly) uncorrelated with unobserved causes of wage.<sup>3</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn3"><p><sup>3</sup>&nbsp;If you’re unfamiliar with this example check out my <a href="https://youtu.be/NeAkMcgdWxA?si=XHsvGG5aPMNvMUfs&amp;t=2034">video overview</a>, including some discussion of why quarter of birth might <em>not</em> really be exogenous after all!</p></div></div><p>So here’s the question: <strong>to get the best possible predictions of wage from the information I have, should I run OLS or IV?</strong> More specifically, let’s use mean squared error (MSE) as our measure of “best”. To borrow a term from <a href="https://www.3blue1brown.com/">Grant Sanderson</a>, “pause and ponder” before reading further.</p>
</section>
<section id="solution" class="level2 page-columns page-full" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="solution"><span class="header-section-number">2</span> Solution</h2>
<div class="solution callout callout-style-simple callout-note no-icon callout-titled" title="Solution">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Solution
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<section id="taking-it-to-the-data" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="taking-it-to-the-data"><span class="header-section-number">3</span> Taking it to the Data</h2>
<p>The Angrist &amp; Krueger (1991) dataset is available from Michal Kolesár’s <a href="https://github.com/kolesarm/ManyIV?tab=readme-ov-file"><code>ManyIV</code> R package</a>.<sup>4</sup> Here I’ll restrict attention to people born in the first or fourth quarter of the year. The instrument is a dummy variable for being born in the fourth quarter, relative to being born in the first quarter:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remotes::install_github("kolesarm/ManyIV") # if needed</span></span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ManyIV) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Contains Angrist &amp; Krueger (1991) dataset</span></span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For information about the dataset, see the package documentation:</span></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ?ManyIV::ak80</span></span>
<span id="cb1-7"></span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb1-9"></span>
<span id="cb1-10">dat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ak80 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_tibble</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(qob <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Q1'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Q4'</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</span> (qob <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Q4'</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> education, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> lwage, z)</span></code></pre></div></div>
</div>
<p>To test how well OLS and IV perform as predictors, we’ll carry out a “pseudo-out-of-sample” experiment. First we’ll randomly split <code>dat</code> into a “training” sample containing 80% of the observations and a “test” sample containing the remaining 20%:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1693</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For reproducibility</span></span>
<span id="cb2-2"></span>
<span id="cb2-3">n_total <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(dat) </span>
<span id="cb2-4">n_train <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> n_total) </span>
<span id="cb2-5">n_test <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> n_total <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> n_train </span>
<span id="cb2-6"></span>
<span id="cb2-7">train_indices <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(n_total, n_train, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) </span>
<span id="cb2-8"></span>
<span id="cb2-9">dat_train <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dat[train_indices, ] </span>
<span id="cb2-10">dat_test <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dat[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>train_indices, ] </span></code></pre></div></div>
</div>
<p>Now we’ll use <code>dat_train</code> to fit IV and OLS:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">ols_fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dat_train) </span>
<span id="cb3-2">ols_coefs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(ols_fit)</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ivreg) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># install with `install.packages("ivreg")` if needed </span></span>
<span id="cb3-5">iv_fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ivreg</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> z, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dat_train)</span>
<span id="cb3-6">iv_coefs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(iv_fit)</span>
<span id="cb3-7"></span>
<span id="cb3-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">OLS =</span> ols_coefs, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">IV =</span> iv_coefs)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>    (Intercept)          x
OLS    5.004283 0.07008633
IV     4.749959 0.09000644</code></pre>
</div>
</div>
<p>Now we’re ready to make our predictive comparison! We’ll “pretend” that we don’t know the wages of the people in our test sample and use the OLS and IV coefficients from above to predict the “missing” wages:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">dat_test <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dat_test <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ols_pred =</span> ols_coefs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ols_coefs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x,</span>
<span id="cb5-3">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iv_pred =</span> iv_coefs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> iv_coefs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x) </span></code></pre></div></div>
</div>
<p>Of course we actually <em>do</em> know the wages of everyone in <code>dat_test</code>; this is the column <code>y</code>. So we can now compare our predictions against the truth.<sup>5</sup> A common measure of predictive quality is mean squared error (MSE), the average squared difference between the truth and our predictions. Because it squares the difference between the truth and our prediction, MSE penalizes larger errors more than smaller ones. While there are other ways to measure prediction error, MSE is a common choice and one that will play a key role in the rest of this post. And the winner is … <strong>OLS</strong>! Because it has a lower MSE, the predictions from the OLS model are, on average, closer to the true wages than the predictions from the IV model:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">dat_test <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ols_mse =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>((y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ols_pred)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb6-3">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iv_mse =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>((y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> iv_pred)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 2
  ols_mse iv_mse
    &lt;dbl&gt;  &lt;dbl&gt;
1   0.407  0.411</code></pre>
</div>
</div>
<p>OLS beats IV by a small but appreciable margin. (The relatively small difference in this case reflects the fact that IV and OLS estimates are fairly similar in this example.) It turns out that this <em>isn’t a fluke</em>. The same will be true in <em>any example</em>. Unless the instrument is perfectly correlated with the endogenous regressor, OLS will always have a lower predictive MSE than IV.</p>
</section>
<section id="whats-really-going-on-here" class="level2" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="whats-really-going-on-here"><span class="header-section-number">4</span> What’s really going on here?</h2>
<p>I ask this question of my introductory econometric students every year and most of them are surprised by the answer. If we have an endogenous regressor OLS is biased and inconsistent; why would we ever pass up the opportunity to use a valid and relevant instrument! The answer is surprisingly simple: <em>by definition</em> the OLS estimand gives the best linear predictor of <img src="https://latex.codecogs.com/png.latex?Y">, the one that minimizes MSE: <img src="https://latex.codecogs.com/png.latex?%5Cmin_%7Ba,b%7D%20%5Cmathbb%7BE%7D%5B%5C%7BY%20-%20(a%20+%20b%20X)%5C%7D%5E2%5D">. This is true <em>regardless</em> of whether <img src="https://latex.codecogs.com/png.latex?X"> is endogenous. Indeed, from a predictive perspective, endogeneity is a feature not a bug! The fact that years of schooling “smuggles in” information about ability and family background is exactly why it gives better predictions than IV. Remember: the whole point of IV is to <em>remove</em> the part of <img src="https://latex.codecogs.com/png.latex?X"> that is related to unobserved causes of <img src="https://latex.codecogs.com/png.latex?Y">. This is exactly what we want if our goal is to understand cause-and-effect, but it’s the <em>opposite</em> of what would make sense in a prediction problem, where we’d like to use as much information as possible.</p>
</section>
<section id="a-red-herring-the-bias-variance-tradeoff" class="level2" data-number="5">
<h2 data-number="5" class="anchored" data-anchor-id="a-red-herring-the-bias-variance-tradeoff"><span class="header-section-number">5</span> A Red Herring: The Bias-Variance Tradeoff</h2>
<p>Students sometimes answer this question by invoking the <a href="https://en.wikipedia.org/wiki/Bias%E2%80%93variance_tradeoff">bias-variance tradeoff</a>, pointing out that “OLS is biased but has a lower variance than IV, so it could have a lower MSE.” This is correct, but misses the deeper point. They’re thinking about bias in estimating the <em>causal parameter</em>.<sup>6</sup> But, again, the point here is that this isn’t relevant when prediction is our goal. When ML researchers discuss the bias-variance tradeoff in predictive settings, they mean something entirely different: bias of a linear predictive model relative to the true conditional mean function. OLS gives the best linear approximation to <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5BY%7CX%5D">, so it’s what we want in this example, since I stipulated we’d be working with linear models.</p>
</section>
<section id="take-home-message" class="level2" data-number="6">
<h2 data-number="6" class="anchored" data-anchor-id="take-home-message"><span class="header-section-number">6</span> Take Home Message</h2>
<p>Causal inference and prediction are different goals. Causality is about <em>counterfactuals</em>: what would happen if we <em>intervened</em> to change someone’s years of education? Prediction answers a different question: if I <em>observe</em> that someone has eight years of schooling, what is my best guess of their wage? If you want to predict, use OLS; if you want to estimate a causal effect, use IV.</p>
</section>
</div>
</div>
</div>


<div class="no-row-height column-margin column-container"><div id="fn6"><p><sup>6</sup>&nbsp;When our goal is to learn the causal parameter, this bias-variance tradeoff becomes relevant. I even <a href="https://ideas.repec.org/a/eee/econom/v195y2016i2p187-208.html">wrote a paper</a>!</p></div></div><div class="no-row-height column-margin column-container"><div id="fn5"><p><sup>5</sup>&nbsp;It’s crucial that we used one dataset to <em>estimate</em> our models and a <em>different</em> one to evaluate their predictive performance to avoid a problem called “overfitting”. This issue calls for a post of its own, but if you want a preview check out this blog post on <a href="https://sohl-dickstein.github.io/2022/11/06/strong-Goodhart.html">Goodhart’s law</a>.</p></div></div><div class="no-row-height column-margin column-container"><div id="fn4"><p><sup>4</sup>&nbsp;You can install this package using the <a href="https://cran.r-project.org/web/packages/remotes/index.html"><code>remotes</code></a> package, which is a convenient way to install packages from GitHub.</p></div></div></section>


 ]]></description>
  <category>econometrics</category>
  <category>causal inference</category>
  <category>puzzler</category>
  <guid>https://www.econometrics.blog/post/econometrics-puzzler-1-to-instrument-or-not/</guid>
  <pubDate>Sun, 13 Jul 2025 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Not Quite the James-Stein Estimator</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/not-quite-the-james-stein-estimator/</link>
  <description><![CDATA[ 




<p>If you study enough econometrics or statistics, you’ll eventually hear someone mention “Stein’s Paradox” or the <a href="https://en.wikipedia.org/wiki/James%E2%80%93Stein_estimator">“James-Stein Estimator”</a>. You’ve probably learned in your introductory econometrics course that ordinary least squares (OLS) is the <a href="https://en.wikipedia.org/wiki/Gauss%E2%80%93Markov_theorem">best linear unbiased estimator</a> (BLUE) in a linear regression model under the Gauss-Markov assumptions. The stipulations “linear” and “unbiased” are crucial here. If we remove them, it’s possible to do better–maybe even <em>much better</em>–than OLS.<sup>1</sup> Stein’s paradox is a famous example of this phenomenon, one that created much consternation among statisticians and fellow-travelers when it was first pointed out by <a href="https://en.wikipedia.org/wiki/Charles_M._Stein">Charles Stein</a> in the mid-1950s. The example is interesting in its own right, but also has deep connections to ideas in Bayesian inference and machine learning making it much more than a mere curiosity.</p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;If I ruled the universe, the Gauss-Markov Theorem would be demoted to much less exalted status in econometrics teaching!</p></div></div><p>The supposed <a href="https://youtu.be/XXhJKzI1u48?si=cS--uLd09_JnAXdr">paradox</a> is most simply stated by considering a special case of linear regression–that of estimating multiple unknown means. <a href="https://www.jstor.org/stable/24954030">Efron &amp; Morris (1977)</a> introduce the basic idea as follows:</p>
<blockquote class="blockquote">
<p>A baseball player who gets seven hits in 20 official times at bat is said to have a batting average of .350. In computing this statistic we are forming an estimate of the player’s true batting ability in terms of his observed average rate of success. Asked how well the player will do in his next 100 times at bat, we would probably predict 35 more hits. In traditional statistical theory it can be proved that no other estimation rule is uniformly better than the observed average. The paradoxical element in Stein’s result is that it sometimes contradicts this elementary law of statistical theory. If we have three or more baseball players, and if we are interested in predicting future batting averages for each of them, then there is a procedure that is better than simply extrapolating from the three separate averages. Here “better” has a strong meaning. The statistician who employs Stein’s method can expect to predict the future averages more accurately no matter what the true batting abilities of the players may be.</p>
</blockquote>
<p>I first encountered Stein’s Paradox in an offhand remark by my PhD supervisor. I dutifully looked it up in an attempt to better understand the point he had been making, but lacked sufficient understanding of decision theory at the time to see what the fuss was all about. The second time I encountered it, after I knew a bit more, it seemed astounding: almost like magic. I decided to include the topic in my <a href="https://ditraglia.com/econ722">Econ 722</a> course at Penn, but struggled to make it accessible to my students. A big problem, in my view, is that the proof–see <a href="https://ditraglia.com/econ722/slides/econ722slides.pdf">lecture 1</a> or <a href="https://ditraglia.com/econ722/main.pdf">section 7.3</a>–is ultimately a bit of a let-down: algebra, followed by repeated integration by parts, and then a fact about the existence of moments for an <a href="https://en.wikipedia.org/wiki/Inverse-chi-squared_distribution">inverse-chi-squared random variable</a>. It seems like a sterile technical exercise when in fact that result itself is deep, surprising, and important. As if a benign deity were keen on making my point for me, the wikipedia article on the <a href="https://en.wikipedia.org/wiki/James%E2%80%93Stein_estimator">James-Stein Estimator</a> is flagged as “may be too technical for readers to understand” at the time of this writing!</p>
<p>After six months of pondering, this post is my attempt to explain the James-Stein Estimator in a way that is accessible to a broad audience. The assumed background is minimal: just an introductory course in probability and statistics. I’ll show how we can arrive at something that is <em>very nearly</em> the James-Stein estimator by following some very simple and natural intuition. After you understand my “not quite James-Stein” estimator, it’s a short step to the real thing. So the “let-down” proof I mentioned before becomes merely a technical justification for a slight modification of a formula that is already intuitively compelling. As far as possible, I’ve tried to keep this post self-contained by introducing, or at least reviewing, key background material as we go along. The cost of this approach, unfortunately, is that the post is pretty long! I hope you’ll soldier on to the end and that you’ll find the payoff worth your time and effort.</p>
<p>As far as I know, the precise way that I motivate the James-Stein estimator in this post is new, but there are many other papers that aim to make sense of the supposed paradox in an intuitive way. In keeping with my injunction that you should always consider <a href="https://www.econometrics.blog/post/how-to-read-an-econometrics-paper/">reading something else instead</a>, here are a few references that you may find helpful. <a href="https://www.jstor.org/stable/24954030">Efron &amp; Morris (1977)</a> is a classic article aimed at the general reader without a background in statistics. <a href="https://projecteuclid.org/journals/statistical-science/volume-5/issue-1/The-1988-Neyman-Memorial-Lecture--A-Galtonian-Perspective-on/10.1214/ss/1177012274.full">Stigler (1988)</a> is a more technical but still accessible discussion of the topic while <a href="https://www.jstor.org/stable/2682801">Casella (1985)</a> is a very readable paper that discusses the James-Stein estimator in the context of empirical Bayes. A less well-known paper that I found helpful is <a href="https://www.jstor.org/stable/2490394">Ijiri &amp; Leitch (1980)</a>, who consider the James-Stein estimator in a real-world setting, namely “Audit Sampling” in accounting. They discuss several interesting practical and philosophical issues including the distinction between “composite” and “individual” risk that I’ll pick up on below.</p>
<section id="warm-up-exercise" class="level2 page-columns page-full" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="warm-up-exercise"><span class="header-section-number">1</span> Warm-up Exercise</h2>
<p>This section provides some important background that we’ll need to understand Stein’s Paradox later in the post reviewing the ideas of <strong>bias</strong>, <strong>variance</strong> and <strong>mean-squared error</strong> along with introducing a very simple <strong>shrinkage estimator</strong>. To make these ideas as transparent as possible we’ll start with a ridiculously simple problem. Suppose that you observe <img src="https://latex.codecogs.com/png.latex?X%20%5Csim%20%5Ctext%7BNormal%7D(%5Cmu,%201)">, a single draw from a normal distribution with variance one and unknown mean <img src="https://latex.codecogs.com/png.latex?%5Cmu">. Your task is to estimate <img src="https://latex.codecogs.com/png.latex?%5Cmu">. This may strike you as a very silly problem: it only involves a single datapoint and we assume the variance of <img src="https://latex.codecogs.com/png.latex?X"> is one! But in fact there’s nothing special about <img src="https://latex.codecogs.com/png.latex?n%20=%201"> and a variance of one: these merely make the notation simpler. If you prefer, you can think of <img src="https://latex.codecogs.com/png.latex?X"> as the sample mean of <img src="https://latex.codecogs.com/png.latex?n"> iid draws from a population with unknown mean <img src="https://latex.codecogs.com/png.latex?%5Cmu"> where we’ve <em>rescaled</em> everything to have variance one. So how should we estimate <img src="https://latex.codecogs.com/png.latex?%5Cmu">? A natural and reasonable idea is to use the sample mean, in this case <img src="https://latex.codecogs.com/png.latex?X"> itself. This is in fact the <a href="https://en.wikipedia.org/wiki/Maximum_likelihood_estimation">maximum likelihood estimator</a> for <img src="https://latex.codecogs.com/png.latex?%5Cmu">, so I’ll define <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cmu%7D_%7B%5Ctext%7BML%7D%7D%20=%20X">. But is this estimator any good? And can we find something better?</p>
<section id="review-of-bias-variance-and-mse" class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="review-of-bias-variance-and-mse">Review of Bias, Variance and MSE</h3>
<p>The concepts of <em>bias</em> and <em>variance</em> are key ideas that we typically reach for when considering the quality of an estimator. To refresh your memory, <em>bias</em> is the difference between an estimators expected value and the true value of the parameter being estimated while <em>variance</em> is the expected squared difference between an estimator and its expected value. So if <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctheta%7D"> is an estimator of some unknown parameter <img src="https://latex.codecogs.com/png.latex?%5Ctheta">, then <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BBias%7D(%5Chat%7B%5Ctheta%7D)%20=%20%5Cmathbb%7BE%7D%5B%5Chat%7B%5Ctheta%7D%5D%20-%20%5Ctheta"> while <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVar%7D(%5Chat%7B%5Ctheta%7D)%20=%20%5Cmathbb%7BE%7D%5B(%5Chat%7B%5Ctheta%7D%20-%20%5Cmathbb%7BE%7D%5B%5Chat%7B%5Ctheta%7D%5D)%5E2%5D">. A bias of zero means that an estimator is <em>correctly centered</em>: its expectation equals the truth. We say that such an estimator is <em>unbiased</em>.<sup>2</sup> A small variance means that an estimator is <em>precise</em>: it doesn’t “jump around” too much. Ideally we’d like an estimator that is correctly centered and precise. But it turns out that there is generally a <em>trade-off</em> between bias and variance: if you want to reduce one of them, you have to accept an increase in the other.</p>
<div class="no-row-height column-margin column-container"><div id="fn2"><p><sup>2</sup>&nbsp;Don’t let words do your thinking for you: “bias” sounds like a very bad thing, like kicking puppies. But that’s because the word “bias” has a negative connotation in English. In statistics, it’s just a technical term for “not centered”. An estimator can be biased and still be very good. Indeed the punchline of this post is that the James-Stein estimator is biased but can be much better than the obvious alternative!</p></div><div id="fn3"><p><sup>3</sup>&nbsp;Why squared bias and not simply bias itself? The answer is units: bias is measured in the same units as the parameter being estimated while the variance is in squared units. It doesn’t make sense to add things with different units, so we either have to square the bias or take the square root of the variance, i.e.&nbsp;replace it with the standard deviation. But bias can be negative, and we wouldn’t want a large negative bias to cancel out a large standard deviation so MSE squares the bias instead.</p></div><div id="fn4"><p><sup>4</sup>&nbsp;See if you can prove this as a homework exercise!</p></div></div><p>A common way of trading off bias and variance relies on a concept called <em>mean-squared error</em> (MSE) defined as the <em>sum</em> of the squared bias and the variance.<sup>3</sup> In particular: <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BMSE%7D(%5Chat%7B%5Ctheta%7D)%20=%20%5Ctext%7BVar%7D(%5Chat%7B%5Ctheta%7D)%20+%20%5Ctext%7BBias%7D(%5Chat%7B%5Ctheta%7D)%5E2">. Equivalently, we can write <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BMSE%7D(%5Chat%7B%5Ctheta%7D)%20=%20%5Cmathbb%7BE%7D%5B(%5Chat%7B%5Ctheta%7D%20-%20%5Ctheta)%5E2%5D">.<sup>4</sup> To borrow some terminology from introductory microeconomics, you can think of MSE as the <em>negative</em> of a utility function over bias and variance. Both bias and variance are “bads” in that we’d rather have less rather than more of each. This formula expresses our <em>preferences</em> in terms of how much of one we’d be willing to accept in exchange for less of the other. Slightly foreshadowing something that will come later in this post, we can think of MSE as the square of the average distance that an archer’s arrows land from the bulls-eye. Smaller values of MSE are better: variance measures how closely the arrows cluster together while bias measures how far the center of the cluster is from the bulls-eye, as in the following diagram:</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/not-quite-the-james-stein-estimator/index_files/figure-html/unnamed-chunk-2-1.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="a-shrinkage-estimator" class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="a-shrinkage-estimator">A Shrinkage Estimator</h3>
<p>Returning to our maximum likelihood estimator: it’s unbiased, <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BBias%7D(%5Chat%7B%5Cmu%7D_%7B%5Ctext%7BML%7D%7D)%20=%200">, so <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BMSE%7D(%5Chat%7B%5Cmu%7D_%7B%5Ctext%7BML%7D%7D)%20=%20%5Ctext%7BVar%7D(%5Chat%7B%5Cmu%7D_%7B%5Ctext%7BML%7D%7D)%20=%201">. Suppose that low MSE is what we’re after. Is there any way to improve on the ML estimator? In other words, can we achieve an MSE that’s lower than one? The answer turns out to be <em>yes</em>. Here’s the idea. Suppose we had some reason to believe that the true mean <img src="https://latex.codecogs.com/png.latex?%5Cmu"> isn’t very large. Then perhaps we could try to adjust our maximum likelihood estimate by <em>shrinking</em> slightly towards zero. One way to do this would be by taking a weighted average of the ML estimator and zero: <img src="https://latex.codecogs.com/png.latex?%0A%5Chat%7B%5Cmu%7D(%5Clambda)%20=%20(1%20-%20%5Clambda)%20%5Ctimes%20%5Chat%7B%5Cmu%7D_%7B%5Ctext%7BML%7D%7D%20+%20%5Clambda%20%5Ctimes%200%20=%20(1%20-%20%5Clambda)X%0A"> for <img src="https://latex.codecogs.com/png.latex?0%20%5Cleq%20%5Clambda%20%5Cleq%201">. The constant <img src="https://latex.codecogs.com/png.latex?(1%20-%20%5Clambda)"> is called the “shrinkage factor” and controls how the ML estimator gets pulled towards zero.<sup>5</sup> We get a different estimator for every value of <img src="https://latex.codecogs.com/png.latex?%5Clambda">. If <img src="https://latex.codecogs.com/png.latex?%5Clambda%20=%200"> then we get the ML estimator back. If <img src="https://latex.codecogs.com/png.latex?%5Clambda%20=%201"> then we get a very silly estimator that ignores the data and simply reports zero no matter what! So let’s see how the MSE depends on our choice of <img src="https://latex.codecogs.com/png.latex?%5Clambda">. Substituting the definition of <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cmu%7D(%5Clambda)"> into the formulas for bias and variance gives: <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Ctext%7BBias%7D%5B%5Chat%7B%5Cmu%7D(%5Clambda)%5D&amp;=%20%5Cmathbb%7BE%7D%5B(1%20-%20%5Clambda)%5Chat%7B%5Cmu%7D_%5Ctext%7BML%7D%5D%20-%20%5Cmu%20=%20(1%20-%20%5Clambda)%5Cmathbb%7BE%7D%5B%5Chat%7B%5Cmu%7D_%5Ctext%7BML%7D%5D%20-%20%5Cmu%20=%20(1%20-%20%5Clambda)%5Cmu%20-%20%5Cmu%20=%20-%5Clambda%5Cmu%5C%5C%20%5C%5C%0A%5Ctext%7BVar%7D%5B%5Chat%7B%5Cmu%7D(%5Clambda)%5D&amp;=%20%5Ctext%7BVar%7D%5B(1%20-%20%5Clambda)%5Chat%7B%5Cmu%7D_%5Ctext%7BML%7D%5D%20=%20(1%20-%20%5Clambda)%5E2%5Ctext%7BVar%7D%5B%5Chat%7B%5Cmu%7D_%5Ctext%7BML%7D%5D%20=%20(1%20-%20%5Clambda)%5E2%5C%5C%20%5C%5C%0A%5Ctext%7BMSE%7D%5B%5Chat%7B%5Cmu%7D(%5Clambda)%5D&amp;=%20%5Ctext%7BVar%7D%5B%5Chat%7B%5Cmu%7D(%5Clambda)%5D%20+%20%5Ctext%7BBias%7D%5B%5Chat%7B%5Cmu%7D(%5Clambda)%5D%5E2%20=%20(1%20-%20%5Clambda)%5E2%20+%20%5Clambda%5E2%5Cmu%5E2%0A%5Cend%7Baligned%7D%0A"> Unless <img src="https://latex.codecogs.com/png.latex?%5Clambda%20=%200">, the shrinkage estimator is <em>biased</em>. And while the MSE of the ML estimator is always one, regardless of the true value of <img src="https://latex.codecogs.com/png.latex?%5Cmu">, the MSE of the shrinkage estimator <em>depends on the unknown parameter</em> <img src="https://latex.codecogs.com/png.latex?%5Cmu">.</p>
<div class="no-row-height column-margin column-container"><div id="fn5"><p><sup>5</sup>&nbsp;In Bayesian terms, we could view this “shrinkage” idea as calculating the posterior mean of <img src="https://latex.codecogs.com/png.latex?%5Cmu"> conditional on our data <img src="https://latex.codecogs.com/png.latex?X"> under a normal prior. In this case <img src="https://latex.codecogs.com/png.latex?%5Clambda"> would equal <img src="https://latex.codecogs.com/png.latex?%5Ctau/(1%20+%20%5Ctau)"> where <img src="https://latex.codecogs.com/png.latex?%5Ctau"> is the <em>prior precision</em>, i.e.&nbsp;the reciprocal of the prior variance. But for this post we’ll mainly stick to the Frequentist perspective.</p></div></div><p>So why should we use a biased estimator? The answer is that by tolerating a small amount of bias we may be able to achieve a <em>larger</em> reduction in variance, resulting in a lower MSE compared to the higher variance but unbiased ML estimator. A quick plot shows us that the shrinkage estimator <em>can indeed</em> have a lower MSE than the ML estimator depending on the value of <img src="https://latex.codecogs.com/png.latex?%5Clambda"> and the true value of <img src="https://latex.codecogs.com/png.latex?%5Cmu">:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Range of values for the unknown parameter mu</span></span>
<span id="cb1-2">mu <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Try three different values of lambda</span></span>
<span id="cb1-4">lambda1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span></span>
<span id="cb1-5">lambda2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span></span>
<span id="cb1-6">lambda3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot the MSE of the shrinkage estimator as a function of mu for all </span></span>
<span id="cb1-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># three values of lambda at once</span></span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matplot</span>(mu, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>((<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> lambda1)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> lambda1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> mu<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, </span>
<span id="cb1-10">                  (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> lambda2)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> lambda2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> mu<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, </span>
<span id="cb1-11">                  (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> lambda3)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> lambda3<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> mu<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>), </span>
<span id="cb1-12">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'l'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, </span>
<span id="cb1-13">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'blue'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'green'</span>), </span>
<span id="cb1-14">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(mu), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MSE'</span>, </span>
<span id="cb1-15">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MSE of Shrinkage Estimator'</span>)</span>
<span id="cb1-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add legend</span></span>
<span id="cb1-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">legend</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'topright'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(lambda <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>), </span>
<span id="cb1-18">                              <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(lambda <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>), </span>
<span id="cb1-19">                              <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(lambda <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>)), </span>
<span id="cb1-20">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'blue'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'green'</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb1-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add dashed line for MSE of ML estimator</span></span>
<span id="cb1-22"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/not-quite-the-james-stein-estimator/index_files/figure-html/unnamed-chunk-3-1.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="some-algebra" class="level3">
<h3 class="anchored" data-anchor-id="some-algebra">Some Algebra</h3>
<p>It’s time for some algebra. If you’re tempted to skip this <em>please don’t</em>: this section is a warm-up for our main event. If you thoroughly understand the mechanics of shrinkage in this simple example, everything that follows below will seem much more natural.</p>
<p>As seen from the plot above, the MSE of our shrinkage estimator (the solid lines) is lower than that of the ML estimator (the dashed line) provided that our chosen value of <img src="https://latex.codecogs.com/png.latex?%5Clambda"> isn’t too large relative to the true value of <img src="https://latex.codecogs.com/png.latex?%5Cmu">. With a bit of algebra, we can work out <em>precisely</em> how large <img src="https://latex.codecogs.com/png.latex?%5Clambda"> can be to make shrinkage worthwhile. Since <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BMSE%7D%5B%5Chat%7B%5Cmu%7D_%5Ctext%7BML%7D%5D=%201">, by expanding and simplifying the expression for <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BMSE%7D%5B%5Chat%7B%5Cmu%7D(%5Clambda)%5D"> we see that <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BMSE%7D%5B%5Chat%7B%5Cmu%7D(%5Clambda)%5D%20%3C%20%5Ctext%7BMSE%7D%5B%5Chat%7B%5Cmu%7D_%5Ctext%7BML%7D%5D"> if and only if <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A(1%20-%20%5Clambda)%5E2%20+%20%5Clambda%5E2%5Cmu%5E2%20&amp;%3C%201%20%5C%5C%0A1%20-%202%5Clambda%20+%20%5Clambda%5E2%20+%20%5Clambda%5E2%5Cmu%5E2%20&amp;%3C%201%20%5C%5C%0A%5Clambda%5E2%20(1%20+%20%5Cmu%5E2)%20-2%20%5Clambda%20&amp;%3C%200%20%5C%5C%0A%5Clambda%20%5B%5Clambda%20(1%20+%20%5Cmu%5E2)%20-%202%5D%20&amp;%3C%200.%0A%5Cend%7Baligned%7D%0A"> Since <img src="https://latex.codecogs.com/png.latex?%5Clambda%20%5Cgeq%200">, the final inequality can only hold if the factor inside the square brackets is negative, i.e.&nbsp; <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Clambda%20(1%20+%20%5Cmu%5E2)%20-%202%20&amp;%3C%200%20%5C%5C%0A%5Clambda%20&amp;%3C%20%5Cfrac%7B2%7D%7B1%20+%20%5Cmu%5E2%7D.%0A%5Cend%7Baligned%7D%0A"> This shows that any choice of <img src="https://latex.codecogs.com/png.latex?%5Clambda"> between <img src="https://latex.codecogs.com/png.latex?0"> and <img src="https://latex.codecogs.com/png.latex?2%20/%20(1%20+%20%5Cmu%5E2)"> will give us a shrinkage estimator with an MSE less than one. To check our algebra, we can change the inequality to an equality and solve for <img src="https://latex.codecogs.com/png.latex?%5Cmu"> to obtain the boundary of the region where shrinkage is better than ML: <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Clambda%20(1%20+%20%5Cmu%5E2)%20-%202%20&amp;=%200%20%5C%5C%0A1%20+%20%5Cmu%5E2%20&amp;=%202/%5Clambda%20%5C%5C%0A%5Cmu%20&amp;=%20%5Cpm%20%5Csqrt%7B2/%5Clambda%20-%201%7D.%0A%5Cend%7Baligned%7D%0A"> Adding these boundaries to a simplified version of our previous plot with only <img src="https://latex.codecogs.com/png.latex?%5Clambda%20=%200.3"> we see that everything works out correctly: the dashed red lines intersect the blue curve at the points where the MSE of the shrinkage estimator equals that of the ML estimator.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot the MSE of the shrinkage estimator as a function of mu for lambda = 0.3</span></span>
<span id="cb2-2">lambda <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span></span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(mu, (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> lambda)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> lambda<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> mu<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'l'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, </span>
<span id="cb2-4">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'blue'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(mu), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MSE'</span>, </span>
<span id="cb2-5">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Boundary of Region Where Shrinkage is Better than ML'</span>)</span>
<span id="cb2-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add dashed line for MSE of ML estimator</span></span>
<span id="cb2-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add boundaries of region where shrinkage is better than ML estimator</span></span>
<span id="cb2-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>lambda <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>lambda <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb2-10">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/not-quite-the-james-stein-estimator/index_files/figure-html/unnamed-chunk-4-1.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>But there’s still more to learn! Suppose we wanted to take things <em>one step further</em> and find the <em>optimal</em> value of <img src="https://latex.codecogs.com/png.latex?%5Clambda"> for any given value of <img src="https://latex.codecogs.com/png.latex?%5Cmu">. In other words, suppose we wanted the value of <img src="https://latex.codecogs.com/png.latex?%5Clambda"> that <em>minimizes</em> the MSE of our shrinkage estimator given a particular assumed value for <img src="https://latex.codecogs.com/png.latex?%5Cmu">. Since <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BMSE%7D%5B%5Chat%7B%5Cmu%7D(%5Clambda)%5D"> is a quadratic function of <img src="https://latex.codecogs.com/png.latex?%5Clambda">, as shown above, this turns out to be a fairly straightforward calculation. Differentiating, <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cfrac%7Bd%7D%7Bd%5Clambda%7D%5Ctext%7BMSE%7D%5B%5Chat%7B%5Cmu%7D(%5Clambda)%5D%20&amp;=%20%5Cfrac%7Bd%7D%7Bd%5Clambda%7D%5B(1%20-%20%5Clambda)%5E2%20+%20%5Clambda%5E2%20%5Cmu%5E2%5D%20%5C%5C%0A&amp;=%20-2(1%20-%20%5Clambda)%20+%202%5Clambda%20%5Cmu%5E2%20%5C%5C%0A&amp;=%202%20%5B%5Clambda%20(1%20+%20%5Cmu%5E2)%20-%201%5D%5C%5C%20%5C%5C%0A%5Cfrac%7Bd%5E2%7D%7Bd%5Clambda%5E2%7D%5Ctext%7BMSE%7D%5B%5Chat%7B%5Cmu%7D(%5Clambda)%5D%20&amp;=%202(1%20+%20%5Cmu%5E2)%20%3E%200%0A%5Cend%7Baligned%7D%0A"> so there is a unique global minimum at <img src="https://latex.codecogs.com/png.latex?%5Clambda%5E*%20%5Cequiv%201/(1%20+%20%5Cmu%5E2)">. This gives the <em>optimal</em> shrinkage factor in the sense that it minimizes the MSE of the shrinkage estimator. Substituting <img src="https://latex.codecogs.com/png.latex?%5Clambda%5E*"> into the expression for <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BMSE%7D%5B%5Chat%7B%5Cmu%7D(%5Clambda)%5D"> gives: <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Ctext%7BMSE%7D%5B%5Chat%7B%5Cmu%7D(%5Clambda%5E*)%5D%20&amp;=%20%5Cleft(1%20-%20%5Cfrac%7B1%7D%7B1%20+%20%5Cmu%5E2%7D%20%5Cright)%5E2%20+%20%5Cleft(%5Cfrac%7B1%7D%7B1%20+%20%5Cmu%5E2%7D%5Cright)%5E2%20%5Cmu%5E2%20%20%5C%5C%0A&amp;=%20%5Cleft(%20%5Cfrac%7B%5Cmu%5E2%7D%7B1%20+%20%5Cmu%5E2%7D%5Cright)%5E2%20+%20%5Cleft(%5Cfrac%7B1%7D%7B1%20+%20%5Cmu%5E2%7D%5Cright)%5E2%20%5Cmu%5E2%20%5C%5C%0A&amp;=%20%5Cleft(%20%5Cfrac%7B1%7D%7B1%20+%20%5Cmu%5E2%7D%5Cright)%5E2%20(%5Cmu%5E4%20+%20%5Cmu%5E2)%20%5C%5C%0A&amp;=%20%5Cleft(%20%5Cfrac%7B1%7D%7B1%20+%20%5Cmu%5E2%7D%5Cright)%5E2%20%5Cmu%5E2(1%20+%20%5Cmu%5E2)%20%5C%5C%0A&amp;=%20%5Cfrac%7B%5Cmu%5E2%7D%7B1%20+%20%5Cmu%5E2%7D%20%3C%201.%0A%5Cend%7Baligned%7D%0A"></p>
</section>
</section>
<section id="steins-paradox" class="level2 page-columns page-full" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="steins-paradox"><span class="header-section-number">2</span> Stein’s Paradox</h2>
<section id="recap" class="level3">
<h3 class="anchored" data-anchor-id="recap">Recap</h3>
<p>We’re moments away from having all the ingredients we need to introduce Stein’s Paradox! But first let’s review what we’ve uncovered thus far. We’ve seen that the shrinkage estimator can improve on the ML estimator in terms of MSE provided that <img src="https://latex.codecogs.com/png.latex?%5Clambda"> is chosen judiciously: it needs to be between zero and <img src="https://latex.codecogs.com/png.latex?2/(1%20+%20%5Cmu%5E2)">. The optimal choice of <img src="https://latex.codecogs.com/png.latex?%5Clambda">, namely <img src="https://latex.codecogs.com/png.latex?%5Clambda%5E*%20=%201%20/%20(1%20+%20%5Cmu%5E2)">, gives an MSE of <img src="https://latex.codecogs.com/png.latex?%5Cmu%5E2/(1%20+%20%5Cmu%5E2)">. This is always lower than one, the MSE of the ML estimator.</p>
<p>There’s just one massive problem we’ve ignored this whole time: <strong>we don’t know the value of</strong> <img src="https://latex.codecogs.com/png.latex?%5Cmu">! As seen from the figure plotted above, the MSE curves for different values of <img src="https://latex.codecogs.com/png.latex?%5Clambda"> <em>cross each other</em>: the best one to use depends on the true value of <img src="https://latex.codecogs.com/png.latex?%5Cmu">. This doesn’t mean that all is lost. Perhaps in practice we have some outside information about the likely value of <img src="https://latex.codecogs.com/png.latex?%5Cmu"> that could help guide our choice of <img src="https://latex.codecogs.com/png.latex?%5Clambda">. What it does mean is that there’s no “one-size-fits-all” value.</p>
</section>
<section id="admissibility" class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="admissibility">Admissibility</h3>
<p>It’s time to introduce a bit of technical vocabulary. We say that an estimator <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7B%5Ctheta%7D"> <strong>dominates</strong> another estimator <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctheta%7D"> if <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BMSE%7D%5B%5Ctilde%7B%5Ctheta%7D%5D%20%5Cleq%20%5Ctext%7BMSE%7D%5B%5Chat%7B%5Ctheta%7D%5D"> for <em>all</em> possible values of the parameter <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> being estimated and <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BMSE%7D%5B%5Ctilde%7B%5Ctheta%7D%5D%20%3C%20%5Ctext%7BMSE%7D%5B%5Chat%7B%5Ctheta%7D%5D"> for at least <em>one</em> possible value of <img src="https://latex.codecogs.com/png.latex?%5Ctheta">.<sup>6</sup> In words, this means that it never makes sense to use <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctheta%7D"> in preference to <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7B%5Ctheta%7D">. No matter what the true parameter value is, you can’t do worse with <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7B%5Ctheta%7D"> and you might do better. An estimator that is <em>not dominated</em> by any other estimator is called <strong>admissible</strong>; an estimator that <em>is dominated</em> by some other estimator is called <strong>inadmissible</strong>. The concept of <em>admissibility</em> in decision theory is a bit like the concept of <a href="https://en.wikipedia.org/wiki/Pareto_efficiency">Pareto efficiency</a> in microeconomics. An admissible estimator is only “good” in the sense that it doesn’t leave any money on the table: there’s no way to do better for one parameter value without doing worse for another. In a similar way, a Pareto efficient allocation in economics is one in which no individual can be made better off without making another person worse off.</p>
<div class="no-row-height column-margin column-container"><div id="fn6"><p><sup>6</sup>&nbsp;Strictly speaking all of this pre-supposes that we’re working with squared-error loss so that MSE is the right thing to minimize. There are other loss functions we could have used instead and these would lead to different risk functions. But for the purposes of this post, I prefer to keep things simple. See <a href="https://ditraglia.com/econ722/slides/econ722slides.pdf">lecture 1</a> of my Econ 722 slides for more detail.</p></div></div><p>It’s quite challenging to prove, but in fact the ML estimator <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctheta%7D_%7BML%7D%20=%20X"> turns out to be admissible in our little example. So while we could potentially do better by using shrinkage, it’s not a slam-dunk case. If we really have no idea of how large <img src="https://latex.codecogs.com/png.latex?%5Cmu"> is likely to be, the ML estimator is a reasonable choice. Because it’s admissible, at the very least we know that there’s no free lunch!</p>
</section>
<section id="a-more-general-example" class="level3">
<h3 class="anchored" data-anchor-id="a-more-general-example">A More General Example</h3>
<p>Now let’s make things a bit more interesting. For the rest of this post, suppose that we observe not a single draw <img src="https://latex.codecogs.com/png.latex?X"> from a <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BNormal%7D(%5Cmu,%201)"> distribution but a <em>collection</em> of <img src="https://latex.codecogs.com/png.latex?p"> independent draws from <img src="https://latex.codecogs.com/png.latex?p"> <em>different</em> normal distributions: <img src="https://latex.codecogs.com/png.latex?%0AX_1,%20X_2,%20...,%20X_p%20%5Csim%20%5Ctext%7Bindependent%20Normal%7D(%5Cmu_j,%201),%20%5Cquad%20j%20=%201,%20...,%20p.%0A"> You can think of this as <img src="https://latex.codecogs.com/png.latex?p"> copies of our original problem: we observe <img src="https://latex.codecogs.com/png.latex?X_j%20%5Csim%20%5Ctext%7BNormal%7D(%5Cmu_j,%201)"> and our task is to estimate <img src="https://latex.codecogs.com/png.latex?%5Cmu_j">. The observations are all independent, and each comes from a distribution with a potentially <strong>different mean</strong>. At first glance it seems like these <img src="https://latex.codecogs.com/png.latex?p"> separate problems should have <em>absolutely nothing to do with each other</em>. And indeed the maximum likelihood estimator for the collection of <img src="https://latex.codecogs.com/png.latex?p"> means is simply <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cmu%7D%5E%7B(j)%7D_%5Ctext%7BML%7D%20=%20X_j">. As above in our example with <img src="https://latex.codecogs.com/png.latex?p=1">, the question is: how good is the ML estimator, and can we do any better?</p>
</section>
<section id="composite-mse" class="level3">
<h3 class="anchored" data-anchor-id="composite-mse">Composite MSE</h3>
<p>But first things first: how can we evaluate the quality of <img src="https://latex.codecogs.com/png.latex?p"> estimators for <img src="https://latex.codecogs.com/png.latex?p"> different parameters <em>at the same time</em>? A common approach, and the one we will follow here, is to take the <em>sum</em> of the individual MSEs of each estimator, yielding a quantity called <strong>composite MSE</strong>. If <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cmu%7D_1,%20%5Chat%7B%5Cmu%7D_2,%20%5Cdots,%20%5Chat%7B%5Cmu%7D_p"> is a collection of estimators for each of the individual unknown means, then the composite MSE is defined as <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BComposite%20MSE%7D%20%5Cequiv%20%5Csum_%7Bj=1%7D%5Ep%20%5Ctext%7BMSE%7D(%5Chat%7B%5Cmu%7D_j)%20=%20%5Csum_%7Bj=1%7D%5Ep%20%5Cleft%5B%20%5Ctext%7BBias%7D(%5Chat%7B%5Cmu%7D_j)%5E2%20+%20%5Ctext%7BVar%7D(%5Chat%7B%5Cmu%7D_j)%5Cright%5D%20=%20%5Csum_%7Bj=1%7D%5Ep%20%5Cmathbb%7BE%7D%5B(%5Chat%7B%5Cmu%7D_j%20-%20%5Cmu_j)%5E2%5D.%0A"> Adopting composite MSE as our measure of <em>good</em> performance means that we view each of the <img src="https://latex.codecogs.com/png.latex?p"> estimation problems as in some way “interchangeable”–we’re happy to accept a trade in which we do a slightly worse job estimating <img src="https://latex.codecogs.com/png.latex?%5Cmu_j"> in exchange for doing a much better job estimating <img src="https://latex.codecogs.com/png.latex?%5Cmu_k">. At the end of the post I’ll say a few more words about this idea and when it may or may not be reasonable. But for the rest of the post, we will assume that our goal is to <strong>minimize the composite MSE</strong>. The concept of composite MSE will be crucial in understanding why the James-Stein estimator works the way it does.</p>
</section>
<section id="steins-paradox-1" class="level3">
<h3 class="anchored" data-anchor-id="steins-paradox-1">Stein’s Paradox</h3>
<p>Putting our new idea into practice, we see that the composite MSE of the ML estimator is <img src="https://latex.codecogs.com/png.latex?p"> regardless of the true values of the individual means <img src="https://latex.codecogs.com/png.latex?%5Cmu_1,%20%5Cdots,%20%5Cmu_p"> since <img src="https://latex.codecogs.com/png.latex?%0A%5Csum_%7Bj=1%7D%5Ep%20%5Ctext%7BMSE%7D%5Cleft%5B%5Chat%7B%5Cmu%7D%5E%7B(j)%7D_%5Ctext%7BML%7D%5Cright%5D%20=%20%5Csum_%7Bj=1%7D%5Ep%20%5Ctext%7BMSE%7D(X_j)%20=%20%5Csum_%7Bj=1%7D%5Ep%20%5Ctext%7BVar%7D(X_j)%20=%20p.%0A"> If the ML estimator is admissible, then there should be no other estimator that always has an MSE less than or equal to <img src="https://latex.codecogs.com/png.latex?p"> and sometimes has an MSE strictly less than <img src="https://latex.codecogs.com/png.latex?p">. I’ve already told you that this is true when <img src="https://latex.codecogs.com/png.latex?p%20=%201">. When <img src="https://latex.codecogs.com/png.latex?p%20=%202"> it’s still true: the ML estimator remains admissible. But when <img src="https://latex.codecogs.com/png.latex?p%20%5Cgeq%203"> something very unexpected happens: it becomes possible to construct an estimator that <strong>dominates</strong> the ML estimator by using information from <em>all</em> of the <img src="https://latex.codecogs.com/png.latex?(X_1,%20...,%20X_p)"> observations to estimate <img src="https://latex.codecogs.com/png.latex?%5Cmu_j">. This is spite of the fact that there is <em>no obvious connection</em> between the observations. Again: they are all independent and come from distributions with different means!</p>
<p>The estimator that does the trick is the so-called “James-Stein Estimator” (JS), defined according to <img src="https://latex.codecogs.com/png.latex?%0A%5Chat%7B%5Cmu%7D%5E%7B(j)%7D_%5Ctext%7BJS%7D%20=%20%5Cleft(1%20-%20%5Cfrac%7Bp%20-%202%7D%7B%5Csum_%7Bk=1%7D%5Ep%20X_k%5E2%7D%5Cright)X_j.%0A"> This estimator dominates the ML estimator when <img src="https://latex.codecogs.com/png.latex?p%20%5Cgeq%203"> in that<br>
<img src="https://latex.codecogs.com/png.latex?%0A%5Csum_%7Bj=1%7D%5Ep%20%5Ctext%7BMSE%7D%5Cleft%5B%5Chat%7B%5Cmu%7D%5E%7B(j)%7D_%5Ctext%7BJS%7D%5Cright%5D%20%5Cleq%20%5Csum_%7Bj=1%7D%5Ep%20%5Ctext%7BMSE%7D%5Cleft%5B%5Chat%7B%5Cmu%7D%5E%7B(j)%7D_%5Ctext%7BML%7D%5Cright%5D=%20p%0A"> for <em>all</em> possible values of the <img src="https://latex.codecogs.com/png.latex?p"> unknown means <img src="https://latex.codecogs.com/png.latex?%5Cmu_j"> with strict inequality for at least <em>some</em> values. Taking a closer look at the formula, we see that the James-Stein estimator is just a <em>shrinkage</em> estimator applied to each of the <img src="https://latex.codecogs.com/png.latex?p"> means, namely <img src="https://latex.codecogs.com/png.latex?%0A%5Chat%7B%5Cmu%7D%5E%7B(j)%7D_%5Ctext%7BJS%7D%20=%20(1%20-%20%5Chat%7B%5Clambda%7D_%5Ctext%7BJS%7D)X_j,%20%5Cquad%20%5Chat%7B%5Clambda%7D_%5Ctext%7BJS%7D%20%5Cequiv%20%5Cfrac%7Bp%20-%202%7D%7B%5Csum_%7Bk=1%7D%5Ep%20X_k%5E2%7D.%0A"> The shrinkage factor in the James-Stein estimator depends on the number of means we’re estimating, <img src="https://latex.codecogs.com/png.latex?p">, along with the <em>overall</em> sum of the squared observations. All else equal, the more parameters we need to estimate, the more we shrink each of them towards zero. And the farther the observations are from zero <em>overall</em>, the less we shrink <em>each of them</em> towards zero.</p>
<p>Just like our simple shrinkage estimator from above, the James-Stein estimator achieves a lower MSE by tolerating a small bias in exchange for a larger reduction in variance, compared to the higher-variance but unbiased ML estimator. Unlike our simple shrinkage estimator, the James-Stein estimator uses the <em>data</em> to determine the shrinkage factor. And as long as <img src="https://latex.codecogs.com/png.latex?p%20%5Cgeq%203"> it is always <em>at least as good</em> as the ML estimator and sometimes <em>much better</em>. The <strong>paradox</strong> is that this seems impossible: how can information from <em>all</em> of the observations be useful when they come from <em>different</em> distributions with no obvious connection?</p>
<p>The rest of this post will <em>not</em> prove that the James-Stein estimator dominates the ML estimator. Instead it will try to convince you that there is some <em>very good intuition</em> for why the formula for the James-Stein estimator takes the form it does. By the end, I hope you’ll feel that, far from seeming paradoxical, using <em>all</em> of the observations to determine the shrinkage factor for one particular <img src="https://latex.codecogs.com/png.latex?%5Cmu_j"> makes perfect sense.</p>
</section>
</section>
<section id="where-does-the-james-stein-estimator-come-from" class="level2 page-columns page-full" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="where-does-the-james-stein-estimator-come-from"><span class="header-section-number">3</span> Where does the James-Stein Estimator Come From?</h2>
<section id="an-infeasible-estimator-when-p-2" class="level3">
<h3 class="anchored" data-anchor-id="an-infeasible-estimator-when-p-2">An Infeasible Estimator When <img src="https://latex.codecogs.com/png.latex?p%20=%202"></h3>
<p>To start the ball rolling, let’s <a href="https://en.wikipedia.org/wiki/Assume_a_can_opener">assume a can-opener</a>: suppose that we don’t know any of the <em>individual</em> means <img src="https://latex.codecogs.com/png.latex?%5Cmu_j"> but for some strange reason a benevolent deity has told us the value of their sum of squares: <img src="https://latex.codecogs.com/png.latex?%0Ac%5E2%20%5Cequiv%20%5Csum_%7Bj=1%7D%5Ep%20%5Cmu_j%5E2.%0A"> It turns out that this is enough information to construct a shrinkage estimator that <em>always</em> has a lower composite MSE than the ML estimator. Let’s see why this is the case. If <img src="https://latex.codecogs.com/png.latex?p%20=%201">, then telling you <img src="https://latex.codecogs.com/png.latex?c%5E2"> is the same as telling you <img src="https://latex.codecogs.com/png.latex?%5Cmu%5E2">. Granted, knowledge of <img src="https://latex.codecogs.com/png.latex?%5Cmu%5E2"> isn’t as informative as knowledge of <img src="https://latex.codecogs.com/png.latex?%5Cmu">. For example, if I told you that <img src="https://latex.codecogs.com/png.latex?%5Cmu%5E2%20=%209"> you couldn’t tell whether <img src="https://latex.codecogs.com/png.latex?%5Cmu%20=%203"> or <img src="https://latex.codecogs.com/png.latex?%5Cmu%20=%20-3">. But, as we showed above, the optimal shrinkage estimator when <img src="https://latex.codecogs.com/png.latex?p=1"> sets <img src="https://latex.codecogs.com/png.latex?%5Clambda%5E*%20=%201/(1%20+%20%5Cmu%5E2)"> and yields an MSE of <img src="https://latex.codecogs.com/png.latex?%5Cmu%5E2/(1%20+%20%5Cmu%5E2)%20%3C%201">. Since <img src="https://latex.codecogs.com/png.latex?%5Clambda%5E*"> only depends on <img src="https://latex.codecogs.com/png.latex?%5Cmu"> through <img src="https://latex.codecogs.com/png.latex?%5Cmu%5E2">, we’ve <em>already shown</em> that knowledge of <img src="https://latex.codecogs.com/png.latex?c%5E2"> allows us to construct a shrinkage estimator that dominates the ML estimator when <img src="https://latex.codecogs.com/png.latex?p%20=%201">.</p>
<p>So what if <img src="https://latex.codecogs.com/png.latex?p"> equals 2? In this case, knowledge of <img src="https://latex.codecogs.com/png.latex?c%5E2%20=%20%5Cmu_1%5E2%20+%20%5Cmu_2%5E2"> is equivalent to knowing the <em>radius</em> of a circle centered at the origin in the <img src="https://latex.codecogs.com/png.latex?(%5Cmu_1,%20%5Cmu_2)"> plane where the two unknown means must lie. For example, if I told you that <img src="https://latex.codecogs.com/png.latex?c%5E2%20=%201"> you would know that <img src="https://latex.codecogs.com/png.latex?(%5Cmu_1,%20%5Cmu_2)"> lies somewhere on a circle of radius one centered at the origin. As illustrated in the following plot, the points <img src="https://latex.codecogs.com/png.latex?(x_1,%20x_2)"> and <img src="https://latex.codecogs.com/png.latex?(y_1,%20y_2)"> would then be potential values of <img src="https://latex.codecogs.com/png.latex?(%5Cmu_1,%20%5Cmu_2)"> as would all other points on the blue circle.</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/not-quite-the-james-stein-estimator/index_files/figure-html/unnamed-chunk-5-1.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>So how can we construct a shrinkage estimator of <img src="https://latex.codecogs.com/png.latex?(%5Cmu_1,%20%5Cmu_2)"> with lower composite MSE than the ML estimator if <img src="https://latex.codecogs.com/png.latex?c%5E2"> is known? While there are other possibilities, the simplest would be to use the <em>same</em> shrinkage factor for each of the two coordinates. In other words, our estimator would be <img src="https://latex.codecogs.com/png.latex?%0A%5Chat%7B%5Cmu%7D_1(%5Clambda)%20=%20(1%20-%20%5Clambda)X_1,%20%5Cquad%20%5Chat%7B%5Cmu%7D_2(%5Clambda)%20=%20(1%20-%20%5Clambda)X_2%0A"> for some <img src="https://latex.codecogs.com/png.latex?%5Clambda"> between zero and one. The composite MSE of this estimator is just the sum of the MSE of each <em>individual</em> component, so we can re-use our algebra from above to obtain <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Ctext%7BMSE%7D%5B%5Chat%7B%5Cmu%7D_1(%5Clambda)%5D%20+%20%5Ctext%7BMSE%7D%5B%5Chat%7B%5Cmu%7D_2(%5Clambda)%5D%20&amp;=%20%5B(1%20-%20%5Clambda)%5E2%20+%20%5Clambda%5E2%5Cmu_1%5E2%5D%20+%20%5B(1%20-%20%5Clambda)%5E2%20+%20%5Clambda%5E2%5Cmu_2%5E2%5D%20%5C%5C%0A&amp;=%202(1%20-%20%5Clambda)%5E2%20+%20%5Clambda%5E2(%5Cmu_1%5E2%20+%20%5Cmu_2%5E2)%20%5C%5C%0A&amp;=%202(1%20-%20%5Clambda)%5E2%20+%20%5Clambda%5E2c%5E2.%0A%5Cend%7Baligned%7D%0A"> Notice that the composite MSE only depends on <img src="https://latex.codecogs.com/png.latex?(%5Cmu_1,%20%5Cmu_2)"> through their sum of squares, <img src="https://latex.codecogs.com/png.latex?c%5E2">. Differentiating with respect to <img src="https://latex.codecogs.com/png.latex?%5Clambda">, just as we did above in the <img src="https://latex.codecogs.com/png.latex?p=1"> case, <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cfrac%7Bd%7D%7Bd%5Clambda%7D%5Cleft%5B2(1%20-%20%5Clambda)%5E2%20+%20%5Clambda%5E2c%5E2%5Cright%5D%20&amp;=%20-4(1%20-%20%5Clambda)%20+%202%5Clambda%20c%5E2%20%5C%5C%0A&amp;=%202%20%5Cleft%5B%5Clambda%20(2%20+%20c%5E2)%20-%202%5Cright%5D%5C%5C%20%5C%5C%0A%5Cfrac%7Bd%5E2%7D%7Bd%5Clambda%5E2%7D%5Cleft%5B2(1%20-%20%5Clambda)%5E2%20+%20%5Clambda%5E2c%5E2%5Cright%5D%20&amp;=%202(2%20+%20c%5E2)%20%3E%200%0A%5Cend%7Baligned%7D%0A"> so there is a unique global minimum at <img src="https://latex.codecogs.com/png.latex?%5Clambda%5E*%20=%202/(2%20+%20c%5E2)">. Substituting this value of <img src="https://latex.codecogs.com/png.latex?%5Clambda"> into the expression for the composite MSE, a few lines of algebra give <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Ctext%7BMSE%7D%5B%5Chat%7B%5Cmu%7D_1(%5Clambda%5E*)%5D%20+%20%5Ctext%7BMSE%7D%5B%5Chat%7B%5Cmu%7D_2(%5Clambda%5E*)%5D%20&amp;=%202%5Cleft(1%20-%20%5Cfrac%7B2%7D%7B2%20+%20c%5E2%7D%5Cright)%5E2%20+%20%5Cleft(%5Cfrac%7B2%7D%7B2%20+%20c%5E2%7D%5Cright)%5E2c%5E2%20%5C%5C%0A&amp;=%202%5Cleft(%5Cfrac%7Bc%5E2%7D%7B2%20+%20c%5E2%7D%5Cright).%0A%5Cend%7Baligned%7D%0A"> Since <img src="https://latex.codecogs.com/png.latex?c%5E2/(2%20+%20c%5E2)%20%3C%201"> for all <img src="https://latex.codecogs.com/png.latex?c%5E2%20%3E%200">, the optimal shrinkage estimator <em>always</em> has a composite MSE less than <img src="https://latex.codecogs.com/png.latex?2">, the composite MSE of the ML estimator. Strictly speaking this estimator is <strong>infeasible</strong> since we don’t know <img src="https://latex.codecogs.com/png.latex?c%5E2">. But it’s a crucial step on our journey to make the leap from applying shrinkage to an estimator for a <em>single</em> unknown mean, to using the same idea for <em>more than one</em> unknown mean.</p>
</section>
<section id="a-simulation-experiment-for-p-2" class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="a-simulation-experiment-for-p-2">A Simulation Experiment for <img src="https://latex.codecogs.com/png.latex?p%20=%202"></h3>
<p>You may have already noticed that it’s easy to generalize this argument to <img src="https://latex.codecogs.com/png.latex?p%3E2">. But before we consider the general case, let’s take a moment to understand the geometry of shrinkage estimation for <img src="https://latex.codecogs.com/png.latex?p=2"> a bit more deeply. The nice thing about two-dimensional problems is that they’re easy to plot. So here’s a graphical representation of both the ML estimator and our infeasible optimum shrinkage estimator when <img src="https://latex.codecogs.com/png.latex?p%20=%202">. I’ve set the true, unknown, values of <img src="https://latex.codecogs.com/png.latex?%5Cmu_1"> and <img src="https://latex.codecogs.com/png.latex?%5Cmu_2"> to one so the true value of <img src="https://latex.codecogs.com/png.latex?c%5E2"> is <img src="https://latex.codecogs.com/png.latex?2"> and the optimal choice of <img src="https://latex.codecogs.com/png.latex?%5Clambda"> is <img src="https://latex.codecogs.com/png.latex?%5Clambda%5E*%20=%202/(2%20+%20c%5E2)%20=%202/4%20=%200.5">. The following R code simulates our estimators and visualizes their performance, helping us see the shrinkage effect in action.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1983</span>)</span>
<span id="cb3-2"></span>
<span id="cb3-3">nreps <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span></span>
<span id="cb3-4">mu1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mu2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb3-5">x1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mu1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(nreps)</span>
<span id="cb3-6">x2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mu2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(nreps)</span>
<span id="cb3-7"></span>
<span id="cb3-8">csq <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mu1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> mu2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb3-9">lambda <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> csq <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> csq)</span>
<span id="cb3-10"></span>
<span id="cb3-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb3-12"></span>
<span id="cb3-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Left panel: ML Estimator</span></span>
<span id="cb3-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(x1, x2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MLE'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pch =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'black'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cex =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, </span>
<span id="cb3-15">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(mu[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(mu[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]))</span>
<span id="cb3-16"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> mu1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb3-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> mu2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb3-18"></span>
<span id="cb3-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add MSE to the plot</span></span>
<span id="cb3-20"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MSE ="</span>, </span>
<span id="cb3-21">                                  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>((x1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mu1)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (x2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mu2)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)))</span>
<span id="cb3-22"></span>
<span id="cb3-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Right panel: Shrinkage Estimator</span></span>
<span id="cb3-24"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(x1, x2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Shrinkage'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(mu[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]), </span>
<span id="cb3-25">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(mu[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]))</span>
<span id="cb3-26"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">points</span>(lambda <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x1, lambda <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pch =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'blue'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cex =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb3-27"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">segments</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x0 =</span> x1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y0 =</span> x2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x1 =</span> lambda <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y1 =</span> lambda <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb3-28"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> mu1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb3-29"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> mu2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb3-30"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb3-31"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb3-32"></span>
<span id="cb3-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add MSE to the plot</span></span>
<span id="cb3-34"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MSE ="</span>, </span>
<span id="cb3-35">                                  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>((lambda <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mu1)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb3-36">                                               (lambda <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mu2)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/not-quite-the-james-stein-estimator/index_files/figure-html/unnamed-chunk-6-1.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>My plot has two panels. The left panel shows the raw data. Each black point is a pair <img src="https://latex.codecogs.com/png.latex?(X_1,%20X_2)"> of independent normal draws with means <img src="https://latex.codecogs.com/png.latex?(%5Cmu_1%20=%201,%20%5Cmu_2%20=%201)"> and variances <img src="https://latex.codecogs.com/png.latex?(1,%201)">. As such, each point is also the <em>ML estimate</em> (MLE) of <img src="https://latex.codecogs.com/png.latex?(%5Cmu_1,%20%5Cmu_2)"> based on <img src="https://latex.codecogs.com/png.latex?(X_1,%20X_2)">. The red cross shows the location of the true values of <img src="https://latex.codecogs.com/png.latex?(%5Cmu_1,%20%5Cmu_2)">, namely <img src="https://latex.codecogs.com/png.latex?(1,%201)">. There are 50 points in the plot, representing 50 replications of the simulation, each independent of the rest and with the same parameter values. This allows us to measure how close the ML estimator is to the true value of <img src="https://latex.codecogs.com/png.latex?(%5Cmu_1,%20%5Cmu_2)"> in repeated sampling, approximating the composite MSE.</p>
<p>The right panel is more complicated. This shows <em>both</em> the ML estimates (unfilled black circles) <em>and</em> the corresponding shrinkage estimates (filled blue circles) along with dashed lines connecting them. Each shrinkage estimate is constructed by “pulling” the corresponding MLE towards the origin by a factor of <img src="https://latex.codecogs.com/png.latex?%5Clambda%20=%200.5">. Thus, if a given unfilled black circle is located at <img src="https://latex.codecogs.com/png.latex?(X_1,%20X_2)">, the corresponding filled blue circle is located at <img src="https://latex.codecogs.com/png.latex?(0.5X_1,%200.5X_2)">. As in the left panel, the red cross in the right panel shows the true values of <img src="https://latex.codecogs.com/png.latex?(%5Cmu_1,%20%5Cmu_2)">, namely <img src="https://latex.codecogs.com/png.latex?(1,%201)">. The black cross, on the other hand, shows the point towards which the shrinkage estimator pulls the ML estimator, namely <img src="https://latex.codecogs.com/png.latex?(0,%200)">.</p>
<p>We see immediately that the ML estimator is <em>unbiased</em>: the black filled dots in the left panel (along with the unfilled ones in the right) are centered at <img src="https://latex.codecogs.com/png.latex?(1,%201)">. But the ML estimator is also <em>high-variance</em>: the black dots are quite spread out around <img src="https://latex.codecogs.com/png.latex?(1,%201)">. We can approximate the composite MSE of the ML estimator by computing the average squared Euclidean distance between the black points and the red cross.<sup>7</sup> And in keeping with our theoretical calculations, the simulation gives a composite MSE of almost exactly 2 for the ML estimator.</p>
<div class="no-row-height column-margin column-container"><div id="fn7"><p><sup>7</sup>&nbsp;Remember that there are two equivalent definitions of MSE: bias squared plus variance on the one hand and expected squared distance from the truth on the other hand.</p></div></div><p>In contrast, the optimal shrinkage estimator is <em>biased</em>: the filled blue dots in the right panel centered somewhere between the red cross (the true means) and the origin. But the shrinkage estimator also has a lower variance: the filled blue dots are much closer together than the black ones. Even more importantly <em>they are on average closer to</em> <img src="https://latex.codecogs.com/png.latex?(%5Cmu_1,%20%5Cmu_2)">, as indicated by the red cross and as measured by composite MSE. Our theoretical calculations showed that the composite MSE of the optimal shrinkage estimator equals <img src="https://latex.codecogs.com/png.latex?2c%5E2/(2%20+%20c%5E2)">. When <img src="https://latex.codecogs.com/png.latex?c%5E2%20=%202">, as in this case, we obtain <img src="https://latex.codecogs.com/png.latex?2%5Ctimes%202/(2%20+%202)%20=%201">. Again, this is almost exactly what we see in the simulation.</p>
<p>If we had used more than 50 simulation replications, the composite MSE values would have been even closer to our theoretical predictions, at the cost of making the plot much harder to read! But I hope the key point is still clear: shrinkage <em>pulls</em> the MLE towards the origin, and can give a <em>much</em> lower composite MSE.</p>
</section>
<section id="an-infeasible-estimator-the-general-case" class="level3">
<h3 class="anchored" data-anchor-id="an-infeasible-estimator-the-general-case">An Infeasible Estimator: The General Case</h3>
<p>Now that we understand the case of <img src="https://latex.codecogs.com/png.latex?p=2">, the general case is a snap. Our shrinkage estimator of each <img src="https://latex.codecogs.com/png.latex?%5Cmu_j"> will take the form <img src="https://latex.codecogs.com/png.latex?%0A%5Chat%7B%5Cmu%7D_j(%5Clambda)%20=%20(1%20-%20%5Clambda)%20X_j,%20%5Cquad%20j%20=%201,%20%5Cdots,%20p%0A"> for some <img src="https://latex.codecogs.com/png.latex?%5Clambda"> between zero and one. To find the optimal choice of <img src="https://latex.codecogs.com/png.latex?%5Clambda">, we minimize <img src="https://latex.codecogs.com/png.latex?%0A%5Csum_%7Bj=1%7D%5Ep%5Ctext%7BMSE%7D%5Cleft%5B%5Chat%7B%5Cmu%7D_j(%5Clambda)%20%5Cright%5D%20=%20%5Csum_%7Bj=1%7D%5Ep%20%5Cleft%5B(1%20-%20%5Clambda)%5E2%20+%20%5Clambda%5E2%20%5Cmu_j%5E2%5Cright%5D%20=%20p(1%20-%20%5Clambda)%5E2%20+%20%5Clambda%5E2%20c%5E2%0A"> with respect to <img src="https://latex.codecogs.com/png.latex?%5Clambda">. Again, the key is that the composite MSE only depends on the unknown means through <img src="https://latex.codecogs.com/png.latex?c%5E2">. Using almost exactly the same calculations as above for the case of <img src="https://latex.codecogs.com/png.latex?p%20=%202">, we find that <img src="https://latex.codecogs.com/png.latex?%0A%5Clambda%5E*%20=%20%5Cfrac%7Bp%7D%7Bp%20+%20c%5E2%7D,%20%5Cquad%20%5Csum_%7Bj=1%7D%5Ep%20%5Ctext%7BMSE%7D%5Cleft%5B%5Chat%7B%5Cmu%7D_j(%5Clambda%5E*)%20%5Cright%5D%20=%20p%5Cleft(%5Cfrac%7Bc%5E2%7D%7Bp%20+%20c%5E2%7D%5Cright).%0A"> since <img src="https://latex.codecogs.com/png.latex?c%5E2/(p%20+%20c%5E2)%20%3C%201"> for all <img src="https://latex.codecogs.com/png.latex?c%5E2%20%3E%200">, the optimal shrinkage estimator <em>always</em> has a composite MSE less than <img src="https://latex.codecogs.com/png.latex?p">, the composite MSE of the ML estimator.</p>
</section>
<section id="not-quite-the-james-stein-estimator" class="level3">
<h3 class="anchored" data-anchor-id="not-quite-the-james-stein-estimator">Not Quite the James-Stein Estimator</h3>
<p>The end is in sight! We’ve shown that if we knew the sum of squares of the unknown means, <img src="https://latex.codecogs.com/png.latex?c%5E2">, we could construct a shrinkage estimator that always has a lower composite MSE than the ML estimator. But we don’t know <img src="https://latex.codecogs.com/png.latex?c%5E2">. So what can we do? To start off, re-write <img src="https://latex.codecogs.com/png.latex?%5Clambda%5E*"> as follows <img src="https://latex.codecogs.com/png.latex?%0A%5Clambda%5E*%20=%20%5Cfrac%7Bp%7D%7Bp%20+%20c%5E2%7D%20=%20%5Cfrac%7B1%7D%7B1%20+%20c%5E2/p%7D.%0A"> This way of writing things makes it clear that it’s not <img src="https://latex.codecogs.com/png.latex?c%5E2"> <em>per se</em> that matters but rather <img src="https://latex.codecogs.com/png.latex?c%5E2/p">. And this quantity is simply is the <em>average</em> of the unknown squared means: <img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7Bc%5E2%7D%7Bp%7D%20=%20%5Cfrac%7B1%7D%7Bp%7D%5Csum_%7Bj=1%7D%5Ep%20%5Cmu_j%5E2.%0A"> So how could we learn <img src="https://latex.codecogs.com/png.latex?c%5E2/p">? An idea that immediately suggests itself is to estimate this quantity by replacing each unobserved <img src="https://latex.codecogs.com/png.latex?%5Cmu_j"> with the corresponding observation <img src="https://latex.codecogs.com/png.latex?X_j">, in other words <img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7B1%7D%7Bp%7D%5Csum_%7Bj=1%7D%5Ep%20X_j%5E2.%0A"> This is a good starting point, but we can do better. Since <img src="https://latex.codecogs.com/png.latex?X_j%20%5Csim%20%5Ctext%7BNormal%7D(%5Cmu_j,%201)">, we see that <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BE%7D%5Cleft%5B%5Cfrac%7B1%7D%7Bp%7D%20%5Csum_%7Bj=1%7D%5Ep%20X_j%5E2%20%5Cright%5D%20=%20%5Cfrac%7B1%7D%7Bp%7D%20%5Csum_%7Bj=1%7D%5Ep%20%5Cmathbb%7BE%7D%5BX_j%5E2%5D%20=%20%5Cfrac%7B1%7D%7Bp%7D%20%5Csum_%7Bj=1%7D%5Ep%20%5B%5Ctext%7BVar%7D(X_j)%20+%20%5Cmathbb%7BE%7D(X_j)%5E2%5D%20=%20%5Cfrac%7B1%7D%7Bp%7D%20%5Csum_%7Bj=1%7D%5Ep%20(1%20+%20%5Cmu_j%5E2)%20=%201%20+%20%5Cfrac%7Bc%5E2%7D%7Bp%7D.%0A"> This means that <img src="https://latex.codecogs.com/png.latex?(%5Csum_%7Bj=1%7D%5Ep%20X_j%5E2)/p"> will on average <em>overestimate</em> <img src="https://latex.codecogs.com/png.latex?c%5E2/p"> by one. But that’s a problem that’s easy to fix: simply subtract one! This is a rare situation in which there is <em>no bias-variance tradeoff</em>. Subtracting a constant, in this case one, doesn’t contribute any additional variation while completely removing the bias. Plugging into our formula for <img src="https://latex.codecogs.com/png.latex?%5Clambda%5E*">, this suggests using the estimator <img src="https://latex.codecogs.com/png.latex?%0A%5Chat%7B%5Clambda%7D%20%5Cequiv%20%5Cfrac%7B1%7D%7B1%20+%20%5Cleft%5B%5Cleft(%5Cfrac%7B1%7D%7Bp%7D%5Csum_%7Bj=1%7D%5Ep%20X_j%5E2%20%5Cright)%20-%201%5Cright%5D%7D%20=%20%5Cfrac%7B1%7D%7B%5Cfrac%7B1%7D%7Bp%7D%5Csum_%7Bj=1%7D%5Ep%20X_j%5E2%7D%20=%20%5Cfrac%7Bp%7D%7B%5Csum_%7Bj=1%7D%5Ep%20X_j%5E2%7D%0A"> as our stand-in for the unknown <img src="https://latex.codecogs.com/png.latex?%5Clambda%5E*">, yielding a shrinkage estimator that I’ll call “NQ” for “not quite” for reasons that will become apparent in a moment: <img src="https://latex.codecogs.com/png.latex?%0A%5Chat%7B%5Cmu%7D%5E%7B(j)%7D_%5Ctext%7BNQ%7D%20=%20%5Cleft(1%20-%20%5Cfrac%7Bp%7D%7B%5Csum_%7Bk=1%7D%5Ep%20X_k%5E2%7D%5Cright)X_j.%0A"> Notice what’s happening here: our optimal shrinkage estimator depends on <img src="https://latex.codecogs.com/png.latex?c%5E2/p">, something we can’t observe. But we’ve constructed an <em>unbiased estimator</em> of this quantity by using <em>all of the observations</em> <img src="https://latex.codecogs.com/png.latex?X_j">. This is the resolution of the paradox discussed above: all of the observations contain information about <img src="https://latex.codecogs.com/png.latex?c%5E2"> since this is simply the sum of the squared means. And because we’ve chosen to minimize composite MSE, the optimal shrinkage factor only depends on the individual <img src="https://latex.codecogs.com/png.latex?%5Cmu_j"> parameters through <img src="https://latex.codecogs.com/png.latex?c%5E2">! This is the sense in which it’s possible to learn something useful about, say, <img src="https://latex.codecogs.com/png.latex?%5Cmu_1"> from <img src="https://latex.codecogs.com/png.latex?X_2"> in spite of the fact that <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5BX_2%5D%20=%20%5Cmu_2"> may bear no relationship to <img src="https://latex.codecogs.com/png.latex?%5Cmu_1">.</p>
<p>But wait a minute! This looks <em>suspiciously familiar</em>. Recall that the James-Stein estimator is given by <img src="https://latex.codecogs.com/png.latex?%0A%5Chat%7B%5Cmu%7D%5E%7B(j)%7D_%5Ctext%7BJS%7D%20=%20%5Cleft(1%20-%20%5Cfrac%7Bp%20-%202%7D%7B%5Csum_%7Bk=1%7D%5Ep%20X_k%5E2%7D%5Cright)X_j.%0A"> Just like the JS estimator, my NQ estimator shrinks each of the <img src="https://latex.codecogs.com/png.latex?p"> means towards zero by a factor that depends on the number of means we’re estimating, <img src="https://latex.codecogs.com/png.latex?p">, and the overall sum of the squared observations. The key difference between JS and NQ is that JS uses <img src="https://latex.codecogs.com/png.latex?p%20-%202"> in the numerator instead of <img src="https://latex.codecogs.com/png.latex?p">. This means that NQ is a more “aggressive” shrinkage estimator than JS: it pulls the means towards zero by a larger amount than JS. This difference turns out to be crucial for proving that the JS estimator dominates the ML estimator. But when it comes to understanding why the JS estimator has the <em>form</em> that it does, I would argue that the difference is minor. If you want all the gory details of where that extra <img src="https://latex.codecogs.com/png.latex?-2"> comes from, along with the closely related issue of why <img src="https://latex.codecogs.com/png.latex?p%5Cgeq%203"> is crucial for JS to dominate the ML estimator, see <a href="https://ditraglia.com/econ722/slides/econ722slides.pdf">lecture 1</a> or <a href="https://ditraglia.com/econ722/main.pdf">section 7.3</a> from my Econ 722 teaching materials.</p>
</section>
</section>
<section id="conclusion" class="level2" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="conclusion"><span class="header-section-number">4</span> Conclusion</h2>
<p>Before we conclude, there’s one important caveat to bear in mind. In addition to the qualifications that NQ isn’t <em>quite</em> JS, and that JS only dominates the MLE when <img src="https://latex.codecogs.com/png.latex?p%20%5Cgeq%203">, there’s one more fundamental issue that could be easily missed. Our decision to minimize <em>composite</em> MSE is <em>absolutely crucial</em> to the reasoning given above. The magic of shrinkage depends on our willingness to accept a trade-off in which we do a worse job estimating one mean in exchange for doing a better job estimating another, as composite MSE imposes. Whether this makes sense in practice depends on the context.</p>
<p>If we’re searching for a lost submarine in the ocean (a 3-dimensional problem), it makes perfect sense to be willing to be farther from the submarine in one dimension in exchange for being closer in another. That’s because <em>Euclidean distance</em> is obviously what we’re after here. But if instead we’re estimating <a href="https://ideas.repec.org/p/nbr/nberwo/27094.html">teacher value-added</a> and the results of our estimation exercise will be used to determine which teachers lose their jobs, it’s less clear that we should be willing to be farther from one teacher in exchange for being closer to another. Certainly that would be no consolation to someone who had been wrongly dismissed! If we were merely using this information to identify teachers who might need extra help, it’s another story. But the point I’m trying to make here is that our choice of which criterion to minimize necessarily encodes our <em>values</em> in a particular problem.</p>
<p>But with that said, I hope you’re satisfied that this extremely long post was worth the effort. Without using any fancy mathematics or statistical theory, we’ve managed to invent something that is <em>nearly identical</em> to the James-Stein estimator and thus to resolve Stein’s paradox. We started by pretending what we knew <img src="https://latex.codecogs.com/png.latex?c%5E2"> and showed that this would allow us to derive a shrinkage estimator with a lower composite MSE than the ML estimator. Then we simply plugged in an unbiased estimator of the key unknown quantity: <img src="https://latex.codecogs.com/png.latex?c%5E2/p">. Because all the observations contain information about <img src="https://latex.codecogs.com/png.latex?c%5E2">, it makes sense that we should decide how much to shrink one component <img src="https://latex.codecogs.com/png.latex?X_j"> by using all of the others. At this point, I hope that the James-Stein estimator seems not only plausible but practically <em>obvious</em>, excepting of course that pesky <img src="https://latex.codecogs.com/png.latex?-2"> in the numerator.</p>


</section>


 ]]></description>
  <category>statistics</category>
  <guid>https://www.econometrics.blog/post/not-quite-the-james-stein-estimator/</guid>
  <pubDate>Sat, 10 Aug 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>How to Do Regression Adjustment</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/how-to-do-regression-adjustment/</link>
  <description><![CDATA[ 




<p>By the end of a typical introductory econometrics course students have become accustomed to the idea of “controlling” for covariates by adding them to the end of a linear regression model. But this familiarity can sometimes cause confusion when students later encounter <em>regression adjustment</em>, a widely-used approach to causal inference under the selection-on-observables assumption. While regression adjustment is simple in theory, the finer points of how and when to apply it in practice are much more subtle. One of these finer points is how to tell whether a particular covariate is a “good control” that will help us learn the causal effect of interest or a “bad control” that will only make things worse.<sup>1</sup> Another, and the topic of today’s post, is how to actually <em>implement</em> regression adjustment after we’ve decided which covariates to adjust for.</p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;See <a href="https://www.econometrics.blog/post/a-good-instrument-is-a-bad-control/">this post</a> for a prototypical example of a “bad control” and the second half of my <a href="https://www.treatment-effects.com/02-selection-on-observables.pdf">slides</a> for some general discussion of “bad controls.” These <a href="https://ditraglia.com/erm/15-selection-on-observables.pdf">alternative slides</a> from my <a href="https://ditraglia.com/erm/">core ERM</a> course cover similar ground but make a more explicit connection to good and bad advice about bad controls that one encounters in introductory econometrics books.</p></div></div><p>The pre-requisites for this post are a basic understanding of selection-on-observables and regression adjustment. If you’re a bit rusty on these points, you might find it helpful to glance at the first half of my <a href="https://www.treatment-effects.com/02-selection-on-observables.pdf">lecture slides</a> along with this series of <a href="https://youtube.com/playlist?list=PLi6qbNWpQUeM1kKYjqq36aY5WQ1Zn-I6E&amp;si=zqxf9LmexMh0cL2E">short videos</a>. If you’re still hungry for more after this, you might also enjoy this <a href="https://www.econometrics.blog/post/misunderstanding-selection-on-observables/">earlier post</a> from <a href="https://econometrics.blog">econometrics.blog</a> on common misunderstandings about the selection-on-observables assumption.</p>
<section id="a-quick-review" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="a-quick-review"><span class="header-section-number">1</span> A Quick Review</h2>
<p>Consider a binary treatment <img src="https://latex.codecogs.com/png.latex?D"> and an observed outcome <img src="https://latex.codecogs.com/png.latex?Y">. Let <img src="https://latex.codecogs.com/png.latex?(Y_0,%20Y_1)"> be the <a href="https://youtu.be/EXgOSj7GdSs?si=0Nhx5p2GwJHH3d69">potential outcomes</a> corresponding to the treatment <img src="https://latex.codecogs.com/png.latex?D">. Our goal is to learn the average treatment effect <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BATE%7D%20%5Cequiv%20%5Cmathbb%7BE%7D(Y_1%20-%20Y_0)"> but, unless <img src="https://latex.codecogs.com/png.latex?D"> is randomly assigned, using the difference of observed means <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y%7CD=1)%20-%20%5Cmathbb%7BE%7D(Y%7CD=0)"> to estimate the ATE in general <a href="https://youtu.be/zbgN0GLolFo?si=aYH_huGqezsWIuUv">won’t work</a>. The idea of <strong>selection-on-observables</strong> is that <img src="https://latex.codecogs.com/png.latex?D"> might be “as good as randomly assigned” after we adjust for a collection of observed covariates <img src="https://latex.codecogs.com/png.latex?X">.</p>
<p>Regression adjustment relies on two assumptions: <strong>selection-on-observables</strong> and <strong>overlap</strong>. The selection-on-observables assumption says that learning <img src="https://latex.codecogs.com/png.latex?D"> provides no additional information about the average values of <img src="https://latex.codecogs.com/png.latex?Y_0"> and <img src="https://latex.codecogs.com/png.latex?Y_1">, provided that we already know <img src="https://latex.codecogs.com/png.latex?X">. This implies that we can learn the <em>conditional average treatment effect</em> (CATE) by comparing observed outcomes of the treated and untreated <strong>holding <img src="https://latex.codecogs.com/png.latex?X"> fixed</strong>: <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BCATE%7D(x)%20%5Cequiv%20%5Cmathbb%7BE%7D%5BY_1%20-%20Y_0%7CX%20=%20x%5D%20=%20%5Cmathbb%7BE%7D%5BY%7CD=1,%20X%20=%20x%5D%20-%20%5Cmathbb%7BE%7D%5BY%7CD=0,%20X%20=%20x%5D.%0A"> For example: older people might be more likely to take a new medication but also more likely to die without it. If so, perhaps by comparing average outcomes <em>holding age fixed</em> we can learn the causal effect of the medication. The overlap assumption says that, for any fixed value <img src="https://latex.codecogs.com/png.latex?x"> of the covariates, there are some treated and some untreated people. This allows us to learn <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCATE%7D(x)"> for every value of <img src="https://latex.codecogs.com/png.latex?x"> in the population and average it using the law of iterated expectations to recover the ATE:<br>
<img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BATE%7D%20=%20%5Cmathbb%7BE%7D%5B%5Ctext%7BCATE%7D(X)%5D%20=%20%5Cmathbb%7BE%7D%5B%5Cmathbb%7BE%7D(Y%7CD=1,%20X)%20-%20%5Cmathbb%7BE%7D(Y%7CD=0,%20X)%5D.%0A"> In the medication example, this would correspond to computing the difference of means for each age group <em>separately</em>, and then averaging them using the share of people in each age group. Notice that this is only possible if there are some people who took the medication and some who didn’t in each age group. That’s exactly what the overlap assumption buys us. For example, if there were no senior citizens who <em>didn’t</em> take the medication, we wouldn’t be able to learn the effect of the medication for senior citizens.</p>
</section>
<section id="which-regression-should-we-run" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="which-regression-should-we-run"><span class="header-section-number">2</span> Which regression should we run?</h2>
<p>So suppose that we’ve found a set of covariates <img src="https://latex.codecogs.com/png.latex?X"> that satisfy the required assumptions. How should we actually <em>carry out</em> regression adjustment? To answer this question, let’s start by making things a bit simpler. Suppose that <img src="https://latex.codecogs.com/png.latex?X"> is a single <em>binary</em> covariate. At the end of the post, we’ll return to the general case. Since <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?D"> are both binary, we can write the conditional mean function of <img src="https://latex.codecogs.com/png.latex?Y"> given <img src="https://latex.codecogs.com/png.latex?(D,%20X)"> as <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BE%7D(Y%7CD,%20X)%20=%20%5Cbeta_0%20+%20%5Cbeta_1%20D%20+%20%5Cbeta_2%20X%20+%20%5Cbeta_3%20DX.%0A"> Since the true conditional mean function is linear, a linear regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?D">, <img src="https://latex.codecogs.com/png.latex?X">, <img src="https://latex.codecogs.com/png.latex?DX"> and an intercept will recover <img src="https://latex.codecogs.com/png.latex?(%5Cbeta_0,%20%5Cbeta_1,%20%5Cbeta_2,%20%5Cbeta_3)">. But what on earth do these coefficients actually <em>mean</em>?! Substituting all possible values of <img src="https://latex.codecogs.com/png.latex?(D,%20X)">, <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cmathbb%7BE%7D(Y%7CD=0,%20X=0)%20&amp;=%20%5Cbeta_0%20%5C%5C%0A%5Cmathbb%7BE%7D(Y%7CD=1,%20X=0)%20&amp;=%20%5Cbeta_0%20+%20%5Cbeta_1%20%5C%5C%0A%5Cmathbb%7BE%7D(Y%7CD=0,%20X=1)%20&amp;=%20%5Cbeta_0%20+%20%5Cbeta_2%20%5C%5C%0A%5Cmathbb%7BE%7D(Y%7CD=1,%20X=1)%20&amp;=%20%5Cbeta_0%20+%20%5Cbeta_1%20+%20%5Cbeta_2%20+%20%5Cbeta_3.%0A%5Cend%7Baligned%7D%0A"> And so, after a bit of re-arranging, <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cbeta_0%20&amp;=%20%5Cmathbb%7BE%7D(Y%7CD=0,%20X=0)%5C%5C%0A%5Cbeta_1%20&amp;=%20%5Cmathbb%7BE%7D(Y%7CD=1,%20X=0)%20-%20%5Cmathbb%7BE%7D(Y%7CD=0,%20X=0)%5C%5C%0A%5Cbeta_2%20&amp;=%20%5Cmathbb%7BE%7D(Y%7CD=0,%20X=1)%20-%20%5Cmathbb%7BE%7D(Y%7CD=0,%20X=0)%5C%5C%0A%5Cbeta_3%20&amp;=%20%5Cmathbb%7BE%7D(Y%7CD=1,%20X=1)%20-%20%5Cmathbb%7BE%7D(Y%7CD=1,%20X=0)%20-%20%5Cmathbb%7BE%7D(Y%7CD=0,%20X=1)%20+%20%5Cmathbb%7BE%7D(Y%7CD=0,%20X=0).%0A%5Cend%7Baligned%7D%0A"> <strong>What a mess!</strong> Alas, we’ll need a few more steps of algebra to figure out how these relate to the ATE. Notice that <img src="https://latex.codecogs.com/png.latex?%5Cbeta_1"> equals the CATE when <img src="https://latex.codecogs.com/png.latex?X=0"> since <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Ctext%7BCATE%7D(0)%20&amp;%5Cequiv%20%5Cmathbb%7BE%7D(Y%7CD=1,%20X=0)%20-%20%5Cmathbb%7BE%7D(Y%7CD=0,%20X=0)%5C%5C%0A&amp;=%20(%5Cbeta_0%20+%20%5Cbeta_1)%20-%20%5Cbeta_0%5C%5C%0A&amp;%20=%20%5Cbeta_1%0A%5Cend%7Baligned%7D%0A"> Proceeding similarly for the CATE when <img src="https://latex.codecogs.com/png.latex?X%20=%201">, we find that <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Ctext%7BCATE%7D(1)%20&amp;%5Cequiv%20%5Cmathbb%7BE%7D(Y%7CD=1,%20X=1)%20-%20%5Cmathbb%7BE%7D(Y%7CD=0,%20X=1)%20%5C%5C%0A&amp;=%20(%5Cbeta_0%20+%20%5Cbeta_1%20+%20%5Cbeta_2%20+%20%5Cbeta_3)%20-%20(%5Cbeta_0%20+%20%5Cbeta_2)%20%5C%5C%0A&amp;=%20%5Cbeta_1%20+%20%5Cbeta_3.%0A%5Cend%7Baligned%7D%0A"> Now that we have expressions for each of the two conditional average treatment effects, corresponding to each of the values that <img src="https://latex.codecogs.com/png.latex?X"> can take, we’re finally ready to compute the ATE: <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Ctext%7BATE%7D%20&amp;=%20%5Cmathbb%7BE%7D%5B%5Ctext%7BCATE%7D(X)%5D%20%5C%5C%0A&amp;=%20%5Ctext%7BCATE%7D(0)%20%5Ctimes%20%5Cmathbb%7BP%7D(X%20=%200)%20+%20%5Ctext%7BCATE%7D(1)%20%5Ctimes%20%5Cmathbb%7BP%7D(X%20=%201)%20%5C%5C%0A&amp;=%20%5Cbeta_1%20%5Cleft%5B1%20-%20%5Cmathbb%7BP%7D(X%20=%201)%5Cright%5D%20+%20(%5Cbeta_1%20+%20%5Cbeta_3)%20%5Cmathbb%7BP%7D(X%20=%201)%20%5C%5C%0A&amp;=%20%5Cbeta_1%20+%20%5Cbeta_3%20p%0A%5Cend%7Baligned%7D%0A"> where we define the shorthand <img src="https://latex.codecogs.com/png.latex?p%20%5Cequiv%20%5Cmathbb%7BP%7D(X=1)">. So to compute the ATE, we need to know the coefficients <img src="https://latex.codecogs.com/png.latex?%5Cbeta_1"> and <img src="https://latex.codecogs.com/png.latex?%5Cbeta_3"> from the regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?D">, <img src="https://latex.codecogs.com/png.latex?X">, and <img src="https://latex.codecogs.com/png.latex?DX">, <em>in addition</em> to the share of people with <img src="https://latex.codecogs.com/png.latex?X%20=%201">. Needless to say, your favorite regression package will not spit out the ATE for you if you run the regression from above. And it <em>certainly</em> won’t spit out the standard error! So what can we do besides computing everything by hand?</p>
</section>
<section id="two-simple-alternatives" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="two-simple-alternatives"><span class="header-section-number">3</span> Two Simple Alternatives</h2>
<p>It turns out that there are two simple ways to get the your favorite software package to spit out the ATE for you and associated standard error. Each involves a slight <em>re-parameterization</em> of the conditional mean expression from above. The first one replaces <img src="https://latex.codecogs.com/png.latex?DX"> with <img src="https://latex.codecogs.com/png.latex?D%5Ctilde%7BX%7D"> where <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BX%7D%20%5Cequiv%20X%20-%20p"> and <img src="https://latex.codecogs.com/png.latex?p%20%5Cequiv%20%5Cmathbb%7BP%7D(X=1)">. To see why this works, notice that <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cmathbb%7BE%7D(Y%7CD,%20X)%20&amp;=%20%5Cbeta_0%20+%20%5Cbeta_1%20D%20+%20%5Cbeta_2%20X%20+%20%5Cbeta_3%20DX%20%5C%5C%0A&amp;=%20%5Cbeta_0%20+%20%5Cbeta_1%20D%20+%20%5Cbeta_2%20X%20+%20%5Cbeta_3%20D(X%20-%20p)%20+%20%5Cbeta_3%20pD%5C%5C%0A&amp;=%20%5Cbeta_0%20+%20(%5Cbeta_1%20+%20%5Cbeta_3%20p)%20D%20+%20%5Cbeta_2%20X%20+%20%5Cbeta_3%20D%5Ctilde%7BX%7D%5C%5C%0A&amp;=%20%5Cbeta_0%20+%20%5Ctext%7BATE%7D%5Ctimes%20D%20+%20%5Cbeta_2%20X%20+%20%5Cbeta_3%20D%5Ctilde%7BX%7D.%0A%5Cend%7Baligned%7D%0A"> This works perfectly well, but there’s something about it that offends my sense of order: why subtract the mean from <img src="https://latex.codecogs.com/png.latex?X"> in <em>one place but not in another</em>? If you share my aesthetic sensibilities, then you can feel free to replace that offending <img src="https://latex.codecogs.com/png.latex?X"> with another <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BX%7D"> since <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cmathbb%7BE%7D(Y%7CD,%20X)%20&amp;=%20%5Cbeta_0%20+%20%5Ctext%7BATE%7D%5Ctimes%20D%20+%20%5Cbeta_2%20X%20+%20%5Cbeta_3%20D%5Ctilde%7BX%7D%5C%5C%0A&amp;=%20%5Cbeta_0%20+%20%5Ctext%7BATE%7D%5Ctimes%20D%20+%20%5Cbeta_2%20(X-p)%20+%20p%20%5Cbeta_2%20+%20%5Cbeta_3%20D%5Ctilde%7BX%7D%5C%5C%0A&amp;=%20(%5Cbeta_0%20+%20p%20%5Cbeta_2)%20+%20%5Ctext%7BATE%7D%5Ctimes%20D%20+%20%5Cbeta_2%20%5Ctilde%7BX%7D%20+%20%5Cbeta_3%20D%5Ctilde%7BX%7D%5C%5C%0A&amp;=%20%5Ctilde%7B%5Cbeta%7D_0%20+%20%5Ctext%7BATE%7D%5Ctimes%20D%20+%20%5Cbeta_2%20%5Ctilde%7BX%7D%20+%20%5Cbeta_3%20D%5Ctilde%7BX%7D%0A%5Cend%7Baligned%7D%0A"> where we define <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7B%5Cbeta%7D_0%20%5Cequiv%20%5Cbeta_0%20+%20p%20%5Cbeta_2">. Notice that the only coefficient that changes is the intercept, and we’re typically not interested in this anyway!</p>
</section>
<section id="what-if-we-ignore-the-interaction" class="level2 page-columns page-full" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="what-if-we-ignore-the-interaction"><span class="header-section-number">4</span> What if we ignore the interaction?</h2>
<p>Wait a minute, you may be ready to object, when researchers claim to be “adjusting” or “controlling” for <img src="https://latex.codecogs.com/png.latex?X"> in practice, they very rarely include an interaction term between <img src="https://latex.codecogs.com/png.latex?D"> and <img src="https://latex.codecogs.com/png.latex?X"> in their regression! Instead, they just regress <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?D"> and <img src="https://latex.codecogs.com/png.latex?X">. What can we say about this approach? To answer this question, let’s continue with our example from above and define the following population linear regression model: <img src="https://latex.codecogs.com/png.latex?%0AY%20=%20%5Calpha_0%20+%20%5Calpha_1%20D%20+%20%5Calpha_2%20X%20+%20V%0A"> where <img src="https://latex.codecogs.com/png.latex?V"> is the population linear regression error term so that, <a href="https://www.econometrics.blog/post/why-econometrics-is-confusing-part-1-the-error-term/">by construction</a>, <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(V)%20=%20%5Cmathbb%7BE%7D(XV)%20=%200">. Notice that I’ve called the coefficients in this regression <img src="https://latex.codecogs.com/png.latex?%5Calpha"> rather than <img src="https://latex.codecogs.com/png.latex?%5Cbeta">. That’s because they will <em>not in general coincide</em> with the conditional mean function from above, namely <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y%7CD,%20X)%20=%20%5Cbeta_0%20+%20%5Cbeta_1%20D%20+%20%5Cbeta_2%20X%20+%20%5Cbeta_3%20DX">. In particular, the regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?D"> and <img src="https://latex.codecogs.com/png.latex?X"> without an interaction will <em>only</em> coincide with the true conditional mean function if <img src="https://latex.codecogs.com/png.latex?%5Cbeta_3%20=%200">.</p>
<p>So what, if anything, can we say about <img src="https://latex.codecogs.com/png.latex?%5Calpha_1"> in relation to the ATE? By <a href="https://en.wikipedia.org/wiki/Frisch%E2%80%93Waugh%E2%80%93Lovell_theorem">Yule’s Rule</a><sup>2</sup> we have <img src="https://latex.codecogs.com/png.latex?%0A%5Calpha_1%20=%20%5Cfrac%7B%5Ctext%7BCov%7D(Y,%20%5Ctilde%7BD%7D)%7D%7B%5Ctext%7BVar%7D(%5Ctilde%7BD%7D)%7D,%20%5Cquad%0AD%20=%20%5Cgamma_0%20+%20%5Cgamma_1%20X%20+%20%5Ctilde%7BD%7D,%20%5Cquad%20%5Cmathbb%7BE%7D(%5Ctilde%7BD%7D)%20=%20%5Cmathbb%7BE%7D(X%5Ctilde%7BD%7D)%20=%200%0A"> where <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BD%7D"> is the error term from a population linear regression of <img src="https://latex.codecogs.com/png.latex?D"> on <img src="https://latex.codecogs.com/png.latex?X">. In words, the way that a regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?D"> and <img src="https://latex.codecogs.com/png.latex?X"> “adjusts” for <img src="https://latex.codecogs.com/png.latex?X"> is by first regressing <img src="https://latex.codecogs.com/png.latex?D"> on <img src="https://latex.codecogs.com/png.latex?X">, taking the part of <img src="https://latex.codecogs.com/png.latex?D"> that is <em>not</em> correlated with <img src="https://latex.codecogs.com/png.latex?X">, namely <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BD%7D">, and regressing <img src="https://latex.codecogs.com/png.latex?Y"> on this alone.<sup>3</sup> As shown in the appendix to this post, <img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7B%5Ctext%7BCov%7D(Y,%5Ctilde%7BD%7D)%7D%7B%5Ctext%7BVar%7D(%5Ctilde%7BD%7D)%7D%20=%20%5Cfrac%7B%5Cmathbb%7BE%7D%5B%5Ctext%7BVar%7D(D%7CX)(%5Cbeta_1%20+%20%5Cbeta_3%20X)%5D%7D%7B%5Cmathbb%7BE%7D%5B%5Ctext%7BVar%7D(D%7CX)%5D%7D.%0A"> in this example. And since <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCATE%7D(X)%20=%20%5Cbeta_1%20+%20%5Cbeta_3%20X"> it follows that <img src="https://latex.codecogs.com/png.latex?%0A%5Calpha_1%20=%20%5Cfrac%7B%5Cmathbb%7BE%7D%5B%5Ctext%7BVar%7D(D%7CX)%20%5Ccdot%20%5Ctext%7BCATE%7D(X)%5D%7D%7B%5Cmathbb%7BE%7D%5B%5Ctext%7BVar%7D(D%7CX)%5D%7D.%0A"> The only thing that’s random in this expression is <img src="https://latex.codecogs.com/png.latex?X">. Both expectations involve averaging over its distribution. To make this clearer, define the <strong>propensity score</strong> <img src="https://latex.codecogs.com/png.latex?%5Cpi(x)%20%5Cequiv%20%5Cmathbb%7BP%7D(D=1%7CX=x)">. Using this notation, <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Ctext%7BVar%7D(D%7CX)%20&amp;=%20%5Cmathbb%7BE%7D(D%5E2%7CX)%20-%20%5Cmathbb%7BE%7D(D%7CX)%5E2%20=%20%5Cmathbb%7BE%7D(D%7CX)%20-%20%5Cmathbb%7BE%7D(D%7CX)%5E2%5C%5C%0A&amp;=%20%5Cpi(X)%20-%20%5Cpi(X)%5E2%20=%20%5Cpi(X)%5B1%20-%20%5Cpi(X)%5D%0A%5Cend%7Baligned%7D%0A"> since <img src="https://latex.codecogs.com/png.latex?D"> is binary. Defining <img src="https://latex.codecogs.com/png.latex?p(x)%20%5Cequiv%20%5Cmathbb%7BP%7D(X%20=%20x)">, we see that <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Calpha_1%20%20&amp;=%20%5Cfrac%7B%5Cmathbb%7BE%7D%5B%5Cpi(X)%5C%7B1%20-%20%5Cpi(X)%5C%7D%5Ccdot%20%5Ctext%7BCATE%7D(X)%5D%7D%7B%5Cmathbb%7BE%7D%5B%5Cpi(X)%5C%7B1%20-%20%5Cpi(X)%5C%7D%5D%7D%5C%5C%20%5C%5C%0A&amp;=%20%5Cfrac%7Bp(0)%20%5Ccdot%20%5Cpi(0)%5B1%20-%20%5Cpi(0)%5D%5Ccdot%20%5Ctext%7BCATE%7D(0)%20+%20p(1)%20%5Ccdot%20%5Cpi(1)%5B1%20-%20%5Cpi(1)%5D%5Ccdot%20%5Ctext%7BCATE%7D(1)%7D%7Bp(0)%20%5Ccdot%20%5Cpi(0)%5B1%20-%20%5Cpi(0)%5D%20+%20p(1)%20%5Ccdot%20%5Cpi(1)%5B1%20-%20%5Cpi(1)%5D%7D%5C%5C%20%5C%5C%0A&amp;=%20w_0%20%5Ccdot%20%5Ctext%7BCATE%7D(0)%20+%20w_1%20%5Ccdot%20%5Ctext%7BCATE%7D(1)%0A%5Cend%7Baligned%7D%0A"> where we introduce the shorthand <img src="https://latex.codecogs.com/png.latex?%0Aw(x)%20%5Cequiv%20%5Cfrac%7Bp(x)%20%5Ccdot%20%5Cpi(x)%5B1%20-%20%5Cpi(x)%5D%7D%7B%5Csum_%7B%5Ctext%7Ball%20%7D%20k%7D%20p(k)%20%5Ccdot%20%5Cpi(k)%5B1%20-%20%5Cpi(k)%5D%7D.%0A"> In other words, the coefficient on <img src="https://latex.codecogs.com/png.latex?D"> in a regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?D"> and <img src="https://latex.codecogs.com/png.latex?X"> excluding the interaction term <img src="https://latex.codecogs.com/png.latex?DX"> gives a <strong>weighted average</strong> of the conditional average treatment effects for the different values of <img src="https://latex.codecogs.com/png.latex?X">. The weights are between zero and one and sum to one. Because <img src="https://latex.codecogs.com/png.latex?w(x)"> is increasing in <img src="https://latex.codecogs.com/png.latex?p(x)">, values of <img src="https://latex.codecogs.com/png.latex?X"> that are <em>more common</em> are given more weight just as they are in the ATE. But since <img src="https://latex.codecogs.com/png.latex?w(x)"> is <em>also</em> increasing in <img src="https://latex.codecogs.com/png.latex?%5Cpi(x)%5B1%20-%20%5Cpi(x)%5D">, values of <img src="https://latex.codecogs.com/png.latex?X"> for which <img src="https://latex.codecogs.com/png.latex?%5Cpi(x)"> is closer to 0.5 are given more weight, <em>unlike</em> in the ATE. As such, we could describe <img src="https://latex.codecogs.com/png.latex?%5Calpha_1"> as a <em>variance-weighted average</em> of the conditional average treatment effects.</p>
<div class="no-row-height column-margin column-container"><div id="fn2"><p><sup>2</sup>&nbsp;Call it “Frisch-Waugh-Lovell” if you must, but I will continue trying to <a href="https://ideas.repec.org/p/arx/papers/2307.00369.html">make fetch happen</a>.</p></div><div id="fn3"><p><sup>3</sup>&nbsp;If you want the standard error of <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> and not just the point estimate, then replace <img src="https://latex.codecogs.com/png.latex?Y"> with the residual from a regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X">.</p></div></div><p>In general, the weighted average <img src="https://latex.codecogs.com/png.latex?%5Calpha_1"> will <em>not</em> coincide with the ATE, although there are two special cases where it will. The first case is when <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCATE%7D(X)"> does not depend on <img src="https://latex.codecogs.com/png.latex?X">, i.e.&nbsp;treatment effects are <em>homogeneous</em>. In this case <img src="https://latex.codecogs.com/png.latex?%5Cbeta_3%20=%200"> so there <em>is no interaction term in the conditional mean function</em>! The second is when <img src="https://latex.codecogs.com/png.latex?%5Cpi(X)"> does not depend on <img src="https://latex.codecogs.com/png.latex?X">, in which case the probability of treatment does not depend on <img src="https://latex.codecogs.com/png.latex?X">, so we don’t need to adjust for <img src="https://latex.codecogs.com/png.latex?X"> in the first place!</p>
</section>
<section id="what-about-the-general-case" class="level2 page-columns page-full" data-number="5">
<h2 data-number="5" class="anchored" data-anchor-id="what-about-the-general-case"><span class="header-section-number">5</span> What about the general case?</h2>
<p>All of the above derivations assumed that <img src="https://latex.codecogs.com/png.latex?X"> is one-dimensional and binary. So how much of this still applies more generally? First, if <img src="https://latex.codecogs.com/png.latex?X"> is a vector of binary variables representing categories like sex, race etc., everything goes through <em>exactly</em> as above.<sup>4</sup> All that changes is that <img src="https://latex.codecogs.com/png.latex?%5Cbeta_2">, <img src="https://latex.codecogs.com/png.latex?%5Cbeta_3"> and <img src="https://latex.codecogs.com/png.latex?p%20=%20%5Cmathbb%7BE%7D(X)"> become vectors. The coefficient on <img src="https://latex.codecogs.com/png.latex?D"> in a regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?D">, <img src="https://latex.codecogs.com/png.latex?X"> and the interaction <img src="https://latex.codecogs.com/png.latex?D%20%5Ctilde%7BX%7D"> is still the ATE, and the coefficient on <img src="https://latex.codecogs.com/png.latex?D"> in a regression that <em>excludes</em> the interaction term is still a weighted average of CATEs that does <em>not in general</em> equal the ATE.</p>
<div class="no-row-height column-margin column-container"><div id="fn4"><p><sup>4</sup>&nbsp;This is a nice homework exercise to test your understanding of the post!</p></div><div id="fn5"><p><sup>5</sup>&nbsp;If you have a very <em>large</em> number of categories things are still fine <em>in theory</em> but can break down in practice, since you’ll typically have very few observations in each “cell” corresponding to the different values of the categorical variables. But this is a topic for another day!</p></div></div><p>So whenever the covariates you need to adjust for are categorical, this post has you covered.<sup>5</sup> But what if some of our covariates are continuous? In this case things are a bit more complicated, but all of the results from above still go through if we’re willing to assume that the conditional mean functions <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y%7CD=0,%20X)">, <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y%7CD=1,X)"> and <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(D%7CX)"> are linear in <img src="https://latex.codecogs.com/png.latex?X">. This is undoubtedly a strong assumption, but not perhaps as strong as it seems. For example, <img src="https://latex.codecogs.com/png.latex?X"> could include logs, squares or other functions of some underlying continuous covariates, e.g.&nbsp;age or years of experience. In this case, the weighted average interpretation of the coefficient on <img src="https://latex.codecogs.com/png.latex?D"> in a regression that excludes the interaction term still holds but now involves an integral rather than a sum.</p>
</section>
<section id="does-it-really-work-an-empirical-example" class="level2 page-columns page-full" data-number="6">
<h2 data-number="6" class="anchored" data-anchor-id="does-it-really-work-an-empirical-example"><span class="header-section-number">6</span> Does it really work? An Empirical Example</h2>
<p>But perhaps you don’t trust my algebra.<sup>6</sup> To assuage your fears, let’s take this to the data! The following example is based on <a href="https://www.almendron.com/tribuna/wp-content/uploads/2018/04/electoral-effects-of-biased-media-russian-television-in-ukraine.pdf">Peisakhin &amp; Rozenas (2018) - Electoral Effects of Biased Media: Russian Television in Ukraine</a>. I’ve adapted it from Llaudet and Imai’s fantastic book <a href="https://press.princeton.edu/books/hardcover/9780691199429/data-analysis-for-social-science">Data Analysis for Social Science</a>, the perfect holiday or birthday gift for the budding social scientist in your life.</p>
<div class="no-row-height column-margin column-container"><div id="fn6"><p><sup>6</sup>&nbsp;I certainly don’t!</p></div></div><p>Here’s a bit of background. In the lead-up to Ukraine’s 2014 parliamentary election, Russian state-controlled TV mounted a fierce media campaign against the Ukrainian government. Ukrainians who lived near the border with Russia could <em>potentially</em> receive Russian TV signals. Did receiving these signals <em>cause</em> them to support pro-Russia parties in the election? To answer this question, we’ll use a dataset called <code>precincts</code> that contains aggregate election results in precincts close to the Russian border:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2">precincts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'https://ditraglia.com/data/UA_precincts.csv'</span>)</span></code></pre></div></div>
</div>
<p>Each row of <code>precincts</code> is an electoral precinct in Ukraine that is near the Russian border. The columns <code>pro_russian</code> and <code>prior_pro_russian</code> give the vote share (in percentage points) of pro-Russian parties in the 2014 and 2012 Ukrainian elections, respectively. Our outcome of interest will be the <em>change</em> in pro-Russian vote share between the two elections, so we first need to construct this:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">precincts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> precincts <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">change =</span> pro_russian <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> prior_pro_russian) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb2-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>pro_russian, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>prior_pro_russian)</span>
<span id="cb2-4">precincts</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3,589 × 3
   russian_tv within_25km change
        &lt;dbl&gt;       &lt;dbl&gt;  &lt;dbl&gt;
 1          0           1  -22.4
 2          0           0  -34.5
 3          1           1  -18.8
 4          0           1  -12.2
 5          0           0  -27.7
 6          1           0  -44.2
 7          0           0  -34.5
 8          0           0  -29.5
 9          0           0  -24.1
10          0           0  -25.4
# ℹ 3,579 more rows</code></pre>
</div>
</div>
<p>The column <code>russian_tv</code> equals <code>1</code> if the precinct has Russian TV reception. This is our treatment variable: <img src="https://latex.codecogs.com/png.latex?D">. But crucially, this is <em>not</em> randomly assigned. While it’s true that there is some natural variation in signal strength that is plausibly independent of other factors related to voting behavior, on average <em>precincts closer to Russia</em> are more likely to receive a signal. So suppose for the sake of argument that <em>conditional</em> on proximity to the Russian border, <code>russian_tv</code> is as good as randomly assigned. This is the <em>selection on observables</em> assumption. There’s no way to check this using our data alone. It’s something we need to justify based on our understanding of the world and the substantive problem at hand.</p>
<p>As our measure of proximity, we’ll use the dummy variable <code>within_25km</code> which equals <code>1</code> if the precinct is within 25km of the Russian border. This our <img src="https://latex.codecogs.com/png.latex?X">-variable. The <em>overlap</em> assumption requires that there are some precincts with Russian TV reception and some without in each distance category. This is an assumption that we <em>can</em> check using the data, so let’s do so before proceeding:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">precincts <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb4-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(within_25km) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">share with Russian tv</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(russian_tv))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 2 × 2
  within_25km `share with Russian tv`
        &lt;dbl&gt;                   &lt;dbl&gt;
1           0                   0.105
2           1                   0.692</code></pre>
</div>
</div>
<p>We see that just over 10% of that are <em>not</em> within 25km of the border have Russian TV reception while just under 70% of those within 25km have reception, so overlap is satisfied in this example. Neither of these values is close to 0% or 100%, so this dataset comfortably satisfies the overlap assumption.</p>
<p>To avoid taxing your memory about which variable is which, for the rest of this exercise, I’ll create a new dataset that renames the columns of <code>precincts</code> to <code>D</code>, <code>X</code>, and <code>Y</code> for the treatment, covariate, and outcome, respectively.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">dat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> precincts <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">D =</span> russian_tv, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">X =</span> within_25km, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Y =</span> change)</span></code></pre></div></div>
</div>
<section id="computing-the-ate-the-hard-way" class="level3">
<h3 class="anchored" data-anchor-id="computing-the-ate-the-hard-way">Computing the ATE the Hard Way</h3>
<p>Now we’re ready to verify the calculations from above. First we’ll compute the ATE “the hard way”, in other words by computing each of the CATEs separately and averaging them. Warning: there’s a fair bit of <code>dplyr</code> to come!</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 1: compute the mean Y for each combination of (D, X)</span></span>
<span id="cb7-2">means <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb7-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(D, X) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb7-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Ybar =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(Y))</span>
<span id="cb7-5">means <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># display the results</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 3
# Groups:   D [2]
      D     X  Ybar
  &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
1     0     0 -24.6
2     0     1 -34.2
3     1     0 -13.0
4     1     1 -32.2</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 2: reshape so the means of Y|D=0,X and Y|D=1,X are in separate cols</span></span>
<span id="cb9-2">means <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> means <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb9-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_wider</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_from =</span> D, </span>
<span id="cb9-4">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_from =</span> Ybar, </span>
<span id="cb9-5">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_prefix =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Ybar'</span>)</span>
<span id="cb9-6">means <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># display the results</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 2 × 3
      X Ybar0 Ybar1
  &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
1     0 -24.6 -13.0
2     1 -34.2 -32.2</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 3: attach a column with the proportion of X = 0 and X = 1</span></span>
<span id="cb11-2">regression_adjustment <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb11-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(X) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb11-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb11-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p =</span> count <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(count)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb11-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>count) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb11-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(means, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb11-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CATE =</span> Ybar1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> Ybar0) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># compute the CATEs</span></span>
<span id="cb11-9">regression_adjustment <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># display the results</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 2 × 5
      X     p Ybar0 Ybar1  CATE
  &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
1     0 0.849 -24.6 -13.0 11.6 
2     1 0.151 -34.2 -32.2  2.01</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 4: at long last, compute the ATE!</span></span>
<span id="cb13-2">ATE <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> regression_adjustment <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb13-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">out =</span> (Ybar1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> Ybar0) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> p) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb13-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(out) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb13-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>()</span>
<span id="cb13-6">ATE</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 10.12062</code></pre>
</div>
</div>
</section>
<section id="computing-the-ate-the-easy-way" class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="computing-the-ate-the-easy-way">Computing the ATE the Easy Way</h3>
<p>And now the easy way, using the two regressions described above<sup>7</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn7"><p><sup>7</sup>&nbsp;If you’re rusty on R’s formula syntax, you may find my <a href="https://www.econometrics.blog/post/the-r-formula-cheatsheet/">cheat sheet</a> helpful.</p></div></div><div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Construct Xtilde = X - mean(X) </span></span>
<span id="cb15-2">dat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb15-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Xtilde =</span> X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(X))</span>
<span id="cb15-4"></span>
<span id="cb15-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Regression of Y on D, X, and D*Xtilde</span></span>
<span id="cb15-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> D <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> D<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>Xtilde, dat)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
lm(formula = Y ~ D + X + D:Xtilde, data = dat)

Coefficients:
(Intercept)            D            X     D:Xtilde  
    -24.591       10.121       -9.604       -9.562  </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Regression of Y on D, Xtilde, and D*Xtilde</span></span>
<span id="cb17-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> D <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Xtilde, dat)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
lm(formula = Y ~ D * Xtilde, data = dat)

Coefficients:
(Intercept)            D       Xtilde     D:Xtilde  
    -26.045       10.121       -9.604       -9.562  </code></pre>
</div>
</div>
<p>Everything works as it should! The coefficient on <code>D</code> in each regression equals the ATE we computed by hand, namely 10.121, and the two regression agree with each other with the exception of the intercept.</p>
</section>
<section id="standard-errors" class="level3">
<h3 class="anchored" data-anchor-id="standard-errors">Standard Errors</h3>
<p>The nice thing about computing the ATE by running a regression rather than computing it “by hand” is that we can easily obtain valid standard errors, confidence intervals, and p-values if desired. For example, if you wanted “robust” standard errors for the ATE, you could simply use <code>lm_robust()</code> from the <code>estimatr</code> package as follows</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(estimatr)</span>
<span id="cb19-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(broom)</span>
<span id="cb19-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm_robust</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> D <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Xtilde, dat) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb19-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tidy</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb19-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(term <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'D'</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb19-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>df, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>outcome)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  term estimate std.error statistic      p.value conf.low conf.high
1    D 10.12062 0.4838613  20.91636 9.315921e-92 9.171946  11.06929</code></pre>
</div>
</div>
<p>Getting these “by hand” would have been much more work!</p>
<p>There is one subtle point that I should mention. I’ve heard it said on numerous occasions that the above standard error calculation is “not quite right” since we <em>estimated</em> the mean of <code>X</code> and used it to re-center <code>X</code> in the regression. Surely we should account for the sampling variability in <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D"> around its mean, the argument goes.</p>
<p>Perhaps I’m about to get blacklisted by the Econometrician’s alliance for saying this, but I’m not convinced. The usual way of thinking about inference for regression is <em>conditional</em> on the regressors, in this case <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?D">. Viewed from this perspective, <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D"> <em>isn’t random</em>. Now, of course, if you prefer to see the world through finite-population design-based lenses, <img src="https://latex.codecogs.com/png.latex?D"> is <em>definitely</em> random. But in this case it’s the <em>only</em> thing that’s random. The design-based view situates randomness exclusively in the <em>treatment assignment mechanism</em>. Under this view, since the units in our dataset are not considered as having been drawn from a hypothetical super-population, any summary statistic of their covariates <img src="https://latex.codecogs.com/png.latex?X"> is <em>fixed</em>. So again, <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D"> isn’t random and doesn’t contribute any uncertainty.</p>
<p><strong>Update</strong>: I initially concluded this section with “as far as I can see, it’s perfectly reasonable to use the sample mean of <img src="https://latex.codecogs.com/png.latex?X"> to re-center <img src="https://latex.codecogs.com/png.latex?X"> in the regression” but <a href="https://twitter.com/Apoorva__Lal">apoorva.lal</a> pointed out that this elides an important distinction. The key is that whether <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D"> is random or not depends on the question you’re interested in. If you want inference for the ATE <em>computed using the population values of</em> <img src="https://latex.codecogs.com/png.latex?X">, then <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D"> is random and you should account for its variability. But if you’re interested in the ATE computed using <em>the observed values</em> of <img src="https://latex.codecogs.com/png.latex?X"> in the sample, then <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D"> is fixed and you shouldn’t:</p>
<blockquote class="twitter-tweet blockquote">
<p lang="en" dir="ltr">
Point about whether Xbar is random depends on whether you're interested in SATE v PATE right? In any case, it is surprisingly easy to propagate that uncertainty forward with (what else?) GMM (earlier posts in the thread discuss the recentering point)<a href="https://t.co/3GXfTeF9DW">https://t.co/3GXfTeF9DW</a>
</p>
— apoorva.lal (<span class="citation" data-cites="Apoorva__Lal">@Apoorva__Lal</span>) <a href="https://twitter.com/Apoorva__Lal/status/1819397448852545620?ref_src=twsrc%5Etfw">August 2, 2024</a>
</blockquote>
<script async="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
<p>This agrees with my logic about conditioning on <img src="https://latex.codecogs.com/png.latex?X"> and the design-based perspective, but it’s a much clearer way of making the relevant distinction so thanks for pointing it out!</p>
</section>
<section id="excluding-the-interaction" class="level3">
<h3 class="anchored" data-anchor-id="excluding-the-interaction">Excluding the Interaction</h3>
<p>Finally, we’ll verify the derivations from above for <img src="https://latex.codecogs.com/png.latex?%5Calpha_1"> in the regression that <em>excludes</em> an interaction term. First we’ll compute the “variance weighted average” of CATEs by hand and check that it does not agree with the ATE:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute the propensity score pi(X)</span></span>
<span id="cb21-2">pscore <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb21-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(X) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb21-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pi =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(D))</span>
<span id="cb21-5"></span>
<span id="cb21-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute the weights w </span></span>
<span id="cb21-7">regression_adjustment <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(regression_adjustment, pscore, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb21-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">w =</span> p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> pi) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> pi))) </span>
<span id="cb21-9"></span>
<span id="cb21-10">regression_adjustment <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># display the results</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 2 × 7
      X     p Ybar0 Ybar1  CATE    pi     w
  &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
1     0 0.849 -24.6 -13.0 11.6  0.105 0.713
2     1 0.151 -34.2 -32.2  2.01 0.692 0.287</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute the variance weighted average of the CATEs</span></span>
<span id="cb23-2">wCATE <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> regression_adjustment <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb23-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">wCATE =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(w <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> CATE)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb23-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(wCATE)</span>
<span id="cb23-5"></span>
<span id="cb23-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">wCATE =</span> wCATE, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ATE =</span> ATE)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>    wCATE       ATE 
 8.822285 10.120617 </code></pre>
</div>
</div>
<p>Finally, we’ll compare this hand calculation to the results of a regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?D"> and <img src="https://latex.codecogs.com/png.latex?X"> without an interaction:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> D <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> X, dat)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
lm(formula = Y ~ D + X, data = dat)

Coefficients:
(Intercept)            D            X  
    -24.302        8.822      -14.614  </code></pre>
</div>
</div>
<p>As promised, the coefficient on <img src="https://latex.codecogs.com/png.latex?D"> equals the variance-weighted average of CATEs that we computed by hand, namely 8.822, which does not equal the ATE, 10.121. Here the CATE for <img src="https://latex.codecogs.com/png.latex?X=1"> receives <em>more weight</em> when the interaction term is omitted, pulling the coefficient on <img src="https://latex.codecogs.com/png.latex?D"> away from the ATE and towards the (smaller) CATE for <img src="https://latex.codecogs.com/png.latex?X=1">.</p>
</section>
</section>
<section id="conclusion" class="level2" data-number="7">
<h2 data-number="7" class="anchored" data-anchor-id="conclusion"><span class="header-section-number">7</span> Conclusion</h2>
<p>I hope this post has convinced you that regression adjustment isn’t simply a matter of tossing a collection of covariates into your regression! In general, the coefficient on <img src="https://latex.codecogs.com/png.latex?D"> in a regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?D"> will <em>not</em> equal the ATE of <img src="https://latex.codecogs.com/png.latex?D">. Instead it will be a weighted average of CATEs. To obtain the ATE we need to include an <em>interaction</em> between <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?D">. The simplest way to get your favorite statistical software package to calculate this for you, along with an appropriate standard error, is by <em>de-meaning</em> <img src="https://latex.codecogs.com/png.latex?X"> before including the interaction. And don’t forget that causal inference <em>always requires untestable assumptions</em>, in this case the selection-on-observables assumption. While implementation details are important, getting them right won’t make any difference if you’re not adjusting for the right covariates in the first place.</p>
</section>
<section id="appendix-the-missing-algebra" class="level2 page-columns page-full" data-number="8">
<h2 data-number="8" class="anchored" data-anchor-id="appendix-the-missing-algebra"><span class="header-section-number">8</span> Appendix: The Missing Algebra</h2>
<p>This section provides the algebra needed to justify the expression for <img src="https://latex.codecogs.com/png.latex?%5Calpha_1"> from a regression that omits the interaction between <img src="https://latex.codecogs.com/png.latex?D"> and <img src="https://latex.codecogs.com/png.latex?X">. In particular, we will show that <img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7B%5Ctext%7BCov%7D(Y,%5Ctilde%7BD%7D)%7D%7B%5Ctext%7BVar%7D(%5Ctilde%7BD%7D)%7D%20=%20%5Cfrac%7B%5Cmathbb%7BE%7D%5B%5Ctext%7BVar%7D(D%7CX)(%5Cbeta_1%20+%20%5Cbeta_3%20X)%5D%7D%7B%5Cmathbb%7BE%7D%5B%5Ctext%7BVar%7D(D%7CX)%5D%7D.%0A"> where <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BD%7D"> is the error term from a population linear regression of <img src="https://latex.codecogs.com/png.latex?D"> on <img src="https://latex.codecogs.com/png.latex?X">, namely <img src="https://latex.codecogs.com/png.latex?D%20=%20%5Cgamma_0%20+%20%5Cgamma_1%20X%20+%20%5Ctilde%7BD%7D"> so that <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(%5Ctilde%7BD%7D)%20=%20%5Cmathbb%7BE%7D(X%5Ctilde%7BD%7D)%20=%200"> by construction. The proof isn’t too difficult, but it’s a bit tedious so I thought you might prefer to skip it on a first reading. Still here? Great! Let’s dive into the algebra.</p>
<p>We need to calculate <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(Y,%20%5Ctilde%7BD%7D)"> and <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVar%7D(%5Ctilde%7BD%7D)">. A nice way to carry out this calculation is by applying the <a href="https://en.wikipedia.org/wiki/Law_of_total_covariance">law of total covariance</a>. You may have heard of the law of total variance, but in my view the law of total covariance is more useful. Just as you can deduce all the properties of variance from the properties of covariance, using <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(W,%20W)%20=%20%5Ctext%7BVar%7D(W)">, you can deduce the law of total variance from the law of covariance! In the present example, the law of total covariance allows us to write <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BCov%7D(Y,%20%5Ctilde%7BD%7D)%20=%20%5Cmathbb%7BE%7D%5B%5Ctext%7BCov%7D(Y,%20%5Ctilde%7BD%7D%7CX)%5D%20+%20%5Ctext%7BCov%7D%5B%5Cmathbb%7BE%7D(Y%7CX),%20%5Cmathbb%7BE%7D(%5Ctilde%7BD%7D%7CX)%5D.%0A"> If this looks intimidating, don’t worry: we’ll break it down piece by piece. The second term on the RHS is a covariance between two random variables: <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y%7CX)"> and <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(%5Ctilde%7BD%7D,X)">.<sup>8</sup> We already have an equation for <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BD%7D">, namely the population linear regression of <img src="https://latex.codecogs.com/png.latex?D"> on <img src="https://latex.codecogs.com/png.latex?X">, so let’s use it to simplify <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(%5Ctilde%7BD%7D%7CX)">: <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BE%7D(%5Ctilde%7BD%7D%7CX)%20=%20%5Cmathbb%7BE%7D(D%20-%20%5Cgamma_0%20-%20%5Cgamma_1%20X%7CX)%20=%20%5Cmathbb%7BE%7D(D%7CX)%20-%20%5Cgamma_0%20-%20%5Cgamma_1%20X.%0A"> Here’s the key thing to note: since <img src="https://latex.codecogs.com/png.latex?D"> is binary, the population linear regression of <img src="https://latex.codecogs.com/png.latex?D"> on <img src="https://latex.codecogs.com/png.latex?X"> is <em>identical</em> to the conditional mean of <img src="https://latex.codecogs.com/png.latex?D"> given <img src="https://latex.codecogs.com/png.latex?X">.<sup>9</sup> This tells us that <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(%5Ctilde%7BD%7D%7CX)=0">. Since the covariance of anything with a constant is zero, the second term on the RHS of the law of total covariance drops out, leaving us with <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BCov%7D(Y,%20%5Ctilde%7BD%7D)%20=%20%5Cmathbb%7BE%7D%5B%5Ctext%7BCov%7D(Y,%20%5Ctilde%7BD%7D%7CX)%5D%20=%20%5Cmathbb%7BE%7D%5B%5Ctext%7BCov%7D(Y,%20D%20-%20%5Cgamma_0%20-%20%5Cgamma_1%20X%20%7C%20X)%5D.%0A"> Now let’s deal with the conditional covariance inside the expectation. Remember that conditioning on <img src="https://latex.codecogs.com/png.latex?X"> is equivalent to saying “suppose that <img src="https://latex.codecogs.com/png.latex?X"> were known”. Anything that’s known is constant, not random. So we can treat <em>both</em> <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?%5Cdelta"> as constants and apply the usual rules for covariance to obtain <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BCov%7D(Y,%20D%20-%20%5Cgamma_0%20-%20%5Cgamma_1%20X%20%7C%20X)%20=%20%5Ctext%7BCov%7D(Y,%20D%7CX).%0A"> Therefore, <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(Y,%20%5Ctilde%7BD%7D)%20=%20%5Cmathbb%7BE%7D%5B%5Ctext%7BCov%7D(Y,%20D%7CX)%5D">. A very similar calculation using the <a href="https://en.wikipedia.org/wiki/Law_of_total_variance">law of total variance</a> gives <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Ctext%7BVar%7D(%5Ctilde%7BD%7D)%20&amp;=%20%5Cmathbb%7BE%7D%5B%5Ctext%7BVar%7D(%5Ctilde%7BD%7D%7CX)%5D%20+%20%5Ctext%7BVar%7D%5B%5Cmathbb%7BE%7D(%5Ctilde%7BD%7D%7CX)%5D%20=%5Cmathbb%7BE%7D%5B%5Ctext%7BVar%7D(%5Ctilde%7BD%7D%7CX)%5D%5C%5C%0A&amp;=%20%5Cmathbb%7BE%7D%5B%5Ctext%7BVar%7D(D%20-%20%5Cgamma_0%20-%20%5Cgamma_1%20X%7C%20X)%5D%5C%5C%0A&amp;=%20%5Cmathbb%7BE%7D%5B%5Ctext%7BVar%7D(D%7CX)%5D%0A%5Cend%7Baligned%7D%0A"> since <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(%5Ctilde%7BD%7D%7CX)%20=%200"> and the variance of any constant is simply zero. So, with the help of the laws of total covariance and variance, we’ve established that<br>
<img src="https://latex.codecogs.com/png.latex?%0A%5Calpha_1%20%5Cequiv%20%5Cfrac%7B%5Ctext%7BCov%7D(Y,%20%5Ctilde%7BD%7D)%7D%7B%5Ctext%7BVar%7D(%5Ctilde%7BD%7D)%7D=%20%5Cfrac%7B%5Cmathbb%7BE%7D%5B%5Ctext%7BCov%7D(Y,%20D%7CX)%5D%7D%7B%5Cmathbb%7BE%7D%5B%5Ctext%7BVar%7D(D%7CX)%5D%7D%0A"> in this example. Note that this does <em>not</em> hold in general: it relies on the fact that <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(%5Ctilde%7BD%7D%7CX)=0">, which holds in our example because <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(D%7CX)%20=%20%5Cgamma_0%20+%20%5Cgamma_1%20X"> given that <img src="https://latex.codecogs.com/png.latex?X"> is binary.</p>
<div class="no-row-height column-margin column-container"><div id="fn8"><p><sup>8</sup>&nbsp;An unconditional expectation like <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y)"> is a constant: it’s a probability-weighted average of all possible realizations of <img src="https://latex.codecogs.com/png.latex?Y">. In contrast, a conditional expectation like <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y%7CX)"> is a random variable: it’s our “best guess” of <img src="https://latex.codecogs.com/png.latex?Y"> based on observing <img src="https://latex.codecogs.com/png.latex?X">, where “best” means “minimum mean-squared error”. See <a href="https://youtu.be/CbsZHNQX54s?si=MN80w00yj1W5yDmX">this video</a> for some more details on conditional expectation.</p></div><div id="fn9"><p><sup>9</sup>&nbsp;In general, a population linear regression gives the best linear approximation of the conditional mean, but when the conditional mean is in fact linear, the two coincide. The reason these coincide in our example is that we can write <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5BD%7CX%5D%20=%20X%20%5Cmathbb%7BE%7D(D%7CX=1)%20+%20(1%20-%20X)%20%5Cmathbb%7BE%7D(D%7CX=0)">. There are only two values that <img src="https://latex.codecogs.com/png.latex?X"> can take, and we are simply “picking out” the average value of <img src="https://latex.codecogs.com/png.latex?D"> in each case. But we can re-arrange this to take precisely the form <img src="https://latex.codecogs.com/png.latex?%5Cdelta%20+%20%5Ckappa%20X"> defining <img src="https://latex.codecogs.com/png.latex?%5Cdelta%20=%20%5Cmathbb%7BE%7D(D%7CX=0)"> and <img src="https://latex.codecogs.com/png.latex?%5Ckappa%20=%20%5Cmathbb%7BE%7D(D%7CX=1)%20-%20%5Cmathbb%7BE%7D(D%7CX=0)">.</p></div></div><p>We’re very nearly finished. All that remains is to simplify the numerator. To do this, we’ll use the equality <img src="https://latex.codecogs.com/png.latex?%0AY%20=%20%5Cbeta_0%20+%20%5Cbeta_1%20D%20+%20%5Cbeta_2%20X%20+%20%5Cbeta_3%20DX%20+%20U%0A"> where <img src="https://latex.codecogs.com/png.latex?U%20%5Cequiv%20Y%20-%20%5Cmathbb%7BE%7D(Y%7CD,%20X)"> satisfies <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(U%7CD,X)%20=%200"> <a href="https://www.econometrics.blog/post/why-econometrics-is-confusing-part-1-the-error-term/">by construction</a>. This allows us to write <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Ctext%7BCov%7D(Y,%20D%7CX)%20&amp;=%20%5Ctext%7BCov%7D(%5Cbeta_0%20+%20%5Cbeta_1%20D%20+%20%5Cbeta_2%20X%20+%20%5Cbeta_3%20DX%20+%20U,%20D%7CX)%5C%5C%0A&amp;=%20%5Cbeta_1%20%5Ctext%7BCov%7D(D,%20D%7CX)%20+%20%5Cbeta_3%20%5Ctext%7BCov%7D(DX,%20D%7CX)%20+%20%5Ctext%7BCov%7D(U,D%7CX)%5C%5C%0A&amp;=%20%5Cbeta_1%20%5Ctext%7BVar%7D(D%7CX)%20+%20%5Cbeta_3%20X%20%5Ccdot%20%5Ctext%7BVar%7D(D%7CX)%20+%20%5Ctext%7BCov%7D(U,D%7CX)%5C%5C%0A&amp;=%20%5Ctext%7BVar%7D(D%7CX)(%5Cbeta_1%20+%20%5Cbeta_3%20X)%20+%20%5Ctext%7BCov%7D(U,%20D%7C%20X).%0A%5Cend%7Baligned%7D%0A"> So what about that pesky <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(U,D%7CX)"> term? By the law of iterated expectations this turns out to equal zero, since <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Ctext%7BCov%7D(U,D%7CX)%20&amp;=%20%5Cmathbb%7BE%7D(DU%7CX)%20-%20%5Cmathbb%7BE%7D(D%7CX)%20%5Cmathbb%7BE%7D(U%7CX)%5C%5C%0A&amp;=%20%5Cmathbb%7BE%7D_%7BD%7CX%7D%5BD%5Cmathbb%7BE%7D(U%7CD,X)%5D%20-%20%5Cmathbb%7BE%7D(D%7CX)%20%5Cmathbb%7BE%7D_%7BD%7CX%7D%5B%5Cmathbb%7BE%7D(U%7CD,X)%5D%0A%5Cend%7Baligned%7D%0A"> and, again, <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(U%7CD,X)%20=%200"> <a href="https://www.econometrics.blog/post/why-econometrics-is-confusing-part-1-the-error-term/">by construction</a>. So we’re left with <img src="https://latex.codecogs.com/png.latex?%0A%5Calpha_1%20=%20%5Cfrac%7B%5Cmathbb%7BE%7D%5B%5Ctext%7BCov%7D(Y,%20D%7CX)%5D%7D%7B%5Cmathbb%7BE%7D%5B%5Ctext%7BVar%7D(D%7CX)%5D%7D%20=%20%5Cfrac%7B%5Cmathbb%7BE%7D%5B%5Ctext%7BVar%7D(D%7CX)(%5Cbeta_1%20+%20%5Cbeta_3%20X)%5D%7D%7B%5Cmathbb%7BE%7D%5B%5Ctext%7BVar%7D(D%7CX)%5D%7D.%0A"></p>


</section>


 ]]></description>
  <category>causal inference</category>
  <guid>https://www.econometrics.blog/post/how-to-do-regression-adjustment/</guid>
  <pubDate>Fri, 02 Aug 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Econometrics Puzzler #0: Is it Better to Improve Sensitivity or Specificity?</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/is-it-better-to-improve-sensitivity-or-specificity/</link>
  <description><![CDATA[ 




<p>Here’s a slightly unusual exercise on the topic of Bayes’ Theorem for those of you teaching or studying introductory probability.<sup>1</sup> Imagine that you’re developing a diagnostic test for a disease. The test is very simple: it either comes back positive or negative. You have a choice between slightly increasing either your test’s <a href="https://en.wikipedia.org/wiki/Sensitivity_and_specificity">sensitivity or its specificity</a>. If your goal is to maximize the <a href="https://en.wikipedia.org/wiki/Positive_and_negative_predictive_values">positive predictive value (PPV)</a> of your test, i.e.&nbsp;the probability that a patient has the disease given that the test comes back positive, which test characteristic should you choose to improve?</p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;<em>What follows started life as a regular post, but I later realized it was a puzzler in disguise! To keep the numbering of the other puzzlers unchanged, I have retroactively dubbed this one “puzzler zero”.</em></p></div></div><section id="odds-arent-so-odd" class="level2 page-columns page-full" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="odds-arent-so-odd"><span class="header-section-number">1</span> Odds aren’t so odd!</h2>
<p>While I give you a few minutes to pause and ponder this question, here’s a brief rant on the topic of odds. If you’re anything like me, the first time you encountered odds, you thought to yourself</p>
<div class="page-columns page-full"><blockquote class="blockquote">
<p>What is this $*@%^!? Why would anyone want to spoil a perfectly good probability by dividing it by one minus itself?“<sup>2</sup></p>
</blockquote><div class="no-row-height column-margin column-container"><div id="fn2"><p><sup>2</sup>&nbsp;I know first-hand that this sentiment is shared by at least one distinguished professor of probability theory, so at least I’m not completely alone in my earlier view of things!</p></div></div></div>
<p>But it’s time to take the red pill and see the world as it really is: <strong>the only reason you prefer to think in terms of probabilities rather than odds is because you’ve been brainwashed by the educational system</strong>. Of course I exaggerate slightly, but the point is that odds are just as natural as probabilities; we’re just not as accustomed to working with them. In many situations in probability, statistics, and econometrics, it turns out that working with odds (or their logarithm) makes life <em>much</em> simpler, as I will try to convince you with a simple example.</p>
<p>First we need to define odds. Consider some event <img src="https://latex.codecogs.com/png.latex?A"> with probability <img src="https://latex.codecogs.com/png.latex?p"> of occurring. Then we say that the <strong>odds</strong> of <img src="https://latex.codecogs.com/png.latex?A"> are <img src="https://latex.codecogs.com/png.latex?p/(1%20-%20p)">. For example, if <img src="https://latex.codecogs.com/png.latex?p%20=%201/3"> then the event <img src="https://latex.codecogs.com/png.latex?A"> is equivalent to drawing a red ball from an urn that contains one red and two blue balls: the probability gives the <em>ratio of red balls to total balls</em>. The odds of <img src="https://latex.codecogs.com/png.latex?A">, on the other hand, equal <img src="https://latex.codecogs.com/png.latex?1/2">: odds give the <em>ratio of red balls to blue balls</em>. Since probabilities are between 0 and 1, odds are between 0 and <img src="https://latex.codecogs.com/png.latex?%5Cinfty">. Odds of 0 mean that the event is impossible, while odds of <img src="https://latex.codecogs.com/png.latex?%5Cinfty"> mean that the event is certain. Odds of 1 mean that the event is just as likely to occur as not to occur.</p>
<p>Now here’s an example that you’ve surely seen before:</p>
<blockquote class="blockquote">
<p>One in a hundred women has breast cancer <img src="https://latex.codecogs.com/png.latex?(B)">. If you have breast cancer, there is a 95% chance that you will test positive <img src="https://latex.codecogs.com/png.latex?(+)">; if you do not have breast cancer <img src="https://latex.codecogs.com/png.latex?(B%5EC)">, there is a 2% chance that you will nonetheless test positive <img src="https://latex.codecogs.com/png.latex?(+)">. We know nothing about Alice other than the fact that she tested positive. How likely is it that she has breast cancer?</p>
</blockquote>
<p>It’s easy enough to solve this problem using Bayes’ Theorem, as long as you have pen and paper handy: <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0AP(B%20%7C%20+)%20&amp;=%20%5Cfrac%7BP(+%7CB)P(B)%7D%7BP(+)%7D%20=%20%5Cfrac%7BP(+%7CB)P(B)%7D%7BP(+%7CB)P(B)%20+%20P(+%7CB%5EC)P(B%5EC)%7D%5C%5C%0A&amp;=%20%5Cfrac%7B0.95%20%5Ctimes%200.01%7D%7B0.95%20%5Ctimes%200.01%20+%200.02%20%5Ctimes%200.99%7D%20%5Capprox%200.32.%0A%5Cend%7Baligned%7D%0A"> But what if I asked you how the result would change if only one in a thousand women had breast cancer? What if I changed the <a href="https://en.wikipedia.org/wiki/Sensitivity_and_specificity">sensitivity</a> of the test from 95% to 99% or the <a href="https://en.wikipedia.org/wiki/Sensitivity_and_specificity">specificity</a> from 98% to 95%? If you’re anything like me, you would struggle to do these calculations in your head. That’s because <img src="https://latex.codecogs.com/png.latex?P(B%7C+)"> is a <em>highly non-linear</em> function of <img src="https://latex.codecogs.com/png.latex?P(B)">, <img src="https://latex.codecogs.com/png.latex?P(+%7CB)">, and <img src="https://latex.codecogs.com/png.latex?P(+%7CB%5EC)">.</p>
<p>In contrast, working with odds makes this problem a snap. The key point is that <img src="https://latex.codecogs.com/png.latex?P(B%7C+)"> and <img src="https://latex.codecogs.com/png.latex?P(B%5EC%7C+)"> have the same denominator, namely <img src="https://latex.codecogs.com/png.latex?P(+)">: <img src="https://latex.codecogs.com/png.latex?%0AP(B%20%7C%20+)%20=%20%5Cfrac%7BP(+%7CB)P(B)%7D%7BP(+)%7D,%20%5Cquad%0AP(B%5EC%20%7C%20+)%20=%20%5Cfrac%7BP(+%7CB%5EC)P(B%5EC)%7D%7BP(+)%7D%0A"> Notice that <img src="https://latex.codecogs.com/png.latex?P(+)"> was the “complicated” term in <img src="https://latex.codecogs.com/png.latex?P(B%7C+)">; the numerator was simple. Since the odds of <img src="https://latex.codecogs.com/png.latex?B"> given <img src="https://latex.codecogs.com/png.latex?(+)"> is defined as the ratio of <img src="https://latex.codecogs.com/png.latex?P(B%7C+)"> to <img src="https://latex.codecogs.com/png.latex?P(B%5EC%7C+)">, the denominator cancels and we’re left with <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BOdds%7D(B%7C+)%20%5Cequiv%20%5Cfrac%7BP(B%7C+)%7D%7BP(B%5EC%7C+)%7D%20=%20%5Cfrac%7BP(+%7CB)%7D%7BP(+%7CB%5EC)%7D%20%5Ctimes%20%5Cfrac%7BP(B)%7D%7BP(B%5EC)%7D.%0A"> In other words, the <em>posterior odds</em> of <img src="https://latex.codecogs.com/png.latex?B"> equal the <em>likelihood ratio</em>, <img src="https://latex.codecogs.com/png.latex?P(+%7CB)/P(+%7CB%5EC)">, multiplied by the <em>prior odds</em> of <img src="https://latex.codecogs.com/png.latex?B">, <img src="https://latex.codecogs.com/png.latex?P(B)/P(B%5EC)">: <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BPosterior%20Odds%7D%20=%20%5Ctext%7B(Likelihood%20Ratio)%7D%20%5Ctimes%20%5Ctext%7B(Prior%20Odds)%7D.%0A"> Now we can easily solve the original problem in our head. The prior odds are 1/99 while the likelihood ratio is 95/2. Rounding these to 0.01 and 50 respectively, we find that the posterior odds are around 1/2. This means that Alice’s chance of having breast cancer is roughly equivalent to the chance of drawing a red ball from an urn with one red and two blue balls. There’s no need to convert this back to a probability since we can already answer the question: it’s considerably more likely that Alice <em>does not</em> have breast cancer. But if you insist, odds of 1/2 give a probability of 1/3, so in spite of rounding and calculating in our heads we’re within 0.3% of the exact answer!</p>
<p>Repeat after me: <strong>odds are on a multiplicative scale</strong>. This is their key virtue and the reason why they make it so easy to explore variations on the original problem. If one in a thousand women has breast cancer, the prior odds become 1/999 so we simply divide our previous result by 10, giving posterior odds of around 1/20. If we instead changed the sensitivity from 95% to 99% and the specificity from 98% to 95%, then the likelihood ratio would change from <img src="https://latex.codecogs.com/png.latex?95/2%20%5Capprox%2050"> to <img src="https://latex.codecogs.com/png.latex?99/5%20%5Capprox%2020">.</p>
</section>
<section id="solution" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="solution"><span class="header-section-number">2</span> Solution</h2>
<div class="solution callout callout-style-simple callout-note no-icon callout-titled" title="Solution">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Solution
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Have I given you enough time to come up with your own solution? Fantastic! In case you hadn’t already guessed, that little digression about odds served an important purpose: my solution will use odds rather than probabilities. Our goal is to increase the positive predictive value (PPV) of the test, namely <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BPPV%7D%20%5Cequiv%20P(%5Ctext%7BHas%20Disease%7D%7C%5Ctext%7BTest%20Positive%7D),%0A"> by as much as possible, either by improving the test’s sensitivity <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BSensitivity%7D%20%5Cequiv%20P(%5Ctext%7BTest%20Positive%7D%20%7C%20%5Ctext%7BHas%20Disease%7D)%0A"> or its specificity <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BSpecificity%7D%20%5Cequiv%20P(%5Ctext%7BTest%20Negative%7D%20%7C%20%5Ctext%7BDoesn't%20Have%20Disease%7D).%0A"> To answer this question, we’ll start by substituting these definitions into the odds form of Bayes’ Theorem introduced above, yielding <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BPosterior%20Odds%7D%20=%20%5Cfrac%7B%5Ctext%7BPPV%7D%7D%7B1%20-%20%5Ctext%7BPPV%7D%7D%20=%20%5Cfrac%7B%5Ctext%7BSensitivity%7D%7D%7B1%20-%20%5Ctext%7BSpecificity%7D%7D%20%5Ctimes%20%5Ctext%7BPrior%20Odds%7D.%0A"> This expression makes it clear that increasing either the sensitivity or specificity of the test increases the posterior odds. And because the PPV is a <em>strictly increasing</em> function of the posterior odds, namely <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BPPV%7D%20=%20%5Cfrac%7B%5Ctext%7BPosterior%20Odds%7D%7D%7B1%20+%20%5Ctext%7BPosterior%20Odds%7D%7D,%0A"> this also increases the PPV. So now the question is: which of these two possibilities gives us the most bang for our buck? A natural idea would be to compare the <em>marginal effect</em> of increasing sensitivity by a small amount to the marginal effect of increasing specificity by the same amount. We can do this by comparing the <em>partial derivatives</em> of the PPV with respect to sensitivity and specificity. But, again, the PPV is an <em>increasing</em> function of the posterior odds, so we can simplify our task by comparing the <em>derivatives</em> of the posterior odds with respect to sensitivity and specificity. By the chain rule, any claim about the relative magnitudes of these derivatives computed for the odds will also hold for the PPV.</p>
<p>But why stop with the odds? We can simplify our task even further by comparing the <em>derivatives</em> of the <em>logarithm</em> of the posterior odds with respect to sensitivity and specificity. This is because the logarithm is, again, an <em>increasing transformation</em> of the odds. Since <img src="https://latex.codecogs.com/png.latex?%0A%5Clog(%5Ctext%7BPosterior%20Odds%7D)%20=%20%5Clog(%5Ctext%7BSensitivity%7D)%20-%20%5Clog(1%20-%20%5Ctext%7BSpecificity%7D)%20%20+%20%5Clog(%5Ctext%7BPrior%20Odds%7D).%0A"> our required derivatives are <img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7B%5Cpartial%20%5Clog(%5Ctext%7BPosterior%20Odds%7D)%7D%7B%5Cpartial%20%5Ctext%7BSensitivity%7D%7D%20=%20%5Cfrac%7B1%7D%7B%5Ctext%7BSensitivity%7D%7D%20%5Cquad%20%5Ctext%7Band%7D%20%5Cquad%20%5Cfrac%7B%5Cpartial%20%5Clog(%5Ctext%7BPosterior%20Odds%7D)%7D%7B%5Cpartial%20%5Ctext%7BSpecificity%7D%7D%20=%20%5Cfrac%7B1%7D%7B1%20-%20%5Ctext%7BSpecificity%7D%7D.%0A"> Now for the punchline: the ratio of the derivative with respect to specificity divided by that with respect to sensitivity is <img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7B%5Cpartial%20%5Clog(%5Ctext%7BPosterior%20Odds%7D)/%5Cpartial%20%5Ctext%7BSpecificity%7D%7D%7B%5Cpartial%20%5Clog(%5Ctext%7BPosterior%20Odds%7D)/%5Cpartial%20%5Ctext%7BSensitivity%7D%7D%20=%20%5Cfrac%7B1/(1%20-%20%5Ctext%7BSpecificity%7D)%7D%7B1/%5Ctext%7BSensitivity%7D%7D%20=%20%5Cfrac%7B%5Ctext%7BSensitivity%7D%7D%7B1%20-%20%5Ctext%7BSpecificity%7D%7D%0A"> and this is <em>precisely the likelihood ratio</em> from the odds form of Bayes’ Theorem! Hence, <strong>whenever the likelihood ratio is greater than one we’d prefer to increase the test’s specificity; whenever it’s less than one we’d prefer to increase the sensitivity.</strong> If the likelihood ratio is equal to one, then it doesn’t matter which we choose.</p>
<p>Case closed, right? Well not quite. We can say a bit more by thinking about what it <em>means</em> for the likelihood ratio to be greater than or less than one. Examining the odds form of Bayes’ Theorem from above, we see that a likelihood ratio less than one means that our posterior probability that a person is sick <em>falls</em> when she tests positive. In other words, this corresponds to a test that is <em>worse than useless</em>: it’s actually <em>misleading</em>. In contrast, a likelihood ratio greater than one means that the test is <em>informative</em>: a positive test result increases our belief that the person is sick. Any real-world diagnostic test will have a likelihood ratio greater than one. Indeed, if we <em>had</em> such an actively mis-leading test, we could easily convert it into an informative one by simply reversing the test’s outcome: if someone tests positive, we tell them they’re negative, and vice versa. This reversal would result in a likelihood ratio greater than one. Therefore, in all cases–whether we start with an informative test or reverse a misleading one–<strong>we should prefer to increase the test’s specificity</strong>.</p>
<section id="epilogue" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="epilogue"><span class="header-section-number">3</span> Epilogue</h2>
<p>Of course, this exercise is predicated upon the assumption that we want to maximize the PPV and that we can freely adjust both the test’s sensitivity and its specificity. In practice, one or more of these assumptions might not hold. Indeed, PPV is not the be all and end all of diagnostic testing. A full accounting would need to consider the relative costs of false positives and false negatives along with the prevalence of the disease. Still, I hope this exercise gives you a flavor of the power of odds for simplifying complex problems in probability and statistics.</p>
</section>
</div>
</div>
</div>


</section>


 ]]></description>
  <category>statistics</category>
  <category>teaching</category>
  <category>puzzler</category>
  <guid>https://www.econometrics.blog/post/is-it-better-to-improve-sensitivity-or-specificity/</guid>
  <pubDate>Thu, 25 Jul 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>How to Read an Econometrics Paper</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/how-to-read-an-econometrics-paper/</link>
  <description><![CDATA[ 




<p>Reading and understanding econometrics papers can be hard work. Most published articles, even review articles, are written by specialists for specialists. Unless you’re already familiar with the literature, it can be a real uphill battle to make it through a recent paper. In grad school I remember our professors repeatedly admonishing me and the rest of the cohort to “read the papers!” But when I did my best to follow this advice, I nearly always felt like I was banging my head against a wall.</p>
<!--Giving a helpful presentation about an econometrics paper is even harder. You don't only need to understand the paper itself, you also need to understand the *needs of your audience*.-->
<p>Effective reading is a skill that can be learned, and the only way to learn is through practice. But you can learn the easy way or the hard way. The hard way is to keep trying and hope for the best; the easy way is to adjust your approach based on the experiences of others. With that in mind, this post offers some tips and tricks that I’ve picked up through the years for reading technical material efficiently and effectively. My target audience is PhD students in Economics, especially students in the Econometrics Reading Group at Oxford, but I hope that some of the following tips will be helpful for others as well.</p>
<p>If you have any tips of your own, or if you violently agree or disagree with any of mine, I hope to hear from you in the comments section below!</p>
<section id="read-something-else-instead" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="read-something-else-instead"><span class="header-section-number">1</span> Read Something Else Instead</h2>
<p>The first question to ask yourself is whether you should even be reading this paper in the first place. Just because White’s (1980) paper on heteroskedasticity-robust standard errors is a “classic” in econometrics, that doesn’t mean that you should read it. In fact, as a graduate student just starting out, you probably shouldn’t! The paper that introduces a new idea or procedure is rarely the paper that gives the clearest explanation. Reading a good textbook explanation is a much more effective way to get to grips with a new idea. You might, for example, try reading the relevant chapters in White’s textbook <em>Asymptotic Theory for Econometricians</em> instead.</p>
<p>But sometimes you have to read a particular paper. Maybe it’s the paper you’ve been assigned to present in a reading group, or maybe it’s highly relevant to your own research. In that case you may still want to <em>start</em> by reading something else. For example, there might be a more recent paper or review article that gives a good summary of the idea or method in question. Reading this paper first can make it much easier for you to tackle the original paper.</p>
<p>So to all those professors out there who keep telling their students to “read the papers!” I say: “read the papers, but only after you’ve read something else first!”</p>
<section id="dont-assume-you-have-to-understand-the-whole-thing" class="level3">
<h3 class="anchored" data-anchor-id="dont-assume-you-have-to-understand-the-whole-thing">Don’t Assume You Have to Understand the Whole Thing</h3>
<p>As a general rule you should <em>not expect to understand everything</em> when you read a paper. You may only get 10% on the first read, but that’s fine! Besides papers I’ve written myself, there are relatively few articles that I’ve checked line-by-line from start to finish. Even if you’ve been assigned to <em>present</em> a paper that doesn’t mean that you need to understand every detail of every lemma in the online technical appendix. Instead your goal should be to understand the <em>key ideas</em> and contributions of the paper. Like anything in life, there are diminishing returns to effort in reading a paper. When reading papers to support your own research, you can be even more selective. The key question becomes: “how is this relevant for what <em>I’m doing</em>?” It may be that you only need to understand a small part of the paper to get what you need.</p>
</section>
<section id="dont-assume-youre-stupid" class="level3">
<h3 class="anchored" data-anchor-id="dont-assume-youre-stupid">Don’t Assume You’re Stupid</h3>
<p>If you’re confused, don’t assume that it’s your fault. Notice your confusion and try to get to the bottom of it without taking things for granted or engaging in negative self-talk. The only way to learn is by getting confused and then unconfusing yourself!</p>
<p>You may be confused because the authors assume you know something that you don’t. They are likely experts in the field who have spent years thinking about this particular question. You, on the other hand, are just starting out. As you gain a bit more context, things may fall rapidly into place. (See my next tip below.)</p>
<p>You may be confused because the paper is confusingly written. Writing is hard, and technical writing is especially hard. The referee process can even make papers <em>more confusing</em>, since our <a href="https://www.sqare.org">present system for evaluating research</a> involves multiple rounds of revisions in which the authors must try to satisfy referees with differing views. The result is that published papers often contain a substantial element of “cruft” that distracts from the main message.</p>
<p>You may even be confused because the paper is wrong! As a good Bayesian, you shouldn’t immediately jump to the conclusion that you, a newcomer to this field, have stumbled upon a crucial error that everyone else has missed. On the other hand, you <em>definitely</em> shouldn’t believe everything that you see in print! All papers are wrong in some way, and some papers are wrong in serious and important ways. If you’re confused, it’s worth considering whether the authors were confused too!</p>
</section>
<section id="spread-yourself-thin" class="level3">
<h3 class="anchored" data-anchor-id="spread-yourself-thin">Spread Yourself Thin</h3>
<p>Let’s say you really need to get to grips with paper X on topic Y. You’ve read the relevant textbook material, you’ve tried a review article, and you’re still struggling. What now? Strange though it may sound, one helpful answer is to read <em>more papers</em> on topic Y in an extremely shallow way. Skim the abstracts, introductions, and conclusions. Note any terms or concepts that keep appearing, especially ones that you don’t understand.</p>
<p>I can think of many occasions when I skimmed nine papers and didn’t understand any of them, but then read a tenth and suddenly everything clicked. The key here is <em>context</em>. When you’re new to topic Y, there will be lots of little things that you’ve never thought of before but that the literature takes for granted. Since most papers are written for specialists by specialists, crucial details are often left out or glossed over as if they were obvious. Just as fish don’t realize that they’re in water, specialists often fail to realize that they’re taking a lot of things for granted. The reason that reading <em>many</em> papers can help is that different specialists will leave out <em>different</em> details. The key that you need to understand paper X might be a seemingly throwaway comment in paper Z!</p>
</section>
<section id="explain-it-to-someone-else" class="level3">
<h3 class="anchored" data-anchor-id="explain-it-to-someone-else">Explain It to Someone Else</h3>
<p>The best way to understand something is by trying to explain it to someone else. This holds true even when the “someone else” in question is just a figment of your imagination. As you read, start by trying to explain the paper to yourself <em>in your own words</em>. I find it helpful to write in the margins of the paper as I go, summarizing the key ideas with less jargon and simpler terminology and notation. When you’re confused about something, try to put your confusion into words; make it concrete and write it down.</p>
<p>Talking to a real person can be even more helpful. If you’re in a reading group, try discussing the paper informally with one your peers who has also read it. You may be surprised at how much two people, neither of whom understands something on their own, can learn from each other. In this brave new world of LLMs like Claude and GPT-4o, you could even try uploading your paper and discussing it with an AI. You <em>cannot</em> assume that the AI will necessarily give you reliable information about the paper, but just like a peer who only partially understands it, an AI can be a useful sounding board for your own ideas and confusions. Noticing mistakes in the AI’s understanding, pointing them out and continuing the conversation can also be a great way to clarify your own thinking.</p>
</section>
<section id="head-straight-for-the-simulation-empirical-example" class="level3">
<h3 class="anchored" data-anchor-id="head-straight-for-the-simulation-empirical-example">Head Straight for the Simulation / Empirical Example</h3>
<p>Ideally every paper would have a fantastic introduction that makes it clear what the paper is about and why it’s important. In real life, introductions can be hit-or-miss. So after reading the introduction, you might consider heading straight for the simulation study and/or empirical example. Most econometrics papers propose a method that solves a particular problem. What is the problem, and why does the particular data generating process (DGP) in the simulation (or the real data in the empirical example) exhibit it? What parameters of the simulation DGP control the extent of the problem? What is the “old” method on which the paper improves? This is likely to be something familiar such as a “textbook” method. How exactly is the new method <em>implemented</em>? In other words, how exactly is it <em>computed</em> from real or simulated data? Try to write down all the steps in the implementation in a sufficiently precise way that you could code it yourself.</p>
<p>Once you know how to answer these questions you’re in a much better position to understand the rest of the paper. As you read through the assumptions and theorems, refer back to the simulation study. Why does the DGP satisfy the assumptions? Can you think of a different DGP in which the assumptions fail? Is there anything “fishy” about the simulation example? Does it seem like the authors have cooked the books in some way, e.g.&nbsp;by introducing a very “mild” version of the central problem, or something else that would be unrealistic in practice? Answering these questions will help you to <em>evaluate</em> the paper, understand its limitations and possibly think about how to improve upon it.</p>
</section>
<section id="make-things-simpler" class="level3">
<h3 class="anchored" data-anchor-id="make-things-simpler">Make Things Simpler</h3>
<p>Many econometrics papers present results at an extremely high level of generality. On the one hand this is a good thing. Much of the power of mathematics comes from abstraction and general results are more widely-applicable. But from an expositional standpoint, this is <em>terrible</em>. The history of mathematics is a history of solutions to concrete problems that were progressively generalized and expanded over time. The history of ideas mirrors the way that the average person learns most effectively: by starting with concrete examples and then generalizing.</p>
<p>With this in mind, try to simplify the theorems and examples in the paper. Getting rid of covariates often cuts down on both algebra and notation, so start with this. Try re-writing the assumptions and theorems in this simpler notation. Are some of the assumptions confusing? Try strengthening them or try to see if you can find a concrete example in which they hold, possibly taken from the simulation DGP.</p>
</section>
<section id="dont-get-hung-up-on-technicalities" class="level3">
<h3 class="anchored" data-anchor-id="dont-get-hung-up-on-technicalities">Don’t Get Hung Up on Technicalities</h3>
<p>Some parts of a paper are “core material” and some parts are “technicalities”. Keeping these separate in your mind will make it much easier to understand a paper. One helpful approach is to make a dependency tree of the assumptions, lemmas, and theorems <em>before trying to understand them.</em> Once you see how things fit together you may notice, for example, that the only role of Proposition 3 is to establish that an appropriate Central Limit Theorem holds and the only role of Assumptions 2-6 is to prove Proposition 3. Fantastic! In this case, just assume the conclusion of Proposition 3 and move on to see where this is needed in the <em>core</em> results. Even when you’re reading assumptions, lemmas, propositions, theorems, and proofs, you should be aiming to get the “big picture” rather than to assimilate every tiny detail.</p>
</section>
<section id="be-appropriately-skeptical-of-asymptotics" class="level3">
<h3 class="anchored" data-anchor-id="be-appropriately-skeptical-of-asymptotics">Be Appropriately Skeptical of Asymptotics</h3>
<p>Asymptotics are a crucial tool in econometrics but remember that it is <em>finite sample</em> properties that we actually care about. The “asymptotic distribution” of an estimator is just a thought experiment, not something you can take to the bank. An asymptotic argument is a kind of approximation that in effect supposes that certain things are “negligible.” This approximation could be fantastic or it could be terrible. It’s only through simulation studies that we can really know which is the case. Or, to quote <a href="https://doi.org/10.1017/CBO9780511802256">van der Vaart (1998)</a>,</p>
<blockquote class="blockquote">
<p>strictly speaking, most asymptotic results that are currently available are logically useless. This is because most asymptotic results are limit results, rather than approximations consisting of an approximating formula plus an accurate error bound … This is why there is good asymptotics and bad asymptotics and why two types of asymptotics sometimes lead to conflicting claims … Because it may be theoretically very hard to ascertain that approximation errors are small, one often takes recourse to simulation studies</p>
</blockquote>
<p>For an example of “good” versus “bad” asymptotics applied to power analysis, see <a href="https://www.econometrics.blog/post/local-asymptotics-the-simplest-possible-example/">this post</a>.</p>
<!--
- Assume that your readers haven't read it and don't know anything about the related literature. You will almost certainly be right!
- That said, don't try to *substitute* for reading the paper. Your goal is instead to make it *easier* for someone to read the paper later on and to communicate why the paper is interesting / important / bad / wrong. By all means try to summarize it, but this summary should *not not not* comprehensive and technical.
- Aim lower than you think you should. You will never aim too low, I promise.
- It may help to spend time on background. E.g. a paper that relies on principal components (diffusion index forecasting): present a quick review / overview of PCA to make sure everyone is on the same page.
- Don't use notation you haven't introduced: put things in a natural order.
- Your talk does *not* necessarily have to follow the same organizational plan as the paper. Indeed it *probably should not*. 
- This is surpsingly difficult: curse of knowledge. You understand it and maybe you can't remember a time when you *didn't* understand it. But try! 
- Use pictures / simple examples / simulations to communicate key ideas. It's usually a bad idea to try to prove a theorem. It may even be a bad idea to state a theorem fully rigorously: this usually takes a lot of notation and set-up. The audience has maybe 30-45 minutes to absorb what you have to say and can only hold 7 things in working memory at once. Every additional piece of notation that you introduce is a tax on their attention.
- This is hard and it takes practice. But guess what: if you become an academic this is something that you will spend the rest of your career doing! Teaching and explaining your research to others! Start practicing now, get feedback, and improve. Nothing worth doing is easy.
-->


</section>
</section>

 ]]></description>
  <category>teaching</category>
  <guid>https://www.econometrics.blog/post/how-to-read-an-econometrics-paper/</guid>
  <pubDate>Sat, 20 Jul 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Sims and Uhlig (1991) Replication</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/sims-and-uhlig-1991-replication/</link>
  <description><![CDATA[ 




<p>As a teaser for our upcoming (2024-07-23) virtual reading group session on Bayesian macro / time series econometrics, this post replicates a classic paper by <a href="https://ideas.repec.org/a/ecm/emetrp/v59y1991i6p1591-99.html">Sims &amp; Uhlig (1991)</a> contrasting Bayesian and Frequentist inferences for a unit root. In the post I’ll focus on explaining and implementing the authors’ simulation design. In the reading group session (and possibly a future post) we’ll talk more about the paper’s implications for the Bayesian-Frequentist debate and relate it to more recent work by <a href="https://ideas.repec.org/a/taf/jnlasa/v111y2016i515p1233-1241.html">Mueller &amp; Norets (2016)</a>. We’ll also be joined by special guest <a href="https://web.sas.upenn.edu/schorf/">Frank Schorfheide</a> who will help guide us through the recent literature on Bayesian approaches to VARs, including <a href="https://ideas.repec.org/a/tpr/restat/v97y2015i2p436-451.html">Giannone et al (2015)</a> and <a href="https://ideas.repec.org/a/taf/jnlasa/v114y2019i526p565-580.html">(2019)</a>. If you’re an Oxford student or staff member, you can sign up for the reading group <a href="https://edstem.org/us/join/6j2hay">here</a>. Otherwise, send me an email and I’ll add you manually.</p>
<section id="a-simple-example" class="level2 page-columns page-full" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="a-simple-example"><span class="header-section-number">1</span> A Simple Example</h2>
<p>To set the stage for Sims &amp; Uhlig (1991), consider the following simple example: <img src="https://latex.codecogs.com/png.latex?X_1,%20X_2,%20%5Cdots,%20X_%7B100%7D%20%5Csim%20%5Ctext%7BNormal%7D(%5Cmu,%20%5Csigma%5E2)"> where <img src="https://latex.codecogs.com/png.latex?%5Cmu"> is unknown but <img src="https://latex.codecogs.com/png.latex?%5Csigma"> is known to equal <img src="https://latex.codecogs.com/png.latex?1">. Let <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D%20=%20%5Cfrac%7B1%7D%7B100%7D%20%5Csum_%7Bi=1%7D%5E%7B100%7D%20X_i"> be the sample mean. Then <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D%20%5Cpm%200.2"> is an approximate 95% Frequentist confidence interval for <img src="https://latex.codecogs.com/png.latex?%5Cmu">. In words: among 95% of the possible datasets that we could potentially observe, the interval <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D%20%5Cpm%200.2"> will cover the true, unknown value of <img src="https://latex.codecogs.com/png.latex?%5Cmu">; in the remaining <img src="https://latex.codecogs.com/png.latex?5%5C%25"> of datasets, the interval will not cover <img src="https://latex.codecogs.com/png.latex?%5Cmu">.</p>
<p>The Frequentist interval conditions on <img src="https://latex.codecogs.com/png.latex?%5Cmu"> and treats <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D"> as random. In contrast, a Bayesian credible interval conditions on <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D"> and treats <img src="https://latex.codecogs.com/png.latex?%5Cmu"> as random. This doesn’t require us to believe that <img src="https://latex.codecogs.com/png.latex?%5Cmu"> is “really” random. Bayesian reasoning simply uses the language of probability to express uncertainty about <em>any quantity that we cannot observe</em>. Let <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bx%7D"> be the observed value of <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D">. Under a vague prior for <img src="https://latex.codecogs.com/png.latex?%5Cmu">, e.g.&nbsp;a Normal(0, 100) distribution, the 95% Bayesian <a href="https://en.wikipedia.org/wiki/Credible_interval">highest posterior density interval</a> for <img src="https://latex.codecogs.com/png.latex?%5Cmu"> is approximately <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bx%7D%20%5Cpm%200.2">. In words: given that we have observed <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D%20=%20%5Cbar%7Bx%7D">, there is a 95% probability that <img src="https://latex.codecogs.com/png.latex?%5Cmu"> lies in the interval <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bx%7D%20%5Cpm%200.2">.</p>
<p>The comforting thing about this example is that, regardless of whether we choose a Bayesian or Frequentist perspective, our inference remains the same: compute the sample mean, then add and subtract <img src="https://latex.codecogs.com/png.latex?0.2">. This means that the Frequentist interval inherits all the nice properties of Bayesian inferences, and the Bayesian interval has correct Frequentist coverage. This equivalence between Bayesian and Frequentist methods crops up in many simple examples, especially in situations where the sample size is large. But in more complex settings, the two approaches can give radically different answers. And to head off a common mis-understanding, this <em>isn’t</em> because Bayesians use priors. In the limit as we accumulate more and more data, the influence of the prior wanes. The key difference is that Bayesian inference adheres to the <a href="https://en.wikipedia.org/wiki/Likelihood_principle">likelihood principle</a>, whereas common Frequentist methods do not.<sup>1</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;A detailed discussion of the likelihood principle would require at least a whole post of its own. If you want to learn more, I highly recommend the classic monograph by <a href="https://external.dandelon.com/download/attachments/dandelon/ids/DE004496C87987070706BC125794B00403A1A.pdf">Berger &amp; Wolpert</a>.</p></div></div></section>
<section id="a-not-so-simple-example" class="level2 page-columns page-full" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="a-not-so-simple-example"><span class="header-section-number">2</span> A Not-so-simple Example</h2>
<p>Sims &amp; Uhlig consider the AR(1) model <img src="https://latex.codecogs.com/png.latex?%0Ay_t%20=%20%5Crho%20y_%7Bt-1%7D%20+%20%5Cvarepsilon_t,%20%5Cquad%20%5Cvarepsilon_t%20%5Csim%20%5Ctext%7Biid%20Normal%7D(0,%201)%0A"> and the conditional maximum likelihood estimator given the initial <img src="https://latex.codecogs.com/png.latex?y_0">, namely <img src="https://latex.codecogs.com/png.latex?%0A%5Cwidehat%7B%5Crho%7D%20=%20%5Cfrac%7B%5Csum_%7Bt=1%7D%5ET%20y_%7Bt-1%7D%20y_t%7D%7B%5Csum_%7Bt=1%7D%5ET%20y_%7Bt-1%7D%5E2%7D.%0A"> Their simulation contrasts the Frequentist sampling distribution of <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7B%5Crho%7D%7C%5Crho"> with the Bayesian posterior distribution of <img src="https://latex.codecogs.com/png.latex?%5Crho%7C%5Cwidehat%7B%5Crho%7D"> under a flat prior on <img src="https://latex.codecogs.com/png.latex?%5Crho">. When <img src="https://latex.codecogs.com/png.latex?%5Crho"> is near one, these two distributions differ markedly: while the Bayesian posterior is always symmetric and centered at <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7B%5Crho%7D">, the Frequentist sampling distribution is highly skewed when <img src="https://latex.codecogs.com/png.latex?%5Crho"> is close to one. This shows that the Bayesian-Frequentist equivalence we found in our simple population mean example from above breaks down completely in this more complex example.</p>
<p>Sims &amp; Uhlig argue that the Bayesian posterior provides a much more sensible and useful characterization of the information contained in the data and after reading the paper, I’m inclined to agree. My replication code follows below, along with plots of the joint distribution of <img src="https://latex.codecogs.com/png.latex?(%5Crho,%20%5Cwidehat%7B%5Crho%7D)"> under a uniform prior for <img src="https://latex.codecogs.com/png.latex?%5Crho"> and the conditional distributions <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7B%5Crho%7D%7C%5Crho=1"> (Frequentist Sampling Distribution) and <img src="https://latex.codecogs.com/png.latex?%5Crho%7C%5Cwidehat%7B%5Crho%7D%20=%201"> (Bayesian Posterior).<sup>2</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn2"><p><sup>2</sup>&nbsp;For further discussion of Sims and Uhlig’s illuminating simulation experiment, see Chapter 6 of <a href="https://mitpress.mit.edu/9780262660945/intermediate-statistics-and-econometrics/">Poirier</a>.</p></div></div></section>
<section id="the-replication" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="the-replication"><span class="header-section-number">3</span> The Replication</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#-------------------------------------------------------------------------------</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sims, C. A., &amp; Uhlig, H. (1991). Understanding unit rooters: A helicopter tour</span></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#</span></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># (See also: Example 6.10.6 from Poirier "Intermediate Statistics and 'Metrics")</span></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#-------------------------------------------------------------------------------</span></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># In the next section we will proceed to construct, by Monte Carlo, an estimated</span></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># joint pdf for \rho and \hat{\rho} under a uniform prior pdf on \rho. We choose</span></span>
<span id="cb1-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 31 values of \rho, from 0.8 to 1.1 at intervals of 0.01. We draw 10000 100 x 1</span></span>
<span id="cb1-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># iid N(0,1) vectors of random variables to use as realizations of \epsilon. For</span></span>
<span id="cb1-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># each of the 10000 \epsilon vectors and each of the 31 \rho values, we</span></span>
<span id="cb1-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># construct a y vector with y(0) = 0, y(t) generated by equation (1).</span></span>
<span id="cb1-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#</span></span>
<span id="cb1-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Equation (1): y(t) = \rho y(t-1) + \epsilon(t), t = 0, ..., T</span></span>
<span id="cb1-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#</span></span>
<span id="cb1-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For each of these y vectors, we construct \hat{\rho}. Using as bins the</span></span>
<span id="cb1-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># intervals [-\infty, 0.795), [0.795, 0.805), [0.805, 0.815), etc. we construct</span></span>
<span id="cb1-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># a histogram that estimates the pdf of \hat{rho} for each fixed \rho value.</span></span>
<span id="cb1-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># When these histograms are lined up side by side, they form a surface that is</span></span>
<span id="cb1-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the joint pdf for \rho and \hat{\rho} under a flat prior on \rho.</span></span>
<span id="cb1-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#-------------------------------------------------------------------------------</span></span>
<span id="cb1-21"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1693</span>)</span>
<span id="cb1-22"></span>
<span id="cb1-23"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-24"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tictoc)</span>
<span id="cb1-25"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(patchwork)</span>
<span id="cb1-26"></span>
<span id="cb1-27">draw_rho_hat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(rho) {</span>
<span id="cb1-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Carry out the simulation once for a fixed value of rho; return rho_hat</span></span>
<span id="cb1-29">  nT <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb1-30">  y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, nT <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-31">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (t <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(nT <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) {</span>
<span id="cb1-32">    y[t] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rho <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> y[t <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-33">  }</span>
<span id="cb1-34">  y_t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> y[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb1-35">  y_tminus1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> y[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(y)]</span>
<span id="cb1-36">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(y_t <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> y_tminus1) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(y_tminus1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb1-37">}</span>
<span id="cb1-38"></span>
<span id="cb1-39"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Function to run the simulation for a fixed value of rho (10000 times)</span></span>
<span id="cb1-40">run_sim <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> \(rho) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dbl</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e4</span>, \(i) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">draw_rho_hat</span>(rho))</span>
<span id="cb1-41"></span>
<span id="cb1-42"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tic</span>()</span>
<span id="cb1-43">foo <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">run_sim</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>)</span>
<span id="cb1-44"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toc</span>() <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ~0.6 seconds on my machine</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>0.38 sec elapsed</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Full sequence of rho values from Sims &amp; Uhlig (1991)</span></span>
<span id="cb3-2">rho <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">to =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>)</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tic</span>()</span>
<span id="cb3-5">results <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho =</span> rho,</span>
<span id="cb3-6">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho_hat =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(rho, run_sim)) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># List columns</span></span>
<span id="cb3-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toc</span>() <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ~17 seconds on my machine (1991 was a long time ago!)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>11.574 sec elapsed</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The results tibble uses a list column for rho_hat. This is convenient for</span></span>
<span id="cb5-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># making histograms of the frequentist sampling distribution (rho fixed) but</span></span>
<span id="cb5-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># not for making histograms of the Bayesian posterior (rho_hat) fixed. For the</span></span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># latter, we will use the unnest() function to "expand" the list column rho_hat</span></span>
<span id="cb5-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># into a regular column. This is the "joint" distribution of rho and rho_hat.</span></span>
<span id="cb5-6">joint <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> results <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest</span>(rho_hat)</span>
<span id="cb5-8"></span>
<span id="cb5-9">joint <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> rho, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> rho_hat)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density2d_filled</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Restrict rho_hat axis</span></span>
<span id="cb5-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Joint Distribution"</span>,</span>
<span id="cb5-14">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(rho),</span>
<span id="cb5-15">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hat</span>(rho)))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/sims-and-uhlig-1991-replication/index_files/figure-html/unnamed-chunk-2-1.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">joint <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(rho_hat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.995</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> rho_hat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.005</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> rho)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binwidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"skyblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hat</span>(rho) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb6-6">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(rho),</span>
<span id="cb6-7">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Frequency"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/sims-and-uhlig-1991-replication/index_files/figure-html/unnamed-chunk-2-2.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">joint <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(rho <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> rho_hat)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binwidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"skyblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(rho <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb7-6">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hat</span>(rho)),</span>
<span id="cb7-7">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Frequency"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/sims-and-uhlig-1991-replication/index_files/figure-html/unnamed-chunk-2-3.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Function that makes the preceding two plots, puts them side-by-side and lets</span></span>
<span id="cb8-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the user specify the value of rho/rho_hat that we condition on:</span></span>
<span id="cb8-3">plot_Bayes_vs_Freq <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> \(r) {</span>
<span id="cb8-4">  p1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> joint <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(rho_hat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> r <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.005</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> rho_hat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> r <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.005</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> rho)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">after_stat</span>(density)),</span>
<span id="cb8-8">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binwidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"skyblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bquote</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hat</span>(rho) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> .(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(r, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))),</span>
<span id="cb8-11">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(rho))</span>
<span id="cb8-12"></span>
<span id="cb8-13">  p2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> joint <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(rho <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> r <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.005</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> rho <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> r <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.005</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-15">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> rho_hat)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-16">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">after_stat</span>(density)),</span>
<span id="cb8-17">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binwidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"skyblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-18">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-19">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bquote</span>(rho <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> .(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(r, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))),</span>
<span id="cb8-20">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hat</span>(rho)))</span>
<span id="cb8-21"></span>
<span id="cb8-22">  p1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p2</span>
<span id="cb8-23">}</span>
<span id="cb8-24"></span>
<span id="cb8-25"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_Bayes_vs_Freq</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.98</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/sims-and-uhlig-1991-replication/index_files/figure-html/unnamed-chunk-2-4.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_Bayes_vs_Freq</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/sims-and-uhlig-1991-replication/index_files/figure-html/unnamed-chunk-2-5.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_Bayes_vs_Freq</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/sims-and-uhlig-1991-replication/index_files/figure-html/unnamed-chunk-2-6.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_Bayes_vs_Freq</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.01</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/sims-and-uhlig-1991-replication/index_files/figure-html/unnamed-chunk-2-7.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_Bayes_vs_Freq</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.02</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/sims-and-uhlig-1991-replication/index_files/figure-html/unnamed-chunk-2-8.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>


</section>


 ]]></description>
  <category>time series</category>
  <guid>https://www.econometrics.blog/post/sims-and-uhlig-1991-replication/</guid>
  <pubDate>Mon, 15 Jul 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>The Return of econometrics.blog!</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/the-return-of-econometrics-blog/</link>
  <description><![CDATA[ 




<p>After a year-long hiatus, I’m excited to return to regular blogging about econometrics! I have a long list of posts that I’m eager to write, and I hope you’ll find them interesting. To whet your appetite, here’s a preview of some of the topics I plan to cover in the coming weeks:</p>
<ul>
<li>Bayesian versus Frequentist Approaches to Unit Roots</li>
<li>How Not To Do Regression Adjustment</li>
<li>Understanding the James-Stein Estimator</li>
</ul>
<p>In the meantime, I have a few econometrics-related announcements:</p>
<ol type="1">
<li>I’ll be teaching a summer course on causal inference at Oxford this September. If you’re interested in attending here are the <a href="https://ouess.web.ox.ac.uk/event/treatment-effects-beyond-the-basics-econometrics">registration details</a> and here’s the <a href="https://www.treatment-effects.com/beyond/">course website</a>.</li>
<li>I’m currently running a virtual summer reading group on Bayesian Econometrics that will continue at least until September and potentially beyond, depending on interest. If you have an email address that ends in <code>.ox.ac.uk</code> you can self-register <a href="https://edstem.org/us/join/6j2hay">here</a>. If you don’t have an Oxford email address, send me an email and I’ll add you manually.</li>
<li><a href="https://users.ox.ac.uk/~econ0610/">Martin Weidner</a> and I recently kicked off an initiative to change the way that research in econometrics is assessed. To find out more, visit <a href="https://sqare.org/">sqare.org</a>.</li>
</ol>
<p>I’m looking forward to getting back to regular posting. If you have any special requests, please add them in the comments below.</p>



 ]]></description>
  <category>meta</category>
  <guid>https://www.econometrics.blog/post/the-return-of-econometrics-blog/</guid>
  <pubDate>Sun, 14 Jul 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>A Good Instrument is a Bad Control</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/a-good-instrument-is-a-bad-control/</link>
  <description><![CDATA[ 




<p>Here’s a puzzle for you. What will happen if we regress some outcome of interest on <em>both</em> an endogenous regressor <em>and</em> a valid instrument for that regressor? I hadn’t thought about this question until 2018, when one of my undergraduate students asked it during class. If memory serves, my off-the-cuff answer left much to be desired.<sup>1</sup> Five years later I’m finally ready to give a fully satisfactory answer; better late than never I suppose!</p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;I seem to recall saying something like “this won’t in general give us the causal effect we’re interested in, but I don’t think it’s possible to say anything more without extra assumptions.” Fortunately my lackluster response didn’t derail the student who asked the question: he’s currently pursuing a PhD in Economics at UChicago!</p></div></div><section id="the-model" class="level2 page-columns page-full" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="the-model"><span class="header-section-number">1</span> The Model</h2>
<p>We’ll start by being a bit more precise about the setup. Suppose that <img src="https://latex.codecogs.com/png.latex?Y"> is related to <img src="https://latex.codecogs.com/png.latex?X"> according to the following <strong>linear causal model</strong> <img src="https://latex.codecogs.com/png.latex?%0AY%20%5Cleftarrow%20%5Calpha%20+%20%5Cbeta%20X%20+%20U%0A"> where <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> is the causal effect of interest and <img src="https://latex.codecogs.com/png.latex?U"> represents unobserved causes of <img src="https://latex.codecogs.com/png.latex?Y"> that may be related to <img src="https://latex.codecogs.com/png.latex?X">. Now, for <em>any</em> observed random variable <img src="https://latex.codecogs.com/png.latex?Z">, we can define <img src="https://latex.codecogs.com/png.latex?%0AV%20%5Cequiv%20X%20-%20(%5Cpi_0%20+%20%5Cpi_1%20Z),%20%5Cquad%20%5Cpi_0%20%5Cequiv%20%5Cmathbb%7BE%7D%5BX%5D%20-%20%5Cpi_1%20%5Cmathbb%7BE%7D%5BZ%5D,%20%5Cquad%20%5Cpi_1%20%5Cequiv%20%5Cfrac%7B%5Ctext%7BCov%7D(X,Z)%7D%7B%5Ctext%7BVar%7D(Z)%7D.%0A"> This is the <strong>population linear regression</strong> of <img src="https://latex.codecogs.com/png.latex?X"> on <img src="https://latex.codecogs.com/png.latex?Z">. By construction it satisfies <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5BV%5D%20=%20%5Ctext%7BCov%7D(Z,V)%20=%200">.<sup>2</sup> Thus we can write, <img src="https://latex.codecogs.com/png.latex?%0AX%20=%20%5Cpi_0%20+%20%5Cpi_1%20Z%20+%20V,%20%5Cquad%20%5Cmathbb%7BE%7D%5BV%5D%20=%20%5Ctext%7BCov%7D(Z,V)%20=%200%0A"> for <em>any</em> random variables <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Z">, simply by constructing <img src="https://latex.codecogs.com/png.latex?V"> as described above. If <img src="https://latex.codecogs.com/png.latex?%5Cpi_1%20%5Cneq%200">, we say that <img src="https://latex.codecogs.com/png.latex?Z"> is <strong>relevant</strong>. If <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(Z,U)%20=%200">, we say that <img src="https://latex.codecogs.com/png.latex?Z"> is <strong>exogenous</strong>. If <img src="https://latex.codecogs.com/png.latex?Z"> is both relevant and exogenous, we say that it is a <strong>valid instrument</strong> for <img src="https://latex.codecogs.com/png.latex?X">.</p>
<div class="no-row-height column-margin column-container"><div id="fn2"><p><sup>2</sup>&nbsp;Check if you don’t believe me: substitute the expressions for <img src="https://latex.codecogs.com/png.latex?%5Cpi_0"> and <img src="https://latex.codecogs.com/png.latex?%5Cpi_1">, take expectations / covariances, and simplify.</p></div></div><p>As we’ve defined it above, <img src="https://latex.codecogs.com/png.latex?V"> is simply a regression residual. But if <img src="https://latex.codecogs.com/png.latex?Z"> is a valid instrument, it turns out that we can think of <img src="https://latex.codecogs.com/png.latex?V"> as the “endogenous part” of <img src="https://latex.codecogs.com/png.latex?X">. To see why, expand <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(X,U)"> as follows: <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BCov%7D(X,U)%20=%20%5Ctext%7BCov%7D(%5Cpi_0%20+%20%5Cpi_1%20Z%20+%20V,%20%5C,U)%20=%20%5Cpi_1%20%5Ctext%7BCov%7D(Z,U)%20+%20%5Ctext%7BCov%7D(U,V)%20=%20%5Ctext%7BCov%7D(U,V)%0A"> since we have assumed that <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(Z,U)%20=%200">. In words, the endogeneity of <img src="https://latex.codecogs.com/png.latex?X"> is <em>precisely the same thing</em> as the covariance between <img src="https://latex.codecogs.com/png.latex?U"> and <img src="https://latex.codecogs.com/png.latex?V">.</p>
<p>Here’s a helpful way of thinking about this. If <img src="https://latex.codecogs.com/png.latex?Z"> is exogenous then our regression of <img src="https://latex.codecogs.com/png.latex?X"> on <img src="https://latex.codecogs.com/png.latex?Z"> <em>partitions</em> the overall variation in <img src="https://latex.codecogs.com/png.latex?X"> into two components: the “good” (exogenous) variation <img src="https://latex.codecogs.com/png.latex?%5Cpi_1%20Z"> is uncorrelated with <img src="https://latex.codecogs.com/png.latex?U">, while the “bad” (endogenous) variation <img src="https://latex.codecogs.com/png.latex?V"> is correlated with <img src="https://latex.codecogs.com/png.latex?U">. The logic of two-stage least squares is that regressing <img src="https://latex.codecogs.com/png.latex?Y"> on the “good” variation, <img src="https://latex.codecogs.com/png.latex?%5Cpi_1%20Z"> allows us to recover <img src="https://latex.codecogs.com/png.latex?%5Cbeta">, the causal effect of interest.<sup>3</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn3"><p><sup>3</sup>&nbsp;See <a href="https://www.econometrics.blog/post/three-ways-of-thinking-about-instrumental-variables/">this blog post</a> for more discussion.</p></div></div></section>
<section id="a-simulation-example" class="level2 page-columns page-full" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="a-simulation-example"><span class="header-section-number">2</span> A Simulation Example</h2>
<p>Using the model and derivations from above, let’s run a little simulation. To simulate a valid instrument <img src="https://latex.codecogs.com/png.latex?Z"> and an endogenous regressor <img src="https://latex.codecogs.com/png.latex?X"> we can proceed as follows. First generate independent standard normal draws <img src="https://latex.codecogs.com/png.latex?%5C%7BZ_i%5C%7D_%7Bi=1%7D%5En">. Next independently generate pairs of correlated standard normal draws <img src="https://latex.codecogs.com/png.latex?%5C%7B(U_i,%20V_i)%5C%7D_%7Bi=1%7D%5En"> with <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCorr%7D(U_i,%20V_i)%20=%20%5Crho">. Finally, set <img src="https://latex.codecogs.com/png.latex?%0AX_i%20=%20%5Cpi_0%20+%20%5Cpi_1%20Z_i%20+%20V_i%20%5Cquad%20%5Ctext%7Band%7D%20%5Cquad%0AY_i%20=%20%5Calpha%20+%20%5Cbeta%20X_i%20+%20U_i%0A"> for each value of <img src="https://latex.codecogs.com/png.latex?i"> between <img src="https://latex.codecogs.com/png.latex?1"> and <img src="https://latex.codecogs.com/png.latex?n">.<sup>4</sup> The following chunk of R code runs this simulation with <img src="https://latex.codecogs.com/png.latex?n%20=%205000">, <img src="https://latex.codecogs.com/png.latex?%5Crho%20=%200.5">, <img src="https://latex.codecogs.com/png.latex?%5Cpi_0%20=%200.5">, <img src="https://latex.codecogs.com/png.latex?%5Cpi_1%20=%200.8">, <img src="https://latex.codecogs.com/png.latex?%5Calpha%20=%20-0.3"> and <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%201">:</p>
<div class="no-row-height column-margin column-container"><div id="fn4"><p><sup>4</sup>&nbsp;We don’t necessarily need <img src="https://latex.codecogs.com/png.latex?Z_i"> to be normally distributed, as long as it’s independent of <img src="https://latex.codecogs.com/png.latex?(U_i,%20V_i)">, so you could use e.g.&nbsp;uniform draws if you prefer. Generating <img src="https://latex.codecogs.com/png.latex?(U_i,%20V_i)"> from a bivariate normal distribution isn’t necessary either, but it’s a simple way of controlling the endogeneity in <img src="https://latex.codecogs.com/png.latex?X">.</p></div></div><div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1234</span>)</span>
<span id="cb1-2">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span></span>
<span id="cb1-3">z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(n)</span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(mvtnorm)</span>
<span id="cb1-6">Rho <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, </span>
<span id="cb1-7">                <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">byrow =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb1-8">errors <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rmvnorm</span>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> Rho)</span>
<span id="cb1-9"></span>
<span id="cb1-10">u <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> errors[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb1-11">v <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> errors[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb1-12">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> z <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> v</span>
<span id="cb1-13">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> u</span></code></pre></div></div>
</div>
<p>In the simulation <img src="https://latex.codecogs.com/png.latex?Z"> is a valid instrument, <img src="https://latex.codecogs.com/png.latex?X"> is an endogenous regressor, and the true causal effect of interest equals one. Using our simulation data, let’s test out three possible estimators:</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7B%5Cbeta%7D_%5Ctext%7BOLS%7D%5Cequiv"> the slope coefficient from an OLS regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X">.</li>
<li><img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7B%5Cbeta%7D_%5Ctext%7BIV%7D%5Cequiv"> slope coefficient from an IV regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X"> with <img src="https://latex.codecogs.com/png.latex?Z"> as an instrument.</li>
<li><img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7B%5Cbeta%7D_%7BX.Z%7D%5Cequiv"> the coefficient on <img src="https://latex.codecogs.com/png.latex?X"> in an OLS regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Z">.</li>
</ul>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb2-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b_OLS =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cov</span>(x, y) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">var</span>(x), </span>
<span id="cb2-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b_IV =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cov</span>(z, y) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cov</span>(z, x), </span>
<span id="cb2-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b_x.z =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unname</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> z))[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># unname() makes the names prettier!</span></span>
<span id="cb2-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>truth b_OLS  b_IV b_x.z 
 1.00  1.31  1.01  1.49 </code></pre>
</div>
</div>
<p>As expected, OLS is far from the truth while IV pretty much nails it. Interestingly, the regression of <code>y</code> on <code>x</code> and <code>z</code> gives the worst performance of all! Is this just a fluke? Perhaps it’s an artifact of the simulation parameters I chose, or just bad luck arising from some unusual simulation draws. To find out, we’ll need a bit more algebra. But stay with me: the payoff is worth it, and there’s not too much extra math required!</p>
</section>
<section id="the-general-result" class="level2 page-columns page-full" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="the-general-result"><span class="header-section-number">3</span> The General Result</h2>
<section id="regression-of-y-on-x-and-z" class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="regression-of-y-on-x-and-z">Regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Z"></h3>
<p>The coefficient on <img src="https://latex.codecogs.com/png.latex?X"> in a population linear regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Z"> is given by <img src="https://latex.codecogs.com/png.latex?%0A%5Cbeta_%7BX.Z%7D%20=%20%5Cfrac%7B%5Ctext%7BCov%7D(%5Ctilde%7BX%7D,%20Y)%7D%7B%5Ctext%7BVar%7D(%5Ctilde%7BX%7D)%7D%0A"> where <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BX%7D"> is defined as the <em>residual</em> in another population linear regression: the regression of <img src="https://latex.codecogs.com/png.latex?X"> on <img src="https://latex.codecogs.com/png.latex?Z">.<sup>5</sup> But wait a minute: we’ve <em>already seen</em> this residual! Above we called it <img src="https://latex.codecogs.com/png.latex?V"> and used it to write <img src="https://latex.codecogs.com/png.latex?X%20=%20%5Cpi_0%20+%20%5Cpi_1%20Z%20+%20V">. Using this equation, along with the linear causal model relating <img src="https://latex.codecogs.com/png.latex?Y"> to <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?U">, we can re-express <img src="https://latex.codecogs.com/png.latex?%5Cbeta_%7BX.Z%7D"> as <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cbeta_%7BX.Z%7D%20&amp;=%20%5Cfrac%7B%5Ctext%7BCov%7D(V,%20Y)%7D%7B%5Ctext%7BVar%7D(V)%7D%20=%20%5Cfrac%7B%5Ctext%7BCov%7D(V,%20%5Calpha%20+%20%5Cbeta%20X%20+%20U)%7D%7B%5Ctext%7BVar%7D(V)%7D%5C%5C%0A&amp;=%20%5Cfrac%7B%5Ctext%7BCov%7D(U,V)%20+%20%5Cbeta%5Ctext%7BCov%7D(V,%20%5Cpi_0%20+%20%5Cpi_1%20Z%20+%20V)%7D%7B%5Ctext%7BVar%7D(V)%7D%5C%5C%0A&amp;=%20%5Cbeta%20+%20%5Cfrac%7B%5Ctext%7BCov%7D(U,V)%7D%7B%5Ctext%7BVar%7D(V)%7D%0A%5Cend%7Baligned%7D%0A"> since <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(Z,%20V)%20=%200"> by construction. We have some simulation data at our disposal, so let’s check this calculation. In the simulation <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%201"> and <img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7B%5Ctext%7BCov%7D(U,%20V)%7D%7B%5Ctext%7BVar%7D(V)%7D%20=%200.5%0A"> since <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVar%7D(U)%20=%20%5Ctext%7BVar%7D(V)%20=%201"> and <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(U,%20V)%20=%200.5">. Therefore <img src="https://latex.codecogs.com/png.latex?%5Cbeta_%7BX.Z%7D%20=%201.5">. And, indeed, this is almost <em>exactly</em> the value of our estimate from our simulation above.</p>
<div class="no-row-height column-margin column-container"><div id="fn5"><p><sup>5</sup>&nbsp;This is a special case of the so-called <a href="https://en.wikipedia.org/wiki/Frisch%E2%80%93Waugh%E2%80%93Lovell_theorem">FWL Theorem</a>, although I’d argue that we should call it “Yule’s Rule” since <a href="https://en.wikipedia.org/wiki/Udny_Yule">George Udny Yule</a> was arguably the first person to popularize it, decades before F, W, or L.</p></div></div></section>
<section id="regression-of-y-on-x-only" class="level3">
<h3 class="anchored" data-anchor-id="regression-of-y-on-x-only">Regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X"> Only</h3>
<p>So far so good. Now what about the “usual” OLS estimator? A quick calculation gives <img src="https://latex.codecogs.com/png.latex?%0A%5Cbeta_%7B%5Ctext%7BOLS%7D%7D%20=%20%5Cbeta%20+%20%5Cfrac%7B%5Ctext%7BCov%7D(X,U)%7D%7B%5Ctext%7BVar%7D(X)%7D%20=%20%5Cbeta%20+%20%5Cfrac%7B%5Ctext%7BCov%7D(V,U)%7D%7B%5Ctext%7BVar%7D(X)%7D%0A"> using the fact that <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(X,U)%20=%20%5Ctext%7BCov%7D(U,V)">, as explained above. Again, we can check this against our simulation results. We know that <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(V,U)%20=%200.5"> and <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BVar%7D(X)%20=%20%5Ctext%7BVar%7D(%5Cpi_0%20+%20%5Cpi_1%20Z%20+%20V)%20=%20%5Cpi_1%5E2%20%5Ctext%7BVar%7D(Z)%20+%20%5Ctext%7BVar%7D(V)%20=%20(0.8)%5E2%20+%201%20=%2041/25%0A"> since <img src="https://latex.codecogs.com/png.latex?Z"> and <img src="https://latex.codecogs.com/png.latex?V"> are uncorrelated by construction, <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVar%7D(Z)%20=%20%5Ctext%7BVar%7D(V)%20=%201"> and <img src="https://latex.codecogs.com/png.latex?%5Cpi_1%20=%200.8"> in the simulation design. Hence, <img src="https://latex.codecogs.com/png.latex?%5Cbeta_%7B%5Ctext%7BOLS%7D%7D%20=%201%20+%2025/82%20%5Capprox%201.305">. Again, this agrees almost perfectly with our simulation.</p>
</section>
<section id="comparing-the-results" class="level3">
<h3 class="anchored" data-anchor-id="comparing-the-results">Comparing the Results</h3>
<p>To summarize, we have shown that <img src="https://latex.codecogs.com/png.latex?%0A%5Cbeta_%7BX.Z%7D%20=%20%5Cbeta%20+%20%5Cfrac%7B%5Ctext%7BCov%7D(U,V)%7D%7B%5Ctext%7BVar%7D(V)%7D,%20%5Cquad%20%5Ctext%7Bwhile%7D%20%5Cquad%0A%5Cbeta_%7B%5Ctext%7BOLS%7D%7D%20=%20%5Cbeta%20+%20%5Cfrac%7B%5Ctext%7BCov%7D(U,V)%7D%7B%5Ctext%7BVar%7D(X)%7D.%0A"> There is only one difference between these two expressions: <img src="https://latex.codecogs.com/png.latex?%5Cbeta_%7BX.Z%7D"> has <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVar%7D(V)"> where <img src="https://latex.codecogs.com/png.latex?%5Cbeta_%7B%5Ctext%7BOLS%7D%7D"> has <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVar%7D(X)">. Returning to our expression for <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVar%7D(X)"> from above, <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BVar%7D(X)%20=%20%5Cpi_1%5E2%20%5Ctext%7BVar%7D(Z)%20+%20%5Ctext%7BVar%7D(V)%20%3E%20%5Ctext%7BVar%7D(V)%0A"> as long as <img src="https://latex.codecogs.com/png.latex?%5Cpi_1%20%5Cneq%200"> and <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVar%7D(Z)%20%5Cneq%200">. In other words, there is always <em>more</em> variation in <img src="https://latex.codecogs.com/png.latex?X"> than there is in <img src="https://latex.codecogs.com/png.latex?V">, since <img src="https://latex.codecogs.com/png.latex?V"> is the “leftover” part of <img src="https://latex.codecogs.com/png.latex?X"> after regressing on <img src="https://latex.codecogs.com/png.latex?Z">. Because the variances of <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?V"> appear in the denominators of our expressions from above, it follows that <img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%7C%20%5Ctext%7BCov%7D(U,V)/%5Ctext%7BVar%7D(V)%5Cright%7C%20%3E%20%5Cleft%7C%20%5Ctext%7BCov%7D(U,V)/%5Ctext%7BVar%7D(X)%5Cright%7C.%0A"> In other words, <img src="https://latex.codecogs.com/png.latex?%5Cbeta_%7BX.Z%7D"> is <strong>always farther from the truth</strong> than <img src="https://latex.codecogs.com/png.latex?%5Cbeta_%7BOLS%7D">, exactly as we found in our simulation.</p>
</section>
</section>
<section id="some-intuition" class="level2" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="some-intuition"><span class="header-section-number">4</span> Some Intuition</h2>
<p>In our simulation, <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7B%5Cbeta%7D_%7BX.Z%7D"> gave a <em>worse</em> estimate of <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> than <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7B%5Cbeta%7D_%5Ctext%7BOLS%7D">. The derivations from above show that this wasn’t a fluke: adding a valid instrument <img src="https://latex.codecogs.com/png.latex?Z"> as an additional control regressor only makes the bias in our estimated causal effect <em>worse</em> than it was to begin with. This holds for any valid instrument and any endogenous regressor in a linear causal model. I hope you found the derivations from above convincing. But even so, you may be wondering if there’s an intuitive explanation for this phenomenon. I am pleased to inform you that the answer is yes!</p>
<p>In an <a href="https://www.econometrics.blog/post/three-ways-of-thinking-about-instrumental-variables/">earlier post</a> I described the <strong>control function</strong> approach to instrumental variables regression. That post showed that the coefficient on <img src="https://latex.codecogs.com/png.latex?X"> in a regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?V"> gives the <em>correct</em> causal effect. We don’t know <img src="https://latex.codecogs.com/png.latex?V">, but we can estimate it by regressing <img src="https://latex.codecogs.com/png.latex?X"> on <img src="https://latex.codecogs.com/png.latex?Z"> and saving the residuals. The logic of multiple regression shows that including <img src="https://latex.codecogs.com/png.latex?V"> as a control regressor “soaks up” the portion of <img src="https://latex.codecogs.com/png.latex?X"> that is explained by <img src="https://latex.codecogs.com/png.latex?V">. Because <img src="https://latex.codecogs.com/png.latex?V"> represents the “bad” (endogenous) variation in <img src="https://latex.codecogs.com/png.latex?X">, this solves our endogeneity problem. In effect, <img src="https://latex.codecogs.com/png.latex?V"> captures the unobserved “omitted variables” that play havoc with a naive regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X">.</p>
<p>Now, contrast this with a regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Z">. In this case, we soak up the variation in <img src="https://latex.codecogs.com/png.latex?X"> that is explained by <img src="https://latex.codecogs.com/png.latex?Z">. But <img src="https://latex.codecogs.com/png.latex?Z"> represents the <strong>good</strong> (exogenous) variation in <img src="https://latex.codecogs.com/png.latex?X">! Soaking up this variation leaves only the bad variation behind, making our endogeneity problem worse than it was to begin with. In this example, <img src="https://latex.codecogs.com/png.latex?Z"> is what is known as a <a href="https://ditraglia.com/erm/16-DAGs-bad-controls.pdf">bad control</a>, a control regressor that makes things worse rather than better. A common piece of advice for avoiding bad controls is to only include control regressors that are correlated with <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y"> but are <em>not themselves</em> caused by <img src="https://latex.codecogs.com/png.latex?X">. The example in this post shows that this advice <strong>is wrong</strong>. Here <img src="https://latex.codecogs.com/png.latex?Z"> is not caused by <img src="https://latex.codecogs.com/png.latex?X">, and is correlated with both <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y">. Nevertheless, it is a bad control. In short, a valid instrument provides a powerful way to carry out causal inference from observational data, but only if you use it in the right way. A good instrument is a bad control!</p>


</section>


 ]]></description>
  <category>econometrics</category>
  <category>causal inference</category>
  <guid>https://www.econometrics.blog/post/a-good-instrument-is-a-bad-control/</guid>
  <pubDate>Thu, 29 Jun 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>The R Formula Cheatsheet</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/the-r-formula-cheatsheet/</link>
  <description><![CDATA[ 




<p>R’s formula syntax is extremely powerful but can be confusing for beginners.<sup>1</sup> This post is a quick reference covering all of the symbols that have a “special” meaning inside of an R formula: <code>~, +, ., -, 1, :, *, ^</code>, and <code>I()</code>. You may never use some of these in practice, but it’s nice to know that they exist. It was many years before I realized that I could simply type <code>y ~ x * z</code> instead of the lengthier <code>y ~ x + z + x:z</code>, for example. While R formulas crop up in a variety of places, they are probably most familiar as the first argument of <code>lm()</code>. For this reason, my verbal explanations assume a simple linear regression setting in which we hope to predict <code>y</code> using a number of regressors <code>x</code>, <code>z</code>, and <code>w</code>.</p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;Fun fact: R’s formula syntax originated in <a href="https://www.jstor.org/stable/2346786">this 1973 paper</a> by Wilkinson and Rogers.</p></div></div><table class="caption-top table">
<colgroup>
<col style="width: 14%">
<col style="width: 29%">
<col style="width: 15%">
<col style="width: 40%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Symbol</th>
<th style="text-align: left;">Purpose</th>
<th style="text-align: left;">Example</th>
<th style="text-align: left;">In Words</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><code>~</code></td>
<td style="text-align: left;">separate LHS and RHS of formula</td>
<td style="text-align: left;"><code>y ~ x</code></td>
<td style="text-align: left;">regress <code>y</code> on <code>x</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>+</code></td>
<td style="text-align: left;">add variable to a formula</td>
<td style="text-align: left;"><code>y ~ x + z</code></td>
<td style="text-align: left;">regress <code>y</code> on <code>x</code> <em>and</em> <code>z</code></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>.</code></td>
<td style="text-align: left;">denotes “everything else”</td>
<td style="text-align: left;"><code>y ~ .</code></td>
<td style="text-align: left;">regress <code>y</code> on <em>all other variables</em> in a data frame</td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>-</code></td>
<td style="text-align: left;">remove variable from a formula</td>
<td style="text-align: left;"><code>y ~ . - x</code></td>
<td style="text-align: left;">regress <code>y</code> on all other variables <em>except</em> <code>x</code></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>1</code></td>
<td style="text-align: left;">denotes intercept</td>
<td style="text-align: left;"><code>y ~ x - 1</code></td>
<td style="text-align: left;">regress <code>y</code> on <code>x</code> <em>without an intercept</em></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>:</code></td>
<td style="text-align: left;">construct interaction term</td>
<td style="text-align: left;"><code>y ~ x + z + x:z</code></td>
<td style="text-align: left;">regress <code>y</code> on <code>x</code>, <code>z</code>, and the product <code>x</code> times <code>z</code></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>*</code></td>
<td style="text-align: left;">shorthand for levels plus interaction</td>
<td style="text-align: left;"><code>y ~ x * z</code></td>
<td style="text-align: left;">regress <code>y</code> on <code>x</code>, <code>z</code>, and the product <code>x</code> times <code>z</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>^</code></td>
<td style="text-align: left;">higher order interactions</td>
<td style="text-align: left;"><code>y ~ (x + z + w)^3</code></td>
<td style="text-align: left;">regress <code>y</code> on <code>x</code>, <code>z</code>, <code>w</code>, all two-way interactions, and the three-way interactions</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>I()</code></td>
<td style="text-align: left;">“as-is” - override special meanings of other symbols from this table</td>
<td style="text-align: left;"><code>y ~ x + I(x^2)</code></td>
<td style="text-align: left;">regress <code>y</code> on <code>x</code> and <code>x</code> squared</td>
</tr>
</tbody>
</table>




 ]]></description>
  <category>computing</category>
  <guid>https://www.econometrics.blog/post/the-r-formula-cheatsheet/</guid>
  <pubDate>Wed, 19 Apr 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Random Variables Cheatsheet</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/random-variables-cheatsheet/</link>
  <description><![CDATA[ 




<p>To do well in an econometrics or statistics course at any level, you need to have a large number of simple properties of random variables at your fingertips. Some years back I made a handout containing the most important properties for my undergraduate students at the University of Pennsylvania. In the hopes that this might be of use to others, I’ve released an <a href="https://github.com/fditraglia/random-variables-cheatsheet/blob/main/random-variables-cheatsheet.pdf">updated pdf on github</a>. You can fork the repository <a href="https://github.com/fditraglia/random-variables-cheatsheet">here</a>. If you spot any errors or want to suggest any additions, feel free to raise an <a href="https://github.com/fditraglia/random-variables-cheatsheet/issues">issue</a> or send me a <a href="https://github.com/fditraglia/random-variables-cheatsheet/pulls">pull request</a>.</p>



 ]]></description>
  <category>statistics</category>
  <guid>https://www.econometrics.blog/post/random-variables-cheatsheet/</guid>
  <pubDate>Sat, 07 Jan 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Why Econometrics is Confusing Part II: The Independence Zoo</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/why-econometrics-is-confusing-part-ii-the-independence-zoo/</link>
  <description><![CDATA[ 




<p>In econometrics it’s absolutely crucial to keep track of which things are <em>dependent</em> and which are <em>independent</em>. To make this as confusing as possible for students, a typical introductory econometrics course moves back and forth between different notions of dependence, stopping occasionally to mention that they’re not equivalent but never fully explaining why, on the premise that “you’ve certainly already learned this in your introductory probability and statistics course.” I remember finding this extremely frustrating as a student, but only recently managed to translate this frustration into meaningful changes in my own teaching.<sup>1</sup> Building on some of my recent teaching materials, this post is a field guide to the menagerie–or at least petting zoo–of “dependence” notions that appear regularly in econometrics. We’ll examine each property on its own along with the relationships between them, using the simple examples to build your intuition. Since a picture is worth a thousand words, here’s one that summarizes the entire post:</p>
<!-- Rendered by TeX Live: latex writes a DVI, dvisvgm turns it into SVG. The
     dvisvgm class option makes pgf emit SVG directly instead of PostScript, so
     no Ghostscript is needed; --no-fonts converts glyphs to outlines, so the
     figure carries no font references. See CLAUDE.md. -->
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;It turns out that teaching well is extremely hard. I am incredibly grateful to those intrepid souls who bravely raise their hand and inform me that no one in the room has any idea what I’m talking about!</p></div></div><div class="cell">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/why-econometrics-is-confusing-part-ii-the-independence-zoo/index_files/figure-html/tikz-ex-1.svg" class="img-fluid figure-img" width="480"></p>
<figcaption>Different notions of dependence in econometrics and their relationships. A directed double arrow indicates that one property implies another.</figcaption>
</figure>
</div>
</div>
</div>
<section id="prerequisites" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="prerequisites"><span class="header-section-number">1</span> Prerequisites</h2>
<p>While written at an introductory level, this post assumes basic familiarity with calculations involving discrete and continuous random variables. In particular, I assume that:</p>
<ul>
<li>You know the definitions of expected value, variance, covariance, and correlation.</li>
<li>You are comfortable working with joint, marginal, and conditional distributions of a pair of discrete random variables.</li>
<li>You understand the uniform distribution and how to compute its moments (mean, variance, etc.).</li>
<li>You’ve encountered the notion of conditional expectation and the law of iterated expectations.</li>
</ul>
<p>If you’re a bit rusty on this material, lectures 7-11 from <a href="http://ditraglia.com/Econ103Public/slides/lecture_slides.pdf">these slides</a> should be helpful. For bivariate, discrete distributions I also suggest watching <a href="https://vimeo.com/119881985">this video</a> from 1:07:00 to the end and <a href="https://vimeo.com/141473625">this other video</a> from 0:00:00 up to the one hour mark.</p>
</section>
<section id="two-examples" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="two-examples"><span class="header-section-number">2</span> Two Examples</h2>
<section id="example-1---discrete-rvs-xy" class="level3">
<h3 class="anchored" data-anchor-id="example-1---discrete-rvs-xy">Example #1 - Discrete RVs <img src="https://latex.codecogs.com/png.latex?(X,Y)"></h3>
<p>My first example involves two discrete random variables <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y"> with joint probability mass function <img src="https://latex.codecogs.com/png.latex?p_%7BXY%7D(x,y)"> given by</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th style="text-align: right;"></th>
<th style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?Y=0"></th>
<th style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?Y=1"></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;"><img src="https://latex.codecogs.com/png.latex?X%20=%20-1"></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?1/3"></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?0"></td>
</tr>
<tr class="even">
<td style="text-align: right;"><img src="https://latex.codecogs.com/png.latex?X%20=%200"></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?0"></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?1/3"></td>
</tr>
<tr class="odd">
<td style="text-align: right;"><img src="https://latex.codecogs.com/png.latex?X=%201"></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?1/3"></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?0"></td>
</tr>
</tbody>
</table>
<p>Even without doing any math, we see that knowing <img src="https://latex.codecogs.com/png.latex?X"> conveys information about <img src="https://latex.codecogs.com/png.latex?Y">, and <em>vice-versa</em>. For example, if <img src="https://latex.codecogs.com/png.latex?X%20=%20-1"> then we know that <img src="https://latex.codecogs.com/png.latex?Y"> must equal zero. Similarly, if <img src="https://latex.codecogs.com/png.latex?Y=1"> then <img src="https://latex.codecogs.com/png.latex?X"> must equal zero. Spend a bit of time thinking about this joint distribution before reading further. We’ll have plenty of time for mathematics below, but it’s always worth seeing where our intuition takes us <em>before</em> calculating everything.</p>
<p>To streamline our discussion below, it will be helpful to work out a few basic results about <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y">. A quick calculation with <img src="https://latex.codecogs.com/png.latex?p_%7BXY%7D"> shows that <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BE%7D(XY)%20%5Cequiv%20%5Csum_%7B%5Ctext%7Ball%20%7D%20x%7D%20%5Csum_%7B%5Ctext%7Ball%20%7D%20y%7D%20x%20y%20%5Ccdot%20p_%7BXY%7D(x,y)%20=%200.%0A"> Calculating the marginal pmfs for <img src="https://latex.codecogs.com/png.latex?X"> we see that <img src="https://latex.codecogs.com/png.latex?%0Ap_X(-1)%20=%20p_X(0)%20=%20p_X(1)%20=%201/3%20%5Cimplies%20%5Cmathbb%7BE%7D(X)%20%5Cequiv%20%5Csum_%7B%5Ctext%7Ball%20%7D%20x%7D%20x%20%5Ccdot%20p_X(x)%20=%200.%0A"> Similarly, calculating the marginal pmf of <img src="https://latex.codecogs.com/png.latex?Y">, we obtain <img src="https://latex.codecogs.com/png.latex?%0Ap_Y(0)%20=%202/3,%5C,%20p_Y(1)%20=%201/3%20%5Cimplies%20%5Cmathbb%7BE%7D(Y)%20%5Cequiv%20%5Csum_%7B%5Ctext%7Ball%20%7D%20y%7D%20y%20%5Ccdot%20p_Y(y)%20=%201/3.%0A"> We’ll use these results as ingredients below as we explain and relate three key notions of dependence: <em>correlation</em>, <em>conditional mean independence</em>, and <em>statistical independence</em>.</p>
</section>
<section id="example-2---continuous-rvs-wz" class="level3">
<h3 class="anchored" data-anchor-id="example-2---continuous-rvs-wz">Example #2 - Continuous RVs <img src="https://latex.codecogs.com/png.latex?(W,Z)"></h3>
<p>My second example concerns two continuous random variables <img src="https://latex.codecogs.com/png.latex?W"> and <img src="https://latex.codecogs.com/png.latex?Z">, where <img src="https://latex.codecogs.com/png.latex?W%20%5Csim%20%5Ctext%7BUniform%7D(-1,%201)"> and <img src="https://latex.codecogs.com/png.latex?Z%20=%20W%5E2">. In this example, <img src="https://latex.codecogs.com/png.latex?W"> and <img src="https://latex.codecogs.com/png.latex?Z"> are very strongly related: if I tell you that the realization of <img src="https://latex.codecogs.com/png.latex?W"> is <img src="https://latex.codecogs.com/png.latex?w">, then you know for sure that the realization of <img src="https://latex.codecogs.com/png.latex?Z"> must be <img src="https://latex.codecogs.com/png.latex?w%5E2">. Again, keep this intuition in mind as we work through the mathematics below.</p>
<p>In the remainder of the post, we’ll find it helpful to refer to a few properties of <img src="https://latex.codecogs.com/png.latex?W"> and <img src="https://latex.codecogs.com/png.latex?Z">, namely <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cmathbb%7BE%7D%5BW%5D%20&amp;%5Cequiv%20%5Cint_%7B-%5Cinfty%7D%5E%5Cinfty%20w%5Ccdot%20f_W(w)%5C,%20dw%20=%20%5Cint_%7B-1%7D%5E1%20w%5Ccdot%20%5Cfrac%7B1%7D%7B2%7D%5C,dw%20=%20%5Cleft.%20%5Cfrac%7Bw%5E2%7D%7B4%7D%5Cright%7C_%7B-1%7D%5E1%20=%200%5C%5C%0A%5Cmathbb%7BE%7D%5BZ%5D%20&amp;%5Cequiv%20%5Cmathbb%7BE%7D%5BW%5E2%5D%20=%20%5Cint_%7B-%5Cinfty%7D%5E%7B%5Cinfty%7D%20w%5E2%20%5Ccdot%20f_W(w)%5C,%20dw%20=%20%5Cint_%7B-1%7D%5E1%20w%5E2%20%5Ccdot%20%5Cfrac%7B1%7D%7B2%7D%20%5C,%20dw%20=%20%5Cleft.%20%5Cfrac%7Bw%5E3%7D%7B6%7D%5Cright%7C_%7B-1%7D%5E1%20=%20%5Cfrac%7B1%7D%7B3%7D%5C%5C%0A%5Cmathbb%7BE%7D%5BWZ%5D%20&amp;=%20%5Cmathbb%7BE%7D%5BW%5E3%5D%20%5Cequiv%20%5Cint_%7B-%5Cinfty%7D%5E%5Cinfty%20w%5E3%20%5Ccdot%20f_W(w)%5C,%20dw%20=%5Cint_%7B-1%7D%5E1%20w%5E3%20%5Ccdot%20%5Cfrac%7B1%7D%7B2%7D%5C,%20dw%20=%20%5Cleft.%20%5Cfrac%7Bw%5E4%7D%7B8%7D%20%20%5Cright%7C_%7B-1%7D%5E1%20=%200.%0A%5Cend%7Baligned%7D%0A"> Since <img src="https://latex.codecogs.com/png.latex?W"> is uniform on the interval <img src="https://latex.codecogs.com/png.latex?%5B-1,1%5D">, its pdf is simply <img src="https://latex.codecogs.com/png.latex?1/2"> on this interval, and zero otherwise. All else equal, I prefer easy integration problems!</p>
<!-- # Example #3
**This example is too complicated when it comes to calculating $E(V|U)$. The post is getting too long already, so ditch this one and just finish up based on the other two examples.** 

$U|V=v \sim \text{Normal}(0, 1 + 3v)$ where $V\sim \text{Bernoulli}(1/2)$. Need to think about whether the math is too complicated here. Maybe it would be better to have a Uniform(-1,1) conditional on $V=0$ and a Uniform$(-1-v, 1+v)$ conditional on $V=1$? The idea of this example is to show that conditional mean independence does *not* imply independence. This is relevant in econometrics courses when studies encounter heteroskedasticity. Poisson regression is a useful case here, and robust standard errors. Don't get too deep into this, but I could provide some references for further reading. 
-->
</section>
</section>
<section id="uncorrelatedness" class="level2 page-columns page-full" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="uncorrelatedness"><span class="header-section-number">3</span> Uncorrelatedness</h2>
<p>Recall that the correlation between two random variables <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y"> is defined as <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BCorr%7D(X,Y)%20%5Cequiv%20%5Cfrac%7B%5Ctext%7BCov%7D(X,Y)%7D%7B%5Ctext%7BSD%7D(X)%5Ctext%7BSD%7D(Y)%7D%20=%20%5Cfrac%7B%5Cmathbb%7BE%7D%5B(X%20-%20%5Cmu_X)(Y%20-%20%5Cmu_Y)%5D%7D%7B%5Csqrt%7B%5Cmathbb%7BE%7D%5B(X%20-%20%5Cmu_X)%5E2%5D%5Cmathbb%7BE%7D%5B(Y%20-%20%5Cmu_Y)%5E2%5D%7D%7D%0A"> where <img src="https://latex.codecogs.com/png.latex?%5Cmu_X%20%5Cequiv%20%5Cmathbb%7BE%7D(X)"> and <img src="https://latex.codecogs.com/png.latex?%5Cmu_Y%20%5Cequiv%20%5Cmathbb%7BE%7D(Y)">. We say that <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y"> are <strong>uncorrelated</strong> if <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCorr%7D(X,Y)=%200">. Unless <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y"> are both constants their variances must be positive. This means that the denominator of our expression for <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCorr%7D(X,Y)"> is likewise positive. It follows that <em>zero correlation is the same thing as zero covariance</em>. Correlation is simply covariance <em>rescaled</em> so that the units of <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y"> cancel out and the result always lies between <img src="https://latex.codecogs.com/png.latex?-1"> and <img src="https://latex.codecogs.com/png.latex?1">.</p>
<p>Correlation and covariance are both measures of <em>linear dependence</em>. If <img src="https://latex.codecogs.com/png.latex?X"> is, on average, above its mean when <img src="https://latex.codecogs.com/png.latex?Y"> is above its mean, then <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCorr%7D(X,Y)"> and <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(X,Y)"> are both positive. If <img src="https://latex.codecogs.com/png.latex?X"> is, on average, below its mean when <img src="https://latex.codecogs.com/png.latex?Y"> is above its mean, then <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCorr%7D(X,Y)"> and <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(X,Y)"> are both negative. If there is, on average, no linear relationship between <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y">, then both the correlation and covariance between them are zero. Using the “shortcut formula” for covariance, namely <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BCov%7D(X,Y)%20%5Cequiv%20%5Cmathbb%7BE%7D%5B(X%20-%20%5Cmu_X)(Y%20-%20%5Cmu_Y)%5D%20=%20%5Cmathbb%7BE%7D%5BXY%5D%20-%20%5Cmathbb%7BE%7D%5BX%5D%5Cmathbb%7BE%7D%5BY%5D,%0A"> it follows that uncorrelatedness is equivalent to <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BE%7D%5BXY%5D%20=%20%5Cmathbb%7BE%7D%5BX%5D%5Cmathbb%7BE%7D%5BY%5D.%0A"> Rendering this in English rather than mathematics,</p>
<blockquote class="blockquote">
<p>Two random variables <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y"> are <strong>uncorrelated</strong> if and only if the expectation of their product equals the product of their expectations.</p>
</blockquote>
<section id="example-1-x-and-y-are-uncorrelated." class="level3">
<h3 class="anchored" data-anchor-id="example-1-x-and-y-are-uncorrelated.">Example #1: <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y"> are uncorrelated.</h3>
<p>In Example #1 from above, <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5BXY%5D=0"> and <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(X)%5Cmathbb%7BE%7D(Y)%20=%200%20%5Ctimes%201/3%20=%200"> so <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y"> are uncorrelated. Lack of correlation is one possible way in which two random variables can be thought of as “unrelated.” But it is a relatively <em>weak</em> property. Indeed, <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y"> are in fact <em>highly dependent</em> in Example #1. For example, if <img src="https://latex.codecogs.com/png.latex?X=-1"> then we know for sure that <img src="https://latex.codecogs.com/png.latex?Y=0">. I simply cooked up the numbers to ensure that <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5BXY%5D=%5Cmathbb%7BE%7D%5BX%5D%5Cmathbb%7BE%7D%5BY%5D"> in spite of this.</p>
</section>
<section id="example-2-w-and-z-are-uncorrelated." class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="example-2-w-and-z-are-uncorrelated.">Example #2: <img src="https://latex.codecogs.com/png.latex?W"> and <img src="https://latex.codecogs.com/png.latex?Z"> are uncorrelated.</h3>
<p>Because Example #1 is discrete, it can be a bit tricky to think about what it would mean for a dependence relationship to be <em>nonlinear</em>. Here Example #2 can help. As mentioned above, there is clearly a relationship between <img src="https://latex.codecogs.com/png.latex?Z"> and <img src="https://latex.codecogs.com/png.latex?W">. But this relationship is <em>nonlinear</em> in that <img src="https://latex.codecogs.com/png.latex?Z"> is a <em>quadratic</em> function of <img src="https://latex.codecogs.com/png.latex?W">. Since <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(WZ)%20=%200"> and <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(W)%20%5Ctimes%20%5Cmathbb%7BE%7D(Z)%20=%200%20%5Ctimes%20%5Cmathbb%7BE%7D(Z)%20=%200">, we see that <img src="https://latex.codecogs.com/png.latex?W"> and <img src="https://latex.codecogs.com/png.latex?Z"> are <em>uncorrelated</em>. Another way to see this is by simulating some data with the same properties as Example #2</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1983</span>)</span>
<span id="cb1-2">n_sims <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">250</span></span>
<span id="cb1-3">w <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(n_sims, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-4">z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> w<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(w, z)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.008379101</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(w, z)</span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(z <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> w))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/why-econometrics-is-confusing-part-ii-the-independence-zoo/index_files/figure-html/unnamed-chunk-2-1.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The regression line is flat despite there being an obvious relationship between <img src="https://latex.codecogs.com/png.latex?W"> and <img src="https://latex.codecogs.com/png.latex?Z">. When <img src="https://latex.codecogs.com/png.latex?W"> is positive, there is a positive relationship between the two RVs; but when <img src="https://latex.codecogs.com/png.latex?W"> is negative the picture is reverses. The line of best fit “averages out” the increasing and decreasing relationships on either side of zero to give an overall slope of zero.<sup>2</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn2"><p><sup>2</sup>&nbsp;I used a small number of simulation draws so it would be easier to see the data in the plot. If you use a larger number of simulations, the correlation will be even closer to zero and the line almost perfectly flat.</p></div></div></section>
</section>
<section id="conditional-mean-independence" class="level2 page-columns page-full" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="conditional-mean-independence"><span class="header-section-number">4</span> Conditional Mean Independence</h2>
<p>We say that <img src="https://latex.codecogs.com/png.latex?Y"> is mean independent of <img src="https://latex.codecogs.com/png.latex?X"> if <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y%7CX)%20=%20%5Cmathbb%7BE%7D(Y)">. In words,</p>
<blockquote class="blockquote">
<p><img src="https://latex.codecogs.com/png.latex?Y"> is mean independent of <img src="https://latex.codecogs.com/png.latex?X"> if the conditional mean of <img src="https://latex.codecogs.com/png.latex?Y"> given <img src="https://latex.codecogs.com/png.latex?X"> equals the unconditional mean of <img src="https://latex.codecogs.com/png.latex?Y">.</p>
</blockquote>
<p>Just to make things confusing, this property is sometimes called “conditional mean independence” and sometimes called simply “mean independence.” The terms are completely interchangeable. Reversing the roles of <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y">, we say that <img src="https://latex.codecogs.com/png.latex?X"> is mean independent of <img src="https://latex.codecogs.com/png.latex?Y"> if the conditional mean of <img src="https://latex.codecogs.com/png.latex?X"> given <img src="https://latex.codecogs.com/png.latex?Y"> is the same as the unconditional mean of <img src="https://latex.codecogs.com/png.latex?X">. <strong>Spoiler alert:</strong> it is possible for <img src="https://latex.codecogs.com/png.latex?X"> to be mean independent of <img src="https://latex.codecogs.com/png.latex?Y"> while <img src="https://latex.codecogs.com/png.latex?Y"> is <em>not</em> mean independent of <img src="https://latex.codecogs.com/png.latex?X">. We’ll discuss this further below.</p>
<p>To better understand the concept of mean independence, let’s quickly review the difference between an unconditional mean and a conditional mean. The unconditional mean <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y)">, also known as the “expected value” or “expectation” of <img src="https://latex.codecogs.com/png.latex?Y">, is a <em>constant number</em>.<sup>3</sup> If <img src="https://latex.codecogs.com/png.latex?Y"> is discrete, this is simply the probability-weighted average of all possible realizations of <img src="https://latex.codecogs.com/png.latex?Y">, namely <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BE%7D(Y)%20=%20%5Csum_%7B%5Ctext%7Ball%20%7D%20y%7D%20y%20%5Ccdot%20p_Y(y).%0A"> If <img src="https://latex.codecogs.com/png.latex?Y"> is continuous, it’s the same idea but with an integral replacing the sum and a probability density <img src="https://latex.codecogs.com/png.latex?f_Y(y)"> multiplied by <img src="https://latex.codecogs.com/png.latex?dy"> replacing the probability mass function <img src="https://latex.codecogs.com/png.latex?p_Y(y)">. Either way, we’re simply multiplying numbers together and adding up the result. Despite the similarity in notation, the conditional expectation <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y%7CX)"> is a <em>function of <img src="https://latex.codecogs.com/png.latex?X"></em> that tells us how the mean of <img src="https://latex.codecogs.com/png.latex?Y"> varies with <img src="https://latex.codecogs.com/png.latex?X">. Since <img src="https://latex.codecogs.com/png.latex?X"> is a random variable, so is <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y%7CX)">. If <img src="https://latex.codecogs.com/png.latex?Y"> is conditionally mean independent of <img src="https://latex.codecogs.com/png.latex?X"> then <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y%7CX)"> equals <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y)">. In words, the mean of <img src="https://latex.codecogs.com/png.latex?Y"> <em>does not vary with <img src="https://latex.codecogs.com/png.latex?X"></em>. Regardless of the value that <img src="https://latex.codecogs.com/png.latex?X"> takes on, the mean of <img src="https://latex.codecogs.com/png.latex?Y"> is the same: <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y)">.</p>
<div class="no-row-height column-margin column-container"><div id="fn3"><p><sup>3</sup>&nbsp;Throughout this post, I make the tacit assumption that all means–conditional or unconditional–exist and are finite.</p></div></div><p>There’s another way to think about this property in terms of <em>prediction</em>. With a bit of calculus, we can show that <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y)"> solves the following optimization problem: <img src="https://latex.codecogs.com/png.latex?%0A%5Cmin_%7B%5Ctext%7Ball%20constants%20%7D%20c%7D%20%5Cmathbb%7BE%7D%5B(Y%20-%20c)%5E2%5D.%0A"> In other words, <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y)"> is the <em>constant number</em> that is as close as possible to <img src="https://latex.codecogs.com/png.latex?Y"> on average, where “close” is measured by squared euclidean distance. In this sense, we can think of <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y)"> as our “best guess” of the value that <img src="https://latex.codecogs.com/png.latex?Y"> will take. Again using a bit of calculus, it turns out that <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y%7CX)"> solves the following optimization problem: <img src="https://latex.codecogs.com/png.latex?%0A%5Cmin_%7B%5Ctext%7Ball%20functions%20%7D%20g%7D%20%5Cmathbb%7BE%7D%5B%5C%7BY%20-%20g(X)%20%5C%7D%5E2%5D.%0A"> (See <a href="https://drive.explaineverything.com/thecode/YZFFBCH">this video</a> for a proof.) Thus, <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y%7CX)"> is the <em>function of <img src="https://latex.codecogs.com/png.latex?X"></em> that is <em>as close as possible</em> to <img src="https://latex.codecogs.com/png.latex?Y"> on average, where “close” is measured using squared Euclidean distance. Thus, <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y%7CX)"> is our “best guess” of <img src="https://latex.codecogs.com/png.latex?Y"> after observing <img src="https://latex.codecogs.com/png.latex?X">. We have seen that <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y)"> and <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y%7CX)"> are the solutions to two related but distinct optimization problems; the former is a <em>constant number</em> that doesn’t depend on the realization of <img src="https://latex.codecogs.com/png.latex?X"> whereas the latter is a <em>function of <img src="https://latex.codecogs.com/png.latex?X"></em>. Mean independence is the special case in which the solutions to the two optimization problems coincide: <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y%7CX)%20=%20%5Cmathbb%7BE%7D(Y)">. Therefore,</p>
<blockquote class="blockquote">
<p><img src="https://latex.codecogs.com/png.latex?Y"> is mean independent of <img src="https://latex.codecogs.com/png.latex?X"> if our best guess of <img src="https://latex.codecogs.com/png.latex?Y"> taking <img src="https://latex.codecogs.com/png.latex?X"> into account is the same as our best guess of <img src="https://latex.codecogs.com/png.latex?Y"> ignoring <img src="https://latex.codecogs.com/png.latex?X">, where “best” is defined by “minimizes average squared distance to <img src="https://latex.codecogs.com/png.latex?Y">.”</p>
</blockquote>
<section id="example-1-x-is-mean-independent-of-y." class="level3">
<h3 class="anchored" data-anchor-id="example-1-x-is-mean-independent-of-y.">Example #1: <img src="https://latex.codecogs.com/png.latex?X"> is mean independent of <img src="https://latex.codecogs.com/png.latex?Y">.</h3>
<p>Using the table of joint probabilities for Example #1 above, we found that <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(X)%20=%200">. To determine whether <img src="https://latex.codecogs.com/png.latex?X"> is mean independent of <img src="https://latex.codecogs.com/png.latex?Y">, we need to calculate <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(X%7CY=y)">, which we can accomplish as follows: <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cmathbb%7BE%7D(X%7CY=0)%20&amp;=%20%5Csum_%7B%5Ctext%7Ball%20%7D%20x%7D%20x%20%5Ccdot%20%5Cmathbb%7BP%7D(X=x%7CY=0)%20=%20%5Csum_%7B%5Ctext%7Ball%20%7D%20x%7D%20x%20%5Ccdot%20%5Cfrac%7B%5Cmathbb%7BP%7D(X=x,Y=0)%7D%7B%5Cmathbb%7BP%7D(Y=0)%7D%5C%5C%20%5C%5C%0A%5Cmathbb%7BE%7D(X%7CY=1)%20&amp;=%20%5Csum_%7B%5Ctext%7Ball%20%7D%20x%7D%20x%20%5Ccdot%20%5Cmathbb%7BP%7D(X=x%7CY=1)%20=%20%5Csum_%7B%5Ctext%7Ball%20%7D%20x%7D%20x%20%5Ccdot%20%5Cfrac%7B%5Cmathbb%7BP%7D(X=x,Y=1)%7D%7B%5Cmathbb%7BP%7D(Y=1)%7D.%0A%5Cend%7Baligned%7D%0A"> Substituting the joint and marginal probabilities from the table above, we find that <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BE%7D(X%7CY=0)%20=%200,%20%5Cquad%0A%5Cmathbb%7BE%7D(X%7CY=1)%20=%200.%0A"> Thus <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(X%7CY=y)"> simply equals zero, regardless of the realization <img src="https://latex.codecogs.com/png.latex?y"> of <img src="https://latex.codecogs.com/png.latex?Y">. Since <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(X)%20=%200"> we have shown that <img src="https://latex.codecogs.com/png.latex?X"> is conditionally mean independent of <img src="https://latex.codecogs.com/png.latex?Y">.</p>
</section>
<section id="example-1-y-is-not-mean-independent-of-x." class="level3">
<h3 class="anchored" data-anchor-id="example-1-y-is-not-mean-independent-of-x.">Example #1: <img src="https://latex.codecogs.com/png.latex?Y"> is <em>NOT</em> mean independent of <img src="https://latex.codecogs.com/png.latex?X">.</h3>
<p>To determine whether <img src="https://latex.codecogs.com/png.latex?Y"> is mean independent of <img src="https://latex.codecogs.com/png.latex?X"> we need to calculate <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y%7CX)">. But this is easy. From the table we see that <img src="https://latex.codecogs.com/png.latex?Y"> is <em>known with certainty</em> after we observe <img src="https://latex.codecogs.com/png.latex?X">: if <img src="https://latex.codecogs.com/png.latex?X%20=%20-1"> then <img src="https://latex.codecogs.com/png.latex?Y%20=%200">, if <img src="https://latex.codecogs.com/png.latex?X%20=%200"> then <img src="https://latex.codecogs.com/png.latex?Y%20=%201">, and if <img src="https://latex.codecogs.com/png.latex?X%20=%201"> then <img src="https://latex.codecogs.com/png.latex?Y%20=%200">. Thus, without doing any math at all we find that <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BE%7D(Y%7CX=-1)%20=%200,%20%5Cquad%0A%5Cmathbb%7BE%7D(Y%7CX=0)%20=%201,%20%5Cquad%0A%5Cmathbb%7BE%7D(Y%7CX=1)%20=%200.%0A"> (If you don’t believe me, work through the arithmetic yourself!) This <em>clearly</em> depends on <img src="https://latex.codecogs.com/png.latex?X">, so <img src="https://latex.codecogs.com/png.latex?Y"> is <em>not</em> mean independent of <img src="https://latex.codecogs.com/png.latex?X">.</p>
</section>
<section id="example-2-z-is-not-mean-independent-of-w." class="level3">
<h3 class="anchored" data-anchor-id="example-2-z-is-not-mean-independent-of-w.">Example #2: <img src="https://latex.codecogs.com/png.latex?Z"> is <em>NOT</em> mean independent of <img src="https://latex.codecogs.com/png.latex?W">.</h3>
<p>Above we calculated that <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Z)%20=%20%5Cmathbb%7BE%7D(W%5E2)%20=%201/3">. But the conditional expectation is <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BE%7D(Z%7CW)%20=%20%5Cmathbb%7BE%7D(W%5E2%7CW)%20=%20W%5E2%0A"> using the “taking out what is known” property: conditional on <img src="https://latex.codecogs.com/png.latex?W">, we know <img src="https://latex.codecogs.com/png.latex?W%5E2"> and can hence treat it as though it were a constant in an unconditional expectation, pulling it in front of the <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D"> operator. We see that <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Z%7CW)"> does not equal <img src="https://latex.codecogs.com/png.latex?1/3">: its value depends on <img src="https://latex.codecogs.com/png.latex?W">. Therefore <img src="https://latex.codecogs.com/png.latex?Z"> is not mean independent of <img src="https://latex.codecogs.com/png.latex?W">.</p>
</section>
<section id="example-2-w-is-mean-independent-of-z." class="level3">
<h3 class="anchored" data-anchor-id="example-2-w-is-mean-independent-of-z.">Example #2: <img src="https://latex.codecogs.com/png.latex?W"> is mean independent of <img src="https://latex.codecogs.com/png.latex?Z">.</h3>
<p>This one is trickier. To keep this post at an elementary level, my explanation won’t be completely rigorous. For more details <a href="https://math.stackexchange.com/questions/829779/conditional-expectation-of-x-given-x2">see here</a>. We need to calculate <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(W%7CZ)">. Since <img src="https://latex.codecogs.com/png.latex?Z%20%5Cequiv%20W%5E2"> this is the same thing as <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(W%7CW%5E2)">. Let’s start with an example. Suppose we observe <img src="https://latex.codecogs.com/png.latex?Z%20=%201">. This means that <img src="https://latex.codecogs.com/png.latex?W%5E2%20=%201"> so <img src="https://latex.codecogs.com/png.latex?W"> either equals <img src="https://latex.codecogs.com/png.latex?1"> or <img src="https://latex.codecogs.com/png.latex?-1">. How likely is each of these possible realizations of <img src="https://latex.codecogs.com/png.latex?W"> given that <img src="https://latex.codecogs.com/png.latex?W%5E2%20=%201">? Because the density of <img src="https://latex.codecogs.com/png.latex?W"> is <em>symmetric about zero</em>, <img src="https://latex.codecogs.com/png.latex?f_W(-1)%20=%20f_W(1)">. So given that <img src="https://latex.codecogs.com/png.latex?W%5E2%20=%201">, it is just as likely that <img src="https://latex.codecogs.com/png.latex?W%20=%201"> as it is that <img src="https://latex.codecogs.com/png.latex?W%20=%20-1">. Therefore, <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BE%7D(W%7CW%5E2%20=%201)%20=%200.5%20%5Ctimes%201%20+%200.5%20%5Ctimes%20-1%20=%200.%0A"> Generalizing this idea, if we observe <img src="https://latex.codecogs.com/png.latex?Z%20=%20z"> then <img src="https://latex.codecogs.com/png.latex?W%20=%20%5Csqrt%7Bz%7D"> or <img src="https://latex.codecogs.com/png.latex?-%5Csqrt%7Bz%7D">. But since <img src="https://latex.codecogs.com/png.latex?f_W(%5Ccdot)"> is symmetric about zero, these possibilities are equally likely. Therefore, <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BE%7D(W%7CZ=z)%20=%200.5%20%5Ctimes%20%5Csqrt%7Bz%7D%20-%200.5%20%5Ctimes%20%5Csqrt%7Bz%7D%20=%200.%0A"> Above we calculated that <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(W)%20=%200">. Therefore, <img src="https://latex.codecogs.com/png.latex?W"> is mean independent of <img src="https://latex.codecogs.com/png.latex?Z">.</p>
</section>
</section>
<section id="statistical-independence" class="level2" data-number="5">
<h2 data-number="5" class="anchored" data-anchor-id="statistical-independence"><span class="header-section-number">5</span> Statistical Independence</h2>
<p>When you see the word “independent” without any qualification, this means “statistically independent.” In keeping with this usage, I often write “independent” rather than “statistically independent.” Whichever terminology you prefer, there are three equivalent ways of defining this idea:</p>
<blockquote class="blockquote">
<p><img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y"> are statistically independent if and only if:</p>
<ol type="1">
<li>their joint distribution equals the product of their marginals, or</li>
<li>the conditional distribution of <img src="https://latex.codecogs.com/png.latex?Y%7CX"> equals the unconditional distribution of <img src="https://latex.codecogs.com/png.latex?Y">, or</li>
<li>the conditional distribution of <img src="https://latex.codecogs.com/png.latex?X%7CY"> equals the unconditional distribution of <img src="https://latex.codecogs.com/png.latex?X">.</li>
</ol>
</blockquote>
<p>The link between these three alternatives is the <em>definition of conditional probability</em>. Suppose that <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y"> are discrete random variables with joint pmf <img src="https://latex.codecogs.com/png.latex?p_%7BXY%7D">, marginal pmfs <img src="https://latex.codecogs.com/png.latex?p_X"> and <img src="https://latex.codecogs.com/png.latex?p_Y">, and conditional pmfs <img src="https://latex.codecogs.com/png.latex?p_%7BX%7CY%7D"> and <img src="https://latex.codecogs.com/png.latex?p_%7BY%7CX%7D">. Version 1 requires that <img src="https://latex.codecogs.com/png.latex?p_%7BXY%7D(x,y)%20=%20p_X(x)%20p_Y(y)"> for all realizations <img src="https://latex.codecogs.com/png.latex?x,y">. But by the definition of conditional probability, <img src="https://latex.codecogs.com/png.latex?%0Ap_%7BX%7CY%7D(x%7Cy)%20%5Cequiv%20%5Cfrac%7Bp_%7BXY%7D(x,y)%7D%7Bp_Y(y)%7D,%20%5Cquad%0Ap_%7BY%7CX%7D(y%7Cx)%20%5Cequiv%20%5Cfrac%7Bp_%7BXY%7D(x,y)%7D%7Bp_X(x)%7D.%0A"> If <img src="https://latex.codecogs.com/png.latex?p_%7BXY%7D%20=%20p_X%20p_Y">, these expressions simplify to <img src="https://latex.codecogs.com/png.latex?%0Ap_%7BX%7CY%7D(x%7Cy)%20%5Cequiv%20%5Cfrac%7Bp_%7BX%7D(x)p_Y(y)%7D%7Bp_Y(y)%7D%20=%20p_X(x),%20%5Cquad%0Ap_%7BY%7CX%7D(y%7Cx)%20%5Cequiv%20%5Cfrac%7Bp_%7BX%7D(x)p_Y(y)%7D%7Bp_X(x)%7D%20=%20p_Y(y)%0A"> so 1 implies 2 and 3. Similarly, if <img src="https://latex.codecogs.com/png.latex?p_%7BX%7CY%7D=p_X"> then by the definition of conditional probability <img src="https://latex.codecogs.com/png.latex?%0Ap_%7BX%7CY%7D(x%7Cy)%20%5Cequiv%20%5Cfrac%7Bp_%7BXY%7D(x,y)%7D%7Bp_Y(y)%7D%20=%20p_X(x).%0A"> Re-arranging, this shows that <img src="https://latex.codecogs.com/png.latex?p_%7BXY%7D%20=%20p_X%20p_Y">, so 3 implies 1. An almost identical argument shows that 2 implies 1, completing our proof that these three seemingly different definitions of statistical independence are equivalent. If <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y"> are continuous, the idea is the same but with densities replacing probability mass functions, e.g.&nbsp;<img src="https://latex.codecogs.com/png.latex?f_%7BXY%7D(x,y)%20=%20f_X(x)%20f_Y(y)"> and so on.</p>
<p>In most examples, it’s easier to show independence (or the lack thereof) using 2 or 3 rather than 1. These latter two definitions are also more intuitively appealing. To say that the conditional distribution of <img src="https://latex.codecogs.com/png.latex?X%7CY"> is the same as the unconditional distribution of <img src="https://latex.codecogs.com/png.latex?X"> is the same thing as saying that</p>
<blockquote class="blockquote">
<p><img src="https://latex.codecogs.com/png.latex?Y"> provides absolutely no information about <img src="https://latex.codecogs.com/png.latex?X"> whatsoever.</p>
</blockquote>
<p>If learning <img src="https://latex.codecogs.com/png.latex?Y"> tells us anything at all about <img src="https://latex.codecogs.com/png.latex?X">, then <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y"> are not independent. Similarly, if <img src="https://latex.codecogs.com/png.latex?X"> tells us anything about <img src="https://latex.codecogs.com/png.latex?Y"> at all, then <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y"> are not independent.</p>
<section id="example-1-x-and-y-are-not-independent." class="level3">
<h3 class="anchored" data-anchor-id="example-1-x-and-y-are-not-independent.">Example #1: <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y"> are <em>NOT</em> independent.</h3>
<p>If I tell you that <img src="https://latex.codecogs.com/png.latex?X%20=%200">, then you know for sure that <img src="https://latex.codecogs.com/png.latex?Y%20=%200">. Before I told you this, you did not know that <img src="https://latex.codecogs.com/png.latex?Y"> would equal zero: it’s a random variable with support set <img src="https://latex.codecogs.com/png.latex?%5C%7B0,1%5C%7D">. Since learning <img src="https://latex.codecogs.com/png.latex?X"> has the potential to tell you something about <img src="https://latex.codecogs.com/png.latex?Y">, <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y"> are not independent. That was easy! For extra credit, <img src="https://latex.codecogs.com/png.latex?p_%7BXY%7D(-1,0)%20=%201/3"> but <img src="https://latex.codecogs.com/png.latex?p_X(-1)p_Y(0)%20=%201/3%20%5Ctimes%202/3%20=%202/9">. Since these are not equal, <img src="https://latex.codecogs.com/png.latex?p_%7BXY%7D%5Cneq%20p_X%20p_Y"> so the joint doesn’t equal the product of the marginals. We didn’t need to check this, but it’s reassuring to see that everything works out as it should.</p>
</section>
<section id="example-2-w-and-z-are-not-independent." class="level3">
<h3 class="anchored" data-anchor-id="example-2-w-and-z-are-not-independent.">Example #2: <img src="https://latex.codecogs.com/png.latex?W"> and <img src="https://latex.codecogs.com/png.latex?Z"> are <em>NOT</em> independent.</h3>
<p>Again, this one is easy: learning that <img src="https://latex.codecogs.com/png.latex?W%20=%20w"> tells us that <img src="https://latex.codecogs.com/png.latex?Z%20=%20w%5E2">. We didn’t know this before, so <img src="https://latex.codecogs.com/png.latex?W"> and <img src="https://latex.codecogs.com/png.latex?Z"> cannot be independent.</p>
</section>
</section>
<section id="relating-the-three-properties" class="level2 page-columns page-full" data-number="6">
<h2 data-number="6" class="anchored" data-anchor-id="relating-the-three-properties"><span class="header-section-number">6</span> Relating the Three Properties</h2>
<p>Now that we’ve described uncorrelatedness, mean independence, and statistical independence, we’re ready to see how these properties relate to one another. Let’s start by reviewing what we learned from the examples given above. In example #1:</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y"> are uncorrelated</li>
<li><img src="https://latex.codecogs.com/png.latex?X"> is mean independent of <img src="https://latex.codecogs.com/png.latex?Y"></li>
<li><img src="https://latex.codecogs.com/png.latex?Y"> is <em>not mean independent</em> of <img src="https://latex.codecogs.com/png.latex?X"></li>
<li><img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y"> are <em>not</em> independent.</li>
</ul>
<p>In example #2, we found that</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?W"> and <img src="https://latex.codecogs.com/png.latex?Z"> are uncorrelated</li>
<li><img src="https://latex.codecogs.com/png.latex?W"> is mean independent of <img src="https://latex.codecogs.com/png.latex?Z">.</li>
<li><img src="https://latex.codecogs.com/png.latex?Z"> is <em>not</em> mean independent of <img src="https://latex.codecogs.com/png.latex?W">.</li>
<li><img src="https://latex.codecogs.com/png.latex?W"> and <img src="https://latex.codecogs.com/png.latex?Z"> are <em>not</em> independent.</li>
</ul>
<p>These are worth remembering, because they are relatively simple and provide a source of <em>counterexamples</em> to help you avoid making tempting but incorrect statements about correlation, mean independence, and statistical independence. For example:</p>
<ol type="1">
<li>Uncorrelatedness does <strong>NOT IMPLY</strong> statistical independence: <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y"> are not independent, but they are uncorrelated. (Ditto for <img src="https://latex.codecogs.com/png.latex?W"> and <img src="https://latex.codecogs.com/png.latex?Z">.)</li>
<li>Mean independence does <strong>NOT IMPLY</strong> statistical independence: <img src="https://latex.codecogs.com/png.latex?W"> is mean independent of <img src="https://latex.codecogs.com/png.latex?Z"> but these random variables are not independent.</li>
<li>Mean independence is <strong>NOT SYMMETRIC</strong>: <img src="https://latex.codecogs.com/png.latex?X"> is mean independent of <img src="https://latex.codecogs.com/png.latex?Y">, but <img src="https://latex.codecogs.com/png.latex?Y"> is not mean independent of <img src="https://latex.codecogs.com/png.latex?X">.</li>
</ol>
<p>Now that we have a handle on what’s <em>not true</em>, let’s see what can be said about correlation, mean independence, and statistical independence.</p>
<section id="uncorrelatedness-and-statistical-independence-are-symmetric" class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="uncorrelatedness-and-statistical-independence-are-symmetric">Uncorrelatedness and Statistical Independence are Symmetric</h3>
<p>In the equality <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(XY)%20=%20%5Cmathbb%7BE%7D(X)%20%5Cmathbb%7BE%7D(Y)">, nothing changes if we swap the roles of <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y">; this statement is equivalent to <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(YX)%20=%20%5Cmathbb%7BE%7D(Y)%20%5Cmathbb%7BE%7D(X)">. This shows that uncorrelatedness is <em>symmetric</em>. The same goes for statistical independence: we showed that <img src="https://latex.codecogs.com/png.latex?p_%7BY%7CX%7D%20=%20p_Y"> is equivalent to <img src="https://latex.codecogs.com/png.latex?p_%7BX%7CY%7D%20=%20p_X"> above. In contrast, mean independence is not symmetric: <img src="https://latex.codecogs.com/png.latex?X"> can be mean independent of <img src="https://latex.codecogs.com/png.latex?Y"> without <img src="https://latex.codecogs.com/png.latex?Y"> being mean independent of <img src="https://latex.codecogs.com/png.latex?X">.</p>
<p>Here’s an analogy: uncorrelatedness and independence are like the relation “being biological siblings.” If <img src="https://latex.codecogs.com/png.latex?X"> is the sibling of <img src="https://latex.codecogs.com/png.latex?Y">, then <img src="https://latex.codecogs.com/png.latex?Y"> must be the sibling of <img src="https://latex.codecogs.com/png.latex?X"> because “being siblings” is defined as “having the same parents.” In contrast, mean independence is like the relation “being in love.” Sadly, it’s possible for <img src="https://latex.codecogs.com/png.latex?X"> to be in love with <img src="https://latex.codecogs.com/png.latex?Y"> despite <img src="https://latex.codecogs.com/png.latex?Y"> not being in love with <img src="https://latex.codecogs.com/png.latex?X">.<sup>4</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn4"><p><sup>4</sup>&nbsp;But on the plus side, we got a lot of great pop songs out of the deal!</p></div></div></section>
<section id="statistical-independence-implies-conditional-mean-independence" class="level3">
<h3 class="anchored" data-anchor-id="statistical-independence-implies-conditional-mean-independence">Statistical Independence Implies Conditional Mean Independence</h3>
<p>Statistical independence is the “strongest” of the three properties: it implies both mean independence and uncorrelatedness. We’ll show this in two steps. In the first step, we’ll show that statistical independence implies mean independence. In the second step we’ll show that mean independence implies uncorrelatedness. Then we’ll bring this overly-long blog post to a close! Suppose that <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y"> are discrete random variables. (For the continuous case, replace sums with integrals.) If <img src="https://latex.codecogs.com/png.latex?X"> is statistically independent of <img src="https://latex.codecogs.com/png.latex?Y">, then <img src="https://latex.codecogs.com/png.latex?p_%7BY%7CX%7D%20=%20p_Y"> and <img src="https://latex.codecogs.com/png.latex?p_%7BX%7CY%7D%20=%20p_X">. Hence, <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cmathbb%7BE%7D(Y%7CX=x)%20&amp;%5Cequiv%20%5Csum_%7B%5Ctext%7Ball%20%7D%20y%7D%20y%20%5Ccdot%20p_%7BY%7CX%7D(y%7Cx)%20=%20%5Csum_%7B%5Ctext%7Ball%20%7D%20y%7D%20y%20%5Ccdot%20p_Y(y)%20%5Cequiv%20%5Cmathbb%7BE%7D(Y)%5C%5C%0A%5Cmathbb%7BE%7D(X%7CY=y)%20&amp;%5Cequiv%20%5Csum_%7B%5Ctext%7Ball%20%7D%20x%7D%20x%20%5Ccdot%20p_%7BX%7CY%7D(x%7Cy)%20=%20%5Csum_%7B%5Ctext%7Ball%20%7D%20x%7D%20x%20%5Ccdot%20p_X(x)%20%5Cequiv%20%5Cmathbb%7BE%7D(X)%0A%5Cend%7Baligned%7D%0A"> so <img src="https://latex.codecogs.com/png.latex?Y"> is mean independent of <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?X"> is mean independent of <img src="https://latex.codecogs.com/png.latex?Y">.</p>
<!--**Bonus fact: statistical independence implies $\mathbb{E}[g(X) h(Y)] = \mathbb{E}[g(X)]\mathbb{E}[h(Y)]$ for any functions $h$ and $g$.**-->
</section>
<section id="conditional-mean-independence-implies-uncorrelatedness" class="level3">
<h3 class="anchored" data-anchor-id="conditional-mean-independence-implies-uncorrelatedness">Conditional Mean Independence Implies Uncorrelatedness</h3>
<p>If <em>either</em> <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y%7CX)%20=%20%5Cmathbb%7BE%7D(Y)"> <em>or</em> <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(X%7CY)%20=%20%5Cmathbb%7BE%7D(X)">, then <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y"> are uncorrelated. To show this, we use the <strong>Law of Iterated Expectations</strong> and the “taking out what is known” property, along with the fact that <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(X)"> and <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y)"> are constants. Suppose first that <img src="https://latex.codecogs.com/png.latex?Y"> is mean independent of <img src="https://latex.codecogs.com/png.latex?X">, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y%7CX)%20=%20%5Cmathbb%7BE%7D(Y)">. Then, taking iterated expectations over <img src="https://latex.codecogs.com/png.latex?X">, <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BE%7D(XY)%20=%20%5Cmathbb%7BE%7D%5B%5Cmathbb%7BE%7D(XY%7CX)%5D%20=%20%5Cmathbb%7BE%7D%5BX%20%5Cmathbb%7BE%7D(Y%7CX)%5D%20=%20%5Cmathbb%7BE%7D%5BX%20%5Cmathbb%7BE%7D(Y)%5D%20=%20%5Cmathbb%7BE%7D(X)%20%5Cmathbb%7BE%7D(Y).%0A"> Alternatively, suppose that <img src="https://latex.codecogs.com/png.latex?X"> is mean independent of <img src="https://latex.codecogs.com/png.latex?Y">, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(X%7CY)%20=%20%5Cmathbb%7BE%7D(X)">. Then, taking iterated expectations over <img src="https://latex.codecogs.com/png.latex?Y">, <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BE%7D(XY)%20=%20%5Cmathbb%7BE%7D%5B%5Cmathbb%7BE%7D(XY%7CY)%5D%20=%20%5Cmathbb%7BE%7D%5BY%5Cmathbb%7BE%7D(X%7CY)%5D%20=%20%5Cmathbb%7BE%7D%5BY%20%5Cmathbb%7BE%7D(X)%5D%20=%20%5Cmathbb%7BE%7D(Y)%20%5Cmathbb%7BE%7D(X).%0A"> Therefore, if either <img src="https://latex.codecogs.com/png.latex?X"> is mean independent of <img src="https://latex.codecogs.com/png.latex?Y">, or <img src="https://latex.codecogs.com/png.latex?Y"> is mean independent of <img src="https://latex.codecogs.com/png.latex?X">, or both, then <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y"> are uncorrelated. Since statistical independence implies mean independence, it follows that statistical independence implies uncorrelatedness. And we’re finally done!</p>
</section>
</section>
<section id="summary" class="level2" data-number="7">
<h2 data-number="7" class="anchored" data-anchor-id="summary"><span class="header-section-number">7</span> Summary</h2>
<p>In this post we have shown that:</p>
<ul>
<li>Statistical Independence <img src="https://latex.codecogs.com/png.latex?%5Cimplies"> Mean Independence <img src="https://latex.codecogs.com/png.latex?%5Cimplies"> Uncorrelatedness.</li>
<li>Uncorrelatedness does not imply mean independence or statistical independence.</li>
<li>Mean independence does not imply statistical independence.</li>
<li>Statistical independence and correlation are symmetric; mean independence is not.</li>
</ul>
<p>Reading the figure from the very beginning of this post from top to bottom: statistical independence is the <em>strongest</em> notion, followed by mean independence, followed by uncorrelatedness.</p>


</section>


 ]]></description>
  <category>econometrics</category>
  <category>teaching</category>
  <guid>https://www.econometrics.blog/post/why-econometrics-is-confusing-part-ii-the-independence-zoo/</guid>
  <pubDate>Sun, 01 Jan 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>From the Poisson Distribution to Stirling’s Approximation</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/from-the-poisson-distribution-to-stirling-s-approximation/</link>
  <description><![CDATA[ 




<p>The <a href="https://en.wikipedia.org/wiki/Poisson_distribution">Poisson distribution</a> is the most famous probability model for <em>counts</em>, non-negative integer values. Many real-world phenomena are well approximated by this distribution, including the <a href="https://www.jstor.org/stable/41138751">number of German bombs</a> that landed in 1/4km grid squares in south London during WWII. Formally, we say that a discrete random variable <img src="https://latex.codecogs.com/png.latex?X"> follows a Poisson distribution with rate parameter <img src="https://latex.codecogs.com/png.latex?%5Cmu%20%3E%200">, abbreviated <img src="https://latex.codecogs.com/png.latex?X%20%5Csim%20%5Ctext%7BPoisson%7D(%5Cmu)">, if <img src="https://latex.codecogs.com/png.latex?X"> has support set <img src="https://latex.codecogs.com/png.latex?%5C%7B0,%201,%202,%20...%5C%7D"> and probability mass function <img src="https://latex.codecogs.com/png.latex?%0Ap(x)%20%5Cequiv%20%5Cmathbb%7BP%7D(X=x)%20=%20%5Cfrac%7Be%5E%7B-%5Cmu%20%7D%5Cmu%5Ex%7D%7Bx!%7D.%0A"> Using some <a href="https://drive.explaineverything.com/thecode/CHAKTHR">clever algebra with sums</a> it’s not too hard to show that the rate parameter, <img src="https://latex.codecogs.com/png.latex?%5Cmu">, is <em>both the mean and the variance</em> of <img src="https://latex.codecogs.com/png.latex?X">.</p>
<section id="numerical-problems-try-taking-logs." class="level2 page-columns page-full" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="numerical-problems-try-taking-logs."><span class="header-section-number">1</span> Numerical problems? Try taking logs.</h2>
<p>Now, suppose that we wanted to plot the pmf of a Poisson RV with rate <img src="https://latex.codecogs.com/png.latex?%5Cmu%20=%20171">. The R function for the pmf of a Poisson RV is <code>dpois()</code>, so we can make our plot as follows (indicating the rate parameter as a vertical line)</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dpois</span>(x, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">171</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, p)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">171</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylab</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Poisson(171) pmf'</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/from-the-poisson-distribution-to-stirling-s-approximation/index_files/figure-html/unnamed-chunk-2-1.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>For such a large value of <img src="https://latex.codecogs.com/png.latex?%5Cmu">, this distribution looks decidedly bell-shaped. And indeed, it turns out to be extremely well-approximated by a normal distribution, as we’ll see below. It’s also clear that <img src="https://latex.codecogs.com/png.latex?X"> is most likely to take on a value relatively close to 171. We can use <code>dpois()</code> to calculate the exact probability that <img src="https://latex.codecogs.com/png.latex?X%20=%20171"> as follows: the answer is just over 3%.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dpois</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">171</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">171</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.03049301</code></pre>
</div>
</div>
<p>Now let’s try to calculate exactly the same probability <em>by hand</em>, that is by using the formula for the Poisson pmf from above.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">my_dpois <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x, mu) {</span>
<span id="cb4-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>mu) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> mu<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factorial</span>(x)</span>
<span id="cb4-3">}</span>
<span id="cb4-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">my_dpois</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">171</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">171</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] NaN</code></pre>
</div>
</div>
<p>What gives?! The abbreviation <code>NaN</code> stands for “not a number.” The problem in this case is that both the numerator and denominator of the fraction inside of <code>my_dpois()</code> evaluate to infinity when <code>mu</code> and <code>x</code> are 171, and the ratio <img src="https://latex.codecogs.com/png.latex?%5Cinfty/%5Cinfty"> is undefined.<sup>1</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;There’s an important but subtle difference between <code>NA</code> and <code>NaN</code>. The former is synonymous with “missing.” If <code>x</code> equals <code>NA</code> this means “we don’t know the value of <code>x</code>.” If instead <code>x</code> equals <code>NaN</code>, this means “<code>x</code> isn’t missing, but it’s not a well-defined numeric value either.”</p></div></div><div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">numerator =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">171</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">171</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">171</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">denominator =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factorial</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">171</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  numerator denominator 
        Inf         Inf </code></pre>
</div>
</div>
<p>As I discussed in an <a href="https://www.econometrics.blog/post/street-fighting-numerical-analysis-part-1/">earlier post</a>, computers can only store a finite number of distinct numeric values. It’s not literally true that <code>factorial(171)</code> equals <img src="https://latex.codecogs.com/png.latex?%5Cinfty">. What’s really going on here is that <code>factorial(171)</code> is <em>such a large number</em> that it can’t be stored as a <a href="https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html">floating-point number</a>. In this case there’s a very simple fix. If you haven’t seen this trick before, it’s a helpful one to keep up your sleeves: <strong>if you run into numerical problems with very large or very small values, try taking logs.</strong><sup>2</sup> The log of the Poisson pmf is simply <img src="https://latex.codecogs.com/png.latex?%0A%5Clog%20p(x)%20=%20-%5Cmu%20+%20x%20%5Clog(%5Cmu)%20-%20%5Clog(x!).%0A"> R even has a convenient, built-in function for evaluating the natural log of a factorial: <code>lfactorial()</code>. Now we can compute the log of our desired probability as follows:</p>
<div class="no-row-height column-margin column-container"><div id="fn2"><p><sup>2</sup>&nbsp;Unless otherwise specified log always means “natural logarithm” on this blog :)</p></div></div><div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">171</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">171</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">171</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lfactorial</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">171</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -3.490258</code></pre>
</div>
</div>
<p>To obtain the probability, simply exponentiate:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">171</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">171</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">171</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lfactorial</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">171</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.03049301</code></pre>
</div>
</div>
<p>Of course this just passes the buck to <code>lfactorial()</code>. So how does this mysterious function work? The bad news is that I’m not going to tell you; the good news is that I’m going to show you something <em>even better</em>, namely <a href="https://en.wikipedia.org/wiki/Stirling%27s_approximation">Stirling’s approximation</a>: a way to understand how <img src="https://latex.codecogs.com/png.latex?n!"> behaves <em>qualitatively</em> that turns out to give a pretty darned good approximation to <code>lfactorial()</code>. This may seem like an odd topic for a blog devoted to econometrics and statistics, so allow me to offer a few words of justification. First, computations involving <img src="https://latex.codecogs.com/png.latex?n!"> come up all the time in applied work. Second, it can be extremely helpful for certain theoretical arguments to have good approximations to <img src="https://latex.codecogs.com/png.latex?n!"> for large values of <img src="https://latex.codecogs.com/png.latex?n">. Finally, and most importantly from my perspective, the heuristic argument I’ll use below relies on none other than the <a href="https://www.econometrics.blog/post/thirty-isn-t-the-magic-number/">central limit theorem</a>. So even if you’ve seen a more traditional proof of Stirling’s approximation, I hope you’ll enjoy this alternative approach.<sup>3</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn3"><p><sup>3</sup>&nbsp;I first came across this argument from the late David MacKay’s fantastic book <a href="http://www.inference.org.uk/mackay/itila/book.html">Information Theory, Inference, and Learning Algorithms</a>. His book on <a href="http://www.withouthotair.com/">sustainable energy</a>, while a bit out-of-date at this point, is also spectacularly good.</p></div></div></section>
<section id="stirlings-approximation" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="stirlings-approximation"><span class="header-section-number">2</span> Stirling’s Approximation</h2>
<p>The key step in our argument is to show that the pmf of a <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BPoisson%7D(%5Cmu)"> random variable is well-approximated by the <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BNormal%7D(%5Cmu,%20%5Cmu)"> density. This explains the bell-shaped curve that we plotted above. To obtain this result, we’ll use the central limit theorem. But there is one fact that you will need to take on faith if you don’t already know it: if <img src="https://latex.codecogs.com/png.latex?X_1%20%5Csim%20%5Ctext%7BPoisson%7D(%5Cmu_1)"> is independent of <img src="https://latex.codecogs.com/png.latex?X_2%20%5Csim%20%5Ctext%7BPoisson%7D(%5Cmu_2)"> then <img src="https://latex.codecogs.com/png.latex?X_1%20+%20X_2%20%5Csim%20%5Ctext%7BPoisson%7D(%5Cmu_1%20+%20%5Cmu_2)">. Proceeding <a href="https://en.wikipedia.org/wiki/Mathematical_induction">by induction</a> we can view a Poisson(171) random variable as the sum of 171 independent Poisson(1) random variables. More generally, we can view a Poisson RV with rate parameter <img src="https://latex.codecogs.com/png.latex?n"> as the sum of <img src="https://latex.codecogs.com/png.latex?n"> iid Poisson(1) random variables. By the <a href="https://www.econometrics.blog/post/thirty-isn-t-the-magic-number/">central limit theorem</a>, it follows that <img src="https://latex.codecogs.com/png.latex?%0A%5Csqrt%7Bn%7D(%5Cbar%7BX%7D_n%20-%201)%20%5Crightarrow_d%20%5Ctext%7BN%7D(0,1)%0A"> since the mean and variance of a Poisson(1) RV are both equal to one. From a practical perspective, this means that <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7Bn%7D(%5Cbar%7BX%7D_n%20-%201)"> is approximately equal to <img src="https://latex.codecogs.com/png.latex?Z">, a standard normal random variable. Re-arranging, <img src="https://latex.codecogs.com/png.latex?%0AX_1%20+%20X_2%20+%20...%20+%20X_n%20=%20n%5Cbar%7BX%7D_n%20=%20n%20+%20%5Csqrt%7Bn%7D%20%5Ctimes%20%5B%5Csqrt%7Bn%7D(%5Cbar%7BX%7D_n%20-%201)%5D%20%5Capprox%20n%20+%20%5Csqrt%7Bn%7D%20Z%0A"> and <img src="https://latex.codecogs.com/png.latex?n%20+%20%5Csqrt%7Bn%7D%20Z"> is simply a <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BN%7D(n,%20n)"> random variable! This is a quick way of seeing why the <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BPoisson%7D(%5Cmu)"> distribution is well-approximated by the <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BN%7D(%5Cmu,%20%5Cmu)"> distribution when <img src="https://latex.codecogs.com/png.latex?%5Cmu"> is large.</p>
<p>Now let’s run with this. As we just saw, for large <img src="https://latex.codecogs.com/png.latex?%5Cmu"> the Poisson<img src="https://latex.codecogs.com/png.latex?(%5Cmu)"> pmf is well-approximated by the Normal<img src="https://latex.codecogs.com/png.latex?(%5Cmu,%20%5Cmu)"> density: <img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7Be%5E%7B-%5Cmu%7D%5Cmu%5Ex%7D%7Bx!%7D%20%5Capprox%20%5Cfrac%7B1%7D%7B%5Csqrt%7B2%5Cpi%20%5Cmu%7D%7D%20%5Cexp%5Cleft%5C%7B%20-%5Cfrac%7B1%7D%7B2%7D%5Cleft(%20%5Cfrac%7Bx%20-%20%5Cmu%7D%7B%5Csqrt%7B%5Cmu%7D%7D%5Cright)%5E2%5Cright%5C%7D%0A"> This approximation is particularly accurate for <img src="https://latex.codecogs.com/png.latex?x"> near the <em>mean</em>. This is convenient, because substituting <img src="https://latex.codecogs.com/png.latex?%5Cmu"> for <img src="https://latex.codecogs.com/png.latex?x"> considerably simplifies the right hand side: <img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7Be%5E%7B-%5Cmu%7D%5Cmu%5E%5Cmu%7D%7B%5Cmu!%7D%20%5Capprox%20%5Cfrac%7B1%7D%7B%5Csqrt%7B2%5Cpi%5Cmu%7D%7D%0A"> Re-arranging, we obtain <img src="https://latex.codecogs.com/png.latex?%0A%5Cmu!%20%5Capprox%20%5Cmu%5E%5Cmu%20e%5E%7B-%5Cmu%7D%20%5Csqrt%7B2%20%5Cpi%20%5Cmu%7D%0A"> Taking logs of both sides gives: <img src="https://latex.codecogs.com/png.latex?%0A%5Clog(%5Cmu!)%20%5Capprox%20%5Cmu%20%5Clog(%5Cmu)%20-%20%5Cmu%20+%20%5Cfrac%7B1%7D%7B2%7D%20%5Clog(2%20%5Cpi%20%5Cmu)%0A"> Writing this with <img src="https://latex.codecogs.com/png.latex?n"> in place of <img src="https://latex.codecogs.com/png.latex?%5Cmu"> gives the following: <img src="https://latex.codecogs.com/png.latex?%0A%5Clog(n!)%20%5Capprox%20n%20%5Clog(n)%20-%20n%20+%20%5Cfrac%7B1%7D%7B2%7D%20%5Clog(2%20%5Cpi%20n)%0A"> This is called <em>Stirling’s Approximation</em>. The usual way of writing this excludes the <img src="https://latex.codecogs.com/png.latex?%5Clog(2%5Cpi%20n)/2"> term, yielding <img src="https://latex.codecogs.com/png.latex?%5Clog(n!)%20%5Capprox%20n%5Clog(n)%20-%20n">, which is fairly easy to remember. Including the extra term, however, gives increased accuracy for smaller values of <img src="https://latex.codecogs.com/png.latex?n">. While I haven’t formally proved this, it turns out that <img src="https://latex.codecogs.com/png.latex?%0A%5Clog(n!)%20%5Csim%20n%20%5Clog(n)%20-%20n%20+%20%5Cfrac%7B1%7D%7B2%7D%20%5Clog(2%20%5Cpi%20n)%0A"> as <img src="https://latex.codecogs.com/png.latex?n%20%5Crightarrow%20%5Cinfty">. In other words, the ratio of the LHS and RHS tends to one in the large <img src="https://latex.codecogs.com/png.latex?n"> limit. Perhaps surprisingly, this approximation is extremely accurate even for fairly small values of <img src="https://latex.codecogs.com/png.latex?n">, as we can see by comparing it against <code>lfactorial()</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">stirling1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(n) n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(n) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> n</span>
<span id="cb12-2">stirling2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(n) n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(n) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> n)</span>
<span id="cb12-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Stirling1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stirling1</span>(n),</span>
<span id="cb12-5">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Stirling2 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stirling2</span>(n),</span>
<span id="cb12-6">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">R =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lfactorial</span>(n)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-7">  knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: right;">n</th>
<th style="text-align: right;">Stirling1</th>
<th style="text-align: right;">Stirling2</th>
<th style="text-align: right;">R</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">1</td>
<td style="text-align: right;">-1.000</td>
<td style="text-align: right;">-0.081</td>
<td style="text-align: right;">0.000</td>
</tr>
<tr class="even">
<td style="text-align: right;">2</td>
<td style="text-align: right;">-0.614</td>
<td style="text-align: right;">0.652</td>
<td style="text-align: right;">0.693</td>
</tr>
<tr class="odd">
<td style="text-align: right;">3</td>
<td style="text-align: right;">0.296</td>
<td style="text-align: right;">1.764</td>
<td style="text-align: right;">1.792</td>
</tr>
<tr class="even">
<td style="text-align: right;">4</td>
<td style="text-align: right;">1.545</td>
<td style="text-align: right;">3.157</td>
<td style="text-align: right;">3.178</td>
</tr>
<tr class="odd">
<td style="text-align: right;">5</td>
<td style="text-align: right;">3.047</td>
<td style="text-align: right;">4.771</td>
<td style="text-align: right;">4.787</td>
</tr>
<tr class="even">
<td style="text-align: right;">6</td>
<td style="text-align: right;">4.751</td>
<td style="text-align: right;">6.565</td>
<td style="text-align: right;">6.579</td>
</tr>
<tr class="odd">
<td style="text-align: right;">7</td>
<td style="text-align: right;">6.621</td>
<td style="text-align: right;">8.513</td>
<td style="text-align: right;">8.525</td>
</tr>
<tr class="even">
<td style="text-align: right;">8</td>
<td style="text-align: right;">8.636</td>
<td style="text-align: right;">10.594</td>
<td style="text-align: right;">10.605</td>
</tr>
<tr class="odd">
<td style="text-align: right;">9</td>
<td style="text-align: right;">10.775</td>
<td style="text-align: right;">12.793</td>
<td style="text-align: right;">12.802</td>
</tr>
<tr class="even">
<td style="text-align: right;">10</td>
<td style="text-align: right;">13.026</td>
<td style="text-align: right;">15.096</td>
<td style="text-align: right;">15.104</td>
</tr>
<tr class="odd">
<td style="text-align: right;">11</td>
<td style="text-align: right;">15.377</td>
<td style="text-align: right;">17.495</td>
<td style="text-align: right;">17.502</td>
</tr>
<tr class="even">
<td style="text-align: right;">12</td>
<td style="text-align: right;">17.819</td>
<td style="text-align: right;">19.980</td>
<td style="text-align: right;">19.987</td>
</tr>
<tr class="odd">
<td style="text-align: right;">13</td>
<td style="text-align: right;">20.344</td>
<td style="text-align: right;">22.546</td>
<td style="text-align: right;">22.552</td>
</tr>
<tr class="even">
<td style="text-align: right;">14</td>
<td style="text-align: right;">22.947</td>
<td style="text-align: right;">25.185</td>
<td style="text-align: right;">25.191</td>
</tr>
<tr class="odd">
<td style="text-align: right;">15</td>
<td style="text-align: right;">25.621</td>
<td style="text-align: right;">27.894</td>
<td style="text-align: right;">27.899</td>
</tr>
<tr class="even">
<td style="text-align: right;">16</td>
<td style="text-align: right;">28.361</td>
<td style="text-align: right;">30.667</td>
<td style="text-align: right;">30.672</td>
</tr>
<tr class="odd">
<td style="text-align: right;">17</td>
<td style="text-align: right;">31.165</td>
<td style="text-align: right;">33.500</td>
<td style="text-align: right;">33.505</td>
</tr>
<tr class="even">
<td style="text-align: right;">18</td>
<td style="text-align: right;">34.027</td>
<td style="text-align: right;">36.391</td>
<td style="text-align: right;">36.395</td>
</tr>
<tr class="odd">
<td style="text-align: right;">19</td>
<td style="text-align: right;">36.944</td>
<td style="text-align: right;">39.335</td>
<td style="text-align: right;">39.340</td>
</tr>
<tr class="even">
<td style="text-align: right;">20</td>
<td style="text-align: right;">39.915</td>
<td style="text-align: right;">42.331</td>
<td style="text-align: right;">42.336</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
<section id="epilogue" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="epilogue"><span class="header-section-number">3</span> Epilogue</h2>
<p>I have a bad habit of trying to add a “moral” or “lesson” to the end of my posts, but I suppose there’s no point trying to break the habit today! While there are easier ways to derive Stirling’s approximation, there are two things I enjoy about this one. First, we get a more accurate approximation than <img src="https://latex.codecogs.com/png.latex?n%20%5Clog(n)%20-%20n"> with practically no effort. Second, making unexpected connections between facts that we already know both <em>deepens our understanding</em> and helps us “compress” information. If you ever forget Stirling’s approximation, now you know how to very quickly re-derive it on the spot!</p>


</section>


 ]]></description>
  <category>statistics</category>
  <category>computing</category>
  <guid>https://www.econometrics.blog/post/from-the-poisson-distribution-to-stirling-s-approximation/</guid>
  <pubDate>Fri, 18 Nov 2022 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Local Asymptotics: The Simplest Possible Example</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/local-asymptotics-the-simplest-possible-example/</link>
  <description><![CDATA[ 




<p>If you study enough econometrics, you will eventually come across an asymptotic argument in which some parameter is assumed to <em>change with sample size</em>. This peculiar notion goes by a variety of names including “Pitman drift,” a “sequence of local alternatives,” and “local mis-specification,” and crops up in a wide range of problems from weak instruments, to model selection, to power analysis.<sup>1</sup> Whatever you choose to call it, the idea of a parameter that changes with sample size is <em>bizarre</em>, and I remember spending weeks trying to understand it when I was a graduate student. How could parameters, fixed quantities that we’re trying to estimate, possibly know anything about our sample size?</p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;I’ve used this idea in my own work on <a href="https://ideas.repec.org/a/eee/econom/v195y2016i2p187-208.html">moment selection</a> and, with <a href="http://minsuchang.com">Minsu Chang</a>, on <a href="https://ideas.repec.org/a/wly/japmet/v33y2018i3p378-397.html">model selection</a>.</p></div></div><p>Do we expect parameters to be smaller when we have more data? Do we expect them to be larger when we have less data? The answer to both questions is a resounding <em>NO</em>. Like all asymptotics, what I will call <em>local asymptotics</em> are nothing more than a thought experiment that we set up for mathematical convenience. Ideally we would derive finite sample results for every problem of interest, but this is rarely possible in practice. For this reason we turn to asymptotic results, such as the central limit theorem. Sometimes this works out OK, and sometimes it’s a <a href="https://www.econometrics.blog/post/don-t-use-the-textbook-ci-for-a-proportion/">disaster</a>. The goal of local asymptotics is to derive results that <em>more closely approximate</em> the finite sample behavior that we can understand from simple examples, in the hope that this will lead to better approximations in more complicated problems. In this post, I’ll illustrate the usefulness of local asymptotics in the simplest example I could think of: a one-sided test for the mean of a normal distribution with known variance. No advanced statistics or econometrics are used below, so even if you found the preceding paragraph off-putting give the rest a go: you may be pleasantly surprised!</p>
<p>Suppose that we observe <img src="https://latex.codecogs.com/png.latex?%0AX_1,%20X_2,%20%5Cdots,%20X_%7Bn%7D%20%5Coverset%7Biid%7D%7B%5Csim%7DN(%5Cmu,%201)%0A"> and want to test <img src="https://latex.codecogs.com/png.latex?H_0%5Ccolon%20%5Cmu%20=%200"> against the one-sided alternative <img src="https://latex.codecogs.com/png.latex?H_1%5Ccolon%20%5Cmu%20%3E0">. In this admittedly very simple example, the Econometrics 101 test statistic is <img src="https://latex.codecogs.com/png.latex?%0AT_%7Bn%7D%20=%20%5Csqrt%7Bn%7D%20%5Cbar%7BX%7D_%7Bn%7D%20%5Csim%20N%5Cleft(%5Cmu%20%5Csqrt%7Bn%7D,%201%5Cright)%0A"> where <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D_%7Bn%7D"> is the sample mean. We reject when <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7Bn%7D%20%5Cbar%7BX%7D_%7Bn%7D%3Ez_%7B1-%5Calpha%7D"> where <img src="https://latex.codecogs.com/png.latex?z_%7B1-%5Calpha%7D"> is the <img src="https://latex.codecogs.com/png.latex?1-%5Calpha"> quantile of a standard normal distribution. Let’s calculate the <em>power</em> of this test: the probability of rejecting the null hypothesis <em>given that it is false</em>. We find that <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%20%20%5Cmbox%7BPower%7D(T_%7Bn%7D)%20&amp;=%20P%5Cleft(%5Csqrt%7Bn%7D%20%5Cbar%7BX%7D_%7Bn%7D%3Ez_%7B1-%5Calpha%7D%5Cright)%20=%20P%5Cleft(Z%20+%20%5Cmu%5Csqrt%7Bn%7D%20%3Ez_%7B1-%5Calpha%7D%5Cright)%5C%5C%0A%20%20&amp;=P%5Cleft(Z%20%3Ez_%7B1-%5Calpha%7D%20-%20%5Cmu%5Csqrt%7Bn%7D%5Cright)%20=%201%20-%20%5CPhi%5Cleft(z_%7B1-%5Calpha%7D%20-%20%5Cmu%5Csqrt%7Bn%7D%5Cright)%0A%20%20%5Cend%7Baligned%7D%0A"> where <img src="https://latex.codecogs.com/png.latex?Z"> is a standard normal random variable and <img src="https://latex.codecogs.com/png.latex?%5CPhi"> is the standard normal CDF.</p>
<p>Now suppose we decided to do something completely crazy: throw away half our sample. Let <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D_%7Bn/2%7D"> denote the sample mean based on observations <img src="https://latex.codecogs.com/png.latex?1,%202,%20%5Cdots,%20%5Clfloor%20n/2%20%5Crfloor"> <em>only</em>, where <img src="https://latex.codecogs.com/png.latex?%5Clfloor%20x%20%5Crfloor"> denotes the <a href="https://en.wikipedia.org/wiki/Floor_and_ceiling_functions">floor function</a>, i.e.&nbsp;the greatest integer less than or equal to <img src="https://latex.codecogs.com/png.latex?x">.<sup>2</sup> We can still construct a perfectly valid test with size <img src="https://latex.codecogs.com/png.latex?%5Calpha"> as follows. Define <img src="https://latex.codecogs.com/png.latex?%0AT_%7Bn/2%7D%20=%20%5Csqrt%7B%5Clfloor%20n/2%20%5Crfloor%20%7D%20%5Cbar%7BX%7D_%7Bn/2%7D%20%5Csim%20N%5Cleft(%5Cmu%20%5Csqrt%7B%5Clfloor%20n/2%20%5Crfloor%20%7D,%201%5Cright)%0A"> and reject if <img src="https://latex.codecogs.com/png.latex?T_%7Bn/2%7D%20%3E%20z_%7B1-%5Calpha%7D">. But there’s an obvious problem here: there <em>must</em> be a cost for throwing away perfectly good data. Indeed, if we calculate the power for this crazy test, we’ll find that it’s <em>strictly lower</em> than that of the sensible test based on the full sample. In particular, <img src="https://latex.codecogs.com/png.latex?%5Cmbox%7BPower%7D(T_%7Bn/2%7D)%20=%201%20-%20%5CPhi%5Cleft(z_%7B1-%5Calpha%7D%20-%20%5Cmu%5Csqrt%7B%5Clfloor%20n/2%20%5Crfloor%20%7D%5Cright)"> using the same argument as above with <img src="https://latex.codecogs.com/png.latex?%5Clfloor%20n/2%20%5Crfloor"> in place of <img src="https://latex.codecogs.com/png.latex?n">. Unsurprisingly, it turns out to be a bad idea to throw away half of your data!</p>
<div class="no-row-height column-margin column-container"><div id="fn2"><p><sup>2</sup>&nbsp;If you don’t like this, ignore it and pretend that <img src="https://latex.codecogs.com/png.latex?n"> is an even number!</p></div></div><p>Now, for an example this simple we’d never resort to asymptotics, but suppose we did. How do these two tests compare as the sample size goes to infinity? The asymptotic size in this example is the same as the finite-sample size since we know the exact sampling distribution of the test statistics under the null and neither depends on sample size. But what about the power? We have, <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%20%20%5Clim_%7Bn%5Crightarrow%20%5Cinfty%7D%20%5Cmbox%7BPower%7D(T_%7Bn%7D)%20&amp;=%20%5Clim_%7Bn%5Crightarrow%20%5Cinfty%7D%5Cleft%5B1%20-%20%5CPhi%5Cleft(z_%7B1-%5Calpha%7D%20-%20%5Cmu%5Csqrt%7Bn%7D%5Cright)%20%5Cright%5D%20=%201%5C%5C%0A%20%20%5Clim_%7Bn%5Crightarrow%20%5Cinfty%7D%20%5Cmbox%7BPower%7D(T_%7Bn/2%7D)%20&amp;=%20%5Clim_%7Bn%5Crightarrow%20%5Cinfty%7D%5Cleft%5B1%20-%20%5CPhi%5Cleft(z_%7B1-%5Calpha%7D%20-%20%5Cmu%5Csqrt%7B%5Clfloor%20n/2%20%5Crfloor%20%7D%5Cright)%20%5Cright%5D%20=%201%0A%20%20%5Cend%7Baligned%7D%0A"> In other words, both of these tests are <em>consistent</em>: as the sample size goes to infinity, the power goes to one. Think about this for a moment: we know that for <em>any</em> fixed sample size a test based on the full sample is <em>strictly more powerful</em> but in the limit this difference disappears. This strongly suggests that something is wrong with comparing two tests on the basis of their asymptotic power. Clearly the second test is worse than the first, but the asymptotics obscure this.</p>
<p>You might object that I’ve cooked up a particularly perverse example, but it turns out that this phenomenon is quite general. It’s easy to find consistent tests, in fact it’s difficult to find tests that <em>aren’t</em> consistent. But we know from simulation studies that not all consistent tests are created equal: some have <em>much</em> better finite sample power than others and it’s ultimately finite sample performance that we care about. One way around this problem would be to only compare the finite-sample properties of different tests and never use asymptotics. But we almost <em>never</em> know the exact sampling distribution of our test statistics.<sup>3</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn3"><p><sup>3</sup>&nbsp;This blog post is a very special exception created for pedagogical purposes :)</p></div></div><p>This is where <em>local alternatives</em> come in. Rather than evaluating our tests against a <em>fixed</em> alternative <img src="https://latex.codecogs.com/png.latex?%5Cmu">, suppose we were to evaluate it against a <em>sequence</em> of <em>local</em> alternatives that <em>drift towards the null</em> at rate <img src="https://latex.codecogs.com/png.latex?n%5E%7B-1/2%7D">. In other words, our alternative becomes <img src="https://latex.codecogs.com/png.latex?H_%7B1,n%7D%20%5Ccolon%20%5Cmu%20=%20%5Cdelta%20/%20%5Csqrt%7Bn%7D"> where, for this one-sided test, <img src="https://latex.codecogs.com/png.latex?%5Cdelta%20%3E%200">. If we substitute <img src="https://latex.codecogs.com/png.latex?%5Cdelta/%5Csqrt%7Bn%7D"> for <img src="https://latex.codecogs.com/png.latex?%5Cmu"> and take the limit as <img src="https://latex.codecogs.com/png.latex?n%5Crightarrow%20%5Cinfty">, we find <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Clim_%7Bn%5Crightarrow%20%5Cinfty%7D%20%5Cmbox%7BPower%7D(T_%7Bn%7D)%20&amp;=%20%5Clim_%7Bn%5Crightarrow%20%5Cinfty%7D%5Cleft%5B1%20-%20%5CPhi%5Cleft(z_%7B1-%5Calpha%7D%20-%20%5Cfrac%7B%5Cdelta%7D%7B%5Csqrt%7Bn%7D%7D%5Csqrt%7Bn%7D%5Cright)%20%5Cright%5D%5C%5C%0A&amp;=%201%20-%20%5CPhi%5Cleft(z_%7B1-%5Calpha%7D%20-%20%5Cdelta%20%5Cright)%0A%5Cend%7Baligned%7D%0A"> and similarly <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Clim_%7Bn%5Crightarrow%20%5Cinfty%7D%20%5Cmbox%7BPower%7D(T_%7Bn/2%7D)%20&amp;=%20%5Clim_%7Bn%5Crightarrow%20%5Cinfty%7D%5Cleft%5B1%20-%20%5CPhi%5Cleft(z_%7B1-%5Calpha%7D%20-%20%5Cfrac%7B%5Cdelta%7D%7B%5Csqrt%7Bn%7D%7D%5Csqrt%7B%5Clfloor%20n/2%20%5Crfloor%20%7D%5Cright)%20%5Cright%5D%5C%5C%0A&amp;=%201%20-%20%5CPhi%5Cleft(z_%7B1-%5Calpha%7D%20-%20%5Cfrac%7B%5Cdelta%7D%7B%5Csqrt%7B2%7D%7D%20%5Cright)%0A%5Cend%7Baligned%7D%0A"> Wow! Our problem has disappeared! The asymptotic power of the two tests now differs in essentially the same way as the finite sample power. Also note that the power no longer converges to one. Intuitively, this is because the drifting sequence of alternatives <img src="https://latex.codecogs.com/png.latex?%5Cdelta/%5Csqrt%7Bn%7D"> makes it “harder and harder” to reject the null as the sample size grows by shrinking <em>just fast enough</em> but not so fast that the power goes to zero. This type of calculation is called a <em>local power analysis</em>. A test that has asymptotic power greater than zero in such a setting is said to have “power against local alternatives.”</p>
<p>This example was a bit silly since we already knew the answer. But this is precisely what made it so obvious that local asymptotics make <em>more sense</em> in this setting than fixed-parameter asymptotics. Now that you understand this basic intuition, I hope you’ll feel more confident tackling examples of local asymptotics that come up in the econometrics literature.</p>




 ]]></description>
  <category>econometrics</category>
  <guid>https://www.econometrics.blog/post/local-asymptotics-the-simplest-possible-example/</guid>
  <pubDate>Sat, 12 Nov 2022 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Three Ways of Thinking About Instrumental Variables</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/three-ways-of-thinking-about-instrumental-variables/</link>
  <description><![CDATA[ 




<p>In this post we’ll examine a very simple instrumental variables model from three different perspectives: two familiar and one a bit more exotic. While all three yield the same solution in this particular model, they lead in different directions in more complicated examples. Crucially, each gives us a different way of <em>thinking</em> about the problem of endogeneity and how to solve it.</p>
<section id="the-setup" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="the-setup"><span class="header-section-number">1</span> The Setup</h2>
<p>Consider a simple linear causal model of the form <img src="https://latex.codecogs.com/png.latex?Y%20%5Cleftarrow%20%5Calpha%20+%20%5Cbeta%20X%20+%20U"> where <img src="https://latex.codecogs.com/png.latex?X"> is <strong>endogenous</strong>, i.e.&nbsp;related to the unobserved random variable <img src="https://latex.codecogs.com/png.latex?U">. Our goal is to learn <img src="https://latex.codecogs.com/png.latex?%5Cbeta">, the causal effect of <img src="https://latex.codecogs.com/png.latex?X"> on <img src="https://latex.codecogs.com/png.latex?Y">. To take a simple example, suppose that <img src="https://latex.codecogs.com/png.latex?Y"> is wage and <img src="https://latex.codecogs.com/png.latex?X"> is years of schooling. Then <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> is the causal effect of one additional year of schooling on a person’s wage. The random variable <img src="https://latex.codecogs.com/png.latex?U"> is a catchall, representing all <em>unobserved</em> causes of wage, such as ability, family background, and so on. A linear regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X"> will not allow us to learn <img src="https://latex.codecogs.com/png.latex?%5Cbeta">. For example, if you’re very smart, you will probably find school easier and stay in school longer. But being smarter likely has <em>its own</em> effect on your wage, separate from years of education. Ability is a <strong>confounder</strong> because it causes both years of schooling and wage.</p>
<p>Now suppose that <img src="https://latex.codecogs.com/png.latex?Z"> is an <strong>instrumental variable</strong>: something that is uncorrelated with <img src="https://latex.codecogs.com/png.latex?U"> (<strong>exogenous</strong>) but correlated with <img src="https://latex.codecogs.com/png.latex?X"> (<strong>relevant</strong>). For example, <a href="https://youtu.be/NeAkMcgdWxA?t=2044">a very famous paper</a> pointed out that quarter of birth is correlated with years of schooling in the US and argued that it is unrelated to other causes of wages. Finding a good instrumental variable is very hard in practice. Indeed, <a href="https://youtu.be/NeAkMcgdWxA?t=2528">I remain skeptical</a> that quarter of birth is really unrelated to <img src="https://latex.codecogs.com/png.latex?U">. But that’s a conversation for another day. For the moment, suppose we have a <em>bona fide</em> exogenous and relevant instrument at our disposal. To make things even simpler, suppose that the true causal effect <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> is <strong>homogeneous</strong>, i.e.&nbsp;the same for everyone.</p>
</section>
<section id="st-perspective-the-iv-approach" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="st-perspective-the-iv-approach"><span class="header-section-number">2</span> 1st Perspective: The IV Approach</h2>
<blockquote class="blockquote">
<p>Regress <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?Z"> to find the causal effect of <img src="https://latex.codecogs.com/png.latex?Z"> on <img src="https://latex.codecogs.com/png.latex?Y">. Rescale it to obtain the causal effect of <img src="https://latex.codecogs.com/png.latex?X"> on <img src="https://latex.codecogs.com/png.latex?Y">.</p>
</blockquote>
<p>If <img src="https://latex.codecogs.com/png.latex?Z"> is a valid and relevant instrument, then <img src="https://latex.codecogs.com/png.latex?%0A%5Cbeta_%7B%5Ctext%7BIV%7D%7D%20%5Cequiv%20%5Cfrac%7B%5Ctext%7BCov%7D(Z,Y)%7D%7B%5Ctext%7BCov%7D(Z,X)%7D%20=%20%5Cfrac%7B%5Ctext%7BCov%7D(Z,%20%5Calpha%20+%20%5Cbeta%20X%20+%20U)%7D%7B%5Ctext%7BCov%7D(Z,X)%7D%20%20=%20%5Cfrac%7B%5Cbeta%5Ctext%7BCov%7D(Z,X)%20+%20%5Ctext%7BCov%7D(Z,U)%7D%7B%5Ctext%7BCov%7D(Z,X)%7D%20=%20%5Cbeta%0A"> which is precisely the causal effect we’re after! The ratio of <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(Z,Y)"> to <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(Z,X)"> is called the <strong>instrumental variables</strong> (IV) estimand, but it seems to come out of nowhere. A more intuitive way to write this quantity multiplies the numerator and denominator by <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVar%7D(Z)"> to yield <img src="https://latex.codecogs.com/png.latex?%0A%5Cbeta_%7B%5Ctext%7BIV%7D%7D%20%5Cequiv%20%5Cfrac%7B%5Ctext%7BCov%7D(Z,Y)%7D%7B%5Ctext%7BCov%7D(Z,X)%7D%20=%20%20%5Cfrac%7B%5Ctext%7BCov%7D(Y,Z)/%5Ctext%7BVar%7D(Z)%7D%7B%5Ctext%7BCov%7D(X,Z)/%5Ctext%7BVar%7D(Z)%7D%20%5Cequiv%20%5Cfrac%7B%5Cgamma%7D%7B%5Cpi%7D.%0A"> We see that <img src="https://latex.codecogs.com/png.latex?%5Cbeta_%7B%5Ctext%7BIV%7D%7D"> is the ratio of two <strong>linear regression slopes</strong>: the slope <img src="https://latex.codecogs.com/png.latex?%5Cgamma"> from a regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?Z"> divided by the slope <img src="https://latex.codecogs.com/png.latex?%5Cpi"> from a regression of <img src="https://latex.codecogs.com/png.latex?X"> on <img src="https://latex.codecogs.com/png.latex?Z">. This makes intuitive sense if we think about units. Because <img src="https://latex.codecogs.com/png.latex?Z"> is unrelated to <img src="https://latex.codecogs.com/png.latex?U">, <img src="https://latex.codecogs.com/png.latex?%5Cgamma"> gives the causal effect of <img src="https://latex.codecogs.com/png.latex?Z"> on <img src="https://latex.codecogs.com/png.latex?Y">. If <img src="https://latex.codecogs.com/png.latex?Y"> is measured in dollars and <img src="https://latex.codecogs.com/png.latex?Z"> is measured in miles (e.g.&nbsp;distance to college), then <img src="https://latex.codecogs.com/png.latex?%5Cgamma"> is measured in dollars per mile. If <img src="https://latex.codecogs.com/png.latex?X"> is years of schooling, then <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> should be measured in dollars per year. To convert from dollars/mile to dollars/year, we need to multiply by miles/year or equivalently to divide by years/mile. And indeed, <img src="https://latex.codecogs.com/png.latex?%5Cpi"> is measured in years/mile as required! This is yet another example of my favorite maxim: <strong>most formulas in statistics and econometrics are obvious if you keep track of the units.</strong></p>
</section>
<section id="nd-perspective-the-tsls-approach" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="nd-perspective-the-tsls-approach"><span class="header-section-number">3</span> 2nd Perspective: The TSLS Approach</h2>
<blockquote class="blockquote">
<p>Construct <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BX%7D"> by using <img src="https://latex.codecogs.com/png.latex?Z"> to “clean out” the part of <img src="https://latex.codecogs.com/png.latex?X"> that is correlated with <img src="https://latex.codecogs.com/png.latex?U">. Then regress <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BX%7D">.</p>
</blockquote>
<p>Let <img src="https://latex.codecogs.com/png.latex?%5Cdelta"> be the intercept and <img src="https://latex.codecogs.com/png.latex?%5Cpi"> be the slope from a population linear regression of <img src="https://latex.codecogs.com/png.latex?X"> on <img src="https://latex.codecogs.com/png.latex?Z">. Defining <img src="https://latex.codecogs.com/png.latex?V%20%5Cequiv%20X%20-%20%5Cdelta%20-%20%5Cpi%20Z">, we can write <img src="https://latex.codecogs.com/png.latex?%0AX%20=%20%5Ctilde%7BX%7D%20+%20V,%20%5Cquad%20%5Ctilde%7BX%7D%20%5Cequiv%20%5Cdelta%20+%20%5Cpi%20Z,%20%5Cquad%20%5Cpi%20%5Cequiv%20%5Cfrac%7B%5Ctext%7BCov%7D(X,Z)%7D%7B%5Ctext%7BVar%7D(Z)%7D,%20%5Cquad%0A%5Cdelta%20%5Cequiv%20%5Cmathbb%7BE%7D(X)%20-%20%5Cpi%5Cmathbb%7BE%7D(Z).%0A"> By definition <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BX%7D%20%5Cequiv%20%5Cdelta%20+%20%5Cpi%20Z"> is the <strong>best linear predictor</strong> of <img src="https://latex.codecogs.com/png.latex?X"> based on <img src="https://latex.codecogs.com/png.latex?Z">, in that <img src="https://latex.codecogs.com/png.latex?%5Cdelta"> and <img src="https://latex.codecogs.com/png.latex?%5Cpi"> solve the optimization problem <img src="https://latex.codecogs.com/png.latex?%0A%5Cmin_%7Ba,%20b%7D%20%5Cmathbb%7BE%7D%5B(X%20-%20a%20-%20bZ)%5E2%5D.%0A"> What’s more, <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(Z,V)%20=%200"> <em>by construction</em> since: <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Ctext%7BCov%7D(Z,V)%20&amp;=%20%5Ctext%7BCov%7D(Z,%20X%20-%20%5Cdelta%20-%20%5Cpi%20Z)%20=%20%5Ctext%7BCov%7D(Z,X)%20-%20%5Cpi%20%5Ctext%7BVar%7D(Z)%5C%5C%0A&amp;=%20%5Ctext%7BCov%7D(Z,X)%20-%20%5Cfrac%7B%5Ctext%7BCov%7D(X,Z)%7D%7B%5Ctext%7BVar%7D(Z)%7D%20%5Ctext%7BVar%7D(Z)%20=%200.%0A%5Cend%7Baligned%7D%0A"> And since <img src="https://latex.codecogs.com/png.latex?Z"> is uncorrelated with <img src="https://latex.codecogs.com/png.latex?U">, so is <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BX%7D">: <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BCov%7D(%5Ctilde%7BX%7D,%20U)%20=%20%5Ctext%7BCov%7D(%5Cdelta%20+%20%5Cpi%20Z,%20U)%20=%20%5Cpi%5Ctext%7BCov%7D(Z,U)%20=%200.%0A"> So now we have a variable <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BX%7D"> that is a good predictor of <img src="https://latex.codecogs.com/png.latex?X"> but is uncorrelated with <img src="https://latex.codecogs.com/png.latex?U">. In essence, we’ve used <img src="https://latex.codecogs.com/png.latex?Z"> to “clean out” the endogeneity from <img src="https://latex.codecogs.com/png.latex?X"> and we did this using a <strong>first stage</strong> regression of <img src="https://latex.codecogs.com/png.latex?X"> on <img src="https://latex.codecogs.com/png.latex?Z">. <strong>Two-stage least squares</strong> (TSLS) combines this with a <strong>second stage</strong> regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BX%7D"> to recover <img src="https://latex.codecogs.com/png.latex?%5Cbeta">. To see why this works, substitute <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BX%7D%20+V"> for <img src="https://latex.codecogs.com/png.latex?X"> in the causal model, yielding <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0AY%20&amp;=%20%5Calpha%20+%20%5Cbeta%20X%20+%20U%20=%20%5Calpha%20+%20%5Cbeta%20(%5Ctilde%7BX%7D%20+%20V)%20+%20U%5C%5C%0A&amp;=%20%5Calpha%20+%20%5Cbeta%20%5Ctilde%7BX%7D%20+%20(%5Cbeta%20V%20+%20U)%5C%5C%0A&amp;=%20%5Calpha%20+%20%5Cbeta%20%5Ctilde%7BX%7D%20+%20%5Ctilde%7BU%7D%0A%5Cend%7Baligned%7D%0A"> where we define <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BU%7D%20%5Cequiv%20%5Cbeta%20V%20+%20U">. Finally, since <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Ctext%7BCov%7D(%5Ctilde%7BX%7D,%20%5Ctilde%7BU%7D)%20&amp;=%20%5Ctext%7BCov%7D(%5Ctilde%7BX%7D,%20%5Cbeta%20V%20+%20U)%5C%5C%0A&amp;=%20%5Cbeta%5Ctext%7BCov%7D(%5Ctilde%7BX%7D,%20V)%20+%20%5Ctext%7BCov%7D(%5Ctilde%7BX%7D,%20U)%5C%5C%0A&amp;=%20%5Cbeta%5Ctext%7BCov%7D(%5Cdelta%20+%20%5Cpi%20Z%20,%20V)%20+%200%20%5C%5C%0A&amp;=%20%5Cbeta%5Cpi%5Ctext%7BCov%7D(Z,%20V)%20=%200%0A%5Cend%7Baligned%7D%0A"> a regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BX%7D"> recovers the causal effect <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> of <img src="https://latex.codecogs.com/png.latex?X"> on <img src="https://latex.codecogs.com/png.latex?Y">.</p>
</section>
<section id="rd-perspective-the-control-function-approach" class="level2" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="rd-perspective-the-control-function-approach"><span class="header-section-number">4</span> 3rd Perspective: The Control Function Approach</h2>
<blockquote class="blockquote">
<p>Use <img src="https://latex.codecogs.com/png.latex?Z"> to solve for <img src="https://latex.codecogs.com/png.latex?V">, the part of <img src="https://latex.codecogs.com/png.latex?U"> that is correlated with <img src="https://latex.codecogs.com/png.latex?X">. Then regress <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X"> controlling for <img src="https://latex.codecogs.com/png.latex?V">.</p>
</blockquote>
<p>I’m willing to bet that you haven’t seen this approach before! The so-called <strong>control function</strong> approach starts from the same place as TSLS: the first-stage regression of <img src="https://latex.codecogs.com/png.latex?X"> on <img src="https://latex.codecogs.com/png.latex?Z"> from above, namely <img src="https://latex.codecogs.com/png.latex?%0AX%20=%20%5Cdelta%20+%20%5Cpi%20Z%20+%20V,%20%5Cquad%20%5Ctext%7BCov%7D(Z,V)%20=%200.%0A"> Like the error term <img src="https://latex.codecogs.com/png.latex?U"> from the causal model <img src="https://latex.codecogs.com/png.latex?Y%20%5Cleftarrow%20%5Calpha%20+%20%5Cbeta%20X%20+%20U">, the first stage regression error <img src="https://latex.codecogs.com/png.latex?V"> is unobserved. But as strange as it sounds, <em>imagine</em> running a regression of <img src="https://latex.codecogs.com/png.latex?U"> on <img src="https://latex.codecogs.com/png.latex?V">. Then we would obtain <img src="https://latex.codecogs.com/png.latex?%0AU%20=%20%5Ckappa%20+%20%5Clambda%20V%20+%20%5Cepsilon,%0A%5Cquad%20%5Clambda%20%5Cequiv%20%5Cfrac%7B%5Ctext%7BCov%7D(U,V)%7D%7B%5Ctext%7BVar%7D(V)%7D,%0A%5Cquad%20%5Ckappa%20%5Cequiv%20%5Cmathbb%7BE%7D(U)%20-%20%5Clambda%20%5Cmathbb%7BE%7D(V)%0A"> where <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(V,%20%5Cepsilon)%20=%200"> by construction. Now, since the causal model for <img src="https://latex.codecogs.com/png.latex?Y"> includes an intercept, <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(U)%20=%200">. And since the first-stage linear regression model that defines <img src="https://latex.codecogs.com/png.latex?V"> likewise includes an intercept, <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(V)%20=%200"> as well. This means that <img src="https://latex.codecogs.com/png.latex?%5Ckappa%20=%200"> so the regression of <img src="https://latex.codecogs.com/png.latex?U"> on <img src="https://latex.codecogs.com/png.latex?V"> becomes <img src="https://latex.codecogs.com/png.latex?%0AU%20=%20%5Clambda%20V%20+%20%5Cepsilon,%20%20%5Cquad%20%5Clambda%20%5Cequiv%20%5Cfrac%7B%5Ctext%7BCov%7D(U,V)%7D%7B%5Ctext%7BVar%7D(V)%7D%0A%5Cquad%20%5Ctext%7BCov%7D(V,%20%5Cepsilon)%20=%200.%0A"> Now, substituting for <img src="https://latex.codecogs.com/png.latex?U"> in the causal model gives <img src="https://latex.codecogs.com/png.latex?%0AY%20=%20%5Calpha%20+%20%5Cbeta%20X%20+%20U%20=%20%5Calpha%20+%20%5Cbeta%20X%20+%20%5Clambda%20V%20+%20%5Cepsilon.%0A"> By construction <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(V,%20%5Cepsilon)%20=%200">. And since <img src="https://latex.codecogs.com/png.latex?X%20=%20%5Cdelta%20+%20%5Cpi%20Z%20+%20V">, it follows that <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Ctext%7BCov%7D(X,%5Cepsilon)%20&amp;=%20%5Ctext%7BCov%7D(%5Cdelta%20+%20%5Cpi%20Z%20+%20V,%20%5Cepsilon)%5C%5C%0A&amp;=%20%5Cpi%20%5Ctext%7BCov%7D(Z,%5Cepsilon)%20+%20%5Ctext%7BCov%7D(V,%20%5Cepsilon)%20%5C%5C%0A&amp;=%20%5Cpi%20%5Ctext%7BCov%7D(Z,%20U%20-%20%5Clambda%20V)%20+%200%5C%5C%0A&amp;=%20%5Cpi%20%5Cleft%5B%20%5Ctext%7BCov%7D(Z,U)%20-%20%5Clambda%20%5Ctext%7BCov%7D(Z,V)%5Cright%5D%20=%200.%0A%5Cend%7Baligned%7D%0A"> Therefore, <em>if only we could observe <img src="https://latex.codecogs.com/png.latex?V"></em>, a regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X"> that controls for <img src="https://latex.codecogs.com/png.latex?V"> would allow us to recover the causal effect of interest, namely <img src="https://latex.codecogs.com/png.latex?%5Cbeta">. Such a regression would also give us <img src="https://latex.codecogs.com/png.latex?%5Clambda">. To see why this is interesting, notice that <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Ctext%7BCov%7D(X,U)%20&amp;=%20%5Ctext%7BCov%7D(%5Cdelta%20+%20%5Cpi%20Z%20+%20V,%20U)%20=%20%5Cpi%5Ctext%7BCov%7D(Z,U)%20+%20%5Ctext%7BCov%7D(V,U)%5C%5C%0A&amp;=%200%20+%20%5Ctext%7BCov%7D(V,%20%5Clambda%20V%20+%20%5Cepsilon)%20%5C%5C%0A&amp;=%20%5Clambda%20%5Ctext%7BVar%7D(V).%0A%5Cend%7Baligned%7D%0A"> Since <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVar%7D(V)%20%3E%200">, <img src="https://latex.codecogs.com/png.latex?%5Clambda"> tell us the <strong>direction of endogeneity</strong> in <img src="https://latex.codecogs.com/png.latex?X">. If <img src="https://latex.codecogs.com/png.latex?%5Clambda%20%3E0"> then <img src="https://latex.codecogs.com/png.latex?X"> is positively correlated with <img src="https://latex.codecogs.com/png.latex?U">, if <img src="https://latex.codecogs.com/png.latex?%5Clambda%20%3C%200"> then <img src="https://latex.codecogs.com/png.latex?X"> is negatively correlated with <img src="https://latex.codecogs.com/png.latex?U">, and if <img src="https://latex.codecogs.com/png.latex?%5Clambda%20=%200"> then <img src="https://latex.codecogs.com/png.latex?X"> is exogenous. If <img src="https://latex.codecogs.com/png.latex?U"> is ability and ability has a positive effect on years of schooling, for example, then <img src="https://latex.codecogs.com/png.latex?%5Clambda"> will be positive.</p>
<p>Now it’s time to address the elephant in the room: <img src="https://latex.codecogs.com/png.latex?V"> is <strong>unobserved</strong>! It’s all fine and well to say that if <img src="https://latex.codecogs.com/png.latex?V"> were observed our problems would be solved, but given that it is not in fact observed what are we supposed to do? Here’s where the TSLS first stage regression comes to the rescue. Both <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Z"> are observed, so we can learn <img src="https://latex.codecogs.com/png.latex?%5Cdelta"> and <img src="https://latex.codecogs.com/png.latex?%5Cpi"> by regressing <img src="https://latex.codecogs.com/png.latex?X"> on <img src="https://latex.codecogs.com/png.latex?Z">. Given these coefficients, we can simply solve for the unobserved error: <img src="https://latex.codecogs.com/png.latex?V%20=%20X%20-%20%5Cdelta%20-%20%5Cpi%20Z">. Like TSLS, the control function approach relies crucially on the first stage regression. But whereas TSLS uses it to construct <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BX%7D%20=%20%5Cdelta%20+%20%5Cpi%20Z">, the control function approach uses it to construct <img src="https://latex.codecogs.com/png.latex?V%20=%20X%20-%20%5Cdelta%20-%20%5Cpi%20Z">. We don’t replace <img src="https://latex.codecogs.com/png.latex?X"> with its exogenous component <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BX%7D">; instead we “pull out” the component of <img src="https://latex.codecogs.com/png.latex?U"> that is correlated with <img src="https://latex.codecogs.com/png.latex?X">, namely <img src="https://latex.codecogs.com/png.latex?V">. In effect we control for the “omitted variable” <img src="https://latex.codecogs.com/png.latex?V">, hence the name <strong>control function</strong>.</p>
</section>
<section id="simulating-the-three-approaches" class="level2" data-number="5">
<h2 data-number="5" class="anchored" data-anchor-id="simulating-the-three-approaches"><span class="header-section-number">5</span> Simulating the Three Approaches</h2>
<p>Perhaps that was all a bit abstract. Let’s make it concrete by simulating some data and actually <em>calculating</em> estimates of <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> using each of the three approaches described above. Because this exercise relies on a sample of data rather than a population, estimates will replace parameters and residuals will replace error terms.</p>
<p>To begin, we need to simulate <img src="https://latex.codecogs.com/png.latex?Z"> independently of <img src="https://latex.codecogs.com/png.latex?(U,V)">. For simplicity I’ll make these standard normal and set the correlation between <img src="https://latex.codecogs.com/png.latex?U"> and <img src="https://latex.codecogs.com/png.latex?V"> to 0.5.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1983</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for replicability of pseudo-random draws</span></span>
<span id="cb1-2">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span></span>
<span id="cb1-3">Z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(n)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(mvtnorm)</span>
<span id="cb1-5">cor_mat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span id="cb1-6">                    <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">byrow =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb1-7">errors <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rmvnorm</span>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> cor_mat)</span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(errors)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>           [,1]        [,2]
[1,]  0.1612255 -0.96692422
[2,]  1.4020130  1.55818062
[3,]  1.7212525 -0.01997204
[4,] -0.6972637 -0.68551762
[5,]  1.3471669 -0.01766333
[6,] -1.0441467 -0.23113677</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">U <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> errors[,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb3-2">V <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> errors[,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span>(errors)</span></code></pre></div></div>
</div>
<p>Since this is a simulation we actually <em>can</em> observe <img src="https://latex.codecogs.com/png.latex?U"> and <img src="https://latex.codecogs.com/png.latex?V"> and hence could regress the one on the other. Since I set the standard deviation of both of them equal to one, <img src="https://latex.codecogs.com/png.latex?%5Clambda"> will simply equal the correlation between them, namely 0.5</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(U <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> V <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># exclude an intercept</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>        V 
0.5047334 </code></pre>
</div>
</div>
<p>Excellent! Everything is working as it should. The next step is to generate <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y">. Again to keep things simple, in my simulation I’ll set <img src="https://latex.codecogs.com/png.latex?%5Calpha%20=%20%5Cdelta%20=%200">.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">pi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span></span>
<span id="cb6-2">beta <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span></span>
<span id="cb6-3">X <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> pi <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Z <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> V</span>
<span id="cb6-4">Y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> beta <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> U</span></code></pre></div></div>
</div>
<p>Now we’re ready to run some regressions! We’ll start with an OLS regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X">. This substantially overestimates <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> because <img src="https://latex.codecogs.com/png.latex?X"> is in fact positively correlated with <img src="https://latex.codecogs.com/png.latex?U">.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">OLS <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X))[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb7-2">OLS</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>       X 
1.567642 </code></pre>
</div>
</div>
<p>In contrast, the IV approach works well.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">IV <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cov</span>(Y, Z) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cov</span>(X, Z)</span>
<span id="cb9-2">IV</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1.049043</code></pre>
</div>
</div>
<p>For the TSLS and control function approaches we need to run the first-stage regression of <img src="https://latex.codecogs.com/png.latex?X"> on <img src="https://latex.codecogs.com/png.latex?Z"> and store the results.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">first_stage <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Z)</span></code></pre></div></div>
</div>
<p>The TSLS approach uses the <em>fitted values</em> of this regression as <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BX%7D">.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">Xtilde <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(first_stage)</span>
<span id="cb12-2">TSLS <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Xtilde))[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># drop the intercept since we're not interested in it</span></span>
<span id="cb12-3">TSLS</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  Xtilde 
1.049043 </code></pre>
</div>
</div>
<p>In contrast, the control function approach uses the <em>residuals</em> from the first stage regression. It also gives us <img src="https://latex.codecogs.com/png.latex?%5Clambda"> in addition to <img src="https://latex.codecogs.com/png.latex?%5Cbeta">.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">Vhat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">residuals</span>(first_stage) </span>
<span id="cb14-2">CF <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Vhat))[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># drop the intercept since we're not interested in it</span></span>
<span id="cb14-3">CF <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The coefficient on Vhat is lambda</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>        X      Vhat 
1.0490432 0.5558904 </code></pre>
</div>
</div>
<p>Notice that we obtain <em>precisely</em> the same estimates for <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> using each of the three approaches.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(IV, TSLS, CF[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>           Xtilde        X 
1.049043 1.049043 1.049043 </code></pre>
</div>
</div>
<p>It turns out that in this simple linear model with a single endogenous regressor and a single instrument, the three approaches are <em>numerically equivalent</em>. In other words, they give <em>exactly</em> the same answer. This will not necessarily be true in more complicated models, so be careful!</p>
</section>
<section id="epilogue" class="level2" data-number="6">
<h2 data-number="6" class="anchored" data-anchor-id="epilogue"><span class="header-section-number">6</span> Epilogue</h2>
<p>It’s time to admit that this post had a secret agenda: to introduce the idea of a control function in the <em>simplest way possible</em>! If you’re interested in learning more about control functions, a canonical example that <em>does not</em> turn out to be identical to IV is the so-called <strong>Heckman Selection Model</strong>, which you can learn more about <a href="https://www.economictricks.com/videos/">here</a>. (Scroll down until you see the heading “Heckman Selection Model.”) The basic logic is similar: to solve an endogeneity problem, use a first-stage regression to estimate an unobserved quantity that “soaks up” the part of the error term that is correlated with your endogenous regressor of interest. If these videos whet your appetite for more control function fun, <a href="http://jhr.uwpress.org/content/50/2/420.short">Wooldridge (2015)</a> provides a helpful overview along with many references to the econometrics literature.</p>


</section>

 ]]></description>
  <category>econometrics</category>
  <category>causal inference</category>
  <guid>https://www.econometrics.blog/post/three-ways-of-thinking-about-instrumental-variables/</guid>
  <pubDate>Sun, 06 Nov 2022 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Why Econometrics is Confusing Part I: The Error Term</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/why-econometrics-is-confusing-part-1-the-error-term/</link>
  <description><![CDATA[ 




<p>“Suppose that <img src="https://latex.codecogs.com/png.latex?Y%20=%20%5Calpha%20+%20%5Cbeta%20X%20+%20U">.” A sentence like this is bound to come up dozens of times in an introductory econometrics course, but if I had my way it would be stamped out completely. Without further clarification, this sentence could mean any number of different things. Even with clarification, it is a source of endless confusion for beginning students. What is <img src="https://latex.codecogs.com/png.latex?U"> exactly? What is the meaning of “<img src="https://latex.codecogs.com/png.latex?=">” in this context? We can do better. Here are a few suggestions.</p>
<section id="population-linear-regression" class="level2 page-columns page-full" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="population-linear-regression"><span class="header-section-number">1</span> Population Linear Regression</h2>
<p>Sometimes <img src="https://latex.codecogs.com/png.latex?Y%20=%20%5Calpha%20+%20%5Cbeta%20X%20+%20U"> is nothing more than the <em>population linear regression model</em>. In other words <img src="https://latex.codecogs.com/png.latex?(%5Calpha,%20%5Cbeta)"> are the solutions to <img src="https://latex.codecogs.com/png.latex?%0A%5Cmin_%7B%5Calpha,%20%5Cbeta%7D%20%5Cmathbb%7BE%7D%5B(Y%20-%20%5Calpha%20-%20%5Cbeta%20X)%5E2%5D.%0A"></p>
<p>The usual way to signal this is by adding the “assumptions” that <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5BXU%5D%20=%20%5Cmathbb%7BE%7D%5BU%5D%20=%200">. It’s no wonder that students find this confusing. Neither of these equalities is in fact an assumption; each is true <em>by construction</em>. Rather than “let <img src="https://latex.codecogs.com/png.latex?Y%20=%20%5Calpha%20+%20%5Cbeta%20X%20+%20U">,” I suggest</p>
<blockquote class="blockquote">
<p>Define <img src="https://latex.codecogs.com/png.latex?U%20%5Cequiv%20Y%20-%20(%5Calpha%20+%20%5Cbeta%20X)"> where <img src="https://latex.codecogs.com/png.latex?%5Calpha"> and <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> are the slope and intercept from a population linear regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X">.</p>
</blockquote>
<p>This makes it clear that <img src="https://latex.codecogs.com/png.latex?U"> has no life of its own; it is defined by the coefficients <img src="https://latex.codecogs.com/png.latex?%5Calpha"> and <img src="https://latex.codecogs.com/png.latex?%5Cbeta">. In this way, the equalities <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5BXU%5D%20=%20%5Cmathbb%7BE%7D%5BU%5D%20=%200"> become a theorem to be deduced rather than a spurious “assumption” of linear regression. Repeat after me: <strong>the population linear regression model has no assumptions</strong>. We can always choose <img src="https://latex.codecogs.com/png.latex?%5Calpha"> and <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> to ensure that <img src="https://latex.codecogs.com/png.latex?U"> satisfies the equalities from above. The solution to the population least squares problem is <img src="https://latex.codecogs.com/png.latex?%0A%5Cbeta%20=%20%5Ctext%7BCov%7D(X,Y)/%5Ctext%7BVar%7D(X),%5Cquad%0A%5Calpha%20=%20%5Cmathbb%7BE%7D%5BY%5D%20-%20%5Cbeta%20%5Cmathbb%7BE%7D%5BX%5D.%0A"> By the linearity of expectation, it follows that <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BE%7D%5BU%5D%20=%20%5Cmathbb%7BE%7D%5BY%20-%20%5Calpha%20-%20%5Cbeta%20X%5D%20=%20%5Cmathbb%7BE%7D%5BY%5D%20-%20(%5Cmathbb%7BE%7D%5BY%5D%20-%20%5Cbeta%20%5Cmathbb%7BE%7D%5BX%5D)%20-%20%5Cbeta%20%5Cmathbb%7BE%7D%5BX%5D%20=%200%0A"> and similarly, although with a bit more algebra<sup>1</sup> <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cmathbb%7BE%7D%5BXU%5D%20&amp;=%20%5Cmathbb%7BE%7D%5BX(Y%20-%20%5Calpha%20-%20%5Cbeta%20X)%5D%20%5C%5C%0A&amp;=%20%5Cmathbb%7BE%7D%5BX(Y%20-%20%5Cleft%5C%7B%5Cmathbb%7BE%7D(Y)%20-%20%5Cbeta%20%5Cmathbb%7BE%7D(X)%5Cright%5C%7D%20-%20%5Cbeta%20X)%5D%5C%5C%0A&amp;=%20%5Cmathbb%7BE%7D%5BX%5Cleft%5C%7BY%20-%20%5Cmathbb%7BE%7D(Y)%20%5Cright%5C%7D%5D%20-%20%20%5Cbeta%20%5Cmathbb%7BE%7D%5BX%5Cleft%5C%7BX%20-%20%5Cmathbb%7BE%7D(X)%5Cright%5C%7D%20%5D%5C%5C%0A&amp;=%20%5Ctext%7BCov%7D(X,Y)%20-%20%5Cbeta%20%5Ctext%7BVar%7D(X)%20=%200.%0A%5Cend%7Baligned%7D%0A"></p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;Surprised that <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5BX(Y%20-%20%5Cmathbb%7BE%7D%5BY%5D)%5D%20=%20%5Ctext%7BCov%7D(X,Y)"> and <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5BX(X%20-%20%5Cmathbb%7BE%7D%5BX%5D)%5D%20=%20%5Ctext%7BVar%7D(X)">? These are great homework problems! Work through the algebra using the definitions of variance and covariance.</p></div></div></section>
<section id="conditional-mean-function" class="level2 page-columns page-full" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="conditional-mean-function"><span class="header-section-number">2</span> Conditional Mean Function</h2>
<p>In other situations <img src="https://latex.codecogs.com/png.latex?Y%20=%20%5Calpha%20+%20%5Cbeta%20X%20+%20U"> is intended to represent a <em>conditional mean function</em>. This is usually signaled by the assumption <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5BU%7CX%5D%20=%200">. This time around I haven’t written the word assumption in “scare quotes.” That’s because there is an assumption lurking here, unlike in the population linear regression model from above. Still, this is a hopelessly confusing way of indicating it. Here’s a better way:</p>
<blockquote class="blockquote">
<p>Define <img src="https://latex.codecogs.com/png.latex?U%20%5Cequiv%20Y%20-%20%5Cmathbb%7BE%7D(Y%7CX)"> and assume that <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y%7CX)%20=%20%5Calpha%20+%20%5Cbeta%20X">.</p>
</blockquote>
<p>Again, this makes it clear that <img src="https://latex.codecogs.com/png.latex?U"> has no life of its own. It is <em>constructed</em> from <img src="https://latex.codecogs.com/png.latex?Y"> and <img src="https://latex.codecogs.com/png.latex?X">. The conditional mean function <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y%7CX)"> is simply the minimizer of <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5B%5Cleft%5C%7B%20Y%20-%20f(X)%5Cright%5C%7D%5E2%5D"> over all (well-behaved) functions.<sup>2</sup> By construction <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5BU%7CX%5D%20=%200"> since <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BE%7D%5BU%7CX%5D%20=%20%5Cmathbb%7BE%7D%5BY%20-%20%5Cmathbb%7BE%7D(Y%7CX)%7CX%5D%20=%20%5Cmathbb%7BE%7D%5BY%7CX%5D%20-%20%5Cmathbb%7BE%7D%5BY%7CX%5D%20=%200%0A"> by the linearity of conditional expectation and the fact that <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y%7CX)"> is a function of <img src="https://latex.codecogs.com/png.latex?X">. But how can we be sure that the conditional mean function is linear? This is a <em>bona fide</em> assumption: it may be true or it may be false. Either way, it is much clearer to emphasize that we are making an assumption about the <em>form</em> of the conditional mean function, <em>not</em> an assumption about the error term <img src="https://latex.codecogs.com/png.latex?U%20%5Cequiv%20Y%20-%20%5Cmathbb%7BE%7D(Y%7CX)">.</p>
<div class="no-row-height column-margin column-container"><div id="fn2"><p><sup>2</sup>&nbsp;See Section 2.1 of my <a href="https://www.economictricks.com/limdep-notes.pdf">lecture notes</a> for details.</p></div></div></section>
<section id="causal-model" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="causal-model"><span class="header-section-number">3</span> Causal Model</h2>
<p>Both interpretations of <img src="https://latex.codecogs.com/png.latex?Y%20=%20%5Calpha%20+%20%5Cbeta%20X%20+%20U"> from above are purely predictive; they say nothing about whether <img src="https://latex.codecogs.com/png.latex?X"> <em>causes</em> <img src="https://latex.codecogs.com/png.latex?Y">. To indicate that a linear model is meant to be causal, it is traditional to write something like “suppose that <img src="https://latex.codecogs.com/png.latex?Y%20=%20%5Calpha%20+%20%5Cbeta%20X%20+%20U"> where <img src="https://latex.codecogs.com/png.latex?X"> may be endogenous.” Often “may be endogenous” is replaced by “where <img src="https://latex.codecogs.com/png.latex?X"> may be correlated with <img src="https://latex.codecogs.com/png.latex?U">.” What on earth is this supposed to mean? The language is vague, evasive, and imprecise. It also stretches the meaning of “<img src="https://latex.codecogs.com/png.latex?=">” beyond all reason. Here’s my suggested improvement:</p>
<blockquote class="blockquote">
<p>Consider the causal model <img src="https://latex.codecogs.com/png.latex?Y%20%5Cleftarrow%20(%5Calpha%20+%20%5Cbeta%20X%20+%20U)"> where <img src="https://latex.codecogs.com/png.latex?U"> is unobserved and <img src="https://latex.codecogs.com/png.latex?(X,U)"> may be dependent.</p>
</blockquote>
<p>Causality is intrinsically directional: cigarettes cause lung cancer; lung cancer doesn’t cause cigarettes. The notation “<img src="https://latex.codecogs.com/png.latex?%5Cleftarrow">” makes this clear. In stark contrast, the notion of mathematical equality is <em>symmetric</em>. If <img src="https://latex.codecogs.com/png.latex?Y%20=%20%5Calpha%20+%20%5Cbeta%20X%20+%20U">, it is just as true to say that <img src="https://latex.codecogs.com/png.latex?X%20=%20(Y%20-%20%5Calpha%20-%20U)%20/%20%5Cbeta">. Of course this is nonsensical when applied to cigarettes and cancer.</p>
<p>In a causal model, <img src="https://latex.codecogs.com/png.latex?U"> <em>does</em> have a life of its own; it represents the causes of <img src="https://latex.codecogs.com/png.latex?Y"> that we cannot observe. Perhaps <img src="https://latex.codecogs.com/png.latex?Y"> is wage, <img src="https://latex.codecogs.com/png.latex?X"> is years of schooling and <img src="https://latex.codecogs.com/png.latex?U"> is “family background” plus “ability.” For this reason I do <em>not</em> write “define <img src="https://latex.codecogs.com/png.latex?U%20%5Cequiv%20(%5Ctext%7Bsomething%7D)">.” We aren’t defining a residual in a prediction problem. We are taking a stand on how the world works by writing down a particular causal model. In a randomized controlled trial, any unobserved causes <img src="https://latex.codecogs.com/png.latex?U"> would be independent of <img src="https://latex.codecogs.com/png.latex?X">. Here we have not made this assumption. We have, however, assumed a particular form for the causal relationship: linear with constant coefficients. Each additional year of schooling causes the same increase (or decrease) in wage regardless of who you are or how many years of schooling you already have. This model could be wrong. But right or wrong, it is fundamentally distinct from the population linear regression and conditional mean models described above. Let’s endeavour to make this clear in our notation.</p>


</section>


 ]]></description>
  <category>econometrics</category>
  <category>teaching</category>
  <guid>https://www.econometrics.blog/post/why-econometrics-is-confusing-part-1-the-error-term/</guid>
  <pubDate>Sun, 24 Jul 2022 00:00:00 GMT</pubDate>
</item>
<item>
  <title>A New Way of Looking at Least Squares</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/a-new-way-of-looking-at-least-squares/</link>
  <description><![CDATA[ 




<p>Have you got a ruler handy? Fantastic! Then hold out your right hand, extend your thumb and little finger as far as they’ll go, and measure the distance in centimeters, rounding to the nearest half centimeter. This is your <em>handspan</em>. Mine is around 23.5 centimeters, but is that big, small, or merely average?<sup>1</sup> Fortunately for you, I’ve asked hundreds of introductory statistics students to measure their handspans over the years and (with their consent) posted the resulting data on my website:</p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;If you play the piano, this may help: I can play parallel 10ths but only just.</p></div></div><div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2">dat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'https://ditraglia.com/econ103/height-handspan.csv'</span>)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>handspan)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  0%  25%  50%  75% 100% 
16.5 20.0 21.5 23.0 28.0 </code></pre>
</div>
</div>
<p>If we take these 326 students as representative of the population of which I am a member, my handspan is roundabout the 84th percentile.</p>
<p>The great thing about handspan, and the reason that I used it in my teaching, is that it’s strongly correlated with height but, in contrast to weight, there’s no temptation to shade the truth. (What’s a <em>good</em> handspan anyway?) Here’s a scatterplot of height against handspan along with the regression line and confidence bands:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">dat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> handspan, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> height)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'lm'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/a-new-way-of-looking-at-least-squares/index_files/figure-html/unnamed-chunk-3-1.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Because handspan is only measured to the nearest half of a centimeter and height to the nearest inch, the dataset contains multiple “tied” values. I’ve used darker colors to indicate situations in which more than one student reported a given height and handspan pair.<sup>2</sup> The correlation between height and handspan is approximately 0.67. From the following simple linear regression, we’d predict approximately a 1.3 inch difference in height between two students whose handspan differed by one centimeter:</p>
<div class="no-row-height column-margin column-container"><div id="fn2"><p><sup>2</sup>&nbsp;Another way to show this is by “jittering” the data: simply replace <code>geom_point(alpha = 0.3)</code> with <code>geom_jitter(alpha = 0.3)</code>.</p></div></div><div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(height <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> handspan, dat))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>(Intercept)    handspan 
  40.943127    1.266961 </code></pre>
</div>
</div>
<section id="where-does-the-regression-line-come-from" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="where-does-the-regression-line-come-from"><span class="header-section-number">1</span> Where does the regression line come from?</h2>
<p>If you’ve taken an introductory statistics or econometrics course, you most likely learned that the least squares regression line <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7B%5Calpha%7D%20+%20%5Cwidehat%7B%5Cbeta%7D%20x"> minimizes the <em>sum of squared vertical deviations</em> by solving the optimization problem <img src="https://latex.codecogs.com/png.latex?%0A%5Cmin_%7B%5Calpha,%20%5Cbeta%7D%20%5Csum_%7Bi=1%7D%5En%20(y_i%20-%20%5Calpha%20-%20%5Cbeta%20x_i)%5E2.%0A"> You probably also learned that the solution is given by <img src="https://latex.codecogs.com/png.latex?%0A%5Cwidehat%7B%5Calpha%7D%20=%20%5Cbar%7By%7D%20-%20%5Cwidehat%7B%5Cbeta%7D%20%5Cbar%7Bx%7D,%20%5Cquad%0A%5Cwidehat%7B%5Cbeta%7D%20=%20%5Cfrac%7B%5Csum_%7Bi=1%7D%5En%20(y_i%20-%20%5Cbar%7By%7D)(x_i%20-%20%5Cbar%7Bx%7D)%7D%7B%5Csum_%7Bi=1%7D%5En%20(x_i%20-%20%5Cbar%7Bx%7D)%5E2%7D%20=%20%5Cfrac%7Bs_%7Bxy%7D%7D%7Bs_x%5E2%7D%0A"> In words: the regression slope equals the ratio of the covariance between height and handspan to the variance of handspan, and the regression line passes through the sample average values of height and handspan. We can check that all of these formulas agree with what we calculated above using <code>lm()</code></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">b <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(dat, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cov</span>(height, handspan) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">var</span>(handspan))</span>
<span id="cb6-2">a <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(dat, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(height) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> b <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(handspan))</span>
<span id="cb6-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(a, b)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 40.943127  1.266961</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(height <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> handspan, dat))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>(Intercept)    handspan 
  40.943127    1.266961 </code></pre>
</div>
</div>
<p>This is all perfectly correct, and an entirely reasonable way of looking at the problem. But I’d now like to suggest a <em>completely different</em> way of looking at regression. Why bother? The more different ways we have of understanding an idea or method, the more deeply we understand how it works, when it works, and when it is likely to fail. So bear with me while I take you on what might at first appear to be a poorly-motivated computational detour. I promise that there’s a payoff at the end!</p>
</section>
<section id="a-crazy-idea" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="a-crazy-idea"><span class="header-section-number">2</span> A Crazy Idea</h2>
<p>There is a unique line that passes through any two distinct points in a plane. So here’s a crazy idea: let’s form every unique pair of <em>students</em> from my height and handspan dataset. To understand what I have in mind, consider a small subset of the data, call it <code>test</code></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">test <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dat[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,]</span>
<span id="cb10-2">test</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 × 2
  height handspan
   &lt;dbl&gt;    &lt;dbl&gt;
1     73     22.5
2     65     17  
3     69     21  </code></pre>
</div>
</div>
<p>With three students, there are three unique unordered pairs: <img src="https://latex.codecogs.com/png.latex?%5C%7B1,2%5C%7D,%20%5C%7B1,3%5C%7D,%20%5C%7B2,3%5C%7D">. Corresponding to these three pairs are <em>three line segments</em>, one through each pair:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(height <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> handspan, test, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pch =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span>
<span id="cb12-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">segments</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x0 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>),     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># FROM: x-coordinates</span></span>
<span id="cb12-3">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y0 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">65</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">65</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">69</span>),     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># FROM: y-coordinates</span></span>
<span id="cb12-4">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">22.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">22.5</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># TO:   x-coordinates</span></span>
<span id="cb12-5">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">69</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">73</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">73</span>),     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># TO:   y-coordinates</span></span>
<span id="cb12-6">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/a-new-way-of-looking-at-least-squares/index_files/figure-html/unnamed-chunk-7-1.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>And associated with each line segment is a <em>slope</em></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">y_differences <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">69</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">65</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">73</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">65</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">73</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">69</span>)</span>
<span id="cb13-2">x_differences <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">22.5</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">22.5</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>) </span>
<span id="cb13-3">slopes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> y_differences <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> x_differences</span>
<span id="cb13-4">slopes</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1.000000 1.454545 2.666667</code></pre>
</div>
</div>
<p>And here’s my question for you: <strong>what, if anything, is the relationship between these three slopes and the slope of the regression line</strong>? While it’s a bit silly to run a regression with only three observations, the results are as follows:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(height <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> handspan, test))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>(Intercept)    handspan 
  41.556701    1.360825 </code></pre>
</div>
</div>
<p>The slope of the regression line doesn’t equal any of the three slopes we calculated above, but it does lie <em>between them</em>. This makes sense: if the regression line were steeper or less steep than all three line segments from above, it couldn’t possibly minimize the sum of squared vertical deviations. Perhaps the regression slope is the arithmetic mean of <code>slopes</code>? No such luck:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(slopes)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1.707071</code></pre>
</div>
</div>
<p>Something interesting is going on here, but it’s not clear what. To learn more, it would be helpful to play with more than three points. But doing this by hand would be extremely tedious. Time to write a function!</p>
</section>
<section id="all-pairs-of-students" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="all-pairs-of-students"><span class="header-section-number">3</span> All Pairs of Students</h2>
<p>The following function generates all unique pairs of elements taken from a vector <code>x</code> and stores them in a matrix:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1">make_pairs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) {</span>
<span id="cb19-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Returns a data frame whose rows contain each unordered pair of elements of x</span></span>
<span id="cb19-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># i.e. all committees of two with members drawn from x</span></span>
<span id="cb19-4">  n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(x)</span>
<span id="cb19-5">  pair_indices <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">combn</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb19-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(x[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(pair_indices)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">byrow =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb19-7">}</span></code></pre></div></div>
</div>
<p>For example, applying <code>make_pairs()</code> to the vector <code>c(1, 2, 3, 4, 5)</code> gives</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_pairs</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>      [,1] [,2]
 [1,]    1    2
 [2,]    1    3
 [3,]    1    4
 [4,]    1    5
 [5,]    2    3
 [6,]    2    4
 [7,]    2    5
 [8,]    3    4
 [9,]    3    5
[10,]    4    5</code></pre>
</div>
</div>
<p>Notice that <code>make_pairs()</code> is constructed such that <em>order doesn’t matter</em>: we don’t distinguish between <img src="https://latex.codecogs.com/png.latex?(4,5)"> and <img src="https://latex.codecogs.com/png.latex?(5,4)">, for example. This makes sense for our example: Alice and Bob denotes the same pair of students as Bob and Alice.</p>
<p>To generate all possible pairs of students from <code>dat</code>, we apply <code>make_pairs()</code> to <code>dat$handspan</code> and <code>dat$height</code> <em>separately</em> and then combine the result into a single dataframe:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1">regression_pairs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_pairs</span>(dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>handspan), </span>
<span id="cb22-2">                               <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_pairs</span>(dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>height))</span>
<span id="cb22-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(regression_pairs)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>    X1   X2 X1.1 X2.1
1 22.5 17.0   73   65
2 22.5 21.0   73   69
3 22.5 25.5   73   71
4 22.5 25.0   73   78
5 22.5 20.0   73   68
6 22.5 20.5   73   75</code></pre>
</div>
</div>
<p>The names are ugly, so let’s clean them up a bit. Handspan is our “x” variable and height is our “y” variable, so we’ll refer to the measurements from each pair as <code>x1, x2, y1, y2</code></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(regression_pairs) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x1'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x2'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'y1'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'y2'</span>)</span>
<span id="cb24-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(regression_pairs)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>    x1   x2 y1 y2
1 22.5 17.0 73 65
2 22.5 21.0 73 69
3 22.5 25.5 73 71
4 22.5 25.0 73 78
5 22.5 20.0 73 68
6 22.5 20.5 73 75</code></pre>
</div>
</div>
<p>The <code>1</code> and <code>2</code> indices indicate a particular <em>student</em>: in a given row <code>x1</code> and <code>y1</code> are the handspan and height of the “first student” in the pair while <code>x2</code> and <code>y2</code> are the handspan and height of the “second student.” Each student appears many times in the <code>regression_pairs</code> dataframe. This is because there are many pairs of students that include Alice: we can pair her up with any other student in the class. For this reason, <code>regression_pairs</code> has a tremendous number of rows, 52975 to be precise. This is the number of ways to form a committee of size 2 from a collection of 326 people when order doesn’t matter:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">326</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 52975</code></pre>
</div>
</div>
</section>
<section id="i-just-calculated-52975-slopes." class="level2" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="i-just-calculated-52975-slopes."><span class="header-section-number">4</span> I just calculated 52,975 slopes.</h2>
<p>Corresponding to each row of <code>regression_pairs</code> is a slope. We can calculate and summarize them as follows, using the <code>dplyr</code> package from the <code>tidyverse</code> to make things easier to read:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb28-2">regression_pairs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> regression_pairs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb28-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">slope =</span> (y2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> y1) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (x2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> x1))</span>
<span id="cb28-4">regression_pairs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb28-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(slope) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb28-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.     NAs 
   -Inf   0.000   1.333     NaN   2.667     Inf     419 </code></pre>
</div>
</div>
<p>The sample mean slope is <code>NaN</code>, the minimum is <code>-Inf</code>, the maximum is <code>Inf</code>, and there are 419 missing values. So what on earth does this mean? First things first: the abbreviation <code>NaN</code> stands for “not a number.” This is R’s way of expressing <img src="https://latex.codecogs.com/png.latex?0/0">, and an <code>NaN</code> “counts” as a missing value:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] NaN</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
</div>
<p>In contrast, <code>Inf</code> and <code>-Inf</code> are R’s way of expressing <img src="https://latex.codecogs.com/png.latex?%5Cpm%20%5Cinfty">. These do <em>not</em> count as missing values, and they also arise when a number is too big or too small for your computer to represent:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9999999</span>), <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9999999</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -Inf  Inf  Inf -Inf</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Inf</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] FALSE FALSE</code></pre>
</div>
</div>
<p>So where do these <code>Inf</code> and <code>NaN</code> values come from? Our slope calculation from above was <code>(y2 - y1) / (x2 - x1)</code>. If <code>x2 == x1</code>, the denominator is zero. This occurs when the two students in a given pair have the same handspan. Because we only measured handspan to the nearest 0.5cm, there are many such pairs. Indeed, <code>handspan</code> only takes on 23 distinct values in our dataset but there are 326 students:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sort</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(dat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>handspan))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] 16.5 17.0 17.5 18.0 18.5 19.0 19.5 20.0 20.5 21.0 21.5 22.0 22.5 23.0 23.5
[16] 24.0 24.5 25.0 25.5 26.0 26.5 27.0 28.0</code></pre>
</div>
</div>
<p>If <code>y1</code> and <code>y2</code> are different but <code>x1</code> and <code>x2</code> are the same, the slope will either be <code>Inf</code> or <code>-Inf</code>, depending on whether <code>y1 &gt; y2</code> or the reverse. When <code>y1 == y2</code> <em>and</em> <code>x1 == x2</code> the slope is <code>NaN</code>.</p>
<p>This isn’t an arcane numerical problem. When <code>y1 == y2</code> and <code>x1 == x2</code>, our pair contains only a <em>single point</em>, so there’s no way to draw a line segment. With no line to draw, there’s clearly no slope to calculate. When <code>y1 != y2</code> but <code>x1 == x2</code> we can draw a line segment, but it will be vertical. Should we call the slope of this vertical line <img src="https://latex.codecogs.com/png.latex?+%5Cinfty">? Or should we call it <img src="https://latex.codecogs.com/png.latex?-%5Cinfty">? Because the labels <code>1</code> and <code>2</code> for the students in a given pair were arbitrary–order doesn’t matter–there’s no way to choose between <code>Inf</code> and <code>-Inf</code>. <strong>From the perspective of calculating a slope, it simply doesn’t make sense to construct pairs of students with the same handspan.</strong></p>
<p>With this in mind, let’s see what happens if we average all of the slopes that are <em>not</em> <code>-Inf</code>, <code>Inf</code>, or <code>NaN</code></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1">regression_pairs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb40-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(slope) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.infinite</span>(slope)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb40-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(slope) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb40-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1.259927</code></pre>
</div>
</div>
<p>This is tantalizingly close to the slope of the regression line from above: 1.266961. But it’s still <em>slightly off</em>.</p>
</section>
<section id="not-all-slopes-are-created-equal" class="level2" data-number="5">
<h2 data-number="5" class="anchored" data-anchor-id="not-all-slopes-are-created-equal"><span class="header-section-number">5</span> Not All Slopes are Created Equal</h2>
<p>The median handspan in my dataset is 21.5. Let’s take a closer look at the heights of students whose handspans are <em>close</em> to this value:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb42" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">boxplot</span>(height <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> handspan, dat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subset =</span> handspan <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">21.5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/a-new-way-of-looking-at-least-squares/index_files/figure-html/unnamed-chunk-21-1.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>There is a large amount of variation in height for a given value of handspan. Indeed, from this boxplot alone you might not even guess that there is a strong positive relationship between height and handspan in the dataset as a whole! The “boxes” in the figure, representing the middle 50% of heights for a given handspan, overlap substantially. If we were to randomly choose one student with a handspan of 21 and another with a handspan of 21.5, it’s quite likely that the slope between them would be <em>negative</em>. It’s true that students with bigger hands are taller on average. But the difference in height that we’d predict for two students who differed by 0.5cm in handspan is very small: 0.6 inches according to the linear regression from the beginning of this post. In contrast, the standard deviation of height among students with the median handspan is more than five times as large:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb43" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb43-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset</span>(dat, handspan <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">21.5</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>height)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3.172248</code></pre>
</div>
</div>
<p>The 25th percentile of handspan is 20 while the 75th percentile is 23. Comparing the heights of students with these handspans rather than those close to the median, gives a very different picture:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb45" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb45-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">boxplot</span>(height <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> handspan, dat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subset =</span> handspan <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">23</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/a-new-way-of-looking-at-least-squares/index_files/figure-html/unnamed-chunk-23-1.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Now there’s much less overlap in the distributions of height. This accords with the predictions of the linear regression from above: for two students whose handspan differs by 3cm, we would predict a difference of 3.8 inches in height. This difference is big enough to discern in spite of the variation in height for students with the same handspan. If we were to choose one student with a handspan of 20cm and another with a handspan of 23cm, it’s fairly unlikely that the slope between these points would be negative.</p>
<p>So where does this leave us? Above we saw that forming a pair of students with the <em>same</em> handspan does not allow us to calculate a slope. Now we’ve seen that the slope for a pair of students with a very <em>similar</em> handspan can give a misleading impression about the overall relationship. This turns out to be the key to our puzzle from above. The ordinary least squares slope estimate <em>does</em> equal an average of the slopes for each pair of students, but this average gives more weight to pairs with a larger difference in handspan. As I’ll derive in my next post: <img src="https://latex.codecogs.com/png.latex?%0A%5Cwidehat%7B%5Cbeta%7D_%7B%5Ctext%7BOLS%7D%7D%20=%20%5Csum_%7B(i,j)%5Cin%20C_2%5En%7D%20%5Comega_%7Bij%7D%20%5Cleft(%5Cfrac%7By_i%20-%20y_j%7D%7Bx_i%20-%20x_j%7D%5Cright),%20%5Cquad%0A%5Comega_%7Bij%7D%20%5Cequiv%20%5Cfrac%7B(x_i%20-%20x_j)%5E2%7D%7B%5Csum_%7B(i,j)%5Cin%20C_2%5En%7D%20(x_i-x_j)%5E2%7D%0A"> The notation <img src="https://latex.codecogs.com/png.latex?C_2%5En"> is shorthand for the set <img src="https://latex.codecogs.com/png.latex?%5C%7B(i,j)%5Ccolon%201%20%5Cleq%20i%20%3C%20j%20%5Cleq%20n%5C%7D">, in other words the set of all unique <em>pairs</em> <img src="https://latex.codecogs.com/png.latex?(i,j)"> where order doesn’t matter. The weights <img src="https://latex.codecogs.com/png.latex?%5Comega_%7Bij%7D"> are between zero and one and sum to one over all pairs. Pairs with <img src="https://latex.codecogs.com/png.latex?x_i%20=%20x_j"> are given <em>zero weight</em>; pairs in which <img src="https://latex.codecogs.com/png.latex?x_i"> is far from <img src="https://latex.codecogs.com/png.latex?x_j"> are given more weight than pairs where these values are closer. And you don’t have to wait for my next post to see that it works:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb46" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb46-1">regression_pairs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> regression_pairs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb46-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x_dist =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(x2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> x1),</span>
<span id="cb46-3">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weight =</span> x_dist<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(x_dist<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb46-4"></span>
<span id="cb46-5">regression_pairs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb46-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.infinite</span>(slope) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(slope)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb46-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(weight <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> slope)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb46-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1.266961</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb48" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb48-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(height <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> handspan, dat))[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>handspan 
1.266961 </code></pre>
</div>
</div>
<p>So there you have it: in a simple linear regression, the OLS slope estimate is a weighted average of the slopes of the line segments between all pairs of observations. The weights are proportional to the squared Euclidean distance between <img src="https://latex.codecogs.com/png.latex?x">-coordinates. I’ll leave things here for today, but there’s much more to say on this topic. Stay tuned for the next installment!</p>


</section>


 ]]></description>
  <category>econometrics</category>
  <guid>https://www.econometrics.blog/post/a-new-way-of-looking-at-least-squares/</guid>
  <pubDate>Mon, 14 Mar 2022 00:00:00 GMT</pubDate>
</item>
<item>
  <title>The Wilson Confidence Interval for a Proportion</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/the-wilson-confidence-interval-for-a-proportion/</link>
  <description><![CDATA[ 




<p>This is the second in a series of posts about how to construct a confidence interval for a proportion. (Simple problems sometimes turn out to be surprisingly complicated in practice!) In the <a href="https://www.econometrics.blog/post/don-t-use-the-textbook-ci-for-a-proportion/">first part</a>, I discussed the serious problems with the “textbook” approach, and outlined a simple hack that works amazingly well in practice: the <a href="https://www.tandfonline.com/doi/abs/10.1080/00031305.1998.10480550">Agresti-Coull confidence interval</a>.</p>
<p>Somewhat unsatisfyingly, my earlier post gave no indication of where the Agresti-Coull interval comes from, how to construct it when you want a confidence level <em>other than</em> 95%, and why it works. In this post I’ll fill in some of the gaps by discussing <em>yet another</em> confidence interval for a proportion: the <em>Wilson interval</em>, so-called because it first appeared in <a href="https://www.tandfonline.com/doi/abs/10.1080/01621459.1927.10502953?journalCode=uasa20">Wilson (1927)</a>. While it’s not usually taught in introductory courses, it easily could be. Not only does the Wilson interval perform extremely well in practice, it packs a powerful pedagogical punch by illustrating the idea of “inverting a hypothesis test.” Spoiler alert: the Agresti-Coull interval is a rough-and-ready approximation to the Wilson interval.</p>
<p>To understand the Wilson interval, we first need to remember a key fact about statistical inference: hypothesis testing and confidence intervals are two sides of the same coin. We can use a test to create a confidence interval, and vice-versa. In case you’re feeling a bit rusty on this point, let me begin by refreshing your memory with the simplest possible example. If this is old hat to you, skip ahead to the next section.</p>
<section id="tests-and-cis-two-sides-of-the-same-coin" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="tests-and-cis-two-sides-of-the-same-coin"><span class="header-section-number">1</span> Tests and CIs – Two Sides of the Same Coin</h2>
<p>Suppose that we observe a random sample <img src="https://latex.codecogs.com/png.latex?X_1,%20%5Cdots,%20X_n"> from a normal population with unknown mean <img src="https://latex.codecogs.com/png.latex?%5Cmu"> and known variance <img src="https://latex.codecogs.com/png.latex?%5Csigma%5E2">. Under these assumptions, the sample mean <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D_n%20%5Cequiv%20%5Cleft(%5Cfrac%7B1%7D%7Bn%7D%20%5Csum_%7Bi=1%7D%5En%20X_i%5Cright)"> follows a <img src="https://latex.codecogs.com/png.latex?N(%5Cmu,%20%5Csigma%5E2/n)"> distribution. Centering and standardizing, <img src="https://latex.codecogs.com/png.latex?%20%5Cfrac%7B%5Cbar%7BX%7D_n%20-%20%5Cmu%7D%7B%5Csigma/%5Csqrt%7Bn%7D%7D%20%5Csim%20N(0,1)."> Now, suppose we want to test <img src="https://latex.codecogs.com/png.latex?H_0%5Ccolon%20%5Cmu%20=%20%5Cmu_0"> against the two-sided alternative <img src="https://latex.codecogs.com/png.latex?H_1%5Ccolon%20%5Cmu%20%5Cneq%20%5Cmu_0"> at the 5% significance level. If <img src="https://latex.codecogs.com/png.latex?%5Cmu%20=%20%5Cmu_0">, then the test statistic <img src="https://latex.codecogs.com/png.latex?T_n%20%5Cequiv%20%5Cfrac%7B%5Cbar%7BX%7D_n%20-%20%5Cmu_0%7D%7B%5Csigma/%5Csqrt%7Bn%7D%7D"> follows a standard normal distribution. If <img src="https://latex.codecogs.com/png.latex?%5Cmu%20%5Cneq%20%5Cmu_0">, then <img src="https://latex.codecogs.com/png.latex?T_n"> does not follow a standard normal distribution. To carry out the test, we reject <img src="https://latex.codecogs.com/png.latex?H_0"> if <img src="https://latex.codecogs.com/png.latex?%7CT_n%7C"> is greater than <img src="https://latex.codecogs.com/png.latex?1.96">, the <img src="https://latex.codecogs.com/png.latex?(1%20-%20%5Calpha/2)"> quantile of a standard normal distribution for <img src="https://latex.codecogs.com/png.latex?%5Calpha%20=%200.05">. To put it another way, we <em>fail to reject</em> <img src="https://latex.codecogs.com/png.latex?H_0"> if <img src="https://latex.codecogs.com/png.latex?%7CT_n%7C%20%5Cleq%201.96">. So for what values of <img src="https://latex.codecogs.com/png.latex?%5Cmu_0"> will we fail to reject? By the definition of absolute value and the definition of <img src="https://latex.codecogs.com/png.latex?T_n"> from above, <img src="https://latex.codecogs.com/png.latex?%7CT_n%7C%20%5Cleq%201.96"> is equivalent to <img src="https://latex.codecogs.com/png.latex?%0A-%201.96%20%5Cleq%20%5Cfrac%7B%5Cbar%7BX%7D_n%20-%20%5Cmu_0%7D%7B%5Csigma/%5Csqrt%7Bn%7D%7D%20%5Cleq%201.96.%0A"> Re-arranging, this in turn is equivalent to <img src="https://latex.codecogs.com/png.latex?%0A%5Cbar%7BX%7D_n%20-%201.96%20%5Ctimes%20%5Cfrac%7B%5Csigma%7D%7B%5Csqrt%7Bn%7D%7D%20%5Cleq%20%5Cmu_0%20%5Cleq%20%5Cbar%7BX%7D_n%20+%201.96%20%5Ctimes%20%5Cfrac%7B%5Csigma%7D%7B%5Csqrt%7Bn%7D%7D.%0A"> This tells us that the values of <img src="https://latex.codecogs.com/png.latex?%5Cmu_0"> we will <em>fail to reject</em> are precisely those that lie in the interval <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D%20%5Cpm%201.96%20%5Ctimes%20%5Csigma/%5Csqrt%7Bn%7D">. Does this look familiar? It should: it’s the usual 95% confidence interval for the mean of a normal population with known variance. The 95% confidence interval corresponds exactly to the set of values <img src="https://latex.codecogs.com/png.latex?%5Cmu_0"> that we <em>fail to reject</em> at the 5% level.</p>
<p>This example is a special case a more general result. If you give me a <img src="https://latex.codecogs.com/png.latex?(1%20-%20%5Calpha)%5Ctimes%20100%5C%25"> confidence interval for a parameter <img src="https://latex.codecogs.com/png.latex?%5Ctheta">, I can use it to test <img src="https://latex.codecogs.com/png.latex?H_0%5Ccolon%20%5Ctheta%20=%20%5Ctheta_0"> against <img src="https://latex.codecogs.com/png.latex?H_1%20%5Ccolon%20%5Ctheta%20%5Cneq%20%5Ctheta_0">. All I have to do is check whether <img src="https://latex.codecogs.com/png.latex?%5Ctheta_0"> lies inside the confidence interval, in which case I fail to reject, or outside, in which case I reject. Conversely, if you give me a two-sided test of <img src="https://latex.codecogs.com/png.latex?H_0%5Ccolon%20%5Ctheta%20=%20%5Ctheta_0"> with significance level <img src="https://latex.codecogs.com/png.latex?%5Calpha">, I can use it to construct a <img src="https://latex.codecogs.com/png.latex?(1%20-%20%5Calpha)%20%5Ctimes%20100%5C%25"> confidence interval for <img src="https://latex.codecogs.com/png.latex?%5Ctheta">. All I have to do is collect the values of <img src="https://latex.codecogs.com/png.latex?%5Ctheta_0"> that are <em>not rejected</em>. This procedure is called <em>inverting a test</em>.</p>
</section>
<section id="how-to-confuse-your-introductory-statistics-students" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="how-to-confuse-your-introductory-statistics-students"><span class="header-section-number">2</span> How to Confuse Your Introductory Statistics Students</h2>
<p>Around the same time as we teach students the duality between testing and confidence intervals–you can use a confidence interval to carry out a test or a test to construct a confidence interval–we throw a wrench into the works. The most commonly-presented test for a population proportion <img src="https://latex.codecogs.com/png.latex?p"> does <em>not</em> coincide with the most commonly-presented confidence interval for <img src="https://latex.codecogs.com/png.latex?p">. To quote from page 355 of <a href="https://imai.fas.harvard.edu/">Kosuke Imai’s</a> fantastic textbook <a href="https://press.princeton.edu/books/hardcover/9780691167039/quantitative-social-science">Quantitative Social Science: An Introduction</a></p>
<blockquote class="blockquote">
<p>the standard error used for confidence intervals is different from the standard error used for hypothesis testing. This is because the latter standard error is derived under the null hypothesis … whereas the standard error for confidence intervals is computed using the estimated proportion.</p>
</blockquote>
<p>Let’s translate this into mathematics. Suppose that <img src="https://latex.codecogs.com/png.latex?X_1,%20...,%20X_n%20%5Csim%20%5Ctext%7Biid%20Bernoulli%7D(p)"> and let <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D%20%5Cequiv%20(%5Cfrac%7B1%7D%7Bn%7D%20%5Csum_%7Bi=1%7D%5En%20X_i)">. The two standard errors that Imai describes are <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BSE%7D_0%20%5Cequiv%20%5Csqrt%7B%5Cfrac%7Bp_0(1%20-%20p_0)%7D%7Bn%7D%7D%20%5Cquad%20%5Ctext%7Bversus%7D%20%5Cquad%0A%5Cwidehat%7B%5Ctext%7BSE%7D%7D%20%5Cequiv%20%5Csqrt%7B%5Cfrac%7B%5Cwidehat%7Bp%7D(1%20-%20%5Cwidehat%7Bp%7D)%7D%7Bn%7D%7D.%0A"> Following the advice of our introductory textbook, we test <img src="https://latex.codecogs.com/png.latex?H_0%5Ccolon%20p%20=%20p_0"> against <img src="https://latex.codecogs.com/png.latex?H_1%5Ccolon%20p%20%5Cneq%20p_0"> at the <img src="https://latex.codecogs.com/png.latex?5%5C%25"> level by checking whether <img src="https://latex.codecogs.com/png.latex?%7C(%5Cwidehat%7Bp%7D%20-%20p_0)%20/%20%5Ctext%7BSE%7D_0%7C"> exceeds <img src="https://latex.codecogs.com/png.latex?1.96">. This is called the <em>score test</em> for a proportion. Again following the advice of our introductory textbook, we report <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D%20%5Cpm%201.96%20%5Ctimes%20%5Cwidehat%7B%5Ctext%7BSE%7D%7D"> as our 95% confidence interval for <img src="https://latex.codecogs.com/png.latex?p">. As you may recall from my <a href="https://www.econometrics.blog/post/don-t-use-the-textbook-ci-for-a-proportion/">earlier post</a>, this is the so-called <em>Wald confidence interval</em> for <img src="https://latex.codecogs.com/png.latex?p">. Because the two standard error formulas in general disagree, the relationship between tests and confidence intervals breaks down.</p>
<p>To make this more concrete, let’s plug in some numbers. Suppose that <img src="https://latex.codecogs.com/png.latex?n%20=%2025"> and our observed sample contains 5 ones and 20 zeros. Then <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D%20=%200.2"> and we can calculate <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7B%5Ctext%7BSE%7D%7D"> and the Wald confidence interval as follows</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span></span>
<span id="cb1-2">n1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span>
<span id="cb1-3">p_hat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> n1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n</span>
<span id="cb1-4">alpha <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span></span>
<span id="cb1-5">SE_hat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(p_hat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p_hat) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n)</span>
<span id="cb1-6">p_hat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> alpha <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> SE_hat</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.04320288 0.35679712</code></pre>
</div>
</div>
<p>The value 0.07 is well within this interval. This suggests that we should <em>fail to reject</em> <img src="https://latex.codecogs.com/png.latex?H_0%5Ccolon%20p%20=%200.07"> against the two-sided alternative. But when we compute the score test statistic we obtain a value well above 1.96, so that <img src="https://latex.codecogs.com/png.latex?H_0%5Ccolon%20p%20=%200.07"> is soundly rejected:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">p0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.07</span></span>
<span id="cb3-2">SE0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(p0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p0) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n)</span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>((p_hat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p0) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> SE0)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2.547551</code></pre>
</div>
</div>
<p>The test says reject <img src="https://latex.codecogs.com/png.latex?H_0%5Ccolon%20p%20=%200.07"> and the confidence interval says don’t. Upon encountering this example, your students decide that statistics is a tangled mess of contradictions, despair of ever making sense of it, and resign themselves to simply memorizing the requisite formulas for the exam.</p>
</section>
<section id="should-we-teach-the-wald-test-instead" class="level2 page-columns page-full" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="should-we-teach-the-wald-test-instead"><span class="header-section-number">3</span> Should we teach the Wald test instead?</h2>
<p>How can we dig our way out of this mess? One idea is to <em>use a different test</em>, one that agrees with the Wald confidence interval. If we had used <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7B%5Ctext%7BSE%7D%7D"> rather than <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D_0"> to test <img src="https://latex.codecogs.com/png.latex?H_0%5Ccolon%20p%20=%200.07"> above, our test statistic would have been</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>((p_hat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p0) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> SE_hat)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1.625</code></pre>
</div>
</div>
<p>which is clearly less than 1.96. Thus we would fail to reject <img src="https://latex.codecogs.com/png.latex?H_0%5Ccolon%20p%20=%200.07"> exactly as the Wald confidence interval instructed us above. This procedure is called the <em>Wald test</em> for a proportion. Its main benefit is that it agrees with the Wald interval, unlike the score test, restoring the link between tests and confidence intervals that we teach our students. Unfortunately <a href="https://www.econometrics.blog/post/don-t-use-the-textbook-ci-for-a-proportion/">the Wald confidence interval is terrible and you should never use it</a>. Because the Wald test is equivalent to checking whether <img src="https://latex.codecogs.com/png.latex?p_0"> lies inside the Wald confidence interval, it inherits all of the latter’s defects.</p>
<p>Indeed, compared to the score test, the Wald test is a disaster, as I’ll now show. Suppose we carry out a 5% test. If the null is true, we should reject it 5% of the time. Because the Wald and Score tests are both based on an approximation provided by the <a href="https://www.econometrics.blog/post/thirty-isn-t-the-magic-number/">central limit theorem</a>, we should allow a bit of leeway here: the actual rejection rates may be slightly different from 5%. Nevertheless, we’d expect them to at least be <em>fairly close</em> to the nominal value of 5%. The following plot shows the <em>actual</em> type I error rates of the score and Wald tests, over a range of values for the true population proportion <img src="https://latex.codecogs.com/png.latex?p"> with sample sizes of 25, 50, and 100. In each case the nominal size of each test, shown as a dashed red line, is 5%.<sup>1</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;For the R code used to generate these plots, see the Appendix at the end of this post.</p></div></div><div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/the-wilson-confidence-interval-for-a-proportion/index_files/figure-html/unnamed-chunk-5-1.svg" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/the-wilson-confidence-interval-for-a-proportion/index_files/figure-html/unnamed-chunk-5-2.svg" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/the-wilson-confidence-interval-for-a-proportion/index_files/figure-html/unnamed-chunk-5-3.svg" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The score test isn’t perfect: if <img src="https://latex.codecogs.com/png.latex?p"> is extremely close to zero or one, its actual type I error rate can be appreciably higher than its nominal type I error rate: as much as 10% compared to 5% when <img src="https://latex.codecogs.com/png.latex?n%20=%2025">. But in general, its performance is good. In contrast, the Wald test is absolutely terrible: its nominal type I error rate is systematically higher than 5% even when <img src="https://latex.codecogs.com/png.latex?n"> is not especially small and <img src="https://latex.codecogs.com/png.latex?p"> is not especially close to zero or one.</p>
<p>Granted, teaching the Wald test alongside the Wald interval would reduce confusion in introductory statistics courses. But it would also equip students with lousy tools for real-world inference. There is a better way: rather than teaching the <em>test</em> that corresponds to the Wald interval, we could teach the <em>confidence interval</em> that corresponds to the score test.</p>
</section>
<section id="inverting-the-score-test" class="level2" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="inverting-the-score-test"><span class="header-section-number">4</span> Inverting the Score Test</h2>
<p>Suppose we collect all values <img src="https://latex.codecogs.com/png.latex?p_0"> that the score test does <em>not reject</em> at the 5% level. If the score test is working well–if its nominal type I error rate is close to 5%–the resulting set of values <img src="https://latex.codecogs.com/png.latex?p_0"> will be an approximate <img src="https://latex.codecogs.com/png.latex?(1%20-%20%5Calpha)%20%5Ctimes%20100%5C%25"> confidence interval for <img src="https://latex.codecogs.com/png.latex?p">. Why is this so? Suppose that <img src="https://latex.codecogs.com/png.latex?p_0"> is the true population proportion. Then an interval constructed in this way will cover <img src="https://latex.codecogs.com/png.latex?p_0"> <em>precisely when</em> the score test does not reject <img src="https://latex.codecogs.com/png.latex?H_0%5Ccolon%20p%20=%20p_0">. This occurs with probability <img src="https://latex.codecogs.com/png.latex?(1%20-%20%5Calpha)">. Because the score test is much more accurate than the Wald test, the confidence interval that we obtain by inverting it way will be much more accurate than the Wald interval. This interval is called the <em>score interval</em> or the <em>Wilson interval</em>.</p>
<p>So let’s do it: let’s <em>invert</em> the score test. Our goal is to find all values <img src="https://latex.codecogs.com/png.latex?p_0"> such that <img src="https://latex.codecogs.com/png.latex?%7C(%5Cwidehat%7Bp%7D%20-%20p_0)/%5Ctext%7BSE%7D_0%7C%5Cleq%20c"> where <img src="https://latex.codecogs.com/png.latex?c"> is the normal critical value for a two-sided test with significance level <img src="https://latex.codecogs.com/png.latex?%5Calpha">. Squaring both sides of the inequality and substituting the definition of <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D_0"> from above gives <img src="https://latex.codecogs.com/png.latex?%0A(%5Cwidehat%7Bp%7D%20-%20p_0)%5E2%20%5Cleq%20c%5E2%20%5Cleft%5B%20%5Cfrac%7Bp_0(1%20-%20p_0)%7D%7Bn%7D%5Cright%5D.%0A"> Multiplying both sides of the inequality by <img src="https://latex.codecogs.com/png.latex?n">, expanding, and re-arranging leaves us with a quadratic inequality in <img src="https://latex.codecogs.com/png.latex?p_0">, namely <img src="https://latex.codecogs.com/png.latex?%0A(n%20+%20c%5E2)%20p_0%5E2%20-%20(2n%5Cwidehat%7Bp%7D%20+%20c%5E2)%20p_0%20+%20n%5Cwidehat%7Bp%7D%5E2%20%5Cleq%200.%0A"> Remember: we are trying to find the values of <img src="https://latex.codecogs.com/png.latex?p_0"> that satisfy the inequality. The terms <img src="https://latex.codecogs.com/png.latex?(n%20+%20c%5E2)"> along with <img src="https://latex.codecogs.com/png.latex?(2n%5Cwidehat%7Bp%7D)"> and <img src="https://latex.codecogs.com/png.latex?n%5Cwidehat%7Bp%7D%5E2"> are <em>constants</em>. Once we choose <img src="https://latex.codecogs.com/png.latex?%5Calpha">, the critical value <img src="https://latex.codecogs.com/png.latex?c"> is known. Once we observe the data, <img src="https://latex.codecogs.com/png.latex?n"> and <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D"> are known. Since <img src="https://latex.codecogs.com/png.latex?(n%20+%20c%5E2)%20%3E%200">, the left-hand side of the inequality is a parabola in <img src="https://latex.codecogs.com/png.latex?p_0"> that opens <em>upwards</em>. This means that the values of <img src="https://latex.codecogs.com/png.latex?p_0"> that satisfy the inequality must lie <em>between</em> the roots of the quadratic equation <img src="https://latex.codecogs.com/png.latex?%0A(n%20+%20c%5E2)%20p_0%5E2%20-%20(2n%5Cwidehat%7Bp%7D%20+%20c%5E2)%20p_0%20+%20n%5Cwidehat%7Bp%7D%5E2%20=%200.%0A"> By the quadratic formula, these roots are <img src="https://latex.codecogs.com/png.latex?%0Ap_0%20=%20%5Cfrac%7B(2%20n%5Cwidehat%7Bp%7D%20+%20c%5E2)%20%5Cpm%20%5Csqrt%7B4%20c%5E2%20n%20%5Cwidehat%7Bp%7D(1%20-%20%5Cwidehat%7Bp%7D)%20+%20c%5E4%7D%7D%7B2(n%20+%20c%5E2)%7D.%0A"> Factoring <img src="https://latex.codecogs.com/png.latex?2n"> out of the numerator and denominator of the right-hand side and simplifying, we can re-write this as <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0Ap_0%20%20&amp;=%20%5Cfrac%7B1%7D%7B2%5Cleft(n%20+%20%5Cfrac%7Bn%20c%5E2%7D%7Bn%7D%5Cright)%7D%5Cleft%5C%7B%5Cleft(2n%5Cwidehat%7Bp%7D%20+%20%5Cfrac%7B2n%20c%5E2%7D%7B2n%7D%5Cright)%20%5Cpm%20%5Csqrt%7B4%20n%5E2c%5E2%20%5Cleft%5B%5Cfrac%7B%5Cwidehat%7Bp%7D(1%20-%20%5Cwidehat%7Bp%7D)%7D%7Bn%7D%5Cright%5D%20+%204n%5E2c%5E2%5Cleft%5B%5Cfrac%7Bc%5E2%7D%7B4n%5E2%7D%5Cright%5D%20%7D%5Cright%5C%7D%20%5C%5C%20%5C%5C%0Ap_0%20%20&amp;=%20%5Cfrac%7B1%7D%7B2n%5Cleft(1%20+%20%5Cfrac%7B%20c%5E2%7D%7Bn%7D%5Cright)%7D%5Cleft%5C%7B2n%5Cleft(%5Cwidehat%7Bp%7D%20+%20%5Cfrac%7Bc%5E2%7D%7B2n%7D%5Cright)%20%5Cpm%202nc%5Csqrt%7B%20%5Cfrac%7B%5Cwidehat%7Bp%7D(1%20-%20%5Cwidehat%7Bp%7D)%7D%7Bn%7D%20+%20%5Cfrac%7Bc%5E2%7D%7B4n%5E2%7D%7D%20%5Cright%5C%7D%0A%5C%5C%20%5C%5C%0Ap_0%20%20&amp;=%20%5Cleft(%20%5Cfrac%7Bn%7D%7Bn%20+%20c%5E2%7D%5Cright)%5Cleft%5C%7B%5Cleft(%5Cwidehat%7Bp%7D%20+%20%5Cfrac%7Bc%5E2%7D%7B2n%7D%5Cright)%20%5Cpm%20c%5Csqrt%7B%20%5Cwidehat%7B%5Ctext%7BSE%7D%7D%5E2%20+%20%5Cfrac%7Bc%5E2%7D%7B4n%5E2%7D%20%7D%5Cright%5C%7D%5C%5C%20%5C%5C%0A%5Cend%7Baligned%7D%0A"> using our definition of <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7B%5Ctext%7BSE%7D%7D"> from above. And there you have it: the right-hand side of the final equality is the <img src="https://latex.codecogs.com/png.latex?(1%20-%20%5Calpha)%5Ctimes%20100%5C%25"> Wilson confidence interval for a proportion, where <img src="https://latex.codecogs.com/png.latex?c%20=%20%5Ctexttt%7Bqnorm%7D(1%20-%20%5Calpha/2)"> is the normal critical value for a two-sided test with significance level <img src="https://latex.codecogs.com/png.latex?%5Calpha">, and <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7B%5Ctext%7BSE%7D%7D%5E2%20=%20%5Cwidehat%7Bp%7D(1%20-%20%5Cwidehat%7Bp%7D)/n">.</p>
<p>Compared to the Wald interval, <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D%20%5Cpm%20c%20%5Ctimes%20%5Cwidehat%7B%5Ctext%7BSE%7D%7D">, the Wilson interval is certainly more complicated. But it is constructed from exactly the same information: the sample proportion <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D">, two-sided critical value <img src="https://latex.codecogs.com/png.latex?c"> and sample size <img src="https://latex.codecogs.com/png.latex?n">. Computing it by hand is tedious, but programming it in R is a snap:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">get_wilson_CI <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>) {</span>
<span id="cb7-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#-----------------------------------------------------------------------------</span></span>
<span id="cb7-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute the Wilson (aka Score) confidence interval for a popn. proportion</span></span>
<span id="cb7-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#-----------------------------------------------------------------------------</span></span>
<span id="cb7-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># x        vector of data (zeros and ones)</span></span>
<span id="cb7-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># alpha    1 - (confidence level)</span></span>
<span id="cb7-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#-----------------------------------------------------------------------------</span></span>
<span id="cb7-8">  n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(x)</span>
<span id="cb7-9">  p_hat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(x)</span>
<span id="cb7-10">  SE_hat_sq <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> p_hat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p_hat) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n</span>
<span id="cb7-11">  crit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> alpha <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb7-12">  omega <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> crit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb7-13">  A <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> p_hat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> crit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> n)</span>
<span id="cb7-14">  B <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> crit <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(SE_hat_sq <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> crit<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb7-15">  CI <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'lower'</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> omega <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (A <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> B), </span>
<span id="cb7-16">          <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'upper'</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> omega <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (A <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> B))</span>
<span id="cb7-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(CI)</span>
<span id="cb7-18">}</span></code></pre></div></div>
</div>
<p>Notice that this is only slightly more complicated to implement than the Wald confidence interval:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">get_wald_CI <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>) {</span>
<span id="cb8-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#-----------------------------------------------------------------------------</span></span>
<span id="cb8-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute the Wald confidence interval for a popn. proportion</span></span>
<span id="cb8-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#-----------------------------------------------------------------------------</span></span>
<span id="cb8-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># x        vector of data (zeros and ones)</span></span>
<span id="cb8-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># alpha    1 - (confidence level)</span></span>
<span id="cb8-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#-----------------------------------------------------------------------------</span></span>
<span id="cb8-8">  n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(x)</span>
<span id="cb8-9">  p_hat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(x)</span>
<span id="cb8-10">  SE_hat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(p_hat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p_hat) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n)</span>
<span id="cb8-11">  ME <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> alpha <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> SE_hat</span>
<span id="cb8-12">  CI <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'lower'</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> p_hat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ME, </span>
<span id="cb8-13">          <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'upper'</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> p_hat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ME)</span>
<span id="cb8-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(CI)</span>
<span id="cb8-15">}</span></code></pre></div></div>
</div>
<p>With a computer rather than pen and paper there’s very little cost using the more accurate interval. Indeed, the built-in R function <code>prop.test()</code> reports the Wilson confidence interval rather than the Wald interval:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1234</span>)</span>
<span id="cb9-2">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbinom</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb9-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">prop.test</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(x), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(x), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">correct =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># no continuity correction</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
    1-sample proportions test without continuity correction

data:  sum(x) out of length(x), null probability 0.5
X-squared = 0.2, df = 1, p-value = 0.6547
alternative hypothesis: true p is not equal to 0.5
95 percent confidence interval:
 0.3420853 0.7418021
sample estimates:
   p 
0.55 </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_wilson_CI</span>(x)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>    lower     upper 
0.3420853 0.7418021 </code></pre>
</div>
</div>
</section>
<section id="understanding-the-wilson-interval" class="level2 page-columns page-full" data-number="5">
<h2 data-number="5" class="anchored" data-anchor-id="understanding-the-wilson-interval"><span class="header-section-number">5</span> Understanding the Wilson Interval</h2>
<p>You could stop reading here and simply use the code from above to construct the Wilson interval. But computing is only half the battle: we want to <em>understand</em> our measures of uncertainty. While the Wilson interval may look somewhat strange, there’s actually some very simple intuition behind it. It amounts to a <em>compromise</em> between the sample proportion <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D"> and <img src="https://latex.codecogs.com/png.latex?1/2">.</p>
<p>The Wald estimator is centered around <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D">, but the Wilson interval is not. Manipulating our expression from the previous section, we find that the midpoint of the Wilson interval is <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cwidetilde%7Bp%7D%20&amp;%5Cequiv%20%5Cleft(%5Cfrac%7Bn%7D%7Bn%20+%20c%5E2%7D%20%5Cright)%5Cleft(%5Cwidehat%7Bp%7D%20+%20%5Cfrac%7Bc%5E2%7D%7B2n%7D%5Cright)%20=%20%20%5Cfrac%7Bn%20%5Cwidehat%7Bp%7D%20+%20c%5E2/2%7D%7Bn%20+%20c%5E2%7D%20%5C%5C%0A&amp;=%20%5Cleft(%20%5Cfrac%7Bn%7D%7Bn%20+%20c%5E2%7D%5Cright)%5Cwidehat%7Bp%7D%20+%20%20%5Cleft(%20%5Cfrac%7Bc%5E2%7D%7Bn%20+%20c%5E2%7D%5Cright)%20%5Cfrac%7B1%7D%7B2%7D%5C%5C%0A&amp;=%20%5Comega%20%5Cwidehat%7Bp%7D%20+%20(1%20-%20%5Comega)%20%5Cfrac%7B1%7D%7B2%7D%0A%5Cend%7Baligned%7D%0A"> where the weight <img src="https://latex.codecogs.com/png.latex?%5Comega%20%5Cequiv%20n%20/%20(n%20+%20c%5E2)"> is always strictly between zero and one. In other words, the center of the Wilson interval lies <em>between</em> <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D"> and <img src="https://latex.codecogs.com/png.latex?1/2">. In effect, <img src="https://latex.codecogs.com/png.latex?%5Cwidetilde%7Bp%7D"> pulls us <em>away</em> from extreme values of <img src="https://latex.codecogs.com/png.latex?p"> and <em>towards</em> the middle of the range of possible values for a population proportion. For a fixed confidence level, the smaller the sample size, the more that we are pulled towards <img src="https://latex.codecogs.com/png.latex?1/2">. For a fixed sample size, the higher the confidence level, the more that we are pulled towards <img src="https://latex.codecogs.com/png.latex?1/2">.</p>
<p>Continuing to use the shorthand <img src="https://latex.codecogs.com/png.latex?%5Comega%20%5Cequiv%20n%20/(n%20+%20c%5E2)"> and <img src="https://latex.codecogs.com/png.latex?%5Cwidetilde%7Bp%7D%20%5Cequiv%20%5Comega%20%5Cwidehat%7Bp%7D%20+%20(1%20-%20%5Comega)/2">, we can write the Wilson interval as <img src="https://latex.codecogs.com/png.latex?%0A%5Cwidetilde%7Bp%7D%20%5Cpm%20c%20%5Ctimes%20%5Cwidetilde%7B%5Ctext%7BSE%7D%7D,%20%5Cquad%20%5Cwidetilde%7B%5Ctext%7BSE%7D%7D%20%5Cequiv%20%5Comega%20%5Csqrt%7B%5Cwidehat%7B%5Ctext%7BSE%7D%7D%5E2%20+%20%5Cfrac%7Bc%5E2%7D%7B4n%5E2%7D%7D.%0A"> So what can we say about <img src="https://latex.codecogs.com/png.latex?%5Cwidetilde%7B%5Ctext%7BSE%7D%7D">? It turns out that the value <img src="https://latex.codecogs.com/png.latex?1/2"> is lurking behind the scenes here as well. The easiest way to see this is by <em>squaring</em> <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7B%5Ctext%7BSE%7D%7D"> to obtain <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cwidetilde%7B%5Ctext%7BSE%7D%7D%5E2%20&amp;=%20%5Comega%5E2%5Cleft(%5Cwidehat%7B%5Ctext%7BSE%7D%7D%5E2%20+%20%5Cfrac%7Bc%5E2%7D%7B4n%5E2%7D%20%5Cright)%20=%20%5Cleft(%5Cfrac%7Bn%7D%7Bn%20+%20c%5E2%7D%5Cright)%5E2%20%5Cleft%5B%5Cfrac%7B%5Cwidehat%7Bp%7D(1%20-%20%5Cwidehat%7Bp%7D)%7D%7Bn%7D%20+%20%5Cfrac%7Bc%5E2%7D%7B4n%5E2%7D%5Cright%5D%5C%5C%0A&amp;=%20%5Cfrac%7B1%7D%7Bn%20+%20c%5E2%7D%20%5Cleft%5B%5Cfrac%7Bn%7D%7Bn%20+%20c%5E2%7D%20%5Ccdot%20%5Cwidehat%7Bp%7D(1%20-%20%5Cwidehat%7Bp%7D)%20+%20%5Cfrac%7Bc%5E2%7D%7Bn%20+%20c%5E2%7D%5Ccdot%20%5Cfrac%7B1%7D%7B4%7D%5Cright%5D%5C%5C%0A&amp;=%20%5Cfrac%7B1%7D%7B%5Cwidetilde%7Bn%7D%7D%20%5Cleft%5B%5Comega%20%5Cwidehat%7Bp%7D(1%20-%20%5Cwidehat%7Bp%7D)%20+%20(1%20-%20%5Comega)%20%5Cfrac%7B1%7D%7B2%7D%20%5Ccdot%20%5Cfrac%7B1%7D%7B2%7D%5Cright%5D%0A%5Cend%7Baligned%7D%0A"> defining <img src="https://latex.codecogs.com/png.latex?%5Cwidetilde%7Bn%7D%20=%20n%20+%20c%5E2">. To make sense of this result, recall that <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7B%5Ctext%7BSE%7D%7D%5E2">, the quantity that is used to construct the Wald interval, is a ratio of two terms: <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D(1%20-%20%5Cwidehat%7Bp%7D)"> is the usual estimate of the <em>population variance</em> based on iid samples from a Bernoulli distribution and <img src="https://latex.codecogs.com/png.latex?n"> is the sample size. Similarly, <img src="https://latex.codecogs.com/png.latex?%5Cwidetilde%7B%5Ctext%7BSE%7D%7D%5E2"> is a ratio of two terms. The first is a <em>weighted average</em> of the population variance estimator and <img src="https://latex.codecogs.com/png.latex?1/4">, the population variance under the assumption that <img src="https://latex.codecogs.com/png.latex?p%20=%201/2">. Once again, the Wilson interval “pulls” away from extremes. In this case it pulls away from extreme estimates of the population variance towards the <em>largest possible</em> population variance: <img src="https://latex.codecogs.com/png.latex?1/4">.<sup>2</sup> We divide this by the sample size <em>augmented</em> by <img src="https://latex.codecogs.com/png.latex?c%5E2">, a strictly positive quantity that depends on the confidence level.<sup>3</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn2"><p><sup>2</sup>&nbsp;The value of <img src="https://latex.codecogs.com/png.latex?p"> that maximizes <img src="https://latex.codecogs.com/png.latex?p(1-p)"> is <img src="https://latex.codecogs.com/png.latex?p=1/2"> and <img src="https://latex.codecogs.com/png.latex?(1/2)%5E2%20=%201/4">.</p></div><div id="fn3"><p><sup>3</sup>&nbsp;If you know anything about Bayesian statistics, you may be suspicious that there’s a connection to be made here. Indeed this whole exercise looks very much like a <a href="http://sims.princeton.edu/yftp/DummyObs/DumObsPrior.pdf">dummy observation prior</a> in which we artificially augment the sample with “fake data.” There is a Bayesian connection here, but the details will have to wait for a future post.</p></div><div id="fn4"><p><sup>4</sup>&nbsp;As far as I’m concerned, 1.96 is effectively 2. If you disagree, please replace all instances of “95%” with “95.45%”.</p></div></div><p>To make this more concrete, consider the case of a 95% Wilson interval. In this case <img src="https://latex.codecogs.com/png.latex?c%5E2%20%5Capprox%204"> so that <img src="https://latex.codecogs.com/png.latex?%5Comega%20%5Capprox%20n%20/%20(n%20+%204)"> and <img src="https://latex.codecogs.com/png.latex?(1%20-%20%5Comega)%20%5Capprox%204/(n+4)">.<sup>4</sup> Using this approximation we find that <img src="https://latex.codecogs.com/png.latex?%0A%5Cwidetilde%7Bp%7D%20%5Capprox%20%5Cfrac%7Bn%7D%7Bn%20+%204%7D%20%5Ccdot%20%5Cwidehat%7Bp%7D%20+%20%5Cfrac%7B4%7D%7Bn%20+%204%7D%20%5Ccdot%20%5Cfrac%7B1%7D%7B2%7D%20=%20%20%5Cfrac%7Bn%20%5Cwidehat%7Bp%7D%20+%202%7D%7Bn%20+%204%7D%0A"> which is <em>precisely</em> the midpoint of the <a href="https://www.econometrics.blog/post/don-t-use-the-textbook-ci-for-a-proportion/">Agresti-Coull confidence interval</a>. And while <img src="https://latex.codecogs.com/png.latex?%0A%5Cwidetilde%7B%5Ctext%7BSE%7D%7D%5E2%20%5Capprox%20%5Cfrac%7B1%7D%7Bn%20+%204%7D%20%5Cleft%5B%5Cfrac%7Bn%7D%7Bn%20+%204%7D%5Ccdot%20%5Cwidehat%7Bp%7D(1%20-%20%5Cwidehat%7Bp%7D)%20+%5Cfrac%7B4%7D%7Bn%20+%204%7D%20%5Ccdot%20%5Cfrac%7B1%7D%7B2%7D%20%5Ccdot%20%5Cfrac%7B1%7D%7B2%7D%5Cright%5D%0A"> is slightly different from the quantity that appears in the Agresti-Coull interval, <img src="https://latex.codecogs.com/png.latex?%5Cwidetilde%7Bp%7D(1%20-%20%5Cwidetilde%7Bp%7D)/%5Cwidetilde%7Bn%7D">, the two expressions give very similar results in practice. The Agresti-Coull interval is nothing more than a rough-and-ready approximation to the 95% Wilson interval. This not only provides some intuition for the Wilson interval, it shows us how to construct an Agresti-Coull interval with a confidence level that differs from 95%: just construct the Wilson interval!</p>
</section>
<section id="comparing-the-wald-and-wilson-intervals" class="level2 page-columns page-full" data-number="6">
<h2 data-number="6" class="anchored" data-anchor-id="comparing-the-wald-and-wilson-intervals"><span class="header-section-number">6</span> Comparing the Wald and Wilson Intervals</h2>
<p>Another way of understanding the Wilson interval is to ask how it will differ from the Wald interval when computed from the <em>same dataset</em>. In large samples, these two intervals will be quite similar. This is because <img src="https://latex.codecogs.com/png.latex?%5Comega%20%5Crightarrow%201"> as <img src="https://latex.codecogs.com/png.latex?n%20%5Crightarrow%20%5Cinfty">. Using the expressions from the preceding section, this implies that <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D%20%5Capprox%20%5Cwidetilde%7Bp%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7B%5Ctext%7BSE%7D%7D%20%5Capprox%20%5Cwidetilde%7B%5Ctext%7BSE%7D%7D"> for very large sample sizes. For smaller values of <img src="https://latex.codecogs.com/png.latex?n">, however, the two intervals can differ markedly. To make a long story short, the Wilson interval gives a much more reasonable description of our uncertainty about <img src="https://latex.codecogs.com/png.latex?p"> for any sample size. Wilson, unlike Wald, is always an <em>interval</em>; it cannot collapse to a single point. Moreover, unlike the Wald interval, the Wilson interval is always bounded below by zero and above by one.</p>
<section id="wald-can-collapse-to-a-single-point-wilson-cant" class="level3">
<h3 class="anchored" data-anchor-id="wald-can-collapse-to-a-single-point-wilson-cant">Wald Can Collapse to a Single Point; Wilson Can’t</h3>
<p>A strange property of the Wald interval is that its width can be zero. Suppose that <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D%20=%200">, i.e.&nbsp;that we observe zero successes. In this case, regardless of sample size and regardless of confidence level, the Wald interval only contains a single point: zero <img src="https://latex.codecogs.com/png.latex?%0A%5Cwidehat%7Bp%7D%20%5Cpm%20c%20%5Csqrt%7B%5Cwidehat%7Bp%7D(1%20-%20%5Cwidehat%7Bp%7D)/n%7D%20=%200%20%5Cpm%20c%20%5Ctimes%20%5Csqrt%7B0(1%20-%200)/n%7D%20=%20%5C%7B0%20%5C%7D.%0A"> This is clearly insane. If we observe zero successes in a sample of ten observations, it is reasonable to suspect that <img src="https://latex.codecogs.com/png.latex?p"> is small, but ridiculous to conclude that it must be zero. We encounter a similarly absurd conclusion if <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D%20=%201">. In contrast, the Wilson interval can never collapse to a single point. Using the expression from the preceding section, we see that its width is given by <img src="https://latex.codecogs.com/png.latex?%0A2c%20%5Cleft(%5Cfrac%7Bn%7D%7Bn%20+%20c%5E2%7D%5Cright)%20%5Ctimes%20%5Csqrt%7B%5Cfrac%7B%5Cwidehat%7Bp%7D(1%20-%20%5Cwidehat%7Bp%7D)%7D%7Bn%7D%20+%20%5Cfrac%7Bc%5E2%7D%7B4n%5E2%7D%7D%0A"> The first factor in this product is strictly positive. And even when <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D"> equals zero or one, the second factor is also positive: the additive term <img src="https://latex.codecogs.com/png.latex?c%5E2/(4n%5E2)"> inside the square root ensures this. For <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D"> equal to zero or one, the width of the Wilson interval becomes <img src="https://latex.codecogs.com/png.latex?%0A2c%20%5Cleft(%5Cfrac%7Bn%7D%7Bn%20+%20c%5E2%7D%5Cright)%20%5Ctimes%20%5Csqrt%7B%5Cfrac%7Bc%5E2%7D%7B4n%5E2%7D%7D%20=%20%5Cleft(%5Cfrac%7Bc%5E2%7D%7Bn%20+%20c%5E2%7D%5Cright)%20=%20(1%20-%20%5Comega).%0A"> Compared to the Wald interval, this is quite reasonable. A sample proportion of zero (or one) conveys much more information when <img src="https://latex.codecogs.com/png.latex?n"> is large than when <img src="https://latex.codecogs.com/png.latex?n"> is small. Accordingly, the Wilson interval is shorter for large values of <img src="https://latex.codecogs.com/png.latex?n">. Similarly, higher confidence levels should demand wider intervals at a fixed sample size. The Wilson interval, unlike the Wald, retains this property even when <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D"> equals zero or one.</p>
</section>
<section id="wald-can-include-impossible-values-wilson-cant" class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="wald-can-include-impossible-values-wilson-cant">Wald Can Include Impossible Values; Wilson Can’t</h3>
<p>A population proportion necessarily lies in the interval <img src="https://latex.codecogs.com/png.latex?%5B0,1%5D">, so it would make sense that any confidence interval for <img src="https://latex.codecogs.com/png.latex?p"> should as well. An awkward fact about the Wald interval is that it can extend <em>beyond</em> zero or one. In contrast, the Wilson interval <em>always</em> lies within <img src="https://latex.codecogs.com/png.latex?%5B0,1%5D">. For example, suppose that we observe two successes in a sample of size 10. Then the 95% Wald confidence interval is approximately [-0.05, 0.45] while the corresponding Wilson interval is [0.06, 0.51]. Similarly, if we observe eight successes in ten trials, the 95% Wald interval is approximately [0.55, 1.05] while the Wilson interval is [0.49, 0.94].</p>
<p>With a bit of algebra we can show that the Wald interval will include negative values whenever <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D"> is less than <img src="https://latex.codecogs.com/png.latex?(1%20-%20%5Comega)%20%5Cequiv%20c%5E2/(n%20+%20c%5E2)">. Why is this so? The lower confidence limit of the Wald interval is negative if and only if <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D%20%3C%20c%20%5Ctimes%20%5Cwidehat%7B%5Ctext%7BSE%7D%7D">. Substituting the definition of <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7B%5Ctext%7BSE%7D%7D"> and re-arranging, this is equivalent to <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cwidehat%7Bp%7D%20&amp;%3C%20c%20%5Csqrt%7B%5Cwidehat%7Bp%7D(1%20-%20%5Cwidehat%7Bp%7D)/n%7D%5C%5C%0An%5Cwidehat%7Bp%7D%5E2%20&amp;%3C%20c%5E2(%5Cwidehat%7Bp%7D%20-%20%5Cwidehat%7Bp%7D%5E2)%5C%5C%0A0%20&amp;%3E%20%5Cwidehat%7Bp%7D%5Cleft%5B(n%20+%20c%5E2)%5Cwidehat%7Bp%7D%20-%20c%5E2%5Cright%5D%0A%5Cend%7Baligned%7D%0A"> The right-hand side of the preceding inequality is a quadratic function of <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D"> that opens upwards. Its roots are <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D%20=%200"> and <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D%20=%20c%5E2/(n%20+%20c%5E2)%20=%20(1%20-%20%5Comega)">. Thus, whenever <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D%20%3C%20(1%20-%20%5Comega)">, the Wald interval will include negative values of <img src="https://latex.codecogs.com/png.latex?p">. A nearly identical argument, exploiting symmetry, shows that the upper confidence limit of the Wald interval will extend beyond one whenever <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D%20%3E%20%5Comega%20%5Cequiv%20n/(n%20+%20c%5E2)">. Putting these two results together, the Wald interval lies within <img src="https://latex.codecogs.com/png.latex?%5B0,1%5D"> if and only if <img src="https://latex.codecogs.com/png.latex?(1%20-%20%5Comega)%20%3C%20%5Cwidehat%7Bp%7D%20%3C%20%5Comega">. This is equivalent to <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0An(1%20-%20%5Comega)%20&amp;%3C%20%5Csum_%7Bi=1%7D%5En%20X_i%20%3C%20n%20%5Comega%5C%5C%0A%5Cleft%5Clceil%20n%5Cleft(%5Cfrac%7Bc%5E2%7D%7Bn%20+%20c%5E2%7D%20%5Cright)%5Cright%5Crceil%20&amp;%5Cleq%20%5Csum_%7Bi=1%7D%5En%20X_i%20%5Cleq%20%5Cleft%5Clfloor%20n%20%5Cleft(%20%5Cfrac%7Bn%7D%7Bn%20+%20c%5E2%7D%5Cright)%20%5Cright%5Crfloor%0A%5Cend%7Baligned%7D%0A"> where <img src="https://latex.codecogs.com/png.latex?%5Clceil%20%5Ccdot%20%5Crceil"> is the <a href="https://en.wikipedia.org/wiki/Floor_and_ceiling_functions">ceiling</a> function and <img src="https://latex.codecogs.com/png.latex?%5Clfloor%20%5Ccdot%20%5Crfloor"> is the <a href="https://en.wikipedia.org/wiki/Floor_and_ceiling_functions">floor</a> function.<sup>5</sup> Using this inequality, we can calculate the minimum and maximum number of successes in <img src="https://latex.codecogs.com/png.latex?n"> trials for which a 95% Wald interval will lie inside the range <img src="https://latex.codecogs.com/png.latex?%5B0,1%5D"> as follows:</p>
<div class="no-row-height column-margin column-container"><div id="fn5"><p><sup>5</sup>&nbsp;The final inequality follows because <img src="https://latex.codecogs.com/png.latex?%5Csum_%7Bi%7D%5En%20X_i"> can only take on a value in <img src="https://latex.codecogs.com/png.latex?%5C%7B0,%201,%20...,%20n%5C%7D"> while <img src="https://latex.codecogs.com/png.latex?n%5Comega"> and <img src="https://latex.codecogs.com/png.latex?n(1%20-%20%5Comega)"> may not be integers, depending on the values of <img src="https://latex.codecogs.com/png.latex?n"> and <img src="https://latex.codecogs.com/png.latex?c%5E2">.</p></div></div><div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span></span>
<span id="cb13-2">omega <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qnorm</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.975</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb13-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> n,  </span>
<span id="cb13-4">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"min_success"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ceiling</span>(n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> omega)),  </span>
<span id="cb13-5">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"max_success"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor</span>(n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> omega))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>       n min_success max_success
 [1,] 10           3           7
 [2,] 11           3           8
 [3,] 12           3           9
 [4,] 13           3          10
 [5,] 14           4          10
 [6,] 15           4          11
 [7,] 16           4          12
 [8,] 17           4          13
 [9,] 18           4          14
[10,] 19           4          15
[11,] 20           4          16</code></pre>
</div>
</div>
<p>This agrees with our calculations for <img src="https://latex.codecogs.com/png.latex?n%20=%2010"> from above. With a sample size of ten, any number of successes <em>outside</em> the range <img src="https://latex.codecogs.com/png.latex?%5C%7B3,%20...,%207%5C%7D"> will lead to a 95% Wald interval that extends beyond zero or one. With a sample size of twenty, this range becomes <img src="https://latex.codecogs.com/png.latex?%5C%7B4,%20...,%2016%5C%7D">.</p>
<p>Finally, we’ll show that the Wilson interval can <em>never</em> extend beyond zero or one. There’s nothing more than algebra to follow, but there’s a fair bit of it. If you feel that we’ve factorized too many quadratic equations already, you have my express permission to skip ahead. Suppose by way of contradiction that the lower confidence limit of the Wilson confidence interval were negative. The only way this could occur is if <img src="https://latex.codecogs.com/png.latex?%5Cwidetilde%7Bp%7D%20-%20%5Cwidetilde%7B%5Ctext%7BSE%7D%7D%20%3C%200">, i.e.&nbsp;if <img src="https://latex.codecogs.com/png.latex?%0A%5Comega%5Cleft%5C%7B%5Cleft(%5Cwidehat%7Bp%7D%20+%20%5Cfrac%7Bc%5E2%7D%7B2n%7D%5Cright)%20-%20c%5Csqrt%7B%20%5Cwidehat%7B%5Ctext%7BSE%7D%7D%5E2%20+%20%5Cfrac%7Bc%5E2%7D%7B4n%5E2%7D%7D%20%5C,%5C,%5Cright%5C%7D%20%3C%200.%0A"> But since <img src="https://latex.codecogs.com/png.latex?%5Comega"> is between zero and one, this is equivalent to <img src="https://latex.codecogs.com/png.latex?%0A%5Cleft(%5Cwidehat%7Bp%7D%20+%20%5Cfrac%7Bc%5E2%7D%7B2n%7D%5Cright)%20%3C%20c%5Csqrt%7B%20%5Cwidehat%7B%5Ctext%7BSE%7D%7D%5E2%20+%20%5Cfrac%7Bc%5E2%7D%7B4n%5E2%7D%7D.%0A"> We will show that this leads to a contradiction, proving that lower confidence limit of the Wilson interval <em>cannot be negative</em>. To begin, factorize each side as follows <img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7B1%7D%7B2n%7D%5Cleft(2n%5Cwidehat%7Bp%7D%20+%20c%5E2%5Cright)%20%3C%20%5Cfrac%7Bc%7D%7B2n%7D%5Csqrt%7B%204n%5E2%5Cwidehat%7B%5Ctext%7BSE%7D%7D%5E2%20+%20c%5E2%7D.%0A"> Cancelling the common factor of <img src="https://latex.codecogs.com/png.latex?1/(2n)"> from both sides and squaring, we obtain <img src="https://latex.codecogs.com/png.latex?%0A%5Cleft(2n%5Cwidehat%7Bp%7D%20+%20c%5E2%5Cright)%5E2%20%3C%20c%5E2%5Cleft(4n%5E2%5Cwidehat%7B%5Ctext%7BSE%7D%7D%5E2%20+%20c%5E2%5Cright).%0A"> Expanding, subtracting <img src="https://latex.codecogs.com/png.latex?c%5E4"> from both sides, and dividing through by <img src="https://latex.codecogs.com/png.latex?4n"> gives <img src="https://latex.codecogs.com/png.latex?%0An%5Cwidehat%7Bp%7D%5E2%20+%20%5Cwidehat%7Bp%7Dc%5E2%20%3C%20nc%5E2%5Cwidehat%7B%5Ctext%7BSE%7D%7D%5E2%20=%20c%5E2%20%5Cwidehat%7Bp%7D(1%20-%20%5Cwidehat%7Bp%7D)%20=%20%5Cwidehat%7Bp%7Dc%5E2%20-%20c%5E2%20%5Cwidehat%7Bp%7D%5E2%0A"> by the definition of <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7B%5Ctext%7BSE%7D%7D">. Subtracting <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7Dc%5E2"> from both sides and rearranging, this is equivalent to <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D%5E2(n%20+%20c%5E2)%20%3C%200">. Since the left-hand side cannot be negative, we have a contradiction.</p>
<p>A similar argument shows that the <em>upper</em> confidence limit of the Wilson interval cannot exceed one. Suppose by way of contradiction that it did. This can only occur if <img src="https://latex.codecogs.com/png.latex?%5Cwidetilde%7Bp%7D%20+%20%5Cwidetilde%7BSE%7D%20%3E%201">, i.e.&nbsp;if <img src="https://latex.codecogs.com/png.latex?%0A%5Cleft(%5Cwidehat%7Bp%7D%20+%20%5Cfrac%7Bc%5E2%7D%7B2n%7D%5Cright)%20-%20%5Cfrac%7B1%7D%7B%5Comega%7D%20%3E%20c%20%5Csqrt%7B%5Cwidehat%7B%5Ctext%7BSE%7D%7D%5E2%20+%20%5Cfrac%7Bc%5E2%7D%7B4n%5E2%7D%7D.%0A"> By the definition of <img src="https://latex.codecogs.com/png.latex?%5Comega"> from above, the left-hand side of this inequality simplifies to <img src="https://latex.codecogs.com/png.latex?%0A-%5Cfrac%7B1%7D%7B2n%7D%20%5Cleft%5B2n(1%20-%20%5Cwidehat%7Bp%7D)%20+%20c%5E2%5Cright%5D%0A"> so the original inequality is equivalent to <img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7B1%7D%7B2n%7D%20%5Cleft%5B2n(1%20-%20%5Cwidehat%7Bp%7D)%20+%20c%5E2%5Cright%5D%20%3C%20c%20%5Csqrt%7B%5Cwidehat%7B%5Ctext%7BSE%7D%7D%5E2%20+%20%5Cfrac%7Bc%5E2%7D%7B4n%5E2%7D%7D.%0A"> Now, if we introduce the change of variables <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bq%7D%20%5Cequiv%201%20-%20%5Cwidehat%7Bp%7D">, we obtain <em>exactly</em> the same inequality as we did above when studying the lower confidence limit, only with <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bq%7D"> in place of <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D">. This is because <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7B%5Ctext%7BSE%7D%7D%5E2"> is <em>symmetric</em> in <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D"> and <img src="https://latex.codecogs.com/png.latex?(1%20-%20%5Cwidehat%7Bp%7D)">. Since we’ve reduced our problem to one we’ve already solved, we’re done!</p>
</section>
</section>
<section id="more-to-come-on-inference-for-a-proportion" class="level2" data-number="7">
<h2 data-number="7" class="anchored" data-anchor-id="more-to-come-on-inference-for-a-proportion"><span class="header-section-number">7</span> More to Come on Inference for a Proportion!</h2>
<p>This has been a post of epic proportions, pun very much intended. Amazingly, we have yet to fully exhaust this seemingly trivial problem. In a future post I will explore <em>yet another</em> approach to inference: the likelihood ratio test and its corresponding confidence interval. This will complete the classical “trinity” of tests for maximum likelihood estimation: Wald, Score (Lagrange Multiplier), and Likelihood Ratio. In yet another future post, I will revisit this problem from a Bayesian perspective, uncovering many unexpected connections along the way. Until then, be sure to maintain a sense of proportion in all your inferences and <a href="https://www.econometrics.blog/post/don-t-use-the-textbook-ci-for-a-proportion/">never use the Wald confidence interval for a proportion</a>.</p>
</section>
<section id="appendix-r-code" class="level2" data-number="8">
<h2 data-number="8" class="anchored" data-anchor-id="appendix-r-code"><span class="header-section-number">8</span> Appendix: R Code</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">get_test_size <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(p_true, n, test, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>) {</span>
<span id="cb15-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute the size of a hypothesis test for a population proportion  </span></span>
<span id="cb15-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   p_true    true population proportion</span></span>
<span id="cb15-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   n         sample size</span></span>
<span id="cb15-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   test      function of p_hat, n, and p_0 that computes test stat </span></span>
<span id="cb15-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   alpha     nominal size of the test</span></span>
<span id="cb15-7">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n</span>
<span id="cb15-8">  p_x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbinom</span>(x, n, p_true)</span>
<span id="cb15-9">  test_stats <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">test</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p_hat =</span> x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sample_size =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p0 =</span> p_true)</span>
<span id="cb15-10">  reject <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(test_stats) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> alpha <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb15-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(reject <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> p_x)</span>
<span id="cb15-12">}</span>
<span id="cb15-13">get_score_test_stat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(p_hat, sample_size, p0) {</span>
<span id="cb15-14">  SE_0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(p0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p0) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> sample_size)</span>
<span id="cb15-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>((p_hat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p0) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> SE_0)</span>
<span id="cb15-16">}</span>
<span id="cb15-17">get_wald_test_stat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(p_hat, sample_size, p0) {</span>
<span id="cb15-18">  SE_hat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(p_hat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p_hat) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> sample_size)</span>
<span id="cb15-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>((p_hat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p0) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> SE_hat)</span>
<span id="cb15-20">}</span>
<span id="cb15-21"></span>
<span id="cb15-22">plot_size <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(n, test, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nominal =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>) {</span>
<span id="cb15-23">  p_seq <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">to =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.99</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.001</span>)</span>
<span id="cb15-24">  size <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(p_seq, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(p) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_test_size</span>(p, n, test, nominal))</span>
<span id="cb15-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(p_seq, size, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'l'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'p'</span>, </span>
<span id="cb15-26">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Type I Error Rate'</span>, </span>
<span id="cb15-27">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> title)</span>
<span id="cb15-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.98</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(size), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bquote</span>(n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> .(n)))</span>
<span id="cb15-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> nominal, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb15-30">}</span>
<span id="cb15-31">plot_size_comparison <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nominal =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>) {</span>
<span id="cb15-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb15-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_size</span>(n, get_score_test_stat, nominal, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Score Test'</span>)</span>
<span id="cb15-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_size</span>(n, get_wald_test_stat, nominal, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Wald Test'</span>)</span>
<span id="cb15-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb15-36">}</span></code></pre></div></div>
</div>


</section>


 ]]></description>
  <category>statistics</category>
  <guid>https://www.econometrics.blog/post/the-wilson-confidence-interval-for-a-proportion/</guid>
  <pubDate>Sat, 05 Feb 2022 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Lessons from the Oxford Vaccination Survey</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/lessons-from-the-oxford-vaccination-survey/</link>
  <description><![CDATA[ 




Back in November a colleague pointed me to a website describing the recent <a href="https://www.ox.ac.uk/coronavirus/students/vaccination-survey">COVID-19 Student Vaccination Survey</a> carried out by my employer, the University of Oxford. At the time I briefly tweeted my concerns at the University:
<blockquote class="twitter-tweet blockquote">
<p lang="en" dir="ltr">
Sorry <a href="https://twitter.com/UniofOxford?ref_src=twsrc%5Etfw"><span class="citation" data-cites="UniofOxford">@UniofOxford</span></a>, but this is wildly misleading. Students who choose to reply to a vaccination survey are likely very different from those who choose not to reply. If your estimate is seriously biased, constructing a confidence interval is pointless.<a href="https://t.co/YB7HUa6Y0k">https://t.co/YB7HUa6Y0k</a> <a href="https://t.co/FYQA97QmU1">pic.twitter.com/FYQA97QmU1</a>
</p>
— Francis DiTraglia (<span class="citation" data-cites="economictricks">@economictricks</span>) <a href="https://twitter.com/economictricks/status/1461664580543954945?ref_src=twsrc%5Etfw">November 19, 2021</a>
</blockquote>
<script async="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
<p>but never received a response. In this post, armed with far more than 280 characters, I’ll explain what went wrong in the Oxford Vaccination Survey and suggest some ways of doing better next time.</p>
<section id="what-we-know-about-the-survey" class="level2 page-columns page-full" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="what-we-know-about-the-survey"><span class="header-section-number">1</span> What We Know About the Survey</h2>
<p>While the <a href="https://www.ox.ac.uk/coronavirus/students/vaccination-survey">university website</a> doesn’t provide detailed information on the survey’s methodology, it does allow us to establish a few key facts. First: this was not in fact a survey; it was a <em>census</em>.</p>
<div class="page-columns page-full"><blockquote class="blockquote">
<p>All students were invited to complete the COVID-19 Student Vaccination Survey in 3rd and 4th Weeks of Michaelmas term.<sup>1</sup> This very short form asked students to confirm their COVID-19 vaccination status.</p>
</blockquote><div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;For those of you who live outside the Oxford bubble, “in 3rd and 4th Weeks of Michaelmas term” means in the last week of October and first week of November 2021.</p></div></div></div>
<p>Surveys use a sample to learn about a population. Pollsters don’t ask all American voters if they approve of Biden; they ask a <em>small subset</em> of voters, using a carefully-designed sampling scheme. A census, on the other hand, attempts to reach each person in the target population. This is how the Oxford Vaccination “Survey” was conducted.</p>
<p>Second: roughly half of Oxford students chose not to respond:</p>
<blockquote class="blockquote">
<p>The response rate was 49.3%, and there were virtually no differences in vaccination rates between different colleges and departments.</p>
</blockquote>
<p>Third: among those who did complete the questionnaire, a very high proportion indicated that they were vaccinated:</p>
<blockquote class="blockquote">
<p>A total of 98% of respondents reported that they were vaccinated (95% fully and 3% partially).</p>
</blockquote>
</section>
<section id="how-oxford-interpreted-the-results" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="how-oxford-interpreted-the-results"><span class="header-section-number">2</span> How Oxford Interpreted the Results</h2>
<p>Their <a href="https://www.ox.ac.uk/coronavirus/students/vaccination-survey">headline conclusion</a> was that “the survey indicated that the vast majority of students are now vaccinated.” Further down, under the FAQs, they added the following clarification:</p>
<blockquote class="blockquote">
<p><strong>Given that the response rate was 50%, how can you be sure that the vaccination rate reflects the whole student population?</strong></p>
<p>50% is considered a high response rate in surveys of this type. It allows for a reliable and powerful statistical test to be conducted on the response data. Based on our evaluation, we can be 95% certain that the true vaccination level among all Oxford students (based on those who responded) is between 97.8% and 98.3%. We can also be 99% certain that the true vaccination level among all Oxford students is between 97.7% and 98.4%. In light of this, we can be extremely confident that the vast majority of Oxford students are either fully or partially vaccinated.</p>
</blockquote>
<p>This is wrong. First, the response rates for other “surveys of this type” are irrelevant: what matters is what we can learn <em>from this dataset</em> given the observed response rate and the reasons why students chose not to respond. It seems quite implausible that the vaccination rate among respondents reflects the whole student population. If, as I suspect, conscientious students are both more likely to be vaccinated and more likely to respond to a questionnaire, the 95% figure is almost certainly too high. Second, statistical tests and confidence intervals are designed to quantify <em>sampling error</em>: non-systematic differences in vaccination rates in the sample compared to the population that arise when a sample is drawn at random. But the problem here is <em>non-sampling error</em>: students who respond are likely different in their rate of vaccination from those who do not. This kind of systematic difference between sample and population is not accounted for in a confidence interval or statistical test. In light of this, we cannot conclude that “the vast majority of Oxford students are either fully or partially vaccinated.”</p>
<p>So what <em>can</em> we conclude? It depends on what we’re willing to assume. I’ll start off by considering the worst case: suppose we’re not willing to assume <em>anything</em> about the reasons why some students chose not to respond.</p>
</section>
<section id="worst-case-bounds-for-the-proportion-vaccinated" class="level2 page-columns page-full" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="worst-case-bounds-for-the-proportion-vaccinated"><span class="header-section-number">3</span> Worst-Case Bounds for the Proportion Vaccinated</h2>
<p>I have a bowl containing 100 balls; <img src="https://latex.codecogs.com/png.latex?W"> of them are white and the rest are black. I remove 50 balls from the bowl and lay them on the table so you can see them all at once. Of these, 49 are white and 1 is black. What can you conclude about <img src="https://latex.codecogs.com/png.latex?W">? The answer depends crucially on <em>how I chose which balls to show you</em>.</p>
<p>If I drew the balls at random, then you can use standard statistical tools–hypothesis testing, confidence intervals, a Bayesian posterior distribution–to make inferences about <img src="https://latex.codecogs.com/png.latex?W">. Random sampling ensures that the balls you see before you on the table are <em>representative</em> of the balls that remain in the bowl. This creates an inferential link between what you can observe and what you can’t, and allows you to quantify uncertainty about <img src="https://latex.codecogs.com/png.latex?W"> using the language of probability.</p>
<p>But what if you know <em>nothing</em> about how I chose which balls to show you? Perhaps I decided to show you all of the white balls in the urn; or perhaps I decided to show you all of the black balls: you have no idea. In this case the inferential link between what you can see on the table and what remains in the bowl is broken and the familiar tools of statistical inference cannot be directly applied. Unless you are willing to take a stand of some kind on how I chose which balls to remove from the urn, all that you can conclude about <img src="https://latex.codecogs.com/png.latex?W"> is that it must be at least 49 and no more than 99. Based on an observed fraction of 49/50 white balls on the table, you can conclude only that between 49/100 and 99/100 of the balls in the bowl are white.</p>
<p>Now replace 100 balls in the bowl with 25820 students in the university, 50 balls on the table with 12729 respondents to the questionnaire, and 49 white balls on the table with 12475 vaccinated students among those who responded.<sup>2</sup> Suppose for simplicity that everyone who responds to the questionnaire does so truthfully. (We’ll revisit this below.) Unless we know something about how and why respondents decided to respond, all we can infer from this information is that no fewer than 12475 and no more than 25566 Oxford students have been vaccinated. Expressed as a proportion, this works out to between 48% and 99% of students at Oxford with at least one COVID-19 vaccination.</p>
<div class="no-row-height column-margin column-container"><div id="fn2"><p><sup>2</sup>&nbsp;These are my approximations based on the information taken from the <a href="https://www.ox.ac.uk/coronavirus/students/vaccination-survey">survey website</a> and <a href="https://www.ox.ac.uk/about/facts-and-figures/student-numbers">student numbers for the University of Oxford</a>.</p></div><div id="fn3"><p><sup>3</sup>&nbsp;A 95% confidence interval for an unknown proportion <img src="https://latex.codecogs.com/png.latex?p"> contains the values of <img src="https://latex.codecogs.com/png.latex?p"> that we cannot reject based on a hypothesis test with significance level <img src="https://latex.codecogs.com/png.latex?0.05">.</p></div></div><p>While this result, [0.48, 0.99], looks superficially similar to a confidence interval, it’s a completely different animal. A confidence interval contains the values of an unknown quantity that are “consistent with the data” in a precise sense.<sup>3</sup> As explained above, confidence intervals quantify uncertainty arising from <em>sampling error</em>. But our uncertainty in the vaccine example does <em>not</em> come from random sampling: it comes from <em>missing data</em>. In effect, we have a complete census of the kind of Oxford student who fills out COVID-19 vaccination questionnaires. What we lack is any data whatsoever on the kind of Oxford student who <em>doesn’t reply</em>. Each point in the interval [0.48, 0.99] corresponds to a different assumption about the relative vaccination rates of respondents compared to non-respondents (the fraction of white balls on the table compared to the fraction remaining in the bowl).</p>
<p>Besides reporting the rather pessimistic bound [0.48, 0.99], is there anything more that we can say about the share of vaccinated Oxford students? Yes, but only if we’re willing to make some assumptions.</p>
</section>
<section id="missing-completely-at-random" class="level2 page-columns page-full" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="missing-completely-at-random"><span class="header-section-number">4</span> Missing (Completely) at Random?</h2>
<p>Let’s start with the strongest possible assumption: data that are <em>missing completely at random</em> (MCAR). In the vaccination example, MCAR amounts to assuming that students who respond are representative of students who do not respond. In the language of my balls-and-bowl example, MCAR would hold if the 50 balls on the table were drawn from the bowl at random. Under MCAR, we can effectively ignore the problem of non-response and report an estimate of 95% vaccine coverage among Oxford students. But while it’s logically <em>possible</em> for the MCAR assumption to hold in this example, it’s not very plausible. As I mentioned above, it seems likely that more conscientious students will both be more likely to get vaccinated and to reply to a questionnaire. If so, MCAR fails.</p>
<p>More plausible than MCAR is the assumption of data that are <em>missing at random</em> (MAR). This idea is best illustrated with an example. Suppose that there are <em>four kinds of balls</em> in the bowl: large white balls, large black balls, small white balls, and small black balls. We’re not interested in the size of the balls; we only want to know how many are white and how many are black. As before, I draw 50 balls from the bowl at random and lay them on the table. Unfortunately, small balls tend to sink to the bottom of the bowl so I’m disproportionately likely to draw a large ball. In this case, the balls you can see on the table are no longer representative of the balls in the bowl: there are probably <em>too many</em> large balls on the table.</p>
<p>To make progress we need an assumption. Suppose that, after accounting for size, the chance that a I draw a <em>particular ball</em> doesn’t depend on its color. Under this assumption the <em>large balls</em> on the table are representative of the large balls that remain in the bowl. Similarly, the <em>small balls</em> on the table are representative of the small balls that remain in the bowl. This is precisely the MAR assumption: conditional on something we know (the size of a ball), the data we can observe (balls on the table) are representative of the missing data (balls in the bowl). MCAR does not hold because the 50 balls on the table are <em>not</em> representative of the 50 balls that remain in the urn (large balls are disproportionately likely to be drawn). They only become representative <em>after</em> we account for size.</p>
<p>So how might the MAR assumption allow us to learn more in the vaccination example? From the figures on page 85 of this <a href="https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/1041447/Weekly_Flu_and_COVID-19_report_w50.pdf">HSA report</a> we see that, within each UK age group, females are more likely to be vaccinated than males. Suppose that this holds among Oxford students as well. If there is any difference between survey response rates for male and female students, MCAR fails and the 95% estimated vaccination rate will be unreliable. But suppose we are willing to assume that female respondents are representative of female non-respondents. In this case, the share of vaccinated female <em>respondents</em> is a good estimate of the share of vaccinated female <em>students</em>, and similarly for male respondents. Because we know that 49% of Oxford students are female, we can use this information to calculate a good estimate of the overall share of vaccinated students.<sup>4</sup> If MAR conditional on sex seems like too strong an assumption, perhaps you’d be willing to assume that female <em>undergraduates from the UK</em> who respond are representative of all female undergraduates from the UK. After all: overseas students are <em>almost certainly</em> vaccinated, given government travel restrictions. In this case we’d form eight groups (male/female <img src="https://latex.codecogs.com/png.latex?%5Ctimes"> overseas/home <img src="https://latex.codecogs.com/png.latex?%5Ctimes"> undergrad/post-grad), estimate the share of vaccination separately for each group using the data for respondents. To obtain an overall estimate for the share of vaccinated students, we’d simply average the estimates for each “type” of students, weighting by their respective shares in the Oxford student body. This approach of re-weighting estimates for particular groups is called <em>post-stratification</em> and is widely used in political polling.<sup>5</sup> The assumption that underlies it is MAR: conditional on characteristics we can observe, whether a person responds to the survey or not is “as good as random,” i.e.&nbsp;unrelated to the question we ask in our poll.</p>
<div class="no-row-height column-margin column-container"><div id="fn4"><p><sup>4</sup>&nbsp;I computed the share of female students from the University statistics posted at this url: <a href="https://www.ox.ac.uk/about/facts-and-figures/student-numbers" class="uri">https://www.ox.ac.uk/about/facts-and-figures/student-numbers</a>.</p></div><div id="fn5"><p><sup>5</sup>&nbsp;An example I particularly enjoy is <a href="https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.708.7828&amp;rep=rep1&amp;type=pdf">this paper</a> by Andrew Gelman and co-authors.</p></div></div><p>So does MAR hold in our vaccination example? Perhaps not. Getting vaccinated is widely viewed as a pro-social act. The phenomenon of <a href="https://en.wikipedia.org/wiki/Social-desirability_bias">social desirability bias</a> suggests that people who are not vaccinated may be less likely to respond <em>even after</em> conditioning on their observed characteristics. If so MAR will not hold. Even so, I would put <em>much more faith</em> in results that used post-stratification to adjust for observed characteristics like sex and home/overseas that are plausibly related to both response rates and vaccination status. Depending on the precise nature of the data that Oxford collected, it’s possible that this analysis could still be carried out.</p>
</section>
<section id="designing-a-better-survey" class="level2 page-columns page-full" data-number="5">
<h2 data-number="5" class="anchored" data-anchor-id="designing-a-better-survey"><span class="header-section-number">5</span> Designing a Better Survey</h2>
<p>There’s an old saying (often attributed to Fisher) that calling in a statistician after you’ve collected your data is like calling in a doctor after a loved one has died: all she can do is perform a post-mortem; it’s too late to save the patient. So how could Oxford do a better job <em>the next time</em> that they want to carry out a survey?</p>
<p>One of the key lessons of statistics is that a relatively small sample can still provide reliable inferences about the population provided that the sample is drawn at random. Rather than contacting all students, choose a random sample and focus on <em>maximizing the response rate</em>. There are several ways to do this. First is multi-mode surveys: send an email in addition to a letter in addition to a text message. Second is incentives: perhaps offer a gift card upon receipt of the completed survey. In spite of your best efforts, inevitably some people <em>still</em> won’t reply. This is where two-stage sampling can help: take a second random sample of the non-respondents and contact these people a second time. As long as you reach a <em>representative</em> sample of non-respondents, it doesn’t matter if you reach them all. If some non-response remains, try to adjust for it using post-stratification.</p>
<p>All of the discussion thus far has assumed that respondents answer truthfully. But social desirability bias might also lead people to mis-represent their true vaccination status. This is a much harder problem to solve, but ensuring privacy can help. Posting a <a href="https://www.ox.ac.uk/coronavirus/students/vaccination-survey/privacy-policy">detailed privacy policy</a> is a helpful first step, but students may still be suspicious when a government regulator asks their university to gather potentially sensitive data about them. <a href="https://graemeblair.com/papers/randresp.pdf">Randomized response methods</a> address this concern by designing surveys in which it is <em>impossible</em> for researchers or anyone with whom they share data to infer individual responses. How does this work? In the simplest possible example, I give you a coin and tell you to flip it in secret. I instruct you to answer the survey truthfully if you flip heads, and to simply check “NO I AM NOT VACCINATED” if you flip tails. When I read the survey, I have no way of knowing if you haven’t been vaccinated or merely flipped tails. In spite of this, it’s <em>still</em> possible to construct a reliable estimator of the share of people who are vaccinated.<sup>6</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn6"><p><sup>6</sup>&nbsp;If you’re studying introductory probability and statistics, see if you can figure out how. It’s a great practice exam question!</p></div></div></section>
<section id="a-new-years-resolution" class="level2" data-number="6">
<h2 data-number="6" class="anchored" data-anchor-id="a-new-years-resolution"><span class="header-section-number">6</span> A New Year’s Resolution</h2>
<p>Numbers are only valuable when they tell us something meaningful about the world. If we care enough to ask the question, we should care enough to do a good job answering it. While statistical inference and modeling can be extremely valuable tools for learning about the world, what matters most is collecting good, clean data. May you be blessed with 100% response rates in 2022. If you’re not, make it your new year’s resolution to do something about it!</p>


</section>


 ]]></description>
  <category>statistics</category>
  <category>applied</category>
  <guid>https://www.econometrics.blog/post/lessons-from-the-oxford-vaccination-survey/</guid>
  <pubDate>Fri, 31 Dec 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Street Fighting Numerical Analysis - Part I</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/street-fighting-numerical-analysis-part-1/</link>
  <description><![CDATA[ 




<p>Computing is a crucial part of modern applied and theoretical econometrics but most economists, myself included, have little if any formal training in numerical analysis and computer science. This means that we often learn things <em>the hard way</em>: by making boneheaded mistakes and spending hours browsing <a href="https://stackoverflow.com/">stackoverflow</a> to try to figure out what went wrong. In preparation for my upcoming course on <a href="https://empirical-methods.com">Empirical Research Methods</a><sup>1</sup> I’ve started trying to collect and organize the various nuggets of computational wisdom that I’ve picked up over the years. This post is the first of several that I plan to write on that theme. Its origin is an enigmatic bug that I detected in a seemingly trivial line of my R code involving <code>rep()</code>.</p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;Content coming soon!</p></div></div><section id="is-r-broken" class="level2 page-columns page-full" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="is-r-broken"><span class="header-section-number">1</span> Is R broken?</h2>
<p>For no particular reason, let’s use the R function <code>rep()</code> to print out the string <code>"econometrics.blog"</code> four times:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"econometrics.blog"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "econometrics.blog" "econometrics.blog" "econometrics.blog"
[4] "econometrics.blog"</code></pre>
</div>
</div>
<p>Since 0.2 multiplied by 20 equals 4, it comes as no surprise that replacing <code>times = 4</code> with <code>times = 0.2 * 20</code> gives the same result:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"econometrics.blog"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "econometrics.blog" "econometrics.blog" "econometrics.blog"
[4] "econometrics.blog"</code></pre>
</div>
</div>
<p>Now let’s try <code>times = (1 - 0.8) * 20</code> instead. Since 0.2 equals (1 - 0.8) this couldn’t possibly make a difference, could it? Distressingly, it does: we obtain only <em>three</em> copies of <code>"econometrics.blog"</code></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"econometrics.blog"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "econometrics.blog" "econometrics.blog" "econometrics.blog"</code></pre>
</div>
</div>
<p>What on earth is going on here? Has R made some kind of mistake? Let’s try a sanity check. First we’ll calculate <code>(1 - 0.8) * 20</code> and call it <code>x</code>. Then we’ll check that <code>x</code> really does equal four:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span></span>
<span id="cb7-2">x</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 4</code></pre>
</div>
</div>
<p>What a relief: surely setting <code>times = x</code> should give us four copies of <code>"econometrics.blog"</code>. Alas, it does not:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'econometrics.blog'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">times =</span> x) </span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "econometrics.blog" "econometrics.blog" "econometrics.blog"</code></pre>
</div>
</div>
<p>Clearly using open-source software like R is a bad idea and I should switch to STATA.<sup>2</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn2"><p><sup>2</sup>&nbsp;Health warning: this sentence is satire. The author does not condone the use of STATA or other closed-source statistical packages.</p></div></div></section>
<section id="numeric-types-in-r" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="numeric-types-in-r"><span class="header-section-number">2</span> Numeric Types in R</h2>
<p>Because R is a <a href="https://en.wikipedia.org/wiki/Type_system#DYNAMIC">dynamically-typed</a> programming language, we can almost always ignore the question of precisely how it stores numeric values “under the hood.” In fact R has <em>two</em> numeric types: integer and double. Integers are rare in practice. The operator <code>:</code> returns an integer vector</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span>
<span id="cb11-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">typeof</span>(y)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "integer"</code></pre>
</div>
</div>
<p>and the length of a vector is always an integer</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(y)</span>
<span id="cb13-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">typeof</span>(n)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "integer"</code></pre>
</div>
</div>
<p>but nearly every other numeric value you encounter in R will be stored as a double, i.e.&nbsp;a double precision <a href="https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html">floating point number</a>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">typeof</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "double"</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">typeof</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "double"</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">typeof</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cos</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "double"</code></pre>
</div>
</div>
<p>To <em>force</em> R to store a value as an integer rather than double, we can either append an <code>L</code></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1">z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>L</span>
<span id="cb21-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">typeof</span>(z)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "integer"</code></pre>
</div>
</div>
<p>or <em>coerce</em>, i.e.&nbsp;convert, a double to an integer using <code>as.integer()</code></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">a <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span></span>
<span id="cb23-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">typeof</span>(a)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "double"</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1">b <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(a)</span>
<span id="cb25-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">typeof</span>(b)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "integer"</code></pre>
</div>
</div>
<p>The trade-off between integers and doubles is between precision and range. Calculations carried out with integers are always <em>exact</em>, but integers can only be used to represent a fairly limited number of values. Calculations with doubles, on the other hand, are not <em>always</em> exact, but doubles can store a much larger range of values, including decimals.</p>
<p>This post isn’t the right place to delve into the details of floating point numbers, of which doubles are an instance, but there are two points worth emphasizing. First, it’s generally safe to store a value that is “truly” an integer, e.g.&nbsp;<code>4</code>, as double. As explained in the help file <code>integer {base}</code></p>
<blockquote class="blockquote">
<p>current implementations of R use 32-bit integers for integer vectors, so the range of representable integers is restricted to about +/-2*10^9: doubles can hold much larger integers exactly.</p>
</blockquote>
<p>This explains why you may never have encountered the <code>L</code> suffix in the wild. Because doubles can represent very large integers <em>exactly</em>, calculations with whole numbers stored as doubles will also be exact. Notice that R automatically converts integers that are “too big” into doubles:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">999999999</span>L</span>
<span id="cb27-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">typeof</span>(x)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "integer"</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9999999999</span>L</span>
<span id="cb29-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">typeof</span>(y)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "double"</code></pre>
</div>
</div>
<p>While converting integers to doubles in innocuous, you need to be <em>careful</em> when converting doubles to integers. This turns out to be the root of our problem with <code>rep()</code> from above. Both <code>0.2 * 20</code> and <code>(1 - 0.8) * 20</code> are doubles, and both <em>appear</em> to equal <code>4</code></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span></span>
<span id="cb31-2">x</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 4</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">typeof</span>(x)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "double"</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span></span>
<span id="cb35-2">y</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 4</code></pre>
</div>
</div>
<p>But <code>x</code> and <code>y</code> are coerced to <em>different</em> integer values:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb37-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(x)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 4</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(y)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3</code></pre>
</div>
</div>
<p>The function <code>rep()</code> expects its second argument <code>times</code> to be an integer. If we supply a double instead, then <code>rep()</code> makes a conversion in the same way as the function <code>as.integer()</code>, namely by <em>truncating</em>. Far down in the help file for <code>rep()</code> we find this crucial caveat:</p>
<blockquote class="blockquote">
<p>Non-integer values of <code>times</code> will be truncated towards zero. If times is a computed quantity it is prudent to add a small fuzz or use <code>round()</code>.</p>
</blockquote>
<p>But wait: aren’t <code>x</code> and <code>y</code> <em>precisely equal</em> to each other? How can one truncate to <code>4</code> while the other truncates to <code>3</code>? As it turns out, appearances can be deceiving:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb41" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb41-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">identical</span>(x, y)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] FALSE</code></pre>
</div>
</div>
<p>To find out <em>why</em> these values aren’t equal, we need to learn a bit more about how computers approximate real numbers using doubles.</p>
</section>
<section id="what-you-see-isnt-always-what-you-get." class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="what-you-see-isnt-always-what-you-get."><span class="header-section-number">3</span> What you see isn’t always what you get.</h2>
<p>R has various handy built-in constants, including <img src="https://latex.codecogs.com/png.latex?%5Cpi"></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb43" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb43-1">pi</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3.141593</code></pre>
</div>
</div>
<p>Notwithstanding <a href="https://en.wikipedia.org/wiki/Indiana_Pi_Bill">bill number 246</a> of the 1897 sitting of the Indiana General Assembly, <img src="https://latex.codecogs.com/png.latex?%5Cpi"> is an irrational number. By default, however, R only shows us a small number of its digits. To display twenty digits of <img src="https://latex.codecogs.com/png.latex?%5Cpi">, we can specify the argument <code>digits</code> to the function <code>print()</code> like so</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb45" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb45-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(pi, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3.141592653589793116</code></pre>
</div>
</div>
<p>To see <em>even more</em> digits, we can use the function <code>sprintf()</code>. Let’s try to display 60 digits of <img src="https://latex.codecogs.com/png.latex?%5Cpi">:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb47" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb47-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%.60f"</span>, pi)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "3.141592653589793115997963468544185161590576171875000000000000"</code></pre>
</div>
</div>
<p>Why do the last twelve decimal points display as zero? The answer is that computers cannot represent real numbers to infinite precision. At some point, the remaining digits of <img src="https://latex.codecogs.com/png.latex?%5Cpi"> get chopped off, and we’re left with an approximation that’s more than sufficient for any practical application.</p>
<p>At first glance, the number 0.8 seems nothing like <img src="https://latex.codecogs.com/png.latex?%5Cpi">. It is, after all, a rational number: 4/5. But <code>sprintf()</code> reveals that there’s more here than meets the eye:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb49" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb49-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%.54f"</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "0.800000000000000044408920985006261616945266723632812500"</code></pre>
</div>
</div>
<p>The fraction 4/5 cannot be represented exactly as a double; it can only be <em>approximated</em>. The same is true of 1/5. Because <code>0.2</code> and <code>0.8</code> can only be represented approximately, <code>1 - 0.8</code> and <code>0.2</code> turn out <em>not</em> to be equal from the computer’s perspective:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb51" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb51-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">identical</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] FALSE</code></pre>
</div>
</div>
<p>This in turn explains why <code>(1 - 0.8) * 20</code> and <code>0.2 * 20</code> truncate to different integer values:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb53" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb53-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%.54f"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "3.999999999999999111821580299874767661094665527343750000"
[2] "4.000000000000000000000000000000000000000000000000000000"</code></pre>
</div>
</div>
</section>
<section id="which-decimals-have-an-exact-representation" class="level2" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="which-decimals-have-an-exact-representation"><span class="header-section-number">4</span> Which decimals have an exact representation?</h2>
<p>The fraction <code>1/3</code> lacks a finite decimal expansion. You might guess that R would simply store <code>1/3</code> as a zero, followed by a decimal, followed by a large number of <code>3</code>s. But in fact it does not:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb55" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb55-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%.54f"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "0.333333333333333314829616256247390992939472198486328125"</code></pre>
</div>
</div>
<p>Every digit from the <code>1</code> onward is <em>wrong</em>. The fraction <code>1/10</code>, on the other hand, clearly <em>does</em> have a finite decimal expansion, <code>0.1</code>, but R gets this one wrong as well:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb57" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb57-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%.54f"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "0.100000000000000005551115123125782702118158340454101562"</code></pre>
</div>
</div>
<p>At the same time, it handles <code>1/4</code> with perfect accuracy:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb59" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb59-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%.54f"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "0.250000000000000000000000000000000000000000000000000000"</code></pre>
</div>
</div>
<p>What’s going on? Here’s a clue: the fraction <code>1/32</code> can also be represented exactly as a double. See if you can figure out why before reading further.</p>
<p>The reason why <code>1/3</code> lacks a terminating decimal expansion is that it can’t be written as a counting number divided by a power of ten. In other words, we can’t find values <img src="https://latex.codecogs.com/png.latex?m,%20n%20%5Cin%20%5Cmathbb%7BN%7D"> such that <img src="https://latex.codecogs.com/png.latex?m/10%5En"> equals <img src="https://latex.codecogs.com/png.latex?1/3">. In contrast, <code>3/4</code> has a finite decimal expansion because it equals <code>75/100</code>, corresponding to <img src="https://latex.codecogs.com/png.latex?m%20=%2075"> and <img src="https://latex.codecogs.com/png.latex?n%20=%202">. Of course I’ve left off a crucial qualification: wherever I wrote “finite decimal expansion” above, I should have added “in base 10.” <em>The same number could have a terminating decimal expansion in one base and not in another.</em></p>
<p>Although R displays numbers on the screen in base 10, it represents and computes with them in binary. So the question becomes: which values have a terminating decimal expansion in base 2? To find out, simply replace <code>10</code> with <code>2</code> in the expression from above. A rational number has a terminating decimal expansion base 2 if it can be written as <img src="https://latex.codecogs.com/png.latex?m/2%5En"> for some <img src="https://latex.codecogs.com/png.latex?m,%20n%20%5Cin%20%5Cmathbb%7BN%7D">. Since <code>1/4</code> equals <img src="https://latex.codecogs.com/png.latex?1/2%5E2">, it has an exact representation. Since <code>3/4</code> can be written as <code>3/2^2</code> it <em>also</em> has an exact representation. In contrast, <code>1/5</code> lacks an exact representation because there are no natural numbers <img src="https://latex.codecogs.com/png.latex?m,n"> such that <img src="https://latex.codecogs.com/png.latex?5%20=%202%5En/m">. We can get <em>close</em> by making <img src="https://latex.codecogs.com/png.latex?n"> large and choosing <img src="https://latex.codecogs.com/png.latex?m"> carefully, but we can never satisfy this equation exactly.</p>
</section>
<section id="take-home-lesson" class="level2" data-number="5">
<h2 data-number="5" class="anchored" data-anchor-id="take-home-lesson"><span class="header-section-number">5</span> Take-home Lesson</h2>
<p>High-level programming languages like R and Python are extremely convenient: they allow us to focus on the big picture rather than writing line after line of boilerplate code. But we should never forget that computers <em>cannot</em> represent all numeric values with perfect accuracy. Sometimes this matters. In R, coercing from integer to double is safe but the reverse can be risky, as we have gleaned from a deceptively simple example involving <code>rep()</code>. To learn more about the subtleties of R, I highly recommend <a href="https://www.burns-stat.com/pages/Tutor/R_inferno.pdf">The R Inferno</a> by Patrick Burns and <a href="https://adv-r.hadley.nz/">Advanced R</a> by Hadley Wickham. Despite what you may have heard, R is quite a capable language, but it does have some quirks!</p>


</section>


 ]]></description>
  <category>computing</category>
  <guid>https://www.econometrics.blog/post/street-fighting-numerical-analysis-part-1/</guid>
  <pubDate>Fri, 29 Oct 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Don’t Use the Textbook CI for a Proportion</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/don-t-use-the-textbook-ci-for-a-proportion/</link>
  <description><![CDATA[ 




<p>In a <a href="https://www.econometrics.blog/post/thirty-isn-t-the-magic-number/">previous post</a> I showed an example in which the “textbook” confidence interval for a proportion performs poorly despite a fairly large sample size. My aim in that post was to convince you that the oft-repeated advice concerning <img src="https://latex.codecogs.com/png.latex?n%20%3E%2030"> and the central limit theorem is worthless. Today I’d like to convince you of something even more subversive: the textbook confidence interval for a proportion is absolutely terrible and you should never use it or teach it under any circumstances. Fortunately there’s a simple fix. For a 95% interval, simply add four “fake” observations to your dataset, two successes and two failures, and then follow the textbook recipe for this “artificially augmented” dataset.</p>
<p>This post draws on two very approachable papers from around the turn of the millennium: <a href="https://www.tandfonline.com/doi/abs/10.1080/00031305.1998.10480550">Agresti &amp; Coull (1998)</a> and <a href="https://projecteuclid.org/journals/statistical-science/volume-16/issue-2/Interval-Estimation-for-a-Binomial-Proportion/10.1214/ss/1009213286.full">Brown, Cai, &amp; DasGupta (2001)</a>. More theoretically-inclined readers may also enjoy a companion paper to the latter reference: <a href="https://projecteuclid.org/journals/annals-of-statistics/volume-30/issue-1/Confidence-Intervals-for-a-binomial-proportion-and-asymptotic-expansions/10.1214/aos/1015362189.full">Brown, Cai, &amp; DasGupta (2002)</a>.</p>
<section id="the-wald-confidence-interval" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="the-wald-confidence-interval"><span class="header-section-number">1</span> The Wald Confidence Interval</h2>
<p>This is what you learned in your introductory statistics course: it’s the textbook interval I alluded to above. Let <img src="https://latex.codecogs.com/png.latex?X_1,%20...,%20X_n%20%5Csim%20%5Ctext%7Biid%20Bernoulli%7D(p)"> and define <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D%20=%20%5Csum_%7Bi=1%7D%5En%20X_i/n">. By the central limit theorem <img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7B%5Cwidehat%7Bp%7D%20-%20p%7D%7B%5Csqrt%7B%5Cwidehat%7Bp%7D(1%20-%20%5Cwidehat%7Bp%7D)/n%7D%7D%20%5Crightarrow_d%20N(0,1)%0A"> leading to the so-called “Wald” 95% confidence interval for a population proportion: <img src="https://latex.codecogs.com/png.latex?%0A%5Cwidehat%7Bp%7D%20%5Cpm%201.96%20%5Ctimes%20%5Csqrt%7B%5Cfrac%7B%5Cwidehat%7Bp%7D(1-%20%5Cwidehat%7Bp%7D)%7D%7Bn%7D%7D.%0A"></p>
</section>
<section id="an-obvious-objection-to-the-wald-interval" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="an-obvious-objection-to-the-wald-interval"><span class="header-section-number">2</span> An Obvious Objection to the Wald Interval</h2>
<p>Suppose we want to estimate the proportion of Trump voters in Berkeley California. We decide to carry out a poll of 25 randomly-sampled Berkeley residents and find that none of them voted for Trump. Then <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D%20=%200"> and the Wald confidence interval is <img src="https://latex.codecogs.com/png.latex?%0A0%20%5Cpm%201.96%20%5Ctimes%20%5Csqrt%7B0%20%5Ctimes%20(1%20-%200)%20/%2025%7D%0A"> in other words <img src="https://latex.codecogs.com/png.latex?%5B0,%200%5D">. Clearly this is absurd: unless there are literally <em>zero</em> Trump voters in Berkeley, we can be <em>certain</em> that this interval does not contain the true population parameter. A similar problem would emerge if we instead tried to estimate the proportion of Biden voters in Berkeley: if <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D%20=%201"> then our confidence interval would be <img src="https://latex.codecogs.com/png.latex?%5B1,1%5D">. This too is absurd. More broadly, the Wald confidence interval is extremely poorly behaved in situations where <img src="https://latex.codecogs.com/png.latex?p"> is close to zero or one. You may have encountered a suggestion that <img src="https://latex.codecogs.com/png.latex?np(1-p)"> should be at least 5 for the Wald interval to perform well. There’s something to this advice, as we’ll see below, but it’s not sufficient. More to the point: we don’t <em>know</em> <img src="https://latex.codecogs.com/png.latex?p"> in practice so there is no way to apply this rule!</p>
</section>
<section id="the-agresti-coull-interval" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="the-agresti-coull-interval"><span class="header-section-number">3</span> The Agresti-Coull Interval</h2>
<p>Here’s a quick and dirty fix that is surprisingly effective. Simply add four “fake” observations to the dataset: two zeros (failures) and two ones (successes). The 95% Agresti-Coull confidence interval is constructed in exactly the same way as 95% Wald interval only using this “artificially augmented” dataset rather than the original one. In other words, if the sample size is <img src="https://latex.codecogs.com/png.latex?n"> and the sample proportion is <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D">, then Agresti-Coull interval is constructed from <img src="https://latex.codecogs.com/png.latex?%5Cwidetilde%7Bn%7D%20=%20n%20+%204"> and <img src="https://latex.codecogs.com/png.latex?%0A%5Cwidetilde%7Bp%7D%20%5Cequiv%20%5Cfrac%7Bn%20%5Cwidehat%7Bp%7D%20+%202%7D%7Bn%20+%204%7D%20=%20%5Cfrac%7B%5Cleft(%5Csum_%7Bi=1%7D%5En%20X_i%5Cright)%20+%200%20+%200%20+%201%20+%201%7D%7Bn%20+%204%7D%0A"> yielding <img src="https://latex.codecogs.com/png.latex?%0A%5Cwidetilde%7Bp%7D%20%5Cpm%201.96%20%5Ctimes%20%5Csqrt%7B%5Cfrac%7B%5Cwidetilde%7Bp%7D(1%20-%20%5Cwidetilde%7Bp%7D)%7D%7B%5Cwidetilde%7Bn%7D%7D%7D,%20%5Cquad%20%5Cwidetilde%7Bn%7D%20%5Cequiv%20n%20+%204,%20%5Cquad%20%5Cwidetilde%7Bp%7D%20%5Cequiv%20%5Cfrac%7Bn%5Cwidehat%7Bp%7D+%202%7D%7Bn%20+%204%7D.%0A"> Note that this “add four fake observations” adjustment is specific to the case of a 95% confidence interval. In a future post, I’ll explain where the rule comes from and how to generalize it to other confidence levels. For now, let’s ask ourselves a more fundamental question: does this adjustment make sense? “Wait!” I can hear you object: “adding fake observations introduces a <em>bias</em>!” Indeed it does. Since <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D"> is an unbiased estimator of <img src="https://latex.codecogs.com/png.latex?p">, <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Ctext%7BBias%7D(%5Cwidetilde%7Bp%7D)%20&amp;%5Cequiv%20%5Cmathbb%7BE%7D%5B%5Cwidetilde%7Bp%7D%20-%20p%5D%20=%20%5Cmathbb%7BE%7D%5Cleft%5B%20%5Cfrac%7Bn%20%5Cwidehat%7Bp%7D%20+%202%7D%7Bn%20+%204%7D%5Cright%5D%20-%20p%5C%5C%0A&amp;=%20%5Cfrac%7Bnp%20+%202%7D%7Bn+4%7D%20-%20p%20%20=%20%20(1%20-%202p)%20%5Cleft(%5Cfrac%7B2%7D%7Bn%20+%204%7D%5Cright).%0A%5Cend%7Baligned%7D%0A"> If <img src="https://latex.codecogs.com/png.latex?p%20=%201/2"> this estimator is unbiased. Otherwise, the addition of four fake observations pulls <img src="https://latex.codecogs.com/png.latex?%5Cwidetilde%7Bp%7D"> <em>away</em> from <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D"> and <em>towards</em> <img src="https://latex.codecogs.com/png.latex?1/2">: when <img src="https://latex.codecogs.com/png.latex?p%3E1/2"> the estimator is downward-biased, and when <img src="https://latex.codecogs.com/png.latex?p%20%3C%201/2"> it is upward-biased. The smaller the sample size, the larger the bias.</p>
<p>Let’s try this out on our Trump/Berkeley example. Adding four fake observations gives a sample proportion of <img src="https://latex.codecogs.com/png.latex?%0A%5Cwidetilde%7Bp%7D%20=%20%5Cfrac%7Bn%20%5Cwidehat%7Bp%7D%20+%202%7D%7Bn%20+%204%7D%20=%20%5Cfrac%7B25%20%5Ctimes%200%20+%202%7D%7B25%20+%204%7D%20=%20%5Cfrac%7B2%7D%7B29%7D%20%5Capprox%200.07%0A"> in the augmented dataset and hence a 95% confidence interval of approximately <img src="https://latex.codecogs.com/png.latex?%0A0.07%20%5Cpm%201.96%20%5Ctimes%20%5Csqrt%7B%5Cfrac%7B0.07%20%5Ctimes%20(1%20-%200.07)%7D%7B29%7D%7D%20=%20%5B-0.02,%200.16%5D.%0A"> Of course a proportion can’t be negative, so we would report <img src="https://latex.codecogs.com/png.latex?%5B0,%200.16%5D">. This seems like a much more reasonable summary of our uncertainty than reporting an interval of <img src="https://latex.codecogs.com/png.latex?%5B0,0%5D">, but does it really work? Does adding fake data really improve things?</p>
</section>
<section id="comparing-the-wald-and-agresti-coull-intervals" class="level2" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="comparing-the-wald-and-agresti-coull-intervals"><span class="header-section-number">4</span> Comparing the Wald and Agresti-Coull Intervals</h2>
<p>To answer the question raised at the end of the last paragraph, let’s use R to calculate the <em>coverage probability</em> of the Wald and Agresti-Coull intervals for a range of values of the sample size <img src="https://latex.codecogs.com/png.latex?n"> and true population proportion <img src="https://latex.codecogs.com/png.latex?p">. In other words, let’s see how often these intervals <em>actually</em> contain the true population parameter <img src="https://latex.codecogs.com/png.latex?p">. If they are bona fide 95% confidence intervals, this should occur with probability close to 0.95. One way to carry out this exercise is via Monte Carlo simulation: repeatedly drawing randomly generated datasets and counting the proportion of our resulting confidence intervals that contain the true value of <img src="https://latex.codecogs.com/png.latex?p">. In this example, however, it turns out that there’s a quick and easy way to calculate <em>exact</em> coverage probabilities using the R function <code>dbinom</code>. For full details, see the R code appendix at the end of the post.</p>
<p>To begin, let’s compare the two confidence intervals over a grid of values for the true population proportion <img src="https://latex.codecogs.com/png.latex?p"> while holding the sample size <img src="https://latex.codecogs.com/png.latex?n"> fixed. When <img src="https://latex.codecogs.com/png.latex?n%20=%2025"> we obtain the following:</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/don-t-use-the-textbook-ci-for-a-proportion/index_files/figure-html/unnamed-chunk-2-1.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>In each of these plots, along with those that follow, the solid black curve gives the coverage probability while the dashed red line passes through <img src="https://latex.codecogs.com/png.latex?0.95"> on the vertical axis. A well-behaved confidence interval should produce a black curve that is close to the dashed red line. To make a long story short: the Agresti-Coull interval is quite well-behaved while the Wald interval is a disaster. For values of <img src="https://latex.codecogs.com/png.latex?p"> close to zero or one, the Wald interval is extremely erratic: its coverage probability can be exactly 95% or far below depending on the precise value of <img src="https://latex.codecogs.com/png.latex?p">. Moreover, the Wald interval systematically <em>undercovers</em>. There are very few values of <img src="https://latex.codecogs.com/png.latex?p"> for which its coverage probability is 0.95 or higher and very many for which it is below this level. In stark contrast, the Agresti-Coull interval at worst undercovers by around 0.01 or 0.02. In general its actual coverage probability is very close to 95%, although it does have a tendency to <em>overcover</em> for values of <img src="https://latex.codecogs.com/png.latex?p"> that are close to zero or one. It turns out that there is nothing special about <img src="https://latex.codecogs.com/png.latex?n%20=%2025">. The same basic story holds for larger sample sizes, for example <img src="https://latex.codecogs.com/png.latex?n=50"> and <img src="https://latex.codecogs.com/png.latex?n%20=%20100">.</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/don-t-use-the-textbook-ci-for-a-proportion/index_files/figure-html/unnamed-chunk-3-1.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/don-t-use-the-textbook-ci-for-a-proportion/index_files/figure-html/unnamed-chunk-3-2.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>So what do we make of the rule of thumb that the Wald interval will perform well if <img src="https://latex.codecogs.com/png.latex?np(1-p)%3E5">? Indeed, values of <img src="https://latex.codecogs.com/png.latex?p"> that are close to zero or one present the biggest problems for this confidence interval. But like many traditional statistical rules of thumb, this one leaves much to be desired. Suppose that <img src="https://latex.codecogs.com/png.latex?n%20=%201270"> and <img src="https://latex.codecogs.com/png.latex?p%20=%200.005">. In this case <img src="https://latex.codecogs.com/png.latex?np(1-p)"> equals 6.3 but the coverage probability of the Wald interval is an unsatisfying 0.947 compared to 0.969 for the Agresti-Coull interval.</p>
<p>Because the central limit theorem is an asymptotic result, one that holds as <img src="https://latex.codecogs.com/png.latex?n"> approaches infinity, we might hope that at least the performance of the Wald interval improves as the sample size grows. Alas, this is not always the case. The following plot compares the coverage of Wald and Agresti-Coull confidence intervals for <img src="https://latex.codecogs.com/png.latex?p%20=%200.005"> as <img src="https://latex.codecogs.com/png.latex?n"> increases from 200 to 2000. Note the pronounced “sawtooth” pattern in the Wald confidence interval. It improves steadily as the sample size grows only to jump precipitously downward, before beginning a steady upward climb followed by another jump. In contrast, the performance of the Agresti-Coull interval is fairly steady. While a bit less dramatic, a similar qualitative pattern holds for <img src="https://latex.codecogs.com/png.latex?p=0.2"> as <img src="https://latex.codecogs.com/png.latex?n"> increases from 25 to 100.</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/don-t-use-the-textbook-ci-for-a-proportion/index_files/figure-html/unnamed-chunk-4-1.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/don-t-use-the-textbook-ci-for-a-proportion/index_files/figure-html/unnamed-chunk-4-2.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="conclusion" class="level2" data-number="5">
<h2 data-number="5" class="anchored" data-anchor-id="conclusion"><span class="header-section-number">5</span> Conclusion</h2>
<p>Friends don’t let friends use the Wald interval for a proportion. Fortunately there’s a simple alternative when you’re after a 95% confidence interval: add two successes and two failures to your dataset, then proceed as normal. I encourage you to use my R code to test out different values of <img src="https://latex.codecogs.com/png.latex?p"> and <img src="https://latex.codecogs.com/png.latex?n">, making your own comparisons of the Wald and Agresti-Coull intervals. In a future post, I’ll show you where the Agresti-Coull interval comes from, why it works so well, and how to generalize it to construct 90%, 99% and indeed arbitrary <img src="https://latex.codecogs.com/png.latex?(1%20-%20%5Calpha)%20%5Ctimes%20100%5C%25"> confidence intervals.</p>
</section>
<section id="r-code-appendix" class="level2" data-number="6">
<h2 data-number="6" class="anchored" data-anchor-id="r-code-appendix"><span class="header-section-number">6</span> R Code Appendix</h2>
<p>I wrote four R functions to generate the plots shown above: <code>get_Wald_coverage()</code> and <code>get_AC_coverage()</code> calculate the coverage probabilities of the Wald and Agresti-Coull confidence intervals, while <code>plot_n_comparison()</code> and <code>plot_p_comparison()</code> construct the plots comparing coverage probabilities across different values of the sample size <img src="https://latex.codecogs.com/png.latex?n"> and true population proportion <img src="https://latex.codecogs.com/png.latex?p">.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">get_Wald_coverage <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(p, n) {</span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#-----------------------------------------------------------------------------</span></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculates the exact coverage probability of a nominal 95% Wald confidence </span></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># interval for a population proportion.</span></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#-----------------------------------------------------------------------------</span></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># p   true population proportion</span></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># n   sample size</span></span>
<span id="cb1-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#-----------------------------------------------------------------------------</span></span>
<span id="cb1-9">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n</span>
<span id="cb1-10">  p_hat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n</span>
<span id="cb1-11">  z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb1-12">  SE <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(p_hat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p_hat) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n)</span>
<span id="cb1-13">  cover <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> p_hat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> z <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> SE) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> (p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> p_hat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> z <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> SE)</span>
<span id="cb1-14">  prob_cover <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbinom</span>(x, n, p)</span>
<span id="cb1-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(cover <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> prob_cover)</span>
<span id="cb1-16">}</span>
<span id="cb1-17"></span>
<span id="cb1-18">get_AC_coverage <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(p, n) {</span>
<span id="cb1-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#-----------------------------------------------------------------------------</span></span>
<span id="cb1-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculates the exact coverage probability of a nominal 95% Agresti-Coull</span></span>
<span id="cb1-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># confidence interval for a population proportion.</span></span>
<span id="cb1-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#-----------------------------------------------------------------------------</span></span>
<span id="cb1-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># p   true population proportion</span></span>
<span id="cb1-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># n   sample size</span></span>
<span id="cb1-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#-----------------------------------------------------------------------------</span></span>
<span id="cb1-26">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n</span>
<span id="cb1-27">  p_tilde <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb1-28">  n_tilde <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>            </span>
<span id="cb1-29">  z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb1-30">  SE <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(p_tilde <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p_tilde) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n_tilde)</span>
<span id="cb1-31">  cover <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> p_tilde <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> z <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> SE) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> (p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> p_tilde <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> z <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> SE)</span>
<span id="cb1-32">  prob_cover <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbinom</span>(x, n, p)</span>
<span id="cb1-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(cover <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> prob_cover)</span>
<span id="cb1-34">}</span>
<span id="cb1-35"></span>
<span id="cb1-36">plot_n_comparison <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(n_seq, p) {</span>
<span id="cb1-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#-----------------------------------------------------------------------------</span></span>
<span id="cb1-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plots a comparison of coverage probabilities for Wald and Agresti-Coull </span></span>
<span id="cb1-39"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># nominal 95% confidence intervals for a population proportion over a grid of</span></span>
<span id="cb1-40"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># values for the sample size, holding the population proportion fixed.</span></span>
<span id="cb1-41"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#-----------------------------------------------------------------------------</span></span>
<span id="cb1-42"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># n_seq   vector of values for the sample size</span></span>
<span id="cb1-43"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># p       true population proportion</span></span>
<span id="cb1-44"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#-----------------------------------------------------------------------------</span></span>
<span id="cb1-45"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Example:</span></span>
<span id="cb1-46"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   plot_n_comparison(n_seq = 25:100, p = 0.2)</span></span>
<span id="cb1-47"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#-----------------------------------------------------------------------------</span></span>
<span id="cb1-48">  wald <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(n_seq, \(n) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_Wald_coverage</span>(p, n))</span>
<span id="cb1-49">  AC <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(n_seq, \(n) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_AC_coverage</span>(p, n))</span>
<span id="cb1-50">  cover_min <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(wald), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(AC))</span>
<span id="cb1-51">  cover_max <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(wald), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(AC))</span>
<span id="cb1-52">  limits <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(cover_min, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-53">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb1-54">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(n_seq, wald, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'l'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'n'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> limits, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Wald'</span>, </span>
<span id="cb1-55">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Coverage Prob.'</span>)</span>
<span id="cb1-56">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(n_seq), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bquote</span>(p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> .(p)))</span>
<span id="cb1-57">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>)</span>
<span id="cb1-58">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(n_seq, AC, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'l'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'n'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> limits,</span>
<span id="cb1-59">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Agresti-Coull'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>)</span>
<span id="cb1-60">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(n_seq), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bquote</span>(p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> .(p)))</span>
<span id="cb1-61">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>)</span>
<span id="cb1-62">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb1-63">}</span>
<span id="cb1-64"></span>
<span id="cb1-65">plot_p_comparison <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(p_seq, n) {</span>
<span id="cb1-66"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#-----------------------------------------------------------------------------</span></span>
<span id="cb1-67"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plots a comparison of coverage probabilities for Wald and Agresti-Coull </span></span>
<span id="cb1-68"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># nominal 95% confidence intervals for a population proportion over a grid of</span></span>
<span id="cb1-69"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># values for the population proportion, holding sample size fixed.</span></span>
<span id="cb1-70"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#-----------------------------------------------------------------------------</span></span>
<span id="cb1-71"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># p_seq   vector of values for the true population proportion</span></span>
<span id="cb1-72"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># n       sample size</span></span>
<span id="cb1-73"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#-----------------------------------------------------------------------------</span></span>
<span id="cb1-74"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Example:</span></span>
<span id="cb1-75"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   plot_p_comparison(my_p_seq, n = 25)</span></span>
<span id="cb1-76"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#-----------------------------------------------------------------------------</span></span>
<span id="cb1-77">  wald <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(p_seq, \(p) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_Wald_coverage</span>(p, n))</span>
<span id="cb1-78">  AC <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(p_seq, \(p) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_AC_coverage</span>(p, n))</span>
<span id="cb1-79">  cover_min <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(wald), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(AC))</span>
<span id="cb1-80">  limits <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(cover_min, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-81">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb1-82">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(p_seq, wald, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'l'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'p'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> limits, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Wald'</span>, </span>
<span id="cb1-83">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Coverage Prob.'</span>)</span>
<span id="cb1-84">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bquote</span>(n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> .(n)))</span>
<span id="cb1-85">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>)</span>
<span id="cb1-86">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(p_seq, AC, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'l'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'p'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> limits,</span>
<span id="cb1-87">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Agresti-Coull'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>)</span>
<span id="cb1-88">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bquote</span>(n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> .(n)))</span>
<span id="cb1-89">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>)</span>
<span id="cb1-90">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb1-91">}</span></code></pre></div></div>
</div>


</section>

 ]]></description>
  <category>statistics</category>
  <guid>https://www.econometrics.blog/post/don-t-use-the-textbook-ci-for-a-proportion/</guid>
  <pubDate>Mon, 04 Oct 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Regressions with a Mis-measured, Binary Outcome</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/regressions-with-a-mis-measured-binary-outcome/</link>
  <description><![CDATA[ 




<p>Many outcomes of interest in economics are binary. For example, we may want to learn how employment status <img src="https://latex.codecogs.com/png.latex?Y%5E*"> varies with demographics <img src="https://latex.codecogs.com/png.latex?X">, where <img src="https://latex.codecogs.com/png.latex?Y%5E*=1"> means “employed” and <img src="https://latex.codecogs.com/png.latex?Y%5E*=0"> means unemployed or not in the labor force. But how do we know if someone is employed? Typically we ask them, perhaps as part of a large, nationally representative survey such as the <a href="https://www.census.gov/programs-surveys/cps.html">CPS</a>. Researchers who study labor market dynamics have long known, however, that observed data on labor market status are often inaccurate <a href="https://ideas.repec.org/a/ecm/emetrp/v54y1986i6p1319-38.html">(Poterba &amp; Summers, 1986)</a>. Administrative errors creep into even the most carefully-administered surveys. But more importantly, survey respondents do not always tell the truth, whether by mistake or deliberately, and this problem seems to have gotten worse in recent years <a href="https://ideas.repec.org/a/aea/jecper/v29y2015i4p199-226.html">(Meyer et al.&nbsp;2015)</a>. Instead of true employment status <img src="https://latex.codecogs.com/png.latex?Y%5E*"> researchers only observe a noisy measure <img src="https://latex.codecogs.com/png.latex?Y%5Cin%20%5C%7B0,%201%5C%7D">.</p>
<p>In my <a href="https://www.econometrics.blog/post/beyond-classical-measurement-error/">previous post</a> I showed that classical measurement error in an outcome variable is basically innocuous. But I also showed that measurement error in a binary random variable <em>cannot</em> be classical. In this post, I’ll explore the consequences of this fact when we want to learn <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y%5E*=1%7CX)"> but only observe <img src="https://latex.codecogs.com/png.latex?Y"> and <img src="https://latex.codecogs.com/png.latex?X">, not the true outcome variable <img src="https://latex.codecogs.com/png.latex?Y%5E*">. To keep things concrete, I will assume throughout that <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y%5E*=1%7CX)%20=%20F(X'%5Cbeta)"> where <img src="https://latex.codecogs.com/png.latex?F"> is a strictly increasing, differentiable function. This covers all the usual suspects: <a href="https://expl.ai/ZULFNLF">logit, probit</a>, and the <a href="https://expl.ai/XSLDYZE">linear probability model</a>.<sup>1</sup> The parameter <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> may have a causal interpretation or may simply have a predictive one. Either way, the question I’ll focus on here is whether, and if so how, <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> can be identified in the presence of measurement error. For simplicity I will assume throughout that that the covariates <img src="https://latex.codecogs.com/png.latex?X"> are measured without error.</p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;For more details of these models, see my <a href="https://www.economictricks.com/limdep-notes.pdf">lecture notes</a>.</p></div></div><section id="whats-the-problem" class="level2 page-columns page-full" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="whats-the-problem"><span class="header-section-number">1</span> What’s the problem?</h2>
<p>Why does observing <img src="https://latex.codecogs.com/png.latex?Y"> rather than <img src="https://latex.codecogs.com/png.latex?Y%5E*"> present a problem? To answer this question, we need to derive the relationship between <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y=1%7CX)"> and <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y%5E*=1%7CX)">. Since <img src="https://latex.codecogs.com/png.latex?Y"> and <img src="https://latex.codecogs.com/png.latex?Y%5E*"> are both binary, <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y%7CX)%20=%20%5Cmathbb%7BP%7D(Y=1%7CX)"> and similarly <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BE%7D(Y%5E*%7CX)%20=%20%5Cmathbb%7BP%7D(Y%5E*=1%7CX)%20=%20F(X'%5Cbeta).%0A"> Now define the measurement error <img src="https://latex.codecogs.com/png.latex?W"> as <img src="https://latex.codecogs.com/png.latex?W%20=%20Y%20-%20Y%5E*"> so we can write <img src="https://latex.codecogs.com/png.latex?Y%20=%20Y%5E*%20+%20W">. By the linearity of expectation, <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cmathbb%7BP%7D(Y=1%7CX)%20&amp;=%20%5Cmathbb%7BE%7D(Y%7CX)%5C%5C%0A&amp;=%20%5Cmathbb%7BE%7D(Y%5E*%20+%20W%7CX)%20=%20%5Cmathbb%7BE%7D(Y%5E*%7CX)%20+%20%5Cmathbb%7BE%7D(W%7CX)%5C%5C%0A&amp;=%20%5Cmathbb%7BP%7D(Y%5E*=1%7CX)%20+%20%5Cmathbb%7BE%7D(W%7CX)%0A%5Cend%7Baligned%7D%0A"> so we see that if <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(W%7CX)=0"> then <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y=1%7CX)"> and <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y%5E*=1%7CX)"> will coincide. Unfortunately <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(W%7CX)"> in general <em>cannot</em> be zero. This means that learning <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y=1%7CX)"> will not tell us what we want to know: <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y%5E*=1%7CX)">.</p>
<p>To see why <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(W%7CX)%20%5Cneq%200">, first define the <em>mis-classification probabilities</em> <img src="https://latex.codecogs.com/png.latex?%5Calpha_0(%5Ccdot)"> and <img src="https://latex.codecogs.com/png.latex?%5Calpha_1(%5Ccdot)"> <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Calpha_0(X)%20&amp;%5Cequiv%20P(Y=1%7CY%5E*=0,X)%5C%5C%0A%5Calpha_1(X)%20&amp;%5Cequiv%20P(Y=0%7CY%5E*=1,X).%0A%5Cend%7Baligned%7D%0A"> The subscripts on <img src="https://latex.codecogs.com/png.latex?%5Calpha"> refer to the value of <img src="https://latex.codecogs.com/png.latex?Y%5E*"> on which we condition: <img src="https://latex.codecogs.com/png.latex?%5Calpha_0(%5Ccdot)"> conditions on <img src="https://latex.codecogs.com/png.latex?Y%5E*=0"> while <img src="https://latex.codecogs.com/png.latex?%5Calpha_1(%5Ccdot)"> conditions on <img src="https://latex.codecogs.com/png.latex?Y%5E*=1">. You can interpret the mis-classification probabilities by analogy to null hypothesis testing: <img src="https://latex.codecogs.com/png.latex?%5Calpha_0(X)"> is effectively the type I error rate as a function of <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?%5Calpha_1(X)"> is the type II error rate as a function of <img src="https://latex.codecogs.com/png.latex?X">.</p>
<p>To keep things fully general for the moment, we allow the mis-classification probabilities to depend on <img src="https://latex.codecogs.com/png.latex?X">. Perhaps a young male worker with five years of experience is more likely to make an erroneous self-report in the CPS than and older female worker with more experience, for example.<sup>2</sup> Since <img src="https://latex.codecogs.com/png.latex?Y"> and <img src="https://latex.codecogs.com/png.latex?Y%5E*"> are both binary, <img src="https://latex.codecogs.com/png.latex?W%20%5Cin%20%5C%7B-1,%200,%201%5C%7D"> and we calculate <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(W%7CX)"> as follows: <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cmathbb%7BE%7D(W%7CX)%20&amp;=%20-1%20%5Ctimes%20%5Cmathbb%7BP%7D(W=-1%7CX)%20+%200%20%5Ctimes%20%5Cmathbb%7BP%7D(W=0%7CX)%20+%201%20%5Ctimes%20%5Cmathbb%7BP%7D(W=1%7CX)%20%5C%5C%0A&amp;=%20%5Cmathbb%7BP%7D(W=1%7CX)%20-%20%5Cmathbb%7BP%7D(W=-1%7CX).%0A%5Cend%7Baligned%7D%0A"> Now consider the event <img src="https://latex.codecogs.com/png.latex?%5C%7BW%20=%20-1%5C%7D">. The only way that this could occur is if <img src="https://latex.codecogs.com/png.latex?Y%20=%200"> and <img src="https://latex.codecogs.com/png.latex?Y%5E*%20=%201">. Accordingly, <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cmathbb%7BP%7D(W%20=%20-1%7CX)%20&amp;=%20%5Cmathbb%7BP%7D(Y%20=%200,%20Y%5E*%20=%201%7CX)%5C%5C%0A&amp;=%20%5Cmathbb%7BP%7D(Y=0%7CY%5E*=1,X)%5Cmathbb%7BP%7D(Y%5E*=1%7CX)%20%5C%5C%0A&amp;=%20%5Calpha_1(X)%20F(X'%5Cbeta).%0A%5Cend%7Baligned%7D%0A"> Similarly, the only way that <img src="https://latex.codecogs.com/png.latex?%5C%7BW=1%5C%7D"> can occur is if <img src="https://latex.codecogs.com/png.latex?Y=1"> and <img src="https://latex.codecogs.com/png.latex?Y%5E*=0"> so that <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cmathbb%7BP%7D(W%20=%201%7CX)%20&amp;=%20%5Cmathbb%7BP%7D(Y%20=%201,%20Y%5E*%20=%200%7CX)%5C%5C%0A&amp;=%20%5Cmathbb%7BP%7D(Y=1%7CY%5E*=0,X)%20%5Cmathbb%7BP%7D(Y%5E*=0%7CX)%5C%5C%0A&amp;=%20%5Calpha_0(X)%20%5Cleft%5B1%20-%20F(X'%5Cbeta)%5Cright%5D.%0A%5Cend%7Baligned%7D%0A"> <!--The event $\{W =0\}$ can occur in two mutually exclusive ways: $\{Y=0,Y^*=0\}$ and $\{Y=1,Y^*=1\}$.
Thus
$$
\begin{aligned}
\mathbb{P}(W = 0|X) &= \mathbb{P}(Y = 0, Y^* = 0|X) + \mathbb{P}(Y=1, Y^*=1|X)\\
&= \mathbb{P}(Y=0|Y^*=0,X)\mathbb{P}(Y^*=0|X) + \mathbb{P}(Y=1|Y^*=1,X)\mathbb{P}(Y^*=1|X)\\
&=\left[ 1 - \alpha_0(X)\right] \left[1 - F(X'\beta) \right] + [1 - \alpha_1(X)] F(X'\beta)
\end{aligned}
$$--> Therefore, <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cmathbb%7BE%7D(W%7CX)%20&amp;=%20%5Cmathbb%7BP%7D(W=1%7CX)%20-%20%5Cmathbb%7BP%7D(W=-1%7CX)%20%5C%5C%0A&amp;=%20%20%5Calpha_0(X)%5Cleft%5B1%20-%20F(X'%5Cbeta)%20%5Cright%5D%20-%5Calpha_1(X)%20F(X'%5Cbeta).%0A%5Cend%7Baligned%7D%0A"> So how could <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(W%7CX)%20=%200">? Re-arranging the preceding to solve for <img src="https://latex.codecogs.com/png.latex?F(X'%5Cbeta)">, <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BE%7D(W%7CX)%20=%200%20%5Ciff%20F(X'%5Cbeta)%20=%20%5Cfrac%7B%5Calpha_0(X)%7D%7B%5Calpha_0(X)%20+%20%5Calpha_1(X)%7D.%0A"> This shows that that <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(W%7CX)"> can only be zero in an <em>extremely peculiar</em> case where <img src="https://latex.codecogs.com/png.latex?%5Calpha_0(%5Ccdot)"> and <img src="https://latex.codecogs.com/png.latex?%5Calpha_1(%5Ccdot)"> depend on <img src="https://latex.codecogs.com/png.latex?X"> in just the right way. If the mis-classification probabilities are constants that do not depend on <img src="https://latex.codecogs.com/png.latex?X">, we would require <img src="https://latex.codecogs.com/png.latex?F(X'%5Cbeta)%20=%20%5Calpha_0/(%5Calpha_0%20+%20%5Calpha_1)">. This is only possible if all the elements of <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> besides the intercept are zero. Since <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(W%7CX)"> will not in general equal zero, <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y=1%7CX)"> will not in general equal <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y%5E*=1%7CX)">.</p>
<div class="no-row-height column-margin column-container"><div id="fn2"><p><sup>2</sup>&nbsp;Below we’ll examine a simpler special case in which <img src="https://latex.codecogs.com/png.latex?%5Calpha_0"> and <img src="https://latex.codecogs.com/png.latex?%5Calpha_1"> are <em>fixed</em> probabilities that do not depend on <img src="https://latex.codecogs.com/png.latex?X">.</p></div></div></section>
<section id="what-happens-if-we-ignore-the-problem" class="level2 page-columns page-full" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="what-happens-if-we-ignore-the-problem"><span class="header-section-number">2</span> What happens if we ignore the problem?</h2>
<p>Substituting our expression for <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(W%7CX)"> and factoring the result, <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cmathbb%7BP%7D(Y=1%7CX)%20&amp;=%20%20%5Cmathbb%7BP%7D(Y%5E*=1%7CX)%20+%20E(W%7CX)%20%5C%5C%0A&amp;=%20%20F(X'%5Cbeta)%20+%20%5Calpha_0(X)%5Cleft%5B1%20-%20F(X'%5Cbeta)%20%5Cright%5D%20-%5Calpha_1(X)%20F(X'%5Cbeta)%5C%5C%0A&amp;=%20%5Calpha_0(X)%20+%20F(X'%5Cbeta)%20%5B1%20-%20%5Calpha_0(X)%20-%20%5Calpha_1(X)%5D.%0A%5Cend%7Baligned%7D%0A"></p>
<p>Because we observe <img src="https://latex.codecogs.com/png.latex?(Y,%20X)">, <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y=1%7CX)"> is <em>identified</em>. With enough data, we can learn this conditional probability as a function of <img src="https://latex.codecogs.com/png.latex?X"> as accurately as we wish. The problem is that <img src="https://latex.codecogs.com/png.latex?%5Calpha_0(X)"> and <img src="https://latex.codecogs.com/png.latex?%5Calpha_1(X)"> drive a wedge between what we can observe, <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y=1%7CX)">, and what we’re trying to learn <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y%5E*=1%7CX)%20=%20F(X'%5Cbeta)">. Without knowing more about the functions <img src="https://latex.codecogs.com/png.latex?%5Calpha_0(%5Ccdot)"> and <img src="https://latex.codecogs.com/png.latex?%5Calpha_1(%5Ccdot)"> we can’t say much about how <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y=1%7CX)"> and <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y%5E*=1%7CX)"> will differ. Because they are <em>probabilities</em>, <img src="https://latex.codecogs.com/png.latex?%0A0%5Cleq%20%5Calpha_0(X)%20%5Cleq%201,%20%5Cquad%200%5Cleq%20%5Calpha_1(X)%20%5Cleq%201.%0A"> But because they are conditional probabilities that condition on <em>different events</em>, <img src="https://latex.codecogs.com/png.latex?%5C%7BY%5E*=0,%20X=x%5C%7D"> versus <img src="https://latex.codecogs.com/png.latex?%5C%7BY%5E*=1,%20X=x%5C%7D">, the sum <img src="https://latex.codecogs.com/png.latex?%5Calpha_0(x)%20+%20%5Calpha_1(x)"> could be greater than one. This means that <img src="https://latex.codecogs.com/png.latex?1%20-%20%5Calpha_0(X)%20-%20%5Calpha_1(X)"> could be <em>negative</em>, at least for certain values of <img src="https://latex.codecogs.com/png.latex?X">. It’s common in practice, however, to assume that <img src="https://latex.codecogs.com/png.latex?%5Calpha_0(X)%20+%20%5Calpha_1(X)%20%3C%201"> for all possible values that the covariates <img src="https://latex.codecogs.com/png.latex?X"> could take on. To understand this assumption, and the problem more generally, it’s helpful to consider a simple special case in which the mis-classification probabilities do not depend on <img src="https://latex.codecogs.com/png.latex?X">. In this case we can say precisely how measurement error in the outcome affects what we can learn about the relationship between <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y%5E*"> and make clear why <img src="https://latex.codecogs.com/png.latex?%5Calpha_0(X)%20+%20%5Calpha_1(X)%20%3C%201"> is usually a reasonable assumption.</p>
<section id="a-special-case-fixed-mis-classification" class="level3">
<h3 class="anchored" data-anchor-id="a-special-case-fixed-mis-classification">A Special Case: Fixed Mis-classification</h3>
<p>Suppose that the mis-classification probabilities are <em>fixed</em>, i.e.&nbsp;that <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Calpha_0(X)%20&amp;%5Cequiv%20%5Cmathbb%7BP%7D(Y=1%7CY%5E*=0,%20X)%20=%20%5Cmathbb%7BP%7D(Y=1%7CY%5E*=0)%5Cequiv%20%5Calpha_0%20%5C%5C%0A%5Calpha_1(X)%20&amp;%5Cequiv%20%5Cmathbb%7BP%7D(Y=0%7CY%5E*=1,%20X)%20=%20%5Cmathbb%7BP%7D(Y=0%7CY%5E*=1)%5Cequiv%20%5Calpha_1.%0A%5Cend%7Baligned%7D%0A"> This is a fairly strong assumption. It says that both self-reporting and administrative errors occur at the same rate for <em>everyone</em>, regardless of their observed characteristics. In this case, our expression for <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y=1%7CX)"> from above becomes <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cmathbb%7BP%7D(Y=1%7CX)%20&amp;=%20%5Calpha_0%20+%20F(X'%5Cbeta)%20(1%20-%20%5Calpha_0%20-%20%5Calpha_1).%0A%5Cend%7Baligned%7D%0A"> Defining <img src="https://latex.codecogs.com/png.latex?f"> as the derivative of <img src="https://latex.codecogs.com/png.latex?F">, this means that the <em>observed</em> partial effect of a continuous covariate <img src="https://latex.codecogs.com/png.latex?X_j"> with respect to <img src="https://latex.codecogs.com/png.latex?Y"> is <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cfrac%7B%5Cpartial%7D%7B%5Cpartial%20X_j%7D%20%5Cmathbb%7BP%7D(Y=1%7CX)%20&amp;=%20%5Cfrac%7B%5Cpartial%7D%7B%5Cpartial%20X_j%7D%20%5Cleft%5B%20%5Calpha_0%20+%20F(X'%5Cbeta)(1%20-%20%5Calpha_0%20-%20%5Calpha_1)%5Cright%5D%5C%5C%0A&amp;=%20f(X'%5Cbeta)%5Cbeta_j%20(1%20-%20%5Calpha_0%20-%20%5Calpha_1)%0A%5Cend%7Baligned%7D%0A"> whereas the <em>true</em> partial effect, with respect to <img src="https://latex.codecogs.com/png.latex?Y%5E*">, is <img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7B%5Cpartial%7D%7B%5Cpartial%20X_j%7D%20%5Cmathbb%7BP%7D(Y%5E*=1%7CX)%20=%20%5Cfrac%7B%5Cpartial%7D%7B%5Cpartial%20X_j%7D%20F(X'%5Cbeta)%20=%20f(X'%5Cbeta)%20%5Cbeta_j.%0A"> If <img src="https://latex.codecogs.com/png.latex?(%5Calpha_0%20+%20%5Calpha_1)%20%3E%201"> then <img src="https://latex.codecogs.com/png.latex?(1%20-%20%5Calpha_0%20-%20%5Calpha_1)"> will be <em>negative</em>. This means that the measurement error problem is so severe that all of the observed partial effects have the wrong sign. A bit of tedious algebra shows that <img src="https://latex.codecogs.com/png.latex?Y"> and <img src="https://latex.codecogs.com/png.latex?Y%5E*"> must be negatively correlated in this case: <img src="https://latex.codecogs.com/png.latex?Y"> is such a noisy measure of <img src="https://latex.codecogs.com/png.latex?Y%5E*"> that when <img src="https://latex.codecogs.com/png.latex?Y=1"> we’re better off predicting that <img src="https://latex.codecogs.com/png.latex?Y%5E*=0">. For this reason, it’s traditional to assume that <img src="https://latex.codecogs.com/png.latex?%5Calpha_0%20+%20%5Calpha_1%20%3C%201">. In this case <img src="https://latex.codecogs.com/png.latex?0%20%3C%20(1%20-%20%5Calpha_0%20-%20%5Calpha_1)%20%5Cleq%201"> so the observed partial effects are <em>attenuated</em> versions of the true partial effects, in that <img src="https://latex.codecogs.com/png.latex?%0A0%20%3C%20%5Cfrac%7B%5Cpartial%7D%7B%5Cpartial%20X_j%7D%20%5Cmathbb%7BP%7D(Y=1%7CX)%20%5Cleq%20%5Cfrac%7B%5Cpartial%7D%7B%5Cpartial%20X_j%7D%20%5Cmathbb%7BP%7D(Y%5E*=1%7CX).%0A"> So in this special case, <em>non-classical</em> measurement error in a binary outcome variable has the same effect as <em>classical</em> measurement error in a continuous <em>regressor</em>: attenuation bias.</p>
</section>
<section id="observational-equivalence-and-alpha_0-alpha_1" class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="observational-equivalence-and-alpha_0-alpha_1">Observational Equivalence and <img src="https://latex.codecogs.com/png.latex?(%5Calpha_0%20+%20%5Calpha_1)"></h3>
<p>You may be wondering: do we really <em>need</em> the assumption <img src="https://latex.codecogs.com/png.latex?%5Calpha_0%20+%20%5Calpha_1%3C1"> or is it merely convenient? Couldn’t the observed <em>data</em> tell us whether <img src="https://latex.codecogs.com/png.latex?%5Calpha_0%20+%20%5Calpha_1"> is less than one or greater than one? The answer turns out to be no, and it’s easy to show why if <img src="https://latex.codecogs.com/png.latex?F(t)%20=%201%20-%20F(-t)"> as in the probit, logit, and the linear probability models. Suppose that this condition on <img src="https://latex.codecogs.com/png.latex?F"> holds. Then we can write <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%20%20%5Cmathbb%7BP%7D(Y=1%7CX)%20&amp;=%20%5Calpha_0%20+%20(1%20-%20%5Calpha_0%20-%20%5Calpha_1)%20F(X'%5Cbeta)%5C%5C%0A%20%20&amp;=%20%5Calpha_0%20+%20(1%20-%20%5Calpha_0%20-%20%5Calpha_1)%20%5Cleft%5B%201%20-%20F%5Cbig(X'(-%5Cbeta)%5Cbig)%5Cright%5D%5C%5C%0A%20%20&amp;=%20%5Cleft(%5Calpha_0%20+%201%20-%20%5Calpha_0%20-%20%5Calpha_1%5Cright)%20-%20(1%20-%20%5Calpha_0%20-%20%5Calpha_1)%20F%5Cbig(X'(-%5Cbeta)%5Cbig)%5C%5C%0A%20%20&amp;=%20(1%20-%20%5Calpha_1)%20+%20%5Cleft%5B%20%5Calpha_0%20-%20(1%20-%20%5Calpha_1)%20%5Cright%5D%20F%5Cbig(X'%20(-%5Cbeta)%5Cbig)%5C%5C%0A%20%20&amp;=%20(1%20-%20%5Calpha_1)%20+%20%5Cleft%5B1%20-%20(1%20-%20%5Calpha_1)%20-%20(1%20-%20%5Calpha_0)%20%5Cright%5D%20F%5Cbig(X'%20(-%5Cbeta)%5Cbig).%0A%5Cend%7Baligned%7D%0A"> Defining <img src="https://latex.codecogs.com/png.latex?%5Cwidetilde%7B%5Calpha%7D_0%20%5Cequiv%20(1%20-%20%5Calpha_1)">, <img src="https://latex.codecogs.com/png.latex?%5Cwidetilde%7B%5Calpha%7D_1%20%5Cequiv%20(1%20-%20%5Calpha_0)">, and <img src="https://latex.codecogs.com/png.latex?%5Cwidetilde%7B%5Cbeta%7D%20%5Cequiv%20-%5Cbeta">, we have established that <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%20%20%5Cmathbb%7BP%7D(Y=1%7CX)%20&amp;=%20%5Calpha_0%20+%20(1%20-%20%5Calpha_0%20-%20%5Calpha_1)%20F(X'%5Cbeta)%5C%5C%0A%20%20&amp;=%20%5Cwidetilde%7B%5Calpha%7D_0%20+%20%5Cleft(%201%20-%20%5Cwidetilde%7B%5Calpha%7D_0%20-%20%5Cwidetilde%7B%5Calpha%7D_1%20%5Cright)%20F%5Cbig(X'%5Cwidetilde%7B%5Cbeta%7D%5Cbig).%0A%5Cend%7Baligned%7D%0A"> Since <img src="https://latex.codecogs.com/png.latex?Y"> is binary, <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y=1%7CX)"> tells us everything that there is to know about the distribution of <img src="https://latex.codecogs.com/png.latex?Y"> given <img src="https://latex.codecogs.com/png.latex?X">. The preceding pair of equalities shows that the observed conditional distribution <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y=1%7CX)"> could just as well have arisen from <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y%5E*=1%7CX)%20=%20F(X'%5Cwidetilde%7B%5Cbeta%7D)"> with mis-classification probabilities <img src="https://latex.codecogs.com/png.latex?(%5Cwidetilde%7B%5Calpha%7D_0,%20%5Cwidetilde%7B%5Calpha%7D_1)"> as it could from <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y%5E*=1%7CX)%20=%20F(X'%5Cbeta)"> with mis-classification probabilities <img src="https://latex.codecogs.com/png.latex?(%5Calpha_0,%20%5Calpha_1)">. From observations of <img src="https://latex.codecogs.com/png.latex?(Y,%20X)"> alone there is no way to tell these possibilities apart: we say that they are <em>observationally equivalent</em>. Notice that if <img src="https://latex.codecogs.com/png.latex?%5Calpha_0%20+%20%5Calpha_1%20%3C%201"> then <img src="https://latex.codecogs.com/png.latex?%5Cwidetilde%7B%5Calpha%7D_0%20+%20%5Cwidetilde%7B%5Calpha%7D_1%20%3E%201"> and vice-versa. This shows that the only way to point identify <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> is to assume either that <img src="https://latex.codecogs.com/png.latex?%5Calpha_0%20+%20%5Calpha_1%20%3C%201"> or the reverse inequality.<sup>3</sup> For the reasons discussed above, it usually makes sense to choose <img src="https://latex.codecogs.com/png.latex?%5Calpha_0%20+%20%5Calpha_1%20%3C%201">.</p>
<div class="no-row-height column-margin column-container"><div id="fn3"><p><sup>3</sup>&nbsp;Alternatively, you could say that the only way to identify <img src="https://latex.codecogs.com/png.latex?(%5Calpha_0,%20%5Calpha_1)"> is by making an assumption about the sign of one component of <img src="https://latex.codecogs.com/png.latex?%5Cbeta">.</p></div></div></section>
</section>
<section id="some-solutions" class="level2 page-columns page-full" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="some-solutions"><span class="header-section-number">3</span> Some Solutions</h2>
<p>So what is an applied researcher to do? If we could somehow learn the mis-classification probabilities, we could use them to “adjust” <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y=1%7CX)"> and identify <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y%5E*=1%7CX)%20=%20F(X'%5Cbeta)"> as follows: <img src="https://latex.codecogs.com/png.latex?%0AF(X'%5Cbeta)%20=%20%5Cfrac%7B%5Cmathbb%7BP%7D(Y=1%7CX)%20-%20%5Calpha_0(X)%7D%7B1%20-%20%5Calpha_0(X)%20-%20%5Calpha_1(X)%7D.%0A"> Broadly speaking there are two ways learn the mis-classification probabilities. The first approach estimates <img src="https://latex.codecogs.com/png.latex?%5Calpha_0(X)"> and <img src="https://latex.codecogs.com/png.latex?%5Calpha_1(X)"> using a <em>second</em> dataset. The second approach uses a single dataset and exploits non-linearity in the function <img src="https://latex.codecogs.com/png.latex?F"> instead. For the remainder of this discussion I will assume that <img src="https://latex.codecogs.com/png.latex?%5Calpha_0(X)%20+%20%5Calpha_1(X)%20%3C%201"> or <img src="https://latex.codecogs.com/png.latex?%5Calpha_0%20+%20%5Calpha_1%20%3C%201"> if the mis-classification probabilities are fixed.</p>
<section id="method-1-auxiliary-data" class="level3">
<h3 class="anchored" data-anchor-id="method-1-auxiliary-data">Method 1: Auxiliary Data</h3>
<p>Let’s start by making life simple: assume <em>fixed</em> mis-classification. Now suppose that we observe two random samples from the <em>same</em> population. In the first, we observe pairs <img src="https://latex.codecogs.com/png.latex?(Y_i,X_i)"> for <img src="https://latex.codecogs.com/png.latex?i%20=%201,%20...,%20n"> and in the second we observe pairs <img src="https://latex.codecogs.com/png.latex?(Y_j,%20Y%5E*_j)"> for <img src="https://latex.codecogs.com/png.latex?j%20=%201,%20...,%20m">. Notice that neither dataset contains observations of <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y%5E*"> for the same individual. Using the <img src="https://latex.codecogs.com/png.latex?(Y_i,X_i)"> observations we can estimate <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y=1%7CX)">, and using the <img src="https://latex.codecogs.com/png.latex?(Y_j,%20Y%5E*_j)"> observations we can estimate <img src="https://latex.codecogs.com/png.latex?%0A%5Calpha_0%20=%20%5Cmathbb%7BP%7D(Y=1%7CY%5E*=0),%20%5Cquad%20%5Calpha_1%20=%20%5Cmathbb%7BP%7D(Y=0%7CY%5E*=1).%0A"> This gives us everything we need to determine <img src="https://latex.codecogs.com/png.latex?F(X'%5Cbeta)"> as a function of <img src="https://latex.codecogs.com/png.latex?X"> and hence <img src="https://latex.codecogs.com/png.latex?%5Cbeta">. The observations of <img src="https://latex.codecogs.com/png.latex?(Y_i,%20Y%5E*_i)"> are called an <em>auxiliary dataset</em>. In theory, auxiliary data provide a simple and general solution to measurement error problems of all stripes. Suppose, for example, that we were uncomfortable with the assumption of fixed mis-classification. If we observed an auxiliary dataset of <em>triples</em> <img src="https://latex.codecogs.com/png.latex?(Y_j,%20Y_j%5E*,%20X_j)"> then we could directly estimate <img src="https://latex.codecogs.com/png.latex?%5Calpha_0(X)"> and <img src="https://latex.codecogs.com/png.latex?%5Calpha_1(X)">. Of course, we if we observed <img src="https://latex.codecogs.com/png.latex?(Y_j,%20Y_j%5E*,%20X_j)"> for a random sample drawn from the population of interest we could estimate <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y%5E*=1%7CX)"> <em>directly</em> without the need to account for measurement error! And here lies the fundamental tension of the auxiliary data approach: if we had sufficiently rich auxiliary data we wouldn’t really have a measurement error problem in the first place. More typically, we either observe <img src="https://latex.codecogs.com/png.latex?(Y_j,%20Y_j%5E*,%20X_j)"> for a <em>different</em> population, or only observe a subset of these variables for our population of interest. Either way we need to rely on modeling assumptions to bridge the gap. For example, fixed mis-classification and an auxiliary dataset of <img src="https://latex.codecogs.com/png.latex?(Y%5E*_j,%20Y_j)"> suffice to solve the measurement error problem but only if <img src="https://latex.codecogs.com/png.latex?%5Calpha_0(X)"> and <img src="https://latex.codecogs.com/png.latex?%5Calpha_1(X)"> do not in fact depend on <img src="https://latex.codecogs.com/png.latex?X">.</p>
</section>
<section id="method-2-nonlinearity-of-f" class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="method-2-nonlinearity-of-f">Method 2: Nonlinearity of <img src="https://latex.codecogs.com/png.latex?F"></h3>
<p>The auxiliary data approach is very general in principle but relies on information that we simply may not have in practice: a second dataset from the same population. An alternative approach uses only one dataset, <img src="https://latex.codecogs.com/png.latex?(Y_i,%20X_i)"> for <img src="https://latex.codecogs.com/png.latex?i%20=%201,%20...,%20n">, and instead exploits the <em>shape</em> of the function <img src="https://latex.codecogs.com/png.latex?F">. This second approach is a bit less general but doesn’t require any outside sources of information.</p>
<p>To begin, suppose that the mis-classification probabilities are <em>fixed</em> and that <img src="https://latex.codecogs.com/png.latex?F"> is a <em>known function</em>, e.g.&nbsp;the standard logistic CDF. Suppose further that <img src="https://latex.codecogs.com/png.latex?F"> is strictly increasing and hence invertible. Then, applying <img src="https://latex.codecogs.com/png.latex?F%5E%7B-1%7D"> to both sides of our expression for <img src="https://latex.codecogs.com/png.latex?F(X'%5Cbeta)"> from above, <img src="https://latex.codecogs.com/png.latex?%0AX'%5Cbeta%20=%20F%5E%7B-1%7D%20%5Cleft%5B%5Cfrac%7B%5Cmathbb%7BP%7D(Y%20=%201%7CX)%20-%20%5Calpha_0%7D%7B1%20-%20%5Calpha_0%20-%20%5Calpha_1%7D%5Cright%5D%0A"> and thus, pre-multiplying both sides by <img src="https://latex.codecogs.com/png.latex?X"> and taking expectations, <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BE%7D%5BXX'%5D%5Cbeta%20=%20%5Cmathbb%7BE%7D%5Cleft%5C%7BX%20F%5E%7B-1%7D%20%5Cleft%5B%5Cfrac%7B%5Cmathbb%7BP%7D(Y%20=%201%7CX)%20-%20%5Calpha_0%7D%7B1%20-%20%5Calpha_0%20-%20%5Calpha_1%7D%5Cright%5D%5Cright%5C%7D.%0A"> Therefore, if <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5BXX'%5D"> is invertible, <img src="https://latex.codecogs.com/png.latex?%0A%5Cbeta%20=%20%5Cmathbb%7BE%7D%5BXX'%5D%5E%7B-1%7D%5Cmathbb%7BE%7D%5Cleft%5C%7BX%20F%5E%7B-1%7D%20%5Cleft%5B%5Cfrac%7B%5Cmathbb%7BP%7D(Y%20=%201%7CX)%20-%20%5Calpha_0%7D%7B1%20-%20%5Calpha_0%20-%20%5Calpha_1%7D%5Cright%5D%5Cright%5C%7D.%0A"> Since <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y=1%7CX)"> depends only on the observed data <img src="https://latex.codecogs.com/png.latex?(Y,X)">, this function is point identified. Since <img src="https://latex.codecogs.com/png.latex?F"> is assumed to be a known function, it follows that <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> is point identified whenever <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5BXX'%5D"> is invertible and <img src="https://latex.codecogs.com/png.latex?(%5Calpha_0,%20%5Calpha_1)"> are known.<sup>4</sup> So if we can find a way to point identify <img src="https://latex.codecogs.com/png.latex?%5Calpha_0"> and <img src="https://latex.codecogs.com/png.latex?%5Calpha_1">, we will immediately identify <img src="https://latex.codecogs.com/png.latex?%5Cbeta">.</p>
<div class="no-row-height column-margin column-container"><div id="fn4"><p><sup>4</sup>&nbsp;We also need <img src="https://latex.codecogs.com/png.latex?%5Calpha_0%20+%20%5Calpha_1%20%5Cneq%201"> to avoid division by zero!</p></div></div><p>Easier said than done! How can we possibly learn <img src="https://latex.codecogs.com/png.latex?%5Calpha_0"> and <img src="https://latex.codecogs.com/png.latex?%5Calpha_1"> without auxiliary data? Nonlinearity is the key. If <img src="https://latex.codecogs.com/png.latex?F"> is a cumulative distribution function, then <img src="https://latex.codecogs.com/png.latex?%5Clim_%7Bt%5Crightarrow%20%5Cinfty%7D%20F(t)%20=%201"> and <img src="https://latex.codecogs.com/png.latex?%5Clim_%7Bt%5Crightarrow%20-%5Cinfty%7D%20F(t)%20=%200">. Now suppose that <img src="https://latex.codecogs.com/png.latex?X"> contains at least one covariate, call it <img src="https://latex.codecogs.com/png.latex?V">, that is continuous and has “large support,” i.e.&nbsp;takes on values in a very wide range. Without loss of generality, suppose that the coefficient <img src="https://latex.codecogs.com/png.latex?%5Cbeta_v"> on <img src="https://latex.codecogs.com/png.latex?V"> is positive. (If it’s negative, then apply the following argument to <img src="https://latex.codecogs.com/png.latex?-V"> instead.) For <img src="https://latex.codecogs.com/png.latex?V"> large and <em>positive</em> <img src="https://latex.codecogs.com/png.latex?X'%5Cbeta"> is large and positive so that <img src="https://latex.codecogs.com/png.latex?F(X'%5Cbeta)"> is close to one. In this case <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Calpha_0%20+%20(1%20-%20%5Calpha_0%20-%20%5Calpha_1)%20F(X'%5Cbeta)%20&amp;%5Capprox%20%5Calpha_0%20+%20(1%20-%20%5Calpha_0%20-%20%5Calpha_1)%20%5Ctimes%201%20%5C%5C%0A&amp;=%20(1%20-%20%5Calpha_1).%0A%5Cend%7Baligned%7D%0A"> For <img src="https://latex.codecogs.com/png.latex?V"> large and <em>negative</em>, on the other hand, <img src="https://latex.codecogs.com/png.latex?X'%5Cbeta"> is large and negative, <img src="https://latex.codecogs.com/png.latex?F(X'%5Cbeta)"> is close to zero, and <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Calpha_0%20+%20(1%20-%20%5Calpha_0%20-%20%5Calpha_1)%20F(X'%5Cbeta)%20&amp;%5Capprox%20%5Calpha_0%20+%20(1%20-%20%5Calpha_0%20-%20%5Calpha_1)%20%5Ctimes%200%5C%5C%0A&amp;=%20%5Calpha_0.%0A%5Cend%7Baligned%7D%0A"> Intuitively, by examining values of <img src="https://latex.codecogs.com/png.latex?X_i"> for which <img src="https://latex.codecogs.com/png.latex?F(X_i'%5Cbeta)"> is close to one we can learn <img src="https://latex.codecogs.com/png.latex?(1%20-%20%5Calpha_1)"> and by examining values for which <img src="https://latex.codecogs.com/png.latex?F(X_i'%5Cbeta)"> is close to zero we can identify <img src="https://latex.codecogs.com/png.latex?%5Calpha_0">.</p>
<p>You may object that the preceding identification argument sounds suspiciously circular: doesn’t this idea at least implicitly require us to know <img src="https://latex.codecogs.com/png.latex?%5Cbeta">? Fortunately the answer is no. We only need to know the <em>signs</em> of <img src="https://latex.codecogs.com/png.latex?%5Cbeta">. Under the assumption that <img src="https://latex.codecogs.com/png.latex?%5Calpha_0%20+%20%5Calpha_1%20%3C%201"> these are the same as the signs of the observed partial effects <img src="https://latex.codecogs.com/png.latex?%5Cpartial%20%5Cmathbb%7BP%7D(Y=1%7CX)%20/%5Cpartial%20%5Cbeta_j">. An example may help. Suppose <img src="https://latex.codecogs.com/png.latex?Y=1"> means “graduated from college.” Under fixed misclassification, we would learn <img src="https://latex.codecogs.com/png.latex?%5Calpha_0"> from the observations of <img src="https://latex.codecogs.com/png.latex?(Y_i,%20X_i)"> for people who almost certainly <em>didn’t</em> graduate from college, based on their covariates, and <img src="https://latex.codecogs.com/png.latex?(1%20-%20%5Calpha_1)"> from observations of <img src="https://latex.codecogs.com/png.latex?(Y_i,%20X_i)"> for people who almost certainly <em>did</em>. By first estimating <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Y=1%7CX)"> we learn <em>attenuated</em> versions of the true partial effects <img src="https://latex.codecogs.com/png.latex?F(X'%5Cbeta)%20%5Cbeta_j">. In other words, we learn how <em>reported</em> education varies with <img src="https://latex.codecogs.com/png.latex?X">. But this information suffices to show us how to make <img src="https://latex.codecogs.com/png.latex?F(X'%5Cbeta)"> close to zero or one.</p>
<p>The preceding argument <em>crucially relies</em> on the assumption that <img src="https://latex.codecogs.com/png.latex?F"> is nonlinear. To see why, consider the linear probability model <img src="https://latex.codecogs.com/png.latex?F(X'%5Cbeta)%20=%20X'%5Cbeta"> and let <img src="https://latex.codecogs.com/png.latex?X'%20=%20(1,%20X_1')"> and <img src="https://latex.codecogs.com/png.latex?%5Cbeta'%20=%20(%5Cbeta_0,%20%5Cbeta_1')">. Then, <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%20%20%5Cmathbb%7BP%7D(Y=1%7CX)%0A%20%20&amp;=%20%5Calpha_0%20+%20%5Cleft(1%20-%20%5Calpha_0%20-%20%5Calpha_1%5Cright)%20F(X'%5Cbeta)%5C%5C%0A%20%20&amp;=%20%5Calpha_0%20+%20%5Cleft(1%20-%20%5Calpha_0%20-%20%5Calpha_1%5Cright)(X'%5Cbeta)%20%5C%5C%0A%20%20&amp;=%20%5Calpha_0%20+%20%5Cleft(1%20-%20%5Calpha_0%20-%20%5Calpha_1%5Cright)(%5Cbeta_0%20+%20X_1'%20%5Cbeta_1)%20%5C%5C%0A%20%20&amp;=%20%5Calpha_0%20+%20(1%20-%20%5Calpha_0%20-%20%5Calpha_1)%20%5Cbeta_0%20+%20X_1'%20(1%20-%20%5Calpha_0%20-%20%5Calpha_1)%20%5Cbeta_1.%0A%5Cend%7Baligned%7D%0A"> Now, defining <img src="https://latex.codecogs.com/png.latex?%5Cwidetilde%7B%5Cbeta%7D_0%20%5Cequiv%20%5Calpha_0%20+%20(1%20-%20%5Calpha_0%20-%20%5Calpha_1)%5Cbeta_0">, <img src="https://latex.codecogs.com/png.latex?%5Cwidetilde%7B%5Cbeta%7D_1%20=%20(1%20-%20%5Calpha_0%20-%20%5Calpha_1)%20%5Cbeta_1">, and <img src="https://latex.codecogs.com/png.latex?%5Cwidetilde%7B%5Cbeta%7D'%20=%20(%5Cwidetilde%7B%5Cbeta%7D_0,%20%5Cwidetilde%7B%5Cbeta%7D_1')"> we have <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BP%7D(Y=1%7CX)%20=%20%5Calpha_0%20+%20(1%20-%20%5Calpha_0%20-%20%5Calpha_1)%20X'%5Cbeta%20%20=%20X'%5Cwidetilde%7B%5Cbeta%7D.%0A"> This shows that a linear probability model with coefficient vector <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> and mis-classification probabilities <img src="https://latex.codecogs.com/png.latex?(%5Calpha_0,%20%5Calpha_1)"> is <em>observationally equivalent</em> to a linear probability model with <em>no mis-classification</em> and coefficient <img src="https://latex.codecogs.com/png.latex?%5Cwidetilde%7B%5Cbeta%7D">. To put it another way: there is no way to tell whether mis-classification is present or absent in a linear model. Doing so requires <em>non-linearity</em>.</p>
<p>So how can we use these results in practice? If <img src="https://latex.codecogs.com/png.latex?(%5Calpha_0,%20%5Calpha_1,%20%5Cbeta)"> are identified and <img src="https://latex.codecogs.com/png.latex?F"> is assumed known, we can proceed via garden-variety maximum likelihood estimation. The log-likelihood function is only slightly more complicated than in the standard binary outcome setting, in particular: <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%20%20%5Cell_n(%5Calpha_0,%20%5Calpha_1,%20%5Cbeta)%20&amp;=%20%5Cfrac%7B1%7D%7Bn%7D%20%5Csum_%7Bi=1%7D%5En%20%5Clog%5Cleft%5C%7B%20%5Cmathbb%7BP%7D(Y_i=1%7CX)%5E%7B%5Cmathbb%7B1%7D(Y_i=1)%7D%5Cmathbb%7BP%7D(Y_i=0%7CX_i)%5E%7B%5Cmathbb%7B1%7D(Y_i=0)%7D%20%5Cright%5C%7D%20%5C%5C%0A%20%20&amp;=%20%5Cfrac%7B1%7D%7Bn%7D%20%5Csum_%7Bi=1%7D%5En%20Y_i%20%5Clog%5Cleft%5C%7B%20%20%5Calpha_0%20+%20(1%20-%20%5Calpha_0%20-%20%5Calpha_1)%20F(X_i'%5Cbeta)%20%5Cright%5C%7D%20+%20(1%20-%20Y_i)%20%5Clog%5Cleft%5C%7B%201%20-%20%5Calpha_0%20-%20(1%20-%20%5Calpha_0%20-%20%5Calpha_1)%20F(X_i'%5Cbeta)%20%5Cright%5C%7D.%0A%5Cend%7Baligned%7D%0A"> If <img src="https://latex.codecogs.com/png.latex?F"> is unknown, estimation is more complicated but the intuition from above continues to hold: a regressor <img src="https://latex.codecogs.com/png.latex?V"> with “large support” allows us to identify the mis-classification probabilities, and hence <img src="https://latex.codecogs.com/png.latex?F">. Indeed, we can even allow <img src="https://latex.codecogs.com/png.latex?%5Calpha_0"> and <img src="https://latex.codecogs.com/png.latex?%5Calpha_1"> to depend covariates, as long as they don’t depend on <img src="https://latex.codecogs.com/png.latex?V"> itself. For more details on the “identification by nonlinearity” approach, see <a href="https://ideas.repec.org/a/eee/econom/v87y1998i2p239-269.html">Hausman et al.&nbsp;(1998)</a> and <a href="https://ideas.repec.org/a/cup/etheor/v16y2000i04p603-609_16.html">Lewbel (2000)</a>.</p>
</section>
</section>
<section id="coming-attractions" class="level2" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="coming-attractions"><span class="header-section-number">4</span> Coming Attractions</h2>
<p>That’s more than enough about measurement error for now! When I return to this topic in a few weeks time, I’ll consider the problem of a mis-measured binary <em>regressor</em>. In my next installment, however, I’ll put measurement error to one side and revisit a classic problem from introductory statistics: constructing a confidence interval for a population proportion. Sometimes the easiest things turn out to be much harder than they first appear.</p>


</section>


 ]]></description>
  <category>measurement error</category>
  <guid>https://www.econometrics.blog/post/regressions-with-a-mis-measured-binary-outcome/</guid>
  <pubDate>Mon, 30 Aug 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Beyond Classical Measurement Error</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/beyond-classical-measurement-error/</link>
  <description><![CDATA[ 




<p>Pop Quiz: If <img src="https://latex.codecogs.com/png.latex?D%5E*"> and <img src="https://latex.codecogs.com/png.latex?D"> are binary random variables and <img src="https://latex.codecogs.com/png.latex?D"> is a noisy measure of <img src="https://latex.codecogs.com/png.latex?D%5E*">, is it possible for the measurement error <img src="https://latex.codecogs.com/png.latex?W%20%5Cequiv%20D%20-%20D%5E*"> to be <em>classical</em>? Explain why or why not. (Answer below)</p>
<section id="classical-measurement-error" class="level2 page-columns page-full" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="classical-measurement-error"><span class="header-section-number">1</span> Classical Measurement Error</h2>
<p>Classical measurement error is a problem that is easy to understand and relatively easy to address. Roughly speaking, classical measurement error refers to a situation in which the variable we observe equals the truth plus noise <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BObserved%7D%20=%20%5Ctext%7BTruth%7D%20+%20%5Ctext%7BNoise%7D%0A"> where the noise is unrelated to the truth and “everything else.” (I’ll be precise about the meaning of “unrelated” and “everything else” in a moment.) Mis-measuring a regressor <img src="https://latex.codecogs.com/png.latex?X"> in this way biases the OLS slope estimator towards zero (<em>attenuation bias</em>) but we can correct for this with a valid instrument. Mis-measuring the outcome <img src="https://latex.codecogs.com/png.latex?Y"> increases standard errors but doesn’t bias the OLS estimator. You can find all the details in your favorite introductory econometrics textbook, but in the interest of making this post self-contained, here’s a quick review.</p>
<section id="least-squares-attenuation-bias" class="level3">
<h3 class="anchored" data-anchor-id="least-squares-attenuation-bias">Least Squares Attenuation Bias</h3>
<p>Suppose that we want to learn the slope coefficient from a population linear regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X%5E*">: <img src="https://latex.codecogs.com/png.latex?%0A%5Cbeta%20%5Cequiv%20%5Cfrac%7B%5Ctext%7BCov%7D(Y,X%5E*)%7D%7B%5Ctext%7BVar%7D(X%5E*)%7D.%0A"> Unfortunately we observe not <img src="https://latex.codecogs.com/png.latex?X%5E*"> but a noisy measure <img src="https://latex.codecogs.com/png.latex?X%20=%20X%5E*%20+%20W_X"> where <img src="https://latex.codecogs.com/png.latex?W_X"> is uncorrelated with both <img src="https://latex.codecogs.com/png.latex?X%5E*"> and <img src="https://latex.codecogs.com/png.latex?Y">. Then <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Ctext%7BCov%7D(Y,%20X)%20&amp;=%20%5Ctext%7BCov%7D(Y,%20X%5E*%20+%20W_X)%20=%20%5Ctext%7BCov%7D(Y,%20X%5E*)%5C%5C%0A%5Ctext%7BVar%7D(X)%20&amp;=%20%5Ctext%7BVar%7D(X%5E*%20+%20W_X)%20=%20%5Ctext%7BVar%7D(X%5E*)%20+%20%5Ctext%7BVar%7D(W_X).%0A%5Cend%7Baligned%7D%0A"> Now, define the <em>reliability ratio</em> <img src="https://latex.codecogs.com/png.latex?%5Clambda"> as follows: <img src="https://latex.codecogs.com/png.latex?%0A%5Clambda%20%5Cequiv%20%5Cfrac%7B%5Ctext%7BVar%7D(X%5E*)%7D%7B%5Ctext%7BVar%7D(X%5E*)%20+%20%5Ctext%7BVar%7D(W_X)%7D.%0A"> Measurement error means that <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVar%7D(W_X)"> is positive. Since variances can’t be negative, this implies <img src="https://latex.codecogs.com/png.latex?0%20%3C%20%5Clambda%20%3C%201">. Combining our definition of <img src="https://latex.codecogs.com/png.latex?%5Clambda"> with the expressions for <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BCov%7D(Y,X)"> and <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVar%7D(X)"> from above, <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cfrac%7B%5Ctext%7BCov%7D(Y,X)%7D%7B%5Ctext%7BVar%7D(X)%7D%20&amp;=%20%5Cfrac%7B%5Ctext%7BCov%7D(Y,%20X%5E*)%7D%7B%5Ctext%7BVar%7D(X%5E*)%20+%20%5Ctext%7BVar%7D(W_X)%7D%20%5C%5C%0A&amp;=%5Cfrac%7B%5Ctext%7BVar%7D(X%5E*)%7D%7B%5Ctext%7BVar%7D(X%5E*)%20+%20%5Ctext%7BVar%7D(W_X)%7D%5Ccdot%20%5Cfrac%7B%5Ctext%7BCov%7D(Y,%20X%5E*)%7D%7B%5Ctext%7BVar%7D(X%5E*)%7D%5C%5C%0A&amp;=%20%5Clambda%20%5Cbeta%0A%5Cend%7Baligned%7D%0A"> so we see that regressing <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X"> gives <img src="https://latex.codecogs.com/png.latex?%5Clambda%20%5Cbeta"> rather than <img src="https://latex.codecogs.com/png.latex?%5Cbeta">. Since <img src="https://latex.codecogs.com/png.latex?0%20%3C%20%5Clambda%20%3C%201">, this phenomenon is called <em>least squares attenuation bias</em>: <img src="https://latex.codecogs.com/png.latex?%5Clambda%20%5Cbeta"> has the same sign as <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> but is smaller in magnitude. The greater the extent of measurement error, the larger the variance of <img src="https://latex.codecogs.com/png.latex?W_X"> and the smaller that <img src="https://latex.codecogs.com/png.latex?%7C%5Clambda%20%5Cbeta%7C"> becomes.</p>
</section>
<section id="instrumental-variables-to-the-rescue" class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="instrumental-variables-to-the-rescue">Instrumental variables to the rescue</h3>
<p>Suppose that <img src="https://latex.codecogs.com/png.latex?Y%20=%20%5Calpha%20+%20%5Cbeta%20X%5E*%20+%20U"> where <img src="https://latex.codecogs.com/png.latex?X%20=%20X%5E*%20+%20W_X"> as above. Now suppose that we can find a variable <img src="https://latex.codecogs.com/png.latex?Z"> that is correlated with <img src="https://latex.codecogs.com/png.latex?X%5E*"> but uncorrelated with <img src="https://latex.codecogs.com/png.latex?U"> and <img src="https://latex.codecogs.com/png.latex?W_X">. Then <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Ctext%7BCov%7D(Y,Z)%20&amp;=%20%5Ctext%7BCov%7D(%5Calpha%20+%20%5Cbeta%20X%5E*%20+%20U,%20Z)%20=%20%5Cbeta%5Ctext%7BCov%7D(X%5E*,Z)%5C%5C%0A%5Ctext%7BCov%7D(X,Z)%20&amp;=%20%5Ctext%7BCov%7D(X%5E*%20+%20W_X,%20Z)%20=%20%5Ctext%7BCov%7D(X%5E*,Z)%0A%5Cend%7Baligned%7D%0A"> so that <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%20%5Ctext%7BCov%7D(Y,%20Z)%20/%20%5Ctext%7BCov%7D(X,Z)">. If <img src="https://latex.codecogs.com/png.latex?X%5E*"> is measured with classical measurement error, a simple instrumental variables regression solves the problem of attenuation bias.<sup>1</sup> Notice that we haven’t said anything about <img src="https://latex.codecogs.com/png.latex?U"> in relation to <img src="https://latex.codecogs.com/png.latex?X%5E*">. If <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> is the population linear regression slope, then <img src="https://latex.codecogs.com/png.latex?U"> is uncorrelated with <img src="https://latex.codecogs.com/png.latex?X%5E*"> by definition. But this derivation still goes through if <img src="https://latex.codecogs.com/png.latex?Y%20=%20%5Calpha%20+%20%5Cbeta%20X%5E*%20+%20U"> is a <em>causal model</em> in which <img src="https://latex.codecogs.com/png.latex?X%5E*"> is correlated with <img src="https://latex.codecogs.com/png.latex?U">, e.g.&nbsp;if <img src="https://latex.codecogs.com/png.latex?Y"> is wage and <img src="https://latex.codecogs.com/png.latex?X%5E*"> is years of schooling, in which case <img src="https://latex.codecogs.com/png.latex?U"> might be “unobserved ability.” In this way, a <em>single</em> valid instrument can serve “double-duty,” eliminating both attenuation bias and selection bias.</p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;Strictly speaking I haven’t used the assumption that <img src="https://latex.codecogs.com/png.latex?W_X"> is uncorrelated with <img src="https://latex.codecogs.com/png.latex?X%5E*"> in this derivation, but it’s implicit in the assumption that <img src="https://latex.codecogs.com/png.latex?Z"> is correlated with <img src="https://latex.codecogs.com/png.latex?X%5E*"> but not with <img src="https://latex.codecogs.com/png.latex?W_X">.</p></div></div></section>
<section id="measurement-error-in-the-outcome" class="level3">
<h3 class="anchored" data-anchor-id="measurement-error-in-the-outcome">Measurement error in the outcome</h3>
<p>Now suppose that <img src="https://latex.codecogs.com/png.latex?X%5E*"> is observed but the true <em>outcome</em> <img src="https://latex.codecogs.com/png.latex?Y%5E*"> is not: we only observe a noisy measure <img src="https://latex.codecogs.com/png.latex?Y%20=%20Y%5E*%20+%20W_Y">. If <img src="https://latex.codecogs.com/png.latex?W_Y"> is uncorrelated with <img src="https://latex.codecogs.com/png.latex?X%5E*">, <img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7B%5Ctext%7BCov%7D(Y,X%5E*)%7D%7B%5Ctext%7BVar%7D(X%5E*)%7D%20=%20%5Cfrac%7B%5Ctext%7BCov%7D(Y%5E*%20+%20W_Y,%20X%5E*)%7D%7B%5Ctext%7BVar%7D(X%5E*)%7D%20=%20%5Cfrac%7B%5Ctext%7BCov%7D(Y%5E*,X%5E*)%7D%7B%5Ctext%7BVar%7D(X%5E*)%7D%0A"></p>
<p>so we’ll obtain the same slope from a regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X%5E*"> as we would from a regression of <img src="https://latex.codecogs.com/png.latex?Y%5E*"> on <img src="https://latex.codecogs.com/png.latex?X%5E*">. Classical measurement error in the outcome variable doesn’t introduce a bias.</p>
</section>
</section>
<section id="solution-to-the-pop-quiz" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="solution-to-the-pop-quiz"><span class="header-section-number">2</span> Solution to the Pop Quiz</h2>
<p>Now that we’ve refreshed our memories about classical measurement error, let’s take a look at my pop quiz question from above:</p>
<blockquote class="blockquote">
<p>If <img src="https://latex.codecogs.com/png.latex?D%5E*"> and <img src="https://latex.codecogs.com/png.latex?D"> are binary random variables and <img src="https://latex.codecogs.com/png.latex?D"> is a noisy measure of <img src="https://latex.codecogs.com/png.latex?D%5E*">, is it possible for the measurement error <img src="https://latex.codecogs.com/png.latex?W%20%5Cequiv%20D%20-%20D%5E*"> to be <em>classical</em>? Explain why or why not.</p>
</blockquote>
<p>If <img src="https://latex.codecogs.com/png.latex?W"> is a classical measurement error then, among other things, it must be uncorrelated with <img src="https://latex.codecogs.com/png.latex?D%5E*">. But this is <em>impossible</em> if both <img src="https://latex.codecogs.com/png.latex?D%5E*"> and <img src="https://latex.codecogs.com/png.latex?D"> are binary. By the definition of <img src="https://latex.codecogs.com/png.latex?W">, <img src="https://latex.codecogs.com/png.latex?D%20=%20D%5E*%20+%20W">. If <img src="https://latex.codecogs.com/png.latex?D%5E*%20=%201"> then <img src="https://latex.codecogs.com/png.latex?D%20=%201%20+%20W">. To ensure that <img src="https://latex.codecogs.com/png.latex?D"> takes on a value in <img src="https://latex.codecogs.com/png.latex?%5C%7B0,%201%5C%7D">, this means that <img src="https://latex.codecogs.com/png.latex?W"> must be either <img src="https://latex.codecogs.com/png.latex?0"> or <img src="https://latex.codecogs.com/png.latex?-1">. If instead <img src="https://latex.codecogs.com/png.latex?D%5E*%20=%200">, then <img src="https://latex.codecogs.com/png.latex?D%20=%20W">, so <img src="https://latex.codecogs.com/png.latex?W"> must be either <img src="https://latex.codecogs.com/png.latex?0"> or <img src="https://latex.codecogs.com/png.latex?1">. Hence, unless <img src="https://latex.codecogs.com/png.latex?W"> always equals zero, in which case there’s no measurement error, <img src="https://latex.codecogs.com/png.latex?W"> must always be <em>negatively correlated</em> with <img src="https://latex.codecogs.com/png.latex?D%5E*">. In other words, measurement error in a binary variable can never be classical. The same basic logic applies whenever <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?X%5E*"> are bounded: to ensure that <img src="https://latex.codecogs.com/png.latex?X"> stays within its bounds, any measurement error must be correlated with <img src="https://latex.codecogs.com/png.latex?X%5E*">.</p>
</section>
<section id="non-differential-measurement-error" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="non-differential-measurement-error"><span class="header-section-number">3</span> Non-Differential Measurement Error</h2>
<p>Classical measurement error, as we’ve seen, is a very special case. Or to put it another way, non-classical measurement error isn’t as exotic as it sounds. Because discrete random variables cannot be subject to classical measurement error, non-classical measurement error should be on any applied economist’s radar. My next few posts will provide an overview of the simplest case: <em>non-differential</em> measurement error in a binary variable. This assumption allows <img src="https://latex.codecogs.com/png.latex?D%5E*"> to be correlated with <img src="https://latex.codecogs.com/png.latex?W">, but assumes that conditioning on <img src="https://latex.codecogs.com/png.latex?D%5E*"> is sufficient to <em>break</em> the dependence between <img src="https://latex.codecogs.com/png.latex?W"> and everything else. Even in this relatively simple case, everything we’ve learned about classical measurement error goes out the window:</p>
<ol type="1">
<li>Non-differential measurement error does <em>not</em> necessarily cause attenuation.</li>
<li>The IV estimator doesn’t correct for non-differential measurement error, and a single instrument cannot serve “double-duty.”</li>
<li>Non-classical measurement error in the outcome variable generally <em>does</em> introduce bias.</li>
</ol>
<p>The good news is that there are methods to address non-differential measurement error. In my next post, I’ll start by considering the case of a mis-measured binary outcome.</p>


</section>


 ]]></description>
  <category>measurement error</category>
  <guid>https://www.econometrics.blog/post/beyond-classical-measurement-error/</guid>
  <pubDate>Mon, 23 Aug 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Understanding the F Statistic</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/understanding-the-f-statistic/</link>
  <description><![CDATA[ 




<p>The F-statistic for a test of multiple linear restrictions is a staple of introductory econometrics courses. In the simplest case, it can be written as <img src="https://latex.codecogs.com/png.latex?F%20%5Cequiv%20%5Cfrac%7B(SSR_r%20-%20SSR_%7Bu%7D)/q%7D%7BSSR_%7Bu%7D%20/%20(n%20-%20k%20-%201)%7D"> where <img src="https://latex.codecogs.com/png.latex?SSR_r"> is the <em>restricted</em> sum of squared residuals, <img src="https://latex.codecogs.com/png.latex?SSR_%7Bu%7D"> is the <em>unrestricted</em> sum of squared residuals, <img src="https://latex.codecogs.com/png.latex?q"> is the number of restrictions, and <img src="https://latex.codecogs.com/png.latex?(n%20-%20k%20-%201)"> is the degrees of freedom of the unrestricted model.<sup>1</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;Specifically, the “simplest case” refers to a setting in which both the restricted and unrestricted models include an intercept and we assume homoskedasticity.</p></div></div><p>In my experience, students encountering this expression for the first time find it bewilderingly arbitrary; it becomes just one more item to add to a list of formulas memorized for the exam and promptly forgotten. My aim in this post is to demystify the <img src="https://latex.codecogs.com/png.latex?F"> statistic. By the end, I hope that you will find the form of this expression intuitive, perhaps even <em>obvious</em>.</p>
<p>This is not a post about asymptotic theory, and it is not a post about heteroskedasticity. I will not prove that <img src="https://latex.codecogs.com/png.latex?F"> follows an <img src="https://latex.codecogs.com/png.latex?F">-distribution, and I will blithely assume that we inhabit the idealized textbook realm in which all errors are homoskedastic. I will also dodge the question of whether you should even be carrying out an F-test in the first place.<sup>2</sup> This is a post about understanding what the <img src="https://latex.codecogs.com/png.latex?F">-statistic measures and why it takes the form that it does.</p>
<div class="no-row-height column-margin column-container"><div id="fn2"><p><sup>2</sup>&nbsp;See <a href="http://www.stat.cmu.edu/~cshalizi/mreg/15/lectures/10/lecture-10.pdf">F-Tests, R-squared, and Other Distractions</a> for an insightful critique.</p></div></div><section id="the-simplest-possible-example" class="level2 page-columns page-full" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="the-simplest-possible-example"><span class="header-section-number">1</span> The Simplest Possible Example</h2>
<p>The best way to understand the <img src="https://latex.codecogs.com/png.latex?F">-statistic is by looking at an example that’s so simple that there’s no reason to use an <img src="https://latex.codecogs.com/png.latex?F">-test in the first place. Here’s a dataset of students’ scores on two introductory statistics midterms that I gave many years ago:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">midterms <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'https://ditraglia.com/econ103/midterms.csv'</span>)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(midterms)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  Midterm1 Midterm2
1    57.14    60.71
2    77.14    77.86
3    83.57    93.57
4    88.00       NA
5    69.29    72.14
6    80.71    89.29</code></pre>
</div>
</div>
<p>As you can see, there is at least one missing observation: student #4 scored 88% on the first midterm, but missed the second. In fact, nine students missed the second midterm:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(midterms)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>    Midterm1        Midterm2    
 Min.   :56.43   Min.   :47.86  
 1st Qu.:70.53   1st Qu.:74.64  
 Median :80.36   Median :84.29  
 Mean   :79.74   Mean   :81.39  
 3rd Qu.:87.86   3rd Qu.:90.71  
 Max.   :97.86   Max.   :99.29  
                 NAs    :9      </code></pre>
</div>
</div>
<p>To keep this example as simple as possible, I’ll drop the missing observations.<sup>3</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn3"><p><sup>3</sup>&nbsp;Health warning: it <em>dangerous</em> to glibly drop missing observations in applied work! The key question is why those nine students missed the second exam. If their reasons for doing so are “as good as randomly assigned,” e.g.&nbsp;a death in the family or an unexpected illness or injury, these observations as <em>missing at random</em> (MAR). In this case, dropping them is statistically innocuous. If instead these students’ reasons for missing the exam are related to their course performance, then dropping their observations could yield a misleading picture of the underlying relationship between midterm scores.</p></div></div><div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">midterms <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>(midterms)</span></code></pre></div></div>
</div>
<p>Let’s call student #4 Natalie: she scored 88% on the first midterm but missed the second. Suppose we wanted to predict how well Natalie <em>would have done</em> on the second midterm had she taken it. There are many ways that we could try to make this prediction. One possibility would be to ignore Natalie’s score on the first midterm, and predict that she would have scored 81.4 on the second: the average score among all students who took this exam. Another possibility would be to fit a linear regression to the scores of all students who took <em>both</em> exams and use this to project Natalie’s score on midterm two based on her score on midterm one. If scores on the two exams are correlated, option two seems like a better idea: Natalie outperformed the class average on midterm one by 8.4 or roughly 0.77 standard deviations. It seems reasonable to account for this when predicting her score on the second exam.</p>
<p>In fact both of these prediction rules can be viewed as special cases of linear regression. Let <img src="https://latex.codecogs.com/png.latex?x_i"> denote student <img src="https://latex.codecogs.com/png.latex?i">’s score on midterm one and <img src="https://latex.codecogs.com/png.latex?y_i"> denote her score on midterm two. The sample mean <img src="https://latex.codecogs.com/png.latex?%5Cbar%7By%7D%20=%20%5Cfrac%7B1%7D%7Bn%7D%20%5Csum_%7Bi=1%7D%5En%20y_i"> solves the optimization problem <img src="https://latex.codecogs.com/png.latex?%0A%5Cmin_a%20%5Csum_%7Bi=1%7D%5En%20(y_i%20-%20a)%5E2%0A"> which is simply least squares without a predictor variable.<sup>4</sup> In contrast, the usual least-squares regression problem is <img src="https://latex.codecogs.com/png.latex?%0A%5Cmin_%7Ba,b%7D%20%5Csum_%7Bi=1%7D%5En%20(y_i%20-%20a%20-%20b%20x_i)%5E2%0A"> with solutions <img src="https://latex.codecogs.com/png.latex?%5Chat%7Ba%7D%20=%20%5Cbar%7By%7D%20-%20%5Chat%7Bb%7D%20%5Cbar%7Bx%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Chat%7Bb%7D%20=%20s_%7Bxy%7D%20/%20s_x%5E2">, where <img src="https://latex.codecogs.com/png.latex?s_%7Bxy%7D"> is the sample covariance of scores on the two midterms and <img src="https://latex.codecogs.com/png.latex?s_x%5E2"> is the sample variance of scores on the first midterm. Notice how these two optimization problems are related: the first is a <em>restricted</em> (aka <em>constrained</em>) version of the second with the constraint <img src="https://latex.codecogs.com/png.latex?b%20=%200">. In the discussion below, I will call the first of these the <em>restricted regression</em> and the second the <em>unrestricted regression</em>.</p>
<div class="no-row-height column-margin column-container"><div id="fn4"><p><sup>4</sup>&nbsp;If you’re taking introductory statistics or econometrics it’s a good idea to try to prove this for yourself!</p></div></div><p>It’s easy to fit these regressions in R. We’ll start with the restricted:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">restricted <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Midterm2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> midterms)</span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(restricted)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
lm(formula = Midterm2 ~ 1, data = midterms)

Residuals:
    Min      1Q  Median      3Q     Max 
-33.531  -6.746   2.899   9.319  17.899 

Coefficients:
            Estimate Std. Error t value Pr(&gt;|t|)    
(Intercept)   81.391      1.457   55.86   &lt;2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 12.28 on 70 degrees of freedom</code></pre>
</div>
</div>
<p>The syntax <code>Midterm2 ~ 1</code> specifies a regression formula containing <em>no predictor variables</em>, only an intercept. Notice that our estimate for the intercept agrees with the sample mean score on the second midterm from above, as it should!</p>
<p>Turning our attention to the unrestricted regression, we see that scores on the first midterm are strongly predictive of scores on the second:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">unrestricted <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Midterm2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Midterm1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> midterms)</span>
<span id="cb8-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(unrestricted)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
lm(formula = Midterm2 ~ Midterm1, data = midterms)

Residuals:
    Min      1Q  Median      3Q     Max 
-22.809  -7.127   2.047   8.125  18.549 

Coefficients:
            Estimate Std. Error t value Pr(&gt;|t|)    
(Intercept)   32.575      9.243   3.524 0.000759 ***
Midterm1       0.613      0.115   5.329 1.17e-06 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 10.41 on 69 degrees of freedom
Multiple R-squared:  0.2916,    Adjusted R-squared:  0.2813 
F-statistic:  28.4 on 1 and 69 DF,  p-value: 1.174e-06</code></pre>
</div>
</div>
<p>For a pair of students who differed by one point in their scores on the first midterm, we would predict a difference of 0.61 points on the second.</p>
<p>The restricted regression <em>ignores</em> a student’s score on the first midterm when predicting her score on the second. But we’ve seen from the unrestricted regression that scores on midterm #1 are strongly correlated with scores on midterm #2. As such, our best bet is to predict Natalie’s second midterm score using the unrestricted regression model:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(unrestricted, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Midterm1 =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">88</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>       1 
86.52169 </code></pre>
</div>
</div>
<p>Because Natalie scored above the mean on the first exam, we predict that she will score above the mean on the second exam.</p>
</section>
<section id="how-much-better-is-the-fit-of-the-unrestricted-regression" class="level2 page-columns page-full" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="how-much-better-is-the-fit-of-the-unrestricted-regression"><span class="header-section-number">2</span> How much better is the fit of the unrestricted regression?</h2>
<p>While I didn’t present it in this way, the choice between restricted and unrestricted regressions above could be formulated as a hypothesis test. The restricted regression imposes a zero regression slope, but the unrestricted regression doesn’t. In this case, we can test the restriction that the slope is in fact zero using simple t-test. Based on the t-statistic of 5.33 from above we would easily <em>reject</em> the restriction at any conventional significance level.<sup>5</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn5"><p><sup>5</sup>&nbsp;If you’re familiar with the “trinity” of classical tests (Score/LM, Likelihood Ratio, and Wald), this reasoning amounts to a Wald test: fit the unrestricted model and use the distance between the parameter estimates and their values under the restriction to form a test statistic. If the unrestricted estimates are close enough to the restriction, don’t reject it.</p></div><div id="fn6"><p><sup>6</sup>&nbsp;Again, if you’re familiar with the aforementioned “trinity” of tests, the procedure I’m about to describe amounts to a Likelihood Ratio Test: fit both the restricted and unrestricted models, and compare their maximized sample likelihoods. If the likelihoods are similar, don’t reject the restriction.</p></div></div><p>But there’s another way to carry out the same test. Although it would be overkill in this example, the principles that underlie it can be used to carry out tests in more complicated situations where a simple t-test wouldn’t suffice. Rather than examining the slope estimate from the unrestricted regression, this alternative approach compares the <em>sum of squared residuals</em> (SSR) of the two regressions to see which does a better job of fitting the observed data.<sup>6</sup> It’s easy to compute the SSR of the two regressions from above using the <code>residuals()</code> function:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">SSR_u <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">residuals</span>(unrestricted)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb12-2">SSR_r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">residuals</span>(restricted)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb12-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Unrestricted =</span> SSR_u, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Restricted =</span> SSR_r)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Unrestricted   Restricted 
    7475.741    10552.883 </code></pre>
</div>
</div>
<p>The SSR of the restricted regression is <em>higher</em> than that of the unrestricted regression. But what exactly should we make of this? A picture can help to make things clearer. This one has two panels: one for the restricted regression and another for the unrestricted regression. Each panel plots the observations from the <code>midterms</code> dataset along with the fitted regression line, using dashed vertical lines to indicate the residuals: the distance from a given observation to the regression line.</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/understanding-the-f-statistic/index_files/figure-html/SSRplot-1.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Notice that the restricted regression line is flat because it does not use scores on the first midterm to predict those on the second. The lower SSR of the unrestricted model reflects the fact that the observations in the <code>midterms</code> dataset are on average <em>closer</em> to a line with slope 0.6 and intercept 32.6 than they are to a line with slope zero and intercept 81.4.</p>
<p>To understand this picture, it helps to think about the following question: is it possible for the unrestricted regression to have a <em>higher</em> SSR than the restricted one? Recall from above that each of these regressions is the solution to an optimization problem. The difference between them is that the restricted regression imposes a <em>constraint</em> while the unrestricted regression doesn’t. If the best slope for predicting second midterm scores using first midterm scores is zero, the unrestricted regression is free to set <img src="https://latex.codecogs.com/png.latex?b%20=%200">. In this case its estimates would coincide with those of the restricted regression. On the other hand, if the best slope <em>isn’t</em> zero that means some other choice of <img src="https://latex.codecogs.com/png.latex?b"> by definition results in a lower SSR: linear regression chooses the line whose slope and intercept minimize the squared vertical deviations between the data and the line. The restricted regression is <em>forced</em> to have <img src="https://latex.codecogs.com/png.latex?b%20=%200">, so in this case it must do a worse job fitting the data.<sup>7</sup> This reasoning shows that we will <em>always</em> find that the SSR of the restricted model is at least as large as that of the unrestricted model.</p>
<div class="no-row-height column-margin column-container"><div id="fn7"><p><sup>7</sup>&nbsp;More generally, imposing a constraint can never result in a <em>better</em> solution to an optimization problem: at best it can leave the optimum unchanged.</p></div></div><p>We shouldn’t be surprised to see that the unrestricted regression “fits the data” better than the restricted one: it can’t do otherwise unless the sample correlation between midterm scores is exactly zero. But there is still the question of <em>how much better</em> it fits. Taking differences tells us how much larger the SSR of the restricted model is compared to that of the unrestricted model:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">SSR_r <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> SSR_u</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3077.142</code></pre>
</div>
</div>
<p>So is this a big difference or a small difference? The answer depends on the <em>units</em> in which <img src="https://latex.codecogs.com/png.latex?y"> is measured. A residual is a vertical deviation, i.e.&nbsp;a distance along the <img src="https://latex.codecogs.com/png.latex?y">-axis. This means that it has the same units as the <img src="https://latex.codecogs.com/png.latex?y">-variable. If <img src="https://latex.codecogs.com/png.latex?y"> is measured in inches, so are the residuals; if <img src="https://latex.codecogs.com/png.latex?y"> is measured in kilometers, so are the residuals. Because the SSR is a sum of squared residuals, it has the same units as <img src="https://latex.codecogs.com/png.latex?y%5E2">. If <img src="https://latex.codecogs.com/png.latex?y"> is measured in inches, the SSR is measured in square inches; if <img src="https://latex.codecogs.com/png.latex?y"> is measured in kilometers, the SSR is measured in square kilometers. Changing the units of <img src="https://latex.codecogs.com/png.latex?y"> changes the units of the SSR. For example, an SSR of one becomes an SSR of <em>one million</em> if we change the units of <img src="https://latex.codecogs.com/png.latex?y"> from kilometers to meters. Accordingly, a comparison of <code>SSR_r</code> to <code>SSR_u</code> is meaningless unless we account for the units of <img src="https://latex.codecogs.com/png.latex?y">.</p>
<p>The simplest way account for units is by <em>eliminating them</em> from the problem. This is precisely what we do when we carry out a t-test: <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bx%7D/%5Ctext%7BSE%7D(%5Cbar%7Bx%7D)"> is unitless because the standard error of <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bx%7D"> has the same units as <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bx%7D"> itself. Any change of units in the numerator would be cancelled out in the denominator. This is a crucial point: test statistics are <em>unitless</em>. We do not compare <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bx%7D"> to a table of normal critical values measured in inches for a distribution with standard deviation <img src="https://latex.codecogs.com/png.latex?2.4">; we compare <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bx%7D/2.4"> to a unitless <em>standard normal</em> distribution.</p>
<p>The t-statistic eliminates units by taking a ratio, so let’s try the same idea in our comparison of <code>SSR_r</code> to <code>SSR_u</code>. There are various possibilities, and any of them would work just as well from the perspective of eliminating units. The F-test statistic is based on a ratio that asks how much <em>worse</em> the restricted model fits <em>relative</em> to the unrestricted regression. In other words, we ask: how much <em>larger</em> is <code>SSR_r</code> compared to <code>SSR_u</code> as a percentage expressed in decimal terms?</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1">(SSR_r <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> SSR_u) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> SSR_u</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.4116171</code></pre>
</div>
</div>
<p>There is nothing subtle going on here. If we wanted to know how much larger US GDP is in 2021 compared to 1921, we would simply calculate <img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7B%5Ctext%7BGDP%7D_%7B2021%7D%20-%20%5Ctext%7BGDP%7D_%7B1921%7D%7D%7B%5Ctext%7BGDP%7D_%7B1921%7D%7D%0A"> assuming, of course, that both of these figures are corrected for inflation! This is precisely the same reasoning that we used above: the SSR “grows” when we impose a restriction. We want to know how much it grows as a percentage. The answer is 0.41 or equivalently 41%.</p>
</section>
<section id="sampling-uncertainty" class="level2 page-columns page-full" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="sampling-uncertainty"><span class="header-section-number">3</span> Sampling Uncertainty</h2>
<p>We’ve <em>nearly</em> arrived at the F-statistic. To see what’s missing, we’ll use a bit of algebra to re-write it as <img src="https://latex.codecogs.com/png.latex?F%20%5Cequiv%20%5Cfrac%7B(SSR_r%20-%20SSR_%7Bu%7D)/q%7D%7BSSR_%7Bu%7D%20/%20(n%20-%20k%20-%201)%7D%20=%20%5Cleft(%5Cfrac%7BSSR_r%20-%20SSR_u%7D%7BSSR_u%7D%5Cright)%20%5Cleft(%5Cfrac%7Bn%20-%20k%20-%201%7D%7Bq%7D%5Cright)."> We obtained the first factor on the RHS, <img src="https://latex.codecogs.com/png.latex?(SSR_r%20-%20SSR_u)%20/%20SSR_u">, simply by reasoning about units and the nature of constrained versus unconstrained optimization problems. Stop for a minute and appreciate how impressive this is: simple intuition has taken us halfway to this rather formidable-looking expression. To understand the second factor, we need to think about sampling uncertainty.</p>
<p>In the midterms example we found that the restricted regression SSR was 41% larger than the unrestricted one. Is this a big difference or a small one? Units don’t enter into it, because we have already eliminated them. But the <code>midterms</code> dataset only contains information on 71 students. If we merely want to summarize the relationship between test scores for these students, there is no role for statistical inference: summary statistics suffice. Tests and confidence intervals enter the picture when we hope to <em>generalize</em> from an observed sample to the population from which it was drawn. Imagine a large population of introductory statistics students who took my two midterms. Now suppose that we observe a random sample of 71 students from this population. How much information do the observed exam scores for <em>these students</em> provide about the relationship between midterm scores that we <em>in the population</em>?<sup>8</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn8"><p><sup>8</sup>&nbsp;It may be difficult to imagine a population from which the students who happen to have taken my course in a particular semester could be viewed as a random sample. For the purposes of this exercise I kindly ask you to suspend your disbelief.</p></div></div><p>The larger the sample size, the more evidence an observed difference in the sample provides about a potential difference in the population. We can see this in the expression for the standard error of the sample mean: <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D(%5Cbar%7BX%7D)%20=%20%5Csigma_x/%5Csqrt%7Bn%7D">. The larger the sample size, the smaller the standard error, all else equal. Accordingly, given two datasets with identical summary statistics, the larger sample will have the larger t-statistic. The same reasoning applies to the F-statistic above. The numerator <img src="https://latex.codecogs.com/png.latex?(n%20-%20k%20-%201)"> in the second factor increases with the sample size <img src="https://latex.codecogs.com/png.latex?n">. This <em>magnifies</em> the effect of the first factor. An <img src="https://latex.codecogs.com/png.latex?SSR_r"> that is 41% higher than the <img src="https://latex.codecogs.com/png.latex?SSR_u"> is “more impressive” evidence when the sample size is <img src="https://latex.codecogs.com/png.latex?1000"> than when it is <img src="https://latex.codecogs.com/png.latex?10">. Small samples are intrinsically more variable than large ones, so we should expect them to turn up anomalous results more frequently. The F-statistic takes this into account.</p>
<p>So why <img src="https://latex.codecogs.com/png.latex?(n%20-%20k%20-%201)"> rather than <img src="https://latex.codecogs.com/png.latex?n">? This is a so-called “degrees of freedom correction.” By estimating <img src="https://latex.codecogs.com/png.latex?k"> regression slope parameters and <img src="https://latex.codecogs.com/png.latex?1"> intercept parameter, we “use up” <img src="https://latex.codecogs.com/png.latex?(k%20+%201)"> of the observations, leaving only <img src="https://latex.codecogs.com/png.latex?(n%20-%20k%20-%201)"> pieces of truly independent information. This is not particularly intuitive. In a stunning departure from my usual advice to introductory statistics and econometrics students, I suggest that you simply memorize this part of the F-statistic. It may help to notice that the same degrees of freedom correction appears in the expression for the standard error of the regression, <img src="https://latex.codecogs.com/png.latex?SER%20%5Cequiv%20%5Csqrt%7BSSR/(n%20-%20k%20-%201)%7D">, a measure of the average distance that the observed data fall from the regression line.</p>
<p>The only as-yet-unexplained quantity in the F-test statistic is <img src="https://latex.codecogs.com/png.latex?q">. This denotes the <em>number of restrictions</em> imposed by the restricted model. Counting restrictions is the same thing as counting equals signs. In a regression of the form <img src="https://latex.codecogs.com/png.latex?Y%20=%20%5Cbeta_0%20+%20%5Cbeta_1%20X%20+%20U"> a restriction of the form <img src="https://latex.codecogs.com/png.latex?%5Cbeta_1%20=%201"> gives <img src="https://latex.codecogs.com/png.latex?q%20=%201"> because there it takes a single equals sign to assert that <img src="https://latex.codecogs.com/png.latex?%5Cbeta_1"> equals one. More complicated regressions allow more complicated kinds of restrictions. For example, in the regression <img src="https://latex.codecogs.com/png.latex?%0AY%20=%20%5Cbeta_0%20+%20%5Cbeta_1%20X_1%20+%20%5Cbeta_2%20X_2%20+%20%5Cbeta_3%20X_3%20+%20U%0A"> we could consider the restriction <img src="https://latex.codecogs.com/png.latex?%5Cbeta_1%20=%20%5Cbeta_2%20=%20%5Cbeta_3%20=%200"> yielding <img src="https://latex.codecogs.com/png.latex?q%20=3">. Alternatively we could consider <img src="https://latex.codecogs.com/png.latex?%5Cbeta_0%20=%20%5Cbeta_2%20=%20%207"> yielding <img src="https://latex.codecogs.com/png.latex?q%20=%202">. We could even consider <img src="https://latex.codecogs.com/png.latex?%5Cbeta_1%20+%20%5Cbeta_2%20=%201"> yielding <img src="https://latex.codecogs.com/png.latex?q%20=%201">. Again: to count the number of restrictions, count the number of equals signs that it requires to express these restrictions.</p>
<p>Now we know how to determine <img src="https://latex.codecogs.com/png.latex?q">, but the question remains: why does it enter the F-test statistic? Above we discussed why <img src="https://latex.codecogs.com/png.latex?SSR_u"> cannot exceed <img src="https://latex.codecogs.com/png.latex?SSR_r"> in a particular dataset. Now we have to think about what happens when sampling uncertainty enters the picture. The sum of squared residuals measures how well a linear regression model fits the <em>observed dataset</em>. Crucially, this is the very same dataset that was used to calculate the regression slope and intercept. In effect, we have used the data twice: first to determine the parameter values that minimize the sum of squared vertical deviations and then to assess how well our regression fits the data, measured by the <em>same</em> vertical deviations. The danger lurking here is a phenomenon called <em>overfitting</em>. We’re not really interested in how well the regression fits <em>this dataset</em>; what we want to know is how well it would help us to predict <em>future observations</em>. In-sample fit, as measured by SSR or related quantities, can be shown to be an <em>over-optimistic</em> measure of out-of-sample fit. This is well-known to machine learning practitioners, who generally use one dataset to fit their models, the training data, and a <em>separate</em> dataset, the test data, to evaluate their predictive performance. In general, the more “flexible” the model, the worse the overfitting problem becomes. Because adding restrictions <em>reduces</em> a model’s flexibility, this creates a challenge for any procedure that compares the in-sample fit of two regressions. Because it is less flexible, we should expect the restricted regression to fit the sample data less well than the unrestricted regression <em>even if the restrictions are true in the population</em>.</p>
<p>To drive this point home, I generated a dataset called <code>sim_data</code> in which <img src="https://latex.codecogs.com/png.latex?Y_i%20=%20%5Calpha%20+%20%5Cepsilon_i"> where <img src="https://latex.codecogs.com/png.latex?%5Cepsilon_i%20%5Csim%20%5Ctext%7BNormal%7D(0,%201)">. I then simulated a large number of regressors <img src="https://latex.codecogs.com/png.latex?(X_%7Bi1%7D,%20X_%7Bi2%7D,%20%5Cdots,%20X_%7Biq%7D)"> <em>completely independently</em> of <img src="https://latex.codecogs.com/png.latex?Y_i">. (For the simulation code, see the appendix below.) In the population from which I simulated my data, none of these regressors contains any information to predict <img src="https://latex.codecogs.com/png.latex?Y">. Nevertheless, if <img src="https://latex.codecogs.com/png.latex?q"> is relatively large compared to the sample size <img src="https://latex.codecogs.com/png.latex?n">, some of these regressors will <em>appear</em> to be correlated with <img src="https://latex.codecogs.com/png.latex?Y"> based on the observed data, purely because of sampling variability. In this example I set <img src="https://latex.codecogs.com/png.latex?n%20=%20100"> and <img src="https://latex.codecogs.com/png.latex?q%20=%2050">. Fitting a restricted regression with only an intercept, and an unrestricted regression that includes all 50 regressors from <code>sim_dat</code> we obtain</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">reg_sim_unrestricted <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> ., sim_dat)</span>
<span id="cb18-2">reg_sim_restricted <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, sim_dat)</span>
<span id="cb18-3">SSR_sim_r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">residuals</span>(reg_sim_restricted)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb18-4">SSR_sim_u <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">residuals</span>(reg_sim_unrestricted)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb18-5">(SSR_sim_r <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> SSR_sim_u) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> SSR_sim_u</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.9144509</code></pre>
</div>
</div>
<p>Even though the restrictions are <em>true</em> in this simulation study, the restricted SSR is 91% larger than the unrestricted SSR purely due to sampling variability. The F-statistic explicitly takes this phenomenon into account via the scaling factor <img src="https://latex.codecogs.com/png.latex?(n%20-%20k%20-%201)%20/%20q">. What matters is not the sample size <em>per se</em>, but the sample size <em>relative</em> to the number of restrictions imposed by the restricted regression.</p>
</section>
<section id="while-were-here-we-might-as-well-carry-out-the-test" class="level2 page-columns page-full" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="while-were-here-we-might-as-well-carry-out-the-test"><span class="header-section-number">4</span> While we’re here we might as well carry out the test!</h2>
<p>Now that we understand why the F-test statistic takes the form that it does, let’s carry out the F-test in each of the two examples from above: <code>midterms</code> and <code>sim_dat</code>. Under the null hypothesis that the constraints imposed by the restricted regression are <em>correct</em>, the F-test statistic follows an <img src="https://latex.codecogs.com/png.latex?F(q,%20n-k-1)"> distribution.<sup>9</sup> For the <code>midterms</code> dataset our test statistic is</p>
<div class="no-row-height column-margin column-container"><div id="fn9"><p><sup>9</sup>&nbsp;Strictly speaking this requires the regression errors to be normally distributed and homoskedastic. If the errors are non-normal but homoskedastic, then the F-statistic is approximately distributed as an <img src="https://latex.codecogs.com/png.latex?F(q,%20%5Cinfty)"> random variable for large <img src="https://latex.codecogs.com/png.latex?n">. Life is much more complicated under heteroskedasticity, but this is a topic for a future post!</p></div></div><div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">F_midterms <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ((SSR_r <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> SSR_u) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> SSR_u) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">71</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb20-2">F_midterms</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 28.40158</code></pre>
</div>
</div>
<p>while the 10%, 5% and 1% critical values for an <img src="https://latex.codecogs.com/png.latex?F(1,%2069)"> distribution are</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1">alpha <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>)</span>
<span id="cb22-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qf</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> alpha, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df1 =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df2 =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">69</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2.779684 3.979807 7.017078</code></pre>
</div>
</div>
<p>The associated p-value is</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pf</span>(F_midterms, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df1 =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df2 =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">69</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1.173624e-06</code></pre>
</div>
</div>
<p>so we resoundingly reject the restriction: first midterm scores <em>clearly do</em> help to predict second midterm scores. The <code>sim_data</code> example gives a very different result. The test statistic in this example is well below any standard critical value, and the p-value is very large:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1">F_sim <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ((SSR_sim_r <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> SSR_sim_u) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> SSR_sim_u) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span></span>
<span id="cb26-2">F_sim</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.8961619</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qf</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> alpha, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df1 =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df2 =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">49</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1.444392 1.604442 1.957803</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pf</span>(F_sim, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df1 =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df2 =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">49</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.6497498</code></pre>
</div>
</div>
<p>In this case we would <em>fail to reject</em> the restrictions. Indeed, the restrictions are <em>true</em>: my simulation generated regressors that are completely independent of <img src="https://latex.codecogs.com/png.latex?Y">!</p>
</section>
<section id="the-bottom-line" class="level2" data-number="5">
<h2 data-number="5" class="anchored" data-anchor-id="the-bottom-line"><span class="header-section-number">5</span> The Bottom Line</h2>
<p>The F-statistic is a product of two factors. The first factor measures how much larger the sum of squared residuals becomes in percentage terms when we impose the restriction. We use a relative comparison to eliminate units from the problem. The second factor accounts for sampling variability. The larger the sample size <img src="https://latex.codecogs.com/png.latex?n"> relative to the number of restrictions <img src="https://latex.codecogs.com/png.latex?q">, the more we “inflate” the value of the first factor. The only thing you need to memorize is the degrees of freedom correction: <img src="https://latex.codecogs.com/png.latex?(n%20-%20k%20-%201)">.</p>
</section>
<section id="appendix-code" class="level2" data-number="6">
<h2 data-number="6" class="anchored" data-anchor-id="appendix-code"><span class="header-section-number">6</span> Appendix: Code</h2>
<p>I used the following code to generate my plot comparing the SSR of the restricted and unrestricted regression models in the midterm exams dataset from above:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb32-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(Midterm2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Midterm1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> midterms, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Unrestricted'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pch =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span>
<span id="cb32-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(unrestricted), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'blue'</span>)</span>
<span id="cb32-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(midterms, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">segments</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x0 =</span> Midterm1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y0 =</span> Midterm2, </span>
<span id="cb32-5">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x1 =</span> Midterm1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fitted</span>(unrestricted),</span>
<span id="cb32-6">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'blue'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb32-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bquote</span>(SSR <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> .(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(SSR_u))))</span>
<span id="cb32-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(Midterm2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Midterm1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> midterms, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Restricted'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pch =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span>
<span id="cb32-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(midterms, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">segments</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x0 =</span> Midterm1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y0 =</span> Midterm2, </span>
<span id="cb32-10">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x1 =</span> Midterm1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fitted</span>(restricted),</span>
<span id="cb32-11">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb32-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">h =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(restricted), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>)</span>
<span id="cb32-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bquote</span>(SSR <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> .(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(SSR_r))))</span>
<span id="cb32-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span></code></pre></div></div>
</div>
<p>and the following code to generate the data contained in <code>sim_data</code></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3817</span>)</span>
<span id="cb33-2">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb33-3">q <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span></span>
<span id="cb33-4">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(n)</span>
<span id="cb33-5">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q), n, q)</span>
<span id="cb33-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(x) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>q)</span>
<span id="cb33-7">sim_dat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(x, y)</span></code></pre></div></div>
</div>


</section>


 ]]></description>
  <category>econometrics</category>
  <category>teaching</category>
  <guid>https://www.econometrics.blog/post/understanding-the-f-statistic/</guid>
  <pubDate>Sun, 15 Aug 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>(Mis)understanding Selection on Observables</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/misunderstanding-selection-on-observables/</link>
  <description><![CDATA[ 




<p>On a recent exam I asked students to extend the logic of <a href="https://expl.ai/BASRRGX">propensity score weighting</a> to handle a treatment that takes on <em>three</em> rather than two values: basically a stripped-down version of <a href="https://academic.oup.com/biomet/article-abstract/87/3/706/293734">Imbens (2000)</a>. Nearly everyone figured this out without much trouble, which is good news! At the same time, I noticed some common misconceptions about the all-important <em>selection-on-observables</em> assumption: <img src="https://latex.codecogs.com/png.latex?%0A%20%20%20%20%5Cmathbb%7BE%7D%5BY_0%7CD,X%5D%20=%20%5Cmathbb%7BE%7D%5BY_0%7CX%5D%20%5Cquad%20%5Ctext%7Band%7D%20%5Cquad%0A%20%20%20%20%5Cmathbb%7BE%7D%5BY_1%7CD,X%5D%20=%20%5Cmathbb%7BE%7D%5BY_1%7CX%5D%0A"> where <img src="https://latex.codecogs.com/png.latex?(Y_0,%20Y_1)"> are the <a href="https://expl.ai/QHUAVRV">potential outcomes</a> corresponding to a binary treatment <img src="https://latex.codecogs.com/png.latex?D"> and <img src="https://latex.codecogs.com/png.latex?X"> is a vector of observed covariates.<sup>1</sup> Since more than a handful of students made the same mistakes, it seemed like a good opportunity for a short post.</p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;For more details see my <a href="https://www.treatment-effects.com/">lecture notes on treatment effects</a></p></div></div><section id="two-misconceptions" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="two-misconceptions"><span class="header-section-number">1</span> Two Misconceptions</h2>
<p>The following two statements about selection on observables are <em>false</em>:</p>
<blockquote class="blockquote">
<ol type="1">
<li>Under selection on observables, if I know the value of someone’s covariate vector <img src="https://latex.codecogs.com/png.latex?X">, then learning her treatment status <img src="https://latex.codecogs.com/png.latex?D"> provides no additional information about the average value of her observed outcome <img src="https://latex.codecogs.com/png.latex?Y">.</li>
<li>Selection on observables requires the treatment <img src="https://latex.codecogs.com/png.latex?D"> and potential outcomes <img src="https://latex.codecogs.com/png.latex?(Y_0,Y_1)"> to be conditionally independent given covariates <img src="https://latex.codecogs.com/png.latex?X">.</li>
</ol>
</blockquote>
<p>If you’ve studied treatment effects, pause for a moment and see if you can figure out what’s wrong with each of them before reading further.</p>
</section>
<section id="the-first-misconception" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="the-first-misconception"><span class="header-section-number">2</span> The First Misconception</h2>
<p>The first statement:</p>
<blockquote class="blockquote">
<p>Under selection on observables, if I know the value of someone’s covariate vector <img src="https://latex.codecogs.com/png.latex?X">, then learning her treatment status <img src="https://latex.codecogs.com/png.latex?D"> provides no additional information about the average value of her observed outcome <img src="https://latex.codecogs.com/png.latex?Y">.</p>
</blockquote>
<p>is a verbal description of the following conditional mean independence condition: <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BE%7D%5BY%7CX,D%5D%20=%20%5Cmathbb%7BE%7D%5BY%7CX%5D.%0A"> So what’s wrong with this equality? The potential outcomes <img src="https://latex.codecogs.com/png.latex?(Y_0,%20Y_1)"> and the observed outcome <img src="https://latex.codecogs.com/png.latex?Y"> are related according to <img src="https://latex.codecogs.com/png.latex?%0AY%20=%20Y_0%20+%20D%20(Y_1%20-%20Y_0).%0A"> Taking conditional expectations of both sides and using the selection on observables assumption <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cmathbb%7BE%7D%5BY%7CX,D%5D%20&amp;=%20%5Cmathbb%7BE%7D%5BY_0%7CX,D%5D%20+%20D%20%5Cmathbb%7BE%7D%5BY_1%20-%20Y_0%7CD,X%5D%5C%5C%0A&amp;=%20%5Cmathbb%7BE%7D%5BY_0%7CX%5D%20+%20D%20%5Cmathbb%7BE%7D%5BY_1%20-%20Y_0%7CX%5D.%0A%5Cend%7Baligned%7D%0A"> In contrast, conditioning on <img src="https://latex.codecogs.com/png.latex?X"> alone gives <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cmathbb%7BE%7D%5BY%7CX%5D%20&amp;=%20%5Cmathbb%7BE%7D%5BY_0%7CX%5D%20+%20%5Cmathbb%7BE%7D%5BD(Y_1%20-%20Y_0)%7CX%5D%5C%5C%0A&amp;=%20%5Cmathbb%7BE%7D%5BY_0%7CX%5D%20+%20%5Cmathbb%7BE%7D_%7BD%7CX%7D%5BD%5Cmathbb%7BE%7D(Y_1%20-%20Y_0%7CD,X)%5D%5C%5C%0A&amp;=%20%5Cmathbb%7BE%7D%5BY_0%7CX%5D%20+%20%5Cmathbb%7BE%7D_%7BD%7CX%7D%5BD%5Cmathbb%7BE%7D(Y_1%20-%20Y_0%7CX)%5D%5C%5C%0A&amp;=%20%5Cmathbb%7BE%7D%5BY_0%7CX%5D%20+%20%5Cmathbb%7BE%7D(D%7CX)%20%5Ccdot%20%5Cmathbb%7BE%7D(Y_1%20-%20Y_0%7CX)%0A%5Cend%7Baligned%7D%0A"> by iterated expectations and the selection on observables assumption, since <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y_1%20-%20Y_0%7CX)"> is a measurable function of <img src="https://latex.codecogs.com/png.latex?X">. Subtracting these expressions, we find that <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BE%7D(Y%7CX,D)%20-%20%5Cmathbb%7BE%7D(Y%7CX)%20=%20%5Cleft%5B%20D%20-%20%5Cmathbb%7BE%7D(D%7CX)%20%5Cright%5D%20%5Ccdot%20%5Cmathbb%7BE%7D(Y_1%20-%20Y_0%7CX)%0A"> so that <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y%7CX,D)%20=%20%5Cmathbb%7BE%7D(Y%7CX)"> if and only if the RHS equals zero.</p>
<p>So how could the RHS equal zero? One way is if <img src="https://latex.codecogs.com/png.latex?D%20=%20%5Cmathbb%7BE%7D(D%7CX)">. Since <img src="https://latex.codecogs.com/png.latex?D"> is a binary random variable, this would require <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(D%7CX)"> to be a binary random variable as well. But notice that <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(D%7CX)%20=%20%5Cmathbb%7BP%7D(D=1%7CX)"> is simply the propensity score <img src="https://latex.codecogs.com/png.latex?p(X)">. Because <img src="https://latex.codecogs.com/png.latex?X"> is a random variable, so is <img src="https://latex.codecogs.com/png.latex?p(X)">. But <img src="https://latex.codecogs.com/png.latex?p(X)"> <em>cannot</em> take on the values zero or one. If it did, this would violate the <em>overlap assumption</em>: <img src="https://latex.codecogs.com/png.latex?0%20%3C%20p(X)%20%3C%201">.</p>
<p>So we can’t have <img src="https://latex.codecogs.com/png.latex?D%20=%20%5Cmathbb%7BE%7D(D%7CX)">, but what about <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y_1%20-%20Y_0%7CX)=0">? Since <img src="https://latex.codecogs.com/png.latex?(Y_1%20-%20Y_0)"> is the treatment effect of <img src="https://latex.codecogs.com/png.latex?D">, it follows that <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y_1%20-%20Y_0%7CX)"> is the conditional average treatment effect <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BATE%7D(X)"> given <img src="https://latex.codecogs.com/png.latex?X">. It’s not a contradiction for <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BATE%7D(X)"> to equal zero, but think about what it would mean: it would require that the average treatment effect for a person with covariates <img src="https://latex.codecogs.com/png.latex?(X%20=%20x)"> is exactly zero <em>regardless</em> of <img src="https://latex.codecogs.com/png.latex?x">. Moreover, by iterated expectations it would imply that <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BATE%7D%20=%20%5Cmathbb%7BE%7D(Y_1%20-%20Y_0)%20=%20%5Cmathbb%7BE%7D_X%5B%5Cmathbb%7BE%7D(Y_1%20-%20Y_0%7C%0AX)%5D%20=%20%5Cmathbb%7BE%7D%5B%5Ctext%7BATE%7D(X)%5D%20=%200%0A"> so the average treatment effect would also be zero. Again, this is not a contradiction but it would definitely be odd to assume that the treatment effect is zero before you even try to estimate it!</p>
<p>To summarize: the first statement above cannot be an implication of selection on observables because it would either require a violation of the overlap assumption, or imply that there is no treatment effect whatsoever. To correct the statement, we simply need to change the last three words:</p>
<blockquote class="blockquote">
<p>Under selection on observables, if I know the value of someone’s covariate vector <img src="https://latex.codecogs.com/png.latex?X">, then learning her treatment status <img src="https://latex.codecogs.com/png.latex?D"> provides no additional information about the average values of her <em>potential outcomes</em> <img src="https://latex.codecogs.com/png.latex?(Y_0,%20Y_1)">.</p>
</blockquote>
<p>This is a correct verbal statement of the mean exclusion restriction <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y_0%7CD,X)%20=%20%5Cmathbb%7BE%7D(Y_0%7CX)"> and <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y_1%7CD,X)%20=%20%5Cmathbb%7BE%7D(Y_1%7CX)">.</p>
</section>
<section id="the-second-misconception" class="level2 page-columns page-full" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="the-second-misconception"><span class="header-section-number">3</span> The Second Misconception</h2>
<p>And this leads nicely to the second misconception:</p>
<blockquote class="blockquote">
<p>Selection on observables requires the treatment <img src="https://latex.codecogs.com/png.latex?D"> and potential outcomes <img src="https://latex.codecogs.com/png.latex?(Y_0,Y_1)"> to be conditionally independent given covariates <img src="https://latex.codecogs.com/png.latex?X">.</p>
</blockquote>
<p>To see why this is false, consider an example in which <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0AY%20&amp;=%20(1%20-%20D)%20%5Ccdot%20(%5Calpha_0%20+%20X'%5Cbeta_0%20+%20U_0)%20+%20D%20%5Ccdot%20(%5Calpha_1%20+%20X'%20%5Cbeta_1%20+%20U_1)%5C%5C%0AU_0%7C(D,X)%20&amp;%5Csim%20%5Ctext%7BNormal%7D(0,1%20-%20D/2)%5C%5C%0AU_1%7C(D,X)%20&amp;%5Csim%20%5Ctext%7BNormal%7D(0,1%20+%20D).%0A%5Cend%7Baligned%7D%0A"> Notice that the distributions of <img src="https://latex.codecogs.com/png.latex?U_0"> and <img src="https://latex.codecogs.com/png.latex?U_1"> given <img src="https://latex.codecogs.com/png.latex?(D,X)"> <em>depend on</em> <img src="https://latex.codecogs.com/png.latex?D">. Now, by iterated expectations, <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cmathbb%7BE%7D(U_0%7CX)%20&amp;=%20%5Cmathbb%7BE%7D_%7B(D%7CX)%7D%5B%5Cmathbb%7BE%7D(U_0%7CD,X)%5D%20=%200%5C%5C%0A%5Cmathbb%7BE%7D(U_0)%20&amp;=%20%5Cmathbb%7BE%7D_%7BX%7D%5B%5Cmathbb%7BE%7D(U_0%7CX)%5D%20=%20%200%0A%5Cend%7Baligned%7D%0A"> and similarly <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(U_1%7CX)%20=%20%5Cmathbb%7BE%7D(U_1)=0">. Substituting <img src="https://latex.codecogs.com/png.latex?D=0"> and <img src="https://latex.codecogs.com/png.latex?D=1">, we can calculate the potential outcomes and average treatment effect as follows <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0AY_0%20&amp;=%20%5Calpha_0%20+%20X'%5Cbeta_0%20+%20U_0%20%5C%5C%0AY_1%20&amp;=%20%5Calpha_1%20+%20X'%5Cbeta_1%20+%20U_1%20%5C%5C%0A%5Ctext%7BATE%7D%20&amp;=%20%5Cmathbb%7BE%7D(Y_1%20-%20Y_0)%20=%20(%5Calpha_1%20-%20%5Calpha_0)%20+%20%5Cmathbb%7BE%7D%5BX'%5D(%5Cbeta_1%20-%20%5Cbeta_0).%0A%5Cend%7Baligned%7D%0A"> It follows that <img src="https://latex.codecogs.com/png.latex?D"> is <em>not</em> conditionally independent of <img src="https://latex.codecogs.com/png.latex?(Y_0,%20Y_1)"> given <img src="https://latex.codecogs.com/png.latex?X">. In particular, the <em>variance</em> of the potential outcomes depends on <img src="https://latex.codecogs.com/png.latex?D"> even after conditioning on <img src="https://latex.codecogs.com/png.latex?X">: <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Ctext%7BVar%7D(Y_0%7CX,D)%20&amp;=%20%5Ctext%7BVar%7D(U_0%7CX,D)%20=%201%20-%20D/2%5C%5C%0A%5Ctext%7BVar%7D(Y_1%7CX,D)%20&amp;=%20%5Ctext%7BVar%7D(U_1%7CX,D)%20=%201%20+%20D.%0A%5Cend%7Baligned%7D%0A"> In spite of this, the selection on observables assumption still holds: <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cmathbb%7BE%7D(Y_0%7CD,X)%20&amp;=%20%5Calpha_0%20+%20X'%5Cbeta_0%20+%20%5Cmathbb%7BE%7D(U_0%7CD,X)%20=%20%5Calpha_0%20+%20X'%5Cbeta_0%5C%5C%0A%5Cmathbb%7BE%7D(Y_0%7CX)%20&amp;=%20%5Calpha_0%20+%20X'%5Cbeta_0%20+%20%5Cmathbb%7BE%7D(U_0%7CX)%20=%20%5Calpha_0%20+%20X'%5Cbeta_0%5C%5C%0A%5Cend%7Baligned%7D%0A"> and similarly <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(Y_1%7CD,X)%20=%20%5Cmathbb%7BE%7D(Y_1%7CX)%20=%20%5Calpha_1%20+%20X'%5Cbeta_1">. While this example is admittedly a bit peculiar, the point is more general: because the average treatment effect is an <em>expectation</em>, identifying it only requires assumptions about <em>conditional means</em>.<sup>2</sup> The second statement is even easier to correct than the first: we need only add a single word:</p>
<div class="no-row-height column-margin column-container"><div id="fn2"><p><sup>2</sup>&nbsp;You might object that in the real world it is difficult to think of settings in which conditional mean independence is plausible but full independence does not. This is a fair point. Nevertheless, it’s important to be clear about which assumptions are actually <em>used</em> in a given derivation, and here we only rely on conditional mean independence.</p></div></div><blockquote class="blockquote">
<p>Selection on observables requires the treatment <img src="https://latex.codecogs.com/png.latex?D"> and potential outcomes <img src="https://latex.codecogs.com/png.latex?(Y_0,Y_1)"> to be conditionally <em>mean</em> independent given covariates <img src="https://latex.codecogs.com/png.latex?X">.</p>
</blockquote>
<p>Conditional independence implies conditional mean independence, but the converse is false.</p>
</section>
<section id="epilogue" class="level2" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="epilogue"><span class="header-section-number">4</span> Epilogue</h2>
<p>So what’s the moral here? First, it’s crucial to distinguish between the <em>observed</em> outcome <img src="https://latex.codecogs.com/png.latex?Y"> and the <em>potential outcomes</em> <img src="https://latex.codecogs.com/png.latex?(Y_0,%20Y_1)">. Second, the various notions of “unrelatedness” between random variables—independence, conditional mean independence, and uncorrelatedness—can be confusing. Be sure to pay attention to exactly which condition is used and why. In a future post, I’ll have more to say about the relationships between these notions.</p>


</section>


 ]]></description>
  <category>causal inference</category>
  <guid>https://www.econometrics.blog/post/misunderstanding-selection-on-observables/</guid>
  <pubDate>Sun, 08 Aug 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Untangling Cause and Effect Without Experiments</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/untangling-cause-and-effect-without-experiments/</link>
  <description><![CDATA[ 




<p><em>The following is a piece I wrote for the <a href="https://www.lmh.ox.ac.uk/sites/default/files/documents/2021-05/LMH%20News%202021%20Issue%204_0.pdf">LMH News</a>, based on a <a href="https://youtu.be/NeAkMcgdWxA">general interest webinar</a> that I gave in November of 2020. If this post inspires you to learn more about causal inference, you may enjoy browsing my <a href="https://treatment-effects.com">teaching materials on treatment effects</a>.</em></p>
<hr>
<p>Will earning a PPE degree from Oxford increase your lifetime earnings? Does eating bacon sandwiches cause cancer? Does watching Fox News make you vote Republican? Will owning a dog increase your lifespan? Each of these questions concerns the causal effect of a treatment on an outcome. In social science, a “treatment” is any factor whose causal effect we hope to learn. As far as I know, there has never been an experiment that compelled people to study a particular subject at university, watch Fox News, or own a dog: nonetheless, papers have been written and published that use data to estimate the causal effects of each of these treatments. Datasets in which the treatment of interest is “naturally occurring” rather than randomly assigned as part of an experiment are called observational. Many of the most interesting and important treatments in social science cannot be randomly assigned. Social scientists have therefore developed a set of tools for studying treatment effects using observational data. By introducing you to some of these tools and briefly summarising the ways in which researchers have used them, I’ll shed some light on that age-old question: how much is your education worth?</p>
<p>Alice read PPE at Oxford and currently earns £75,000 a year. Would she have earned as much if she had studied at Oxford Brookes instead? The fundamental problem of causal inference is that we can never observe a person’s counterfactual outcome. In other words, we can never know what her outcome would have been if her treatment had been different. A counterfactual is fundamentally a “within-person” comparison, asking us to imagine two parallel universes, one in which Alice attends Oxford and another in which she attends Brookes. The causal question of interest is how much the Alice in our world earns compared to the Alice who resides through the looking glass. Of course, this comparison can never be more than a thought experiment. To learn about treatment effects in the real world, we develop methods and assumptions that allow us to substitute the idealized within-person comparison with a between-person comparison.</p>
<p>According to recent data from Department for Education, UCAS and the ONS, the median salary of Oxford graduates is nearly £15,000 higher than that of Brookes graduates.<sup>1</sup> Does this mean that the treatment effect of attending Oxford rather than Brookes is £15,000 a year? Almost certainly not! This is not an apples-to-apples comparison. One of the crucial differences between the two universities is entry requirements: Oxford requires A*AA for Economics and Management applicants, whereas Brookes asks for BCC for a similar degree. Oxford students on average have higher levels of academic preparation and ability upon entering university: accordingly, it’s possible that attending Oxford has no causal effect on wage, but earning high grades at A level does. In statistical parlance, we would say that ability <em>confounds</em> the relationship between university attended and wage.</p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;<a href="https://www.economist.com/britain/2017/08/12/which-british-universities-do-most-to-boost-graduate-salaries" class="uri">https://www.economist.com/britain/2017/08/12/which-british-universities-do-most-to-boost-graduate-salaries</a></p></div><div id="fn2"><p><sup>2</sup>&nbsp;<a href="https://www.ifs.org.uk/publications/13036" class="uri">https://www.ifs.org.uk/publications/13036</a></p></div></div><p>So how can we solve the problem of confounding in observational datasets? One approach is matching, which compares treated and untreated people with the <em>same</em> values of any confounders. For example, we might compare Oxford Economics students with three A-stars at A-level to Brookes Economics students with the same A level results. Repeating this for every combination of subject and A-levels and averaging the results gives an estimate of the overall causal effect of attending Oxford. A recent report from the IFS used a closely related approach to estimate the relative returns to different undergraduate degrees in the UK.<sup>2</sup> Their findings suggest that confounding is a very serious problem when comparing raw wages of students across universities. For example, women who graduate from LSE earn over 70% more than the average female graduate. After adjusting for differences in student characteristics, however, this wage premium falls dramatically: female graduates of LSE earn only a little over 35% more than similar women who attended different universities. The same story applies to other elite UK institutions such as Oxford, Cambridge, and UCL.</p>
<p>For matching methods to be effective, we need to observe all important confounders. In some settings this is a reasonable assumption, but in others it clearly isn’t. For this reason, researchers have developed a number of techniques to address the problem of unobserved confounding. Much of my own research focuses on the use of so-called “instrumental variables.” An instrumental variable, or instrument for short, is something that affects the treatment of interest but is unrelated to any unobserved confounders. To understand this idea, we’ll examine one of the most famous papers to use the instrumental variables approach: a 1991 article by Josh Angrist and Alan Krueger studying the impact of compulsory school attendance on later-life earnings.<sup>3</sup> The paper begins with a striking observation: in the US, people born in the first quarter of the year tend to complete fewer years of education. Why might this be the case? According to Angrist and Krueger: “children born in different months of the year start school at different ages, while compulsory schooling laws generally require students to remain in school until their sixteenth or seventeenth birthday. In effect, the interaction of school-entry requirements and compulsory schooling laws compels students born in certain months to attend school longer than students born in other months.”</p>
<div class="no-row-height column-margin column-container"><div id="fn3"><p><sup>3</sup>&nbsp;<a href="https://ideas.repec.org/a/oup/qjecon/v106y1991i4p979-1014..html" class="uri">https://ideas.repec.org/a/oup/qjecon/v106y1991i4p979-1014..html</a></p></div></div><p>Angrist and Krueger use quarter of birth as an instrumental variable to estimate the causal effect of schooling on wage. Quarter of birth is indeed related to the treatment of interest, years of schooling. But there are many unobserved factors that influence both how many years of education a person attains, and her later-life outcomes: demographics, family background etc. Is quarter of birth unrelated to these? Angrist and Krueger argue in the affirmative: “one’s birthday is unlikely to be correlated with personal attributes other than age at school entry.” If this is correct, then we can estimate the causal effect of education on wages as follows. First we calculate the difference of wages between men born in the first quarter and those born in the rest of the year. Those born in the first quarter earn less on average, so this difference is negative. Next we calculate the corresponding difference in years of education for these two groups. Those born in the first quarter have fewer years of education on average, so this difference is also negative. The <em>ratio</em> of the two differences tells us the <em>fraction</em> of the observed difference in wages that is caused by differences in education. Since both differences are negative, the ratio is positive. Angrist and Krueger find that an extra year of education causes between a 5% and 15% increase in wages.</p>
<p>But is it really true that a person’s birthday is uncorrelated with “personal attributes other than age at school entry?” About seven years ago, Buckles and Hungerman revisited this question, examining US data that includes information on both birth dates and family background<sup>4</sup>. In the years since Angrist and Krueger published their original paper, there have been more than 20 other published papers using season of birth as an instrumental variable. Across these studies, US children born in the first quarter—or more generally in the winter months—earn less, pursue less education, and have lower measured intelligence on average compared to those born in other parts of the year. At the same time, researchers have found a correlation between season of birth and schizophrenia, autism, dyslexia, extreme shyness, and even suicide risk.</p>
<div class="no-row-height column-margin column-container"><div id="fn4"><p><sup>4</sup>&nbsp;<a href="https://ideas.repec.org/a/tpr/restat/v95y2013i3p711-724.html" class="uri">https://ideas.repec.org/a/tpr/restat/v95y2013i3p711-724.html</a></p></div></div><p>What’s going on here? Buckles and Hungerman propose a simple explanation: “children born in different seasons are not initially similar but rather are conceived by different groups of women.” Mothers who give birth in the winter months are disproportionately likely to be teenagers. They are also less educated, and less likely to be married. Buckles and Hungerman conclude that: “The well-known relationship between season of birth and later outcomes is largely driven by differences in fertility patterns across socioeconomic groups, and not merely natural phenomena or schooling laws that intervene after conception.” In other words, quarter of birth is <em>indeed</em> related to confounders that were unobserved by Angrist and Krueger in their original paper.</p>
<p>So where does all of this leave us? Untangling cause and effect is extremely challenging, and <em>always</em> relies upon assumptions. Social scientists have a powerful toolbox for studying treatment effects in settings where randomized experimentation is impossible, impractical, or unethical. But like any tools, matching, instrumental variables, and related methods depend for their success on the care with which they are used. We can indeed learn about cause-and-effect from observational data, but doing so requires knowledge of the problem we’re studying, a willingness to question our assumptions, and some good old-fashioned intellectual humility.</p>




 ]]></description>
  <category>causal inference</category>
  <category>teaching</category>
  <guid>https://www.econometrics.blog/post/untangling-cause-and-effect-without-experiments/</guid>
  <pubDate>Sun, 01 Aug 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Thirty isn’t the magic number</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/thirty-isn-t-the-magic-number/</link>
  <description><![CDATA[ 




<p>The simplest version of the central limit theorem (CLT) says that if <img src="https://latex.codecogs.com/png.latex?X_1,%20%5Cdots,%20X_n"> are iid random variables with mean <img src="https://latex.codecogs.com/png.latex?%5Cmu"> and finite variance <img src="https://latex.codecogs.com/png.latex?%5Csigma%5E2"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7B%5Cbar%7BX%7D_n%20-%20%5Cmu%7D%7B%5Csigma/%5Csqrt%7Bn%7D%7D%20%5Crightarrow_d%20N(0,1)%0A"> where <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D_n%20=%20%5Cfrac%7B1%7D%7Bn%7D%20%5Csum_%7Bi=1%7D%5En%20X_i">. In other words, if <img src="https://latex.codecogs.com/png.latex?n"> is sufficiently large, the sample mean is <em>approximately</em> normally distributed with mean <img src="https://latex.codecogs.com/png.latex?%5Cmu"> and variance <img src="https://latex.codecogs.com/png.latex?%5Csigma%5E2/n">, regardless of the distribution of <img src="https://latex.codecogs.com/png.latex?X_1,%20%5Cdots,%20X_n">. This is a pretty impressive result! It is so impressive, in fact, that students encountering it for the first time are usually a little wary. I’m typically asked “but how large is <em>sufficiently large</em>?” or “how do we know when the CLT will provide a good approximation?” My answer is disappointing: without some additional information about the distribution from which <img src="https://latex.codecogs.com/png.latex?X_1,%20%5Cdots,%20X_n"> were drawn, we simply <em>can’t say</em> how large a sample is large enough for the CLT to work well. At this point, someone invariably volunteers “but in my high school statistics course, we learned that <img src="https://latex.codecogs.com/png.latex?n%20=%2030"> is big enough for the CLT to hold!”</p>
<p>I’ve always been surprised by the prevalence of the <img src="https://latex.codecogs.com/png.latex?n%20%5Cgeq%2030"> dictum. It even appears in Charles Wheelan’s <em>Naked Statistics</em>, an otherwise excellent book that I assign as summer reading for our incoming economics undergraduates: “as a rule of thumb, the sample size must be at least 30 for the central limit theorem to hold true.” In this post I’d like to set the record straight: <img src="https://latex.codecogs.com/png.latex?n%5Cgeq%2030"> is neither necessary <em>nor</em> sufficient for the CLT to provide a good approximation, as we’ll see by examining two simple examples. Along the way, we’ll learn about two useful tools for visualizing and comparing distributions: the empirical cdf, and quantile-quantile plots.</p>
<section id="a-sample-size-of-thirty-isnt-necessary." class="level2 page-columns page-full" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="a-sample-size-of-thirty-isnt-necessary."><span class="header-section-number">1</span> A sample size of thirty isn’t necessary.</h2>
<p>We’ll start by showing that the CLT can work extremely well even when <img src="https://latex.codecogs.com/png.latex?n"> is much smaller than <img src="https://latex.codecogs.com/png.latex?30"> and the random variables that we average are far from normally distributed themselves. Along the way we’ll learn about the <em>empirical CDF</em> and <em>quantile-quantile</em> plots, two extremely useful tools for comparing probability distributions.</p>
<p>Informally speaking, a Uniform<img src="https://latex.codecogs.com/png.latex?(0,1)"> random variable is equally likely to take on any continuous value in the range <img src="https://latex.codecogs.com/png.latex?%5B0,1%5D">.<sup>1</sup> Here’s a histogram of 1000 random draws from this distribution:</p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;More formally, <img src="https://latex.codecogs.com/png.latex?U%5Csim"> Uniform<img src="https://latex.codecogs.com/png.latex?(0,1)"> if and only if <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(a%20%5Cleq%20U%20%5Cleq%20b)%20=%20(b%20-%20a)"> for any <img src="https://latex.codecogs.com/png.latex?0%20%5Cleq%20a%20%5Cleq%20b%20%5Cleq%201">. </p></div></div><div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># set the seed to get the same draws I did</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12345</span>) </span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hist</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">freq =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, </span>
<span id="cb1-4">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Histogram of 1000 Uniform(0,1) Draws'</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/thirty-isn-t-the-magic-number/index_files/figure-html/unnamed-chunk-2-1.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>This distribution clearly isn’t normal! Indeed, its probability density function is <img src="https://latex.codecogs.com/png.latex?f(x)%20=%201"> for <img src="https://latex.codecogs.com/png.latex?x%20%5Cin%20%5B0,1%5D">. This is a flat line rather than a bell curve. But if we <em>average</em> even a relatively small number of Uniform<img src="https://latex.codecogs.com/png.latex?(0,1)"> draws, the result will be <em>extremely close</em> to normality. To see that this is true, we’ll carry out a simulation in which we draw <img src="https://latex.codecogs.com/png.latex?n"> Uniform<img src="https://latex.codecogs.com/png.latex?(0,1)"> RVs, calculate their sample mean, and store the result. Repeating this a large number of times allows us to approximate the sampling distribution of <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D_n">. I’ll start by writing a function <code>get_unif_sim</code> that takes a single argument <code>n</code>. This function returns the sample mean of <code>n</code> Uniform<img src="https://latex.codecogs.com/png.latex?(0,1)"> draws:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">get_unif_sim <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(n) {</span>
<span id="cb2-2">  sims <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(n)</span>
<span id="cb2-3">  xbar <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(sims)</span>
<span id="cb2-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(xbar)</span>
<span id="cb2-5">}</span></code></pre></div></div>
</div>
<p>Next I’ll use the <code>replicate</code> function to call <code>get_unif_sim</code> a large number of times, <code>nreps</code>, and store the results as a vector called <code>xbar_sims</code>. Here I’ll take <img src="https://latex.codecogs.com/png.latex?n%20=%2010"> standard uniform draws, blatantly violating the <img src="https://latex.codecogs.com/png.latex?n%20%5Cgeq%2030"> rule-of-thumb:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12345</span>)</span>
<span id="cb3-2">nreps <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e5</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># scientific notation for 100,000 </span></span>
<span id="cb3-3">xbar_sims <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replicate</span>(nreps, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_unif_sim</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hist</span>(xbar_sims, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">freq =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, </span>
<span id="cb3-5">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Sampling Dist. of Sample Mean of 10 Uniform(0,1) Draws'</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/thirty-isn-t-the-magic-number/index_files/figure-html/unnamed-chunk-4-1.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>A beautiful bell curve! This certainly looks normal, but histograms can be tricky to interpret. Their shape depends on how many bins we use to make the plot, something that can be difficult to choose well in practice. In the following two sections, we’ll instead compare <em>distribution functions</em> and <em>quantiles</em>.</p>
<section id="the-empirical-cdf" class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="the-empirical-cdf">The Empirical CDF</h3>
<!--To find out what the CLT implies in this example, we first need to know the mean and variance of $X_i$. If $X \sim U(0,1)$, then
$$
\mathbb{E}[X] = \int_{-\infty}^{\infty} x f(x)\, dx = \int_0^1 x \cdot 1 \,dx = \left. \frac{x^2}{2} \right|_0^1 = \frac{1}{2}
$$
which makes perfect sense given the symmetry of the Uniform$(0,1)$ density about $x = 1/2$. The quickest way to calculate $\text{Var}(X)$ is to begin by calculating $\mathbb{E}[X^2]$ 
$$
\mathbb{E}[X^2] = \int_{0}^{1} x^2\, dx = \left. \frac{x^3}{3} \right|_0^1 = \frac{1}{3}
$$
and then use the "shortcut rule" as follows:
$$
\text{Var}(X) = \mathbb{E}[X^2] - \mathbb{E}[X]^2 = \frac{1}{3} - \left(\frac{1}{2}\right)^2 = \frac{1}{12}.
$$-->
<p>If <img src="https://latex.codecogs.com/png.latex?X%20%5Csim"> Uniform<img src="https://latex.codecogs.com/png.latex?(0,1)">, then <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D(X)%20=%201/2"> and <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVar%7D(X)%20=%201/12">, which follows from <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5BX%5E2%5D=1/3"> and the definition of variance.<sup>2</sup> For <img src="https://latex.codecogs.com/png.latex?n%20=%2010">, <img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7B%5Csigma%7D%7B%5Csqrt%7Bn%7D%7D%20=%20%5Cfrac%7B1%7D%7B%5Csqrt%7B12%7D%7D%20%5Ccdot%20%5Cfrac%7B1%7D%7B%5Csqrt%7B10%7D%7D%20=%20%5Cfrac%7B1%7D%7B%5Csqrt%7B120%7D%7D%0A"> so if the CLT provides a good approximation in this example, we should find that <img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7B%5Cbar%7BX%7D_n%20-%201/2%7D%7B1/%5Csqrt%7B120%7D%7D%20=%20%5Csqrt%7B120%7D%20(%5Cbar%7BX%7D_n%20-%201/2)%20%5Capprox%20N(0,1)%0A"> in the sense that that the <em>cumulative distribution function</em> (CDF) of <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7B120%7D%20(%5Cbar%7BX%7D_n%20-%201/2)">, call it <img src="https://latex.codecogs.com/png.latex?F">, is approximately equal to the standard normal CDF <code>pnorm()</code>. An obvious way to see if this holds is to plot <img src="https://latex.codecogs.com/png.latex?F"> against <code>pnorm()</code> and see how they compare. From now on, we’ll be working with the z-scores of <code>xbar_sims</code> rather than the raw simulation values themselves, so we’ll start by constructing them, subtracting the population mean and dividing by the population standard deviation:</p>
<div class="no-row-height column-margin column-container"><div id="fn2"><p><sup>2</sup>&nbsp;If you’re taking introductory probability and statistics, filling in the missing details for these calculations would be an excellent homework problem!</p></div></div><div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (xbar_sims <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>))</span></code></pre></div></div>
</div>
<p>We haven’t worked out an expression for the function <img src="https://latex.codecogs.com/png.latex?F">, but we can approximate it using our simulation draws <code>xbar_sims</code>. We do this by calculating the <em>empirical CDF</em> of our centered and standardized simulation draws <code>z</code>. Recall that if <img src="https://latex.codecogs.com/png.latex?Z"> is a random variable, its CDF <img src="https://latex.codecogs.com/png.latex?F"> is defined as <img src="https://latex.codecogs.com/png.latex?F(t)%20=%20%5Cmathbb%7BP%7D(Z%20%5Cleq%20t)">. Given a large number of observed random draws <img src="https://latex.codecogs.com/png.latex?z_1,%20%5Cdots,%20z_J"> from the distribution of <img src="https://latex.codecogs.com/png.latex?Z">, we can approximate <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D(Z%20%5Cleq%20t)"> by calculating the fraction of observed draws less than or equal to <img src="https://latex.codecogs.com/png.latex?t">. In other words <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BP%7D(Z%20%5Cleq%20t)%20%5Capprox%20%5Cfrac%7B1%7D%7BJ%7D%5Csum_%7Bj=1%7D%5EJ%20%5Cmathbf%7B1%7D%5C%7Bz_j%20%5Cleq%20t%5C%7D%0A"> where <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7B1%7D%5C%7Bz_j%20%5Cleq%20t%20%5C%7D"> is the <em>indicator function</em>: it equals one if <img src="https://latex.codecogs.com/png.latex?z_j"> is less than or equal to the threshold <img src="https://latex.codecogs.com/png.latex?t"> and zero otherwise. The sample average on the right-hand side of the preceding expression is called the <em>empirical CDF</em>. It uses empirical data–in this case our simulation draws <img src="https://latex.codecogs.com/png.latex?z_j">–to approximate the unknown CDF. By increasing the number of random draws <img src="https://latex.codecogs.com/png.latex?J"> that we use, we can make this approximation as accurate as we like.<sup>3</sup> For example, we don’t know the exact value of <img src="https://latex.codecogs.com/png.latex?F(0)">, the probability that <img src="https://latex.codecogs.com/png.latex?Z%20%5Cleq%200">. But using our simulated values <code>z</code> from above, we can approximate it as</p>
<div class="no-row-height column-margin column-container"><div id="fn3"><p><sup>3</sup>&nbsp;Here we take <img src="https://latex.codecogs.com/png.latex?J%20=%20100,000"> which is more than enough for the purposes of this exercise.</p></div></div><div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(z <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.49993</code></pre>
</div>
</div>
<p>and if we wanted the probability that <img src="https://latex.codecogs.com/png.latex?Z%20%5Cleq%202">, we could approximate this as</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(z <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.97797</code></pre>
</div>
</div>
<p>So far so good: these values agree with <code>pnorm(0)</code>, which equals 0.5, and <code>pnorm(2)</code>, which is approximately 0.9772. But we’ve only looked at two values of <img src="https://latex.codecogs.com/png.latex?t">. While we could continue trying additional values one at a time, it’s much faster to use R’s built-in function for computing an empirical cdf, <code>ecdf()</code>. First we pass our simulated z-scores <code>z</code> into <code>ecdf()</code> function to calculate the empirical CDF and plot the result. Next we overlay some points from the standard normal CDF, <code>pnorm</code> in blue for comparison:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (xbar_sims <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb9-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ecdf</span>(z), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'t'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'F(t)'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'F(t) versus pnorm(t)'</span>)</span>
<span id="cb9-3">tseq <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>)</span>
<span id="cb9-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">points</span>(tseq, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pnorm</span>(tseq), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'blue'</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/thirty-isn-t-the-magic-number/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The fit is almost perfect, despite <img src="https://latex.codecogs.com/png.latex?n=10"> being far below 30. This kind of plot is much more informative than the histogram from above, but it can still be a bit difficult to read. When constructing confidence intervals or calculating p-values it is probabilities in the tails of the distribution that matter most, i.e.&nbsp;values of <img src="https://latex.codecogs.com/png.latex?t"> that are far from zero in the plot. Ideally, we’d like a plot that makes any discrepancies in the tails <em>jump out</em> at us. That is precisely what we’ll construct next.</p>
</section>
<section id="quantile-quantile-plots" class="level3 page-columns page-full">
<h3 class="anchored" data-anchor-id="quantile-quantile-plots">Quantile-Quantile Plots</h3>
<p>So far we’ve seen that the histogram of <code>xbar_sims</code> is bell-shaped, and that the empirical CDF of <code>sqrt(120) * (xbar_sims - 0.5)</code> is well-approximated by the standard normal CDF <code>pnorm()</code>. If you’re still not convinced that the CLT <em>can</em> work perfectly well with <img src="https://latex.codecogs.com/png.latex?n%20=%2010">, the final plot that we’ll make should dispel any remaining doubts. <!--If $Z$ is a continuous random variable with CDF $F$, its *quantile function* is given by $Q(p) = F^{-1}(p)$. In words $Q(p)$ is the threshold $t$ such that $\mathbb{P}(Z \leq t) = p$, i.e.\ the *inverse function* of the CDF.^[Defining a quantile function for discrete random variables requires a bit more care.] For example, here I have plotted the standard normal quantile function `qnorm` alongside the corresponding CDF `pnorm`

::: {.cell}

```{.r .cell-code}
par(mfrow = c(1, 2))
curve(qnorm(x), 0, 1, n = 1001)
curve(pnorm(x), -3, 3, n = 1001)
```

::: {.cell-output-display}
![](index_files/figure-html/unnamed-chunk-9-1.svg){width=672}
:::

```{.r .cell-code}
par(mfrow = c(1, 1))
```
:::

--> As its name suggests, a quantile-quantile plot compares the quantiles of two probability distributions. But rather than comparing two quantile functions plotted against <img src="https://latex.codecogs.com/png.latex?p">, it compares the quantiles of two distributions <em>plotted against each other</em>. This is a bit confusing the first time you encounter it, so we’ll take things step-by-step.</p>
<p>If our simulated z-scores from above are well-approximated by a standard normal distribution, then their median should be close to that of a standard normal random variable, i.e.&nbsp;zero. This is indeed the case:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(z)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.0001169817</code></pre>
</div>
</div>
<p>But it’s not just the medians that should be close to each other: <em>all</em> the quantiles should be. So now let’s look at the 25th-percentile and 75th-percentile as well. Rather than computing them one-by-one, we can generate them in a single batch by first setting up a vector <code>p</code> of probabilities and using <code>rbind</code> to print the results in a convenient format</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>)</span>
<span id="cb12-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">normal =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qnorm</span>(p), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">simulation =</span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(z, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">probs =</span> p))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>                  25%          50%       75%
normal     -0.6744898 0.0000000000 0.6744898
simulation -0.6779843 0.0001169817 0.6791994</code></pre>
</div>
</div>
<p>This looks good as well. If we want to compare quantiles over a finer grid of values for <code>p</code>, it’s more convenient to make a plot rather than a table. Suppose that we treat the values <code>qnorm</code> as an <img src="https://latex.codecogs.com/png.latex?x">-coordinate and the quantiles of <code>z</code> as a <img src="https://latex.codecogs.com/png.latex?y">-coordinate. If the CLT is giving us a good approximation, then we should have <img src="https://latex.codecogs.com/png.latex?x%20%5Capprox%20y"> and all of the points should fall near the 45-degree line. This is indeed what we observe:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">to =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>)</span>
<span id="cb14-2">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qnorm</span>(p)</span>
<span id="cb14-3">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(z, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">probs =</span> p)</span>
<span id="cb14-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(x, y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'std. normal quantiles'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'quantiles of z'</span>)</span>
<span id="cb14-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># plot the 45-degree line</span></span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/thirty-isn-t-the-magic-number/index_files/figure-html/unnamed-chunk-12-1.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The plot that we have just made is called a <em>normal quantile-quantile plot</em>. It is constructed as follows:</p>
<ol type="1">
<li>Set up a vector <code>p</code> of probabilities.</li>
<li>Calculate the corresponding quantiles of a standard normal RV, <code>qnorm(p)</code>. Call these <img src="https://latex.codecogs.com/png.latex?x">.</li>
<li>Calculate the corresponding quantiles of your data, <code>quantile(your_data_here, probs = p)</code>. Call them <img src="https://latex.codecogs.com/png.latex?y">.</li>
<li>Plot <img src="https://latex.codecogs.com/png.latex?y"> against <img src="https://latex.codecogs.com/png.latex?x">.</li>
</ol>
<p>If the points all fall on a line, then the quantiles of the observed data agree with those of <em>some</em> normal distribution, although perhaps not a standard normal. If we standardize the data before making such a plot, as we did to construct <code>z</code> above, the relevant line will be the 45-degree line. If not, it will be a different line but the interpretation remains the same. The easiest way to make a normal quantile-quantile plot in R is by using the function <code>qqnorm</code> followed by <code>qqline</code>. We could do this either using the centered and standardized simulation draws <code>z</code> or the original draws <code>xbar_sims</code></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb15-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qqnorm</span>(z, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Quantiles of z'</span>)</span>
<span id="cb15-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qqline</span>(z)</span>
<span id="cb15-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qqnorm</span>(xbar_sims, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Quantiles of xbar_sims'</span>)</span>
<span id="cb15-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qqline</span>(xbar_sims)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/thirty-isn-t-the-magic-number/index_files/figure-html/unnamed-chunk-13-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span></code></pre></div></div>
</div>
<p>The only difference between these two plots is the scale of the <img src="https://latex.codecogs.com/png.latex?y">-axis. The plot that uses the original simulation draws <code>xbar_sims</code> has a <img src="https://latex.codecogs.com/png.latex?y">-axis that runs between <img src="https://latex.codecogs.com/png.latex?0.1"> and <img src="https://latex.codecogs.com/png.latex?0.9"> because the sample average of <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BUniform%7D(0,1)"> random variables must lie within the interval <img src="https://latex.codecogs.com/png.latex?%5B0,1%5D">. In contrast, the corresponding <img src="https://latex.codecogs.com/png.latex?z">-scores lie in the range <img src="https://latex.codecogs.com/png.latex?%5B-4,4%5D">.<sup>4</sup> For <img src="https://latex.codecogs.com/png.latex?x">-values between <img src="https://latex.codecogs.com/png.latex?-3"> and <img src="https://latex.codecogs.com/png.latex?3">, we can’t even see the line generated by <code>qqline</code>: the quantiles of our simulation draws are extremely close to those of a normal distribution. Outside of this range, however, we see that the black circles <em>curve away</em> from the line. For values of <img src="https://latex.codecogs.com/png.latex?x"> around <img src="https://latex.codecogs.com/png.latex?-4">, the quantiles of <code>z</code> are above those of a standard normal, i.e.&nbsp;shifted to the right. For values of <img src="https://latex.codecogs.com/png.latex?x"> around <img src="https://latex.codecogs.com/png.latex?4">, the picture is reversed: the quantiles of <code>z</code> are below those of a standard normal, i.e.&nbsp;shifted to the left. This means that <code>z</code> has <em>lighter tails</em> than a standard normal: it is a bit less likely to yield <em>extremely</em> large positive or negative values, for example</p>
<div class="no-row-height column-margin column-container"><div id="fn4"><p><sup>4</sup>&nbsp;Recall that we subtracted <img src="https://latex.codecogs.com/png.latex?1/2"> and multiplied by <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7B120%7D%5Capprox%2011"> to construct <code>z</code> from <code>xbar_sims</code>.</p></div></div><div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">simulation =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(z, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0001</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">normal =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qnorm</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0001</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>      simulation    normal
0.01%  -3.563576 -3.719016</code></pre>
</div>
</div>
<p>This makes perfect sense. A standard normal can take on arbitrarily large values, while the sample mean of ten uniforms is necessarily bounded above by <img src="https://latex.codecogs.com/png.latex?1">. So if you want to carry out a <img src="https://latex.codecogs.com/png.latex?0.01%5C%25"> test (<img src="https://latex.codecogs.com/png.latex?%5Calpha%20=%200.0001">), the approximation provided by the CLT won’t quite cut it with <img src="https://latex.codecogs.com/png.latex?n%20=%2010"> in this example. But for any conventional significance level, it’s nearly perfect:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1">p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.025</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)</span>
<span id="cb19-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">normal =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qnorm</span>(p), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">simulation =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(z, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob =</span> p))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>                  1%      2.5%        5%       10%
normal     -2.326348 -1.959964 -1.644854 -1.281552
simulation -2.302793 -1.958568 -1.654508 -1.290797</code></pre>
</div>
</div>
</section>
</section>
<section id="a-sample-size-of-thirty-isnt-sufficient." class="level2 page-columns page-full" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="a-sample-size-of-thirty-isnt-sufficient."><span class="header-section-number">2</span> A sample size of thirty isn’t sufficient.</h2>
<p>Now suppose that <img src="https://latex.codecogs.com/png.latex?n%20=%20100"> and <img src="https://latex.codecogs.com/png.latex?X_1,%20%5Cdots%20X_n%20%5Csim"> iid Bernoulli<img src="https://latex.codecogs.com/png.latex?(1/60)">. What is the CDF of <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D_n">? Rather than approximating the answer to this question by simulation, as we did in the uniform example from above, we’ll work out the <em>exact</em> result and compare it to the approximation provided by the CLT. If <img src="https://latex.codecogs.com/png.latex?X_1,%20%5Cdots,%20X_n%20%5Csim"> iid Bernoulli<img src="https://latex.codecogs.com/png.latex?(p)">, then by definition the sum <img src="https://latex.codecogs.com/png.latex?S_n%20=%20%5Csum_%7Bi=1%7D%5En%20X_i"> follows a Binomial<img src="https://latex.codecogs.com/png.latex?(n,p)"> distribution. The probability mass function and CDF of this distribution are available in R via the <code>dbinom()</code> and <code>pbinom()</code> commands. So what about <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D_n">? Notice that <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BP%7D(%5Cbar%7BX%7D_n%20=%20x)%20=%20%5Cmathbb%7BP%7D(S_n/n%20=%20x)%20=%20%5Cmathbb%7BP%7D(S_n%20=%20nx)%0A"> Thus, if <img src="https://latex.codecogs.com/png.latex?f(s)%20=%20%5Cmathbb%7BP%7D(S_n%20=%20s)"> is the pmf of <img src="https://latex.codecogs.com/png.latex?S_n"> for <img src="https://latex.codecogs.com/png.latex?s%20%5Cin%20%5C%7B0,%201,%20%5Cdots%20n%5C%7D">, it follows that <img src="https://latex.codecogs.com/png.latex?f(nx)"> is the pmf of <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D_n"> for <img src="https://latex.codecogs.com/png.latex?x%20%5Cin%20%5C%7B0,%201/n,%202/n,%20%5Cdots,%201%5C%7D">. This means that we can use <code>dbinom</code> to plot the <em>exact</em> sampling distribution of <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D_n"> when <img src="https://latex.codecogs.com/png.latex?n%20=%20100"> and <img src="https://latex.codecogs.com/png.latex?p%20=%201/60"> as follows<sup>5</sup></p>
<div class="no-row-height column-margin column-container"><div id="fn5"><p><sup>5</sup>&nbsp;Notice that I “zoomed in” on the most interesting part of the plot by setting <code>xlim = c(0, 0.1)</code>.</p></div></div><div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb21-2">p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span></span>
<span id="cb21-3">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">to =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>n)</span>
<span id="cb21-4">P_x_bar <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbinom</span>(n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob =</span> p)</span>
<span id="cb21-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(x, P_x_bar, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'h'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pmf of Xbar'</span>, </span>
<span id="cb21-6">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'blue'</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/thirty-isn-t-the-magic-number/index_files/figure-html/unnamed-chunk-16-1.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The result is <em>far</em> from a normal distribution. Not only is it noticeably discrete, it is also seriously asymmetric. Another way to see this is by examining the CDF. If the central limit theorem is working well in this example, we should have <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D_n%20%5Capprox%20N%5Cbig(p,%20p(1%20-%20p)/n%5Cbig)">. Extending the idea from above, we can plot the exact CDF of <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D_n"> using the binomial CDF <code>pbinom()</code> and compare it to the approximation suggested by the CLT:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.02</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.08</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.001</span>)</span>
<span id="cb22-2">F_x_bar <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pbinom</span>(n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob =</span> p)</span>
<span id="cb22-3">F_clt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pnorm</span>(x, p, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n))</span>
<span id="cb22-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(x, F_x_bar, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'s'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'blue'</span>)</span>
<span id="cb22-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">points</span>(x, F_clt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'l'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>)</span>
<span id="cb22-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">legend</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'topleft'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Exact'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'CLT'</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'blue'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'red'</span>),</span>
<span id="cb22-7">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/thirty-isn-t-the-magic-number/index_files/figure-html/unnamed-chunk-17-1.svg" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The approximation is noticeably poor, but is the problem serious enough to affect any inferences we might hope to draw? Suppose we wanted to construct a 95% confidence interval for <img src="https://latex.codecogs.com/png.latex?p">. The textbook approach, based on the CLT, would have us report <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D%20%5Cpm%201.96%20%5Ctimes%20%5Csqrt%7B%5Cwidehat%7Bp%7D(1%20-%20%5Cwidehat%7Bp%7D)/n%7D"> where <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%7Bp%7D"> is the sample proportion, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D_n">. Let’s set up a little simulation experiment to see how well this interval performs when <img src="https://latex.codecogs.com/png.latex?n%20=%20100"> and <img src="https://latex.codecogs.com/png.latex?p%20=%201/60">.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulate 5000 draws for phat</span></span>
<span id="cb23-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># with p = 1/60, n = 100</span></span>
<span id="cb23-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">54321</span>)</span>
<span id="cb23-4">draw_sim_phat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(p, n) {</span>
<span id="cb23-5">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbinom</span>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob =</span> p)</span>
<span id="cb23-6">  phat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(x)</span>
<span id="cb23-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(phat)</span>
<span id="cb23-8">}</span>
<span id="cb23-9">p_true <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span></span>
<span id="cb23-10">sample_size <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb23-11">phat_sims <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replicate</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">draw_sim_phat</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p =</span> p_true, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> sample_size))</span>
<span id="cb23-12"></span>
<span id="cb23-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># What fraction of the CIs cover the true value of p?</span></span>
<span id="cb23-14">SE <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(phat_sims <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> phat_sims) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> sample_size) </span>
<span id="cb23-15">lower <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> phat_sims <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.96</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> SE </span>
<span id="cb23-16">upper <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> phat_sims <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.96</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> SE </span>
<span id="cb23-17">coverage_prob <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>((lower <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> p_true) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> (p_true <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> upper))</span>
<span id="cb23-18">coverage_prob</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.824</code></pre>
</div>
</div>
<p>So only 82% of these supposed 95% confidence intervals actually cover the true value of <img src="https://latex.codecogs.com/png.latex?p">! Clearly 100 observations aren’t enough to rely upon the CLT in this example.</p>
</section>
<section id="epilogue" class="level2 page-columns page-full" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="epilogue"><span class="header-section-number">3</span> Epilogue</h2>
<p>I hope these examples have convinced you that, in spite of what you may have heard elsewhere, <img src="https://latex.codecogs.com/png.latex?n%5Cgeq%2030"> is neither necessary nor sufficient for the CLT to provide an adequate approximation. But some important questions remain. First, what can we do in situations like the second example, where we want to carry out inference for a small proportion? The problem is hardly academic: at the time of this writing, the most recent estimate of coronavirus prevalence in the UK was approximately 0.2%, i.e.&nbsp;nearly ten times <em>smaller</em> than the value I used for <img src="https://latex.codecogs.com/png.latex?p"> in my second example.<sup>6</sup> Second, how did the <img src="https://latex.codecogs.com/png.latex?n%5Cgeq%2030"> folk wisdom arise? Is there anything that we can say about <img src="https://latex.codecogs.com/png.latex?n%5Cgeq%2030">? Finally, are there any theoretical results that can provide guidance about the quality of the approximation provided by the CLT? These questions will have to wait for a future post!</p>


<div class="no-row-height column-margin column-container"><div id="fn6"><p><sup>6</sup>&nbsp;Source: <a href="https://www.gov.uk/government/publications/react-1-study-of-coronavirus-transmission-march-2021-final-results/react-1-study-of-coronavirus-transmission-march-2021-final-results">REACT-1 study of coronavirus transmission: March 2021 final results</a></p></div></div></section>


 ]]></description>
  <category>statistics</category>
  <category>teaching</category>
  <guid>https://www.econometrics.blog/post/thirty-isn-t-the-magic-number/</guid>
  <pubDate>Sat, 08 May 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Econometricians Anonymous</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/econometricians-anonymous/</link>
  <description><![CDATA[ 




<p><em>Some years back, I wrote a monologue for the Economics Department Skit Night at UPenn. It was a niche venue, granted, but the crowd seemed to enjoy my contribution. In honor of April Fool’s day I’ve posted a lightly-edited version below. On the off chance that anyone else finds this amusing, I grant unlimited rights for this material to be borrowed, adapted, remixed, stolen, execrated, or burned in effigy as you see fit. It’s funnier when you use the names of your own colleagues so I encourage you to fill in the blanks below!</em></p>
<hr>
<p><em>Group leader walks in and writes “Econometricians Anonymous” in big letters on the blackboard.</em></p>
<p><strong>Group Leader:</strong> Thanks for coming everyone. Tonight we’re going to hear from [YOUR NAME].</p>
<p><strong>Econometrician:</strong> I’m [YOUR NAME] and I’m an econometrician. It’s been six months, twelve days, and five hours since my last derivation.</p>
<p>So how did it all start? Like a lot of people, I started out deriving socially: at parties, out clubbing with the econometrics group. [SENIOR ECONOMETRICS COLLEAGUE] would have the bartender set us up with a dozen lemmas and we’d each prove four on the spot: one right after the other. Sure it was a little wild, but I always told myself I was in control. I realize now that I wasn’t.</p>
<p>The more I derived, the more I needed to derive. Sometimes I couldn’t find any co-authors to derive with me, and eventually I started deriving alone. I still remember waking up on the floor of my office after one of my all-night limit theory benders: pads of paper covered with equations strewn about the floor. I was a mess.</p>
<p>Pretty soon they started recognizing me in stationary shops and office supply stores. Sometimes they wouldn’t sell me paper and pencils. One night in my desperation, I broke into the Econ office to steal some notepads. [DEPARTMENT ADMINISTRATOR] was waiting for me: “I think you’ve had enough of those, [YOUR NAME].” It was the most embarrassing moment of my adult life.</p>
<p>I didn’t realize it at the time, but I had completely cut myself off from my family, friends, and colleagues. I kept finding ways to justify my behavior: “Don’t listen to them: Econometrica’s a great journal. Who cares if no one uses your results?” It all sounds so hollow now, but I really believed it at the time.</p>
<p>Pretty soon I started making outrageous assumptions in my papers: “Suppose that X has finite 128th moments; these regularity conditions are basically standard.” I was out of my mind. Eventually, I started experimenting with simulations. After a while, real data just didn’t do it for me. How could it when I could make thousands of pristine pseudo-random draws dance across my laptop screen at the touch of a button, any time of day or night.</p>
<p>I don’t know what would have happened to me if my friends hadn’t staged an intervention. When I got home there were two stacks on the coffee table: one of my recent working papers and the other, ten times as thick, of the corresponding technical appendices. I knew I had hit rock bottom.</p>
<p>But I’ve made a lot of progress since then, thanks in large part to the love and support of my fellow recovering econometricians here at <em>Econometricians Anonymous</em>. Still, it’s a daily struggle to stay clean. I remember calling my sponsor [APPLIED COLLEAGUE] back in December. It was the middle of the night, I was in the computer lab and I had just double-clicked on the Matlab icon. [APPLIED COLLEAGUE] was there in 15 minutes. He logged me off the computer, took me to a diner and ordered us coffee. We stayed up most of the night talking and running cross-country growth regressions in STATA. It really helped.</p>
<p>I’m doing a lot better now. I’m doing applied work, I’m publishing in general interest journals, and people are citing my research. And I’m here to tell you that if I can do it so can you. We’re all here, all of us at <em>Econometricians Anonymous</em>, to help each other kick the habit. Thank you.</p>
<p><strong>Group Leader:</strong> Thanks [YOUR NAME]. That’s all for tonight, but we hope you’ll join us tomorrow for <em>Game Theorists Anonymous</em> where we’ll be hearing about [SENIOR THEORY COLLEAGUE]’s exciting new research agenda in [RESEARCH AREA THAT SENIOR THEORY COLLEAGUE DEPLORES].</p>



 ]]></description>
  <category>meta</category>
  <guid>https://www.econometrics.blog/post/econometricians-anonymous/</guid>
  <pubDate>Thu, 01 Apr 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Past the Peak? Excess Deaths in England and Wales</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/past-the-peak-excess-deaths-in-england-and-wales/</link>
  <description><![CDATA[ 




<p>Since my previous post, the <a href="https://www.ons.gov.uk/peoplepopulationandcommunity/birthsdeathsandmarriages/deaths/datasets/weeklyprovisionalfiguresondeathsregisteredinenglandandwales">Office for National Statistics</a> has posted updated data on weekly deaths, and we’ve updated <a href="https://github.com/fditraglia/rcovidUK">rcovidUK</a> accordingly. Here’s where things stand:</p>
<div class="cell">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/past-the-peak-excess-deaths-in-england-and-wales/index_files/figure-html/plot-1.svg" class="img-fluid figure-img" width="672"></p>
<figcaption>Total Weekly Deaths in England &amp; Wales.</figcaption>
</figure>
</div>
</div>
</div>
<p>With the caveat that the date at which a death is <em>reported</em> need not agree with the date at which it actually occurred, excess deaths appear to have peaked and begun to decline in each of the ten regions. (Recently the ONS has started posting data on occurrences rather than reports, but these are unavailable for the historical comparison we’re making here.) As in my earlier post, the red curve is an equally-weighted average of <em>reported</em> weekly deaths in a given region for the past five years, up to and including 2019. Weeks are defined by the ONS to end on Fridays. This means that a given week does not necessarily correspond to the same days of the year across years, but always exactly one of each day of the week: Monday, Tuesday, etc. This eliminates seasonality from day-of-the-week effects, but creates the possibility of neglected seasonality from holidays that move across weeks during different years, e.g.&nbsp;Easter. For each point on the red curve, we have added simple error bars: <img src="https://latex.codecogs.com/png.latex?%5Cpm%202%20%5Ctimes%20%5Ctext%7BSE%7D"> where SE is the usual standard error for a mean, ignoring any possible dependence between deaths in the same week across different years. The blue curve depicts weekly deaths in 2020.</p>
<p>We had hoped to include data from Scotland in this update, but it turns out that the <a href="https://www.nrscotland.gov.uk/">NRS</a>, Scotland’s equivalent of the <a href="https://www.ons.gov.uk/">ONS</a> uses a different definition of weeks of the year, so it wouldn’t be easy to line up the respective series for purposes of comparison. We may revisit this later on.</p>
<p>If you’d like to replicate our plots, the R code is as follows:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rcovidUK)</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Set the most recent week of data for 2020</span></span>
<span id="cb1-5">week_2020 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ONSweekly <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2020</span>, reg_nm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"North East"</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(deaths)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(week) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>()</span>
<span id="cb1-9"></span>
<span id="cb1-10">df_2020 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ONSweekly <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2020</span>)</span>
<span id="cb1-12"></span>
<span id="cb1-13">df_prev5 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ONSweekly <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2020</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2015</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use last 5 years for average</span></span>
<span id="cb1-15"></span>
<span id="cb1-16"></span>
<span id="cb1-17">df_prev5 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(reg_nm, week) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deaths_mean =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(deaths),</span>
<span id="cb1-20">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deaths_sd =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(deaths),</span>
<span id="cb1-21">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(),</span>
<span id="cb1-22">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> deaths_sd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(n)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2015-2019"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deaths =</span> deaths_mean) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>(df_2020) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(week<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>week_2020) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>week, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> deaths, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> reg_id)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_errorbar</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin=</span>deaths <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> se, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax=</span>deaths <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> se), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width=</span>.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>reg_nm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set1"</span>,</span>
<span id="cb1-32">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Av. Past 5 Years"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2020"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Week from Start of Year"</span>,</span>
<span id="cb1-34">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Deaths per Week"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'top'</span>,</span>
<span id="cb1-36">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb1-37">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.key.size =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cm"</span>))</span></code></pre></div></div>
</div>



 ]]></description>
  <category>applied</category>
  <guid>https://www.econometrics.blog/post/past-the-peak-excess-deaths-in-england-and-wales/</guid>
  <pubDate>Wed, 13 May 2020 00:00:00 GMT</pubDate>
</item>
<item>
  <title>UK Excess Deaths by Age Group and Sex</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/uk-excess-deaths-are-concentrated-among-older-men/</link>
  <description><![CDATA[ 




<!--One of the most striking facts about covid has been its disproportionate impact on men and older people.-->
<p>In an earlier post I looked at regional differences in the effects of Covid-19 by calculating excess deaths in each week of 2020 relative to an average of the preceding five years. There the idea was to get a sense for the number of deaths that were likely caused by Covid-19 regardless of whether they were officially registered as such, e.g.&nbsp;deaths that occurred outside of hospitals. Thanks to my RA Dan Mead, here’s the same analysis carried out by age group and sex rather than region:</p>
<div class="cell">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/uk-excess-deaths-are-concentrated-among-older-men/index_files/figure-html/plot-1.svg" class="img-fluid figure-img" width="672"></p>
<figcaption>Total Weekly Deaths in England &amp; Wales by Sex and Age Group.</figcaption>
</figure>
</div>
</div>
</div>
<p>The red curve is an equally-weighted average of reported weekly deaths for the past five years up to and including 2019, while the blue curve depicts weekly deaths in 2020. All calculations are based on data from the <a href="https://www.ons.gov.uk/peoplepopulationandcommunity/birthsdeathsandmarriages/deaths/datasets/weeklyprovisionalfiguresondeathsregisteredinenglandandwales">ONS</a>. Each point on the red curve is accompanied by error bars indicating plus/minus two standard errors of the mean. Roughly speaking these bounds are equivalent to the margin of error from a typical opinion poll. (For the precise calculations, see the R code below.)</p>
<p>Deaths below age 1 show no clear pattern in 2020 relative to the average of past years. There’s a hint of slightly <em>lower</em> deaths among males aged 1-14 since the lockdown began, although the data are quite noisy given the extremely small number of deaths in this age group. For the remaining age groups, we start to see a clear effect around the time of the covid outbreak, approximately weeks 11 through 22 of 2020. Because death rates vary with age and sex in both ordinary years and 2020, the cleanest way to compare across groups is by computing <em>relative</em> excess deaths. To do this, we add up the differences between the red curve and the blue curve between weeks 11 and 22 and divide the result by sum of the red curve during the same period of time. The result give us the percentage by which total deaths in a given age/sex group in weeks 11 through 22 of 2020 exceed the “usual” number of deaths for this age/sex group during the same weeks based on past data.</p>
<p>In the 15-44 age group, male deaths during weeks 11 through 22 of 2020 were 6.4% higher than usual. In contrast, female deaths were 9.7% higher than usual. Deaths in this age group, however, are relatively rare. Accordingly, the blue curves lie comfortably within the error bars for all but a small number of weeks. For the remaining age groups, the picture becomes much starker and the relative excess deaths much larger:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Age Group</th>
<th>Men</th>
<th>Women</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>45-64</td>
<td>+42.2%</td>
<td>+32.4%</td>
</tr>
<tr class="even">
<td>65-74</td>
<td>+42.9%</td>
<td>+31.6%</td>
</tr>
<tr class="odd">
<td>75-84</td>
<td>+58.2%</td>
<td>+45.5%</td>
</tr>
<tr class="even">
<td>Over 85</td>
<td>+65.3%</td>
<td>+48.5%</td>
</tr>
</tbody>
</table>
<p>In each age group, relative excess deaths are substantially higher for men than women. For example, male deaths in the 45-64 age group were 42.2% higher than usual in weeks 11 through 22 of 2020 compared to 32.4% higher for women. Although relative excess deaths increase markedly with age for both sexes, even the rates in the 45-64 group are alarming, particularly for men.</p>
<p>You can replicate the plot from above by running this R code:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rcovidUK)</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Set the most recent week of data for 2020</span></span>
<span id="cb1-5">week_2020 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ONSweeklyagegender <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2020</span>, age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;1"</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(deaths)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(week) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>()</span>
<span id="cb1-9"></span>
<span id="cb1-10">df_2020 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ONSweeklyagegender <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2020</span>)</span>
<span id="cb1-12"></span>
<span id="cb1-13">df_prev5 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ONSweeklyagegender <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2020</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2015</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use last 5 years for average</span></span>
<span id="cb1-15"></span>
<span id="cb1-16"></span>
<span id="cb1-17">df_prev5 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(age, week, gender) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deaths_mean =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(deaths),</span>
<span id="cb1-20">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deaths_sd =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(deaths),</span>
<span id="cb1-21">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(),</span>
<span id="cb1-22">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> deaths_sd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(n)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2015-2019"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deaths =</span> deaths_mean) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>(df_2020) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(week <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> week_2020) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>week, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> deaths, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span>year)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_errorbar</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin=</span>deaths <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> se, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax=</span>deaths <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> se), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width=</span>.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_grid</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(age), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(gender), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set1"</span>,</span>
<span id="cb1-33">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Av. Past 5 Years"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2020"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Week from Start of Year"</span>,</span>
<span id="cb1-35">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Deaths per Week"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-36">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'top'</span>,</span>
<span id="cb1-37">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb1-38">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.key.size =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cm"</span>))</span></code></pre></div></div>
</div>



 ]]></description>
  <category>applied</category>
  <guid>https://www.econometrics.blog/post/uk-excess-deaths-are-concentrated-among-older-men/</guid>
  <pubDate>Wed, 13 May 2020 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Excess Deaths in England and Wales</title>
  <dc:creator>Francis J. DiTraglia</dc:creator>
  <link>https://www.econometrics.blog/post/uk-excess-deaths-by-region/</link>
  <description><![CDATA[ 




<p>To get a better idea of the impact of Covid19 in the UK, Dan Mead and I put together an R package <a href="https://github.com/fditraglia/rcovidUK">rcovidUK</a> with weekly deaths in England and Wales, taken from the <a href="https://www.ons.gov.uk/peoplepopulationandcommunity/birthsdeathsandmarriages/deaths/datasets/weeklyprovisionalfiguresondeathsregisteredinenglandandwales">Office for National Statistics</a> (ONS), allowing us to produce a plot of total deaths by region in 2020:</p>
<div class="cell">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.econometrics.blog/post/uk-excess-deaths-by-region/index_files/figure-html/plot-1.svg" class="img-fluid figure-img" width="672"></p>
<figcaption>Total Weekly Deaths in England &amp; Wales.</figcaption>
</figure>
</div>
</div>
</div>
<p>The red curve is an equally-weighted average of weekly deaths in a given region for the past five years, up to and including 2019. Weeks are defined by the ONS to end on Fridays. This means that a given week does not necessarily correspond to the same days of the year across years, but always exactly one of each day of the week: Monday, Tuesday, etc. This eliminates seasonality from day-of-the-week effects, but creates the possibility of neglected seasonality from holidays that move across weeks during different years, e.g.&nbsp;Easter. For each point on the red curve, we have added simple error bars: <img src="https://latex.codecogs.com/png.latex?%5Cpm%202%20%5Ctimes%20%5Ctext%7BSE%7D"> where SE is the usual standard error for a mean, ignoring any possible dependence between deaths in the same week across different years. The blue curve depicts weekly deaths in 2020. From week 12 or 13, depending on region, we see a dramatic uptick in deaths across England and Wales, well outside the two standard error bars. R source code for this plot follows below.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rcovidUK)</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Set the most recent week of data for 2020</span></span>
<span id="cb1-5">week_2020 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># original version of this post only had data up to week 16</span></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#week_2020 &lt;- ONSweekly |&gt;</span></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  filter(year == 2020, !is.na(deaths)) |&gt;</span></span>
<span id="cb1-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  pull(week) |&gt;</span></span>
<span id="cb1-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  max</span></span>
<span id="cb1-10"></span>
<span id="cb1-11">df_2020 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ONSweekly <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2020</span>)</span>
<span id="cb1-13"></span>
<span id="cb1-14">df_prev5 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ONSweekly <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2020</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> year <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2015</span>) </span>
<span id="cb1-16"></span>
<span id="cb1-17">df_prev5 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(reg_nm, week) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deaths_mean =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(deaths),</span>
<span id="cb1-20">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deaths_sd =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(deaths),</span>
<span id="cb1-21">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(),</span>
<span id="cb1-22">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> deaths_sd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(n)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2015-2019"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deaths =</span> deaths_mean) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>(df_2020) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(week<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>week_2020) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>week, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> deaths, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> year, </span>
<span id="cb1-28">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> reg_id)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_errorbar</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin=</span>deaths <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> se, </span>
<span id="cb1-30">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> deaths <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> se),</span>
<span id="cb1-31">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width=</span>.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb1-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb1-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>reg_nm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set1"</span>,</span>
<span id="cb1-35">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Avg Past 5 Yrs"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2020"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-36">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Week from Start of Year"</span>,</span>
<span id="cb1-37">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Deaths per Week"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-38">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'top'</span>,</span>
<span id="cb1-39">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb1-40">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.key.size =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cm"</span>))</span></code></pre></div></div>
</div>



 ]]></description>
  <category>applied</category>
  <guid>https://www.econometrics.blog/post/uk-excess-deaths-by-region/</guid>
  <pubDate>Mon, 04 May 2020 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>
