在pdp :: partial图上添加概率边界

时间:2020-10-28 17:48:14

标签: r ggplot2 pdp

我有一个二进制分类问题的预测概率图。我有两个问题:

  1. 如何为p = 0.5绘制单个概率中断?使用contour = TRUE自变量会添加几行。
  2. 是否可以添加另一个预测变量来绘制3D图?

这就是我现在拥有的:

enter image description here

代表:


require(caret)
train.ctrl <- trainControl(method = "repeatedcv",
                          number = 10,
                          repeats = 5,
                          summaryFunction = twoClassSummary,
                          classProbs = TRUE,
                          )

set.seed(1337)
lr.fit <- train(Class ~ ., data = train.small,
                 trControl = train.ctrl,
                 method = "glm",
                 family = "binomial",
                 metric = "ROC",
                 preProcess = c("center", "scale")
                 )

require(pdp)
lr.pd <- partial(lr.fit, pred.var = c("x1", "x2"), prob = TRUE, plot = TRUE,
                chull = FALSE, 
                grid.resolution = 20,
                progress = "text",
                plot.engine = "ggplot"     
                #, levelplot = TRUE
                #, contour = TRUE
                )
lr.pd

# dataset used: 
> dput(train.small)

structure(list(x1 = c(-2.27966818800131, -0.338448010439568, 
-0.205849502281457, 0.845604758116324, 0.237243699016471, 1.24653636015188, 
0.141150345247212, -1.94634378874626, 0.844265652370458, -0.364520848772231, 
-1.23724847469945, 1.38848801851204, -1.36106463689431, -0.563757474548103, 
-1.06688494837985, 1.95249141033933, -0.19013864138727, -0.582286822918036, 
-0.121851924837777, 0.708635458099694, 0.232606893541271, -0.973242157622908, 
-0.839124499580887, -1.60414910952904, -2.48941991263215, -0.307956293709429, 
1.47833943928667, 0.878621754086077, -0.98322797566842, -1.07796826066294
), x2 = c(-2.52279563738851, -1.75938416652951, 1.39754416931398, 
-0.00212755901980971, 1.08662786676201, -0.515199741525909, 0.467733663414545, 
-0.466317884258174, -0.836744523943926, -0.718136687911924, -1.73906216686269, 
1.59934935188848, -1.27639037325246, -0.32993091478973, -2.05445383777137, 
-0.0189083922948782, -0.818464313993987, -1.15168959538223, 1.99668352214088, 
0.671670710096937, 0.680397233698906, 0.267937956913229, -1.78107607016152, 
-1.53728406964112, -1.05660014431803, -0.316921861836086, 2.65603302304451, 
0.885179019517534, -0.220384928450498, -1.94445363537753), Class = c("No", 
"Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", "Yes", "No", 
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "No", "No", "No", "Yes", 
"No", "Yes", "No", "No", "No", "No", "No", "No", "Yes")), row.names = c(24L, 
2L, 10L, 9L, 26L, 22L, 12L, 11L, 21L, 17L, 7L, 19L, 27L, 8L, 
25L, 16L, 5L, 18L, 29L, 6L, 14L, 15L, 13L, 20L, 1L, 30L, 4L, 
23L, 28L, 3L), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

以下是您对问题号的回答。 1.
您可以使用以下简单代码在p = 0.5处绘制轮廓线:

lr.pd + geom_contour(color = "white", breaks=0.5, size=1)

enter image description here