2

我像这样从 R 中的模型和数据框进行预测

> prediction = predict (m, df)

这将返回一个因子变量,我可以将其转换为行向量数据框。然后,我可以将该行向量数据框与正确结果的行向量合并,然后手动计算两个数据向量的分歧点。

有没有更简单的方法来做到这一点?

4

1 回答 1

2

有很多方法可以解决这个问题,而您没有提供太多信息。以下是一些获取此类信息的快速示例:

> library(randomForest)
> data(imports85)
> model <- randomForest(bodyStyle~curbWeight+cityMpg ,data=imports85)
> 
> #contingency table
> (tab <- table(predict(model,imports85),imports85$bodyStyle))

              convertible hardtop hatchback sedan wagon
  convertible           6       0         0     0     0
  hardtop               0       7         0     0     0
  hatchback             0       1        63     4     3
  sedan                 0       0         7    92     1
  wagon                 0       0         0     0    21
> #error per class
> diag(prop.table(tab,1))
convertible     hardtop   hatchback       sedan       wagon 
  1.0000000   1.0000000   0.8873239   0.9200000   1.0000000 
> #overall error
> mean(predict(model,imports85) == imports85$bodyStyle)
[1] 0.9219512

虽然我通常会建议使用类似的东西caret()可以在训练时执行交叉验证并为您提供这些结果。

于 2013-10-29T01:40:06.227 回答