'How to create treatment variables to specify pre- and post-treatment periods for diff-in-diff in r?
I am performing a diff-in-diff analysis with three interventions: the first in 2014, the second in 2015 and the third in 2018. My code is:
set.seed(30)
n <-9
dat <- data.frame(ID=1:n,
year=c("2020", "2019", "2018", "2017", "2016", "2015", "2014", "2013", "2012"))
dat
ID year
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
In order to perform the analysis, I need three columns in my data frame which indicate for each threatment period the pre- or post-treatment period with either a 0 or 1. I tried it myself and do always receive an error message saying "Error in mutate():
! Problem while computing treatment = ifelse(year < 2014, "0", ifelse(year >= 2015, "1")).
i The error occurred in row 7.
Caused by error in ifelse():
! Argument "no" fehlt (ohne Standardwert)
Backtrace:
- all %>% rowwise %>% ...
- base::ifelse(year >= 2015, "1""
My code is:
all <- all %>% rowwise %>% mutate(
treatment = ifelse(year < 2014, "0",
ifelse(year >= 2015, "1"
)))
all <- all %>% rowwise %>% mutate(
treatment1 = ifelse(year >= 2013 && year < 2015, "0",
ifelse(year < 2018, "1"
)))
all <- all %>% rowwise %>% mutate(
treatment2 = ifelse(year >= 2015 && year < 2018, "0",
ifelse(year >= 2018, "1"
)))
I am trying here not to have any overlaps in the periods to create unique periods in order to better extract the effects of each intervention. Unfortunately, I don't see the solution here. Any help would be amazing and much appreciated!
Best, Tobias
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
