0

我正在优化使用 . 制作的逻辑回归模型glm,优化是使用glmnet. 我想使用 Hosmer Lemeshow 测试的输出来比较这两个模型,我得到了这个输出。对于glm我得到

> hl <- hoslem.test(trainingDatos$Exited, fitted(logit.Mod))
> hl

    Hosmer and Lemeshow goodness of fit (GOF) test

data:  trainingDatos$Exited, fitted(logit.Mod)
X-squared = 2.9161, df = 8, p-value = 0.9395

当我尝试运行套索回归测试时,我得到

> hll <- hoslem.test(trainingDatos$Exited, fitted(lasso.model), g=10)
Error in cut.default(yhat, breaks = qq, include.lowest = TRUE) : 
  'x' must be numeric

我还尝试使用套索回归的系数使其成为数字,我得到

> hll <- hoslem.test(trainingDatos$Exited, fitted(lasso.model$beta), g=10)
Error: $ operator not defined for this S4 class

但是当我把它当作 S4

> hll <- hoslem.test(trainingDatos$Exited, fitted(lasso.model@beta), g=10)
Error in fitted(lasso.model@beta) : 
  trying to get slot "beta" from an object (class "lognet") that is not an S4 object

有什么方法可以对我的套索回归进行测试吗?这是我的套索回归的完整代码,现在无法共享数据库对不起

    #Creation of Training Data Set
input_ones <- Datos[which(Datos$Exited == 1), ] #All 1s
input_zeros <- Datos[which(Datos$Exited == 0), ] #All 0s
set.seed(100) 
#Training 1s
input_ones_training_rows <- sample(1:nrow(input_ones), 0.7*nrow(input_ones)) 
#Training 0s
input_zeros_training_rows <- sample(1:nrow(input_zeros), 0.7*nrow(input_ones)) 
training_ones <- input_ones[input_ones_training_rows, ]
training_zeros <- input_zeros[input_zeros_training_rows, ]
trainingDatos <- rbind(training_ones, training_zeros) 
library(glmnet)
#Conversion of training data into matrix form
x <- model.matrix(Exited ~ CreditScore + Geography + Gender
                  + Age + Tenure + Balance + IsActiveMember
                  + EstimatedSalary, trainingDatos)[,-1]
#Defining numeric response variable
y <- trainingDatos$Exited
sed.seed(100) 
#Grid search to find best lambda
cv.lasso<-cv.glmnet(x, y, alpha = 1, family = "binomial")
#Creation of the model
lasso.model <- glmnet(x, y, alpha = 1, family = "binomial", 
                      lambda = cv.lasso$lambda.1se)
coef(cv.lasso, cv.lasso$lambda.1se)
#Now trying to run the test
library(ResourceSelection)
set.seed(12657)
hll <- hoslem.test(trainingDatos$Exited, fitted(lasso.model), g=10)#numeric value error
hll <- hoslem.test(trainingDatos$Exited, fitted(lasso.model$beta), g=10)#$ not defined for S4
hll <- hoslem.test(trainingDatos$Exited, fitted(lasso.model@beta), g=10)#saying that beta is nos S4
4

0 回答 0