is there a way to organize/manipulate data for use in a case crossover study?
-
Login or Register
- Log in with
| id | x1 | x2 | x3 | x4 | x5.......... | x365 | hospitalization date (or day) |
| 1 | 0 | 0 | 1 | 1 | 1 | 0 | 270 |
| 2 | |||||||
| 3 | |||||||
| n |
| id | x1 | x2 | x3 | x4 | x5... | x365 | hospitalization day | case window (day263-270) | control window1(day256-262) | control window2(day233-240) |
| 1 | 0 | 0 | 1 | 1 | 1 | 0 | 270 | 1 | 0 | 0 |
| 2 | ||||||||||
| 3 | ||||||||||
| n | ||||||||||
| stratum | observation # | case | exposure |
| 1 | 1 | 1 | 1 |
| 1 | 2 | 0 | 1 |
| 1 | 3 | 0 | 1 |
| 2 | 1 | 1 | 0 |
| 2 | 2 | 0 | 1 |
| 2 | 3 | 0 | 0 |
| 3 | 1 | 1 | 1 |
| 3 | 2 | 0 | 0 |
| 3 | 3 | 0 | 1 |
// CREATE RANDOM DEMONSTRATION DATA
clear*
set obs 50
set seed 1234
gen id = _n
forvalues i = 1/365 {
gen x`i' = runiform() < 0.25
}
gen hospitalization_day = runiformint(1, 365)
// MANAGE THE DATA TO SET IT UP FOR A CASE CROSSOVER ANALYSIS
reshape long x, i(id) j(day)
by id, sort: egen exposedcase = max(cond(inrange(hospitalization_day-day, 0, 7), x, .))
by id: egen exposedcontrol1 = max(cond(inrange(hospitalization_day-day, 8, 14), x, .))
by id: egen exposedcontrol2 = max(cond(inrange(hospitalization_day-day, 30, 37), x, .))
drop x day
duplicates drop
reshape long exposed, i(id) j(_window) string
// MAKE THE WINDOW VARIABLE NUMERIC SO IT CAN BE USED IN ANALYSIS
encode _window, gen(window)
drop _window
// simulate data
clear
set obs 50
set seed 447 // 48234
gen id = _n
forvalues i = 1/365 {
gen DrugB`i' = runiform() < 0.25
}
gen hospitalization_day = runiformint(1, 365)
// long format, with "person-days" as the unit of analysis
reshape long DrugB, i(id) j(day)
gen byte case_day = (day == hospitalization_day)
bysort id (day): gen byte exposed = DrugB[_n-1] // simplified exposure
// Analysis
clogit case_day exposed, group(id)
// melogit case_day exposed || id
// cc case_day exposed, by(id)
Comment