{"id":1018,"date":"2025-08-10T10:28:40","date_gmt":"2025-08-10T14:28:40","guid":{"rendered":"https:\/\/www.clayford.net\/statistics\/?p=1018"},"modified":"2025-08-10T10:28:40","modified_gmt":"2025-08-10T14:28:40","slug":"the-average-treatment-effect-figure-20-15-of-regression-and-other-stories","status":"publish","type":"post","link":"https:\/\/www.clayford.net\/statistics\/the-average-treatment-effect-figure-20-15-of-regression-and-other-stories\/","title":{"rendered":"The average treatment effect: Figure 20.15 of Regression and Other Stories"},"content":{"rendered":"<p>Chapter 20 of Regression and Other Stories presents a toy example in section 20.8 of calculating an average treatment effect using weights based on the estimated inverse probability of treatment assignment. They leave out several steps in how they arrive at their values. In this post I fill in those steps. The data is presented in figure 20.15, which is actually a table. Here is part of the table.<\/p>\n<pre class=\"r\"><code>n &lt;- c(80, 50, 20, 50)\r\nx &lt;- c(0, 1, 0, 1)\r\nt &lt;- c(0, 0, 1, 1)\r\ny &lt;- c(110, 90, 120, 90)\r\nd &lt;- data.frame(n, x, t, y)\r\nd<\/code><\/pre>\n<pre><code>##    n x t   y\r\n## 1 80 0 0 110\r\n## 2 50 1 0  90\r\n## 3 20 0 1 120\r\n## 4 50 1 1  90<\/code><\/pre>\n<p>The first column, n, tells us the sample size for each condition. This columns sums to 200 total subjects. The second column, x, is a binary covariate. The third column, t, tells us the treatment (0 = control, 1 = treated). And the fourth column, y, tells us the outcome.<\/p>\n<p>A naive estimate of the average treatment effect can be calculated as follows. Notice we need to use the sample size to accurately calculate the mean.<\/p>\n<pre class=\"r\"><code>weighted.mean(y[3:4], n[3:4]) - weighted.mean(y[1:2], n[1:2])<\/code><\/pre>\n<pre><code>## [1] -3.736264<\/code><\/pre>\n<p>This indicates the treatment causes a small decrease in the outcome. But this is wrong because the data is unbalanced in the covariate. In the control group (t = 0), there are 80 subjects with x = 0 and 50 with x = 1. However, the treatment group (t = 1) has 20 subjects with x = 0 and 50 subjects with x = 1. Using propensity scores we can reweight the data such that\u2019s it\u2019s balanced in the covariate.<\/p>\n<p>The propensity score is the estimated probability of receiving the treatment. When x = 0, the estimated probability of receiving the treatment is 20\/(20 + 80), or 0.2. When x = 1, the estimated probability of receiving the treatment is 50\/(50 + 50), or 0.5.<\/p>\n<pre class=\"r\"><code>d$p &lt;- c(0.2, 0.5, 0.2, 0.5)\r\nd<\/code><\/pre>\n<pre><code>##    n x t   y   p\r\n## 1 80 0 0 110 0.2\r\n## 2 50 1 0  90 0.5\r\n## 3 20 0 1 120 0.2\r\n## 4 50 1 1  90 0.5<\/code><\/pre>\n<p>We calculate weights by taking the inverse of the propensity score. Treated units receive weights of 1\/p, while control units receive weights of 1\/(1 &#8211; p). The weights for control units are the inverse of the probability they ended up in the control group.<\/p>\n<pre class=\"r\"><code>d$w &lt;- ifelse(d$t == 1, 1\/d$p, 1\/(1 - d$p))\r\nd<\/code><\/pre>\n<pre><code>##    n x t   y   p    w\r\n## 1 80 0 0 110 0.2 1.25\r\n## 2 50 1 0  90 0.5 2.00\r\n## 3 20 0 1 120 0.2 5.00\r\n## 4 50 1 1  90 0.5 2.00<\/code><\/pre>\n<p>Next we \u201cnorm\u201d the weights so they average to 1. This is carried out by dividing by the average of the unnormed weights. The average of the unnormed weights needs to be calculated as a weighted mean. Then we can do the division.<\/p>\n<pre class=\"r\"><code>avg &lt;- weighted.mean(d$w, d$n)\r\nd$nw &lt;- d$w\/avg\r\nd<\/code><\/pre>\n<pre><code>##    n x t   y   p    w    nw\r\n## 1 80 0 0 110 0.2 1.25 0.625\r\n## 2 50 1 0  90 0.5 2.00 1.000\r\n## 3 20 0 1 120 0.2 5.00 2.500\r\n## 4 50 1 1  90 0.5 2.00 1.000<\/code><\/pre>\n<p>To get the reweighted data (or pseudo-population) that is balanced in the covariate, we multiply the group samples sizes by the normed weight.<\/p>\n<pre class=\"r\"><code># pseudo-population\r\nd$pn &lt;- d$nw * n\r\nd<\/code><\/pre>\n<pre><code>##    n x t   y   p    w    nw pn\r\n## 1 80 0 0 110 0.2 1.25 0.625 50\r\n## 2 50 1 0  90 0.5 2.00 1.000 50\r\n## 3 20 0 1 120 0.2 5.00 2.500 50\r\n## 4 50 1 1  90 0.5 2.00 1.000 50<\/code><\/pre>\n<p>Notice we have balance across the groups. Each group now has a pseudo-population of 50. Now we can estimate the average treatment effect, which comes out to the true value of 5.<\/p>\n<pre class=\"r\"><code>weighted.mean(d$y[3:4], d$pn[3:4]) - weighted.mean(d$y[1:2], d$pn[1:2])<\/code><\/pre>\n<pre><code>## [1] 5<\/code><\/pre>\n<p>The reason we know 5 is the correct value is because the authors provide the hypothetical \u201cpotential outcomes\u201d for both treatment groups in the table. y0 is the potential outcome when untreated and y1 is the potential outcome when treated.<\/p>\n<pre class=\"r\"><code>data.frame(t = t, \r\n           y0 = c(110, 90, 110, 90), \r\n           y1 = c(120, 90, 120, 90))<\/code><\/pre>\n<pre><code>##   t  y0  y1\r\n## 1 0 110 120\r\n## 2 0  90  90\r\n## 3 1 110 120\r\n## 4 1  90  90<\/code><\/pre>\n<p>With this information we can determine the true average treatment effect is 5.<\/p>\n<pre class=\"r\"><code>mean(c(120,90)) - mean(c(110,90))<\/code><\/pre>\n<pre><code>## [1] 5<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Chapter 20 of Regression and Other Stories presents a toy example in section 20.8 of calculating an average treatment effect&#8230; <a class=\"read-more\" href=\"https:\/\/www.clayford.net\/statistics\/the-average-treatment-effect-figure-20-15-of-regression-and-other-stories\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[90,89,84],"class_list":["post-1018","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-average-treatment-effect","tag-propensity-score","tag-regression-and-other-stories"],"_links":{"self":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/1018","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/comments?post=1018"}],"version-history":[{"count":2,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/1018\/revisions"}],"predecessor-version":[{"id":1020,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/1018\/revisions\/1020"}],"wp:attachment":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/media?parent=1018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/categories?post=1018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/tags?post=1018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}