我在 R 中工作,并使用多种方法探索使用插入符号进行变量选择和加权。在这里,我正在探索使用前向逐步和最小角度回归 (LARS),对每个参数使用调整参数。在下面的代码中,我任意选择了一个因变量 (y) 和一个预测变量子集 (x's),并使用 70% 的数据子集通过训练算法运行它们。为此,我正在应用重复的 10 倍交叉验证。我正在努力寻找一个命令来识别从训练函数派生的最终模型参数(例如,截距、β 权重)。当我调用 object$finalModel 时,我不容易看到它。有没有办法使用列出的方法(前向逐步回归和 LARS)在 R 中恢复这些?感觉这个应该是存在的......
谢谢!
library (caret)
library(AppliedPredictiveModeling)
data(abalone)
str(abalone)
set.seed(18)
inTrain <- sample(1:(round(nrow(abalone)*.7)),replace=FALSE)
train_df <- abalone [inTrain,]
test_df <- abalone [-inTrain,]
#predicting Diameter using several of the dataset's variables#
train_df_x <- train_df [,4:8]
test_df_x <- test_df [,4:8]
y_train <- train_df [,3]
y_test <- test_df [,3]
set.seed(18)
fold.ids <- createMultiFolds(y_train,k=10,times=3)
fitControl <- trainControl(method = "repeatedcv",
number = 10,
repeats = 3,
returnResamp = "final",
index = fold.ids,
summaryFunction = defaultSummary,
selectionFunction = "oneSE")
### Forward regression ###
library(leaps)
forwardLmGrid <- expand.grid (.nvmax=seq(2,5))
set.seed(18)
F_OLS_fit <- train(train_df_x, y_train,"leapForward",trControl = fitControl,metric="RMSE", tuneGrid=forwardLmGrid)
### LARS ###
larGrid <- expand.grid(.fraction=seq(.01,.99,length=50))
library(lars)
Lar_fit <- train(train_df_x, y_train,"lars",trControl = fitControl,metric="RMSE", tuneGrid=larGrid)