|
| 1 | +#It can be useful to follow patient progression over time. The following code calls the first recorded metric after a 6 month interval |
| 2 | +#where this data is available, and appends this to each row. |
| 3 | + |
| 4 | +################################################################################### |
| 5 | +#FIRST LOG CREATININE AFTER 6M |
| 6 | + |
| 7 | +first<-crea.rep[,c("PatientID","event.date","log_CREA")] |
| 8 | +first$event.date2<-first$event.date+180 |
| 9 | +indx1<-neardate(first$PatientID,crea.rep$PatientID,first$event.date2,crea.rep$event.date,best="after",nomatch=NA_integer_) |
| 10 | +first$LogCrea6M<-crea.rep[indx1,"log_CREA"] |
| 11 | + |
| 12 | +#THE ABOVE CODE PREFERABLY MATCHES A RESULT FROM AFTER 6 MONTHS, BUT OTHERWISE SELECTS ONE BEFORE IT. |
| 13 | +#WE NEED TO REMOVE DATA COLLECTED FROM BEFORE 6 MONTHS: |
| 14 | + |
| 15 | +maxes<-first %>% |
| 16 | +group_by(PatientID) %>% |
| 17 | +slice(which.max(event.date)) %>% |
| 18 | +as.data.frame |
| 19 | +maxes<-maxes[,c(1,2)] |
| 20 | +colnames(maxes)<-c("PatientID","MaxDate") |
| 21 | +first<-merge(first,maxes) |
| 22 | +first<-first[first$event.date2<=first$MaxDate,c(1,2,5)] |
| 23 | + first$PatientID<-as.character(first$PatientID) |
| 24 | +crea.rep$PatientID<-as.character(crea.rep$PatientID) |
| 25 | +crea.rep<-merge(crea.rep,first,all.x=TRUE,all.y=FALSE) |
| 26 | +crea.rep$LogCrea6M<-unlist(crea.rep$LogCrea6M) |
| 27 | + |
| 28 | +################################################################################### |
| 29 | +#FIRST EGFR AFTER 6M-MDRD |
| 30 | + |
| 31 | +first<-crea.rep[,c("PatientID","event.date","MDRDeGFR")] |
| 32 | +first$event.date2<-first$event.date+180 |
| 33 | + |
| 34 | +indx1<-neardate(first$PatientID,crea.rep$PatientID,first$event.date2,crea.rep$event.date,best="prior",nomatch=NA_integer_) |
| 35 | +first$MDRDeGFR6M<-crea.rep[indx1,"MDRDeGFR"] |
| 36 | + |
| 37 | +#THE ABOVE CODE PREFERABLY MATCHES A RESULT FROM AFTER 6 MONTHS, BUT OTHERWISE SELECTS ONE BEFORE IT. |
| 38 | +#WE NEED TO REMOVE DATA COLLECTED FROM BEFORE 6 MONTHS: |
| 39 | + |
| 40 | +maxes<-first %>% |
| 41 | +group_by(PatientID) %>% |
| 42 | +slice(which.max(event.date)) %>% |
| 43 | +as.data.frame |
| 44 | +maxes<-maxes[,c(1,2)] |
| 45 | +colnames(maxes)<-c("PatientID","MaxDate") |
| 46 | + |
| 47 | +first$PatientID<-as.character(first$PatientID) |
| 48 | +maxes$PatientID<-as.character(maxes$PatientID) |
| 49 | +first<-merge(first,maxes) |
| 50 | +first<-first[first$event.date2<=first$MaxDate,c(1,2,5)] |
| 51 | + |
| 52 | +crea.rep<-merge(crea.rep,first,all.x=TRUE) |
| 53 | +crea.rep$MDRDeGFR6M<-unlist(crea.rep$MDRDeGFR6M) |
| 54 | + |
| 55 | +################################################################################### CHECKED |
| 56 | +#FIRST CKDEPI AFTER 6M |
| 57 | + |
| 58 | +first<-crea.rep[,c("PatientID","event.date","CKDEPIeGFR")] |
| 59 | +first$event.date2<-first$event.date+180 |
| 60 | + |
| 61 | +indx1<-neardate(first$PatientID,crea.rep$PatientID,first$event.date2,crea.rep$event.date,best="prior",nomatch=NA_integer_) |
| 62 | +first$CKDEPIeGFR6M<-crea.rep[indx1,"CKDEPIeGFR"] |
| 63 | + |
| 64 | +#THE ABOVE CODE PREFERABLY MATCHES A RESULT FROM AFTER 6 MONTHS, BUT OTHERWISE SELECTS ONE BEFORE IT. |
| 65 | +#WE NEED TO REMOVE DATA COLLECTED FROM BEFORE 6 MONTHS: |
| 66 | + |
| 67 | +maxes<-first %>% |
| 68 | +group_by(PatientID) %>% |
| 69 | +slice(which.max(event.date)) %>% |
| 70 | +as.data.frame |
| 71 | +maxes<-maxes[,c(1,2)] |
| 72 | +colnames(maxes)<-c("PatientID","MaxDate") |
| 73 | +first<-merge(first,maxes) |
| 74 | +first<-first[first$event.date2<=first$MaxDate,c(1,2,5)] |
| 75 | + |
| 76 | +crea.rep<-merge(crea.rep,first,all.x=TRUE) |
| 77 | +crea.rep$CKDEPIeGFR6M<-unlist(crea.rep$CKDEPIeGFR6M) |
| 78 | + |
| 79 | +################################################################################### CHECKED |
| 80 | + |
| 81 | +#FIRST CREATININE AFTER 6M |
| 82 | +first<-crea.rep[,c("PatientID","event.date","Creatinine")] |
| 83 | +first$event.date2<-first$event.date+180 |
| 84 | + |
| 85 | +indx1<-neardate(first$PatientID,crea.rep$PatientID,first$event.date2,crea.rep$event.date,best="prior",nomatch=NA_integer_) |
| 86 | +first$Creatinine6M<-crea.rep[indx1,"Creatinine"] |
| 87 | + |
| 88 | +#THE ABOVE CODE PREFERABLY MATCHES A RESULT FROM AFTER 6 MONTHS, BUT OTHERWISE SELECTS ONE BEFORE IT. |
| 89 | +#WE NEED TO REMOVE DATA COLLECTED FROM BEFORE 6 MONTHS: |
| 90 | + |
| 91 | +maxes<-first %>% |
| 92 | +group_by(PatientID) %>% |
| 93 | +slice(which.max(event.date)) %>% |
| 94 | +as.data.frame |
| 95 | +maxes<-maxes[,c(1,2)] |
| 96 | +colnames(maxes)<-c("PatientID","MaxDate") |
| 97 | +first<-merge(first,maxes) |
| 98 | +first<-first[first$event.date2<=first$MaxDate,c(1,2,5)] |
| 99 | + |
| 100 | +crea.rep<-merge(crea.rep,first,all.x=TRUE) |
| 101 | +crea.rep$Creatinine6M<-unlist(crea.rep$Creatinine6M) |
| 102 | + |
| 103 | +################################################################################ |
0 commit comments