set.seed(123)
library(randomForest)
## randomForest 4.6-2
## Type rfNews() to see new features/changes/bug fixes.
suppressPackageStartupMessages(library(party))
## Warning message: package 'party' was built under R version 2.15.1
suppressPackageStartupMessages(library(ggplot2)) # diamondsデータ用
diamonds$cut_clarity <- interaction(diamonds$clarity, diamonds$cut)
smp <- diamonds[sample(nrow(diamonds), 500), ]
smp$cut_clarity <- droplevels(smp$cut_clarity)
nlevels(smp$cut_clarity)
## [1] 33
res1 <- randomForest(data = smp, color ~ carat + cut_clarity)
## Error: Can not handle categorical predictors with more than 32 categories.
res2 <- predict(cforest(data = smp, color ~ carat + cut_clarity,
control = cforest_classical()))
table(smp$color, res2)
## res2
## D E F G H I J
## D 0 50 0 19 2 0 0
## E 0 72 0 16 2 0 1
## F 0 47 0 25 10 0 1
## G 0 40 0 54 5 0 3
## H 0 18 0 33 20 1 0
## I 0 12 1 14 6 3 6
## J 0 11 0 8 6 2 12
32カテゴリ以上あるような変数をそのまま使うんじゃなくて、自分の目的に沿って少しはまとめろよっていうBreimanのメッセージかと思ってたら、元のFortranのコードは8年前の時点で修正されていてその後randomForestパッケージの方で対応していないだけだった。
https://stat.ethz.ch/pipermail/r-packages/2004/000526.html
http://cran.r-project.org/web/packages/randomForest/NEWS