【问题标题】:How to properly plot ICE in R?如何在 R 中正确绘制 ICE?
【发布时间】:2019-06-27 22:18:28
【问题描述】:

我想绘制个人条件期望 (ICE),我有以下代码段:

library(caret)
library(gridExtra)
library(grid)
library(ggridges)
library(ggthemes)
library(iml)
library(partykit)
library(rpart)
library(tidyverse)

theme_set(theme_minimal())
set.seed(88)

kfolds <- 3

load_dataset <- function() {
  dataset <- read_csv("https://gist.githubusercontent.com/dmpe/bfe07a29c7fc1e3a70d0522956d8e4a9/raw/7ea71f7432302bb78e58348fede926142ade6992/pima-indians-diabetes.csv", col_names=FALSE)  %>%
    mutate(X9=as.factor(ifelse(X9== 1, "diabetes", "nondiabetes")))
  X = dataset[, 1:8]
  Y = dataset$X9
  return(list(dataset, X, Y))
}

compute_rf_model <- function(dataset) {
  index <- createDataPartition(dataset$X9,
                               p=0.8,
                               list=FALSE,
                               time=1)

  dataset_train <- dataset[index,]
  dataset_test <- dataset[-index,]

  fit_control <- trainControl(method="repeatedcv",
                              number=kfolds,
                              repeats=1,
                              classProbs=TRUE,
                              savePredictions=TRUE,
                              verboseIter=FALSE,
                              allowParallel=FALSE,
                              summaryFunction=defaultSummary)

  rf_model <- train(X9~.,
                    data=dataset_train,
                    method="rf",
                    preProcess=c("center","scale"),
                    trControl=fit_control,
                    metric="Accuracy",
                    verbose=FALSE)
  return(list(rf_model, dataset_train, dataset_test))
}


main <- function() {
  data <- load_dataset()
  dataset <- data[[1]]
  X <- data[[2]]
  Y <- data[[3]]

  rf_model_data <- compute_rf_model(dataset)
  rf_model <- rf_model_data[[1]]
  dataset_train <- rf_model_data[[2]]
  dataset_test <- rf_model_data[[3]]

  X <- dataset_train    %>%
    select(-X9) %>%
    as.data.frame()

  predictor <- Predictor$new(rf_model, data=X, y=dataset_train$X9)

  ice <- FeatureEffect$new(predictor, feature="X2", center.at=min(X$X2), method="pdp+ice")
  ice_plot_glucose <- ice$plot() + 
    scale_color_discrete(guide="none") +
    scale_y_continuous("Predicted Diabetes")
  ice <- FeatureEffect$new(predictor, feature="X4", center.at=min(X$X4), method="pdp+ice")
  ice_plot_insulin <- ice$plot() + 
    scale_color_discrete(guide="none") +
    scale_y_continuous("Predicted Diabetes")
  grid.arrange(ice_plot_glucose, ice_plot_insulin, ncol=1)

}

if (!interactive()) {
  main()
} else if (identical(environment(), globalenv())) {
  quit(status = main())
}

我最后收到的情节是这样的:

而且这个情节在网上的一些 ICE 情节中看起来并不那么漂亮,比如下面的这个:

任何想法为什么会发生这种情况?我相信我拥有的数据与上面帖子中显示的数据相似,至少在价值方面。

【问题讨论】:

  • 请考虑减少问题中重现错误/不良行为并非绝对必要的元素。您尝试过什么来解决这个问题?
  • @PavoDive 我试图在这里提供最小的可重现问题。不知道为什么剧情会这样。
  • 我得到了第一个情节,但随后我的 RStudio 会话中止了。 (我正在运行 R 3.5.2,所以这可能取决于当前版本?)看起来行为是由这部分代码触发的:else if (identical(environment(), globalenv())) { quit(status = main())
  • @42- 你说的第一个情节是什么?有奇怪线条的那个?这就是我在运行代码时在 RStudio 中收到的内容,我使用的是 R 3.6.0。但结果应该看起来像最后一张图片,有很多黑线。
  • 谢谢。是的,我的意思是带有奇怪线条的那个。我误解了与控制台的交互。没有崩溃,但 quit() 函数的行为类似于崩溃对话框。我想知道这是否是一个公开可用的数据集(除了你的要点)并记录在案?我认为这将是一组非常有用的代码,它的答案简短而甜美,如果他们有解释,结果会更有趣。也许从这里:kaggle.com/uciml/pima-indians-diabetes-database

标签: r machine-learning ice


【解决方案1】:

问题在于预测器给出的是类标签而不是类概率。

变化

predictor &lt;- Predictor$new(rf_model, data=X, y=dataset_train$X9)

predictor &lt;- Predictor$new(rf_model, data=X, y=dataset_train$X9, type = "prob")

应该修复你的情节。

these fixed PD plots

【讨论】:

    猜你喜欢
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    • 2019-10-01
    相关资源
    最近更新 更多