R中决策树的party包不支持字符数据类型?

use*_*121 5 r decision-tree

如果我的数据框中的一列是数据类型字符,我会收到以下错误。

\n\n
> library("party")\n> r2 <- ctree(Sepal.Length ~ .,data=df)\nError in trafo(data = data, numeric_trafo = numeric_trafo, factor_trafo = factor_trafo,  : \n  data class character is not supported\n> plot(r2)    \n> sapply(df,class)\nSepal.Length  Sepal.Width Petal.Length  Petal.Width      Species \n    "factor"     "factor"     "factor"  "character"     "factor" \n
Run Code Online (Sandbox Code Playgroud)\n\n

有时,我也会收到此错误

\n\n
 Error in match.arg(type) : \n  \'arg\' should be one of \xe2\x80\x9cresponse\xe2\x80\x9d, \xe2\x80\x9cnode\xe2\x80\x9d, \xe2\x80\x9cprob\xe2\x80\x9d > \n> sapply(df,class)\n          AGE        GENDER          STAY      GRADE          XYNS        CHARGE \n    "integer"     "integer"      "factor"     "integer"     "integer"     "integer" \n
Run Code Online (Sandbox Code Playgroud)\n\n

我该如何解决这些问题?

\n

Ach*_*eis 1

响应变量和所有解释变量的规模对于 CTree 算法的两个方面很重要:(1)在每个节点中进行关联测试以确定应使用哪个变量进行分裂。(2)给定解释变量中最佳分割点的选择。

关联测试始终捕获响应与每个解释变量之间的“相关性”或“缺乏独立性”。相关性度量的类型取决于所涉及变量的规模(请参阅交叉验证上的这篇文章: https: //stats.stackexchange.com/questions/144143)。变量可以是数值(或整数)、无序分类(即因子)、有序分类或审查(Surv 对象)。为数据框中的给定变量选择适当的变量类型对于从树中获得有意义的结果至关重要。

类似地,给定变量中可能的二元分割的确定很大程度上取决于规模。并且character不是一个有评估相关性或分裂的标准方法的量表。