-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBMIRcodetestdatawritetable
135 lines (121 loc) · 10 KB
/
BMIRcodetestdatawritetable
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
#Read in data
setwd("c:/Users/mqbpjhr4/Documents")
champtable<-read.table("LMStableedited.csv", header=T,sep=",")
tail(champtable)
#IN EXCEL, APPLY THE NCMP ERROR REJECTION CUTOFFS BASED ON Z SCORE FOR HEIGHT, WEIGHT, BMI (+ OR - 7)
attach(champtable)
#Check how many children and schools are recorded
as.factor(SchoolCode) #201 schools
as.factor(ChildID) #63348 children
SchoolCode<-as.character(SchoolCode)
ChildID<-as.character(ChildID)
#The data does not have day values within the dates. I have assigned these all as the 1st of the month for R work purposes (getting lubridate to function), so 1 in 12 ages could be inaccurate by up to 30 d
champtable$ageattestyr<-champtable$ageattest/52.149
#REMOVE OUT OF RANGE AGES DUE TO DATE ENTRY ERRORS OR MEASUREMENTS RELATING TO BABY WEIGHTS
champ2<-subset(champtable, champtable$ageattestyr<12 & champtable$ageattestyr>=4 & champtable$BMI>0)
attach(champ2)
champ2$ageattestyr<-as.integer(champ2$ageattestyr)
#DRAW IN BMI THRESHOLD TO WORK OUT CENTILES FOR OVERWEIGHT, OBESE, SEVERELY OBESE
champ2$weightcat<-"normal"
champ2$weightcat<-ifelse(champ2$Centile_BMI>=90.879,"overweight",champ2$weightcat)
champ2$weightcat<-ifelse(champ2$Centile_BMI>=97.725,"obese",champ2$weightcat)
champ2$weightcat<-ifelse(champ2$Centile_BMI<2.275,"underweight",champ2$weightcat)
champ2$weightcat<-ifelse(champ2$Centile_BMI>=99.913,"morbidly obese",champ2$weightcat)
champ2$weightcat<-ifelse(champ2$Centile_BMI<0.383,"very underweight",champ2$weightcat)
table(champ2$weightcat)
attach(champ2)
aggdatachamp <-aggregate(champ2$IsAccountRegistered, by=list(champ2$ChildID), FUN=sum, na.rm=TRUE)
names(aggdatachamp)[names(aggdatachamp)=="x"] <- "totalchamp"
champ2$ones<-1
aggdataall <-aggregate(champ2$ones, by=list(champ2$ChildID), FUN=sum, na.rm=TRUE)
names(aggdataall)[names(aggdataall)=="x"] <- "Totalrecords"
regchange<-aggdatachamp/aggdataall
#CREATE DATA SUBSET FOR THIS YEAR'S DATA
thisyear<-subset(champ2, champ2$AcademicYear=="2016/2017")
attach(thisyear)
error<-((qnorm(0.95)*BMI)/(sqrt(length(BMI))))
thisyear$Obesity<-as.numeric(ifelse(thisyear$weightcat=="obese",1,0))
thisyear$Overweight<-as.numeric(ifelse(thisyear$weightcat=="overweight",1,0))
thisyear$MorbidObesity<-as.numeric(ifelse(thisyear$weightcat=="morbidly obese",1,0))
thisyear$Underweight<-as.numeric(ifelse(thisyear$weightcat=="underweight",1,0))
thisyear$VeryUnderweight<-as.numeric(ifelse(thisyear$weightcat=="very underweight",1,0))
thisyear$Normal<-as.numeric(ifelse(thisyear$weightcat=="normal",1,0))
library(lubridate)
#CALCULATE CLASS WHEN MEASURED (Install Lubridate)
ndate1<-paste(DateOfBirth,"-01",sep=""); ndate1
ndate2<-paste(AssessmentDate,"-01",sep=""); ndate1
thisyear$DateOfBirth<-(as.Date(ndate1))
thisyear$AssessmentDate<-(as.Date(ndate2))
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<-NA
thisyear$ageattestyr<-floor(thisyear$ageattest/52.149) #Round year down to give age
thisyear$class<-ifelse(thisyear$birthmonth<9 & thisyear$ageattestyr==4,0,thisyear$class) #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 Reception
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 yr1
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)
attach(thisyear)
#Summary stats by class and gender
aggdatamean <-aggregate(thisyear, by=list(class,Gender), FUN=mean, na.rm=TRUE)
names(aggdatamean)[names(aggdatamean)=="BMI"] <- "MeanBMI"
aggerror <-aggregate(error, by=list(class,Gender), mean, na.rm=TRUE)
names(aggerror)[names(aggerror)=="x"] <- "SE95"
aggdatamedian <-aggregate(BMI, by=list(class,Gender), median, na.rm=TRUE)
names(aggdatamedian)[names(aggdatamedian)=="x"] <- "MedianBMI"
aggdataobsums <-aggregate(thisyear$Obesity, by=list(class,Gender), FUN=sum, na.rm=TRUE)
names(aggdataobsums)[names(aggdataobsums)=="x"] <- "TotalObese"
aggdataovsums <-aggregate(thisyear$Overweight, by=list(class,Gender), FUN=sum, na.rm=TRUE)
names(aggdataovsums)[names(aggdataovsums)=="x"] <- "TotalOverweight"
aggdataunsums <-aggregate(thisyear$Underweight, by=list(class,Gender), FUN=sum, na.rm=TRUE)
names(aggdataunsums)[names(aggdataunsums)=="x"] <- "TotalUnderweight"
aggdatansums <-aggregate(thisyear$Normal, by=list(class,Gender), FUN=sum, na.rm=TRUE)
names(aggdatansums)[names(aggdatansums)=="x"] <- "TotalNormal"
aggdatamosums <-aggregate(thisyear$MorbidObesity, by=list(class,Gender), FUN=sum, na.rm=TRUE)
names(aggdatamosums)[names(aggdatamosums)=="x"] <- "TotalMorbid"
aggdatavosums <-aggregate(thisyear$VeryUnderweight, by=list(class,Gender), FUN=sum, na.rm=TRUE)
names(aggdatavosums)[names(aggdatavosums)=="x"] <- "TotalVUnderweight"
thisyear$ChildID<-"1"
thisyear$ChildID<-as.numeric(thisyear$ChildID)
aggdatasums <-aggregate(thisyear$ChildID, by=list(class,Gender), FUN=sum, na.rm=TRUE)
names(aggdatasums)[names(aggdatasums)=="x"] <- "TotalChildren"
summarydata<-merge(aggdatamean,aggdatamedian,all=TRUE, by=c('Group.1','Group.2'))
summarydata2A<-merge(summarydata,aggdataobsums,all=TRUE, by=c('Group.1','Group.2'))
summarydata2B<-merge(summarydata2A,aggdataunsums,all=TRUE, by=c('Group.1','Group.2'))
summarydata2C<-merge(summarydata2B,aggdatansums,all=TRUE, by=c('Group.1','Group.2'))
summarydata2D<-merge(summarydata2C,aggdatamosums,all=TRUE, by=c('Group.1','Group.2'))
summarydata2<-merge(summarydata2D,aggdatavosums,all=TRUE, by=c('Group.1','Group.2'))
summarydata3<-merge(summarydata2,aggdataovsums,all=TRUE, by=c('Group.1','Group.2'))
summarydata4<-merge(summarydata3,aggdatasums,all=TRUE, by=c('Group.1','Group.2'))
summarydata5<-merge(summarydata4,aggerror,all=TRUE, by=c('Group.1','Group.2'))
names(summarydata5)
summaryfigures2016<-file(paste("summaryfigures2016.csv"), open="w")
cat("Gender", "Class","TotalChildren","MeanBMI","SE95","MedianBMI","NumberMorbid","NumberObese","NumberOverweight","NumberNormalWeight","NumberUnderweight","NumberVUnderweight","PropMorbid","PropObese","PropOverweight","PropNormal","PropUnderweight","PropVUnderweight","\n", sep=",",file="summaryfigures2016.csv",append=TRUE)
for (n in 1:14){
cat((paste(summarydata5$Group.2[n])),(paste(summarydata5$Group.1[n])),(paste(summarydata5$TotalChildren[n])), (paste(summarydata5$MeanBMI[n])),(paste(summarydata5$SE95[n])),
(paste(summarydata5$MedianBMI[n])),(paste(summarydata5$TotalMorbid[n])),(paste(summarydata5$TotalObese[n])),(paste(summarydata5$TotalOverweight[n])),(paste(summarydata5$TotalNormal[n])),
(paste(summarydata5$TotalUnderweight[n])),(paste(summarydata5$TotalVUnderweight[n])),
(paste(summarydata5$TotalMorbid[n]/summarydata5$TotalChildren[n])),
(paste(summarydata5$TotalObese[n]/summarydata5$TotalChildren[n])),
(paste(summarydata5$TotalOverweight[n]/summarydata5$TotalChildren[n])),
(paste(summarydata5$TotalNormal[n]/summarydata5$TotalChildren[n])),
(paste(summarydata5$TotalUnderweight[n]/summarydata5$TotalChildren[n])),
(paste(summarydata5$TotalVUnderweight[n]/summarydata5$TotalChildren[n])),
"\n", file="summaryfigures2016.csv", sep=",", fill=FALSE, labels=NULL, append=TRUE)
}