<?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>Xiang Ao</title>
<link>https://xiangao.github.io/blogs.html</link>
<atom:link href="https://xiangao.github.io/blogs.xml" rel="self" type="application/rss+xml"/>
<description>Posts and notes on causal inference, econometrics, machine learning, and statistical programming.</description>
<image>
<url>https://xiangao.github.io/profile.jpg</url>
<title>Xiang Ao</title>
<link>https://xiangao.github.io/blogs.html</link>
</image>
<generator>quarto-1.8.27</generator>
<lastBuildDate>Mon, 17 Nov 2025 05:00:00 GMT</lastBuildDate>
<item>
  <title>Gender Wage Gap Analysis Using US Current Population Survey Data</title>
  <dc:creator>Xiang Ao</dc:creator>
  <link>https://xiangao.github.io/posts/gwg/</link>
  <description><![CDATA[ 






<p>Traditionally Oaxaca-Blinder decomposition is used to decompose the wage gap into explained and unexplained parts. The explained part is due to differences in observable characteristics, such as education, experience, occupation, etc. The unexplained part is due to discrimination or other unobserved factors.</p>
<p>It has a problem that when decomposing, using female as reference group or male as reference group will give different results.</p>
<p>Słoczyński (2015) and Słoczyński (2013) have shown that a regression adjustment type of analysis is equivalent to Oaxaca-Blinder decomposition in a nonstandard way.</p>
<section id="oaxaca-blinder-decomposition" class="level2">
<h2 class="anchored" data-anchor-id="oaxaca-blinder-decomposition">Oaxaca-Blinder decomposition</h2>
<p>Assume the regression model is different for male and female: <img src="https://latex.codecogs.com/png.latex?%20y_i%20=%20X_i%20%5Cbeta_a%20+%20%5Cepsilon_a%20"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?a"> is gender (female is 1), <img src="https://latex.codecogs.com/png.latex?X">’s are person characteristics.</p>
<p><img src="https://latex.codecogs.com/png.latex?%20E%5By_i%20%7C%20a_i%20=%201%5D%20-%20E%5By_i%20%7C%20a_i%20=%200%5D%20=%20E%5BX_i%20%7C%20a_i=1%5D%20%20(%5Cbeta_1%20-%20%5Cbeta_0)%20+%20(E%5BX_i%20%7C%20a_i%20=1%20%5D%20-%20E%5BX_i%20%7C%20a_i%20=%200%5D)%20%5Cbeta_0%20"></p>
<p>or</p>
<p><img src="https://latex.codecogs.com/png.latex?%20E%5By_i%20%7C%20a_i%20=%201%5D%20-%20E%5By_i%20%7C%20a_i%20=%200%5D%20=%20E%5BX_i%20%7C%20a_i=0%5D%20%20(%5Cbeta_1%20-%20%5Cbeta_0)%20+%20(E%5BX_i%20%7C%20a_i%20=1%20%5D%20-%20E%5BX_i%20%7C%20a_i%20=%200%5D)%20%5Cbeta_1%20"> These two equations are different forms of the Oaxaca-Blinder decomposition. The first element is the “unexplained component”, which is the difference in the coefficients, and the second element is the “explained component”, which is the difference in the means of the covariates.</p>
<p>These two equations can give different results for the two components, one has a baseline of male, and other one female.</p>
<p>We can further shows that</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0AE%5By_i%20%7C%20a_i%20=1%5D%20-%20E%5By_i%20%7C%20a_i=0%5D%20&amp;=%20E%5BX_i%20%7C%20a_i=1%5D%20%20(%5Cbeta_1%20-%20%5Cbeta_0)%20+%20(E%5BX_i%20%7C%20a_i%20=1%20%5D%20-%20E%5BX_i%20%7C%20a_i%20=%200%5D)%20%5Cbeta_0%20%20%5C%5C%20%20%20%20%20%20%20%20%20%0A&amp;=%20E%5By_i(1)%20-%20y_i(0)%20%7C%20a_i=1%5D%20+%20(E%5By_i(0)%20%7C%20a_i=1%5D%20-%20E%5By_i(0)%20%7C%20a_i%20=%200%5D)%20%5C%5C%20%20%20%20%20%20%20%20%20%0A&amp;=%20%5Ctau_%7BPATT%7D%20+%20(E%5By_i(0)%20%7C%20a_i=1%5D%20-%20E%5By_i(0)%20%7C%20a_i=0%5D)%0A%5Cend%7Balign%7D%0A"></p>
<p>That is, the observed difference is the sum of <img src="https://latex.codecogs.com/png.latex?%5Ctau_%7BPATT%7D"> and selection bias. It is also know in the OB decomposition literature as unexplained component and explained component. The former as “discrimination”.</p>
<p>We can simply use Regression Adjustment for <img src="https://latex.codecogs.com/png.latex?%5Ctau_%7BPATT%7D">. The easiest would be an OLS of <img src="https://latex.codecogs.com/png.latex?y_i"> on demeaned <img src="https://latex.codecogs.com/png.latex?X_i"> and <img src="https://latex.codecogs.com/png.latex?a_i"> and interaction, then the coefficient on <img src="https://latex.codecogs.com/png.latex?a_i"> is <img src="https://latex.codecogs.com/png.latex?%5Ctau_%7BPATT%7D">.</p>
<p>He aslo advocates the “population average gender effect” (PAGE):</p>
<p><img src="https://latex.codecogs.com/png.latex?%20%5Ctau_%7BPAGE%7D%20=%20E%5B%5Ctau(X_i)%5D%20"></p>
<p>which is basically ATE, or conditional average effect (CATE), then average over the <img src="https://latex.codecogs.com/png.latex?X">’s.</p>
<p>In this version, the two forms of OB decomposition can be <img src="https://latex.codecogs.com/png.latex?%5Ctau_%7BPAGM%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Ctau_%7BPAGW%7D">, for men and women.</p>
<p>The nice thing about this estimator <img src="https://latex.codecogs.com/png.latex?%5Ctau_%7BPAGE%7D"> is that first, it is consistent with the treatment effect (causal inference) literature, and it is invariant to the choice of reference group.</p>
<p>The second advantage is that we also do not need to assume homogeneity of treatment effect, which is a strong assumption in the Oaxaca-Blinder decomposition.</p>
<p>The third advantage is that we can use nonparametric methods to estimate the treatment effect. Traditional OB decomposition is based on linear regression, which assumes a linear relationship between the covariates and the outcome.</p>
</section>
<section id="example" class="level2">
<h2 class="anchored" data-anchor-id="example">Example</h2>
<p>I am using the data from https://github.com/d2cml-ai/CausalAI-Course/tree/main/data.</p>
<p>“The data set we consider is from the 2015 March Supplement of the U.S. Current Population Survey. We select white non-hispanic individuals, aged 25 to 64 years, and working more than 35 hours per week for at least 50 weeks of the year. We exclude self-employed workers; individuals living in group quarters; individuals in the military, agricultural or private household sectors; individuals with inconsistent reports on earnings and employment status; individuals with allocated or missing information in any of the variables used in the analysis; and individuals with hourly wage below 3.”</p>
<p>I am using this data to study gender wage gap (GWP).</p>
<p>We can model GWP using potential outcome framework. Here treatment is gender discrimination, and outcome is logged wage. Treatment is 1 if female, 0 if male.</p>
<p>The ATE would be</p>
<p><img src="https://latex.codecogs.com/png.latex?%20%5Cdelta_%7BATE%7D%20=%20E%5BY(1)%20-%20Y(0)%5D%20"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?Y(1)"> is the potential outcome if treated, and <img src="https://latex.codecogs.com/png.latex?Y(0)"> is the potential outcome if not treated.</p>
<section id="regression-adjustment" class="level3">
<h3 class="anchored" data-anchor-id="regression-adjustment">Regression adjustment</h3>
<p>Since this is to estimate ATE, we can use regression adjustment to estimate the ATE. First let’s use de-meaned <img src="https://latex.codecogs.com/png.latex?X">’s. Then the coefficient on gender is the ATE.</p>
<p>We use These variables as <img src="https://latex.codecogs.com/png.latex?X">’s:</p>
<p>“shs”,“hsg”,“scl”,“clg”,“ad”,“ne”,“mw”,“so”,“we”,“exp1”, “occ”, “occ2”, “ind”, and “ind2”.</p>
<p>They are:</p>
<p>“Less then High School”,“High School Graduate”,“Some College”,“College Graduate”,“Advanced Degree”, “Northeast”,“Midwest”,“South”,“West”,“Experience”, “occupation”, “occupation 2”, “industry”, and “industry 2”.</p>
<p>I decide to use “occ2” and “ind2” as they are broader categories of occupation and industry. We don’t have enough data for finer categories “occ” and “ind”.</p>
<p>“exp1” seems to be the only variable that is continuous, let’s demean it first. Let’s also input “occ2” and “ind2” as factors.</p>
<p>Let’s include demeaned “exp1”, sohs”, “hsg”, “socl”, “clg”, “mw”, “sout”, and “we” first. I leave out “occ2” and “ind2” since there are too many levels to demean.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(broom)</span>
<span id="cb1-3"></span>
<span id="cb1-4"></span>
<span id="cb1-5">data <span class="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;">"~/Downloads/wage2015_subsample_inference.csv"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">socl =</span> scl, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sohs =</span> shs, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sout =</span> so) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">exp_dm =</span> exp1 <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>(exp1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">female=</span>sex, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">occ2 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(occ2), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ind2 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(ind2)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sohs_dm =</span> sohs <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>(sohs, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb1-9">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hsg_dm =</span> hsg <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>(hsg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb1-10">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">socl_dm =</span> socl <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>(socl, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb1-11">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">clg_dm =</span> clg <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>(clg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb1-12">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mw_dm =</span> mw <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>(mw, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb1-13">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sout_dm =</span> sout <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>(sout, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb1-14">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">we_dm =</span> we <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>(we, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)) </span>
<span id="cb1-15"></span>
<span id="cb1-16"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>(data)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Rows: 5,150
Columns: 30
$ rownames &lt;dbl&gt; 10, 12, 15, 18, 19, 30, 43, 44, 47, 71, 73, 77, 84, 89, 96, 1…
$ wage     &lt;dbl&gt; 9.615385, 48.076923, 11.057692, 13.942308, 28.846154, 11.7307…
$ lwage    &lt;dbl&gt; 2.263364, 3.872802, 2.403126, 2.634928, 3.361977, 2.462215, 2…
$ sex      &lt;dbl&gt; 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1…
$ sohs     &lt;dbl&gt; 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
$ hsg      &lt;dbl&gt; 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0…
$ socl     &lt;dbl&gt; 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1…
$ clg      &lt;dbl&gt; 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0…
$ ad       &lt;dbl&gt; 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
$ mw       &lt;dbl&gt; 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
$ sout     &lt;dbl&gt; 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
$ we       &lt;dbl&gt; 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
$ ne       &lt;dbl&gt; 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
$ exp1     &lt;dbl&gt; 7.0, 31.0, 18.0, 25.0, 22.0, 1.0, 42.0, 37.0, 31.0, 4.0, 7.0,…
$ exp2     &lt;dbl&gt; 0.4900, 9.6100, 3.2400, 6.2500, 4.8400, 0.0100, 17.6400, 13.6…
$ exp3     &lt;dbl&gt; 0.343000, 29.791000, 5.832000, 15.625000, 10.648000, 0.001000…
$ exp4     &lt;dbl&gt; 0.24010000, 92.35210000, 10.49760000, 39.06250000, 23.4256000…
$ occ      &lt;dbl&gt; 3600, 3050, 6260, 420, 2015, 1650, 5120, 5240, 4040, 3255, 40…
$ occ2     &lt;fct&gt; 11, 10, 19, 1, 6, 5, 17, 17, 13, 10, 13, 14, 11, 11, 1, 19, 1…
$ ind      &lt;dbl&gt; 8370, 5070, 770, 6990, 9470, 7460, 7280, 5680, 8590, 8190, 82…
$ ind2     &lt;fct&gt; 18, 9, 4, 12, 22, 14, 14, 9, 19, 18, 18, 18, 18, 18, 17, 4, 4…
$ exp_dm   &lt;dbl&gt; -6.760583, 17.239417, 4.239417, 11.239417, 8.239417, -12.7605…
$ female   &lt;dbl&gt; 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1…
$ sohs_dm  &lt;dbl&gt; -0.02330097, -0.02330097, -0.02330097, -0.02330097, -0.023300…
$ hsg_dm   &lt;dbl&gt; -0.2438835, -0.2438835, 0.7561165, -0.2438835, -0.2438835, -0…
$ socl_dm  &lt;dbl&gt; -0.2780583, -0.2780583, -0.2780583, -0.2780583, -0.2780583, -…
$ clg_dm   &lt;dbl&gt; 0.6823301, 0.6823301, -0.3176699, -0.3176699, 0.6823301, 0.68…
$ mw_dm    &lt;dbl&gt; -0.2596117, -0.2596117, -0.2596117, -0.2596117, -0.2596117, -…
$ sout_dm  &lt;dbl&gt; -0.2965049, -0.2965049, -0.2965049, -0.2965049, -0.2965049, -…
$ we_dm    &lt;dbl&gt; -0.2161165, -0.2161165, -0.2161165, -0.2161165, -0.2161165, -…</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;"># construct matrices for estimation from the data </span></span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. basic model</span></span>
<span id="cb3-4">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>(lwage <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> female, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>data)</span>
<span id="cb3-5"><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)   2.99      0.0107    280.    0     
2 female       -0.0383    0.0160     -2.40  0.0165</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">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>(lwage <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> female<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(exp_dm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sohs_dm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> hsg_dm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> socl_dm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> clg_dm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> mw_dm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sout_dm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> we_dm), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>data)</span>
<span id="cb5-2"><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: 18 × 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)     3.02     0.00974    310.    0       
 2 female         -0.115    0.0147      -7.80  7.39e-15
 3 exp_dm          0.00801  0.000957     8.37  7.31e-17
 4 sohs_dm        -0.811    0.0620     -13.1   1.63e-38
 5 hsg_dm         -0.706    0.0350     -20.1   6.09e-87
 6 socl_dm        -0.553    0.0351     -15.7   1.45e-54
 7 clg_dm         -0.256    0.0345      -7.41  1.42e-13
 8 mw_dm           0.0337   0.0280       1.20  2.29e- 1
 9 sout_dm         0.0148   0.0271       0.548 5.84e- 1
10 we_dm           0.0555   0.0290       1.91  5.59e- 2
11 female:exp_dm   0.00212  0.00140      1.51  1.30e- 1
12 female:sohs_dm -0.0180   0.117       -0.154 8.78e- 1
13 female:hsg_dm   0.0398   0.0507       0.786 4.32e- 1
14 female:socl_dm  0.0466   0.0482       0.968 3.33e- 1
15 female:clg_dm   0.0992   0.0468       2.12  3.41e- 2
16 female:mw_dm   -0.124    0.0417      -2.98  2.93e- 3
17 female:sout_dm -0.0530   0.0403      -1.31  1.89e- 1
18 female:we_dm   -0.0655   0.0435      -1.51  1.32e- 1</code></pre>
</div>
</div>
<p>We see the raw gap is 3.8%. Adjusting for some covariates we have 11% gap.</p>
<p>Or, we can use “marginaleffects” instead of demeaning.</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">reg3 <span class="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>(lwage <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> female<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(exp1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sohs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> hsg<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> socl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> clg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> mw <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sout <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> we ), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>data)</span>
<span id="cb7-2"></span>
<span id="cb7-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(marginaleffects)</span>
<span id="cb7-4"></span>
<span id="cb7-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">avg_comparisons</span>(reg3,</span>
<span id="cb7-6">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variables =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"female"</span>,</span>
<span id="cb7-7">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vcov =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>subclass)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
 Estimate Std. Error    z Pr(&gt;|z|)    S  2.5 %  97.5 %
   -0.115     0.0147 -7.8   &lt;0.001 47.2 -0.143 -0.0858

Term: female
Type: response
Comparison: 1 - 0</code></pre>
</div>
</div>
<p>We get the same result as the demeaned regression.</p>
<p>If we want <img src="https://latex.codecogs.com/png.latex?%5Ctau_%7BATT%7D">, then we can use the “newdata” argument to specify the treatment group,</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;">avg_comparisons</span>(reg3,</span>
<span id="cb9-2">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variables =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"female"</span>,</span>
<span id="cb9-3">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vcov =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>subclass,</span>
<span id="cb9-4">                <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;">subset</span>(female <span 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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
 Estimate Std. Error     z Pr(&gt;|z|)    S  2.5 %  97.5 %
   -0.113     0.0148 -7.65   &lt;0.001 45.5 -0.142 -0.0844

Term: female
Type: response
Comparison: 1 - 0</code></pre>
</div>
</div>
<p>Next, we adjust for occupation and industry. We can use “occ2” and “ind2” as factors.</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">reg4 <span class="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>(lwage <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> female<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(exp1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sohs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> hsg<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> socl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> clg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> mw <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sout <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> we <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> occ2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ind2), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>data)</span>
<span id="cb11-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">avg_comparisons</span>(reg4,</span>
<span id="cb11-3">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variables =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"female"</span>,</span>
<span id="cb11-4">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vcov =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>subclass,</span>
<span id="cb11-5">                <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;">subset</span>(female <span 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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
 Estimate Std. Error     z Pr(&gt;|z|)    S  2.5 %  97.5 %
   -0.075     0.0162 -4.62   &lt;0.001 18.0 -0.107 -0.0432

Term: female
Type: response
Comparison: 1 - 0</code></pre>
</div>
</div>
<p>So in the model with full interaction, we have a gap of 7.5%.</p>
<p>Now let’s try nonparametric estimation.</p>
</section>
<section id="nonparametric-estimation" class="level3">
<h3 class="anchored" data-anchor-id="nonparametric-estimation">Nonparametric estimation</h3>
<p>If we’d like to go for nonparametric, relaxing linearity assumption, then we can use “npcausal”. Note this may take a while, depending on which machine learning packages you decide to include. We can also use other nonparametric estimators, such as TMLE or doubleML.</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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(npcausal)</span>
<span id="cb13-2"></span>
<span id="cb13-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#SL.library &lt;- c("SL.earth","SL.glmnet","SL.glm.interaction", "SL.mean","SL.ranger", "SL.xgboost")</span></span>
<span id="cb13-4"></span>
<span id="cb13-5"></span>
<span id="cb13-6">SL.library <span class="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;">"SL.earth"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SL.glmnet"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SL.mean"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SL.ranger"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SL.xgboost"</span>)</span>
<span id="cb13-7"></span>
<span id="cb13-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># construct W from model matrix of reg3 </span></span>
<span id="cb13-9">reg3 <span class="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>(lwage <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> exp_dm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sohs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> hsg<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> socl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> clg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> mw <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sout <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> we <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> occ2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ind2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>data)</span>
<span id="cb13-10">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;">as.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model.matrix</span>(reg3)[, <span 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;"># remove intercept</span></span>
<span id="cb13-11"></span>
<span id="cb13-12">Y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>lwage</span>
<span id="cb13-13">A <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>female</span>
<span id="cb13-14"><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;">123</span>)</span>
<span id="cb13-15">aipw_ate <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>Y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">a=</span>A, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>W, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nsplits=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sl.lib=</span>SL.library)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
  |                                                                            
  |                                                                      |   0%</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |==============                                                        |  20%</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |=====================                                                 |  30%</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |======================================================================| 100%
     parameter         est         se       ci.ll       ci.ul pval
1      E{Y(0)}  2.99614039 0.01093002  2.97471755  3.01756324    0
2      E{Y(1)}  2.93954753 0.01328396  2.91351096  2.96558410    0
3 E{Y(1)-Y(0)} -0.05659286 0.01598082 -0.08791527 -0.02527045    0</code></pre>
</div>
<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">aipw_att <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">att</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>Y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">a=</span>A, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>W, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nsplits=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sl.lib=</span>SL.library)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |==============                                                        |  20%</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |========================================================              |  80%</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |======================================================================| 100%
      parameter         est         se       ci.ll       ci.ul pval
1      E(Y|A=1)  2.94948490 0.01160804  2.92673314  2.97223666    0
2   E{Y(0)|A=1}  3.01543445 0.01199017  2.99193371  3.03893519    0
3 E{Y-Y(0)|A=1} -0.06594955 0.01392369 -0.09323998 -0.03865913    0</code></pre>
</div>
</div>
<p>We can see this effect is about -6.6%, similar to the regression adjustment result -7.5%.</p>


</section>
</section>

 ]]></description>
  <category>R</category>
  <category>code</category>
  <category>analysis</category>
  <guid>https://xiangao.github.io/posts/gwg/</guid>
  <pubDate>Mon, 17 Nov 2025 05:00:00 GMT</pubDate>
  <media:content url="https://xiangao.github.io/posts/gwg/image.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>More on Correlated Random Effects (CRE)</title>
  <dc:creator>Xiang Ao</dc:creator>
  <link>https://xiangao.github.io/posts/more_cre/</link>
  <description><![CDATA[ 






<p>Jeff Wooldridge suggested using Correlated Random Effect (CRE) for binary outcomes in this twitter post: https://x.com/jmwooldridge/status/1986100627454206220</p>
<p>Here I summarize what I learned so far:</p>
<section id="linear-model" class="level2">
<h2 class="anchored" data-anchor-id="linear-model">linear model</h2>
<p>For panel data, the usual set up is:</p>
<p><img src="https://latex.codecogs.com/png.latex?y_%7Bit%7D%20=%20%5Calpha%20+%20X_%7Bit%7D%20%5Cbeta%20+%20v_i%20+%20%5Cepsilon_%7Bit%7D%20"></p>
<p>Wooldridge (2021) paper on DiD shows that these models are the same: 1. Two way fixed effect OLS. Dummy variable approach is the same too. 2. Mundlak’s CRE approach. This includes a pooled OLS with Mundlak device (means of <img src="https://latex.codecogs.com/png.latex?X">’s by group), and CRE with random effects.</p>
<p>The idea of Chamberlain’s device is that since we don’t have <img src="https://latex.codecogs.com/png.latex?v_i">, we can project <img src="https://latex.codecogs.com/png.latex?X_i"> onto <img src="https://latex.codecogs.com/png.latex?v_i">. Basically, we can replace <img src="https://latex.codecogs.com/png.latex?v_i"> with projection of <img src="https://latex.codecogs.com/png.latex?X_i"> onto <img src="https://latex.codecogs.com/png.latex?v_i">. Mundlak’s device is a special case, that we put all <img src="https://latex.codecogs.com/png.latex?X_t">’s the same weight, thus the mean of <img src="https://latex.codecogs.com/png.latex?X_%7Bit%7D"> for each <img src="https://latex.codecogs.com/png.latex?i"> is the projection of <img src="https://latex.codecogs.com/png.latex?X_%7Bit%7D"> onto <img src="https://latex.codecogs.com/png.latex?v_i">. Replace <img src="https://latex.codecogs.com/png.latex?v_i"> with that projection, and what is left then by definition is uncorrelated with <img src="https://latex.codecogs.com/png.latex?X_%7Bit%7D">.</p>
<p>Here is an example:</p>
<section id="example-1" class="level3">
<h3 class="anchored" data-anchor-id="example-1">example 1</h3>
<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>(fixest)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(bacondecomp)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"castle"</span>)</span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fixed-effects model with individual and time fixed effects</span></span>
<span id="cb1-6">fe_model_twoway <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">feols</span>(l_homicide <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> poverty <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> l_police <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> state <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> castle)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(fe_model_twoway)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>OLS estimation, Dep. Var.: l_homicide
Observations: 550
Fixed-effects: state: 50,  year: 11
Standard-errors: IID 
          Estimate Std. Error   t value Pr(&gt;|t|)    
poverty  -0.027068   0.013306 -2.034206 0.042471 *  
l_police  0.066033   0.104588  0.631364 0.528098    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
RMSE: 0.176686     Adj. R2: 0.898978
                 Within R2: 0.009286</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;"># random effect</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lme4)</span>
<span id="cb3-3">re_model <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmer</span>(l_homicide <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> poverty <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> l_police <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;">factor</span>(year) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>  (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> state), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> castle)</span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(re_model)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Linear mixed model fit by REML ['lmerMod']
Formula: l_homicide ~ poverty + l_police + factor(year) + (1 | state)
   Data: castle

REML criterion at convergence: 3.9

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-4.5357 -0.4657  0.0151  0.4601  3.6973 

Random effects:
 Groups   Name        Variance Std.Dev.
 state    (Intercept) 0.30041  0.5481  
 Residual             0.03551  0.1884  
Number of obs: 550, groups:  state, 50

Fixed effects:
                   Estimate Std. Error t value
(Intercept)       0.4395941  0.6019389   0.730
poverty          -0.0007991  0.0120087  -0.067
l_police          0.1665913  0.1015581   1.640
factor(year)2001  0.0225820  0.0379597   0.595
factor(year)2002  0.0005681  0.0389059   0.015
factor(year)2003  0.0475588  0.0395876   1.201
factor(year)2004  0.0419220  0.0411546   1.019
factor(year)2005  0.0612232  0.0435803   1.405
factor(year)2006  0.0806576  0.0418469   1.927
factor(year)2007  0.0790850  0.0409066   1.933
factor(year)2008  0.0401268  0.0414964   0.967
factor(year)2009 -0.0424481  0.0488959  -0.868
factor(year)2010 -0.0959167  0.0470940  -2.037</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;"># Mundlak </span></span>
<span id="cb5-2">castle2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> castle <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;">group_by</span>(state) <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;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">poverty_mean =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(poverty, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">l_police_mean=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(l_police, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span>
<span id="cb5-5">cre_model <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">feols</span>(l_homicide <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> poverty <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> poverty_mean <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> l_police <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> l_police_mean <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;">factor</span>(year), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> castle2)</span>
<span id="cb5-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(cre_model)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>OLS estimation, Dep. Var.: l_homicide
Observations: 550
Standard-errors: IID 
                  Estimate Std. Error    t value   Pr(&gt;|t|)    
(Intercept)      -7.800873   0.531186 -14.685774  &lt; 2.2e-16 ***
poverty          -0.027068   0.030004  -0.902125 3.6740e-01    
poverty_mean      0.125283   0.030674   4.084272 5.0983e-05 ***
l_police          0.066033   0.235836   0.279996 7.7959e-01    
l_police_mean     1.318037   0.253323   5.202983 2.7987e-07 ***
factor(year)2001  0.033071   0.085349   0.387481 6.9855e-01    
factor(year)2002  0.022870   0.087972   0.259968 7.9499e-01    
factor(year)2003  0.074569   0.089852   0.829907 4.0696e-01    
factor(year)2004  0.079080   0.094152   0.839920 4.0133e-01    
factor(year)2005  0.109800   0.100737   1.089973 2.7622e-01    
factor(year)2006  0.118822   0.095991   1.237857 2.1631e-01    
factor(year)2007  0.115412   0.093475   1.234679 2.1749e-01    
factor(year)2008  0.077190   0.095054   0.812069 4.1711e-01    
factor(year)2009  0.027528   0.114987   0.239400 8.1089e-01    
factor(year)2010 -0.034286   0.110153  -0.311255 7.5573e-01    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
RMSE: 0.417154   Adj. R2: 0.486344</code></pre>
</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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Mundlak </span></span>
<span id="cb7-2">cre_model2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmer</span>(l_homicide <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> poverty <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> poverty_mean <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> l_police <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> l_police_mean <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;">factor</span>(year) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>  (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> state), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> castle2)</span>
<span id="cb7-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(cre_model2)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Linear mixed model fit by REML ['lmerMod']
Formula: l_homicide ~ poverty + poverty_mean + l_police + l_police_mean +  
    factor(year) + (1 | state)
   Data: castle2

REML criterion at convergence: -30.1

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-4.5991 -0.4336  0.0219  0.4307  3.6228 

Random effects:
 Groups   Name        Variance Std.Dev.
 state    (Intercept) 0.14872  0.3856  
 Residual             0.03518  0.1876  
Number of obs: 550, groups:  state, 50

Fixed effects:
                 Estimate Std. Error t value
(Intercept)      -7.80087    1.60978  -4.846
poverty          -0.02707    0.01331  -2.034
poverty_mean      0.12528    0.02360   5.309
l_police          0.06603    0.10459   0.631
l_police_mean     1.31804    0.30140   4.373
factor(year)2001  0.03307    0.03785   0.874
factor(year)2002  0.02287    0.03901   0.586
factor(year)2003  0.07457    0.03985   1.871
factor(year)2004  0.07908    0.04175   1.894
factor(year)2005  0.10980    0.04467   2.458
factor(year)2006  0.11882    0.04257   2.791
factor(year)2007  0.11541    0.04145   2.784
factor(year)2008  0.07719    0.04215   1.831
factor(year)2009  0.02753    0.05099   0.540
factor(year)2010 -0.03429    0.04885  -0.702</code></pre>
</div>
</div>
<p>In this example, fixed effect model, CRE1 and CRE2 all give the same coefficient on poverty.</p>
<p>As we know, RE model assumes <img src="https://latex.codecogs.com/png.latex?v_i"> is uncorrelated with <img src="https://latex.codecogs.com/png.latex?X_%7Bit%7D">, while CRE and FE allow for correlation between <img src="https://latex.codecogs.com/png.latex?v_i"> and <img src="https://latex.codecogs.com/png.latex?X_%7Bit%7D">.</p>
</section>
</section>
<section id="nonlinear-model" class="level2">
<h2 class="anchored" data-anchor-id="nonlinear-model">nonlinear model</h2>
<p>What I did not realize before is that this is only valid for linear case. For OLS to be consistent, we only need contemporaneous exogeneity, which means <img src="https://latex.codecogs.com/png.latex?E%5B%5Cepsilon_%7Bit%7D%20%7C%20X_%7Bit%7D,%20v_i%5D%20=%200">. This is not the case for nonlinear cases, such as logit or probit models.</p>
<section id="binary-outcome" class="level3">
<h3 class="anchored" data-anchor-id="binary-outcome">Binary outcome</h3>
<p>For binary outcomes, the model is:</p>
<p><img src="https://latex.codecogs.com/png.latex?y_%7Bit%7D%20=%20%5CPhi(%5Calpha%20+%20X_%7Bit%7D%20%5Cbeta%20+%20v_i%20+%20%5Cepsilon_%7Bit%7D)%20"> I used to think it’s the best to use conditional logit. However, it needs “conditional independence” (serial independence could be a better name) to be consistent. That is, we have to assume the series of <img src="https://latex.codecogs.com/png.latex?y_%7Bit%7D"> is conditionally independent of each other given <img src="https://latex.codecogs.com/png.latex?X_%7Bit%7D"> and <img src="https://latex.codecogs.com/png.latex?v_i">. This is a strong assumption. If we think we have serial correlation in the error term, then this would not hold. The other disadvantage of conditional logit (also called fixed effect logit) is that <img src="https://latex.codecogs.com/png.latex?v_i">’s are not estimated, it is just a nuisance parameter. Any partial effects cannot be calculated, because any partial effects are functions of <img src="https://latex.codecogs.com/png.latex?v_i">’s.</p>
<p>Instead, Wooldridge suggests using CRE probit. Basically adding Mundlak device (group means of <img src="https://latex.codecogs.com/png.latex?X">’s) to the model, and then use pooled probit. Note that we do not use RE probit or logit, since that would need conditional independence too.</p>
<p>For nonlinear panel data with unobserved heterogeneity (meaning there is <img src="https://latex.codecogs.com/png.latex?v_i"> that we don’t observe), we have a few assumptions.</p>
<p>First, strict exogeneity, <img src="https://latex.codecogs.com/png.latex?%20D(y%60_%7Bit%7D%20%7C%20X_%7Bi1%7D,%20...,%20X_%7BiT%7D,%20v_i)%20=%20D(y_%7Bit%7D%20%7C%20X_%7Bit%7D,%20v_i)%20"> This means that the distribution of <img src="https://latex.codecogs.com/png.latex?y_%7Bit%7D"> only depends on <img src="https://latex.codecogs.com/png.latex?X_%7Bit%7D">, given <img src="https://latex.codecogs.com/png.latex?v_i">.</p>
<p>Second, conditional independence, <img src="https://latex.codecogs.com/png.latex?%20D(y_%7Bi1%7D,%20y_%7Bi2%7D,%20...,%20y_%7BiT%7D%20%7C%20X_i,%20v_i)%20=%20%5Cprod_%7Bi=1%7D%5ET%20D(y_%7Bit%7D%20%7C%20X_i,%20v_i)%20"></p>
<p>This means that the distribution of joint distriubtion of <img src="https://latex.codecogs.com/png.latex?y_%7Bit%7D">’s can be modeled by each <img src="https://latex.codecogs.com/png.latex?y_%7Bit%7D"> independently, given <img src="https://latex.codecogs.com/png.latex?X_i"> and <img src="https://latex.codecogs.com/png.latex?v_i">. We need this in most nonlinear cases, but not in linear case.</p>
<p>Then we need to specify <img src="https://latex.codecogs.com/png.latex?D(v_i)%20%7C%20X_i">. The random effect assumption is saying <img src="https://latex.codecogs.com/png.latex?%20D(v_i%20%7C%20X_i)%20=%20D(v_i)%20">, that is, <img src="https://latex.codecogs.com/png.latex?v_i"> is independent of <img src="https://latex.codecogs.com/png.latex?X_i">. This is a strong assumption. We should try to avoid this assumption.</p>
<p>Now, most nonlinear models make the second assumption, in the panel setting, when we have <img src="https://latex.codecogs.com/png.latex?v_i">. That includes RE model, conditional logit. However, that does not include pooled probit/logit, or CRE with pooled logit/probit. That is why Wooldridge recommend using CRE with pooled probit.</p>
</section>
<section id="example-2" class="level3">
<h3 class="anchored" data-anchor-id="example-2">Example 2</h3>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># generate a binary outcome, high_homocide</span></span>
<span id="cb9-2"></span>
<span id="cb9-3">castle2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> castle2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb9-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;">high_homicide =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(l_homicide <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;">mean</span>(l_homicide, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</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;">0</span>))</span>
<span id="cb9-5"></span>
<span id="cb9-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># CRE pooled probit</span></span>
<span id="cb9-7">cre_probit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glm</span>(high_homicide <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> poverty <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> poverty_mean <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> l_police <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> l_police_mean <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;">factor</span>(year), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> castle2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">binomial</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">link =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"probit"</span>))</span>
<span id="cb9-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute Average Marginal Effects (AMEs)</span></span>
<span id="cb9-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(marginaleffects)</span>
<span id="cb9-10">ame_cre_probit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">avg_slopes</span>(cre_probit)</span>
<span id="cb9-11">ame_cre_probit</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
          Term    Contrast Estimate Std. Error       z Pr(&gt;|z|)   S     2.5 %
 l_police      dY/dX        0.01924     0.2622  0.0734   0.9415 0.1 -0.494686
 l_police_mean dY/dX       -0.00304     0.2824 -0.0108   0.9914 0.0 -0.556572
 poverty       dY/dX       -0.06477     0.0334 -1.9417   0.0522 4.3 -0.130144
 poverty_mean  dY/dX        0.06620     0.0341  1.9424   0.0521 4.3 -0.000597
 year          2001 - 2000  0.06596     0.0990  0.6659   0.5054 1.0 -0.128159
 year          2002 - 2000  0.09470     0.1019  0.9293   0.3527 1.5 -0.105016
 year          2003 - 2000  0.22958     0.1020  2.2518   0.0243 5.4  0.029753
 year          2004 - 2000  0.15492     0.1082  1.4315   0.1523 2.7 -0.057193
 year          2005 - 2000  0.22388     0.1133  1.9766   0.0481 4.4  0.001880
 year          2006 - 2000  0.26184     0.1068  2.4526   0.0142 6.1  0.052595
 year          2007 - 2000  0.24899     0.1048  2.3756   0.0175 5.8  0.043564
 year          2008 - 2000 -0.03718     0.1099 -0.3383   0.7352 0.4 -0.252646
 year          2009 - 2000 -0.02471     0.1345 -0.1837   0.8542 0.2 -0.288280
 year          2010 - 2000 -0.21753     0.1186 -1.8339   0.0667 3.9 -0.450000
   97.5 %
 0.533162
 0.550499
 0.000608
 0.132987
 0.260070
 0.294406
 0.429403
 0.367039
 0.445887
 0.471080
 0.454421
 0.178276
 0.238865
 0.014950

Type: response</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;"># linear model</span></span>
<span id="cb11-2">cre_lm <span class="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>(high_homicide <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> poverty <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> poverty_mean <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> l_police <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> l_police_mean <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;">factor</span>(year), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> castle2)</span>
<span id="cb11-3">ame_lm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">avg_slopes</span>(cre_lm)</span>
<span id="cb11-4">ame_lm</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
          Term    Contrast Estimate Std. Error       z Pr(&gt;|z|)   S    2.5 %
 l_police      dY/dX        0.01234     0.2666  0.0463   0.9631 0.1 -0.51010
 l_police_mean dY/dX        0.00427     0.2865  0.0149   0.9881 0.0 -0.55724
 poverty       dY/dX       -0.06406     0.0339 -1.8891   0.0589 4.1 -0.13053
 poverty_mean  dY/dX        0.06578     0.0347  1.8975   0.0578 4.1 -0.00217
 year          2001 - 2000  0.06384     0.0965  0.6618   0.5081 1.0 -0.12522
 year          2002 - 2000  0.09082     0.0994  0.9134   0.3610 1.5 -0.10406
 year          2003 - 2000  0.22452     0.1016  2.2108   0.0270 5.2  0.02548
 year          2004 - 2000  0.14789     0.1064  1.3898   0.1646 2.6 -0.06068
 year          2005 - 2000  0.21659     0.1139  1.9023   0.0571 4.1 -0.00656
 year          2006 - 2000  0.25687     0.1085  2.3677   0.0179 5.8  0.04423
 year          2007 - 2000  0.24413     0.1056  2.3108   0.0208 5.6  0.03706
 year          2008 - 2000 -0.04737     0.1074 -0.4410   0.6592 0.6 -0.25794
 year          2009 - 2000 -0.03438     0.1300 -0.2645   0.7914 0.3 -0.28910
 year          2010 - 2000 -0.20933     0.1245 -1.6814   0.0927 3.4 -0.45335
 97.5 %
 0.5348
 0.5658
 0.0024
 0.1337
 0.2529
 0.2857
 0.4236
 0.3565
 0.4397
 0.4695
 0.4512
 0.1632
 0.2203
 0.0347

Type: response</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;"># CRE RE probit</span></span>
<span id="cb13-2">cre_re_probit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glmer</span>(high_homicide <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> poverty <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> poverty_mean <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> l_police <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> l_police_mean <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;">factor</span>(year) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>  (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> state), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> castle2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">binomial</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">link =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"probit"</span>))</span>
<span id="cb13-3">ame_cre_re_probit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">avg_slopes</span>(cre_re_probit)</span>
<span id="cb13-4">ame_cre_re_probit</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
          Term    Contrast Estimate Std. Error       z Pr(&gt;|z|)   S    2.5 %
 l_police      dY/dX        0.01927     0.2499  0.0771   0.9385 0.1 -0.47055
 l_police_mean dY/dX       -0.00307     0.2720 -0.0113   0.9910 0.0 -0.53625
 poverty       dY/dX       -0.06477     0.0336 -1.9275   0.0539 4.2 -0.13063
 poverty_mean  dY/dX        0.06619     0.0343  1.9285   0.0538 4.2 -0.00108
 year          2001 - 2000  0.06595     0.0995  0.6631   0.5072 1.0 -0.12898
 year          2002 - 2000  0.09469     0.1021  0.9279   0.3535 1.5 -0.10532
 year          2003 - 2000  0.22958     0.1018  2.2554   0.0241 5.4  0.03007
 year          2004 - 2000  0.15492     0.1085  1.4278   0.1534 2.7 -0.05775
 year          2005 - 2000  0.22388     0.1136  1.9714   0.0487 4.4  0.00130
 year          2006 - 2000  0.26184     0.1068  2.4524   0.0142 6.1  0.05258
 year          2007 - 2000  0.24899     0.1047  2.3784   0.0174 5.8  0.04380
 year          2008 - 2000 -0.03719     0.1101 -0.3378   0.7355 0.4 -0.25298
 year          2009 - 2000 -0.02471     0.1346 -0.1836   0.8543 0.2 -0.28848
 year          2010 - 2000 -0.21752     0.1188 -1.8308   0.0671 3.9 -0.45039
  97.5 %
 0.50910
 0.53011
 0.00109
 0.13347
 0.26089
 0.29471
 0.42908
 0.36759
 0.44646
 0.47110
 0.45418
 0.17860
 0.23906
 0.01535

Type: response</code></pre>
</div>
</div>
<p>Ultimately we are interested in the partial effect, therefore we can compare across models. In this case, we should prefer average marginal effect by CRE pooled probit.</p>


</section>
</section>

 ]]></description>
  <category>news</category>
  <category>code</category>
  <category>analysis</category>
  <guid>https://xiangao.github.io/posts/more_cre/</guid>
  <pubDate>Tue, 11 Nov 2025 05:00:00 GMT</pubDate>
  <media:content url="https://xiangao.github.io/posts/more_cre/image.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Using NumPyro</title>
  <dc:creator>Xiang Ao</dc:creator>
  <link>https://xiangao.github.io/posts/numpyro/</link>
  <description><![CDATA[ 






<p>I just discovered numpyro, which is a probabilistic programming library in Python. It can be coupled with Jax and is very powerful.</p>
<p>Basically you can set up a Bayesian model, and then let numpyro do the MCMC sampling and inference for you.</p>
<p>Here I am using a numpyro example and compare it with a traditional model (frequentist).</p>
<section id="example-1" class="level2">
<h2 class="anchored" data-anchor-id="example-1">Example 1</h2>
<p>I am using the numpyro example here: https://num.pyro.ai/en/stable/tutorials/bayesian_hierarchical_linear_regression.html</p>
<p>The data set is from https://www.kaggle.com/c/osic-pulmonary-fibrosis-progression</p>
<p>“Pulmonary fibrosis is a disorder with no known cause and no known cure, created by scarring of the lungs. In this competition, we were asked to predict a patient’s severity of decline in lung function. Lung function is assessed based on output from a spirometer, which measures the forced vital capacity (FVC), i.e.&nbsp;the volume of air exhaled.</p>
<p>In medical applications, it is useful to evaluate a model’s confidence in its decisions. Accordingly, the metric used to rank the teams was designed to reflect both the accuracy and certainty of each prediction.”</p>
<p>I read it in R first.</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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># read in csv file</span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(readr)</span>
<span id="cb1-4"></span>
<span id="cb1-5">data <span class="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;">"./osic_pulmonary_fibrosis.csv"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(Patient, Weeks)</span>
<span id="cb1-7"></span>
<span id="cb1-8">data</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1,549 × 7
   Patient                   Weeks   FVC Percent   Age Sex   SmokingStatus
   &lt;chr&gt;                     &lt;dbl&gt; &lt;dbl&gt;   &lt;dbl&gt; &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt;        
 1 ID00007637202177411956430    -4  2315    58.3    79 Male  Ex-smoker    
 2 ID00007637202177411956430     5  2214    55.7    79 Male  Ex-smoker    
 3 ID00007637202177411956430     7  2061    51.9    79 Male  Ex-smoker    
 4 ID00007637202177411956430     9  2144    54.0    79 Male  Ex-smoker    
 5 ID00007637202177411956430    11  2069    52.1    79 Male  Ex-smoker    
 6 ID00007637202177411956430    17  2101    52.9    79 Male  Ex-smoker    
 7 ID00007637202177411956430    29  2000    50.3    79 Male  Ex-smoker    
 8 ID00007637202177411956430    41  2064    51.9    79 Male  Ex-smoker    
 9 ID00007637202177411956430    57  2057    51.8    79 Male  Ex-smoker    
10 ID00009637202177434476278     8  3660    85.3    69 Male  Ex-smoker    
# ℹ 1,539 more rows</code></pre>
</div>
</div>
<section id="a-random-effect-model" class="level3">
<h3 class="anchored" data-anchor-id="a-random-effect-model">a random effect model</h3>
<p>We’d do a random effect model on intercept and slope of weeks, with a linear trend in weeks.</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;"># a random effect model with FVC as DV, and a linear time trend, with random effect on Patient</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lme4)</span>
<span id="cb3-3">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;">lmer</span>(FVC <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Weeks <span 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> Weeks <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> Patient), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data)</span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(reg1)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Linear mixed model fit by REML ['lmerMod']
Formula: FVC ~ Weeks + (1 + Weeks | Patient)
   Data: data

REML criterion at convergence: 20891.4

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-9.3209 -0.4257  0.0063  0.4458  5.6990 

Random effects:
 Groups   Name        Variance  Std.Dev. Corr 
 Patient  (Intercept) 686691.11 828.668       
          Weeks           25.88   5.087  -0.14
 Residual              18592.11 136.353       
Number of obs: 1549, groups:  Patient, 176

Fixed effects:
             Estimate Std. Error t value
(Intercept) 2810.2843    62.9497  44.643
Weeks         -4.2629     0.4377  -9.739

Correlation of Fixed Effects:
      (Intr)
Weeks -0.174</code></pre>
</div>
</div>
<p>Then we do the same with a Bayesian hierarchical model. Should have similar results.</p>
</section>
<section id="hierarchical-model-in-numpyro" class="level3">
<h3 class="anchored" data-anchor-id="hierarchical-model-in-numpyro">hierarchical model in numpyro</h3>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb5-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb5-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb5-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> seaborn <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> sns</span>
<span id="cb5-5"></span>
<span id="cb5-6">train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.read_csv(</span>
<span id="cb5-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://gist.githubusercontent.com/ucals/"</span></span>
<span id="cb5-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2cf9d101992cb1b78c2cdd6e3bac6a4b/raw/"</span></span>
<span id="cb5-9">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"43034c39052dcf97d4b894d2ec1bc3f90f3623d9/"</span></span>
<span id="cb5-10">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"osic_pulmonary_fibrosis.csv"</span></span>
<span id="cb5-11">)</span>
<span id="cb5-12"></span>
<span id="cb5-13"></span>
<span id="cb5-14"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> jax <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> random</span>
<span id="cb5-15"></span>
<span id="cb5-16"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpyro</span>
<span id="cb5-17"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpyro.distributions <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> dist</span>
<span id="cb5-18"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> numpyro.infer <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> MCMC, NUTS, Predictive</span>
<span id="cb5-19"></span>
<span id="cb5-20"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">assert</span> numpyro.__version__.startswith(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0.19.0"</span>)</span>
<span id="cb5-21"></span>
<span id="cb5-22"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> model(patient_code, Weeks, FVC_obs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>):</span>
<span id="cb5-23">    μ_α <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> numpyro.sample(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"μ_α"</span>, dist.Normal(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">500.0</span>))</span>
<span id="cb5-24">    σ_α <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> numpyro.sample(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"σ_α"</span>, dist.HalfNormal(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100.0</span>))</span>
<span id="cb5-25">    μ_β <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> numpyro.sample(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"μ_β"</span>, dist.Normal(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>))</span>
<span id="cb5-26">    σ_β <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> numpyro.sample(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"σ_β"</span>, dist.HalfNormal(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>))</span>
<span id="cb5-27"></span>
<span id="cb5-28">    n_patients <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(np.unique(patient_code))</span>
<span id="cb5-29"></span>
<span id="cb5-30">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> numpyro.plate(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plate_i"</span>, n_patients):</span>
<span id="cb5-31">        α <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> numpyro.sample(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"α"</span>, dist.Normal(μ_α, σ_α))</span>
<span id="cb5-32">        β <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> numpyro.sample(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"β"</span>, dist.Normal(μ_β, σ_β))</span>
<span id="cb5-33"></span>
<span id="cb5-34">    σ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> numpyro.sample(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"σ"</span>, dist.HalfNormal(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100.0</span>))</span>
<span id="cb5-35">    FVC_est <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> α[patient_code] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> β[patient_code] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Weeks</span>
<span id="cb5-36"></span>
<span id="cb5-37">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> numpyro.plate(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(patient_code)):</span>
<span id="cb5-38">        numpyro.sample(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"obs"</span>, dist.Normal(FVC_est, σ), obs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>FVC_obs)</span>
<span id="cb5-39">        </span>
<span id="cb5-40"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.preprocessing <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> LabelEncoder</span>
<span id="cb5-41"></span>
<span id="cb5-42">patient_encoder <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> LabelEncoder()</span>
<span id="cb5-43">train[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"patient_code"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> patient_encoder.fit_transform(train[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Patient"</span>].values)</span>
<span id="cb5-44"></span>
<span id="cb5-45">FVC_obs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FVC"</span>].values</span>
<span id="cb5-46">Weeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Weeks"</span>].values</span>
<span id="cb5-47">patient_code <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"patient_code"</span>].values</span>
<span id="cb5-48"></span>
<span id="cb5-49"></span>
<span id="cb5-50">nuts_kernel <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> NUTS(model)</span>
<span id="cb5-51"></span>
<span id="cb5-52">mcmc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> MCMC(nuts_kernel, num_samples<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>, num_warmup<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>, progress_bar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb5-53">rng_key <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> random.PRNGKey(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb5-54">mcmc.run(rng_key, patient_code, Weeks, FVC_obs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>FVC_obs)</span>
<span id="cb5-55"></span>
<span id="cb5-56">posterior_samples <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mcmc.get_samples()</span>
<span id="cb5-57">mcmc.print_summary() </span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
                mean       std    median      5.0%     95.0%     n_eff     r_hat
      α[0]   2181.96     60.74   2182.26   2080.02   2276.12   3478.15      1.00
      α[1]   3783.49     73.96   3784.85   3667.70   3913.58   4928.66      1.00
      α[2]   3265.39     58.09   3264.75   3175.63   3366.59   4088.11      1.00
      α[3]   3478.90     66.67   3477.34   3371.61   3589.76   3993.87      1.00
      α[4]   3700.88    118.15   3699.46   3507.03   3892.74   4173.49      1.00
      α[5]   3724.77     59.33   3725.87   3623.35   3817.91   5760.40      1.00
      α[6]   3050.36     87.13   3052.33   2901.09   3187.95   4301.39      1.00
      α[7]   2151.55     78.65   2149.32   2031.82   2287.03   4117.25      1.00
      α[8]   2344.04     89.13   2344.16   2197.93   2493.25   4350.38      1.00
      α[9]   1496.10     59.90   1495.72   1400.21   1595.97   3821.66      1.00
     α[10]   3147.59     59.34   3146.73   3054.35   3247.87   3823.34      1.00
     α[11]   3061.16    134.88   3059.77   2859.88   3302.93   4108.39      1.00
     α[12]   2776.40     81.49   2777.66   2638.22   2904.36   5336.77      1.00
     α[13]   2681.60    125.34   2682.50   2470.12   2876.24   5991.18      1.00
     α[14]   5195.74    116.71   5196.11   5008.86   5385.35   4844.13      1.00
     α[15]   2780.42     75.71   2780.33   2663.12   2909.89   4404.98      1.00
     α[16]   3924.87     78.16   3923.05   3795.71   4053.21   4959.80      1.00
     α[17]   3613.71     61.09   3613.50   3513.57   3715.78   3498.32      1.00
     α[18]   3276.12     66.24   3276.51   3162.19   3382.03   2883.28      1.00
     α[19]   1477.57     69.84   1478.19   1352.44   1579.33   3962.45      1.00
     α[20]   1703.49     56.27   1704.27   1612.68   1796.76   2952.87      1.00
     α[21]   2984.97    218.20   2988.79   2625.00   3337.65   5150.51      1.00
     α[22]   3392.43     93.03   3391.44   3252.03   3554.48   3935.83      1.00
     α[23]   3637.04    103.11   3639.29   3468.56   3799.16   4211.06      1.00
     α[24]   2483.06    144.52   2483.37   2247.02   2721.52   5069.97      1.00
     α[25]   3453.93     66.54   3455.23   3345.72   3565.21   4795.24      1.00
     α[26]   2834.53     75.77   2834.91   2716.25   2961.84   4413.27      1.00
     α[27]   2419.90     83.80   2420.70   2289.84   2568.34   4094.23      1.00
     α[28]   2495.48     68.58   2494.98   2379.58   2601.87   5322.98      1.00
     α[29]   1828.49    109.39   1829.96   1650.90   2013.15   3854.51      1.00
     α[30]   2387.46     59.38   2387.19   2291.45   2484.52   4065.98      1.00
     α[31]   3258.15     64.09   3259.99   3156.46   3366.40   4339.93      1.00
     α[32]   1885.49     63.12   1886.79   1779.17   1986.83   3903.91      1.00
     α[33]   2946.67    109.14   2945.00   2761.04   3113.03   4230.53      1.00
     α[34]   3373.80     62.44   3373.85   3277.11   3478.82   4635.49      1.00
     α[35]   2537.18     72.11   2536.82   2427.51   2662.56   4037.66      1.00
     α[36]   3219.40     92.97   3219.59   3067.16   3361.41   3682.34      1.00
     α[37]   3578.86     64.44   3579.33   3473.77   3685.27   4499.79      1.00
     α[38]   4806.80     67.82   4806.16   4704.59   4926.04   4021.18      1.00
     α[39]   3142.70    128.24   3140.92   2944.80   3357.44   4483.11      1.00
     α[40]   2303.84     64.02   2304.47   2191.04   2403.73   4225.49      1.00
     α[41]   2567.85    104.45   2568.37   2408.97   2752.63   4547.57      1.00
     α[42]   2746.64    132.81   2749.36   2525.71   2960.18   4787.09      1.00
     α[43]   2856.82     79.62   2858.73   2734.38   2990.30   5177.49      1.00
     α[44]   2553.31    161.52   2556.90   2279.42   2807.01   4763.51      1.00
     α[45]   2028.61     99.29   2029.85   1870.11   2198.04   4007.81      1.00
     α[46]   2415.84     85.37   2412.85   2274.98   2551.99   4389.82      1.00
     α[47]   2559.61     91.70   2560.16   2415.14   2713.06   4703.63      1.00
     α[48]   2903.48     67.59   2903.82   2783.52   3003.76   3074.21      1.00
     α[49]   2916.36     61.66   2917.28   2815.96   3018.28   3231.64      1.00
     α[50]   2433.25     62.36   2433.10   2333.02   2535.01   4112.43      1.00
     α[51]   1874.57    149.88   1874.53   1636.37   2125.48   4466.82      1.00
     α[52]   3885.27    152.80   3884.64   3634.18   4129.37   4764.01      1.00
     α[53]   2453.30     74.16   2454.34   2332.25   2572.95   4032.64      1.00
     α[54]   2289.13     98.67   2291.88   2113.48   2432.82   4228.26      1.00
     α[55]   1968.45    148.75   1967.96   1727.68   2223.98   4738.61      1.00
     α[56]   2448.41    168.45   2449.80   2177.20   2731.69   4271.57      1.00
     α[57]   2284.98     61.21   2285.08   2188.96   2386.97   5302.78      1.00
     α[58]   1690.54     75.88   1692.69   1572.17   1820.44   4384.45      1.00
     α[59]   3652.09    175.83   3651.85   3371.15   3952.49   5137.58      1.00
     α[60]   2659.02     72.32   2658.87   2540.28   2776.74   4343.19      1.00
     α[61]   3162.24     59.04   3164.49   3066.44   3260.57   3628.39      1.00
     α[62]   2697.13     68.00   2697.47   2589.83   2809.31   3821.55      1.00
     α[63]   3943.73    119.42   3946.85   3729.60   4121.03   4423.43      1.00
     α[64]   3339.19     64.10   3340.30   3229.28   3435.80   3789.94      1.00
     α[65]   4422.48     85.06   4424.31   4280.59   4564.35   6713.01      1.00
     α[66]   4017.49     92.23   4016.28   3868.26   4169.53   4683.90      1.00
     α[67]   4089.30     82.06   4091.63   3959.81   4223.71   4983.39      1.00
     α[68]   3549.15    138.06   3549.95   3327.27   3776.16   3890.86      1.00
     α[69]   2901.59     73.02   2900.42   2790.57   3030.32   4683.16      1.00
     α[70]   2181.65    219.45   2175.94   1841.71   2541.47   4550.12      1.00
     α[71]   4770.96     74.12   4771.11   4654.07   4898.68   4209.58      1.00
     α[72]   3148.67     68.49   3149.51   3038.27   3261.98   4719.43      1.00
     α[73]   2568.25     84.10   2568.06   2422.14   2691.78   4620.57      1.00
     α[74]   2071.70    135.79   2069.03   1851.62   2289.57   4258.39      1.00
     α[75]   2805.05    126.29   2808.18   2587.92   3004.50   4962.74      1.00
     α[76]   2851.84     84.64   2854.08   2708.30   2982.13   4178.09      1.00
     α[77]   3069.15     75.47   3067.51   2947.36   3191.41   4765.39      1.00
     α[78]   2473.79    134.71   2472.45   2246.52   2689.86   4316.09      1.00
     α[79]   3176.65    112.70   3181.20   2992.57   3366.85   4780.32      1.00
     α[80]   2918.38     75.56   2919.69   2795.13   3045.32   4288.53      1.00
     α[81]   4380.73     73.78   4380.60   4256.85   4500.40   4449.85      1.00
     α[82]   1901.83     73.17   1900.47   1776.18   2016.42   3629.52      1.00
     α[83]   3939.29     74.29   3939.85   3822.65   4061.64   4840.37      1.00
     α[84]   2302.36     64.27   2301.19   2197.80   2406.84   3878.34      1.00
     α[85]   2803.07     66.43   2803.56   2706.09   2922.83   4427.60      1.00
     α[86]   4430.48     94.38   4430.86   4275.12   4581.68   4876.03      1.00
     α[87]   2862.12    122.95   2862.95   2663.85   3066.27   5159.40      1.00
     α[88]   2831.12     59.21   2830.93   2738.42   2929.85   3505.62      1.00
     α[89]   3174.82     69.33   3175.22   3065.44   3291.17   2897.17      1.00
     α[90]   2907.47     63.66   2908.82   2807.63   3013.57   3894.72      1.00
     α[91]   2321.38     73.67   2321.39   2197.67   2435.79   4540.13      1.00
     α[92]   2534.82     92.63   2533.36   2380.92   2685.90   4466.08      1.00
     α[93]   5897.89     61.90   5898.57   5798.89   6001.60   3792.77      1.00
     α[94]   1493.36     77.54   1494.47   1357.99   1613.48   4334.43      1.00
     α[95]   2553.11     64.25   2552.00   2446.32   2654.63   5198.04      1.00
     α[96]   2703.26     61.69   2704.33   2597.79   2800.33   3588.30      1.00
     α[97]   1254.25     84.35   1254.03   1111.30   1386.08   4096.89      1.00
     α[98]   1977.65     81.96   1975.93   1850.62   2117.53   4584.07      1.00
     α[99]   3626.00     94.04   3624.16   3453.35   3765.02   6152.51      1.00
    α[100]   1960.98     74.47   1960.78   1839.09   2081.26   5508.20      1.00
    α[101]   3618.52     56.55   3618.62   3520.58   3705.30   3322.28      1.00
    α[102]   1974.77     65.12   1974.23   1860.57   2073.28   3503.83      1.00
    α[103]   2105.64    128.71   2105.25   1886.33   2306.98   6616.46      1.00
    α[104]   2910.32     68.85   2910.23   2789.15   3014.76   4584.25      1.00
    α[105]   1401.37     63.49   1401.44   1303.52   1512.56   4920.60      1.00
    α[106]   1840.26     65.45   1840.61   1734.82   1946.79   4199.05      1.00
    α[107]   1381.50     80.50   1379.99   1243.57   1515.55   5144.95      1.00
    α[108]   3918.88    134.89   3916.91   3674.47   4118.55   4558.49      1.00
    α[109]   2231.49    103.29   2231.06   2077.49   2414.70   4677.99      1.00
    α[110]   1565.15     66.49   1567.76   1456.32   1674.67   3762.40      1.00
    α[111]   2075.41     87.26   2076.29   1941.11   2223.85   4193.44      1.00
    α[112]   1065.46     73.31   1067.54    938.10   1176.44   4130.45      1.00
    α[113]   2863.31     68.76   2865.82   2749.60   2974.66   4549.16      1.00
    α[114]   2517.66    146.55   2517.04   2289.99   2768.97   4708.89      1.00
    α[115]   2973.36     73.24   2973.90   2847.22   3086.66   4239.57      1.00
    α[116]   4160.73     70.88   4160.89   4043.80   4277.77   4756.33      1.00
    α[117]   1981.23     59.08   1981.86   1887.80   2081.02   3847.90      1.00
    α[118]   1402.20     63.46   1401.69   1303.28   1512.53   3938.85      1.00
    α[119]   2096.95    108.78   2098.23   1910.34   2266.26   4640.21      1.00
    α[120]   2363.06     80.42   2361.92   2237.11   2505.73   3919.36      1.00
    α[121]   4055.78     59.60   4055.97   3955.94   4151.83   3641.06      1.00
    α[122]   2289.47     66.27   2286.09   2188.57   2405.43   4131.95      1.00
    α[123]   2191.83     74.29   2192.15   2071.21   2315.32   3979.08      1.00
    α[124]   1923.23     66.87   1922.47   1815.24   2032.27   3807.17      1.00
    α[125]   2250.75    100.50   2249.12   2086.16   2409.58   5053.45      1.00
    α[126]   2915.61     75.51   2915.58   2794.77   3037.53   5279.26      1.00
    α[127]   3239.20     64.65   3237.52   3144.70   3354.14   4017.07      1.00
    α[128]   3222.20     66.91   3222.45   3109.25   3328.30   3365.91      1.00
    α[129]   1859.73     61.92   1858.81   1758.87   1964.10   4568.50      1.00
    α[130]   2614.03    115.54   2615.68   2420.21   2804.06   4947.98      1.00
    α[131]   3910.90     81.49   3911.47   3779.04   4048.18   4029.07      1.00
    α[132]   2618.84     63.93   2618.52   2509.26   2720.43   4529.40      1.00
    α[133]   1617.22    126.03   1614.94   1406.29   1819.48   4226.61      1.00
    α[134]   2891.23    130.63   2892.29   2684.80   3108.29   4700.87      1.00
    α[135]   3080.86    116.70   3078.24   2867.59   3246.76   5356.82      1.00
    α[136]   2736.10     65.37   2737.26   2632.36   2845.87   4798.27      1.00
    α[137]   2616.46     70.43   2617.43   2499.96   2731.58   4693.34      1.00
    α[138]   3931.41    113.71   3931.37   3744.14   4120.55   6092.02      1.00
    α[139]   3540.65     66.25   3541.71   3435.03   3652.82   4975.09      1.00
    α[140]   2319.48     64.50   2319.73   2214.62   2429.24   3795.68      1.00
    α[141]   2933.86    114.23   2931.55   2746.68   3115.25   4674.73      1.00
    α[142]   2405.33     99.42   2404.73   2248.27   2568.26   4378.19      1.00
    α[143]   1608.70     94.94   1607.20   1452.57   1764.13   4365.56      1.00
    α[144]   2065.78     66.48   2066.25   1962.33   2177.87   4630.61      1.00
    α[145]   2608.69     80.14   2607.56   2476.55   2740.32   5272.79      1.00
    α[146]   4202.96    105.58   4201.76   4034.35   4378.06   5225.38      1.00
    α[147]   2052.41     75.69   2052.85   1930.84   2175.75   4737.47      1.00
    α[148]   2888.84     66.31   2888.21   2785.91   3000.87   4069.44      1.00
    α[149]   3538.52    130.67   3538.85   3311.72   3734.16   5014.65      1.00
    α[150]   2671.02    152.86   2672.30   2421.11   2920.64   5058.42      1.00
    α[151]   1734.95    130.10   1735.16   1529.26   1966.11   4134.36      1.00
    α[152]   3036.62     77.43   3037.41   2913.63   3163.50   5443.08      1.00
    α[153]   2280.62     80.84   2280.98   2157.90   2419.70   6519.05      1.00
    α[154]   2535.44     67.69   2535.98   2424.18   2645.61   4251.41      1.00
    α[155]   4401.42    142.25   4403.37   4172.91   4646.13   4468.32      1.00
    α[156]   3468.31    103.94   3469.87   3318.57   3657.42   4614.79      1.00
    α[157]   2300.55     74.07   2301.65   2186.87   2430.85   4202.74      1.00
    α[158]   3354.23    157.23   3350.28   3104.44   3608.42   4578.01      1.00
    α[159]   3142.29     62.26   3142.13   3032.66   3240.83   4841.13      1.00
    α[160]   2204.44     70.52   2202.80   2089.87   2320.99   4692.09      1.00
    α[161]   3737.23     61.33   3736.79   3634.25   3833.27   3300.82      1.00
    α[162]   3000.90     86.93   3000.32   2858.64   3146.88   4496.44      1.00
    α[163]   3965.14    100.36   3964.14   3793.29   4120.08   3782.56      1.00
    α[164]   2020.35    142.58   2021.99   1791.25   2264.88   5086.22      1.00
    α[165]   1580.13     64.85   1579.95   1486.46   1700.85   2607.56      1.00
    α[166]   3250.36     94.84   3251.18   3103.08   3407.85   4652.29      1.00
    α[167]   2455.41     89.25   2455.88   2307.87   2606.59   4002.42      1.00
    α[168]   1770.46     70.57   1770.27   1659.72   1888.74   4205.60      1.00
    α[169]   2892.62     76.82   2890.41   2774.02   3024.54   3921.34      1.00
    α[170]   3357.47     69.56   3356.52   3252.30   3478.19   4614.54      1.00
    α[171]   2836.92     67.01   2839.11   2729.38   2944.73   4768.27      1.00
    α[172]   2828.49     81.06   2828.37   2687.76   2949.13   4523.36      1.00
    α[173]   1989.75     77.96   1989.84   1854.52   2106.57   3484.11      1.00
    α[174]   3007.70     85.53   3010.05   2869.75   3152.74   4488.10      1.00
    α[175]   2931.63     67.48   2931.62   2813.62   3032.52   4880.61      1.00
      β[0]     -3.49      2.13     -3.52     -6.86      0.11   3777.86      1.00
      β[1]     -8.02      2.36     -8.00    -11.68     -4.02   4356.88      1.00
      β[2]    -14.27      2.28    -14.26    -17.84    -10.21   3486.20      1.00
      β[3]     -4.27      2.31     -4.27     -7.77     -0.22   4751.57      1.00
      β[4]     -7.13      2.19     -7.13    -10.57     -3.43   4758.71      1.00
      β[5]    -11.69      2.32    -11.69    -15.47     -7.84   4411.74      1.00
      β[6]     -6.71      2.27     -6.70    -10.40     -3.02   4800.55      1.00
      β[7]     -5.62      2.20     -5.63     -9.54     -2.30   4401.94      1.00
      β[8]     -6.81      2.27     -6.88    -10.33     -2.83   4538.73      1.00
      β[9]     -3.06      2.28     -3.04     -6.67      0.83   5195.12      1.00
     β[10]     -9.32      2.33     -9.33    -13.17     -5.60   3699.40      1.00
     β[11]     -3.07      2.15     -3.07     -6.56      0.49   4320.79      1.00
     β[12]     -5.17      2.26     -5.14     -8.90     -1.54   4824.94      1.00
     β[13]     -4.64      2.92     -4.56     -9.56     -0.14   4782.43      1.00
     β[14]    -11.04      2.19    -11.04    -14.47     -7.42   4283.43      1.00
     β[15]     -8.45      2.30     -8.48    -12.12     -4.57   4260.51      1.00
     β[16]     -1.18      2.36     -1.12     -5.03      2.76   4967.28      1.00
     β[17]     -0.16      2.14     -0.19     -3.62      3.29   3726.29      1.00
     β[18]     -6.65      3.82     -6.71    -13.21     -0.86   3985.31      1.00
     β[19]     -2.16      2.20     -2.18     -5.76      1.43   3160.09      1.00
     β[20]     -3.71      2.20     -3.68     -7.30     -0.02   3877.82      1.00
     β[21]    -11.26      3.68    -11.34    -17.13     -5.13   4327.97      1.00
     β[22]     -5.95      2.40     -5.94     -9.97     -2.07   4703.96      1.00
     β[23]      5.92      2.37      5.92      2.08      9.67   4153.01      1.00
     β[24]     -4.07      2.26     -4.07     -7.76     -0.37   5563.09      1.00
     β[25]     -5.09      3.02     -5.11    -10.29     -0.54   3885.94      1.00
     β[26]     -6.07      2.34     -6.07    -10.20     -2.40   5647.53      1.00
     β[27]     -2.60      2.13     -2.55     -6.46      0.69   4187.02      1.00
     β[28]    -13.06      3.09    -13.13    -18.11     -7.93   4795.54      1.00
     β[29]     -7.67      2.38     -7.71    -11.57     -3.82   4117.66      1.00
     β[30]      8.04      2.25      8.04      4.13     11.55   4600.00      1.00
     β[31]      2.45      2.26      2.47     -1.03      6.30   4007.59      1.00
     β[32]     -6.58      2.23     -6.59    -10.21     -2.82   3832.25      1.00
     β[33]     -0.80      2.37     -0.81     -4.99      2.80   4361.64      1.00
     β[34]     -0.83      2.16     -0.83     -4.16      2.91   4025.73      1.00
     β[35]    -10.61      2.40    -10.61    -14.60     -6.78   3563.09      1.00
     β[36]     -5.19      2.17     -5.18     -8.89     -1.93   3299.45      1.00
     β[37]     -0.25      2.42     -0.19     -4.01      3.83   3704.36      1.00
     β[38]     -2.73      2.35     -2.70     -6.32      1.24   4342.30      1.00
     β[39]     -3.52      1.84     -3.51     -6.77     -0.84   3976.06      1.00
     β[40]     -1.34      2.30     -1.37     -5.12      2.42   3742.77      1.00
     β[41]    -11.93      2.96    -11.83    -16.93     -7.23   4682.23      1.00
     β[42]     -5.60      1.96     -5.62     -8.69     -2.24   4732.31      1.00
     β[43]     -4.12      2.35     -4.08     -7.66     -0.08   4616.45      1.00
     β[44]     -5.79      2.20     -5.80     -9.27     -2.09   4924.40      1.00
     β[45]    -19.96      3.06    -19.91    -24.63    -14.61   3679.62      1.00
     β[46]     -7.46      3.85     -7.50    -13.75     -1.59   4155.61      1.00
     β[47]     -5.62      2.99     -5.66    -10.20     -0.32   4985.98      1.00
     β[48]      0.74      2.23      0.77     -2.89      4.42   3883.18      1.00
     β[49]     -0.05      1.96     -0.03     -3.14      3.21   5014.63      1.00
     β[50]     -6.66      2.21     -6.70    -10.32     -3.04   3677.17      1.00
     β[51]     -4.38      2.23     -4.35     -8.12     -0.87   4395.64      1.00
     β[52]     -6.26      2.33     -6.21    -10.11     -2.64   4683.23      1.00
     β[53]    -11.29      3.00    -11.30    -16.50     -6.57   4172.36      1.00
     β[54]      0.53      2.20      0.49     -2.74      4.33   3909.65      1.00
     β[55]     -4.76      2.25     -4.77     -8.53     -1.01   4825.57      1.00
     β[56]     -6.44      3.85     -6.41    -12.56      0.06   4113.27      1.00
     β[57]     -2.80      2.27     -2.74     -6.67      0.70   3687.04      1.00
     β[58]     -7.49      2.26     -7.51    -10.84     -3.50   4276.53      1.00
     β[59]     -4.83      2.15     -4.82     -8.31     -1.30   5575.94      1.00
     β[60]     -1.40      2.24     -1.41     -4.98      2.35   4515.19      1.00
     β[61]     -3.11      2.28     -3.11     -6.95      0.55   4081.08      1.00
     β[62]     -8.59      2.31     -8.61    -12.35     -4.93   3377.48      1.00
     β[63]     -0.72      2.21     -0.73     -4.17      3.04   4547.00      1.00
     β[64]     -1.68      2.26     -1.63     -5.77      1.66   3385.73      1.00
     β[65]     -3.64      2.22     -3.64     -7.07      0.12   5571.42      1.00
     β[66]     -3.56      2.21     -3.63     -7.35     -0.16   4991.63      1.00
     β[67]     -1.55      1.90     -1.54     -4.73      1.41   4627.93      1.00
     β[68]     -7.49      2.09     -7.48    -11.06     -4.34   4079.94      1.00
     β[69]     -1.36      2.33     -1.39     -5.11      2.52   4569.82      1.00
     β[70]     -0.29      2.23     -0.25     -4.25      2.95   4242.55      1.00
     β[71]    -10.03      1.98    -10.06    -13.32     -6.94   4442.15      1.00
     β[72]     -7.53      2.24     -7.46    -11.36     -3.98   5482.66      1.00
     β[73]     -3.37      2.34     -3.39     -7.11      0.40   5019.89      1.00
     β[74]      2.41      2.23      2.45     -1.18      6.12   4508.04      1.00
     β[75]     -1.25      2.26     -1.33     -4.92      2.55   5646.07      1.00
     β[76]     -4.17      2.23     -4.17     -7.97     -0.66   5675.45      1.00
     β[77]     -4.32      2.26     -4.33     -8.17     -0.89   4923.77      1.00
     β[78]     -0.77      2.26     -0.75     -4.48      2.97   4750.75      1.00
     β[79]     -3.90      2.27     -3.89     -7.59     -0.15   4487.57      1.00
     β[80]    -12.61      2.64    -12.60    -17.07     -8.36   4246.54      1.00
     β[81]     -1.52      2.29     -1.45     -5.58      2.05   4651.25      1.00
     β[82]     -4.46      2.18     -4.50     -7.95     -0.89   4468.52      1.00
     β[83]     -1.38      2.24     -1.39     -4.98      2.40   5150.16      1.00
     β[84]     10.63      2.32     10.60      6.78     14.33   4505.47      1.00
     β[85]      0.58      2.27      0.59     -3.09      4.36   3539.17      1.00
     β[86]    -10.39      2.28    -10.40    -14.11     -6.87   4488.76      1.00
     β[87]     -3.61      2.29     -3.60     -7.36      0.10   5316.67      1.00
     β[88]     -0.30      2.22     -0.32     -3.80      3.35   3819.55      1.00
     β[89]     -1.93      2.32     -1.92     -5.96      1.63   4179.68      1.00
     β[90]     -2.15      2.37     -2.15     -5.91      1.70   3341.19      1.00
     β[91]     -3.70      2.26     -3.69     -7.61     -0.12   3904.55      1.00
     β[92]      2.18      2.20      2.17     -1.22      6.00   4908.78      1.00
     β[93]     -3.33      2.28     -3.41     -7.01      0.50   3571.76      1.00
     β[94]     -2.74      2.32     -2.75     -6.52      0.98   4606.86      1.00
     β[95]     -4.31      2.28     -4.40     -7.87     -0.61   4132.73      1.00
     β[96]     -0.62      2.29     -0.61     -4.24      3.25   3616.64      1.00
     β[97]     -7.27      2.28     -7.18    -11.10     -3.62   4140.66      1.00
     β[98]     -5.21      2.29     -5.23     -8.89     -1.43   5202.64      1.00
     β[99]     -3.25      1.91     -3.24     -6.55     -0.23   4539.26      1.00
    β[100]     -2.23      2.32     -2.18     -6.27      1.43   5201.07      1.00
    β[101]     -4.05      2.14     -4.01     -7.60     -0.70   3869.35      1.00
    β[102]     -3.24      2.97     -3.28     -7.89      1.76   3127.43      1.00
    β[103]      0.79      2.43      0.81     -2.78      5.17   6492.31      1.00
    β[104]     -4.63      2.39     -4.58     -8.28     -0.38   4463.70      1.00
    β[105]     -3.55      2.32     -3.48     -7.44      0.17   3883.25      1.00
    β[106]     -3.12      2.38     -3.04     -7.04      0.68   4317.92      1.00
    β[107]     -0.32      2.26     -0.27     -3.93      3.38   4878.10      1.00
    β[108]    -19.46      2.24    -19.44    -23.17    -15.80   3707.86      1.00
    β[109]     -5.55      2.08     -5.50     -8.91     -2.23   5596.44      1.00
    β[110]     -3.00      2.37     -2.99     -6.79      0.91   4362.65      1.00
    β[111]     -1.77      2.96     -1.76     -6.89      2.89   4287.54      1.00
    β[112]     -5.76      4.03     -5.67    -12.41      0.60   4568.87      1.00
    β[113]      0.30      2.26      0.29     -3.39      4.05   4201.45      1.00
    β[114]     -8.96      2.15     -8.93    -12.54     -5.55   4902.72      1.00
    β[115]     -6.70      3.75     -6.70    -12.80     -0.58   3675.94      1.00
    β[116]     -4.58      2.27     -4.57     -7.94     -0.57   4612.40      1.00
    β[117]     -0.47      2.21     -0.45     -4.30      2.92   4604.65      1.00
    β[118]     -0.88      2.17     -0.89     -4.45      2.67   3857.26      1.00
    β[119]      3.37      3.06      3.34     -1.63      8.40   4641.89      1.00
    β[120]     -7.61      2.76     -7.61    -12.08     -3.07   4584.45      1.00
    β[121]     -7.80      2.18     -7.84    -11.53     -4.31   4143.18      1.00
    β[122]     -4.47      2.28     -4.42     -8.62     -1.11   5003.13      1.00
    β[123]     -1.87      3.11     -1.85     -6.48      3.77   3837.84      1.00
    β[124]     -7.47      2.30     -7.42    -11.15     -3.56   3488.98      1.00
    β[125]     -2.96      2.30     -2.95     -6.51      0.86   4045.44      1.00
    β[126]     -4.62      2.32     -4.67     -8.37     -0.90   4501.75      1.00
    β[127]     -7.88      2.22     -7.87    -11.55     -4.17   4004.76      1.00
    β[128]     -4.28      2.31     -4.32     -7.78     -0.44   3548.34      1.00
    β[129]     -5.02      2.21     -5.08     -8.72     -1.32   3864.48      1.00
    β[130]     -9.61      2.21     -9.60    -13.21     -6.13   4645.20      1.00
    β[131]      5.26      2.23      5.33      1.71      9.07   3949.80      1.00
    β[132]     -5.85      2.23     -5.85     -9.48     -2.19   4969.66      1.00
    β[133]      0.48      3.14      0.54     -4.68      5.72   4701.96      1.00
    β[134]     -1.64      2.16     -1.65     -5.28      1.70   4533.16      1.00
    β[135]      1.34      2.32      1.36     -2.49      5.12   4581.77      1.00
    β[136]     -5.27      2.41     -5.27     -9.53     -1.62   4665.97      1.00
    β[137]     -5.41      2.89     -5.32    -10.61     -1.06   4848.17      1.00
    β[138]     -0.34      2.25     -0.35     -3.84      3.53   6135.90      1.00
    β[139]     -8.16      2.34     -8.12    -11.84     -4.21   4310.14      1.00
    β[140]     -3.98      2.25     -3.96     -7.82     -0.19   5176.38      1.00
    β[141]     -2.42      2.22     -2.46     -5.94      1.29   4938.04      1.00
    β[142]     -7.50      2.23     -7.48    -10.93     -3.83   5237.66      1.00
    β[143]     -1.22      1.95     -1.24     -4.48      1.89   4102.71      1.00
    β[144]     -6.33      3.78     -6.32    -12.02      0.48   3855.15      1.00
    β[145]      0.36      2.30      0.31     -3.04      4.50   5106.15      1.00
    β[146]      5.67      2.24      5.69      2.32      9.50   4880.28      1.00
    β[147]     -1.28      2.33     -1.32     -4.71      2.91   4092.35      1.00
    β[148]     -2.58      2.34     -2.60     -6.47      1.23   3386.65      1.00
    β[149]    -12.39      2.23    -12.40    -16.17     -8.91   5298.42      1.00
    β[150]     -6.77      3.07     -6.72    -11.85     -1.95   4858.98      1.00
    β[151]     -7.95      2.38     -7.92    -11.65     -3.74   4461.45      1.00
    β[152]     -2.49      2.33     -2.50     -6.22      1.21   3764.36      1.00
    β[153]     -1.30      2.37     -1.29     -5.04      2.67   6336.56      1.00
    β[154]     -3.86      2.20     -3.87     -7.43     -0.39   3279.35      1.00
    β[155]     -2.65      2.41     -2.66     -7.01      0.97   4424.41      1.00
    β[156]     -3.71      2.22     -3.71     -7.33     -0.15   4825.16      1.00
    β[157]     -8.63      3.94     -8.60    -14.57     -1.90   3967.71      1.00
    β[158]     -3.99      2.31     -3.98     -7.49     -0.07   4349.28      1.00
    β[159]     -4.94      1.98     -4.90     -8.41     -1.91   4700.91      1.00
    β[160]     -6.93      2.43     -6.99    -10.98     -2.99   4785.30      1.00
    β[161]     -3.47      2.13     -3.47     -7.00     -0.16   3972.92      1.00
    β[162]    -15.86      3.11    -15.80    -21.10    -10.84   4183.32      1.00
    β[163]     -0.94      1.87     -0.95     -4.08      2.08   4370.92      1.00
    β[164]     -2.01      2.22     -2.00     -5.44      1.98   5639.13      1.00
    β[165]     -2.00      2.22     -1.98     -5.55      1.68   3479.20      1.00
    β[166]     -4.23      2.27     -4.25     -7.71     -0.32   4338.94      1.00
    β[167]     -1.70      3.12     -1.79     -6.56      3.57   4373.63      1.00
    β[168]     -1.91      2.28     -1.90     -5.93      1.68   4487.30      1.00
    β[169]    -17.05      2.31    -17.07    -20.95    -13.41   4857.17      1.00
    β[170]     -1.69      2.27     -1.68     -5.50      1.95   4521.26      1.00
    β[171]     -2.54      2.31     -2.57     -6.12      1.41   4106.66      1.00
    β[172]     -1.94      1.91     -1.91     -4.93      1.32   3996.98      1.00
    β[173]     -4.88      2.92     -4.92     -9.76     -0.26   4223.72      1.00
    β[174]     -7.95      2.24     -7.97    -11.47     -4.07   4681.34      1.00
    β[175]     -1.66      2.25     -1.69     -4.95      2.32   3655.75      1.00
       μ_α   2774.79     54.96   2775.49   2686.53   2863.70   3474.50      1.00
       μ_β     -4.17      0.41     -4.17     -4.85     -3.51   2474.19      1.00
         σ    136.71      2.74    136.64    132.14    141.13   1906.90      1.00
       σ_α    723.12     31.96    722.08    672.81    774.22   3737.96      1.00
       σ_β      4.99      0.36      4.96      4.39      5.55   2482.51      1.00

Number of divergences: 0</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"></span>
<span id="cb7-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> arviz <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> az</span>
<span id="cb7-3"></span>
<span id="cb7-4">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> az.from_numpyro(mcmc)</span>
<span id="cb7-5">az.plot_trace(data, compact<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, figsize<span class="op" 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 class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://xiangao.github.io/posts/numpyro/index_files/figure-html/numpyro1-1.png" class="img-fluid figure-img" alt="Posterior predictive FVC trajectories for a sample of patients, with credible bands" width="1440"></p>
</figure>
</div>
</div>
</div>
<p>Let’s see how this is done:</p>
<p>The model is described by the following equations:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0A%5Cmu_%7B%5Calpha%7D%20&amp;%5Csim%20%5Ctext%7BNormal%7D(0,%20500)%20%5C%5C%0A%5Csigma_%7B%5Calpha%7D%20&amp;%5Csim%20%5Ctext%7BHalf-Normal%7D(100)%20%5C%5C%0A%5Cmu_%7B%5Cbeta%7D%20&amp;%5Csim%20%5Ctext%7BNormal%7D(0,%203)%20%5C%5C%0A%5Csigma_%7B%5Cbeta%7D%20&amp;%5Csim%20%5Ctext%7BHalf-Normal%7D(3)%20%5C%5C%0A%5Calpha_i%20&amp;%5Csim%20%5Ctext%7BNormal%7D(%5Cmu_%7B%5Calpha%7D,%20%5Csigma_%7B%5Calpha%7D)%20%5C%5C%0A%5Cbeta_i%20&amp;%5Csim%20%5Ctext%7BNormal%7D(%5Cmu_%7B%5Cbeta%7D,%20%5Csigma_%7B%5Cbeta%7D)%20%5C%5C%0A%5Csigma%20&amp;%5Csim%20%5Ctext%7BHalf-Normal%7D(100)%20%5C%5C%0AFVC_%7Bij%7D%20&amp;%5Csim%20%5Ctext%7BNormal%7D(%5Calpha_i%20+%20t%20%5Cbeta_i,%20%5Csigma)%0A%5Cend%7Balign%7D%0A"> where <img src="https://latex.codecogs.com/png.latex?t"> is week variable.</p>
<p>There are 176 patients, each has multiple weekly observations. There are 176 <img src="https://latex.codecogs.com/png.latex?%5Calpha_i"> and 176 <img src="https://latex.codecogs.com/png.latex?%5Cbeta_i"> (random effects for intercept and for slope). There are two “fixed effects”, <img src="https://latex.codecogs.com/png.latex?%5Cmu_%7B%5Calpha%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Cmu_%7B%5Cbeta%7D">. And there is one standard deviation <img src="https://latex.codecogs.com/png.latex?%5Csigma"> for the residuals, two standard deviations <img src="https://latex.codecogs.com/png.latex?%5Csigma_%7B%5Calpha%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Csigma_%7B%5Cbeta%7D"> for the random effects.</p>
<p>The idea of Bayesian inference is that we have this model, and the data, and we want to infer the posterior distribution of the parameters given the data. The posterior distribution is proportional to the likelihood times the prior distribution.</p>
<p>We can see in this case, the random effects model has similar results as the Bayesian model.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb8-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb8-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb8-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> seaborn <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> sns</span>
<span id="cb8-5"></span>
<span id="cb8-6"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpyro</span>
<span id="cb8-7"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpyro.distributions <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> dist</span>
<span id="cb8-8"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> numpyro.infer <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> MCMC, NUTS, Predictive</span>
<span id="cb8-9"></span>
<span id="cb8-10">train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.read_csv(</span>
<span id="cb8-11">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://gist.githubusercontent.com/ucals/"</span></span>
<span id="cb8-12">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2cf9d101992cb1b78c2cdd6e3bac6a4b/raw/"</span></span>
<span id="cb8-13">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"43034c39052dcf97d4b894d2ec1bc3f90f3623d9/"</span></span>
<span id="cb8-14">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"osic_pulmonary_fibrosis.csv"</span></span>
<span id="cb8-15">)</span>
<span id="cb8-16"></span>
<span id="cb8-17"></span>
<span id="cb8-18"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> jax <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> random</span>
<span id="cb8-19"></span>
<span id="cb8-20"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> model(patient_code, Weeks, FVC_obs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>):</span>
<span id="cb8-21">    μ_α <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> numpyro.sample(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"μ_α"</span>, dist.Normal(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">500.0</span>))</span>
<span id="cb8-22">    σ_α <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> numpyro.sample(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"σ_α"</span>, dist.HalfNormal(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100.0</span>))</span>
<span id="cb8-23">    μ_β <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> numpyro.sample(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"μ_β"</span>, dist.Normal(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>))</span>
<span id="cb8-24">    σ_β <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> numpyro.sample(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"σ_β"</span>, dist.HalfNormal(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>))</span>
<span id="cb8-25"></span>
<span id="cb8-26">    n_patients <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(np.unique(patient_code))</span>
<span id="cb8-27"></span>
<span id="cb8-28">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> numpyro.plate(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plate_i"</span>, n_patients):</span>
<span id="cb8-29">        α <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> numpyro.sample(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"α"</span>, dist.Normal(μ_α, σ_α))</span>
<span id="cb8-30">        β <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> numpyro.sample(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"β"</span>, dist.Normal(μ_β, σ_β))</span>
<span id="cb8-31"></span>
<span id="cb8-32">    σ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> numpyro.sample(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"σ"</span>, dist.HalfNormal(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100.0</span>))</span>
<span id="cb8-33">    FVC_est <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> α[patient_code] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> β[patient_code] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Weeks</span>
<span id="cb8-34"></span>
<span id="cb8-35">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> numpyro.plate(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(patient_code)):</span>
<span id="cb8-36">        numpyro.sample(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"obs"</span>, dist.Normal(FVC_est, σ), obs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>FVC_obs)</span>
<span id="cb8-37">       </span>
<span id="cb8-38"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.preprocessing <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> LabelEncoder</span>
<span id="cb8-39"></span>
<span id="cb8-40">patient_encoder <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> LabelEncoder()</span>
<span id="cb8-41">train[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"patient_code"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> patient_encoder.fit_transform(train[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Patient"</span>].values)</span>
<span id="cb8-42"></span>
<span id="cb8-43">FVC_obs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FVC"</span>].values</span>
<span id="cb8-44">Weeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Weeks"</span>].values</span>
<span id="cb8-45">patient_code <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"patient_code"</span>].values</span>
<span id="cb8-46">        </span>
<span id="cb8-47">        </span>
<span id="cb8-48">numpyro.render_model(</span>
<span id="cb8-49">    model<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>model,</span>
<span id="cb8-50">    model_args<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(patient_code, Weeks, FVC_obs),</span>
<span id="cb8-51">    render_distributions<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>,</span>
<span id="cb8-52">    render_params<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>,</span>
<span id="cb8-53">)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>&lt;graphviz.graphs.Digraph object at 0x7cfcfdb6a510&gt;</code></pre>
</div>
</div>


</section>
</section>

 ]]></description>
  <category>R</category>
  <category>python</category>
  <category>code</category>
  <category>analysis</category>
  <guid>https://xiangao.github.io/posts/numpyro/</guid>
  <pubDate>Fri, 17 Oct 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Uplift Modeling in Python</title>
  <dc:creator>Xiang Ao</dc:creator>
  <link>https://xiangao.github.io/posts/uplift/</link>
  <description><![CDATA[ 






<p>Uplift modeling is a technique used to estimate the incremental impact of a treatment or intervention on an outcome, often used in marketing. It helps identify which individuals are more likely to respond positively to a treatment, allowing for more targeted interventions.</p>
<p>To me this is basically conditional average treatment effect (CATE).</p>
<p>I am copying this code from https://github.com/uber/causalml/blob/master/docs/examples/uplift_trees_with_synthetic_data.ipynb. Just to see how it works and also test out python code in quarto.</p>
<div id="3dd2fc86" class="cell" data-execution_count="1">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> causalml.dataset <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> make_uplift_classification</span>
<span id="cb1-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> causalml.inference.tree <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> UpliftRandomForestClassifier</span>
<span id="cb1-6"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> causalml.metrics <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> plot_gain</span>
<span id="cb1-7"></span>
<span id="cb1-8"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.model_selection <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> train_test_split</span>
<span id="cb1-9"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> importlib</span>
<span id="cb1-10"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(importlib.metadata.version(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'causalml'</span>) )</span>
<span id="cb1-11"></span>
<span id="cb1-12">df, x_names <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> make_uplift_classification()</span>
<span id="cb1-13">df.head()</span>
<span id="cb1-14"></span>
<span id="cb1-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Look at the conversion rate and sample size in each group</span></span>
<span id="cb1-16">df.pivot_table(values<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'conversion'</span>,</span>
<span id="cb1-17">               index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'treatment_group_key'</span>,</span>
<span id="cb1-18">               aggfunc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[np.mean, np.size],</span>
<span id="cb1-19">               margins<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb1-20">               </span>
<span id="cb1-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Split data to training and testing samples for model validation (next section)</span></span>
<span id="cb1-22">df_train, df_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train_test_split(df, test_size<span class="op" 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>, random_state<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">111</span>)     </span>
<span id="cb1-23"></span>
<span id="cb1-24"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> causalml.inference.tree <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> UpliftTreeClassifier</span>
<span id="cb1-25"></span>
<span id="cb1-26">clf <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> UpliftTreeClassifier(control_name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'control'</span>)</span>
<span id="cb1-27">clf.fit(df_train[x_names].values,</span>
<span id="cb1-28">         treatment<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df_train[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'treatment_group_key'</span>].values,</span>
<span id="cb1-29">         y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df_train[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'conversion'</span>].values)</span>
<span id="cb1-30">p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> clf.predict(df_test[x_names].values)</span>
<span id="cb1-31"></span>
<span id="cb1-32">df_res <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(p, columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>clf.classes_)</span>
<span id="cb1-33">df_res.head()</span>
<span id="cb1-34"></span>
<span id="cb1-35">uplift_model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> UpliftRandomForestClassifier(control_name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'control'</span>)</span>
<span id="cb1-36"></span>
<span id="cb1-37">uplift_model.fit(df_train[x_names].values,</span>
<span id="cb1-38">                 treatment<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df_train[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'treatment_group_key'</span>].values,</span>
<span id="cb1-39">                 y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df_train[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'conversion'</span>].values)</span>
<span id="cb1-40"></span>
<span id="cb1-41">df_res <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> uplift_model.predict(df_test[x_names].values, full_output<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb1-42"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(df_res.shape)</span>
<span id="cb1-43">df_res.head()          </span>
<span id="cb1-44"></span>
<span id="cb1-45">y_pred <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> uplift_model.predict(df_test[x_names].values)</span>
<span id="cb1-46"></span>
<span id="cb1-47"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Put the predictions to a DataFrame for a neater presentation</span></span>
<span id="cb1-48"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The output of `predict()` is a numpy array with the shape of [n_sample, n_treatment] excluding the</span></span>
<span id="cb1-49"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># predictions for the control group.</span></span>
<span id="cb1-50">result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(y_pred,</span>
<span id="cb1-51">                      columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>uplift_model.classes_[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:])</span>
<span id="cb1-52">result.head()</span>
<span id="cb1-53"></span>
<span id="cb1-54"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># If all deltas are negative, assing to control; otherwise assign to the treatment</span></span>
<span id="cb1-55"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># with the highest delta</span></span>
<span id="cb1-56">best_treatment <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.where((result <span class="op" 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 class="bu" style="color: null;
background-color: null;
font-style: inherit;">all</span>(axis<span class="op" 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-57">                           <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'control'</span>,</span>
<span id="cb1-58">                           result.idxmax(axis<span class="op" 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-59"></span>
<span id="cb1-60"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create indicator variables for whether a unit happened to have the</span></span>
<span id="cb1-61"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># recommended treatment or was in the control group</span></span>
<span id="cb1-62">actual_is_best <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.where(df_test[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'treatment_group_key'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> best_treatment, <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;">0</span>)</span>
<span id="cb1-63">actual_is_control <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.where(df_test[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'treatment_group_key'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'control'</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;">0</span>)</span>
<span id="cb1-64"></span>
<span id="cb1-65">synthetic <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (actual_is_best <span class="op" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> (actual_is_control <span class="op" 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-66">synth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> result[synthetic]</span>
<span id="cb1-67"></span>
<span id="cb1-68">auuc_metrics <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (synth.assign(is_treated <span class="op" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> actual_is_control[synthetic],</span>
<span id="cb1-69">                             conversion <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df_test.loc[synthetic, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'conversion'</span>].values,</span>
<span id="cb1-70">                             uplift_tree <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> synth.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(axis<span class="op" 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-71">                     .drop(columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(uplift_model.classes_[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:])))</span>
<span id="cb1-72"> </span>
<span id="cb1-73">plot_gain(auuc_metrics, outcome_col<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'conversion'</span>, treatment_col<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'is_treated'</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>0.17.0
(800, 9)</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://xiangao.github.io/posts/uplift/index_files/figure-html/cell-2-output-2.png" alt="Cumulative gain curves comparing uplift random forest variants against a random targeting baseline" width="744" height="692" class="figure-img"></p>
</figure>
</div>
</div>
</div>



 ]]></description>
  <category>python</category>
  <category>code</category>
  <category>analysis</category>
  <guid>https://xiangao.github.io/posts/uplift/</guid>
  <pubDate>Fri, 10 Oct 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Causal Simulation</title>
  <dc:creator>Xiang Ao</dc:creator>
  <link>https://xiangao.github.io/posts/causal_simulation/</link>
  <description><![CDATA[ 






<p>Evans, R. J., &amp; Didelez, V. (2024). “Parameterizing and simulating from causal models” is about simulating from a causal model. I found that interesting.</p>
<p>The usual causal inference is to have observational data, then assume causal structure, then estimate the parameters. This paper is about the opposite: Suppose we have a causal model, what kind of distribution would generate it?</p>
<p>Suppose we are interested in joint distribution <img src="https://latex.codecogs.com/png.latex?p(x,a,y)">, <img src="https://latex.codecogs.com/png.latex?x"> is covariates, <img src="https://latex.codecogs.com/png.latex?a"> is treatment, <img src="https://latex.codecogs.com/png.latex?y"> is outcome.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0Ap(x,a,y)%20&amp;=%20p(x,a)p(y%7Cx,a)%20%5C%5C%0A%20%20%20%20%20%20%20%20%20&amp;=%20p(x,a)p_a(y%7Cx)%20%5C%5C%0A%20%20%20%20%20%20%20%20%20&amp;=%20p(x,a)%5Cfrac%7Bp_a(x,y)%7D%7Bp_a(x)%7D%20%5C%5C%0A%20%20%20%20%20%20%20%20%20&amp;=%20p(x,a)%5Cfrac%7Bp_a(x)p_a(y)c(x,y%7Ca)%7D%7Bp_a(x)%7D%20%5C%5C%0A%20%20%20%20%20%20%20%20%20&amp;=%20p(x,a)p_a(y)c(x,y%7Ca)%0A%5Cend%7Balign%7D%0A"></p>
<p>Basically the joint distribution of <img src="https://latex.codecogs.com/png.latex?(x,a,y)"> can be factorized into the marginal distribution of <img src="https://latex.codecogs.com/png.latex?(x,a)">, the conditional distribution of <img src="https://latex.codecogs.com/png.latex?y"> given <img src="https://latex.codecogs.com/png.latex?x"> and <img src="https://latex.codecogs.com/png.latex?a">, and the copula <img src="https://latex.codecogs.com/png.latex?c(x,y%7Ca)">. The copula is a function that captures the dependence between <img src="https://latex.codecogs.com/png.latex?x"> and <img src="https://latex.codecogs.com/png.latex?y"> given <img src="https://latex.codecogs.com/png.latex?a">. <img src="https://latex.codecogs.com/png.latex?(x,a)"> is the “past”, which can be specified. <img src="https://latex.codecogs.com/png.latex?p_a(y)"> is the “marginal structure model” which can be specified. The copula model can also be specified.</p>
<p>About copula: copula is a function can link joint distribution to marginal distributions.</p>
<p><img src="https://latex.codecogs.com/png.latex?%20p(x,y)%20=%20f(x)g(y)c(F(x),G(y))%20"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?F(x)"> and <img src="https://latex.codecogs.com/png.latex?G(y)"> are the marginal distributions of <img src="https://latex.codecogs.com/png.latex?x"> and <img src="https://latex.codecogs.com/png.latex?y">, respectively, and <img src="https://latex.codecogs.com/png.latex?c"> is the copula function that captures the dependence between <img src="https://latex.codecogs.com/png.latex?x"> and <img src="https://latex.codecogs.com/png.latex?y">. The copula function is a multivariate distribution with uniform marginals. It can be used to generate joint distributions from marginal distributions.</p>
<p>Here <img src="https://latex.codecogs.com/png.latex?x">, <img src="https://latex.codecogs.com/png.latex?a"> and <img src="https://latex.codecogs.com/png.latex?y"> can all be vectors.</p>
<section id="examples" class="level2">
<h2 class="anchored" data-anchor-id="examples">examples</h2>
<p>What do we use this for? We can use this to simulate data from a causal model, and then fit different models to the simulated data. This allows to compare the performance of different models.</p>
<section id="example-1" class="level3">
<h3 class="anchored" data-anchor-id="example-1">example 1</h3>
<p>Suppose we have a causal graph like this:</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://xiangao.github.io/posts/causal_simulation/index_files/figure-html/dag1-1.png" class="img-fluid figure-img" alt="Directed acyclic graph with covariates X, treatment A, and outcome Y" width="672"></p>
</figure>
</div>
</div>
</div>
<p>We need to specify <img src="https://latex.codecogs.com/png.latex?P(y%7Cdo(a))%20=%20%5Csum_x%20P(x)%20P(y%7Cx,a)">, where <img src="https://latex.codecogs.com/png.latex?P(x)"> is the marginal distribution of <img src="https://latex.codecogs.com/png.latex?x">, and <img src="https://latex.codecogs.com/png.latex?P(y%7Cx,a)"> is the conditional distribution of <img src="https://latex.codecogs.com/png.latex?y"> given <img src="https://latex.codecogs.com/png.latex?x"> and <img src="https://latex.codecogs.com/png.latex?a">. This is the “marginal structural model” (Robins, 2000). Also the “g-formula”.</p>
<p>We also need to specify <img src="https://latex.codecogs.com/png.latex?P(x,a)">, the “past”.</p>
<p>Finally we need to specify the copula <img src="https://latex.codecogs.com/png.latex?c(x,y%7Ca)">, which captures the dependence between <img src="https://latex.codecogs.com/png.latex?x"> and <img src="https://latex.codecogs.com/png.latex?y"> given <img src="https://latex.codecogs.com/png.latex?a">. Depending on different situations, different copula can be used.</p>
<p>In the “causl” package example, we specify: <img src="https://latex.codecogs.com/png.latex?%20X%20%5Csim%20N(0,1)%20"> <img src="https://latex.codecogs.com/png.latex?%20A%20%7C%20X=x%20%20%5Csim%20N(x/2,%201)%20"> <img src="https://latex.codecogs.com/png.latex?%20Y%20%7C%20do(A=a)%20%5Csim%20N(%7B(a-1)/2%7D,%201)%20"> and Gaussian copula with correlation <img src="https://latex.codecogs.com/png.latex?%5Crho%20=%202%20expit(1)-1">.</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>(causl)</span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># formulae corresponding to covariates, treatments, outcomes and the dependence</span></span>
<span id="cb1-3">forms <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</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;">1</span>, A <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X, Y <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> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># vector of model families (3=gamma/exponential, 1=normal/Gaussian)</span></span>
<span id="cb1-5">fam <span class="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;">1</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># list of parameters, including 'beta' (regression params) and 'phi' dispersion</span></span>
<span id="cb1-7">pars <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<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;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">beta=</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;">phi=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),   </span>
<span id="cb1-8">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">A =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">beta=</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.5</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">phi=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb1-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;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">beta=</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="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="at" style="color: #657422;
background-color: null;
font-style: inherit;">phi=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb1-10">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">beta=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb1-11"></span>
<span id="cb1-12"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## now create a `causl_model` object</span></span>
<span id="cb1-13">cm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">causl_model</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formulas=</span>forms, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family=</span>fam, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pars=</span>pars,<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;">"inversion"</span>)</span>
<span id="cb1-14"></span>
<span id="cb1-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># now simulate 1000 observations</span></span>
<span id="cb1-16"><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;">123456</span>)</span>
<span id="cb1-17">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rfrugal</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;">1000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">causl_model=</span>cm)</span>
<span id="cb1-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(data)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>            X           A          Y
1  0.83373317  1.10709111  1.1416713
2 -0.27604777 -0.27218853 -1.0495996
3 -0.35500184  0.77575209  1.3179678
4  0.08748742 -0.05959823 -0.6106023
5  2.25225573  0.61909216  2.0789188
6  0.83446013  0.52019822  0.3229069</code></pre>
</div>
</div>
<p>In this example, first we specify the structure of the model using a list of formulas. The first formula is for the covariates, the second for the treatments, the third for the outcomes, and the fourth for the dependence structure (copula). Note that outcome <img src="https://latex.codecogs.com/png.latex?Y"> only depends on <img src="https://latex.codecogs.com/png.latex?A"> in this interventional distribution.</p>
<p>Then we specify the families of the random variables. Here we use exponential distribution for <img src="https://latex.codecogs.com/png.latex?X">, normal distribution for <img src="https://latex.codecogs.com/png.latex?A"> and <img src="https://latex.codecogs.com/png.latex?Y">, and Gaussian copula for the dependence structure.</p>
<p>Finally we specify the parameters of the model, including regression coefficients and dispersion parameters. For example, <img src="https://latex.codecogs.com/png.latex?Y%20%5Csim%20A"> has two coefficients, intercept -.5 and slope .5.</p>
<p>The <code>causl_model</code> object is then created using these components.</p>
<p>In this simple example, if we run a regression with <img src="https://latex.codecogs.com/png.latex?A">, <img src="https://latex.codecogs.com/png.latex?X"> in the model, on the simulated data, we would get the correct effect back:</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">lm1 <span class="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> A<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>data)</span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(lm1)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
lm(formula = Y ~ A * X, data = data)

Residuals:
    Min      1Q  Median      3Q     Max 
-2.6326 -0.6212 -0.0105  0.6051  3.3132 

Coefficients:
             Estimate Std. Error t value Pr(&gt;|t|)    
(Intercept) -0.491922   0.030413 -16.175   &lt;2e-16 ***
A            0.493471   0.027820  17.738   &lt;2e-16 ***
X            0.468038   0.031878  14.682   &lt;2e-16 ***
A:X         -0.009247   0.022360  -0.414    0.679    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.8953 on 996 degrees of freedom
Multiple R-squared:  0.4875,    Adjusted R-squared:  0.4859 
F-statistic: 315.8 on 3 and 996 DF,  p-value: &lt; 2.2e-16</code></pre>
</div>
</div>
<p>We can also use MLE:</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">out <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit_causl</span>(data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formulas =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</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;">1</span>, Y <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> <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;">family =</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 class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb5-2">out</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>log-likelihood:  -2716.696 
X ~ 1
              est.   s.e. sandwich
(intercept) 0.0108 0.0314   0.0314
  residual s.e.:  0.983 0.044 0.0447 

Y ~ A
              est.   s.e. sandwich
(intercept) -0.491 0.0319   0.0319
A            0.494 0.0278   0.0295
  residual s.e.:  1.01 0.0472 0.0475 

copula parameters:
cop ~ 1
             est.  s.e. sandwich
(intercept) 0.998 0.069   0.0734</code></pre>
</div>
</div>
</section>
<section id="plasmode-simulation" class="level3">
<h3 class="anchored" data-anchor-id="plasmode-simulation">Plasmode simulation</h3>
<p>In reality, we have a data set and we have a causal model. We want to simulate data from the causal model, but we want the simulated data to have the same distribution as the original data. This is called “plasmode simulation”. For example, we don’t want to use the real outcome variable. In stead, we simulate X’s from the original data, Then simulate Y and A from the causal model. We can then test which method works better in estimating the causal effect of <img src="https://latex.codecogs.com/png.latex?A"> on <img src="https://latex.codecogs.com/png.latex?Y">.</p>
<div class="cell">
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4,802 × 58
     x_1 x_2     x_3   x_4   x_5   x_6   x_7   x_8   x_9  x_10  x_11  x_12  x_13
   &lt;int&gt; &lt;fct&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;
 1    29 C         1     7    60    85     0     0     1     0     0     1     0
 2    27 C         0     0    64   178     0     0     0     0     0     2     1
 3    27 C         0     0    60   102     0     0     0     0     0     1     0
 4    37 C         0     0    65   174     0     0     0     0     0     1     0
 5    24 C        20    14    63   129     0     0     0     0     0     1     0
 6    27 C        40    15    63   135     0     0     0     0     0     1     1
 7    26 C        20     8    69   140     0     0     0     1     0     0     0
 8    33 C         0     0    60   110     0     0     0     1     0     0     1
 9    28 C         3     5    61   160     0     0     0     0     0     3     1
10    31 C         0     0    63   114     0     1     0     0     0     0     0
# ℹ 4,792 more rows
# ℹ 45 more variables: x_14 &lt;int&gt;, x_15 &lt;int&gt;, x_16 &lt;int&gt;, x_17 &lt;int&gt;,
#   x_18 &lt;int&gt;, x_19 &lt;int&gt;, x_20 &lt;int&gt;, x_21 &lt;fct&gt;, x_22 &lt;int&gt;, x_23 &lt;int&gt;,
#   x_24 &lt;fct&gt;, x_25 &lt;int&gt;, x_26 &lt;int&gt;, x_27 &lt;int&gt;, x_28 &lt;int&gt;, x_29 &lt;int&gt;,
#   x_30 &lt;int&gt;, x_31 &lt;int&gt;, x_32 &lt;int&gt;, x_33 &lt;int&gt;, x_34 &lt;int&gt;, x_35 &lt;int&gt;,
#   x_36 &lt;int&gt;, x_37 &lt;int&gt;, x_38 &lt;int&gt;, x_39 &lt;int&gt;, x_40 &lt;int&gt;, x_41 &lt;int&gt;,
#   x_42 &lt;int&gt;, x_43 &lt;int&gt;, x_44 &lt;int&gt;, x_45 &lt;int&gt;, x_46 &lt;int&gt;, x_47 &lt;int&gt;, …</code></pre>
</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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model for the causal effect of smoking on birthweight</span></span>
<span id="cb8-2">forms <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(),</span>
<span id="cb8-3">              <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(A <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x_1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x_3 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x_4),</span>
<span id="cb8-4">              <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> A),</span>
<span id="cb8-5">              <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</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="cb8-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># fams &lt;- list(integer(0), 5, 1, 1)</span></span>
<span id="cb8-7">fams <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">integer</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"binomial"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gaussian"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb8-8">pars <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">A =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">beta=</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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.03</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.05</span>)),</span>
<span id="cb8-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;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">beta=</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;">3200</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;">500</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">phi=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">400</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="cb8-10">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">beta=</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="cb8-11"></span>
<span id="cb8-12">cm2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">causl_model</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formulas=</span>forms, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family=</span>fams, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pars=</span>pars, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dat =</span> dat)</span>
<span id="cb8-13"> </span>
<span id="cb8-14"><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;">123456</span>)</span>
<span id="cb8-15">data2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rfrugal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">causl_model=</span>cm2)</span>
<span id="cb8-16"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(data2)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  x_1 x_2 x_3 x_4 x_5 x_6 x_7 x_8 x_9 x_10 x_11 x_12 x_13 x_14 x_15 x_16 x_17
1  29   C   1   7  60  85   0   0   1    0    0    1    0    0    2    0    0
2  27   C   0   0  64 178   0   0   0    0    0    2    1    0    2    0    0
3  27   C   0   0  60 102   0   0   0    0    0    1    0    0    0    0    0
4  37   C   0   0  65 174   0   0   0    0    0    1    0    0    1    0    0
5  24   C  20  14  63 129   0   0   0    0    0    1    0    0    0    0    0
6  27   C  40  15  63 135   0   0   0    0    0    1    1    2    2    0    0
  x_18 x_19 x_20 x_21 x_22 x_23 x_24 x_25 x_26 x_27 x_28 x_29 x_30 x_31 x_32
1   12   35   10    J    1   43    B   17   64  105   30   11    0    0    0
2   12   75   12    J    1   50    E   12   90  183   40    8    0    0    0
3   12   35   10    J    1   57    E   14   70  121   27   12    0    0    0
4   11   35   12    J    1   43    E   10   80  185   39   10    0    0    0
5    9   35   15    J    1   33    E   14   70  160   28    7    0    0    0
6    9   45   10    J    1   40    E   15   64  160   31    9    0    0    0
  x_33 x_34 x_35 x_36 x_37 x_38 x_39 x_40 x_41 x_42 x_43 x_44 x_45 x_46 x_47
1    0   79   28  340   48    1    9    9    3    0    3   54   16    0    0
2    0   69   30  430   73    0    9    9    3    0    5   51   16    0    0
3    0   84   29  450   76    0    3    6    2    0    4   41   13    0    0
4    0   82   30  440   78    1    8    9    2    0    8   70   16    0    0
5    1   85   35  360   71    1    5    8    0    2    3   65   18    0    0
6    1   81   28  515   56    1    7    9    2    0    1   63   20    0    0
  x_48 x_49 x_50 x_51 x_52 x_53 x_54 x_55 x_56 x_57 x_58 A        Y
1    0    0    0    0    0    0    0    0    0   45   39 1 2125.677
2    0    0    0    0    0    0    0    0    0   46   42 1 2688.990
3    0    1    0    0    0    0    0    0    0   45   40 0 3442.924
4    0    0    0    0    0    0    0    0    0   47   40 0 3212.584
5    0    2    0    0    0    0    0    0    0   47   43 0 2992.775
6    0    0    0    0    0    0    0    0    0   45   44 0 2738.893</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;"># we can also use rfrugalParam(), which is older version of rfrugal().</span></span>
<span id="cb10-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#datAY &lt;- rfrugalParam(formulas=forms, family=fams, pars=pars, dat=dat)</span></span></code></pre></div></div>
</div>
<p>In this specification, we have a binary treatment <img src="https://latex.codecogs.com/png.latex?A"> and a continuous outcome <img src="https://latex.codecogs.com/png.latex?Y">. The covariates are <img src="https://latex.codecogs.com/png.latex?x_1">, <img src="https://latex.codecogs.com/png.latex?x_3"> and <img src="https://latex.codecogs.com/png.latex?x_4">. The copula is a Gaussian copula with correlation -1. We can then simulate the data.</p>
</section>
</section>
<section id="a-more-complicated-model" class="level2">
<h2 class="anchored" data-anchor-id="a-more-complicated-model">a more complicated model</h2>
<p>Let’s simulate a more complicated model:</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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb11-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggdag)</span>
<span id="cb11-3"></span>
<span id="cb11-4">g <span class="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="cb11-5">  A1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> L1,</span>
<span id="cb11-6">  L2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> L1,</span>
<span id="cb11-7">  L2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> A1,</span>
<span id="cb11-8">  A2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> A1,</span>
<span id="cb11-9">  A2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> L2,</span>
<span id="cb11-10">  Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> A2,</span>
<span id="cb11-11">  Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> L2,</span>
<span id="cb11-12">  Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> A1,</span>
<span id="cb11-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">exposure =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A2"</span>,</span>
<span id="cb11-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outcome =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Y"</span>,</span>
<span id="cb11-15">  <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 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;">L1 =</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;">L2 =</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;">A1 =</span> <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;">A2 =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>),</span>
<span id="cb11-16">                <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;">L1 =</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;">L2 =</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;">Y =</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;">A1 =</span> <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;">A2 =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>))</span>
<span id="cb11-17">)</span>
<span id="cb11-18"></span>
<span id="cb11-19"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggdag</span>(g) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb11-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_dag</span>() </span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://xiangao.github.io/posts/causal_simulation/index_files/figure-html/dag2-1.png" class="img-fluid figure-img" alt="Directed acyclic graph for the smoking and birthweight example, with a latent confounder" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Let’s see how to write a causal model:</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;"># formulae corresponding to covariates, treatments, outcomes and the dependence</span></span>
<span id="cb12-2">forms <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(L1  <span 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>, L2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> L1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>A1),   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># covariates</span></span>
<span id="cb12-3">              <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(A1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> L1, A2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> L2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>A1), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># treatments</span></span>
<span id="cb12-4">              Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> A1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>A2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>L2,             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># outcome</span></span>
<span id="cb12-5">              <span 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="cb12-6"></span>
<span id="cb12-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># vector of model families (3=gamma/exponential, 1=normal/Gaussian)</span></span>
<span id="cb12-8"></span>
<span id="cb12-9">fams <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</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 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;">5</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">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;">1</span>)</span>
<span id="cb12-10">pars <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">L1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">beta=</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;">phi=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb12-11">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">L2 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">beta=</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.3</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.2</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.1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">phi=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb12-12">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">A1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">beta=</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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">phi=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb12-13">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">A2 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">beta=</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.5</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</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;">phi=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb12-14">             <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;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">beta=</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;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</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.8</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">phi=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), </span>
<span id="cb12-15">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">beta =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>))</span>
<span id="cb12-16"></span>
<span id="cb12-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># we can use rfrugalParam() to simulate data from the model, or rfrugal().</span></span>
<span id="cb12-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#dat &lt;- rfrugalParam(n=1e4, formulas=forms, family = fams, pars=pars)</span></span>
<span id="cb12-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#head(dat)</span></span>
<span id="cb12-20"></span>
<span id="cb12-21">cm3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">causl_model</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formulas=</span>forms, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family=</span>fams, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pars=</span>pars)</span>
<span id="cb12-22"> </span>
<span id="cb12-23"><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;">123456</span>)</span>
<span id="cb12-24">data3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rfrugal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e4</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">causl_model=</span>cm3)</span>
<span id="cb12-25"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(data3)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>           L1          L2 A1 A2         Y
1  0.83373317  1.91691936  1  1 8.1680612
2 -0.27604777  0.17008294  1  1 5.2276976
3 -0.35500184  0.16907708  1  1 2.8194835
4  0.08748742  0.19688985  0  1 2.6789661
5  2.25225573 -0.29257431  0  0 0.3180188
6  0.83446013  0.02661196  0  1 1.5694426</code></pre>
</div>
</div>
<p>If we have the correct specification for the outcome model, we’ll get it right by linear 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">m1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> A1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>A2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>L2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data3)</span>
<span id="cb14-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(m1)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
glm(formula = Y ~ A1 * A2 * L2, data = data3)

Coefficients:
            Estimate Std. Error t value Pr(&gt;|t|)    
(Intercept) -0.11524    0.02110  -5.461 4.85e-08 ***
A1           1.11589    0.03245  34.391  &lt; 2e-16 ***
A2           2.02332    0.02682  75.454  &lt; 2e-16 ***
L2           0.80424    0.01929  41.682  &lt; 2e-16 ***
A1:A2        0.98345    0.04090  24.044  &lt; 2e-16 ***
A1:L2        0.16028    0.02976   5.386 7.37e-08 ***
A2:L2       -0.03446    0.02401  -1.435    0.151    
A1:A2:L2     0.83541    0.03693  22.620  &lt; 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for gaussian family taken to be 0.9100255)

    Null deviance: 53902  on 9999  degrees of freedom
Residual deviance:  9093  on 9992  degrees of freedom
AIC: 27446

Number of Fisher Scoring iterations: 2</code></pre>
</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;">library</span>(marginaleffects)</span>
<span id="cb16-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">avg_comparisons</span>(</span>
<span id="cb16-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> m1,</span>
<span id="cb16-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variables =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A2"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
 Estimate Std. Error   z Pr(&gt;|z|)   S 2.5 % 97.5 %
     2.51     0.0202 124   &lt;0.001 Inf  2.47   2.55

Term: A2
Type: response
Comparison: 1 - 0</code></pre>
</div>
</div>
<p>So the ATE is 2.5.</p>
<p>Let’s use “lmtp” package to estimate the treatment effect of <img src="https://latex.codecogs.com/png.latex?A_2"> on <img src="https://latex.codecogs.com/png.latex?Y">.</p>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lmtp)</span>
<span id="cb18-2"></span>
<span id="cb18-3">d1 <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>(data, trt) {</span>
<span id="cb18-4">  <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;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(data))</span>
<span id="cb18-5">}</span>
<span id="cb18-6">A <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A2"</span></span>
<span id="cb18-7">Y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Y"</span></span>
<span id="cb18-8">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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"L1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"L2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A1"</span>)</span>
<span id="cb18-9"></span>
<span id="cb18-10"><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;">34465</span>)</span>
<span id="cb18-11"></span>
<span id="cb18-12">treat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmtp_tmle</span>(</span>
<span id="cb18-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data3, </span>
<span id="cb18-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trt =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A2"</span>, </span>
<span id="cb18-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outcome =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Y"</span>, </span>
<span id="cb18-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">baseline =</span> W, </span>
<span id="cb18-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outcome_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"continuous"</span>, </span>
<span id="cb18-18">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shift =</span> d1, </span>
<span id="cb18-19">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">folds =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, </span>
<span id="cb18-20">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">learners_trt =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SL.glm"</span>, </span>
<span id="cb18-21">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">learners_outcome =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SL.glm"</span></span>
<span id="cb18-22">)</span>
<span id="cb18-23"></span>
<span id="cb18-24"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(treat)</span></code></pre></div></div>
<pre><code>LMTP Estimator: TMLE</code></pre>
<pre><code>Trt. Policy: (d1)</code></pre>
<pre><code></code></pre>
<pre><code>── Population intervention estimate ──</code></pre>
<pre><code></code></pre>
<pre><code>  Estimate: 3.046
Std. error: 0.022</code></pre>
<pre><code>95% Conf. int.: 3.003, 3.089</code></pre>
<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">d2 <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>(data, trt) {</span>
<span id="cb26-2">  <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>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(data))</span>
<span id="cb26-3">}</span>
<span id="cb26-4">control <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmtp_tmle</span>(</span>
<span id="cb26-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data3, </span>
<span id="cb26-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trt =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A2"</span>, </span>
<span id="cb26-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outcome =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Y"</span>, </span>
<span id="cb26-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">baseline =</span> W, </span>
<span id="cb26-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outcome_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"continuous"</span>, </span>
<span id="cb26-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shift =</span> d2, </span>
<span id="cb26-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">folds =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, </span>
<span id="cb26-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">learners_trt =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SL.glm"</span>, </span>
<span id="cb26-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">learners_outcome =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SL.glm"</span></span>
<span id="cb26-14">)</span>
<span id="cb26-15"></span>
<span id="cb26-16"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(control)</span></code></pre></div></div>
<pre><code>LMTP Estimator: TMLE</code></pre>
<pre><code>Trt. Policy: (d2)</code></pre>
<pre><code>── Population intervention estimate ──</code></pre>
<pre><code></code></pre>
<pre><code>  Estimate: 0.542
Std. error: 0.021</code></pre>
<pre><code>95% Conf. int.: 0.501, 0.582</code></pre>
<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;">lmtp_contrast</span>(treat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ref =</span> control)</span></code></pre></div></div>
<pre><code>LMTP Contrast: additive</code></pre>
<pre><code>Null hypothesis: theta == 0</code></pre>
<p>shift ref estimate std.error conf.low conf.high p.value 1 3.05 0.542 2.5 0.0227 2.46 2.55 &lt;0.001</p>


</section>

 ]]></description>
  <category>news</category>
  <category>code</category>
  <category>analysis</category>
  <guid>https://xiangao.github.io/posts/causal_simulation/</guid>
  <pubDate>Wed, 01 Oct 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Longitudinal Modified Treatment Policies (LMTP)</title>
  <dc:creator>Xiang Ao</dc:creator>
  <link>https://xiangao.github.io/posts/lmtp/</link>
  <description><![CDATA[ 






<p>I read a few papers with longitudinal modified treatment policy (LMTP), and found it interesting. It has been used in epidemiology and biostatistics, but I have not seen it in applied econometrics yet.</p>
<p>Here I am mostly following Nicholas Williams: https://beyondtheate.com/</p>
<p>We are usually interested in ATE, the average treatment effect. However, there could be more complicated situations that the treatment is continuous, or the treatment is multivalued, or the treatment is time-varying. The static interventions have problems. For example, the hypothetical interventions that treatment applies to everyone might be inconceivable. Or such intervention could make positivity assumption fail.</p>
<p>Suppose we have such a DAG:</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>(ggplot2)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggdag)</span>
<span id="cb1-3"></span>
<span id="cb1-4">g <span class="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="cb1-5">  A1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> L1,</span>
<span id="cb1-6">  L2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> L1,</span>
<span id="cb1-7">  L2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> A1,</span>
<span id="cb1-8">  A2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> A1,</span>
<span id="cb1-9">  A2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> L2,</span>
<span id="cb1-10">  Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> A2,</span>
<span id="cb1-11">  Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> L2,</span>
<span id="cb1-12">  Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> A1,</span>
<span id="cb1-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">exposure =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A2"</span>,</span>
<span id="cb1-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outcome =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Y"</span>,</span>
<span id="cb1-15">  <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 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;">L1 =</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;">L2 =</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;">A1 =</span> <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;">A2 =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>),</span>
<span id="cb1-16">                <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;">L1 =</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;">L2 =</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;">Y =</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;">A1 =</span> <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;">A2 =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>))</span>
<span id="cb1-17">)</span>
<span id="cb1-18"></span>
<span id="cb1-19"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggdag</span>(g) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb1-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_dag</span>() </span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://xiangao.github.io/posts/lmtp/index_files/figure-html/dag1-1.png" class="img-fluid figure-img" alt="Directed acyclic graph for a two-period longitudinal setting with time-varying treatment and confounding" width="672"></p>
</figure>
</div>
</div>
</div>
<p>We have multiple time points, and the treatment <img src="https://latex.codecogs.com/png.latex?A"> is time-varying. We are interested in not only the ATE of <img src="https://latex.codecogs.com/png.latex?A_2"> on <img src="https://latex.codecogs.com/png.latex?Y">, but also some other hypothetical interventions.</p>
<section id="definitions-and-assumptions" class="level2">
<h2 class="anchored" data-anchor-id="definitions-and-assumptions">Definitions and assumptions</h2>
<section id="assumptions" class="level3">
<h3 class="anchored" data-anchor-id="assumptions">Assumptions</h3>
<ol type="1">
<li><p>Positivity. Basically if there is a unit with <img src="https://latex.codecogs.com/png.latex?a_t"> and <img src="https://latex.codecogs.com/png.latex?h_t">, then there is a unit with <img src="https://latex.codecogs.com/png.latex?d(a_t,%20h_t)"> and <img src="https://latex.codecogs.com/png.latex?h_t">.</p></li>
<li><p>Sequential unconfoundedness. There is no unmeasured confounders.</p></li>
</ol>
</section>
<section id="dynamic-treatment-regime" class="level3">
<h3 class="anchored" data-anchor-id="dynamic-treatment-regime">dynamic treatment regime</h3>
<p>Some notations commonly used in this literature: We observe <img src="https://latex.codecogs.com/png.latex?Z%20=%20(L_0,%20A_0,%20L_1,%20A_1,%20Y)">, where <img src="https://latex.codecogs.com/png.latex?L"> is the observed confounder, <img src="https://latex.codecogs.com/png.latex?A"> is the treatment, and <img src="https://latex.codecogs.com/png.latex?Y"> is the outcome. The treatment <img src="https://latex.codecogs.com/png.latex?A"> can be time-varying. We can also have multiple treatments at different time points. For example, we can have <img src="https://latex.codecogs.com/png.latex?A_2">, <img src="https://latex.codecogs.com/png.latex?A_3">, etc. In this graph, baseline <img src="https://latex.codecogs.com/png.latex?L_0"> affects <img src="https://latex.codecogs.com/png.latex?A_0">, which in turn affects <img src="https://latex.codecogs.com/png.latex?L_1">, which then affects <img src="https://latex.codecogs.com/png.latex?A_1">, and finally <img src="https://latex.codecogs.com/png.latex?A_1"> affects the outcome <img src="https://latex.codecogs.com/png.latex?Y">.</p>
<p>History <img src="https://latex.codecogs.com/png.latex?H_t"> is the history of data up to time <img src="https://latex.codecogs.com/png.latex?t">, right before <img src="https://latex.codecogs.com/png.latex?A_t">. for example, <img src="https://latex.codecogs.com/png.latex?H_1%20=%20(L_0,%20A_0,%20L_1)">, and <img src="https://latex.codecogs.com/png.latex?H_2%20=%20(L_0,%20A_0,%20L_1,%20A_1)">. <img src="https://latex.codecogs.com/png.latex?d"> is the hypothetical intervention function, or shift function, which is a function of the history <img src="https://latex.codecogs.com/png.latex?H_t"> and the treatment <img src="https://latex.codecogs.com/png.latex?A_t">. For example, <img src="https://latex.codecogs.com/png.latex?d_0(a_0,h_0,%5Cepsilon_0)"> is a user-given function to map <img src="https://latex.codecogs.com/png.latex?a_0">, <img src="https://latex.codecogs.com/png.latex?h_0">, and <img src="https://latex.codecogs.com/png.latex?%5Cepsilon_0"> to a potential treatment value. The function <img src="https://latex.codecogs.com/png.latex?d"> can be deterministic, or it can be stochastic. Then we can replace <img src="https://latex.codecogs.com/png.latex?A_0"> with <img src="https://latex.codecogs.com/png.latex?A_0%5Ed%20=%20d_0(A_0,%20H_0,%20%5Cepsilon_0)">. Then after that <img src="https://latex.codecogs.com/png.latex?A_1(A_0%5Ed)"> is called the natural value of treatment.</p>
<p>This is very general, comparing to the static treatment regime.</p>
<p>Suppose treatment <img src="https://latex.codecogs.com/png.latex?A"> is a function of the history of treatment and confounders, for example, <img src="https://latex.codecogs.com/png.latex?A%20=%20d(A_1,%20L_1,%20A_2,%20L_2)">. <img src="https://latex.codecogs.com/png.latex?A"> can be set to a fixed value, say 1 or 0, or some value <img src="https://latex.codecogs.com/png.latex?A%5Ed">. This function <img src="https://latex.codecogs.com/png.latex?d"> can be anything, it can be taking a deterministic value, or it can be a function that takes the natural treatment value <img src="https://latex.codecogs.com/png.latex?A"> as input. In the package “lmtp”, this is called a shift function, or hypothetical intervention. For example, <img src="https://latex.codecogs.com/png.latex?d"> can be set to 1 if <img src="https://latex.codecogs.com/png.latex?age%20%3C%2030">, or <img src="https://latex.codecogs.com/png.latex?d"> can be set to double the natural value of <img src="https://latex.codecogs.com/png.latex?A">. Many possibilities.</p>
<p>In comparison, for ATE, we only need to set <img src="https://latex.codecogs.com/png.latex?A"> to 0 or 1.</p>
<p>Under this LMTP, the causal parameter is</p>
<p><img src="https://latex.codecogs.com/png.latex?%20%5Ctheta%20=%20E%5BY%5E%7B%5Cbar%20A%5Ed%7D%5D%20"></p>
<p><img src="https://latex.codecogs.com/png.latex?Y%5E%7B%5Cbar%20A%5Ed%7D"> is the potential outcome under the hypothetical intervention <img src="https://latex.codecogs.com/png.latex?%5Cbar%20A%5Ed">. At time 1, <img src="https://latex.codecogs.com/png.latex?A%5Ed_1%20=%20d(A_1,H_1)">.</p>
</section>
<section id="modified-treatment-policy" class="level3">
<h3 class="anchored" data-anchor-id="modified-treatment-policy">modified treatment policy</h3>
<p>Let’s look at a simulated data set to see how exactly we can estimate it.</p>
<p>This simulation is from Susmann et al.&nbsp;(2024) “Longitudinal Generalizations of the Average Treatment Effect on the Treated for Multi-valued and Continuous Treatments”. I modified slightly to fit the DAG above.</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;">library</span>(tidyverse)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyr)</span>
<span id="cb2-3"></span>
<span id="cb2-4">mtp <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>(data, trt) {</span>
<span id="cb2-5">  a <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data[[trt]]</span>
<span id="cb2-6">  a <span 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="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="cb2-7">}</span>
<span id="cb2-8">simulate_data <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>(seed, N, tau, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>) {</span>
<span id="cb2-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(seed)</span>
<span id="cb2-10">  </span>
<span id="cb2-11">  data <span class="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;">id =</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="cb2-12">  </span>
<span id="cb2-13">  <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;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>tau) {</span>
<span id="cb2-14">    Lt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"L_"</span>, t)</span>
<span id="cb2-15">    Ltd <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"L_"</span>, t, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"d"</span>)</span>
<span id="cb2-16">    </span>
<span id="cb2-17">    At <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A_"</span>, t)</span>
<span id="cb2-18">    Atd <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A_"</span>, t, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"d"</span>)</span>
<span id="cb2-19">    </span>
<span id="cb2-20">    Lt1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"L_"</span>, 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>
<span id="cb2-21">    Lt1d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"L_"</span>, 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"d"</span>)</span>
<span id="cb2-22">    </span>
<span id="cb2-23">    At1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A_"</span>, 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>
<span id="cb2-24">    At1d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A_"</span>, 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"d"</span>)</span>
<span id="cb2-25">    </span>
<span id="cb2-26">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(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>
<span id="cb2-27">      data[[Lt]] <span class="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 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>
<span id="cb2-28">      data[[Ltd]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data[[Lt]]</span>
<span id="cb2-29">      </span>
<span id="cb2-30">      data[[At]] <span class="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> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb2-31">    } </span>
<span id="cb2-32">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb2-33">      data[[Lt]] <span class="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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> data[[Lt1]], <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb2-34">      data[[Ltd]] <span class="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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> data[[Lt1d]], <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb2-35">      </span>
<span id="cb2-36">      data[[At]] <span class="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> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plogis</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.2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> data[[At1]] <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> data[[Lt1]]))</span>
<span id="cb2-37">    }</span>
<span id="cb2-38">    </span>
<span id="cb2-39">    data[[Atd]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mtp</span>(data, At)</span>
<span id="cb2-40">  }</span>
<span id="cb2-41">  data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>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;">rnorm</span>(N, data[[At]] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> data[[Lt]], sigma)</span>
<span id="cb2-42">  data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Yd <span class="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, data[[Atd]] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> data[[Ltd]], sigma)</span>
<span id="cb2-43">  data</span>
<span id="cb2-44">}</span>
<span id="cb2-45"></span>
<span id="cb2-46">simulated_data1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">simulate_data</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</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;">10000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-47"></span>
<span id="cb2-48"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(simulated_data1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Yd)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1.128593</code></pre>
</div>
<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">mtp <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>(data, trt) {</span>
<span id="cb4-2">  a <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data[[trt]]</span>
<span id="cb4-3">  a <span 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="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>
<span id="cb4-4">}</span>
<span id="cb4-5"></span>
<span id="cb4-6">simulated_data2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">simulate_data</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</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;">1000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb4-7"></span>
<span id="cb4-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(simulated_data2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Yd)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.114403</code></pre>
</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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(simulated_data1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Yd) <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>(simulated_data2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Yd)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1.01419</code></pre>
</div>
</div>
<p>Note in this simulation the variables ending with “d” are the variables under hypothetical intervention, or modified treatment policy. <img src="https://latex.codecogs.com/png.latex?L_1"> is from <img src="https://latex.codecogs.com/png.latex?uniform(0,1)">, and <img src="https://latex.codecogs.com/png.latex?L_2"> is from <img src="https://latex.codecogs.com/png.latex?N(0.25%20*%20L_1,%200.5)">. The treatment <img src="https://latex.codecogs.com/png.latex?A_1"> is from a Bernoulli distribution with probability 0.5, and the treatment <img src="https://latex.codecogs.com/png.latex?A_2"> is from a Bernoulli distribution with probability <img src="https://latex.codecogs.com/png.latex?plogis(0.5%20-%200.2%20*%20A_1%20+%200.1%20*%20L_2)">. The outcome <img src="https://latex.codecogs.com/png.latex?Y"> is from a normal distribution with mean <img src="https://latex.codecogs.com/png.latex?A2%20+%20L2">. In the simulated data, the modified treatment policy is to set the treatment to 0, then 1. The difference would be SATE.</p>
<p>In this case, we can just do a linear regression to get the effect of A2 on Y, knowing the exact DAG.</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">m1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> L_2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> A_1   <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> A_2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> simulated_data1)</span>
<span id="cb8-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(m1)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
glm(formula = Y ~ L_2 + A_1 + A_2, data = simulated_data1)

Coefficients:
              Estimate Std. Error t value Pr(&gt;|t|)    
(Intercept) -3.453e-05  9.615e-03  -0.004    0.997    
L_2          9.998e-01  9.889e-03 101.103   &lt;2e-16 ***
A_1         -6.042e-03  1.001e-02  -0.604    0.546    
A_2          1.000e+00  1.027e-02  97.380   &lt;2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for gaussian family taken to be 0.2500364)

    Null deviance: 7400.0  on 9999  degrees of freedom
Residual deviance: 2499.4  on 9996  degrees of freedom
AIC: 14523

Number of Fisher Scoring iterations: 2</code></pre>
</div>
</div>
<p>Let’s try a different MTP: set half of the time to 0, the other half remain unchanged.<br>
<img src="https://latex.codecogs.com/png.latex?%0Ad(a_t,%20%5Cepsilon_t)%20=%20%5Cbegin%7Bcases%7D%0A%20%20%20%200,%20&amp;%20%5Ctext%7Bif%20%7D%20%5Cepsilon_t%20%3C%20.5%20%5C%20and%20%5C%20a_t%20=1%20%5C%5C%0A%20%20%20%20a_t,%20&amp;%20%5Ctext%7Botherwise%7D%0A%5Cend%7Bcases%7D%0A"></p>
<p>This is, say, to set half of smokers to non-smokers, and the other half remain smokers.</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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># mtp &lt;- function(data, trt) {</span></span>
<span id="cb10-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   a &lt;- data[[trt]]</span></span>
<span id="cb10-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   epsilon &lt;- rbinom(nrow(data), size = 1, prob = 0.5)</span></span>
<span id="cb10-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   ifelse(epsilon &lt;.5 &amp; a == 1, 0, a)</span></span>
<span id="cb10-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># }</span></span>
<span id="cb10-6">d <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>(a) {</span>
<span id="cb10-7">  epsilon <span class="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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(a))</span>
<span id="cb10-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(epsilon <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;">0.5</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> a <span 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;">0</span>, a)</span>
<span id="cb10-9">}</span>
<span id="cb10-10">simulated_data1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>m3_d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> simulated_data1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y </span>
<span id="cb10-11"></span>
<span id="cb10-12">m2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glm</span>(m3_d <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> L_1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> A_1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> L_2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> A_2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> simulated_data1)</span>
<span id="cb10-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(m2)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
glm(formula = m3_d ~ L_1 + A_1 + L_2 + A_2, data = simulated_data1)

Coefficients:
             Estimate Std. Error t value Pr(&gt;|t|)    
(Intercept) -0.005815   0.012842  -0.453    0.651    
L_1          0.011964   0.017621   0.679    0.497    
A_1         -0.005956   0.010012  -0.595    0.552    
L_2          0.998831   0.009987 100.015   &lt;2e-16 ***
A_2          0.999873   0.010273  97.334   &lt;2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for gaussian family taken to be 0.2500499)

    Null deviance: 7400.0  on 9999  degrees of freedom
Residual deviance: 2499.2  on 9995  degrees of freedom
AIC: 14525

Number of Fisher Scoring iterations: 2</code></pre>
</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">simulated_data1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>m2_d <span class="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>(m2, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(simulated_data1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">A_2 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">d</span>(A_2)))</span>
<span id="cb12-2"></span>
<span id="cb12-3">m1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glm</span>(m2_d <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> L_1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> A_1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> simulated_data1)</span>
<span id="cb12-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(m1)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
glm(formula = m2_d ~ L_1 + A_1, data = simulated_data1)

Coefficients:
             Estimate Std. Error t value Pr(&gt;|t|)    
(Intercept)  0.277598   0.015072  18.418   &lt;2e-16 ***
L_1          0.289658   0.023474  12.339   &lt;2e-16 ***
A_1         -0.008247   0.013462  -0.613     0.54    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for gaussian family taken to be 0.4528119)

    Null deviance: 4596.0  on 9999  degrees of freedom
Residual deviance: 4526.8  on 9997  degrees of freedom
AIC: 20461

Number of Fisher Scoring iterations: 2</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">m1_d <span class="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>(m1, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(simulated_data1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">A_1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">d</span>(A_1)))</span>
<span id="cb14-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(m1_d)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.4197166</code></pre>
</div>
</div>
<p>Note this recursive process is based on Diaz, et al.&nbsp;(2023) “Nonparametric Causal Effects Based on Longitudinal Modified Treatment Policies”.</p>
<p>Here is how exactly we estimate it: we start with the last time point. Regress it on previous treatment and confounders, and then get the predicted value with <img src="https://latex.codecogs.com/png.latex?A"> changed based on the MTP. Regress that predicted value on the previous treatment and confounders, get predicted values with <img src="https://latex.codecogs.com/png.latex?A"> changed based on MTP. Repeat until the first time point. The average of the predicted value at time 1 is the expected value under this MTP.</p>
<p>This is basically g-formula extended to longitudinal data.</p>
<p>In a general case, this is the generalized g-formula to estimate <img src="https://latex.codecogs.com/png.latex?%5Ctheta">:</p>
<p>Set <img src="https://latex.codecogs.com/png.latex?m_%7B%5Ctau%20+%201%7D%20=%20Y">, let <img src="https://latex.codecogs.com/png.latex?A_t%5Ed%20=%20d(A_t,%20H_t)">. For <img src="https://latex.codecogs.com/png.latex?t%20=%20%5Ctau,%20%5Cldots,%201">, recursively define:</p>
<p><img src="https://latex.codecogs.com/png.latex?%20m_t%20:%20(a_t,%20h_t)%20%20=%20E%5Bm_%7Bt%20+%201%7D%20(A_%7Bt+1%7D%5Ed,%20H_%7Bt+1%7D)%20%7C%20A_t%20=%20a_t,%20H_t%20=h_t%5D%20"></p>
<p>Then <img src="https://latex.codecogs.com/png.latex?%5Ctheta%20=%20E%5Bm_1(A_1%5Ed,%20L_1)%5D">.</p>
<p>We start from the last period. Regress <img src="https://latex.codecogs.com/png.latex?m_%7B%5Ctau%20+%201%7D">, which is <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?A_%7B%5Ctau%7D"> and <img src="https://latex.codecogs.com/png.latex?H_%7B%5Ctau%7D">. Then get the predicted value with <img src="https://latex.codecogs.com/png.latex?A_%7B%5Ctau%7D"> changed based on the MTP. Then regress that predicted value on <img src="https://latex.codecogs.com/png.latex?A_%7B%5Ctau%20-%201%7D"> and <img src="https://latex.codecogs.com/png.latex?H_%7B%5Ctau%20-%201%7D">. Repeat until the first time point. The average of the predicted value at time 1 is the expected value under this MTP.</p>
</section>
<section id="estimators" class="level3">
<h3 class="anchored" data-anchor-id="estimators">Estimators</h3>
<p>The authors advocate two estimators, TMLE and SDR (sequentially doubly robust estimator). The procedures are the same, starting from the last time point, then apply TMLE or SDR, iterate to the first time point.</p>
</section>
</section>
<section id="using-lmtp-package" class="level2">
<h2 class="anchored" data-anchor-id="using-lmtp-package">Using lmtp package</h2>
<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;">library</span>(tidyverse)</span>
<span id="cb16-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lmtp)</span>
<span id="cb16-3">d1 <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>(data, trt) {</span>
<span id="cb16-4">  <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;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(data))</span>
<span id="cb16-5">}</span>
<span id="cb16-6">A <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A_2"</span></span>
<span id="cb16-7">Y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Y"</span></span>
<span id="cb16-8">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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"L_1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"L_2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A_1"</span>)</span>
<span id="cb16-9"></span>
<span id="cb16-10"><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;">34465</span>)</span>
<span id="cb16-11"></span>
<span id="cb16-12">treat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmtp_tmle</span>(</span>
<span id="cb16-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> simulated_data1, </span>
<span id="cb16-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trt =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A_2"</span>, </span>
<span id="cb16-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outcome =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Y"</span>, </span>
<span id="cb16-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">baseline =</span> W, </span>
<span id="cb16-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outcome_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"continuous"</span>, </span>
<span id="cb16-18">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shift =</span> d1, </span>
<span id="cb16-19">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">folds =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, </span>
<span id="cb16-20">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">learners_trt =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SL.glm"</span>, </span>
<span id="cb16-21">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">learners_outcome =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SL.glm"</span></span>
<span id="cb16-22">)</span>
<span id="cb16-23"></span>
<span id="cb16-24"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(treat)</span></code></pre></div></div>
<pre><code>LMTP Estimator: TMLE</code></pre>
<pre><code>Trt. Policy: (d1)</code></pre>
<pre><code></code></pre>
<pre><code>── Population intervention estimate ──</code></pre>
<pre><code></code></pre>
<pre><code>  Estimate: 1.117
Std. error: 0.008</code></pre>
<pre><code>95% Conf. int.: 1.101, 1.133</code></pre>
<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">d2 <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>(data, trt) {</span>
<span id="cb24-2">  <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>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(data))</span>
<span id="cb24-3">}</span>
<span id="cb24-4">A <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A_2"</span></span>
<span id="cb24-5">Y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Y"</span></span>
<span id="cb24-6">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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"L_1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"L_2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A_1"</span>)</span>
<span id="cb24-7"></span>
<span id="cb24-8"><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;">34465</span>)</span>
<span id="cb24-9"></span>
<span id="cb24-10">control <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmtp_tmle</span>(</span>
<span id="cb24-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> simulated_data1, </span>
<span id="cb24-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trt =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A_2"</span>, </span>
<span id="cb24-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outcome =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Y"</span>, </span>
<span id="cb24-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">baseline =</span> W, </span>
<span id="cb24-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outcome_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"continuous"</span>, </span>
<span id="cb24-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shift =</span> d2, </span>
<span id="cb24-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">folds =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, </span>
<span id="cb24-18">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">learners_trt =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SL.glm"</span>, </span>
<span id="cb24-19">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">learners_outcome =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SL.glm"</span></span>
<span id="cb24-20">)</span>
<span id="cb24-21"></span>
<span id="cb24-22"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(control)</span></code></pre></div></div>
<pre><code>LMTP Estimator: TMLE</code></pre>
<pre><code>Trt. Policy: (d2)</code></pre>
<pre><code></code></pre>
<pre><code>── Population intervention estimate ──</code></pre>
<pre><code></code></pre>
<pre><code>  Estimate: 0.117
Std. error: 0.009</code></pre>
<pre><code>95% Conf. int.: 0.099, 0.136</code></pre>
<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;">lmtp_contrast</span>(treat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ref =</span> control)</span></code></pre></div></div>
<pre><code>LMTP Contrast: additive</code></pre>
<pre><code>Null hypothesis: theta == 0</code></pre>
<p>shift ref estimate std.error conf.low conf.high p.value 1 1.12 0.117 1 0.0102 0.98 1.02 &lt;0.001</p>
</section>
<section id="another-example" class="level2">
<h2 class="anchored" data-anchor-id="another-example">another example</h2>
<p>The data set bmi, from the DynTxRegime package, are simulated to reflect a two-stage RCT {A1, A2} that studied the effect of meal replacement (MR) shakes versus a calorie deficit (CD) diet on adolescent</p>
<section id="shift-function-1" class="level3">
<h3 class="anchored" data-anchor-id="shift-function-1">shift function 1</h3>
<p>Consider a shift function that assigns meal replacement to all observations at time 1, but only meal replacement at time 2 to those observations whose 4-month BMI is greater than 30.</p>
<div class="cell">
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(DynTxRegime)</span>
<span id="cb35-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(bmiData)</span>
<span id="cb35-3">bmi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> bmiData</span>
<span id="cb35-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>(bmi)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Rows: 210
Columns: 8
$ gender      &lt;int&gt; 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1…
$ race        &lt;int&gt; 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0…
$ parentBMI   &lt;dbl&gt; 31.59683, 30.17564, 30.27918, 27.49256, 26.42350, 29.30970…
$ baselineBMI &lt;dbl&gt; 35.84005, 37.30396, 36.83889, 36.70679, 34.84207, 36.68640…
$ month4BMI   &lt;dbl&gt; 34.22717, 36.38014, 34.42168, 32.52011, 33.72922, 32.06622…
$ month12BMI  &lt;dbl&gt; 34.27263, 36.38401, 34.41447, 32.52397, 33.73546, 32.15977…
$ A1          &lt;chr&gt; "CD", "CD", "MR", "CD", "CD", "MR", "CD", "CD", "CD", "CD"…
$ A2          &lt;chr&gt; "MR", "MR", "CD", "CD", "CD", "MR", "MR", "CD", "MR", "MR"…</code></pre>
</div>
<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">d_dtr <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>(data, trt) {</span>
<span id="cb37-2">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (trt <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;">"A1"</span>) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<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;">"MR"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(data)))</span>
<span id="cb37-3">  </span>
<span id="cb37-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>month4BMI <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MR"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CD"</span>)</span>
<span id="cb37-5">}</span>
<span id="cb37-6"></span>
<span id="cb37-7">fit_dtr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmtp_sdr</span>(</span>
<span id="cb37-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> bmi, </span>
<span id="cb37-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trt =</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;">"A1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A2"</span>), </span>
<span id="cb37-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outcome =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"month12BMI"</span>, </span>
<span id="cb37-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">baseline =</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;">"gender"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"race"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"parentBMI"</span>), </span>
<span id="cb37-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time_vary =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"baselineBMI"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"month4BMI"</span>),</span>
<span id="cb37-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shift =</span> d_dtr, </span>
<span id="cb37-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outcome_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"continuous"</span>,</span>
<span id="cb37-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">folds =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb37-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">learners_trt =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SL.glm"</span>, </span>
<span id="cb37-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">learners_outcome =</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;">"SL.mean"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SL.glm"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SL.gam"</span>)</span>
<span id="cb37-18">)</span>
<span id="cb37-19"></span>
<span id="cb37-20">fit_dtr</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>      Estimate: 35.853
    Std. error: 0.362</code></pre>
</div>
</div>
</section>
<section id="shift-function-2" class="level3">
<h3 class="anchored" data-anchor-id="shift-function-2">shift function 2</h3>
<p>Suppose we are interested in comparing the dynamic treatment regime to a static treatment regime where all patients receive meal replacement at both time points. Using the SDR estimator, estimate the effect of this static intervention.</p>
<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">fit_MR <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmtp_sdr</span>(</span>
<span id="cb39-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> bmi, </span>
<span id="cb39-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trt =</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;">"A1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A2"</span>), </span>
<span id="cb39-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outcome =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"month12BMI"</span>, </span>
<span id="cb39-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">baseline =</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;">"gender"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"race"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"parentBMI"</span>), </span>
<span id="cb39-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time_vary =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"baselineBMI"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"month4BMI"</span>),</span>
<span id="cb39-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shift =</span> \(data, trt) <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;">"MR"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(data)), </span>
<span id="cb39-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outcome_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"continuous"</span>,</span>
<span id="cb39-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">folds =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb39-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">learners_trt =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SL.glm"</span>, </span>
<span id="cb39-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">learners_outcome =</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;">"SL.mean"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SL.glm"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SL.gam"</span>)</span>
<span id="cb39-12">)</span>
<span id="cb39-13"></span>
<span id="cb39-14">fit_MR</span></code></pre></div></div>
<pre><code>LMTP Estimator: SDR</code></pre>
<pre><code>Trt. Policy: (function(data, trt) rep("MR", nrow(data)))</code></pre>
<pre><code></code></pre>
<pre><code>── Population intervention estimate ──</code></pre>
<pre><code></code></pre>
<pre><code>  Estimate: 35.831
Std. error: 0.361</code></pre>
<pre><code>95% Conf. int.: 35.123, 36.539</code></pre>
<p>Let’s also estimate the effect of an intervention where all patients receive a calorie deficit diet at both time points.</p>
<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">fit_CD <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmtp_sdr</span>(</span>
<span id="cb47-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> bmi, </span>
<span id="cb47-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trt =</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;">"A1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A2"</span>), </span>
<span id="cb47-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outcome =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"month12BMI"</span>, </span>
<span id="cb47-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">baseline =</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;">"gender"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"race"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"parentBMI"</span>), </span>
<span id="cb47-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time_vary =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"baselineBMI"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"month4BMI"</span>),</span>
<span id="cb47-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shift =</span> \(data, trt) <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;">"CD"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(data)), </span>
<span id="cb47-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outcome_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"continuous"</span>,</span>
<span id="cb47-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">folds =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb47-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">learners_trt =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SL.glm"</span>, </span>
<span id="cb47-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">learners_outcome =</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;">"SL.mean"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SL.glm"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SL.gam"</span>)</span>
<span id="cb47-12">)</span>
<span id="cb47-13"></span>
<span id="cb47-14">fit_CD</span></code></pre></div></div>
<pre><code>LMTP Estimator: SDR</code></pre>
<pre><code>Trt. Policy: (function(data, trt) rep("CD", nrow(data)))</code></pre>
<pre><code></code></pre>
<pre><code>── Population intervention estimate ──</code></pre>
<pre><code></code></pre>
<pre><code>  Estimate: 35.053
Std. error: 0.301</code></pre>
<pre><code>95% Conf. int.: 34.463, 35.643</code></pre>
<p>Finally, we can compare the three treatment regimes using the <code>lmtp_contrast</code> function.</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;">lmtp_contrast</span>(fit_dtr, fit_MR, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ref =</span> fit_CD)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
  shift  ref estimate std.error conf.low conf.high p.value
1  35.9 35.1    0.801     0.335    0.144      1.46  0.0169
2  35.8 35.1    0.778     0.335    0.122      1.43  0.0201</code></pre>
</div>
</div>


</section>
</section>

 ]]></description>
  <category>R</category>
  <category>code</category>
  <category>analysis</category>
  <guid>https://xiangao.github.io/posts/lmtp/</guid>
  <pubDate>Sat, 23 Aug 2025 04:00:00 GMT</pubDate>
</item>
</channel>
</rss>
