1
我有一個二進制變量的不平衡面板數據,指示事件是否發生。我想控制時間依賴性。做到這一點的方法是控制事件發生之前已經過去的時間。如何在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))
「time_since_event」只與「發病」有關嗎?或者你是否打算將它與「id」或「year」聯繫起來? – A5C1D2H2I1M1N2O1R2T1
它連接到ID和年份,因爲ID切換時,我不希望time_since_event繼續積累,它需要設置回0 – user46257
這不是在你的例子中說明,是嗎?你可以請更新的要求和期望的輸出? – A5C1D2H2I1M1N2O1R2T1