在R中,为什么ksvm中的概率和响应不一致?

12

我正在使用R中kernlab包中的ksvm来预测概率,使用predict.ksvm中的type="probabilities"选项。然而,我发现有时候使用predict(model,observation,type="r")并不能得到由predict(model,observation,type="p")给出的具有最高概率的类。

例如:

> predict(model,observation,type="r")
[1] A
Levels: A B
> predict(model,observation,type="p")
        A    B
[1,] 0.21 0.79

这是正确的行为还是一个错误?如果这是正确的行为,那么我如何从概率中估计最可能的类别?
尝试可重现的示例:
library(kernlab)
set.seed(1000)
# Generate fake data
n <- 1000
x <- rnorm(n)
p <- 1 / (1 + exp(-10*x))
y <- factor(rbinom(n, 1, p))
dat <- data.frame(x, y)
tmp <- split(dat, dat$y)
# Create unequal sizes in the groups (helps illustrate the problem)
newdat <- rbind(tmp[[1]][1:100,], tmp[[2]][1:10,])
# Fit the model using radial kernal (default)
out <- ksvm(y ~ x, data = newdat, prob.model = T)
# Create some testing points near the boundary

testdat <- data.frame(x = seq(.09, .12, .01))
# Get predictions using both methods
responsepreds <- predict(out, newdata = testdat, type = "r")
probpreds <- predict(out, testdat, type = "p")

results <- data.frame(x = testdat, 
                      response = responsepreds, 
                      P.x.0 = probpreds[,1], 
                      P.x.1 = probpreds[,2])

结果输出:

> results
     x response     P.x.0     P.x.1
1 0.09        0 0.7199018 0.2800982
2 0.10        0 0.6988079 0.3011921
3 0.11        1 0.6824685 0.3175315
4 0.12        1 0.6717304 0.3282696

如果您能提供一些样本数据和一个已拟合的模型,那将对我们非常有帮助。这将使我们更容易解释这种行为。总之,这很可能是正确的行为。 - nograpes
除非您提供可重现此行为的代码,否则可能没有人会同意这是一个错误。否则,在我们中的贝叶斯主义者会简单地说他们的"你犯了一个错误"先验概率很可能是正确的。 - joran
@joran 那个行为是可重现的。我会添加一些可重现的代码。roelandvanbeek - 如果我添加的代码没有说明你谈论的内容,请随意删除它。 - Dason
谢谢您的帮助,@Dason,这正是我想要的行为。 - roelandvanbeek
@joran 不同的 class.weights 会改变界限,但问题仍然存在。 - roelandvanbeek
显示剩余3条评论
1个回答

14

如果你看一下决策矩阵和投票结果,它们似乎更符合回应:

> predict(out, newdata = testdat, type = "response")
[1] 0 0 1 1
Levels: 0 1
> predict(out, newdata = testdat, type = "decision")
            [,1]
[1,] -0.07077917
[2,] -0.01762016
[3,]  0.02210974
[4,]  0.04762563
> predict(out, newdata = testdat, type = "votes")
     [,1] [,2] [,3] [,4]
[1,]    1    1    0    0
[2,]    0    0    1    1
> predict(out, newdata = testdat, type = "prob")
             0         1
[1,] 0.7198132 0.2801868
[2,] 0.6987129 0.3012871
[3,] 0.6823679 0.3176321
[4,] 0.6716249 0.3283751
kernlab 帮助页面 (?predict.ksvm) 中提到了一篇论文Probability estimates for Multi-class Classification by Pairwise Coupling by T.F. Wu, C.J. Lin, and R.C. Weng.
在第7.3节中,指出决策和概率可能不同:
“…我们解释了基于概率和决策值的方法的结果可能有如此明显的差异。对于某些问题,δDV选择的参数与其他五个规则非常不同。在波形中,对于某些参数,所有基于概率的方法给出的交叉验证精度要比δDV高得多,例如,验证集的决策值分别为[0.73,0.97]和[0.93,1.02](它们属于两个类),因此将所有验证集数据分类为一个类别时,误差很大。相反,基于概率的方法通过S型函数拟合决策值,这可以在决策值约为0.95时更好地区分两个类别。这种观察结果解释了基于概率和基于决策值的方法之间的差异...”
我不太熟悉这些方法,无法理解问题,但或许您可以理解。看起来似乎有不同的方法用于预测概率,而type=response对应的方法与用于预测概率的方法不同。

不错的侦探工作!该链接将我发送到一个包含许多PDF文件的目录中。您能否更具体地说明您找到的实际论文是哪一个? - joran
抱歉,复制粘贴时链接被截断了,我现在已经更正了链接,直接指向实际论文。 - Jouni Helske

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接