Rでrpartパッケージのtree()を使い、分類木を描こうとしています。
下記のコードで、class、class2には全く同じ値、"NO", "SL", "DH"が入っているはずなのですが、
classの分類では発生しないエラーが、class2の分類では発生します。
どこがおかしいのでしょうか?よろしくお願いいたします。
(背景:当初はclass2で、"NO"を"normal" 、"SL""DH"を"unnormal"と書き換え、二項分類を試みました。ここで"non-numeric argument to binary operator”エラーが発生したため、まったく同じ値ならどうなるのか?と思い、上記のようなclass2で試したところやはり同じエラーが出ました)
R
1library(dplyr) 2library(tree) 3rm(list = ls()) 4vercl =read.table("vertebralcolumn.dat",header = TRUE) 5 6vercol =mutate(vercl, class2 = if_else(condition = class=="NO", 7 true = "NO", false = 8 (if_else(condition = class=="SL", true = "SL", false ="DH")))) 9 10t1 <- tree(class ~ pelvInc+pelvTilt+lumbLord+SacrSl+pelvRad+degrS, 11 data = vercol ) 12t2 <- tree(class2 ~ pelvInc+pelvTilt+lumbLord+SacrSl+pelvRad+degrS, 13 data = vercol)
t1 は動きます。
R
1> summary(t1) 2 3Classification tree: 4tree(formula = class ~ pelvInc + pelvTilt + lumbLord + SacrSl + 5 pelvRad + degrS, data = vercol) 6Variables actually used in tree construction: 7[1] "degrS" "SacrSl" "pelvRad" "pelvTilt" "pelvInc" 8Number of terminal nodes: 13 9Residual mean deviance: 0.4035 = 119.8 / 297 10Misclassification error rate: 0.08387 = 26 / 310
t2ではエラーが発生します。
R
1> summary(t2) 2Error in y - frame$yval[object$where] : 3 non-numeric argument to binary operator
vercolデータは、下記の通り。310 obs。class, class2にはDH,SL,NOの三項があります。
head(vercol)
pelvInc pelvTilt lumbLord SacrSl pelvRad degrS class class2
1 63.03 22.55 39.61 40.48 98.67 -0.25 DH DH
2 39.06 10.06 25.02 29.00 114.41 4.56 DH DH
3 68.83 22.22 50.09 46.61 105.99 -3.53 DH DH
4 69.30 24.65 44.31 44.64 101.87 11.21 DH DH
5 49.71 9.65 28.32 40.06 108.17 7.92 DH DH
6 40.25 13.92 25.12 26.33 130.33 2.23 DH DH
*関連質問:下記も同じ原因によるエラーと思われます。
分類木分析で”NAs introduced by coercion” エラー発生
あなたの回答
tips
プレビュー