library(pls)
data(gasoline, package = "pls")
# PVS: Select 10 variables using all PCs in voting
pvs_result <- PVS(gasoline$NIR, nvar = 10)
# Compare with PCA using pcr() (octane is unused in PCA)
pca_result <- pcr(octane ~ NIR, ncomp = 10, data = gasoline, scale = FALSE)
# Plot cumulative variance explained
plot(cumsum(pvs_result$ssEX), type = "b", col = "blue",
xlab = "Number of Variables/Components",
ylab = "Cumulative % Variance Explained",
main = "PVS vs PCA", ylim = c(0, 100))
pca_var <- 100 * cumsum(pca_result$Xvar) / pca_result$Xtotvar
lines(seq_along(pca_var), pca_var, type = "b", col = "red")
legend("bottomright", legend = c("PVS (10 variables)", "PCA (10 components)"),
col = c("blue", "red"), lty = 1, pch = 1)
Run the code above in your browser using DataLab