#size boys according to charts, based on stature: X10


####################################################
#reading data and description for boys for t-shirt
####################################################


d=read.table("tshirt.txt", sep = ",")


medidas <- c("X10","X0030_mk","X1510","X1520","X1530","X3020_mk",
             "X3030_LR","X4020","X4030","X4040_mk","X4080_LR","X4510","X5020","X5030_mk","X5040_mk",
             "X6010_mk","X6011_mk","X6012_mk","X6510_mk","X7520","X7525","X7540","X8010_LR",
             "X8030_LR","X8040_LR","X8520_LR","X8550_LR")
             #,"X0100","X0110","X9035_LR","X9510_LR","X9520_LR","X9550_LR")

#datos=d[,c("User_code","Size_fit","Expert_evaluation", medidas)]

 datos=d[,c(1,3,32,34)]

st=read.table('statures.txt',header=T,sep="\t",dec='.')


###########################
#Classification boys Leave One Out individual
###########################



#boys

db=datos[d$SEXO=="V",]
#girls
#db=datos[d$SEXO=="M",]
#db=db[-22,] #nan in expert evaluation 

#removing nans, boys

kk=db[-24,]
kk=kk[-24,]

kk=kk[-62,]

db=kk
###end removing nans


ninos=unique(db[,1])

stnc=st[st[,1] %in% ninos,]
stn=stnc[,2]/10

#sizes
ss=rep(0,length(ninos))
ss[stn<163]='Y14'
ss[stn<153]='Y12' 
ss[stn<141]='Y10' 
ss[stn<129]='Y08' 
ss[stn<117]='Y06' 
ss[stn<111]='Y05' 
ss[stn<105]='Y04' 
ss[stn<99]='Y03' 
ss[stn<93]='Y02' 

se=rep(0,length(ninos))
for(i in 1:length(ninos)){
ac=db[,1] %in% ninos[i]
se[i]= as.character(db[ac,4][1])
}

#for labels intermediate-> expert evaluation, others (at least bigger, etc, one more) manually 

#example:
# i=9
# ac=db[,1] %in% ninos[i]
# db[ac,]

sef=se

sef[9]='Y10'
sef[11]='Y12'
sef[13]='Y03'
sef[32]='Y05'
sef[37]='Y02'
sef[39]='Y08'
sef[47]='Y06'
sef[48]='Y06'

(length(ninos)-sum(sef==ss))/length(ninos)
#[1] 0.74

#####################
#girls


db=datos[d$SEXO=="M",]

#removing nan

kk=db[-22,] #nan de expert evaluation
kk=kk[-22,]

kk=kk[-64,]

kk=kk[-64,]


db=kk
###end removing nans

ninos=unique(db[,1])

stnc=st[st[,1] %in% ninos,]
stn=stnc[,2]/10

#sizes
ss=rep(0,length(ninos))
ss[stn<163]='Y14'
ss[stn<153]='Y12' 
ss[stn<141]='Y10' 
ss[stn<129]='Y08' 
ss[stn<117]='Y06' 
ss[stn<111]='Y05' 
ss[stn<105]='Y04' 
ss[stn<99]='Y03' 
ss[stn<93]='Y02' 

se=rep(0,length(ninos))
for(i in 1:length(ninos)){
ac=db[,1] %in% ninos[i]
se[i]= as.character(db[ac,4][1])
}

#for labels intermediate-> expert evaluation, others manually
#if both same expert evaluation, the bigger one; if only one, that

#example:
# i=9
# ac=db[,1] %in% ninos[i]
# db[ac,]

sef=se

sef[1]='Y05'
sef[2]='Y14'
sef[4]='Y02'
sef[11]='Y04'
sef[15]='Y02'
sef[17]='Y02'
sef[22]='Y14'
sef[23]='Y04'
sef[26]='Y06'
sef[28]='Y10'
sef[35]='Y03'
sef[45]='Y04' #expert evaluation is bigger for big than correct
sef[54]='Y04'
sef[58]='Y03'




(length(ninos)-sum(sef==ss))/length(ninos)
# 0.6896552

#boys and girls
#errors:

(58+50-13-18)/(58+50)
#0.712963
