user2547973
user2547973

Reputation: 363

CHAID with r-caret error x is not a factor

I am using the CHAID method from caret. I am getting the same error as this post that x is not a factor when all the x's are factors.

I'm using R 3.3.3 and caret_6.0-78

Here is a toy example:

    library(datasets)
    library(caret)
    library(CHAID)

testDat<-data.frame(HairEyeColor, stringsAsFactors=T)[,1:3]

str(testDat)
'data.frame':   32 obs. of  3 variables:
 $ Hair: Factor w/ 4 levels "Black","Brown",..: 1 2 3 4 1 2 3 4 1 2 ...
 $ Eye : Factor w/ 4 levels "Brown","Blue",..: 1 1 1 1 2 2 2 2 3 3 ...
 $ Sex : Factor w/ 2 levels "Male","Female": 1 1 1 1 1 1 1 1 1 1 ...

 control <- trainControl(method="repeatedcv", number=10, repeats=3, 
+                         savePredictions="final", summaryFunction=twoClassSummary, classProbs=TRUE)

fit.chaid <- train(Sex~Hair+Eye, data=testDat, method="chaid", metric="ROC", trControl=control)

Error: is.factor(x) is not TRUE

In addition: There were 50 or more warnings (use warnings() to see the first 50)
Timing stopped at: 0.02 0 0.02 
warnings()
Warning messages:
1: model fit failed for Fold01.Rep1: alpha2=0.05, alpha3=-1, alpha4=0.05 Error : is.factor(x) is not TRUE
.
.

Upvotes: 1

Views: 1433

Answers (1)

Jovan
Jovan

Reputation: 815

I know that this question already old, but here I got the answers for it by experimenting:

For CHAID Modeling, try to use xy modeling rather than formula modeling, like this:

fit.chaid <- train(x=testDat[,c(1,2)], #Hair and Eye Variable
y=testDat[,c(3)],  #Sex Variable
method="chaid", 
metric="ROC", 
trControl=control)

Upvotes: 0

Related Questions