我正在使用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
class.weights
会改变界限,但问题仍然存在。 - roelandvanbeek