|
1 | 1 | { |
2 | 2 | "cells": [ |
3 | 3 | { |
4 | | - "attachments": {}, |
5 | 4 | "cell_type": "markdown", |
6 | 5 | "metadata": {}, |
7 | 6 | "source": [ |
|
38 | 37 | ] |
39 | 38 | }, |
40 | 39 | { |
41 | | - "attachments": {}, |
42 | 40 | "cell_type": "markdown", |
43 | 41 | "metadata": {}, |
44 | 42 | "source": [ |
|
72 | 70 | ] |
73 | 71 | }, |
74 | 72 | { |
75 | | - "attachments": {}, |
76 | 73 | "cell_type": "markdown", |
77 | 74 | "metadata": {}, |
78 | 75 | "source": [ |
|
108 | 105 | ] |
109 | 106 | }, |
110 | 107 | { |
111 | | - "attachments": {}, |
112 | 108 | "cell_type": "markdown", |
113 | 109 | "metadata": {}, |
114 | 110 | "source": [ |
|
313 | 309 | ] |
314 | 310 | }, |
315 | 311 | { |
316 | | - "attachments": {}, |
317 | 312 | "cell_type": "markdown", |
318 | 313 | "metadata": {}, |
319 | 314 | "source": [ |
|
665 | 660 | ] |
666 | 661 | }, |
667 | 662 | { |
668 | | - "attachments": {}, |
669 | 663 | "cell_type": "markdown", |
670 | 664 | "metadata": {}, |
671 | 665 | "source": [ |
|
826 | 820 | ] |
827 | 821 | }, |
828 | 822 | { |
829 | | - "cell_type": "markdown", |
830 | | - "metadata": {}, |
831 | | - "source": [ |
832 | | - "## Analysis 4 - Two-way fixed effects\n", |
833 | | - "\n", |
834 | | - "Finally, we can evaluate the difference-in-differences model in its two-way fixed effects (TWFE) formulation. The TWFE model is widely used in econometrics for causal inference in panel data. It accounts for both unit-specific effects (e.g., differences between districts) and time-specific effects (e.g., shocks or trends affecting all units simultaneously).\n", |
835 | | - "\n", |
836 | | - "The TWFE model is equivalent to the classic 2\\u00d72 DiD model (Model 1) only in the special case of two groups and two time periods. Outside of this case the approaches diverge, and TWFE can have important limitations {cite:p}`hill2020limitations,imai2021twfepanel`. Readers should proceed with caution when applying TWFE in richer settings {cite:p}`kropko2018two,collischon2020let`.\n", |
837 | | - "\n", |
838 | | - "The TWFE approach is similar to the previous model in that the `district:post_treatment` interaction term still defines a treatment indicator variable, and its coefficient $\\Delta$ represents the causal effect of the intervention. The difference is that there is no _linear_ `year` term\\u2014instead we use a _categorical_ year variable. This allows the model to flexibly capture any time-specific shocks that affect all units.\n", |
839 | | - "\n", |
840 | | - "The expected values can be written as:\n", |
841 | | - "\n", |
842 | | - "$$\n", |
843 | | - "\\\\begin{aligned}\n", |
844 | | - "\\\\mu_i \\; &= \\; \\\\gamma \\quad \\\\textcolor{salmon}{\\\\text{(global intercept)}} \\\\\\\\\n", |
845 | | - "&+ \\\\alpha[district_i] \\quad \\\\textcolor{salmon}{\\\\text{(unit fixed effect)}} \\\\\\\\\n", |
846 | | - "&+ \\\\beta[year_i] \\quad \\\\textcolor{salmon}{\\\\text{(time fixed effect)}} \\\\\\\\\n", |
847 | | - "&+ \\\\Delta \\\\cdot district_i \\\\cdot post\\\\_treatment_i \\quad \\\\textcolor{salmon}{\\\\text{(treatment indicator)}}\n", |
848 | | - "\\\\end{aligned}\n", |
849 | | - "$$\n", |
850 | | - "\n", |
851 | | - "Here:\n", |
852 | | - "* $\\\\gamma$ is a scalar global intercept.\n", |
853 | | - "* $\\\\alpha[district_i]$ is a vector of unit (district) fixed effects. With 2 districts, this is a vector of length 2. The index $district_i$ selects the effect for the $i^{th}$ observation.\n", |
854 | | - "* $\\\\beta[year_i]$ is a vector of time (year) fixed effects. With 6 years, this is a vector of length 6. The index $year_i$ selects the effect for the $i^{th}$ observation.\n", |
855 | | - "* $\\\\Delta$ is a scalar treatment effect, the coefficient on the `district:post_treatment` interaction term.\n" |
856 | | - ] |
| 823 | + "cell_type": "markdown", |
| 824 | + "metadata": {}, |
| 825 | + "source": [ |
| 826 | + "## Analysis 4 - Two-way fixed effects\n", |
| 827 | + "\n", |
| 828 | + "Finally, we can evaluate the difference-in-differences model in its two-way fixed effects (TWFE) formulation. The TWFE model is widely used in econometrics for causal inference in panel data. It accounts for both unit-specific effects (e.g., differences between districts) and time-specific effects (e.g., shocks or trends affecting all units simultaneously).\n", |
| 829 | + "\n", |
| 830 | + "The TWFE model is equivalent to the classic 2$\\times$2 DiD model (Model 1) only in the special case of two groups and two time periods. Outside of this case the approaches diverge, and TWFE can have important limitations {cite:p}`hill2020limitations,imai2021twfepanel`. Readers should proceed with caution when applying TWFE in richer settings {cite:p}`kropko2018two,collischon2020let`.\n", |
| 831 | + "\n", |
| 832 | + "The TWFE approach is similar to the previous model in that the `district:post_treatment` interaction term still defines a treatment indicator variable, and its coefficient $\\Delta$ represents the causal effect of the intervention. The difference is that there is no _linear_ `year` term, instead we use a _categorical_ year variable. This allows the model to flexibly capture any time-specific shocks that affect all units.\n", |
| 833 | + "\n", |
| 834 | + "The expected values can be written as:\n", |
| 835 | + "\n", |
| 836 | + "$$\n", |
| 837 | + "\\begin{aligned}\n", |
| 838 | + "\\mu_i &= \\gamma && \\color{#FA8072}{\\text{(global intercept)}} \\\\\n", |
| 839 | + "&+ \\alpha[\\text{district}_i] && \\color{#FA8072}{\\text{(unit fixed effect)}} \\\\\n", |
| 840 | + "&+ \\beta[\\text{year}_i] && \\color{#FA8072}{\\text{(time fixed effect)}} \\\\\n", |
| 841 | + "&+ \\Delta \\cdot \\text{district}_i \\cdot \\text{post\\_treatment}_i && \\color{#FA8072}{\\text{(treatment indicator)}}\n", |
| 842 | + "\\end{aligned}\n", |
| 843 | + "$$\n", |
| 844 | + "\n", |
| 845 | + "Here: \n", |
| 846 | + "* $\\gamma$ is a scalar global intercept. \n", |
| 847 | + "* $\\alpha[\\text{district}_i]$ is a vector of unit (district) fixed effects. With 2 districts, this is a vector of length 2. The index $\\text{district}_i$ selects the effect for the $i^{\\text{th}}$ observation. \n", |
| 848 | + "* $\\beta[\\text{year}_i]$ is a vector of time (year) fixed effects. With 6 years, this is a vector of length 6. The index $\\text{year}_i$ selects the effect for the $i^{\\text{th}}$ observation. \n", |
| 849 | + "* $\\Delta$ is a scalar treatment effect, corresponding to the coefficient on the `district:post_treatment` interaction term.\n" |
| 850 | + ] |
857 | 851 | }, |
858 | 852 | { |
859 | 853 | "cell_type": "code", |
|
1050 | 1044 | ] |
1051 | 1045 | }, |
1052 | 1046 | { |
1053 | | - "attachments": {}, |
1054 | 1047 | "cell_type": "markdown", |
1055 | 1048 | "metadata": {}, |
1056 | 1049 | "source": [ |
|
1063 | 1056 | ], |
1064 | 1057 | "metadata": { |
1065 | 1058 | "kernelspec": { |
1066 | | - "display_name": "CausalPy", |
| 1059 | + "display_name": "Python 3 (ipykernel)", |
1067 | 1060 | "language": "python", |
1068 | 1061 | "name": "python3" |
1069 | 1062 | }, |
|
1077 | 1070 | "name": "python", |
1078 | 1071 | "nbconvert_exporter": "python", |
1079 | 1072 | "pygments_lexer": "ipython3", |
1080 | | - "version": "3.13.2" |
| 1073 | + "version": "3.9.18" |
1081 | 1074 | } |
1082 | 1075 | }, |
1083 | 1076 | "nbformat": 4, |
|
0 commit comments