2017-09-10 243 views
0

我试图在R中实现一个简单的随机森林算法,以了解R和随机森林如何工作,并测试测试集中的准确性。R - 随机森林 - 在测试数据上应用混淆矩阵的错误

我的样本数据(561个总行的五行)是:

bulbasaur[1:5,] 
    Appt_date count no_of_reps PerReCount 
1 2016-01-01  2   1 2.000000 
2 2016-01-04 174   58 3.000000 
3 2016-01-05 206   59 3.491525 
4 2016-01-06 203   61 3.327869 
5 2016-01-07 236   64 3.687500 

我写的代码是:

install.packages("caret") 
library(caret) 

leaf <- bulbasaur 
ctrl = trainControl(method="repeatedcv", number=100, repeats=50, selectionFunction = "oneSE") 
in_train = createDataPartition(leaf$PerReCount, p=.75, list=FALSE) 

#random forest 
trf = train(PerReCount ~ ., data=leaf, method="rf", metric="RMSE",trControl=ctrl, subset = in_train) 


#boosting 
tgbm = train(PerReCount ~ ., data=leaf, method="gbm", metric="RMSE", 
      trControl=ctrl, subset = in_train, verbose=FALSE) 

resampls = resamples(list(RF = trf, GBM = tgbm)) 
difValues = diff(resampls) 
summary(difValues) 



######Using it on test matrix 
test = leaf[-in_train,] 
test$pred.leaf.rf = predict(trf, test, "raw") 
confusionMatrix(test$pred.leaf.rf, test$PerReCount) 

不过,我得到以下错误:

Error in confusionMatrix.default(test$pred.leaf.rf, test$PerReCount) : 
    the data cannot have more levels than the reference 

我尝试了一些更改,如采取leaf$PerReCount <- as.factors(leaf$PerReCount),并添加type = "class",但所得出的准确性很糟糕,我不想从回归到分类。我怎样才能解决它,而不用转换因素,或以任何其他方式解决问题,或者在不使用混淆矩阵的情况下获得准确的计数。谢谢

+1

混淆矩阵是指**分类符**,当您的目标变量是数字时没有意义。现在,'PerReCount'变量显然是一个连续的数字变量。您的问题不在代码中,而是在了解您的数据。 –

回答

0

@Damiano提出的问题是正确的,回归模型不会给出混淆矩阵,因为它不是或不是。我解决的问题是使用RMSE:

piko.chu = predict(trf, test) 
RMSE.forest <- sqrt(mean((piko.chu-test$PerReCount)^2))