2017-04-13 25 views
0

我使用R中的rpart庫準備迴歸樹!我有4個獨立變量和三個獨立預測變量。所有瓦爾的規模(即米尺)rpart:confusionMatrix錯誤(迴歸樹)

mydata="mydata.csv" 
library(caret) 
library("rpart") 
class1=read.csv(mydata,sep=";",dec=",") 
# rpart 
fit <- rpart(y1+y2+y3+y6~ ., method="anova",data=class1) 
set.seed(123) 
index <- sample(1:nrow(class1),round(0.75*nrow(class1))) 
train <- class1[index,] 
str(train) 
test <- class1[-index,] 
str(test) 
fit <- rpart(y1+y2+y3+y6~ ., method="anova",data=train) 
predict(fit) 
predicted.t=predict(fit) 

現在我想用confusionMatrix(),但我得到了錯誤

> confusionMatrix(predicted.t,test) 
Error in sort.list(y) : 'x' must be atomic for 'sort.list' 
Have you called 'sort' on a list? 

>

dput(class) 
structure(list(x1 = c(215L, 170L, 340L, 320L, 320L, 320L, 300L, 
305L, 345L, 300L, 340L, 220L, 320L, 220L, 300L, 300L, 300L, 215L, 
275L, 255L, 275L, 320L, 345L, 250L, 220L, 250L, 300L, 220L, 215L, 
300L, 255L, 345L, 350L, 305L, 320L, 215L, 215L, 300L, 220L, 255L, 
305L, 320L, 345L, 250L, 305L, 255L, 305L, 305L, 255L, 275L, 320L, 
340L, 250L, 300L, 305L, 320L, 250L, 300L, 215L, 250L, 220L, 220L, 
320L, 300L, 350L, 350L, 350L, 305L, 170L, 220L, 350L, 320L, 215L, 
305L, 255L, 170L, 340L, 300L, 300L, 255L, 300L, 320L, 275L, 275L, 

如果

View(class) 
x1 x2 x3 y1 y2 y3 y6 
215 15,4 94 90 7 3 54,886501 
170 16 85 90 6 4 54,886501 
340 12,2 72 70 15 15 54,886501 
320 15,1 78 80 12 8 54,886501 
320 9,7 77 80 5 15 54,886501 
320 11,1 70 90 1 9 54,886501 
300 14,6 85 80 14 6 54,886501 
305 8,6 74 90 6 4 54,886501 
345 15 85 90 5 5 54,886501 
300 13,7 85 90 7 3 54,886501 
340 14,3 82 80 18 2 54,886501 
220 8,6 77 80 15 5 54,886501 
320 13,2 73 80 12 8 54,886501 
220 8,4 85 90 5 5 54,886501 
300 16 85 90 7 3 56,08118233 
300 8,4 72 90 7 3 54,886501 
300 13,7 77 90 7 3 54,886501 
215 15,1 77 90 6 4 56,08118233 
275 12,2 94 70 15 15 56,08118233 
255 16 85 80 12 8 54,886501 
275 11,1 94 80 5 15 54,886501 

如何解決這個錯誤?

+0

請注意'class'是R中的一個基本函數,您的數據集將覆蓋函數定義。同時,在使用隨機採樣時,請記住以'set.seed(x)'作爲您最喜歡的數字,以確保重複性結果。 – OdeToMyFiddle

+0

嗨,我編輯了我的帖子。這裏的mydata格式爲.csv。 [鏈接](https://www.sendspace.com/file/ws44ig) –

回答

0

這裏有回答

u = union(round(predicted.t),test$y6) 
t = table(factor(round(predicted.t), u), factor(test$y6, u)) 
confusionMatrix(t) 

所以矩陣作品。