我正在尝试编写要绘制的代码
validationplot {pls}
等效于ggplot。
我可以使用基数R来实现,但无法弄清楚如何在ggplot中进行绘制。有人可以帮我吗?
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
)
答案 0 :(得分:2)
您可以使用 pcre 变量上的 MSEP 函数简单地找到 MSEP,然后您就可以使用 ggpplot 处理所有内容(请参阅下面的代码和图片)。
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()