1
$\begingroup$

I'm trying to create a logistic regression model using Ridge, this is the code:

glmnet(X_Train, Y_Train, family='binomial', alpha=0, type.measure='auc')

And this is the error message I'm getting:

Error in storage.mode(xd) <- "double" : 'list' object cannot be coerced to type 'double'

I tried converting all the variables into "numeric" but still doesn't work.

I'm going to post the code for those two datasets so you can reproduce it:

libraries:

library(dplyr)
library(fastDummies)
library(missForest)
library(glmnet)

Data:

url <- 'https://archive.ics.uci.edu/ml/machine-learning-databases/credit-screening/crx.data'
crx <- read.csv(url, sep = ",", header = F)

Getting rid of null-values:

crx[crx == "?"] <- NA
crx <- type.convert(crx, as.is=FALSE)
crx.i <- missForest(as.data.frame(crx))
crx <- crx.i$ximp

Data transformations:

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:
crx$Age <- scale(crx$Age)
crx$Debt <- scale(crx$Debt)
crx$YearsEmployed <- scale(crx$YearsEmployed)
crx$CreditScore <- scale(crx$CreditScore)
crx$Income <- scale(crx$Income)

crx$Gender <- NULL
crx$DriversLicense <- NULL

Creation of dummy variables:

df <- dummy_cols(crx, remove_selected_columns = T)

df$ApprovalStatus_0 <- NULL
df$ApprovalStatus_1 <- NULL
df$Married_l <- NULL
df$BankCustomer_gg <- NULL

df$ApprovalStatus <- crx$ApprovalStatus

Creation of Training datasets and Test datasets:

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)]

And trying to use the glmnet:

glmnet(X_Train, Y_Train, family='binomial', alpha=0, type.measure='auc')

I did some research and I found an article saying that you have to convert everything into numeric class, so I tried converting everything into numeric variables like this:

Y_Train <- as.numeric(Y_Train)
X_Train <- as.data.frame(apply(X_Train, 2, as.numeric))

And still doesn't work. What am I doing wrong exactly?

$\endgroup$
2
  • $\begingroup$ What is the data type here? glmnet needs a matrix as input. Try as.matrix() for both X and y $\endgroup$
    – Peter
    Jan 31 '21 at 21:39
  • $\begingroup$ oh yes, I tried it and it worked $\endgroup$
    – JMarcos87
    Feb 1 '21 at 20:19
1
$\begingroup$

Glmnet requires a matrix as input for both, $X$ and $y$. So you need to define as.matrix() on all model inputs.

For further examples also see the Glmnet Vignette by Trevor Hastie and Junyang Qian.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.