R Caret包中的逻辑回归调整参数网格?
Logistic Regression Tuning Parameter Grid in R Caret Package?
我正在尝试使用 caret package
在 R 中拟合逻辑回归模型。我做了以下事情:
model <- train(dec_var ~., data=vars, method="glm", family="binomial",
trControl = ctrl, tuneGrid=expand.grid(C=c(0.001, 0.01, 0.1, 1,10,100, 1000)))
但是,我不确定该模型的调整参数应该是什么,而且我很难找到它。我假设它是 C,因为 C 是 sklearn
中使用的参数。目前,我收到以下错误 -
Error: The tuning parameter grid should have columns parameter
你对如何解决这个问题有什么建议吗?
根据 Max Kuhn 的网络书 - search for method = 'glm'
here,caret
中没有调整参数 glm
。
我们可以通过测试几个基本的 train
调用轻松验证情况是否如此。首先,让我们从一个方法 (rpart
) 开始,根据网络书,它确实有一个调整参数 (cp
)。
library(caret)
data(GermanCredit)
# Check tuning parameter via `modelLookup` (matches up with the web book)
modelLookup('rpart')
# model parameter label forReg forClass probModel
#1 rpart cp Complexity Parameter TRUE TRUE TRUE
# Observe that the `cp` parameter is tuned
set.seed(1)
model_rpart <- train(Class ~., data=GermanCredit, method='rpart')
model_rpart
#CART
#1000 samples
# 61 predictor
# 2 classes: 'Bad', 'Good'
#No pre-processing
#Resampling: Bootstrapped (25 reps)
#Summary of sample sizes: 1000, 1000, 1000, 1000, 1000, 1000, ...
#Resampling results across tuning parameters:
# cp Accuracy Kappa
# 0.01555556 0.7091276 0.2398993
# 0.03000000 0.7025574 0.1950021
# 0.04444444 0.6991700 0.1316720
#Accuracy was used to select the optimal model using the largest value.
#The final value used for the model was cp = 0.01555556.
我们看到 cp
参数已调整。现在让我们试试 glm
.
# Check tuning parameter via `modelLookup` (shows a parameter called 'parameter')
modelLookup('glm')
# model parameter label forReg forClass probModel
#1 glm parameter parameter TRUE TRUE TRUE
# Try out the train function to see if 'parameter' gets tuned
set.seed(1)
model_glm <- train(Class ~., data=GermanCredit, method='glm')
model_glm
#Generalized Linear Model
#1000 samples
# 61 predictor
# 2 classes: 'Bad', 'Good'
#No pre-processing
#Resampling: Bootstrapped (25 reps)
#Summary of sample sizes: 1000, 1000, 1000, 1000, 1000, 1000, ...
#Resampling results:
# Accuracy Kappa
# 0.7386384 0.3478527
在上述 glm
的情况下,没有执行任何参数调整。根据我的经验,名为 parameter
的 parameter
似乎只是一个占位符,而不是真正的调整参数。正如下面的代码所示,即使我们试图强制它调整 parameter
,它基本上也只会调整一个值。
set.seed(1)
model_glm2 <- train(Class ~., data=GermanCredit, method='glm',
tuneGrid=expand.grid(parameter=c(0.001, 0.01, 0.1, 1,10,100, 1000)))
model_glm2
#Generalized Linear Model
#1000 samples
# 61 predictor
# 2 classes: 'Bad', 'Good'
#No pre-processing
#Resampling: Bootstrapped (25 reps)
#Summary of sample sizes: 1000, 1000, 1000, 1000, 1000, 1000, ...
#Resampling results across tuning parameters:
# Accuracy Kappa parameter
# 0.7386384 0.3478527 0.001
# 0.7386384 0.3478527 0.001
# 0.7386384 0.3478527 0.001
# 0.7386384 0.3478527 0.001
# 0.7386384 0.3478527 0.001
# 0.7386384 0.3478527 0.001
# 0.7386384 0.3478527 0.001
#Accuracy was used to select the optimal model using the largest value.
#The final value used for the model was parameter = 0.001.
我正在尝试使用 caret package
在 R 中拟合逻辑回归模型。我做了以下事情:
model <- train(dec_var ~., data=vars, method="glm", family="binomial",
trControl = ctrl, tuneGrid=expand.grid(C=c(0.001, 0.01, 0.1, 1,10,100, 1000)))
但是,我不确定该模型的调整参数应该是什么,而且我很难找到它。我假设它是 C,因为 C 是 sklearn
中使用的参数。目前,我收到以下错误 -
Error: The tuning parameter grid should have columns parameter
你对如何解决这个问题有什么建议吗?
根据 Max Kuhn 的网络书 - search for method = 'glm'
here,caret
中没有调整参数 glm
。
我们可以通过测试几个基本的 train
调用轻松验证情况是否如此。首先,让我们从一个方法 (rpart
) 开始,根据网络书,它确实有一个调整参数 (cp
)。
library(caret)
data(GermanCredit)
# Check tuning parameter via `modelLookup` (matches up with the web book)
modelLookup('rpart')
# model parameter label forReg forClass probModel
#1 rpart cp Complexity Parameter TRUE TRUE TRUE
# Observe that the `cp` parameter is tuned
set.seed(1)
model_rpart <- train(Class ~., data=GermanCredit, method='rpart')
model_rpart
#CART
#1000 samples
# 61 predictor
# 2 classes: 'Bad', 'Good'
#No pre-processing
#Resampling: Bootstrapped (25 reps)
#Summary of sample sizes: 1000, 1000, 1000, 1000, 1000, 1000, ...
#Resampling results across tuning parameters:
# cp Accuracy Kappa
# 0.01555556 0.7091276 0.2398993
# 0.03000000 0.7025574 0.1950021
# 0.04444444 0.6991700 0.1316720
#Accuracy was used to select the optimal model using the largest value.
#The final value used for the model was cp = 0.01555556.
我们看到 cp
参数已调整。现在让我们试试 glm
.
# Check tuning parameter via `modelLookup` (shows a parameter called 'parameter')
modelLookup('glm')
# model parameter label forReg forClass probModel
#1 glm parameter parameter TRUE TRUE TRUE
# Try out the train function to see if 'parameter' gets tuned
set.seed(1)
model_glm <- train(Class ~., data=GermanCredit, method='glm')
model_glm
#Generalized Linear Model
#1000 samples
# 61 predictor
# 2 classes: 'Bad', 'Good'
#No pre-processing
#Resampling: Bootstrapped (25 reps)
#Summary of sample sizes: 1000, 1000, 1000, 1000, 1000, 1000, ...
#Resampling results:
# Accuracy Kappa
# 0.7386384 0.3478527
在上述 glm
的情况下,没有执行任何参数调整。根据我的经验,名为 parameter
的 parameter
似乎只是一个占位符,而不是真正的调整参数。正如下面的代码所示,即使我们试图强制它调整 parameter
,它基本上也只会调整一个值。
set.seed(1)
model_glm2 <- train(Class ~., data=GermanCredit, method='glm',
tuneGrid=expand.grid(parameter=c(0.001, 0.01, 0.1, 1,10,100, 1000)))
model_glm2
#Generalized Linear Model
#1000 samples
# 61 predictor
# 2 classes: 'Bad', 'Good'
#No pre-processing
#Resampling: Bootstrapped (25 reps)
#Summary of sample sizes: 1000, 1000, 1000, 1000, 1000, 1000, ...
#Resampling results across tuning parameters:
# Accuracy Kappa parameter
# 0.7386384 0.3478527 0.001
# 0.7386384 0.3478527 0.001
# 0.7386384 0.3478527 0.001
# 0.7386384 0.3478527 0.001
# 0.7386384 0.3478527 0.001
# 0.7386384 0.3478527 0.001
# 0.7386384 0.3478527 0.001
#Accuracy was used to select the optimal model using the largest value.
#The final value used for the model was parameter = 0.001.