# This R environment comes with all of CRAN preinstalled, as well as many other helpful packages
# The environment is defined by the kaggle/rstats docker image: https://github.com/kaggle/docker-rstats
# For example, here's several helpful packages to load in 

library(ggplot2) # Data visualization
library(readr) # CSV file I/O, e.g. the read_csv function
library(caret)
library(mlbench)
library(scales)
library(nnet)
# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory

system("ls ../input")

full<-read.csv("../input/train.csv")
test<-read.csv("../input/test.csv")
full_cont<-full[,c("id","cont1","cont2","cont3","cont4","cont5","cont6","cont7","cont8","cont9","cont10","cont11","cont12","cont13","cont14","loss")]
full_cat<- within(full, rm("cont1","cont2","cont3","cont4","cont5","cont6","cont7","cont8","cont9","cont10","cont11","cont12","cont13","cont14"))


roc_imp <- filterVarImp(x = full[, -ncol(full)], y = full$loss)
head(roc_imp, n = 130)

# using cat2,cat7,cat10,cat11,cat12,cat57,cat72,cat80,cat79,cat81,cat87,cat89,cat101,cont2,cont3,cont7,cont11,cont12 variables as they are most important

feautres<- c("id","cat2","cat7","cat10","cat11","cat12","cat57","cat72","cat80","cat79","cat81","cat87","cat101","cont2","cont3","cont7","cont11","cont12","loss")
feautres_1<- c("id","cat2","cat7","cat10","cat11","cat12","cat57","cat72","cat80","cat79","cat81","cat87","cat101","cont2","cont3","cont7","cont11","cont12")

full_feautres<-full[,feautres]
#test<-test[,feautres_1]

lm.fit <- lm(loss ~ ., data = full_feautres)
lm.predict <- predict(lm.fit)
lm.predict.test<- predict(lm.fit, newdata = test, interval = "confidence")

test_1<-test[,c("id")]
lm_submission<-cbind(test_1,lm.predict.test)


write.csv(lm_submission,"lm_submission.csv",row.names = F)
mean((lm.predict - full_feautres$loss)^ 2)
plot(full_feautres$loss, lm.predict, main = "Linear Regression prediction vs actual", xlab = "Actual")

#require(nnet)

#nnet.fit <- nnet(loss ~ ., data=full_feautres, size=2)

#nnet.predict <- predict(nnet.fit)

#mean((nnet.predict - full_feautres$loss)^ 2)

#plot(full_feautres$loss, nnet.predict, main = "Neural Network prediction vs actual", xlab = "Actual")


# Any results you write to the current directory are saved as output.