library(ISLR) library(pls) library(plotly) # Base de datos Hitters. Hitters = Hitters Hitters = na.omit(Hitters) # Componentes principales de los predictores x = model.matrix(Salary~., Hitters)[, -1] y = Hitters$Salary pc = prcomp(x, retx = TRUE, center = TRUE, scale. = TRUE)$x pc = as.data.frame(pc) pc$y = y plot_ly(pc, x = ~PC1, y = ~PC2, z = ~PC3, marker = list(color = ~y, colorscale = c('#FFE1A1', '#683531'), showscale = TRUE)) %>% add_markers() plot(pc$PC1, log(y)) # RegresiĆ³n de componenes principales pcr.fit <- pcr(Salary ~ ., data = Hitters, scale = TRUE, validation = "CV") summary(pcr.fit) validationplot(pcr.fit, val.type = "MSEP")