我试图使用Ridge创建一个logistic回归模型,这是代码:
glmnet(X_Train, Y_Train, family='binomial', alpha=0, type.measure='auc')
这是我得到的错误信息:
Error in storage.mode(xd) <- "double" : 'list' object cannot be coerced to type 'double'
我尝试将所有变量转换为“数字”,但仍然无法工作。
我将发布这两个数据集的代码,以便您可以复制它:
图书馆:
library(dplyr)
library(fastDummies)
library(missForest)
library(glmnet)
数据:
url <- 'https://archive.ics.uci.edu/ml/machine-learning-databases/credit-screening/crx.data'
crx <- read.csv(url, sep = ",", header = F)
去掉空值:
crx[crx == "?"] <- NA
crx <- type.convert(crx, as.is=FALSE)
crx.i <- missForest(as.data.frame(crx))
crx <- crx.i$ximp
数据转换:
crx <- crx %>%
rename(Gender = V1,
Age = V2,
Debt = V3,
Married = V4,
BankCustomer = V5,
EducationLevel = V6,
Ethnicity = V7,
YearsEmployed = V8,
PriorDefault = V9,
Employed = V10,
CreditScore = V11,
DriversLicense = V12,
Citizen = V13,
ZipCode = V14,
Income = V15,
ApprovalStatus = V16)
crx = subset(crx, select = -ZipCode)
crx <- crx %>%
mutate(ApprovalStatus = recode(ApprovalStatus,
"+" = "1",
"-" = "0"))
# Normalizing numeric variables:
crxAge <- scale(crx#qcStackCode#Age)
crxDebt <- scale(crx#qcStackCode#Debt)
crxYearsEmployed <- scale(crx#qcStackCode#YearsEmployed)
crxCreditScore <- scale(crx#qcStackCode#CreditScore)
crxIncome <- scale(crx#qcStackCode#Income)
crxGender <- NULL
crx#qcStackCode#DriversLicense <- NULL
创建虚拟变量:
df <- dummy_cols(crx, remove_selected_columns = T)
dfApprovalStatus_0 <- NULL
df#qcStackCode#ApprovalStatus_1 <- NULL
dfMarried_l <- NULL
df#qcStackCode#BankCustomer_gg <- NULL
dfApprovalStatus <- crx#qcStackCode#ApprovalStatus
建立培训数据集和测试数据集:
X <- df %>% dplyr::select(-ApprovalStatus)
Y <- df$ApprovalStatus
X_Train <- X[0:590, ]
Y_Train <- Y[0:590]
X_Test <- X[591:nrow(X), ]
Y_Test <- Y[591:length(Y)]
试着使用glmnet:
glmnet(X_Train, Y_Train, family='binomial', alpha=0, type.measure='auc')
我做了一些研究,发现一篇文章说你必须把所有的东西都转换成数值类,所以我试着把所有的东西都转换成数值变量,如下所示:
Y_Train <- as.numeric(Y_Train)
X_Train <- as.data.frame(apply(X_Train, 2, as.numeric))
但还是不起作用。我到底做错了什么?
发布于 2021-02-01 20:53:52
Glmnet需要一个矩阵作为X
和y
的输入。因此,您需要在所有模型输入上定义as.matrix()
。
关于进一步的例子,也见Glmnet小体由特雷弗哈斯蒂和钱俊阳。
https://datascience.stackexchange.com/questions/88757
复制相似问题