Suraj Kumar
Suraj Kumar

Reputation: 35

one of the class levels is not a valid R variable in R programming under caret package

I have tried number of attempts of trying commands like make.names I used the following command to create a model with help of my training data set (trdata)

m1=train(c ~ . , data=trdata, method = "rf", trControl=cp,tuneGrid=pg)

I recieved the following error

Error: At least one of the class levels is not a valid R variable name; This will cause errors when class probabilities are generated because the variables names will be converted to X1, X2, X3 . Please use factor levels that can be used as valid R variable names (see ?make.names for help).

> head(trdata)

       x         y           z         ae         r     c
2  0.8724050 -0.9649373  4.771520  0.5641121  4.440748  1
4  3.6568255  2.2998766 -1.315827  5.0710688  5.408739  1
5  7.5037121  0.3572034  5.717547 -1.7319635  8.536827  1
6 -0.7128616  1.6537590  3.042365  7.2987685 15.933234  1
7 -3.7937849 -0.5391126 11.540201  2.6702112 13.750957  1
8 -1.9557774  1.9698985 -5.556559  1.8364337  6.564570  1

I have total 3 unique values under c variable.

Can someone help me what is wrong with my model making statement m1 ?

Upvotes: 0

Views: 2375

Answers (1)

hydradon
hydradon

Reputation: 1426

The values for the response variable cannot be a number or TRUE, FALSE. You have to convert them to something else.

trdata$c<- factor(trdata$c, 
                  levels = c(1, 2, 3,...), 
                  labels = c("One", "Two", "Three", ...))

Upvotes: 1

Related Questions