library(neuralnet)
library(openxlsx)

data <- read.csv("Documents/Evidencia1.csv" )
str(data)
## 'data.frame':    12510 obs. of  6 variables:
##  $ ent       : int  0 0 0 0 0 1 1 1 1 1 ...
##  $ mun       : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ est       : int  1 2 3 4 5 1 2 3 4 5 ...
##  $ pea       : num  58.743 0.165 58.471 59.015 0.281 ...
##  $ ocupados  : num  96.546 0.079 96.416 96.677 0.082 ...
##  $ informales: num  55.237 0.271 54.791 55.682 0.49 ...
#Normalizamos los datos
data$pea <- (data$pea - min(data$pea)) / (max(data$pea) - min(data$pea))
hist(data$pea)

data$ocupados <- (data$ocupados - min(data$ocupados)) / (max(data$ocupados) - min(data$ocupados))
hist(data$ocupados)

data$informales <- (data$informales - min(data$informales)) / (max(data$informales) - min(data$informales))
hist(data$informales)

#Train

set.seed(37)
inp <- sample(2, nrow(data), replace = TRUE, prob = c(0.7, 0.3))
training_data <- data[inp==1, ]
test_data <- data[inp==2, ]
#Crear la red
set.seed(300) #hidden es la cantidad de neuronas en la capa escondida, stepmax es que tantos pasos hará


red <- neuralnet(pea~ ocupados + informales,
               data = training_data,
               hidden = 3,
               linear.output = FALSE,
               lifesign = 'full',
               rep = 2,
               algorithm = "rprop+",
               stepmax = 800000)
## hidden: 3    thresh: 0.01    rep: 1/2    steps:     789  error: 8.35893  time: 0.63 secs
## hidden: 3    thresh: 0.01    rep: 2/2    steps:    1000  min thresh: 0.0559863606164079
##                                                    2000  min thresh: 0.0146741424908057
##                                                    3000  min thresh: 0.0146741424908057
##                                                    3458  error: 8.11757  time: 2.41 secs
?neuralnet
plot(red, rep = 1)