I am using ksvm
from the kernlab package in R to predict probabilities, using the type="probabilities"
option in predict.ksvm
. However, I find that sometimes using predict(model,observation,type="r")
yields not the class with the highest probability given by predict(model,observation,type="p")
.
Example:
> predict(model,observation,type="r")
[1] A
Levels: A B
> predict(model,observation,type="p")
A B
[1,] 0.21 0.79
Is this correct behavior, or a bug? If it is correct behavior, how can I estimate the most likely class from the probabilities?
Attempt at reproducible example:
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])
Output of results:
> 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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…