# Principal Component Analysis Pre-processing

# load the libraries
library(mlbench)
# 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", "pca"))
# summarize transform parameters
print(preprocessParams)
# transform the dataset using the parameters
x_pca <- predict(preprocessParams, x)
# summarize the transformed dataset
summary(x_pca)



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_pca, y=y, plot="density", scales=scales)





# calculate components
components <- prcomp(x, center=TRUE, scale=TRUE)
# display components
x_pca <- components$x

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_pca, y=y, plot="density", scales=scales)