'Equivalent of validation plot in ggplot

I am trying to write the code to plot validationplot {pls}'s equivalent using ggplot.

I could make this using base R, but could'nt figure out how to plot this in ggplot. Can someone please help me?

library(pls)
library(ggplot2)
library(ISLR)
data("College")
clg=College

cat("Partitioning 50/50")
set.seed(702)
trainindex=sample(1:nrow(clg),size=ceiling(nrow(clg)/2))

ctrain=clg[trainindex,]
ctest=clg[-trainindex,]

dim(ctrain)
dim(ctest)

pcre = pcr(Apps ~ .,
           data = ctrain,
           scale = T,
           validation = "CV")
#Validation plot
vplote = validationplot(pcre, val.type = "MSEP", xaxt = "none")
axis(1, at = 0:17, labels = 0:17)
abline(
  h = c(1:30) / 3 * 1e6,
  v = c(0:17),
  col = "gray",
  lty = 3
)


Solution 1:[1]

You can simply find the MSEP using the MSEP function on your pcre variable and from there you can handle everything with ggplot (see the code and image below).

library(pls)
library(ggplot2)
library(ISLR)
data("College")
clg=College

cat("Partitioning 50/50")
set.seed(702)
trainindex=sample(1:nrow(clg),size=ceiling(nrow(clg)/2))

ctrain=clg[trainindex,]
ctest=clg[-trainindex,]

dim(ctrain)
dim(ctest)

pcre = pcr(Apps ~ .,
           data = ctrain,
           scale = T,
           validation = "CV")

#Validation ggplot

ggplot(data.frame(Components=0:17,MSEP=MSEP(pcre)$val[1, 1,]),
       aes(x=Components,y=MSEP))+geom_line()+geom_point()

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1