library(readr)
library(cluster)
library(fpc)
library(mclust)
library(neuralnet)

train10_1 = read_csv('../input/train/subj10_series1_data.csv')
events10_1 = read_csv("../input/train/subj10_series1_events.csv")

# collapsing event columns into one
#events10_1$Results[events10_1$HandStart == 1] <- 1
#events10_1$Results[events10_1$FirstDigitTouch == 1] <- 2
#events10_1$Results[events10_1$BothStartLoadPhase == 1] <- 3
#events10_1$Results[events10_1$LiftOff == 1] <- 4
#events10_1$Results[events10_1$Replace == 1] <- 5
#events10_1$Results[events10_1$BothReleased == 1] <- 6

#HandStart, FirstDigitTouch, BothStartLoadPhase, LiftOff, Replace, BothReleased

#table(is.na(events10_1$Results))

#temp$Results <- as.numeric(temp$Results)
#events10_1$Results <- as.numeric(events10_1$Results)

#summary(temp)
#events10_1$Results <- temp$Results
#hist(events10_1$Results)
#summary(train10_1)

# transforming id
train10_1$id <- as.character(train10_1$id)
train10_1$id <- sapply(train10_1$id, FUN=function(x) {strsplit(x, split='[_]')[[1]][3]})
train10_1$id <- as.factor(train10_1$id)

events10_1$id <- as.character(events10_1$id)
events10_1$id <- sapply(events10_1$id, FUN=function(x) {strsplit(x, split='[_]')[[1]][3]})
events10_1$id <- as.factor(events10_1$id)


# checking n/a and null values
#table(is.na(train10_1))

#summary(train10_1)
#summary(events10_1)

# 1/ 
#fit1 <- kmeans(train10_1, 6, iter.max = 10, nstart = 1,
#       algorithm = c("Hartigan-Wong"), trace=FALSE)
       
#hist(fit1$cluster)
#hist(events10_1$Results)

#clusplot(train10_1, fit1$cluster, color=TRUE, shade=TRUE,
#   labels=2, lines=0)

#plotcluster(train10_1, fit$cluster) 

# 2/
#fit2 <- kmeans(train10_1, 6)
#clusplot(train10_1, fit2$cluster, color=TRUE, shade=TRUE,
#   labels=2, lines=0)



# 3/
#fit3 <- Mclust(train10_1)
#plot(fit3) # plot results
#summary(fit3)


# 4/
#fit4 <- kmeans(train10_1, 6, iter.max = 10, nstart = 1,
#       algorithm = c("Forgy"), trace=FALSE)
#hist(fit4$cluster)
#clusplot(train10_1, fit4$cluster, color=TRUE, shade=TRUE,
#   labels=2, lines=0)

# 5/
#fit5 <- kmeans(train10_1, 6, iter.max = 10, nstart = 1,
#       algorithm = c("MacQueen"), trace=FALSE)
#hist(fit5$cluster)
#clusplot(train10_1, fit5$cluster, color=TRUE, shade=TRUE,
#   labels=2, lines=0)


#############
#plot(train10_1, col = fit1$cluster)


# ==============================================================
#fit <- kmeans(train10_1, 6, iter.max = 10, nstart = 25,
#       algorithm = c("Hartigan-Wong"), trace=FALSE)
#hist(fit$cluster)
#clusplot(train10_1, fit$cluster, color=TRUE, shade=TRUE,
#   labels=2, lines=0)
#summary(fit)

#train10_1$Cluster <- fit$cluster
train10_1$Results <- events10_1$Results

#summary(train10_1)
#table(train10_1$Results)
#table(train10_1$Cluster)

#plot(train10_1$Results)

#plotcluster(train10_1, fit$cluster) 


#summary(train10_1)
#summary(events10_1)

#events10_1$HandStart[events10_1$HandStart == 0] <- NA
#events10_1$FirstDigitTouch[events10_1$FirstDigitTouch == 0] <- NA
#events10_1$BothStartLoadPhase[events10_1$BothStartLoadPhase == 0] <- NA
#events10_1$LiftOff[events10_1$LiftOff == 0] <- NA
#events10_1$Replace[events10_1$Replace == 0] <- NA
#events10_1$BothReleased[events10_1$BothReleased == 0] <- NA
#HandStart, FirstDigitTouch, BothStartLoadPhase, LiftOff, Replace, BothReleased



# designing and executing
# For Right Handed people
# Fp1 designs the movement?
# FC5, FC1, and C3 execute movement
#plot(train10_1$Fp1, type="l", col="blue", 
#   axes=F, ann=T,
#   cex.lab=0.8, lwd=2)

#lines(train10_1$FC5, type="o", pch=22, col="red")
#lines(train10_1$FC1, type="o", pch=23, col="magenta")
#lines(train10_1$C3, type="o", pch=24, col="purple")

#points(events10_1$HandStart, type="o", col="blue")
#points(events10_1$FirstDigitTouch, type="o", col="red")
#points(events10_1$BothStartLoadPhase, type="o", col="magenta")
#points(events10_1$LiftOff, type="o", col="yellow")
#points(events10_1$Replace, type="o", col="green")
#points(events10_1$BothReleased, type="o", col="black")



# OR F7, and F3 design the movement?
#plot(train10_1$F7, type="l", col="blue", 
#   axes=F, ann=T,
#   cex.lab=0.8, lwd=2)

