If you do predict
on a glm
binomial regression, you get the probability of the positive class, and the probabilities from tidymodels
are rounded up.
For example, a simple regression with response as 0/1, 1 being positive class :
library(tidymodels)
set.seed(111)
df = data.frame(y = factor(rbinom(50,1,0.5)),x=runif(50),z=runif(50))
train = df[1:40,]
test = df[41:50,]
logit_mod <- logistic_reg() %>%
set_mode("classification") %>%
set_engine("glm") %>%
fit(y ~ x + z, data=train)
res <- predict(logit_mod, new_data = test, type="prob")
This is the prediction for class 1 :
res$.pred_1
41 42 43 44 45 46 47 48
0.3186626 0.3931925 0.4259043 0.3651420 0.6670263 0.6732433 0.5844562 0.5584770
49 50
0.6791727 0.7567285
Do glm and you can see its exactly the same:
fit <- glm(y ~ x + z, data=train, family=binomial)
res2 <- predict(fit, newdata=test, type="response")
res2
41 42 43 44 45 46 47 48
0.3186626 0.3931925 0.4259043 0.3651420 0.6670263 0.6732433 0.5844562 0.5584770
49 50
0.6791727 0.7567285
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…