{ "cells": [ { "cell_type": "markdown", "id": "e401eb2c", "metadata": {}, "source": [ "# R: DoubleML for Difference-in-Differences" ] }, { "cell_type": "markdown", "id": "8ff5d0ae", "metadata": {}, "source": [ "In this example, we demonstrate, how `DoubleML` can be used in combination with the [did package for R](https://bcallaway11.github.io/did/index.html) in order to estimate group-time average treatment effects in difference-in-difference (DiD) models with multiple periods." ] }, { "cell_type": "code", "execution_count": 1, "id": "e8e29a2f", "metadata": { "execution": { "iopub.execute_input": "2024-08-31T09:43:43.070559Z", "iopub.status.busy": "2024-08-31T09:43:43.069050Z", "iopub.status.idle": "2024-08-31T09:43:43.573195Z", "shell.execute_reply": "2024-08-31T09:43:43.571993Z" }, "vscode": { "languageId": "r" } }, "outputs": [], "source": [ "library(DoubleML)\n", "library(did)\n", "library(mlr3)\n", "library(mlr3learners)\n", "\n", "# suppress messages during fitting\n", "lgr::get_logger(\"mlr3\")$set_threshold(\"warn\")\n", "\n", "set.seed(1234)\n" ] }, { "cell_type": "markdown", "id": "19aaa906", "metadata": {}, "source": [ "# Demo Example from `did`\n", "\n", "We will demonstrate the use of `DoubleML` for DiD in the [introductory example](https://bcallaway11.github.io/did/articles/did-basics.html) of the `did` package. " ] }, { "cell_type": "code", "execution_count": 2, "id": "c5d9b7ac", "metadata": { "execution": { "iopub.execute_input": "2024-08-31T09:43:43.597534Z", "iopub.status.busy": "2024-08-31T09:43:43.575619Z", "iopub.status.idle": "2024-08-31T09:43:43.850466Z", "shell.execute_reply": "2024-08-31T09:43:43.849368Z" }, "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "15916" ], "text/latex": [ "15916" ], "text/markdown": [ "15916" ], "text/plain": [ "[1] 15916" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
G | X | id | cluster | period | Y | treat |
---|---|---|---|---|---|---|
<dbl> | <dbl> | <int> | <int> | <dbl> | <dbl> | <dbl> |
3 | -0.8762330 | 1 | 5 | 1 | 5.562556 | 1 |
3 | -0.8762330 | 1 | 5 | 2 | 4.349213 | 1 |
3 | -0.8762330 | 1 | 5 | 3 | 7.134037 | 1 |
3 | -0.8762330 | 1 | 5 | 4 | 6.243056 | 1 |
2 | -0.8738481 | 2 | 36 | 1 | -3.659387 | 1 |
2 | -0.8738481 | 2 | 36 | 2 | -1.274099 | 1 |