#---------------------------------------------------------------#
library(lhs)
##
set.seed(11)
n <- 300 # num observaciones
k <- 4 # num features
X_ <- optimumLHS(n, k) # dist máx LHS uniforme
X <- matrix(0,nrow = n, ncol = k ) # para recibir al transformador
X[,1] <- qnorm(X_[,1]) # Transformada gaussiana
X[,2] <- qexp(X_[,2]) # exponencial
X[,3] <- qpois(X_[,3],lambda = 1) # poisson
X[,4] <- X_[,4] # sin cambios (uniforme)

require(ggplot2)
require(GGally)
df <- data.frame(X)
names(df) <- c("X1_Norm", "X2_Exp", "X3_Pois_1", "X4_unif")
p1 <- ggpairs(df[,c(1,2,3,4)], # elección de variables
              alfa = 1/2) # transparencia

p1<- p1 + theme(panel.background = # ¡es más bonito!
                  element_rect(color = "rosa"))

p1<- p1 + theme(text=element_text(size=14)) # tamaño caracteres
p1 # pantalla#---------------------------------------------------------------#
require(caret)

set.seed(1)                   
y <- 10*df[,1] +                      # una función desconocida
     7* df[,2] * rnorm(nrow(df))+ 
     3*sin(df[,3])+
     2*sin(df[,4])*runif(nrow(df))
y <-  ifelse(y < 7,0,1)               # fabricación de clases 
sum(y)/length(y)                      # verif % clases
          
df_<- data.frame(df,y)                # más simple para:
p          <- createDataPartition(y=df_$y,
                                  p= 50/100,
                                  list=FALSE)
training   <- df_[p,]  
test       <- df_[-p,]
#---------------------------------------------------------------#
library(gbm)
set.seed(1) 

m <- gbm(formula = y ~ .,
     distribution = "bernoulli",
     data = training,
     n.trees = 1000,
     interaction.depth = 4,
     shrinkage = 0.01,
     cv.folds=10)

summary(m)
top = gbm.perf(m,method="cv")       # curva óptima iter
top                                 # num iteración óptima
#---------------------------------------------------------------#
