如何在r中的不平衡面板数据中为事件留出时间

  • 本文关键字:事件 数据 时间 不平衡 r
  • 更新时间 :
  • 英文 :


我有不平衡的面板数据,其中有一个二进制变量指示事件是否发生。我想控制时间依赖性。这样做的方法是控制事件发生后经过的时间。

这是一个可重复的例子,用一个我试图实现的向量。谢谢

   id year onset time_since_event
1   1 1989     0                0
2   1 1990     0                1
3   1 1991     1                2
4   1 1992     0                0
5   1 1993     0                1
6   1 1994     0                2  
7   2 1989     0                0
8   2 1990     1                1
9   2 1991     0                0
10  2 1992     1                1
11  2 1993     0                2
12  2 1994     0                3  
13  3 1991     0                0
14  3 1992     0                1
15  3 1993     0                2  

˚

id <- c(1,1,1,1,1,2,2,2,2,3,3)
year <- c(1989,1990,1991,1992,1993,1994,1989,1990,1991,1992,1993,1994,1991,1992,1993)
onset <- c(0,0,1,0,0,0,0,1,0,1,0,0,0,0)
time_since_event<-c(0,1,2,0,1,2,0,1,2,3,0,1,2) #what I want to create
df <- data.frame(cbind(id, year, onset,time_since_event))

试试这个:

id <- c(1,1,1,1,1,2,2,2,2,3,3)
year <- c(1989,1990,1991,1992,1993,1989,1990,1991,1992,1991,1992)
onset <- c(0,0,1,0,0,0,1,0,1,0,0)
period <- c(0, cumsum(onset)[-length(onset)])
time_since_event <- ave(year, id, period, FUN=function(x) x-x[1])
df <- data.frame(id, year, onset, time_since_event)

我创建了一个名为period的变量,它描述了每个事件之前的不同时期。周期与患者重叠并不重要,因为我们将按患者和周期进行分组,所以如果是新患者或新周期,计数将重新开始。

使用ave()函数可以在每个分组中分配值。在这里,我们基于分组变量idperiod来分析year。我在最后使用的函数只是从每个分组中的当前值中减去第一个值。

最新更新