我正在使用来自插入符号的CHAID方法。我得到与post相同的错误,当所有x都是因子时,x不是一个因素。
我使用的是R 3.3.3和caret_6.0-78
这是一个玩具示例:
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 . .
答案 0 :(得分:0)
我知道这个问题已经很老了,但是在这里我通过实验得到了答案:
对于CHAID建模,请尝试使用xy建模而不是公式建模,如下所示:
fit.chaid <- train(x=testDat[,c(1,2)], #Hair and Eye Variable
y=testDat[,c(3)], #Sex Variable
method="chaid",
metric="ROC",
trControl=control)