Running `ctree` using the` party` package, the column as a factor, not a character

I called Convert the column format data.frame from character to coefficient and Convert multiple columns of data.table to factors in R and Convert column classes to data.table

Unfortunately, this did not solve my problem. I am working with a bodyfat dataset and my dataframe is called> bf. I added an agegrp column to classify people of different ages as young, middle, or old, this way:

bf$agegrp<-ifelse(bf$age<=40, "young", ifelse(bf$age>40 & bf$age<55,"middle", "old"))

This is 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 it bf$agegrphas a character class, I ran,

> bf$agegrp<-as.factor(bf$agegrp)

the agegrp column is now coerced to factor.

> Class (bf$agegrp)gives [1] "Factor".

ctree, . - , ?

+4
1

:

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)

+3

Source: https://habr.com/ru/post/1529602/


All Articles