#read rhdf file
library(rhdf5)
train <- h5read("../input/train.h5","train")
columns <- train$axis0
train <- data.frame(as.double(train$block0_values[1,]), as.double(train$block0_values[2,]), t(train$block1_values))
colnames(train) <- columns
H5close()

#taken test 2% of test data and remove column 1 and 2 because ID and timestamp
testsize<-nrow(train)*0.02
testframe<-train[c(1:abs(testsize)),c(-1,-2)]


# cleaning process replace empty values with zero
testframe[is.na(testframe)]<-0


#testframe some of box plot to observe skewness
boxplot(testframe[1:500,])


# apply normalization min-max scaling
test.mm <- apply(testframe[,-109], MARGIN = 2, FUN = function(x) (x - min(x))/diff(range(x)))
boxplot(test.mm[,c(1:10)])

# z-score

test.mm <- apply(testframe[,-109], MARGIN = 2, FUN = function(x) (x - mean(x))/sd(x))
test.mm <- apply(testframe, MARGIN = 2, FUN = function(x) (x - mean(x))/(2*sd(x)))

boxplot (test.mm[,c(1:10)], main = "Z-score, 1 sd")
boxplot (test.mm[,c(1:10)], main = "Z-score, 2 sd")

#-----Section 09-------------------------------------------
# soft max scaling - iterative
library(DMwR)

#lambda scaling
test.lm <- apply(testframe[-109], MARGIN = 2, FUN = function(x) (SoftMax(x,lambda = 4, mean(x), sd(x))))
boxplot (test.lm , main = "Soft Max, lambda = 4")

test.lm <- apply(testframe[-109], MARGIN = 2, FUN = function(x) (SoftMax(x,lambda = 6, mean(x), sd(x))))
boxplot (test.lm , main = "Soft Max, lambda = 10")

test.lm <- apply(testframe[,-109], MARGIN = 2, FUN = function(x) (SoftMax(x,lambda = 20, mean(x), sd(x))))
boxplot (test.lm , main = "Soft Max, lambda = 20")

#min-max
modelframe<-data.frame(test.mm)
#lambda
modelframe<-data.frame(test.lm)
#all na clean
modelframe[is.na(modelframe)]<-0

summary (modelframe)

#regression predictors
modelframe$y<-testframe$y
n<-names(modelframe)

f<- as.formula(paste("y ~",paste(n[!n %in% "y"],collapse = " + ")))

reg<-lm(f,data = modelframe)
summary(reg)


#all signifcant min / max
modelrefined<-data.frame(modelframe$fundamental_1,modelframe$fundamental_7,modelframe$fundamental_18,modelframe$fundamental_20,modelframe$fundamental_21,modelframe$fundamental_24,modelframe$fundamental_25,modelframe$fundamental_34,modelframe$fundamental_36,modelframe$fundamental_41,modelframe$fundamental_43,modelframe$fundamental_52,modelframe$fundamental_54,modelframe$technical_0,modelframe$technical_2,modelframe$technical_6,modelframe$technical_11,modelframe$technical_13,modelframe$technical_17,modelframe$technical_18,modelframe$technical_19,modelframe$technical_20,modelframe$technical_21,modelframe$technical_22,modelframe$technical_30,modelframe$technical_36,modelframe$technical_40,modelframe$technical_43)
boxplot(modelrefined)

#all signifcant z score
modelrefined<-data.frame(modelframe$fundamental_5,modelframe$fundamental_7,modelframe$fundamental_10,modelframe$fundamental_25,modelframe$fundamental_26,modelframe$fundamental_27,modelframe$fundamental_31,modelframe$fundamental_48,modelframe$fundamental_51,modelframe$fundamental_53,modelframe$fundamental_54,modelframe$technical_2,modelframe$technical_6,modelframe$technical_13,modelframe$technical_17,modelframe$technical_18,modelframe$technical_19,modelframe$technical_20,modelframe$technical_21,modelframe$technical_30,modelframe$technical_37)

#all signifcant lambda 4
modelrefined<-data.frame(modelframe$fundamental_5,modelframe$fundamental_7,modelframe$fundamental_10,modelframe$fundamental_25,modelframe$fundamental_26,modelframe$fundamental_26,modelframe$fundamental_27,modelframe$fundamental_31,modelframe$fundamental_41,modelframe$fundamental_48,modelframe$fundamental_51,modelframe$fundamental_53,modelframe$fundamental_54,modelframe$technical_2,modelframe$technical_13,modelframe$technical_17,modelframe$technical_19,modelframe$technical_20,modelframe$technical_21,modelframe$technical_30,modelframe$technical_36,modelframe$technical_41)

#all signifcant lambda 20
modelrefined<-data.frame(modelframe$fundamental_10,modelframe$fundamental_25,modelframe$fundamental_41,modelframe$fundamental_43,modelframe$fundamental_48,modelframe$fundamental_51,modelframe$fundamental_53,modelframe$fundamental_54,modelframe$fundamental_60,modelframe$technical_0,modelframe$technical_2,modelframe$technical_6,modelframe$technical_11,modelframe$technical_13,modelframe$technical_17,modelframe$technical_20,modelframe$technical_21,modelframe$technical_22,modelframe$technical_30,modelframe$technical_36,modelframe$technical_40)

# give to other predicted model to check significance
library(neuralnet)


# simple ANN with only a single hidden neuron
n<-names(modelrefined)
modelrefined1<-round(modelrefined,digits=2)
modelrefined1
d<-modelframe$y
f<- as.formula(paste("d~",paste(n[!n %in% "y"],collapse = " + ")))
#scaled data
nn <- neuralnet(f,data=modelrefined1,stepmax = 1e+05,threshold = 0.01, hidden=2,learningrate=2,algorithm = "rprop+")
  #modelframe$y~modelframe$fundamental_10+modelframe$fundamental_18+modelframe$fundamental_25+modelframe$fundamental_33+modelframe$fundamental_43+modelframe$fundamental_51+modelframe$fundamental_52+modelframe$fundamental_62+modelframe$technical_2+modelframe$technical_13+modelframe$technical_19+modelframe$technical_21+modelframe$technical_22+modelframe$technical_36+modelframe$technical_40, data=modelrefined1,stepmax = 1e+05,threshold = 0.01, hidden=2,learningrate=2,algorithm = "sag" )
 


# visualize the network topology
plot(nn)



#this repeatedly improve the result when hidden nodes increased
res<-compute(nn,modelrefined)




# obtain predicted 
predict1 <- res$net.result

# examine the relationship between predicted and actual values
plot(predict1, modelframe$y)
cor(predict1, modelframe$y)



#- hidden node at 2 it gives correlation of 0.11 but if tested with more
#-- nodes the correlation at 5 hidden layers is 0.16, it eventually 
#-- deduced that it require a continues learning process into deep
#-- layers with adusted weight precautionary ,the conclusion gives reader a
#-- standing direction of reinforcement learning with more layers.
#-- Min - Max at hidden layers 2 give 0.11 correlation
#-- z at hidden layers 2 give 0.13 result
#-- 0.11 is at lambda 4
#-- 0.093 is at lambda 20

rm(list=ls())

