问题
I am trying to fit a logistic regression model in R using the caret package
. I have done the following:
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)))
However, I am unsure what the tuning parameter should be for this model and I am having a difficult time finding it. I assumed it is C because C is the parameter used in sklearn
. Currently, I am getting the following error -
Error: The tuning parameter grid should have columns parameter
Do you have any suggestions on how to fix this?
回答1:
Per Max Kuhn's web-book - search for method = 'glm' here ,there is no tuning parameter glm
within caret
.
We can easily verify this is the case by testing out a few basic train
calls. First off, let's start with a method (rpart
) that does have a tuning parameter (cp
) per the web book.
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.
We see that the cp
parameter was tuned. Now let's try 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
In this case with glm
above there was no parameter tuning performed. From my experience, it appears the parameter
named parameter
is just a placeholder and not a real tuning parameter. As demonstrated in the code that follows, even if we try to force it to tune parameter
it basically only does a single value.
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.
来源:https://stackoverflow.com/questions/47822694/logistic-regression-tuning-parameter-grid-in-r-caret-package