3

rpart()およびpredict()コマンドを使用して予測モデルを生成した後、混同行列を実行するには、R でどのコマンドを使用すればよいですか?

# Grow tree
library(rpart)
fit <- rpart(activity ~ ., method="class", data=train.data)

printcp(fit) # display the results
plotcp(fit) # visualize cross-validation results
summary(fit) # detailed summary of splits

# Prune the tree (in my case is exactly the same as the initial model)
pfit <- prune(fit, cp=0.10) # from cptable
pfit <- prune(fit,cp=fit$cptable[which.min(fit$cptable[,"xerror"]),"CP"])

# Predict using the test dataset
pred1 <- predict(fit, test.data, type="class")

# Show re-substitution error
table(train.data$activity, predict(fit, type="class"))

# Accuracy rate
sum(test.data$activity==pred1)/length(pred1)

True Positive、False Negative、False Positive、および True Negative を明確に要約したいと思います。感度、特異度、陽性的中率、陰性的中率を同じマトリックスに持つことも素晴らしいでしょう。

用語間の関係 ソース: http://en.wikipedia.org/wiki/Sensitivity_and_specificity

4

1 に答える 1

1

次のように、フィットと元のデータ フレームでメソッドを使用predict()します。

pred = predict(train.fit, newdata, type = "vector")
newdata$pred = as.vector(pred)
newdata$prediction = activities[newdata$pred]

tab = table (newdata$prediction, newdata$activity)
print(tab)

上記の例では、rpart モデルがアクティビティ (因子変数) を予測します。 pred因子のレベルに対応する値を持つ数値です。 activities = sort(unique(data$activity))デフォルトの因子マッピングに対応します。

于 2013-12-08T21:09:15.273 に答える