-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathWeight category transition analysis
233 lines (195 loc) · 12.6 KB
/
Weight category transition analysis
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
champtable<-read.table("CMFTfile.csv", header=T,sep=",")
head(champtable)
#CHECK FOR WEIGHT AND HEIGHT DATA ENTRY ERRORS.
#USES THE NHS 'VALIDATION OF CHILD MEASUREMENT PROGRAMME DATA' REPORT 2016 ERROR REJECTION CUTOFFS
#Update this with a table?
attach(champtable)
library(lubridate)
ndate1<-paste(DateOfBirth,"-01",sep=""); ndate1 #The data does not have day values within the dates, so these are assigned as the 1st of the month for R purposes.
ndate1<-(as.Date(ndate1))
ndate2<-paste(AssessmentDate,"-01",sep=""); ndate2
ndate2<-(as.Date(ndate2))
require(lubridate)
champtable$ageattest<-floor(difftime(strptime(ndate2, format="%Y-%m-%d"),
strptime(ndate1, format="%Y-%m-%d"),units="weeks"))
champtable$ageattest<-as.numeric(champtable$ageattest)
attach(champtable)
#Gives ages in weeks
#REMOVE OUT OF RANGE AGES DUE TO DATE ENTRY ERRORS
champ<-champtable
champ$ageattest<-as.numeric(champ$ageattest)
champ<-subset(champ, champ$ageattest<=625 & champ$ageattest>=208)
attach(champ)
champ$ageattest<-as.integer(champ$ageattest)
champ <- champ[order(champ$ChildID),]
#DEFINE FACTORS
sch<-as.character(SchoolCode)
ID<-as.character(ChildID)
champ$BMI<-champ$WeightKG/(champ$HeightCM/100)^2
LMStable<-file(paste("LMStableedited.csv"), open="w")
cat("ChildID","SchoolCode","AcademicYear","AssessmentDate","DateOfBirth","Gender","HeightCM","WeightKG","BMI","IsAccountRegistered","Ethnicity","PostcodeDecile","ageattest","\n", sep=",",file="LMStable1005.csv",append=TRUE)
for (n in 1:108364){cat((paste(champ$ChildID[n])),(paste(champ$SchoolCode[n])),(paste(champ$AcademicYear[n])),(paste(champ$AssessmentDate[n])),(paste(champ$DateOfBirth[n])),(paste(champ$Gender[n])), (paste(champ$HeightCM[n])),(paste(champ$WeightKG[n])),(paste(champ$BMI[n])),(paste(champ$IsAccountRegistered[n])),(paste(champ$Ethnicity[n])),(paste(champ$IMDdecile[n])),(paste(champ$ageattest[n])),"\n", file="LMStable1005.csv", sep=",", fill=FALSE, labels=NULL, append=TRUE)
}
#USE the excel plugin LMSGrowth to add SDs from BMIs and save table.
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)
levels(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)
head(alldata2,n=20)
alldata2<-alldata2[!d,]
length(alldata$ChildID)-length(alldata2$ChildID) #134 duplicate entries removed
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"))
alldata$ageattestyr<-floor(alldata$ageattest/52.149) #Round year down to give age
attach(alldata)
alldata$ChildID<-as.factor(alldata$ChildID)
alldata$SchoolCode<-as.factor(alldata$SchoolCode)
alldata$IsAccountRegistered<-as.factor(alldata$IsAccountRegistered)
alldata$ageattestyr<-floor(alldata$ageattest/52.149)
alldata$ageattestyr<-as.integer(alldata$ageattestyr)
alldata<-alldata[!alldata$AcademicYear=="2011/2012"&!alldata$AcademicYear=="2012/2013",]
alldata$AcademicYear <- droplevels(alldata$AcademicYear)
#CALCULATE CLASS WHEN MEASURED (Install Lubridate)
thisyear<-alldata[!alldata$AcademicYear=="2012/2013",] #Exclude pre 2013/14 measurements as there are too few of these
thisyear$sumage<-round(thisyear$ageattestyr,digits=0)
thisyear$birthmonth<-month(as.POSIXlt(thisyear$DateOfBirth, format="%Y-%m-%d"))
thisyear$assmonth<-month(as.POSIXlt(thisyear$AssessmentDate, format="%Y-%m"))
thisyear$class<-0
thisyear$class<-ifelse(thisyear$ageattestyr==4,0,NA) #All 4 year olds are in Reception
thisyear$class<-ifelse(thisyear$ageattestyr==5,1,thisyear$class) #most 5 year olds are in year one
thisyear$class<-ifelse(thisyear$birthmonth>=9 & thisyear$ageattestyr==5 &thisyear$assmonth>8,0,thisyear$class) #5 year olds that have a birthday from September onwards that are measured in the autumn term are in Reception
thisyear$class<-ifelse(thisyear$birthmonth<=thisyear$assmonth & thisyear$ageattestyr==5 &thisyear$assmonth<=8,0,thisyear$class) #5 year olds with a birthday before September that are measured in the spring term are in Reception
thisyear$class<-ifelse(thisyear$ageattestyr==6,2,thisyear$class) # most 6 year olds are in year 2
thisyear$class<-ifelse(thisyear$birthmonth>=9 & thisyear$ageattestyr==6&thisyear$assmonth>8,1,thisyear$class) #6 year olds that have a birthday from September onwards that are measured in the autumn term are in yr1
thisyear$class<-ifelse(thisyear$birthmonth<=thisyear$assmonth & thisyear$ageattestyr==6 &thisyear$assmonth<=8,1,thisyear$class) #6 year olds with a birthday before September that are measured in the spring term are in Reception
thisyear$class<-ifelse(thisyear$ageattestyr==7,3,thisyear$class) # most 7 year olds are in year 3
thisyear$class<-ifelse(thisyear$birthmonth>=9 & thisyear$ageattestyr==7 &thisyear$assmonth>8,2,thisyear$class) #7 year olds that have a birthday from September onwards that are measured in the autumn term are in yr2
thisyear$class<-ifelse(thisyear$birthmonth<=thisyear$assmonth & thisyear$ageattestyr==7 &thisyear$assmonth<=8,2,thisyear$class) #7 year olds with a birthday before September that are measured in the spring term are in yr1
thisyear$class<-ifelse(thisyear$ageattestyr==8,4,thisyear$class) # most 8 year olds are in year 4
thisyear$class<-ifelse(thisyear$birthmonth>=9 & thisyear$ageattestyr==8 &thisyear$assmonth>8,3,thisyear$class) #8 year olds that have a birthday from September onwards that are measured in the autumn term are in yr3
thisyear$class<-ifelse(thisyear$birthmonth<=thisyear$assmonth & thisyear$ageattestyr==8 &thisyear$assmonth<=8,3,thisyear$class) #8 year olds with a birthday before September that are measured in the spring term are in yr3
thisyear$class<-ifelse(thisyear$ageattestyr==9,5,thisyear$class) # most 9 year olds are in year 5
thisyear$class<-ifelse(thisyear$birthmonth>=9 & thisyear$ageattestyr==9 &thisyear$assmonth>8,4,thisyear$class) #9 year olds that have a birthday from September onwards that are measured in the autumn term are in yr4
thisyear$class<-ifelse(thisyear$birthmonth<=thisyear$assmonth & thisyear$ageattestyr==9 &thisyear$assmonth<=8,4,thisyear$class) #9 year olds with a birthday before September that are measured in the spring term are in yr4
thisyear$class<-ifelse(thisyear$ageattestyr==10,6,thisyear$class) # most 10 year olds are in year 6
thisyear$class<-ifelse(thisyear$birthmonth>=9 & thisyear$ageattestyr==10 &thisyear$assmonth>8,5,thisyear$class) #10 year olds that have a birthday from September onwards that are measured in the autumn term are in yr5
thisyear$class<-ifelse(thisyear$birthmonth<=thisyear$assmonth & thisyear$ageattestyr==10 &thisyear$assmonth<=8,5,thisyear$class) #10 year olds with a birthday before September that are measured in the spring term are in yr5
thisyear$class<-ifelse(thisyear$ageattestyr==11,6,thisyear$class)
champ2<-thisyear
attach(champ2)
champ2$class<-as.numeric(champ2$class)
#CHOOSE FIRST WEIGHT CATEGORY RECORDED PER YEAR WHERE THERE ARE MULTIPLE MEASUREMENTS WITHIN A YEAR
d<-duplicated(champ2[,c(1,21)],fromLast=FALSE)
table(d)
#2991 (~3% of children) have multiple measurements within a year
head(champ2[d,],n=10)
champ2<-champ2[!d,]
#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)#morbid
table(champ2$weightcat)
champ2$NoRecords<-1
alldata<-champ2
attach(alldata)
#Assign class at test vector
alldata$Recordr<-as.numeric(ifelse(class==0,1,0))
alldata$Record1<-as.numeric(ifelse(class==1,1,0))
alldata$Record2<-as.numeric(ifelse(class==2,1,0))
alldata$Record3<-as.numeric(ifelse(class==3,1,0))
alldata$Record4<-as.numeric(ifelse(class==4,1,0))
alldata$Record5<-as.numeric(ifelse(class==5,1,0))
alldata$Record6<-as.numeric(ifelse(class==6,1,0))
#Print BMI at each test
alldata$BMIr<-as.numeric(ifelse(class==0,BMI,NA))
alldata$BMI1<-as.numeric(ifelse(class==1,BMI,NA))
alldata$BMI2<-as.numeric(ifelse(class==2,BMI,NA))
alldata$BMI3<-as.numeric(ifelse(class==3,BMI,NA))
alldata$BMI4<-as.numeric(ifelse(class==4,BMI,NA))
alldata$BMI5<-as.numeric(ifelse(class==5,BMI,NA))
alldata$BMI6<-as.numeric(ifelse(class==6,BMI,NA))
#Print weightcat at each age
alldata$weightcatr<-as.numeric(ifelse(class==0,weightcat,NA))
alldata$weightcat1<-as.numeric(ifelse(class==1,weightcat,NA))
alldata$weightcat2<-as.numeric(ifelse(class==2,weightcat,NA))
alldata$weightcat3<-as.numeric(ifelse(class==3,weightcat,NA))
alldata$weightcat4<-as.numeric(ifelse(class==4,weightcat,NA))
alldata$weightcat5<-as.numeric(ifelse(class==5,weightcat,NA))
alldata$weightcat6<-as.numeric(ifelse(class==6,weightcat,NA))
attach(alldata)
#Determine how many measurements taken per child
alldata$NoRecords<-(Recordr+Record1+Record2+Record3+Record4+Record5+Record6)
Recs<-aggregate(alldata$NoRecords, by=list(Category=ChildID), FUN=sum)
names(Recs)[names(Recs)=="x"] <- "NoRecs"
names(Recs)[names(Recs)=="Category"] <- "ChildID"
##############################################################################
#SELECT CHILDREN WITH MULTIPLE MEASUREMENTS
Recs2<-Recs[Recs$NoRecs>1,]
length(Recs2$ChildID) #33127 children were measured twice
multiple<-alldata[alldata$ChildID %in% Recs2$ChildID,]
multiple<-with(multiple, multiple[order(ChildID),])
attach(multiple)
as.numeric(c(weightcatr,weightcat1,weightcat2,weightcat3,weightcat4,weightcat5,weightcat6))
multiple$ChildID<-as.factor(multiple$ChildID)
multiple$IsAccountRegistered<-as.numeric(multiple$IsAccountRegistered)-1
multiplebase<-multiple[,c(1,2)]
multiplefirst<-multiple[!is.na(multiple$weightcatr),c(1, 38)]
multiple1<-multiple[!is.na(multiple$weightcat1),c(1, 39)]
multiple2<-multiple[!is.na(multiple$weightcat2),c(1, 40)]
multiple3<-multiple[!is.na(multiple$weightcat3),c(1, 41)]
multiple4<-multiple[!is.na(multiple$weightcat4),c(1, 42)]
multiple5<-multiple[!is.na(multiple$weightcat5),c(1, 43)]
multiple6<-multiple[!is.na(multiple$weightcat6),c(1, 44)]
multiplereg<-multiple[c(1, 10)]
library(plyr)
multi<-merge(multiplebase,multiplefirst,by="ChildID",all=TRUE)
multi<-merge(multi,multiple1,by="ChildID",all=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,multiple6,by="ChildID",all.x=TRUE)
multi<-merge(multi,multiplereg,by="ChildID",all.x=TRUE)
multi2<-aggregate(multi,by=list(multi$ChildID),FUN=mean)
multi<-multi2
multi$ChildID<-multi$Group.1
length(multi$ChildID)#33127
attach(multi)
measure1<-weightcatr
measure1<-as.numeric(measure1)
measure1<-ifelse(is.na(measure1)&weightcat1>=1,paste(weightcat1),measure1)
measure1<-ifelse(is.na(measure1)&weightcat2>=1,paste(weightcat2),measure1)
measure1<-ifelse(is.na(measure1)&weightcat3>=1,paste(weightcat3),measure1)
measure1<-ifelse(is.na(measure1)&weightcat4>=1,paste(weightcat4),measure1)
measure1<-ifelse(is.na(measure1)&weightcat5>=1,paste(weightcat5),measure1)
measure2<-ifelse(!is.na(weightcatr),paste(weightcat1),NA)
measure2<-as.numeric(measure2)
measure2<-ifelse(is.na(measure2)&(!is.na(weightcatr)|!is.na(weightcat1)),paste(weightcat2),measure2)
measure2<-as.numeric(measure2)
measure2<-ifelse(is.na(measure2)&(!is.na(weightcatr)|!is.na(weightcat1)|!is.na(weightcat2)),paste(weightcat3),measure2)
measure2<-as.numeric(measure2)
measure2<-ifelse(is.na(measure2)&(!is.na(weightcatr)|!is.na(weightcat1)|!is.na(weightcat2)|!is.na(weightcat3)),paste(weightcat4),measure2)
measure2<-as.numeric(measure2)
measure2<-ifelse(is.na(measure2)& (!is.na(weightcatr)|!is.na(weightcat1)|!is.na(weightcat2)|!is.na(weightcat3)|!is.na(weightcat4))&weightcat5>=1,paste(weightcat5),measure2)
measure2<-as.numeric(measure2)
measure2<-ifelse(is.na(measure2)& (!is.na(weightcatr)|!is.na(weightcat1)|!is.na(weightcat2)|!is.na(weightcat3)|!is.na(weightcat4)|!is.na(weightcat5))&weightcat6>=1,paste(weightcat6),measure2)
measure2<-as.numeric(measure2)
multi$measure1<-measure1
multi$measure2<-measure2
yes<-multi[multi$IsAccountRegistered=="1",]
no<-multi[multi$IsAccountRegistered=="0",]
table(yes$measure1,yes$measure2)
table(no$measure1,no$measure2)