R Caret包中的逻辑回归调参网格是什么?

7

我正在使用caret包在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中使用的参数。目前,我遇到了以下错误 -

错误:调优参数网格应该有参数列

你有什么建议如何解决这个问题吗?

尝试使用 modelLookup("glm"),请参见此处:https://stackoverflow.com/questions/43970831/the-tuning-parameter-in-glm-vs-rf/44010331#44010331 - Marco Sandri
开始时指定tuneLength并观察caret决定变化的参数,而不是直接开始制定网格,这也是一个不错的想法。 - dmi3kno
glm 方法没有调整参数 https://topepo.github.io/caret/train-models-by-tag.html,里面的那个是虚拟调整参数,它不起作用。 - jmuhlenkamp
1个回答

12
根据Max Kuhn的网络书-在此处搜索method = 'glm',在caret中没有调整参数glm

enter image description here

我们可以通过测试一些基本的train调用来轻松验证这一点。首先,让我们从一个有调整参数(cp)的方法(rpart)开始,这是根据网络书籍的要求。
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案例中,没有进行参数调整。根据我的经验,似乎名为parameterparameter只是一个占位符,而不是真正的调整参数。如下面的代码所示,即使我们试图强制调整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.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接