Sam
Sam

Reputation: 97

I'm getting a warning message in R I don't understand

** When I train my SVM models I get this warning message:

Warning message: In .local(x, ...): Variable(s) `' constant. Cannot scale data.

Could someone shine some light on this warning? Is this something I should be concerned with? I was thinking the warning maybe because of my dummy variable coding I did. If so, does anyone know a way around this warning message? Thanks for y'all's time! **

# importing libraries
library(tidyverse)
library(caret)

# importing the data and changing data types ----
loan <- read_csv("loan_data.csv", col_types = cols(
    credit.policy    = col_factor(),
    inq.last.6mths = col_factor(),
    delinq.2yrs    = col_factor(),
    pub.rec        = col_factor(),
    not.fully.paid = col_factor(),
    purpose        = col_factor()
))

# dummy coding purpose column for SVM models
loan_dum <- loan %>% 
    fastDummies::dummy_cols(select_columns = "purpose",
                            remove_first_dummy  = TRUE,
                            remove_selected_columns = TRUE)
loan_dum %>% View()

# Checking classes of new dummy variables 
loan_dum %>% glimpse()

# Changing the data type of new dummy variables from int to factors
loan_dum <- loan_dum %>% 
    mutate(
        purpose_credit_card      = as.factor(purpose_credit_card),
        purpose_all_other        = as.factor(purpose_all_other),
        purpose_home_improvement = as.factor(purpose_home_improvement),
        purpose_small_business   = as.factor(purpose_small_business),
        purpose_major_purchase   = as.factor(purpose_major_purchase),
        purpose_educational      = as.factor(purpose_educational)
    )
loan_dum %>% glimpse()



# standardizing continuous values
loan_dum_stand <- loan_dum %>% 
    mutate(
        int.rate          = as.vector(scale(int.rate,center = TRUE,scale = TRUE)),
        installment       = as.vector(scale(installment,center = TRUE,scale = TRUE)),
        log.annual.inc    = as.vector(scale(log.annual.inc,center = TRUE,scale = TRUE)),
        dti               = as.vector(scale(dti,center = TRUE,scale = TRUE)),
        fico              = as.vector(scale(fico,center = TRUE,scale = TRUE)),
        days.with.cr.line = as.vector(scale(days.with.cr.line,center = TRUE,scale = TRUE)),
        revol.bal         = as.vector(scale(revol.bal,center = TRUE,scale = TRUE)),
        revol.util        = as.vector(scale(revol.util,center = TRUE,scale = TRUE))
    )
loan_dum_stand %>% view()


# splitting the data into train test ----
set.seed(123)
trainIndexDum <- createDataPartition(loan_dum_stand$not.fully.paid,p = 0.70,list = FALSE,times = 1)
loan_dum_train <- loan_dum_stand[trainIndexDum,]
loan_dum_test  <- loan_dum_stand[-trainIndexDum,]

loan_dum_train %>% glimpse()



#  SVM model with default settings ----
####################################################################################
# Runing model building in parallel
cl <- makePSOCKcluster(10)
registerDoParallel(cl)


# fitting svm model with radial kernel no tuning 
set.seed(123)
svm_model_radial <- train(
    form = not.fully.paid ~.,
    data = loan_dum_train,
    method = "svmRadial"
)
svm_model_radial
plot(svm_model_radial)

Upvotes: 2

Views: 2985

Answers (1)

ashwin agrawal
ashwin agrawal

Reputation: 1611

This error is generally due the fact that you might have a column in your training data which has constant data (same value in each row) and therefore, If the variable is constant, it cannot be scaled to unit variance (and zero mean).

Upvotes: 3

Related Questions