用于逻辑回归的交叉验证和套索正则化错误

问题描述 投票:0回答:1

我想用套索正则化创建一个5折CV Logistic回归模型,但出现以下错误消息:Something is wrong; all the RMSE metric values are missing:

我通过设置alpha=1开始进行套索正则化的逻辑回归。这可行。我从this example开始扩展。

# Load data set
data("mtcars")

# Prepare data set 
x   <- model.matrix(~.-1, data= mtcars[,-1])
mpg <- ifelse( mtcars$mpg < mean(mtcars$mpg), 0, 1)
y   <- factor(mpg, labels = c('notEfficient', 'efficient'))

#find minimum coefficient
mod_cv <- cv.glmnet(x=x, y=y, family='binomial', alpha=1)

#logistic regression with lasso regularization
logistic_model <- glmnet(x, y, alpha=1, family = "binomial",
                         lambda = mod_cv$lambda.min)

我了解到glmnet函数已经执行了10倍cv。但我想使用5折简历。因此,当我使用n_folds将修改添加到cv.glmnet时,我找不到最小系数,也无法在修改trControl时仅制作模型。

#find minimum coefficient by adding 5-fold cv
mod_cv <- cv.glmnet(x=x, y=y, family='binomial', alpha=1, n_folds=5)


#Error in glmnet(x, y, weights = weights, offset = offset, #lambda = lambda,  : 
#  unused argument (n_folds = 5)

#logistic regression with 5-fold cv
    # define training control
    train_control <- trainControl(method = "cv", number = 5)

# train the model with 5-fold cv
model <- train(x, y, trControl = train_control, method = "glm", family="binomial", alpha=1)

#Something is wrong; all the Accuracy metric values are missing:
#    Accuracy       Kappa    
#Min.   : NA   Min.   : NA  
# 1st Qu.: NA   1st Qu.: NA  
# Median : NA   Median : NA  
# Mean   :NaN   Mean   :NaN  
# 3rd Qu.: NA   3rd Qu.: NA  
# Max.   : NA   Max.   : NA  
 # NA's   :1     NA's   :1  

为什么当我添加5倍简历时会出现错误?

r logistic-regression cross-validation glmnet lasso-regression
1个回答
0
投票

您的代码中有2个问题:1)n_folds中的cv.glmnet参数实际上称为nfolds,2)train函数不使用alpha参数。如果您解决了这些,您的代码将起作用:

# Load data set
data("mtcars")
library(glmnet)
library(caret)

# Prepare data set 
x   <- model.matrix(~.-1, data= mtcars[,-1])
mpg <- ifelse( mtcars$mpg < mean(mtcars$mpg), 0, 1)
y   <- factor(mpg, labels = c('notEfficient', 'efficient'))

#find minimum coefficient
mod_cv <- cv.glmnet(x=x, y=y, family='binomial', alpha=1)

#logistic regression with lasso regularization
logistic_model <- glmnet(x, y, alpha=1, family = "binomial",
                         lambda = mod_cv$lambda.min)



#find minimum coefficient by adding 5-fold cv
mod_cv <- cv.glmnet(x=x, y=y, family='binomial', alpha=1, nfolds=5)


#logistic regression with 5-fold cv
# define training control
train_control <- trainControl(method = "cv", number = 5)

# train the model with 5-fold cv
model <- train(x, y, trControl = train_control, method = "glm", family="binomial")
model$results
#>  parameter  Accuracy     Kappa AccuracySD   KappaSD
#>1      none 0.8742857 0.7362213 0.07450517 0.1644257


© www.soinside.com 2019 - 2024. All rights reserved.