错误:运行hoslem.test时未为此S4类定义$运算符

时间:2020-05-14 17:03:27

标签: r logistic-regression lasso-regression goodness-of-fit

我正在使用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

0 个答案:

没有答案