

library(archetypes)
library(Anthropometry)

#waveform

casose <-c('we1.txt','we2.txt','we3.txt','we4.txt','we5.txt','we6.txt','we7.txt','we8.txt','we9.txt','we10.txt','we11.txt','we12.txt','we13.txt','we14.txt','we15.txt','we16.txt','we17.txt','we18.txt','we19.txt','we20.txt','we21.txt','we22.txt','we23.txt','we24.txt','we25.txt','we26.txt','we27.txt','we28.txt','we29.txt','we30.txt','we31.txt','we32.txt','we33.txt','we34.txt','we35.txt','we36.txt','we37.txt','we38.txt','we39.txt','we40.txt','we41.txt','we42.txt','we43.txt','we44.txt','we45.txt')

casose <- c(casose,'we46.txt','we47.txt','we48.txt','we49.txt','we50.txt')



casost <-c('wt1.txt','wt2.txt','wt3.txt','wt4.txt','wt5.txt','wt6.txt','wt7.txt','wt8.txt','wt9.txt','wt10.txt','wt11.txt','wt12.txt','wt13.txt','wt14.txt','wt15.txt','wt16.txt','wt17.txt','wt18.txt','wt19.txt','wt20.txt','wt21.txt','wt22.txt','wt23.txt','wt24.txt','wt25.txt','wt26.txt','wt27.txt','wt28.txt','wt29.txt','wt30.txt','wt31.txt','wt32.txt','wt33.txt','wt34.txt','wt35.txt','wt36.txt','wt37.txt','wt38.txt','wt39.txt','wt40.txt','wt41.txt','wt42.txt','wt43.txt','wt44.txt','wt45.txt')

casost <- c(casost,'wt46.txt','wt47.txt','wt48.txt','wt49.txt','wt50.txt')




norep=3 #20

ese=c()

K=3
for (i in 1:50){

we=read.table(casose[i])


 ex=we[,1:101]



set.seed(2015)
lass10 <- stepArchetypesRawData(data = ex, numArch=K, numRep = norep,verbose = FALSE)
#lass10 <- FstepArchetypesMod(data=X,k=K,verbose=FALSE,nrep=norep,PM=PM,saveHistory=F) 
ai <- bestModel(lass10[[1]]) #archetypes



a3=ai$archetypes #son coeficientes en base de archetypes; archetypes



  data=ex
huge=200
k=3 #3 archetypes

  n <- ncol(t(data))
  x_gvv <- rbind(t(data), rep(huge, n))
  zs <- rbind(t(a3), rep(huge, k))  
  zs <- as.matrix(zs)
  alphascce <- matrix(0, nrow = k, ncol = n)
  
  for (j in 1 : n){
   alphascce[, j] = coef(nnls(zs, x_gvv[,j]))
  }



#test
wt=read.table(casost[i])

tex=wt[,1:101]

data=tex

  
huge=200
k2=k

  n <- ncol(t(data))
  x_gvv <- rbind(t(data), rep(huge, n))
  
  alphascct <- matrix(0, nrow = k2, ncol = n)
  
  for (j in 1 : n){
   alphascct[, j] = coef(nnls(zs, x_gvv[,j]))
  }



tae=t(alphascce)
tat=t(alphascct)

#mirar donde esté minimo en tae para cada clase, para saber que clase en tat


#en entrenamiento
cle=max.col(-tae)

permuta=c()
#coinci=c()

for (ii in 1:3){
aux=0
 for ( jj in 1:3){

s=sum((cle==ii)== (we[,102]==jj))
if(aux<s){
aux=s
permuta[ii]=jj
}
}
}



cl=max.col(-tat)


clc=cl
clc[cl==1]=permuta[1]
 clc[cl==2]=permuta[2]
 clc[cl==3]=permuta[3]


ese[i]=sum(clc!=wt[,102])/750

cat("iteration = ", i,"mean ese",  mean(ese), "\n")
}


summary(ese)

######################
#norep=3
> summary(ese)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
0.04000 0.06400 0.06867 0.06963 0.07600 0.09867

> sd(ese)
[1] 0.01093303


#################
#discretizing, after smoothing

numbase=32#;%12,32,64,256
mibspline15=create.bspline.basis(c(1,21), numbase,6);
miLfdobj = int2Lfd(2);  
la = 10#;   %tb. 1
mifdPar = fdPar(mibspline15, miLfdobj, la);

norep=3 #20

ese=c()

K=3
start.time <- Sys.time()
for (i in 1:50){

we=read.table(casose[i])


 ex=we[,1:101]
 #nombre=['wt' num2str(i) '.txt'];
#wt=load(nombre);
#tex=wt(:,1:101);




 ti=seq(1,21,by=0.2)

direcsbspline15 = smooth.basis(ti,t(ex),mifdPar)


fp=eval.fd(ti,direcsbspline15$fd) #evaluate the smoothed 


set.seed(2015)
lass10 <- stepArchetypesRawData(data = t(fp), numArch=K, numRep = norep,verbose = FALSE)
#lass10 <- FstepArchetypesMod(data=X,k=K,verbose=FALSE,nrep=norep,PM=PM,saveHistory=F) 
ai <- bestModel(lass10[[1]]) #archetypes



a3=ai$archetypes #son coeficientes en base de archetypes; archetypes



  data=t(fp)
huge=200
k=3 #3 archetypes

  n <- ncol(t(data))
  x_gvv <- rbind(t(data), rep(huge, n))
  zs <- rbind(t(a3), rep(huge, k))  
  zs <- as.matrix(zs)
  alphascce <- matrix(0, nrow = k, ncol = n)
  
  for (j in 1 : n){
   alphascce[, j] = coef(nnls(zs, x_gvv[,j]))
  }



#test
wt=read.table(casost[i])

tex=wt[,1:101]

direcsbspline15t = smooth.basis(ti,t(tex),mifdPar)


fpt=eval.fd(ti,direcsbspline15t$fd) #evaluate the smoothed 


data=t(fpt)

  
huge=200
k2=k

  n <- ncol(t(data))
  x_gvv <- rbind(t(data), rep(huge, n))
  
  alphascct <- matrix(0, nrow = k2, ncol = n)
  
  for (j in 1 : n){
   alphascct[, j] = coef(nnls(zs, x_gvv[,j]))
  }



tae=t(alphascce)
tat=t(alphascct)

#mirar donde esté minimo en tae para cada clase, para saber que clase en tat


#en entrenamiento
cle=max.col(-tae)

permuta=c()
#coinci=c()

for (ii in 1:3){
aux=0
 for ( jj in 1:3){

s=sum((cle==ii)== (we[,102]==jj))
if(aux<s){
aux=s
permuta[ii]=jj
}
}
}



cl=max.col(-tat)


clc=cl
clc[cl==1]=permuta[1]
 clc[cl==2]=permuta[2]
 clc[cl==3]=permuta[3]


ese[i]=sum(clc!=wt[,102])/750

cat("iteration = ", i,"mean ese",  mean(ese), "\n")
}
end.time <- Sys.time()
time.taken <- end.time - start.time

summary(ese)


########################
> summary(ese)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
0.04267 0.05767 0.06400 0.06397 0.07300 0.08933
> sd(ese)
[1] 0.01113427





