-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuring_after_without_smoking.R
More file actions
460 lines (334 loc) · 19.3 KB
/
Copy pathDuring_after_without_smoking.R
File metadata and controls
460 lines (334 loc) · 19.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#Author: Christina Chatsatourian
# During-After_without_smoking
library(survival)
# Adjust `tstop` to avoid issues where `tstart >= tstop`
# Set `tstop` to the minimum of `age` and `bc_age` (censor at BC diagnosis if applicable)
filtered_data$tstop <- pmin(filtered_data$age, filtered_data$bc_age, na.rm = TRUE)
# Filter out rows where `tstop` is less than or equal to 0 (ensuring valid `tstop` values)
filtered_data <- filtered_data[filtered_data$tstop > 0, ] # Ensure no negative or zero `tstop`
# Initialize the timed data frame with age as the primary time scale
# Adjust tstop to censor at breast cancer diagnosis (bc_age)
t_data <- tmerge(
data1 = filtered_data,
data2 = filtered_data,
id = ID,
tstart = 0, # Start time (age at birth)
tstop = pmin(age, bc_age, na.rm = TRUE) # Stop time (censor at BC diagnosis if applicable)
)
# Add the event status for BC diagnosis
t_data <- tmerge(
data1 = t_data,
data2 = t_data,
id = ID,
bc_diagnosis = event(bc_age) # Track BC event
)
# Add time-dependent hrt statuses (Ever/Never user)
hrt_data <- tmerge(
data1 = t_data,
data2 = filtered_data,
id = ID,
exposed_to_hrt = tdc(hrt_start) # Change status to "ever-users" (status = 1) at the age of initiation (hrt_start)
)
# Add a time-dependent hrt.status column
hrt_data$hrt.status <- factor(
ifelse(hrt_data$exposed_to_hrt == 1, "ever", "never"),
levels = c("never", "ever") # Set "never" as the reference
)
# Add time-dependent meno status (Had yes/no)
meno_data <- tmerge(
data1 = hrt_data,
data2 = filtered_data,
id = ID,
meno_stat = tdc(age_meno) # Change status to "yes-they've had menopause" (status = 1) at the age of (age_meno)
)
# Add a time-dependent meno_status column
meno_data$meno_status <- factor(
ifelse(meno_data$meno_stat == 1, "yes", "no"),
levels = c("no", "yes") # Set "no" as the reference
)
# Add time-dependent hyster status (Had yes/no)
hyster_data <- tmerge(
data1 = meno_data,
data2 = filtered_data,
id = ID,
hyster_stat = tdc(age_hyster) # Change status to "yes-they've had hyster" (status = 1) at the age of (age_hyster)
)
# Add a time-dependent hyster_status column
hyster_data$hyster_status <- factor(
ifelse(hyster_data$hyster_stat == 1, "yes", "no"),
levels = c("no", "yes") # Set "no" as the reference
)
# Initialize the timed data frame with age as the primary time scale
t_data_oc <- tmerge(
data1 = filtered_data,
data2 = filtered_data,
id = ID,
tstart = 0, # Start time (age at birth)
tstop = pmin(age, bc_age, na.rm = TRUE) # Stop time (censor at BC diagnosis if applicable)
)
# Define time-varying covariates for OC exposure (initiation and discontinuation)
t_data_oc <- tmerge(
data1 = t_data_oc, # Original data frame with time and event information
data2 = t_data_oc, # Data frame with exposure data
id = ID, # Merge based on 'id'
exposed_to_oc_start = tdc(start_oc) # OC initiation time (start_oc)
)
# Add OC discontinuation as a time-varying covariate
t_data_oc <- tmerge(
data1 = t_data_oc,
data2 = t_data_oc,
id = ID,
exposed_to_oc_stop = tdc(stop_oc + 2) # OC discontinuation time (stop_oc age + 2years lag period)
)
# Create the exposure group column based on exposure status (during, after, never)
t_data_oc$exposure_group <- with(t_data_oc,
ifelse(exposed_to_oc_start == 1 & exposed_to_oc_stop == 0, 1, # During exposure (initiated but not stopped)
ifelse(exposed_to_oc_start == 0 & exposed_to_oc_stop == 0, 0, # No exposure (never used OC)
ifelse(exposed_to_oc_start == 1 & exposed_to_oc_stop == 1, 2, # After exposure (initiated and stopped)
NA)))) # This case should ideally never happen
# Combine OC exposure with BC data
t_data_combined <- tmerge(
data1 = hyster_data,
data2 = t_data_oc,
id = ID,
exposure_status = tdc(tstart, exposure_group) # Merge OC status
)
## Define the reference and target groups for Cox analysis
# The reference group is the "never exposed" group (exposure_group == 0)
# The target groups are the "during exposure" (exposure_group == 1) and "after exposure" (exposure_group == 2)
# Create a variable to denote group categories: 0 = never exposed, 1 = during exposure, 2 = after exposure
t_data_combined$oc_group <- with(t_data_combined,
ifelse(exposure_status == 0, "Unexposed", # Never exposed (reference group)
ifelse(exposure_status == 1, "During Exposure", # During exposure
ifelse(exposure_status == 2, "After Exposure", NA))))
# Filter data to only include "Unexposed" and "During Exposure" groups
t_data_during <- subset(t_data_combined, oc_group %in% c("Unexposed", "During Exposure"))
# Define the reference and target groups for during
t_data_during$oc_group <- as.factor(t_data_during$oc_group)
t_data_during$oc_group <- relevel(t_data_during$oc_group, ref = "Unexposed")
levels(t_data_during$oc_group)
# Filter data to only include "Unexposed" and "After Exposure" groups
t_data_after <- subset(t_data_combined, oc_group %in% c("Unexposed", "After Exposure"))
# Define the reference and target groups for after
t_data_after$oc_group <- as.factor(t_data_after$oc_group)
t_data_after$oc_group <- relevel(t_data_after$oc_group, ref = "Unexposed")
levels(t_data_after$oc_group)
# Fit the Cox model comparing "During Exposure" vs "Unexposed"
cox_model_during <- coxph(Surv(tstart, tstop, bc_diagnosis) ~ oc_group + hrt.status + meno_status + hyster_status + bmi + tdi + AgeMenarch + yob, data = t_data_during)
summary(cox_model_during)
#~~Check for missing data~~
# Original number of participants
length(which(!(unique(t_data_combined$ID)%in%cox_model_during$na.action)))
# Original number of participants in during
length(which(!(unique(t_data_during$ID)%in%cox_model_during$na.action)))
# Number of rows excluded due to missing data
length(cox_model_during$na.action) # This gives the number of rows removed
# Number of unique IDs excluded due to missing data
removed_ids <- unique(t_data_during$ID[cox_model_during$na.action])
length(removed_ids) # This gives the number of unique individuals removed
# See from which columns specifically are the missing data from
# Define the columns used in order of priority
columns_used <- c("bmi", "tdi", "AgeMenarch", "yob") # Ordered by priority
# Get the unique IDs that were removed from the Cox model
removed_ids <- unique(t_data_during$ID[cox_model_during$na.action])
# Extract only the rows that correspond to these removed IDs
removed_rows <- t_data_during[t_data_during$ID %in% removed_ids, ]
# Initialize an empty dataframe to track which variable caused exclusion
id_assignment <- data.frame(ID = removed_ids, AssignedVar = NA, stringsAsFactors = FALSE)
# Loop through each variable in priority order and assign the first missing variable
for (var in columns_used) {
# Find IDs with missing values in the current variable and that haven't been assigned yet
ids_missing_var <- unique(removed_rows$ID[is.na(removed_rows[[var]])])
# Assign only if the ID has not already been assigned a missing variable
id_assignment$AssignedVar[id_assignment$ID %in% ids_missing_var & is.na(id_assignment$AssignedVar)] <- var
}
# Count the number of unique IDs assigned to each missing variable
missing_by_variable <- table(id_assignment$AssignedVar, useNA = "no") # Exclude NAs (not assigned)
# Print results
cat("Total number of unique IDs excluded:", length(removed_ids), "\n")
cat("Number of unique IDs excluded due to specific missing variables:\n")
print(missing_by_variable)
#~~end of check~~
# Fit the Cox model comparing "After Exposure" vs "Unexposed"
cox_model_after <- coxph(Surv(tstart, tstop, bc_diagnosis) ~ oc_group + hrt.status + meno_status + hyster_status + bmi + tdi + AgeMenarch + yob, data = t_data_after)
summary(cox_model_after)
#~~Check for missing data~~
# Original number of participants
length(which(!(unique(t_data_combined$ID)%in%cox_model_after$na.action)))
# Original number of participants in during
length(which(!(unique(t_data_after$ID)%in%cox_model_after$na.action)))
# Number of rows excluded due to missing data
length(cox_model_after$na.action) # This gives the number of rows removed
# Number of unique IDs excluded due to missing data
removed_ids <- unique(t_data_after$ID[cox_model_after$na.action])
length(removed_ids) # This gives the number of unique individuals removed
# See from which columns specifically are the missing data from
# Define the columns used in order of priority
columns_used <- c("bmi", "tdi", "AgeMenarch", "yob") # Ordered by priority
# Get the unique IDs that were removed from the Cox model
removed_ids <- unique(t_data_after$ID[cox_model_after$na.action])
# Extract only the rows that correspond to these removed IDs
removed_rows <- t_data_after[t_data_after$ID %in% removed_ids, ]
# Initialize an empty dataframe to track which variable caused exclusion
id_assignment <- data.frame(ID = removed_ids, AssignedVar = NA, stringsAsFactors = FALSE)
# Loop through each variable in priority order and assign the first missing variable
for (var in columns_used) {
# Find IDs with missing values in the current variable and that haven't been assigned yet
ids_missing_var <- unique(removed_rows$ID[is.na(removed_rows[[var]])])
# Assign only if the ID has not already been assigned a missing variable
id_assignment$AssignedVar[id_assignment$ID %in% ids_missing_var & is.na(id_assignment$AssignedVar)] <- var
}
# Count the number of unique IDs assigned to each missing variable
missing_by_variable <- table(id_assignment$AssignedVar, useNA = "no") # Exclude NAs (not assigned)
# Print results
cat("Total number of unique IDs excluded:", length(removed_ids), "\n")
cat("Number of unique IDs excluded due to specific missing variables:\n")
print(missing_by_variable)
#~~end of check~~
# 1) PRS filtering
# Load necessary libraries
library(dplyr)
# Ensure no missing values in PRS_BC
filtered_data <- filtered_data %>%
filter(!is.na(prs))
# Fit the models below
cox_model_during_2 <- coxph(Surv(tstart, tstop, bc_diagnosis) ~ oc_group + prs + hrt.status + meno_status + hyster_status + bmi + tdi + AgeMenarch + yob, data = t_data_during)
summary(cox_model_during_2)
#~~Check for missing data~~
# Original number of participants
length(which(!(unique(t_data_combined$ID)%in%cox_model_during_2$na.action)))
# Original number of participants in during
length(which(!(unique(t_data_during$ID)%in%cox_model_during_2$na.action)))
# Number of rows excluded due to missing data
length(cox_model_during_2$na.action) # This gives the number of rows removed
# Number of unique IDs excluded due to missing data
removed_ids <- unique(t_data_during$ID[cox_model_during_2$na.action])
length(removed_ids) # This gives the number of unique individuals removed
# See from which columns specifically are the missing data from
# Define the columns used in order of priority
columns_used <- c("bmi", "tdi", "AgeMenarch", "yob") # Ordered by priority
# Get the unique IDs that were removed from the Cox model
removed_ids <- unique(t_data_during$ID[cox_model_during_2$na.action])
# Extract only the rows that correspond to these removed IDs
removed_rows <- t_data_during[t_data_during$ID %in% removed_ids, ]
# Initialize an empty dataframe to track which variable caused exclusion
id_assignment <- data.frame(ID = removed_ids, AssignedVar = NA, stringsAsFactors = FALSE)
# Loop through each variable in priority order and assign the first missing variable
for (var in columns_used) {
# Find IDs with missing values in the current variable and that haven't been assigned yet
ids_missing_var <- unique(removed_rows$ID[is.na(removed_rows[[var]])])
# Assign only if the ID has not already been assigned a missing variable
id_assignment$AssignedVar[id_assignment$ID %in% ids_missing_var & is.na(id_assignment$AssignedVar)] <- var
}
# Count the number of unique IDs assigned to each missing variable
missing_by_variable <- table(id_assignment$AssignedVar, useNA = "no") # Exclude NAs (not assigned)
# Print results
cat("Total number of unique IDs excluded:", length(removed_ids), "\n")
cat("Number of unique IDs excluded due to specific missing variables:\n")
print(missing_by_variable)
#~~end of check~~
cox_model_after_2 <- coxph(Surv(tstart, tstop, bc_diagnosis) ~ oc_group + prs + hrt.status + meno_status + hyster_status + bmi + tdi + AgeMenarch + yob, data = t_data_after)
summary(cox_model_after_2)
#~~Check for missing data~~
# Original number of participants
length(which(!(unique(t_data_combined$ID)%in%cox_model_after_2$na.action)))
# Original number of participants in during
length(which(!(unique(t_data_after$ID)%in%cox_model_after_2$na.action)))
# Number of rows excluded due to missing data
length(cox_model_after_2$na.action) # This gives the number of rows removed
# Number of unique IDs excluded due to missing data
removed_ids <- unique(t_data_after$ID[cox_model_after_2$na.action])
length(removed_ids) # This gives the number of unique individuals removed
# See from which columns specifically are the missing data from
# Define the columns used in order of priority
columns_used <- c("bmi", "tdi", "AgeMenarch", "yob") # Ordered by priority
# Get the unique IDs that were removed from the Cox model
removed_ids <- unique(t_data_after$ID[cox_model_after_2$na.action])
# Extract only the rows that correspond to these removed IDs
removed_rows <- t_data_after[t_data_after$ID %in% removed_ids, ]
# Initialize an empty dataframe to track which variable caused exclusion
id_assignment <- data.frame(ID = removed_ids, AssignedVar = NA, stringsAsFactors = FALSE)
# Loop through each variable in priority order and assign the first missing variable
for (var in columns_used) {
# Find IDs with missing values in the current variable and that haven't been assigned yet
ids_missing_var <- unique(removed_rows$ID[is.na(removed_rows[[var]])])
# Assign only if the ID has not already been assigned a missing variable
id_assignment$AssignedVar[id_assignment$ID %in% ids_missing_var & is.na(id_assignment$AssignedVar)] <- var
}
# Count the number of unique IDs assigned to each missing variable
missing_by_variable <- table(id_assignment$AssignedVar, useNA = "no") # Exclude NAs (not assigned)
# Print results
cat("Total number of unique IDs excluded:", length(removed_ids), "\n")
cat("Number of unique IDs excluded due to specific missing variables:\n")
print(missing_by_variable)
#~~end of check~~
cox_model_during_3 <- coxph(Surv(tstart, tstop, bc_diagnosis) ~ oc_group * prs + hrt.status + meno_status + hyster_status + bmi + tdi + AgeMenarch + yob, data = t_data_during)
summary(cox_model_during_3)
#~~Check for missing data~~
# Original number of participants
length(which(!(unique(t_data_combined$ID)%in%cox_model_during_3$na.action)))
# Original number of participants in during
length(which(!(unique(t_data_during$ID)%in%cox_model_during_3$na.action)))
# Number of rows excluded due to missing data
length(cox_model_during_3$na.action) # This gives the number of rows removed
# Number of unique IDs excluded due to missing data
removed_ids <- unique(t_data_during$ID[cox_model_during_3$na.action])
length(removed_ids) # This gives the number of unique individuals removed
# See from which columns specifically are the missing data from
# Define the columns used in order of priority
columns_used <- c("bmi", "tdi", "AgeMenarch", "yob") # Ordered by priority
# Get the unique IDs that were removed from the Cox model
removed_ids <- unique(t_data_during$ID[cox_model_during_3$na.action])
# Extract only the rows that correspond to these removed IDs
removed_rows <- t_data_during[t_data_during$ID %in% removed_ids, ]
# Initialize an empty dataframe to track which variable caused exclusion
id_assignment <- data.frame(ID = removed_ids, AssignedVar = NA, stringsAsFactors = FALSE)
# Loop through each variable in priority order and assign the first missing variable
for (var in columns_used) {
# Find IDs with missing values in the current variable and that haven't been assigned yet
ids_missing_var <- unique(removed_rows$ID[is.na(removed_rows[[var]])])
# Assign only if the ID has not already been assigned a missing variable
id_assignment$AssignedVar[id_assignment$ID %in% ids_missing_var & is.na(id_assignment$AssignedVar)] <- var
}
# Count the number of unique IDs assigned to each missing variable
missing_by_variable <- table(id_assignment$AssignedVar, useNA = "no") # Exclude NAs (not assigned)
# Print results
cat("Total number of unique IDs excluded:", length(removed_ids), "\n")
cat("Number of unique IDs excluded due to specific missing variables:\n")
print(missing_by_variable)
#~~end of check~~
cox_model_after_3 <- coxph(Surv(tstart, tstop, bc_diagnosis) ~ oc_group * prs + hrt.status + meno_status + hyster_status + bmi + tdi + AgeMenarch + yob, data = t_data_after)
summary(cox_model_after_3)
#~~Check for missing data~~
# Original number of participants
length(which(!(unique(t_data_combined$ID)%in%cox_model_after_3$na.action)))
# Original number of participants in during
length(which(!(unique(t_data_after$ID)%in%cox_model_after_3$na.action)))
# Number of rows excluded due to missing data
length(cox_model_after_3$na.action) # This gives the number of rows removed
# Number of unique IDs excluded due to missing data
removed_ids <- unique(t_data_after$ID[cox_model_after_3$na.action])
length(removed_ids) # This gives the number of unique individuals removed
# See from which columns specifically are the missing data from
# Define the columns used in order of priority
columns_used <- c("bmi", "tdi", "AgeMenarch", "yob") # Ordered by priority
# Get the unique IDs that were removed from the Cox model
removed_ids <- unique(t_data_after$ID[cox_model_after_3$na.action])
# Extract only the rows that correspond to these removed IDs
removed_rows <- t_data_after[t_data_after$ID %in% removed_ids, ]
# Initialize an empty dataframe to track which variable caused exclusion
id_assignment <- data.frame(ID = removed_ids, AssignedVar = NA, stringsAsFactors = FALSE)
# Loop through each variable in priority order and assign the first missing variable
for (var in columns_used) {
# Find IDs with missing values in the current variable and that haven't been assigned yet
ids_missing_var <- unique(removed_rows$ID[is.na(removed_rows[[var]])])
# Assign only if the ID has not already been assigned a missing variable
id_assignment$AssignedVar[id_assignment$ID %in% ids_missing_var & is.na(id_assignment$AssignedVar)] <- var
}
# Count the number of unique IDs assigned to each missing variable
missing_by_variable <- table(id_assignment$AssignedVar, useNA = "no") # Exclude NAs (not assigned)
# Print results
cat("Total number of unique IDs excluded:", length(removed_ids), "\n")
cat("Number of unique IDs excluded due to specific missing variables:\n")
print(missing_by_variable)
#~~end of check~~