逻辑回归是一种用于有监督学习,解决分类问题的算法。虽然叫回归,但是是一个分类算法,本质上是根据特征变量预测某个事件是否发生的概率,然后基于概率值,进一步做出二元分类的预测。
1、逻辑回归理解与实操
1.1 简单理解与实操
- 逻辑回归与线性回归的相同之处在于参数类型都是一致的,而且由于通过
Sigmoid
函数将输出结果限定在0~1(概率值)的范围内,以对应是否发生事件(一般1
表示 阳性结果)
-
glm(y ~ x, family = "binomial", data = data)
基础函数实现逻辑回归
# 特征变量为连续型变量
model1 <- glm(Attrition ~ MonthlyIncome, family = "binomial", data = churn_train)
model1$coefficients
# (Intercept) MonthlyIncome
# -0.9992505506 -0.0001134135
model2 <- glm(Attrition ~ OverTime, family = "binomial", data = churn_train)
model2$coefficients
# (Intercept) OverTimeYes
# -2.162438 1.407991
1.2关于逻辑回归的参数解释
(1)如上Attrition ~ MonthlyIncome
逻辑回归的结果中,月收入的系数为负值。这表明月收入越高,输出结果越接近0。由于在Sigmoid
函数转换时经过log处理,因此参数需要exp()
处理才能得到有意义的结果;
exp(coef(model1))
# (Intercept) MonthlyIncome
# 0.3681553 0.9998866
(2)如上结果表明:月收入每增长1美元,会离职的概率就要乘一次0.9998866,即离职概率降低。
- 多变量线性回归
model3 <- glm(
Attrition ~ MonthlyIncome + OverTime,
family = "binomial",
data = churn_train
)
rsample::tidy(model3)
# # A tibble: 3 x 5
# term estimate std.error statistic p.value
# <chr> <dbl> <dbl> <dbl> <dbl>
# 1 (Intercept) -1.46 0.174 -8.41 4.20e-17
# 2 MonthlyIncome -0.000126 0.0000258 -4.87 1.11e- 6
# 3 OverTimeYes 1.48 0.181 8.20 2.48e-16
如上结果表明两个特征变量都对分类预测有显著意义(p.value);其中月收入越高、不加班,离职的概率会低
2、10折交叉验证比较模型性能
caret::train( method = "glm", family = "binomial")
2.1 使用caret包,对建立的逻辑回归模型进行10折交叉验证
library(caret)
set.seed(123)
cv_model1 <- train(
Attrition ~ MonthlyIncome, #单变量
data = churn_train,
method = "glm",
family = "binomial",
trControl = trainControl(method = "cv", number = 10)
)
set.seed(123)
cv_model2 <- train(
Attrition ~ MonthlyIncome + OverTime, #多变量
data = churn_train,
method = "glm",
family = "binomial",
trControl = trainControl(method = "cv", number = 10)
)
set.seed(123)
cv_model3 <- train(
Attrition ~ ., #全部变量
data = churn_train,
method = "glm",
family = "binomial",
trControl = trainControl(method = "cv", number = 10)
)
- 查看、比较模型的准确率
stat = summary(
resamples(
list(model1 = cv_model1,
model2 = cv_model2,
model3 = cv_model3)))
stat$statistics$Accuracy
# Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
# model1 0.8349515 0.8349515 0.8398379 0.8395076 0.8431373 0.8446602 0
# model2 0.8349515 0.8349515 0.8398379 0.8395076 0.8431373 0.8446602 0
# model3 0.8349515 0.8630782 0.8774510 0.8755063 0.8834951 0.9223301 0
如上结果可以看出,还是最后一个考虑了所有变量的模型具有较高的准确率
有一个小问题:逻辑回归得到的结果一般为概率值,在基于一定的阈值对结果进行分类。那么上述计算准确率应该是基于二分类结果进行判断的,但我好像并未发现默认的阈值设定是多少【网上查到的说法默认为0.5】。
2.2 绘制AUC曲线:ROCR
包
library(ROCR)
# 计算预测结果的概率值
m1_prob <- predict(cv_model1, churn_train, type = "prob")$Yes
m3_prob <- predict(cv_model3, churn_train, type = "prob")$Yes
# 根据概率值,计算AUC指标
# 即不同分类阈值的选择下,模型预测结果的假阳性率(x轴)与真阳性率(y轴)
perf1 <- prediction(m1_prob, churn_train$Attrition) %>%
performance(measure = "tpr", x.measure = "fpr")
perf2 <- prediction(m3_prob, churn_train$Attrition) %>%
performance(measure = "tpr", x.measure = "fpr")
# 绘制ROC曲线,AUC面积越大,表明模型性能越优
plot(perf1, col = "black", lty = 2)
plot(perf2, add = TRUE, col = "blue")
legend(0.8, 0.2, legend = c("cv_model1", "cv_model3"),
col = c("black", "blue"), lty = 2:1, cex = 0.6)
假阳性率FPR:把所有实际为阴性的样本错误预测为阳性的比例(越高表明模型越差);
真阳性率TPR:把所有实际为阳性的样本正确预测为阳性的比例(越高表明模型越好)
这两个指标的互相平衡的过程:当阈值为0时,对应坐标(1,1)
;当阈值为1时,对应坐标为(0,0)
3、模型的重要特征变量
- vip :extract our top 20 influential variables
vip::vip(cv_model3, num_features = 20)
- PDP:monotonic linear relationship
pred.fun <- function(object, newdata) {
Yes <- mean(predict(object, newdata, type = "prob")$Yes)
as.data.frame(Yes)
}
pdp::partial(cv_model3, pred.var = "OverTime", pred.fun = pred.fun) %>%
autoplot(rug = TRUE) + ylim(c(0, 1))
pdp::partial(cv_model3, pred.var = "OverTime", pred.fun = pred.fun) %>%
autoplot(rug = TRUE) + ylim(c(0, 1))