从R'检索系数;s使用前向回归和/或LAR在插入符号中训练函数

从R'检索系数;s使用前向回归和/或LAR在插入符号中训练函数,r,linear-regression,r-caret,coefficients,lars,R,Linear Regression,R Caret,Coefficients,Lars,我在R工作,探索使用插入符号进行变量选择和加权的方法。在这里,我将使用正向逐步回归和最小角度回归(LAR)进行探索,并为每种方法使用调整参数。在下面的代码中,我任意选择了一个因变量(y)和一个预测因子子集(x),并使用70%的数据子集通过训练算法运行它们。为此,我应用了重复的10倍交叉验证。 我正在努力寻找一个命令,以识别从训练函数导出的最终模型参数(例如,截距、β权重)。当我调用object$finalModel时,我并不容易看到它。是否有办法使用列出的方法(前向逐步回归和LARS)恢复这些数

我在R工作,探索使用插入符号进行变量选择和加权的方法。在这里,我将使用正向逐步回归和最小角度回归(LAR)进行探索,并为每种方法使用调整参数。在下面的代码中,我任意选择了一个因变量(y)和一个预测因子子集(x),并使用70%的数据子集通过训练算法运行它们。为此,我应用了重复的10倍交叉验证。 我正在努力寻找一个命令,以识别从训练函数导出的最终模型参数(例如,截距、β权重)。当我调用object$finalModel时,我并不容易看到它。是否有办法使用列出的方法(前向逐步回归和LARS)恢复这些数据?我觉得这必须存在

谢谢

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)
库(插入符号)
库(AppliedPredictiveModeling)
数据(鲍鱼)
str(鲍鱼)
种子(18)

inTrain我将通过一个示例向您展示我是如何做到这一点的:

library(data.table)
n <- 1000
x1 <- runif(n,min=-10,max=10)
x2 <- runif(n,min=-10,max=10)
x3 <- runif(n,min=-10,max=10)
x4 <- runif(n,min=-10,max=10)
x5 <- runif(n,min=-10,max=10)
y1 <- 30 + x1 + 4*x2 + x3
synthetic <- data.table(x1=x1,x2=x2,x3=x3,x4=x4,x5=x5,y=y1)
library(caret)
library(lars)
ctrl <- trainControl(method = "cv", savePred=T, number=3)
fractionGrid <- expand.grid (fraction=seq(0,1,(1/(ncol(widedt)-1))))
cvresult <- train(y~.,
                  data=synthetic,
                  method = "lars",
                  trControl = ctrl,
                  metric="RMSE",
                  tuneGrid=fractionGrid,
                  use.Gram=FALSE)
coeffs <- predict.lars(cvresult$finalModel,type="coefficients")
models <- as.data.table(coeffs$coefficients)
winnermodelscoeffs <- models[which(coeffs$fraction==cvresult$bestTune$fraction)]
库(data.table)
N