R机器学习之二:逻辑回归

逻辑回归是啥?

Logistic 回归是一个二分类算法,用来预测给定独立变量集的二分类输出。我们使用哑变量代替二分类输出。也可以把逻辑回归看成输出为类别变量的特殊的线性回归(使用对数几率作为依赖变量)。简而言之,它通过拟合一个logit函数预测一件事情的发生的概率。

逻辑回归方程的由来

广义线性模型的基本等式是;

g(E(y))=α+βx1+γx2

注意:

  1. GLM不假设自变量和因变量之间线性相关,而是假设连接函数和自变量之间线性相关。
  2. 依赖变量不需要规则化
  3. 使用极大似然估计
  4. 误差独立但未必是正态分布
    连接函数要满足的条件
    由于我们只关心输出的概率,连接函数应输出概率,因此应满足:

  5. 总是正的

  6. 不大于1
    考虑一个简单的因变量被连接函数作用的线性回归
    g(y)=β0+β(Age)
    —(a)
    我们先把g()记为p得了,由上面第一个条件
    p=exp(β0+β(Age))=e(β0+β(Age))
    —(b)
    考虑上面第二个条件
    p=exp(β0+β(Age))exp(β0+β(Age))+1=e(β0+β(Age))e(β0+β(Age))+1
    —(c)
    由(a),(b),(c)得到
    p=ey1+ey
    —(d)
    (d)就是logistic函数
    p1p=ey

    log(p1p)=y

评估逻辑回归

  1. AIC—logistic调整的 R2 是AIC,它惩罚参数个数
  2. NULL Deviance and Residual Deviance—–Null Deviance indicates the response predicted
    by a model with nothing but an intercept. Lower the value, better the model. Residual
    deviance indicates the response predicted by a model on adding independent variables.
    Lower the value, better the model.

  3. 混淆矩阵

  4. ROC曲线
    先明确两个概念吧:
    灵敏度 Sn=TPTP+FN
    特异度 Sp=TNTN+FP
    即Sn表示在真阳性样本中有多少比例能被正确检验出来,Sp表示在真阳性样本中有多少比例没有被误判。
    通过采用不同的阈值,可以使第一类错误率和第二类错误率连续变化。 ROC曲线把灵敏度即真阳性率作为纵坐标轴,把假阳性率作为横坐标轴。然后根据要求确定曲线上某一适当的工作点,以此确定似然比阈值。
    ## 服装推荐案例 ##
    Dressify 是一个服装公司,希望基于服装和市场属性找出推荐销售的的服装。
> head(train)
      ID    Style   Price Rating Size Season  NeckLine SleeveLength waiseline Material FabricType
1 100346     Sexy     Low    0.0 free Winter    v-neck    sleevless    empire   cotton    chiffon
2 100348   Casual     low    4.8 free Summer    o-neck    sleevless   natural   cotton       null
3 100349     work Average    4.7    M Spring    v-neck    sleevless      null     null       null
4 100351  Novelty Average    0.0 free winter    o-neck        short   natural polyster broadcloth
5 100352   Casual     Low    4.6 free Winter boat-neck         full   natural   cotton       null
6 100353 bohemian     Low    4.6 free winter    o-neck    sleevless    empire polyster       null
  Decoration Pattern.Type Area Recommended
1       null        solid    C           1
2       null        print    D           1
3       null         null    A           1
4       lace         null    A           1
5       null    patchwork    C           1
6       null    patchwork    A           1

## R代码实现 ##

#load data
train <- read.csv('Train_Old.csv')
install.packages('caTools')
library(caTools)
set.seed(88)
split <- sample.split(train$Recommended, SplitRatio = 0.75)
#get training and test data
dresstrain <- subset(train, split == TRUE)
dresstest <- subset(train, split == FALSE)
#logistic regression model
model <- glm (Recommended ~ .-ID, data = dresstrain, family = binomial)
summary(model)
predict <- predict(model, type = 'response')
#confusion matrix
table(dresstrain$Recommended, predict > 0.5)
    FALSE TRUE
  0   142   12
  1    15  100
#ROCR Curve
library(ROCR)
ROCRpred <- prediction(predict, dresstrain$Recommended)
ROCRperf <- performance(ROCRpred, 'tpr','fpr')
plot(ROCRperf, colorize = TRUE, text.adj = c(-0.2,1.7))

这里写图片描述

#plot glm
library(ggplot2)
ggplot(dresstrain, aes(x=Rating, y=Recommended)) + geom_point() +
stat_smooth(method="glm", family="binomial", se=FALSE)

这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值