-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tracking patient health over time
103 lines (79 loc) · 3.89 KB
/
Tracking patient health over time
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
#It can be useful to follow patient progression over time. The following code calls the first recorded metric after a 6 month interval
#where this data is available, and appends this to each row.
###################################################################################
#FIRST LOG CREATININE AFTER 6M
first<-crea.rep[,c("PatientID","event.date","log_CREA")]
first$event.date2<-first$event.date+180
indx1<-neardate(first$PatientID,crea.rep$PatientID,first$event.date2,crea.rep$event.date,best="after",nomatch=NA_integer_)
first$LogCrea6M<-crea.rep[indx1,"log_CREA"]
#THE ABOVE CODE PREFERABLY MATCHES A RESULT FROM AFTER 6 MONTHS, BUT OTHERWISE SELECTS ONE BEFORE IT.
#WE NEED TO REMOVE DATA COLLECTED FROM BEFORE 6 MONTHS:
maxes<-first %>%
group_by(PatientID) %>%
slice(which.max(event.date)) %>%
as.data.frame
maxes<-maxes[,c(1,2)]
colnames(maxes)<-c("PatientID","MaxDate")
first<-merge(first,maxes)
first<-first[first$event.date2<=first$MaxDate,c(1,2,5)]
first$PatientID<-as.character(first$PatientID)
crea.rep$PatientID<-as.character(crea.rep$PatientID)
crea.rep<-merge(crea.rep,first,all.x=TRUE,all.y=FALSE)
crea.rep$LogCrea6M<-unlist(crea.rep$LogCrea6M)
###################################################################################
#FIRST EGFR AFTER 6M-MDRD
first<-crea.rep[,c("PatientID","event.date","MDRDeGFR")]
first$event.date2<-first$event.date+180
indx1<-neardate(first$PatientID,crea.rep$PatientID,first$event.date2,crea.rep$event.date,best="prior",nomatch=NA_integer_)
first$MDRDeGFR6M<-crea.rep[indx1,"MDRDeGFR"]
#THE ABOVE CODE PREFERABLY MATCHES A RESULT FROM AFTER 6 MONTHS, BUT OTHERWISE SELECTS ONE BEFORE IT.
#WE NEED TO REMOVE DATA COLLECTED FROM BEFORE 6 MONTHS:
maxes<-first %>%
group_by(PatientID) %>%
slice(which.max(event.date)) %>%
as.data.frame
maxes<-maxes[,c(1,2)]
colnames(maxes)<-c("PatientID","MaxDate")
first$PatientID<-as.character(first$PatientID)
maxes$PatientID<-as.character(maxes$PatientID)
first<-merge(first,maxes)
first<-first[first$event.date2<=first$MaxDate,c(1,2,5)]
crea.rep<-merge(crea.rep,first,all.x=TRUE)
crea.rep$MDRDeGFR6M<-unlist(crea.rep$MDRDeGFR6M)
################################################################################### CHECKED
#FIRST CKDEPI AFTER 6M
first<-crea.rep[,c("PatientID","event.date","CKDEPIeGFR")]
first$event.date2<-first$event.date+180
indx1<-neardate(first$PatientID,crea.rep$PatientID,first$event.date2,crea.rep$event.date,best="prior",nomatch=NA_integer_)
first$CKDEPIeGFR6M<-crea.rep[indx1,"CKDEPIeGFR"]
#THE ABOVE CODE PREFERABLY MATCHES A RESULT FROM AFTER 6 MONTHS, BUT OTHERWISE SELECTS ONE BEFORE IT.
#WE NEED TO REMOVE DATA COLLECTED FROM BEFORE 6 MONTHS:
maxes<-first %>%
group_by(PatientID) %>%
slice(which.max(event.date)) %>%
as.data.frame
maxes<-maxes[,c(1,2)]
colnames(maxes)<-c("PatientID","MaxDate")
first<-merge(first,maxes)
first<-first[first$event.date2<=first$MaxDate,c(1,2,5)]
crea.rep<-merge(crea.rep,first,all.x=TRUE)
crea.rep$CKDEPIeGFR6M<-unlist(crea.rep$CKDEPIeGFR6M)
################################################################################### CHECKED
#FIRST CREATININE AFTER 6M
first<-crea.rep[,c("PatientID","event.date","Creatinine")]
first$event.date2<-first$event.date+180
indx1<-neardate(first$PatientID,crea.rep$PatientID,first$event.date2,crea.rep$event.date,best="prior",nomatch=NA_integer_)
first$Creatinine6M<-crea.rep[indx1,"Creatinine"]
#THE ABOVE CODE PREFERABLY MATCHES A RESULT FROM AFTER 6 MONTHS, BUT OTHERWISE SELECTS ONE BEFORE IT.
#WE NEED TO REMOVE DATA COLLECTED FROM BEFORE 6 MONTHS:
maxes<-first %>%
group_by(PatientID) %>%
slice(which.max(event.date)) %>%
as.data.frame
maxes<-maxes[,c(1,2)]
colnames(maxes)<-c("PatientID","MaxDate")
first<-merge(first,maxes)
first<-first[first$event.date2<=first$MaxDate,c(1,2,5)]
crea.rep<-merge(crea.rep,first,all.x=TRUE)
crea.rep$Creatinine6M<-unlist(crea.rep$Creatinine6M)
################################################################################