There are a few things:
- the outcome (
mtcars$am
) is numeric 0/1 andtrain
will treat this as a regression model - when the outcome is a factor,
train
will assume classification and will automatically addglm=list(family=binomial)
- with classification and
train
, you will need to addclassProbs = TRUE
totrainControl
for the model to produce class probabilities.
Here is an example with a different data set in the earth
package:
library(earth)
library(caret)
data(etitanic)
a1 <- earth(survived ~ .,
data = etitanic,
glm=list(family=binomial),
degree = 2,
nprune = 5)
etitanic$survived <- factor(ifelse(etitanic$survived == 1, "yes", "no"),
levels = c("yes", "no"))
a2 <- train(survived ~ .,
data = etitanic,
method = "earth",
tuneGrid = data.frame(degree = 2, nprune = 5),
trControl = trainControl(method = "none",
classProbs = TRUE))
then:
> predict(a1, head(etitanic), type = "response")
survived
[1,] 0.8846552
[2,] 0.9281010
[3,] 0.8846552
[4,] 0.4135716
[5,] 0.8846552
[6,] 0.4135716
>
> predict(a2, head(etitanic), type = "prob")
yes no
1 0.8846552 0.11534481
2 0.9281010 0.07189895
3 0.8846552 0.11534481
4 0.4135716 0.58642840
5 0.8846552 0.11534481
6 0.4135716 0.58642840
Max