#lines(train10_1$F3, type="o", pch=21, col="yellow")
#lines(train10_1$FC5, type="o", pch=22, col="red")
#lines(train10_1$FC1, type="o", pch=23, col="magenta")
#lines(train10_1$C3, type="o", pch=24, col="purple")

#points(events10_1$HandStart, type="o", col="blue")
#points(events10_1$FirstDigitTouch, type="o", col="red")
#points(events10_1$BothStartLoadPhase, type="o", col="magenta")
#points(events10_1$LiftOff, type="o", col="yellow")
#points(events10_1$Replace, type="o", col="green")
#points(events10_1$BothReleased, type="o", col="black")


# hmm
#plot(train10_1[2500:7717,]$F7, type="l", col="blue", 
#   axes=F, ann=T,
#   cex.lab=0.8, lwd=2)

#lines(train10_1[2500:7717,]$F3, type="o", pch=21, col="yellow")
#lines(train10_1[2500:7717,]$FC5, type="o", pch=22, col="red")
#lines(train10_1[2500:7717,]$FC1, type="o", pch=23, col="magenta")
#lines(train10_1[2500:7717,]$C3, type="o", pch=24, col="purple")

#points(events10_1[2500:7717,]$HandStart, type="o", col="blue")
#points(events10_1[2500:7717,]$FirstDigitTouch, type="o", col="red")
#points(events10_1[2500:7717,]$BothStartLoadPhase, type="o", col="magenta")
#points(events10_1[2500:7717,]$LiftOff, type="o", col="yellow")
#points(events10_1[2500:7717,]$Replace, type="o", col="green")
#points(events10_1[2500:7717,]$BothReleased, type="o", col="black")




#plot(events10_1$HandStart, type="o", col="blue", 
#   axes=F, ann=T,
#   cex.lab=0.8, lwd=2)
#lines(events10_1$FirstDigitTouch, type="o", pch=21, col="red")
#lines(events10_1$BothStartLoadPhase, type="o", pch=22, col="magenta")
#lines(events10_1$LiftOff, type="o", pch=23, col="yellow")
#lines(events10_1$Replace, type="o", pch=24, col="green")
#lines(events10_1$BothReleased, type="o", pch=25, col="black")


#events10_1$HandStart[events10_1$HandStart == 0] <- NA
#events10_1$FirstDigitTouch[events10_1$FirstDigitTouch == 0] <- NA
#events10_1$BothStartLoadPhase[events10_1$BothStartLoadPhase == 0] <- NA
#events10_1$LiftOff[events10_1$LiftOff == 0] <- NA
#events10_1$Replace[events10_1$Replace == 0] <- NA
#events10_1$BothReleased[events10_1$BothReleased == 0] <- NA


train10_1$HandStart <- events10_1$HandStart
train10_1$FirstDigitTouch <- events10_1$FirstDigitTouch
train10_1$BothStartLoadPhase <- events10_1$BothStartLoadPhase
train10_1$LiftOff <- events10_1$LiftOff
train10_1$Replace <- events10_1$Replace
train10_1$BothReleased <- events10_1$BothReleased

# Neural network
#fit <- neuralnet(HandStart + FirstDigitTouch + BothStartLoadPhase + LiftOff + Replace + BothReleased ~ Fp1 + Fp2 +
#            F7 + F3 + F8 + F4 + FC1 + FC2 + FC5 + FC6 + Fz + T7 + T8 + C3 + Cz + C4 + TP9 + TP10 + CP1 + CP2 + CP5 + CP6 +
#            P7 + P8 + P3 + P4 + Pz + PO9 + PO10 + O1 + O2 + Oz, train10_1, hidden = c(4, 8), lifesign = "minimal", 
#            linear.output = FALSE, threshold = 0.1, rep = 1)
            
#fit <- neuralnet(HandStart + FirstDigitTouch + BothStartLoadPhase + LiftOff + Replace + BothReleased ~ Fp1 + 
#            F7 + F3 + FC1 + FC5 + T7 + C3 + TP9 + CP1 + CP5 + 
#            P7 + P3 + PO9 + O1, train10_1, hidden = 4, lifesign = "minimal", 
#            linear.output = FALSE, threshold = 0.1, rep = 1, err.fct = 'ce')            

train <- train10_1[0:246852,]
temp <- train10_1[246853:262387,]
test <- c(temp$Fp1, temp$F7, temp$F3, temp$FC1, temp$FC5, temp$T7, temp$C3, temp$TP9, temp$CP1, temp$CP5, temp$P7, temp$P3, temp$PO9, temp$O1) 
 
fit <- neuralnet(HandStart + FirstDigitTouch ~ Fp1 + F7 + F3 + FC1 + FC5 + T7 + C3 + TP9 + CP1 + CP5 + 
            P7 + P3 + PO9 + O1, data=train, algorithm="backprop", hidden=c(14, 8, 4), learningrate=0.1, 
            linear.output=FALSE, lifesign = "maximum")            
            
summary(fit)
coefficients(fit)

table(fit$response)
table(fit$net)

plot(fit, rep = "best")

fit$results <- compute(fit, test)#$as.numeric(fit.result)

table(pred$fit.result)
summary(pred)
pred$fit.result


#test$HandStartPred <- prediction(fit, train10_1[246853:262387,])
#predict(fit, train10_1[246853:262387,])
#test[246853:262387,]$Test[test$HandStartPred == test$HandStart] <- 1

#sum(test)


