R语言如何绘制GBM的校准曲线
时间: 2023-09-08 14:08:15 浏览: 49
可以使用R语言中的gbm库中的gbm.perf函数来绘制GBM的校准曲线。这个函数需要指定训练数据的预测值和真实值,然后可以用ggplot2库中的函数绘制出校准曲线。具体的代码如下:
```R
library(gbm)
library(ggplot2)
# 载入训练数据
data(train)
# 使用gbm算法训练模型
gbm_fit <- gbm(Sale_Price ~ ., data = train, distribution = "gaussian", n.trees = 5000, interaction.depth = 4, cv.folds = 5, verbose = FALSE)
# 获取训练数据的预测值和真实值
train_pred <- predict(gbm_fit, newdata = train)
train_true <- train$Sale_Price
# 计算校准曲线的数据点
bin_width <- 10000
bin_means <- tapply(train_pred, ceiling(train_true / bin_width), mean)
bin_counts <- tapply(train_pred, ceiling(train_true / bin_width), length)
bin_se <- bin_width / (2 * sqrt(bin_counts))
# 绘制校准曲线
ggplot(data.frame(train_true, train_pred), aes(x = train_true, y = train_pred)) +
stat_summary(fun.data = mean_sdl, fun.args = list(mult = 1), geom = "crossbar", width = 5000) +
geom_point(color = "orange") +
geom_errorbar(aes(ymin = train_pred - bin_se, ymax = train_pred + bin_se), width = 5000) +
scale_x_continuous(limits = c(0, 800000)) +
scale_y_continuous(limits = c(0, 800000)) +
xlab("True Sale Price") +
ylab("Predicted Sale Price") +
theme_bw()
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)