R 在虚拟变量(或接近)之前/之后一年创建事件(虚拟)

R 在虚拟变量(或接近)之前/之后一年创建事件(虚拟),r,date,dummy-variable,panel-data,R,Date,Dummy Variable,Panel Data,我在一个不平衡的泛数据集中做一个事件研究。基本结构是,在大约15年的时间里,我在不同的时间点对每个公司进行了不同数量的观察(交付)。我对一个事件(价格上涨)感兴趣,如果该事件发生,则将其编码为虚拟变量,并通过一些虚拟超前和滞后来检查价格上涨对我的因变量的影响是否在该事件前后变得明显。例如,对于一些公司来说,价格上涨发生在5次交货时,例如15年内50次 然而,现在我还想在一年后和一年前“模拟”同一事件研究,以改进推理。因此,我希望R在最接近一年前和一年后的交付时,为每个公司复制事件虚拟。交货日期不

我在一个不平衡的泛数据集中做一个事件研究。基本结构是,在大约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))

我想根据每个公司的价格上涨和日期,在右边创建两个虚拟列。虽然我会从dyplr的
groupby
mutate
方法和
if-else
功能开始,但我不知道如何创建一个条件,当一年内的交付距离上一年或下一年的日期近+1/-1个月时,该条件变为
TRUE
,以及如何选择相应的交付。你们有什么想法吗?

这里有一个可能的方法,使用
dplyr

分组依据(公司id)
之后,
过滤
并包括价格上涨的分组

然后,如果日期是
涨价等于1的日期之前或之后的一年(+/-30天),则创建两个虚拟变量。然后,将
筛选满足这些条件的行

使用
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)
库(dplyr)
df$日期%
分组依据(公司id)%>%
过滤器(任何(价格上涨==1))%>%
变异(
前一年价格上涨=如果其他(
介于(日期[价格上涨==1]-日期335395),1,0)之间,
价格上涨年后=如果其他(
介于(日期-日期[价格上涨==1],335395),1,0),
) %>%
过滤器(价格(上涨)年份(前==1 |价格(上涨)年份(后==1)%>%
不同(公司id、前一年价格上涨、后一年价格上涨、.keep\u all=TRUE)%>%
右联合(df)%>%
替换(列表(涨价年前=0,涨价年后=0))%>%
安排(公司id、日期)
输出

   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
firm\u id交货\u id日期价格\u涨价\u涨价\u年前\u涨价\u年后
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

为什么在
价格上涨之后的一年中只有一个1?此外,您共享的数据似乎不完整。你能纠正吗?嗨,罗纳克,谢谢你的评论。原因是因为
公司id=1
的价格上涨一年后,下一次交货(
交货id=15
)将在两年后进行,超出了一年+1/-1个月的范围。感谢您的解决方案。它完全符合我的计划(你对
独特的
部分绝对正确。我发现了一种不那么优雅的方法,因为我不知道你可以在
as.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