-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAnalyse by measurement count
221 lines (183 loc) · 9.8 KB
/
Analyse by measurement count
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
#Gross weight category transition between first measurements of first and second recorded measurement year per child
alldata<-read.table("LMStableedited.csv",header=TRUE, sep=",")
length(alldata$ChildID)
alldata<-subset(alldata,!is.na(alldata$BMI),)
length(alldata$ChildID) #44 records removed
alldata$ChildID<-as.factor(alldata$ChildID)
length(unique(alldata$ChildID)) #63339 children
alldata$PostcodeDecile<-ceiling(alldata$PostcodeDecile)
alldata<-alldata[floor(alldata$ageattest/52.12)<12&floor(alldata$ageattest/52.12)>3,]
alldata2<-alldata[order(alldata$ChildID,alldata$AssessmentDate),]
names(alldata2)
d<-duplicated(alldata2[,c(1,4)])
head(alldata2[d,],n=10)
d4<-alldata2[alldata2$ChildID %in% alldata2$ChildID[d],]
alldata2<-alldata2[!d,]
length(alldata2$ChildID)-length(alldata$ChildID) #134 second entries from the same date removed
head(alldata2[alldata2 %in% d4,])
alldata2<-alldata2[!(alldata2$ChildID %in% d4$ChildID & alldata2$AssessmentDate %in% d4$AssessmentDate & alldata2$BMI %in% d4$BMI), ]
#6 further entries removed
length(alldata2$ChildID)-length(alldata2$ChildID) #140 second entries from reused child or those with edits to erroneous values
d2<-duplicated(alldata2[,c(1,3)])
head(alldata2[d2,],n=10)
alldata2<-alldata2[!d2,]
length(alldata$ChildID)-length(alldata2$ChildID) #178 second and later entries removed from the same assessment year
length(unique(alldata2$ChildID))
alldata<-alldata2
library(lubridate)
alldata$DateOfBirth<-(as.Date(paste(alldata$DateOfBirth,"-01",sep="")))
alldata$AssessmentDate<-as.Date(paste(alldata$AssessmentDate,"-01",sep=""))
alldata$assyr<-year(as.POSIXlt(alldata$AssessmentDate, format="%Y-%m-%d"))
attach(alldata)
alldata$ChildID<-as.factor(alldata$ChildID)
alldata$SchoolCode<-as.factor(alldata$SchoolCode)
alldata$IsAccountRegistered<-as.factor(alldata$IsAccountRegistered)
alldata<-alldata[!alldata$AcademicYear=="2011/2012"&!alldata$AcademicYear=="2012/2013",]
alldata$AcademicYear <- droplevels(alldata$AcademicYear)
champ2<-alldata
#DRAW IN BMI THRESHOLD TO WORK OUT CENTILES FOR OVERWEIGHT, OBESE, SEVERELY OBESE
champ2$weightcat<-1#normal
champ2$weightcat<-ifelse(champ2$Centile_BMI>=90.879,2,champ2$weightcat)#overweight
champ2$weightcat<-ifelse(champ2$Centile_BMI>=97.725,3,champ2$weightcat)#obese
champ2$weightcat<-ifelse(champ2$Centile_BMI<2.275,4,champ2$weightcat)#underweight
champ2$weightcat<-ifelse(champ2$Centile_BMI>=99.617,5,champ2$weightcat)#severe
champ2$weightcat<-ifelse(champ2$Centile_BMI<0.383,6,champ2$weightcat)#very underweight
table(champ2$weightcat)
champ2$NoRecords<-1
alldata<-champ2
champ2<-champ2[order(champ2$ageattest),]
champ3<-champ2[match(unique(champ2$ChildID),champ2$ChildID),]
summary(champ3$ageattest[champ3$IsAccountRegistered==0])/52.1429
attach(alldata)
#Assign academic year
alldata$Record1<-as.numeric(ifelse(champ2$AcademicYear=="2013/2014",1,0))
alldata$Record2<-as.numeric(ifelse(champ2$AcademicYear=="2014/2015",1,0))
alldata$Record3<-as.numeric(ifelse(champ2$AcademicYear=="2015/2016",1,0))
alldata$Record4<-as.numeric(ifelse(champ2$AcademicYear=="2016/2017",1,0))
#Print BMI at each test
alldata$BMI1<-as.numeric(ifelse(champ2$AcademicYear=="2013/2014",BMI,NA))
alldata$BMI2<-as.numeric(ifelse(champ2$AcademicYear=="2014/2015",BMI,NA))
alldata$BMI3<-as.numeric(ifelse(champ2$AcademicYear=="2015/2016",BMI,NA))
alldata$BMI4<-as.numeric(ifelse(champ2$AcademicYear=="2016/2017",BMI,NA))
#Print weightcat at each age
alldata$weightcat1<-as.numeric(ifelse(champ2$AcademicYear=="2013/2014",weightcat,NA))
alldata$weightcat2<-as.numeric(ifelse(champ2$AcademicYear=="2014/2015",weightcat,NA))
alldata$weightcat3<-as.numeric(ifelse(champ2$AcademicYear=="2015/2016",weightcat,NA))
alldata$weightcat4<-as.numeric(ifelse(champ2$AcademicYear=="2016/2017",weightcat,NA))
attach(alldata)
#Print centile at each age
alldata$cent1<-as.numeric(ifelse(champ2$AcademicYear=="2013/2014",Centile_BMI,NA))
alldata$cent2<-as.numeric(ifelse(champ2$AcademicYear=="2014/2015",Centile_BMI,NA))
alldata$cent3<-as.numeric(ifelse(champ2$AcademicYear=="2015/2016",Centile_BMI,NA))
alldata$cent4<-as.numeric(ifelse(champ2$AcademicYear=="2016/2017",Centile_BMI,NA))
attach(alldata)
#Print date at each age
alldata$d1<-as.numeric(ifelse(champ2$AcademicYear=="2013/2014",AssessmentDate,NA))
alldata$d2<-as.numeric(ifelse(champ2$AcademicYear=="2014/2015",AssessmentDate,NA))
alldata$d3<-as.numeric(ifelse(champ2$AcademicYear=="2015/2016",AssessmentDate,NA))
alldata$d4<-as.numeric(ifelse(champ2$AcademicYear=="2016/2017",AssessmentDate,NA))
attach(alldata)
#Determine how many measurements taken per child
alldata$NoRecords<-(Record1+Record2+Record3+Record4)
Recs<-aggregate(alldata$NoRecords, by=list(Category=ChildID), FUN=sum)
names(Recs)[names(Recs)=="x"] <- "NoRecs"
names(Recs)[names(Recs)=="Category"] <- "ChildID"
##############################################################################
#Merge to have BMI at different ages on the same line
Recs2<-Recs[Recs$NoRecs>=2,]
multipleb<-alldata[alldata$ChildID %in% Recs2$ChildID,] #children measured more than once
multipleb<-with(multipleb, multipleb[order(ChildID),])
attach(multipleb)
as.numeric(c(weightcat1,weightcat2,weightcat3,weightcat4))
multipleb$ChildID<-as.factor(multipleb$ChildID)
multipleb$IsAccountRegistered<-as.numeric(multipleb$IsAccountRegistered)
multiplebase<-multipleb[,c(1,2)]
multiple1<-multipleb[!is.na(multipleb$weightcat1),c(1, 27)]
multiple2<-multipleb[!is.na(multipleb$weightcat2),c(1, 28)]
multiple3<-multipleb[!is.na(multipleb$weightcat3),c(1, 29)]
multiple4<-multipleb[!is.na(multipleb$weightcat4),c(1, 30)]
multiple5<-multipleb[c(1, 10)]
library(plyr)
multiple10<-multipleb[!is.na(multipleb$d1),c(1, 35)]
multiple11<-multipleb[!is.na(multipleb$d2),c(1, 36)]
multiple12<-multipleb[!is.na(multipleb$d3),c(1, 37)]
multiple13<-multipleb[!is.na(multipleb$d4),c(1, 38)]
multiple14<-multipleb[!is.na(multipleb$BMI1),c(1, 23)]
multiple15<-multipleb[!is.na(multipleb$BMI2),c(1, 24)]
multiple16<-multipleb[!is.na(multipleb$BMI3),c(1,25)]
multiple17<-multipleb[!is.na(multipleb$BMI4),c(1, 26)]
multiple18<-multipleb[!is.na(multipleb$cent1),c(1, 31)]
multiple19<-multipleb[!is.na(multipleb$cent2),c(1, 32)]
multiple20<-multipleb[!is.na(multipleb$cent3),c(1,33)]
multiple21<-multipleb[!is.na(multipleb$cent4),c(1, 34)]
multi<-merge(multiplebase,multiple1,by="ChildID",all.x=TRUE)
multi<-merge(multi,multiple2,by="ChildID",all.x=TRUE)
multi<-merge(multi,multiple3,by="ChildID",all.x=TRUE)
multi<-merge(multi,multiple4,by="ChildID",all.x=TRUE)
multi<-merge(multi,multiple5,by="ChildID",all.x=TRUE)
multi<-merge(multi,multiple10,by="ChildID",all.x=TRUE)
multi<-merge(multi,multiple11,by="ChildID",all.x=TRUE)
multi<-merge(multi,multiple12,by="ChildID",all.x=TRUE)
multi<-merge(multi,multiple13,by="ChildID",all.x=TRUE)
multi<-merge(multi,multiple14,by="ChildID",all.x=TRUE)
multi<-merge(multi,multiple15,by="ChildID",all.x=TRUE)
multi<-merge(multi,multiple16,by="ChildID",all.x=TRUE)
multi<-merge(multi,multiple17,by="ChildID",all.x=TRUE)
multi<-merge(multi,multiple18,by="ChildID",all.x=TRUE)
multi<-merge(multi,multiple19,by="ChildID",all.x=TRUE)
multi<-merge(multi,multiple20,by="ChildID",all.x=TRUE)
multi<-merge(multi,multiple21,by="ChildID",all.x=TRUE)
multi2<-aggregate(multi,by=list(multi$ChildID),FUN=mean)
multi2$ChildID<-multi2$Group.1
multi<-multi2
#CALCULATE DIFF BETWEEN MEASUREMENTS
length(multi$ChildID)#25024
attach(multi)
measure1<-as.numeric(weightcat1)
measure1<-ifelse(is.na(measure1)&!is.na(weightcat2),paste(weightcat2),measure1)
measure1<-ifelse(is.na(measure1)&!is.na(weightcat3),paste(weightcat3),measure1)
measure1<-ifelse(is.na(measure1)&!is.na(weightcat4),paste(weightcat4),measure1)
measure2<-ifelse(!is.na(weightcat1)&!is.na(weightcat2),(weightcat2),NA)
measure2<-ifelse(is.na(measure2)&(!is.na(weightcat3)) & (!is.na(weightcat2)|!is.na(weightcat1)),paste(weightcat3),measure2)
measure2<-ifelse(is.na(measure2)&!is.na(weightcat4),paste(weightcat4),measure2)
BMIm1<-as.numeric(BMI1)
BMIm1<-ifelse(is.na(BMIm1)&!is.na(BMI2),paste(BMI2),BMIm1)
BMIm1<-ifelse(is.na(BMIm1)&!is.na(BMI3),paste(BMI3),BMIm1)
BMIm1<-ifelse(is.na(BMIm1)&!is.na(BMI4),paste(BMI4),BMIm1)
BMIm2<-ifelse(!is.na(BMI1)&!is.na(BMI2),paste(BMI2),NA)
BMIm2<-ifelse(is.na(BMIm2)&(!is.na(BMI3)) & (!is.na(BMI2)|!is.na(BMI1)),paste(BMI3),BMIm2)
BMIm2<-ifelse(is.na(BMIm2)&!is.na(BMI4),paste(BMI4),BMIm2)
c1<-as.numeric(cent1)
c1<-ifelse(is.na(cent1)&!is.na(cent2),paste(cent2),c1)
c1<-ifelse(is.na(c1)&!is.na(cent3),paste(cent3),c1)
c1<-ifelse(is.na(c1)&!is.na(cent4),paste(cent4),c1)
c2<-ifelse(!is.na(cent1)&!is.na(cent2),paste(cent2),NA)
c2<-ifelse(is.na(c2)&!is.na(cent3) & (!is.na(cent2)|!is.na(cent1)),paste(cent3),c2)
c2<-ifelse(is.na(c2)&!is.na(cent4),paste(cent4),c2)
multi$ds1<-as.numeric(multi$d1)
multi$ds1<-ifelse(is.na(multi$ds1)&!is.na(multi$d2),multi$d2,multi$ds1)
multi$ds1<-ifelse(is.na(multi$ds1)&!is.na(multi$d3),as.numeric(multi$d3),as.numeric(multi$ds1))
multi$ds1<-ifelse(is.na(multi$ds1)&!is.na(multi$d4),as.numeric(multi$d4),as.numeric(multi$ds1))
multi$ds2<-ifelse(!is.na(multi$d1)&!is.na(multi$d2),multi$d2,NA)
multi$ds2<-ifelse(is.na(multi$ds2)&(!is.na(multi$d1)|!is.na(multi$d2)),multi$d3,multi$ds2)
multi$ds2<-ifelse(is.na(multi$ds2),multi$d4,multi$ds2)
multi$measure1<-as.numeric(measure1)
multi$measure2<-as.numeric(measure2)
multi$B1<-as.numeric(BMIm1)
multi$B2<-as.numeric(BMIm2)
multi$c1<-as.numeric(c1)
multi$c2<-as.numeric(c2)
multi$cendiff<-multi$c2-multi$c1
attach(multi)
multi$days<-as.numeric(multi$ds2)-as.numeric(multi$ds1)
multi$cendiffs<-(multi$cendiff/multi$days)*365.25
summary(multi$cendiffs[multi$IsAccountRegistered==2])
summary(multi$cendiffs[multi$IsAccountRegistered==1])
yes<-multi[multi$IsAccountRegistered==2,]
no<-multi[multi$IsAccountRegistered==1,]
table(yes$measure1,yes$measure2)
table(no$measure1,no$measure2)
#Select starting data for each child:
alldata<-alldata[alldata$ChildID %in% multi$ChildID,]
alldata<-alldata[order(AcademicYear),]
alldata<-alldata[!duplicated(alldata$ChildID),]