<?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/r-feed.html</link>
<atom:link href="https://www.econometrics.blog/r-feed.xml" rel="self" type="application/rss+xml"/>
<description>Posts from econometrics.blog that use R: simulations, numerical methods, and statistical computing.</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>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 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>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>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>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>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>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>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>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>
<p>and the following code to generate the data contained in <code>sim_data</code></p>


</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>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>
</channel>
</rss>
