我已经提到了 convert data.frame column format from character to factor 和 Converting multiple data.table columns to factors in R 和 Convert column classes in data.table
不幸的是它没有解决我的问题。我正在使用 bodyfat 数据集,我的数据框称为 > bf。我添加了一个名为 agegrp 的列来将不同年龄的人分类为年轻、中年或年老:
bf$agegrp<-ifelse(bf$age<=40, "young", ifelse(bf$age>40 & bf$age<55,"middle", "old"))
这是ctree分析:
> 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)
我收到以下错误:
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
由于
bf$agegrp
是我运行的“字符”类,> bf$agegrp<-as.factor(bf$agegrp)
现在,agegrp 列被强制为因子。
> Class (bf$agegrp)
给出 [1] "Factor"
。我尝试再次运行
ctree
,但它抛出了同样的错误。有谁知道问题的根本原因是什么? 最佳答案
这对我有用:
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)
关于使用 `ctree` 包运行 `party`,列作为因子而不是字符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22120498/