具有分类因变量的随机森林

时间:2013-11-19 02:15:43

标签: r random-forest

我正在尝试使用少量依赖变量和少量自变量来运行randomForest包。任何人都可以告诉我是否可以使用分类变量(其值为“AAA”,“HCC1”,“HCC2”等)作为randomForest包的因变量? 请注意我不是在谈论cforest包。 对于上面的问题,我听说我需要将分类变量转换为因子。但是当我检查Class时,变量显示为factor。当我运行以下语法时:

fit <- randomForest(Bkt_DerivedVariable1+Bkt_DerivedVariable4+hcc_trans_2013_col1 ~ Bkt_TopSecondaryDiagnosis1_2+Bkt_TopPrimaryDiagnosis3,data=testdata1)

错误是:

Error in na.fail.default(list(`Bkt_DerivedVariable1 + Bkt_DerivedVariable4 + Bkt_TopSecondaryDiagnosis1_1` = c(NA,  : 
  missing values in object
In addition: Warning message:
In Ops.factor(Bkt_DerivedVariable1 + Bkt_DerivedVariable4, Bkt_TopSecondaryDiagnosis1_1) :
  + not meaningful for factors

我确信这是具有上述字符串值的hcc_trans_2013_col1变量。

有人可以告诉我该怎么办?

1 个答案:

答案 0 :(得分:0)

错误是说有missing values in object 因此,在应用randomForest()之前,请尝试以下操作:

library(randomForest)
testdata1 <- na.roughfix(testdata1)  # for imputing the missing values in the data

然后:

fit <- randomForest(Bkt_DerivedVariable1+Bkt_DerivedVariable4+hcc_trans_2013_col1 ~ Bkt_TopSecondaryDiagnosis1_2+Bkt_TopPrimaryDiagnosis3,data=testdata1)

如果问题是缺少值,那么这应该有帮助。

相关问题