Reputation: 3594
I have referred convert data.frame column format from character to factor and Converting multiple data.table columns to factors in R and Convert column classes in data.table
Unfortunately it did not solve my problem. I am working with the bodyfat dataset and my dataframe is called > bf. I added a column called agegrp to categorize persons of different ages as young, middle or old thus :
bf$agegrp<-ifelse(bf$age<=40, "young", ifelse(bf$age>40 & bf$age<55,"middle", "old"))
This is the ctree analysis:
> set.seed(1234)
> modelsample<-sample(2, nrow(bf), replace=TRUE, prob=c(0.7, 0.3))
> traindata<-bf[modelsample==1, ]
> testdata<-bf[modelsample==2, ]
> predictor<-agegrp~DEXfat+waistcirc+hipcirc+kneebreadth` and ran, `bf_ctree<-ctree(predictor, data=traindata)
> bf_ctree<-ctree(predictor, data=traindata)
I got the following error:
Error in trafo(data = data, numeric_trafo = numeric_trafo, factor_trafo = factor_trafo, :
data class character is not supported
In addition: Warning message:
In storage.mode(RET@predict_trafo) <- "double" : NAs introduced by coercion
Since bf$agegrp
is of class "character" I ran,
> bf$agegrp<-as.factor(bf$agegrp)
the agegrp column is now coerced to factor.
> Class (bf$agegrp)
gives [1] "Factor"
.
I tried running the ctree
again, but it throws the same error. Does anyone know what the root-cause of the problem is?
Upvotes: 4
Views: 8857
Reputation: 59335
This works for me:
library(mboot)
library(party)
bf <- bodyfat
bf$agegrp <- cut(bf$age,c(0,40,55,100),labels=c("young","middle","old"))
predictor <- agegrp~DEXfat+waistcirc+hipcirc+kneebreadth
set.seed(1234)
modelsample <-sample(2, nrow(bf), replace=TRUE, prob=c(0.7, 0.3))
traindata <-bf[modelsample==1, ]
testdata <-bf[modelsample==2, ]
bf_ctree <-ctree(predictor, data=traindata)
plot(bf_ctree)
Upvotes: 3