# Independent Component Analysis Pre-processing

# load libraries
library(mlbench)
library(caret)
library(fastICA)
# load the dataset
data(PimaIndiansDiabetes)
# summarize dataset
summary(PimaIndiansDiabetes[,1:8])
# calculate the pre-process parameters from the dataset
preprocessParams <- preProcess(PimaIndiansDiabetes[,1:8], method=c("center", "scale", "ica"), n.comp=5)
# summarize transform parameters
print(preprocessParams)
# transform the dataset using the parameters
transformed <- predict(preprocessParams, PimaIndiansDiabetes[,1:8])
# summarize the transformed dataset
summary(transformed)




# load the dataset
data(iris)
datos <- iris
x <- datos[,1:4]
y <- datos[,5]
# summarize dataset
summary(x)
# calculate the pre-process parameters from the dataset
preprocessParams <- preProcess(x, method=c("center", "scale", "ica"), n.comp=4)
# summarize transform parameters
print(preprocessParams)
# transform the dataset using the parameters
x_ica <- predict(preprocessParams, x)
# summarize the transformed dataset
summary(x_ica)



scales <- list(x=list(relation="free"), y=list(relation="free"))
# ejes adaptados a cada variable
featurePlot(x=x, y=y, plot="density", scales=scales)
featurePlot(x=x_ica, y=y, plot="density", scales=scales)