r-在伪变量(或接近)之前/之后一年创建事件(伪)



我正在不平衡的panal数据集中进行事件研究。基本结构是,在大约15年的时间里,我在不同的时间点对每家公司进行了不同数量的观察(交付(。我对一个事件(价格上涨(感兴趣,如果它发生了,它被编码为一个伪变量,并有一些伪超前和滞后,以检查价格上涨对我的因变量的影响是否在该事件周围变得明显。例如,对于一些公司来说,价格上涨发生在5次交付时,例如在15年内交付50次。

然而,现在我也想"模拟";在一年后和一年前对同一事件进行研究,以提高推理能力。因此,我希望R在最接近一年前和一年后的交付中为每家公司复制事件假人。交货日期不是每天都有,而是平均每25天一次。

因此,作为代码,数据看起来像这样:

df <- data.frame(firm_id = c(1,1,1,1,1,2,2,2,3,3,3,3,3,3,3,3,3,3,4,4,4,4),
delivery_id = c(1,2,6,9,15,3,5,18,4,7,8,10,11,13,17,19,22,12,14,16,20,21),
date=c("2004-06-16", "2004-08-12", "2004-11-22", "2005-07-03", "2007-01-04",
"2004-09-07", "2005-02-01", "2006-01-17", 
"2004-10-11", "2005-02-01", "2005-04-27", "2005-06-01", "2005-07-01",
"2006-01-03", "2007-01-06", "2007-03-24", "2007-05-03", 
"2005-08-03", "2006-02-19", "2006-06-13", "2007-02-04", "2007-04-26"),
price_increase = c(0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0),
price_increase_year_before = c(1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0),
price_increase_year_afer = c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0))

创建

firm_id delivery_id  date     price_increase  price_increase_year_before   price_increase_year_after
1        1           1 2004-06-16              0                          1                        0
2        1           2 2004-08-12              0                          0                        0
3        1           6 2004-11-22              0                          0                        0
4        1           9 2005-07-03              1                          0                        0
5        1          15 2007-01-04              0                          0                        0
6        2           3 2004-09-07              0                          0                        0
7        2           5 2005-02-01              0                          0                        0
8        2          18 2006-01-17              0                          0                        0
9        3           4 2004-10-11              0                          0                        0
10       3           7 2005-02-01              0                          1                        0
11       3           8 2005-04-27              0                          0                        0
12       3          10 2005-06-01              0                          0                        0
13       3          11 2005-07-01              0                          0                        0
14       3          13 2006-01-03              1                          0                        0
15       3          17 2007-01-06              0                          0                        1
16       3          19 2007-03-24              0                          0                        0
17       3          22 2007-05-03              0                          0                        0
18       3          12 2005-08-03              0                          0                        0
19       4          14 2006-02-19              0                          0                        0
20       4          16 2006-06-13              0                          0                        0
21       4          20 2007-02-04              0                          0                        0
22       4          21 2007-04-26              0                          0                        0

我想在右边为每个公司创建两个基于price_increation和date的伪列。虽然我会从dyplr的group_bymutate方法以及if_else函数开始,但我不知道如何创建一个条件,当一年中的交付距离上一年或下一年的日期+1/-1个月时,该条件变为TRUE,也不知道如何选择相应的交付。你们有主意吗?

这里有一种使用dplyr的可能方法。

group_by(firm_id)filter之后,包括价格上涨的组。

然后,如果日期在price_increase等于1的日期之前或之后一年(+/-30天(,则创建两个伪变量。那么,对于满足这些条件的行,将使用filter

使用distinct,您可以防止组/公司中虚拟变量的倍数或重复。否则,如果你的交货间隔25天,这似乎是一种理论上的可能性。

之后的其余部分是连接回原始数据,将空列的NA替换为零,并进行排序。

library(dplyr)
df$date <- as.Date(df$date)
df %>%
group_by(firm_id) %>%
filter(any(price_increase == 1)) %>%
mutate(
price_increase_year_before = ifelse(
between(date[price_increase == 1] - date, 335, 395), 1, 0),
price_increase_year_after = ifelse(
between(date - date[price_increase == 1], 335, 395), 1, 0),
) %>%
filter(price_increase_year_before == 1 | price_increase_year_after == 1) %>%
distinct(firm_id, price_increase_year_before, price_increase_year_after, .keep_all = TRUE) %>%
right_join(df) %>%
replace_na(list(price_increase_year_before = 0, price_increase_year_after = 0)) %>%
arrange(firm_id, date)

输出

firm_id delivery_id date       price_increase price_increase_year_before price_increase_year_after
<dbl>       <dbl> <date>              <dbl>                      <dbl>                     <dbl>
1       1           1 2004-06-16              0                          1                         0
2       1           2 2004-08-12              0                          0                         0
3       1           6 2004-11-22              0                          0                         0
4       1           9 2005-07-03              1                          0                         0
5       1          15 2007-01-04              0                          0                         0
6       2           3 2004-09-07              0                          0                         0
7       2           5 2005-02-01              0                          0                         0
8       2          18 2006-01-17              0                          0                         0
9       3           4 2004-10-11              0                          0                         0
10       3           7 2005-02-01              0                          1                         0
11       3           8 2005-04-27              0                          0                         0
12       3          10 2005-06-01              0                          0                         0
13       3          11 2005-07-01              0                          0                         0
14       3          12 2005-08-03              0                          0                         0
15       3          13 2006-01-03              1                          0                         0
16       3          17 2007-01-06              0                          0                         1
17       3          19 2007-03-24              0                          0                         0
18       3          22 2007-05-03              0                          0                         0
19       4          14 2006-02-19              0                          0                         0
20       4          16 2006-06-13              0                          0                         0
21       4          20 2007-02-04              0                          0                         0
22       4          21 2007-04-26              0                          0                         0

最新更新