我试图按照库恩和约翰逊的《应用预测模型》一书中的图6.15所示,使用Caret包重新创建岭回归系数路径。提供了目标输出。
通过以下代码获取数据:
require(tidyverse)
require(caret)
require(AppliedPredictiveModeling)
require(elasticnet)
data(solubility)
set.seed(100)
indx <- createFolds(solTrainY, returnTrain = TRUE)
ctrl <- trainControl(method = "cv", index = indx)
ridgeGrid <- data.frame(lambda = seq(0, .1, length = 15))
set.seed(100)
cvresult <- train(x = solTrainXtrans,
y = solTrainY,
method = "ridge",
tuneGrid = ridgeGrid,
trControl = ctrl,
preProc = c("center", "scale"))
编辑:以下是我的尝试;我意识到我不想要最终模型,但是predict.enet
函数否则会给我一个错误:
coeffs <- predict.enet(cvresult$finalModel, type = "coefficients", mode = "fraction")
as.data.frame(unclass(coeffs$coefficients)) %>%
mutate(Fraction = coeffs$fraction) %>%
gather(Variable, Coefficient, -Fraction) %>%
mutate(Col = ifelse(Variable %in% c("NumNonHAtoms", "NumNonHBonds", "NumMultBonds"), Variable, "Other")) %>%
ggplot(aes(Fraction, Coefficient, group=Variable, colour = Col)) +
geom_line()
注释掉的代码(slow)或ggplot都不按书显示。