R语言决策树calibration plot代码
时间: 2023-04-09 13:02:57 浏览: 209
以下是R语言决策树calibration plot的代码:
```R
library(rpart.plot)
library(ggplot2)
# 构建决策树模型
model <- rpart(Species ~ ., data = iris)
# 预测结果
pred <- predict(model, type = "prob")
# 将预测结果与实际结果合并
result <- data.frame(pred, iris$Species)
# 计算每个预测概率区间的实际结果比例
calib <- result %>%
group_by(Species) %>%
mutate(bin = cut(pred[, Species], breaks = seq(0, 1, 0.1), include.lowest = TRUE)) %>%
group_by(bin, .add = TRUE) %>%
summarise(n = n()) %>%
mutate(prop = n / sum(n))
# 绘制calibration plot
ggplot(calib, aes(x = as.numeric(bin), y = prop, fill = Species)) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_manual(values = c("#00AFBB", "#E7B800", "#FC4E07")) +
labs(x = "Predicted probability", y = "Actual proportion", fill = "Species") +
theme_minimal()
```
希望对你有帮助!
阅读全文