網站首頁 編程語言 正文
前言
回歸的本質是建立一個模型用來預測,而邏輯回歸的獨特性在于,預測的結果是只能有兩種,true or false
在R里面做邏輯回歸也很簡單,只需要構造好數據集,然后用glm函數(廣義線性模型(generalized linear model))建模即可,預測用predict函數。
我這里簡單講一個例子,來自于加州大學洛杉磯分校的課程
首先加載需要用的包
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.1.3
library(Rcpp)
## Warning: package 'Rcpp' was built under R version 3.2.2
然后加載測試數據
mydata <- read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
## 這里直接讀取網絡數據
head(mydata)
## admit gre gpa rank
## 1 0 380 3.61 3
## 2 1 660 3.67 3
## 3 1 800 4.00 1
## 4 1 640 3.19 4
## 5 0 520 2.93 4
## 6 1 760 3.00 2
#This dataset has a binary response (outcome, dependent) variable called admit.
#There are three predictor variables: gre, gpa and rank. We will treat the variables gre and gpa as continuous.
#The variable rank takes on the values 1 through 4.
summary(mydata)
## admit gre gpa rank
## Min. :0.0000 Min. :220.0 Min. :2.260 Min. :1.000
## 1st Qu.:0.0000 1st Qu.:520.0 1st Qu.:3.130 1st Qu.:2.000
## Median :0.0000 Median :580.0 Median :3.395 Median :2.000
## Mean :0.3175 Mean :587.7 Mean :3.390 Mean :2.485
## 3rd Qu.:1.0000 3rd Qu.:660.0 3rd Qu.:3.670 3rd Qu.:3.000
## Max. :1.0000 Max. :800.0 Max. :4.000 Max. :4.000
sapply(mydata, sd)
## admit gre gpa rank
## 0.4660867 115.5165364 0.3805668 0.9444602
xtabs(~ admit + rank, data = mydata) ##保證結果變量只能是錄取與否,不能有其它!!!
## rank
## admit 1 2 3 4
## 0 28 97 93 55
## 1 33 54 28 12
可以看到這個數據集是關于申請學校是否被錄取的,根據學生的GRE成績,GPA和排名來預測該學生是否被錄取。
其中GRE成績是連續性的變量,學生可以考取任意正常分數。
而GPA也是連續性的變量,任意正常GPA均可。
最后的排名雖然也是連續性變量,但是一般前幾名才有資格申請,所以這里把它當做因子,只考慮前四名!
而我們想做這個邏輯回歸分析的目的也很簡單,就是想根據學生的成績排名,績點信息,托福或者GRE成績來預測它被錄取的概率是多少!
接下來建模
mydata$rank <- factor(mydata$rank)
mylogit <- glm(admit ~ gre + gpa + rank, data = mydata, family = "binomial")
summary(mylogit)
##
## Call:
## glm(formula = admit ~ gre + gpa + rank, family = "binomial",
## data = mydata)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.6268 -0.8662 -0.6388 1.1490 2.0790
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -3.989979 1.139951 -3.500 0.000465 ***
## gre 0.002264 0.001094 2.070 0.038465 *
## gpa 0.804038 0.331819 2.423 0.015388 *
## rank2 -0.675443 0.316490 -2.134 0.032829 *
## rank3 -1.340204 0.345306 -3.881 0.000104 ***
## rank4 -1.551464 0.417832 -3.713 0.000205 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 499.98 on 399 degrees of freedom
## Residual deviance: 458.52 on 394 degrees of freedom
## AIC: 470.52
##
## Number of Fisher Scoring iterations: 4
根據對這個模型的summary結果可知:
GRE成績每增加1分,被錄取的優勢對數(log odds)增加0.002
而GPA每增加1單位,被錄取的優勢對數(log odds)增加0.804,不過一般GPA相差都是零點幾。
最后第二名的同學比第一名同學在其它同等條件下被錄取的優勢對數(log odds)小了0.675,看來排名非常重要啊!!!
這里必須解釋一下這個優勢對數(log odds)是什么意思了,如果預測這個學生被錄取的概率是p,那么優勢對數(log odds)就是log2(p/(1-p)),一般是以自然對數為底
最后我們可以根據模型來預測啦
## 重點是predict函數,type參數
newdata1 <- with(mydata,
data.frame(gre = mean(gre), gpa = mean(gpa), rank = factor(1:4)))
newdata1
## gre gpa rank
## 1 587.7 3.3899 1
## 2 587.7 3.3899 2
## 3 587.7 3.3899 3
## 4 587.7 3.3899 4
## 這里構造一個需要預測的矩陣,4個學生,除了排名不一樣,GRE和GPA都一樣
newdata1$rankP <- predict(mylogit, newdata = newdata1, type = "response")
newdata1
## gre gpa rank rankP
## 1 587.7 3.3899 1 0.5166016
## 2 587.7 3.3899 2 0.3522846
## 3 587.7 3.3899 3 0.2186120
## 4 587.7 3.3899 4 0.1846684
## type = "response" 直接返回預測的概率值0~1之間
可以看到,排名越高,被錄取的概率越大!!!
log(0.5166016/(1-0.5166016)) ## 第一名的優勢對數0.06643082
log((0.3522846/(1-0.3522846))) ##第二名的優勢對數-0.609012
兩者的差值正好是0.675,就是模型里面預測的!
newdata2 <- with(mydata,
data.frame(gre = rep(seq(from = 200, to = 800, length.out = 100), 4),
gpa = mean(gpa), rank = factor(rep(1:4, each = 100))))
##newdata2
##這個數據集也是構造出來,需要用模型來預測的!
newdata3 <- cbind(newdata2, predict(mylogit, newdata = newdata2, type="link", se=TRUE))
## type="link" 返回fit值,需要進一步用plogis處理成概率值
## ?plogis The Logistic Distribution
newdata3 <- within(newdata3, {
PredictedProb <- plogis(fit)
LL <- plogis(fit - (1.96 * se.fit))
UL <- plogis(fit + (1.96 * se.fit))
})
最后可以做一些簡單的可視化
head(newdata3)
## gre gpa rank fit se.fit residual.scale UL
## 1 200.0000 3.3899 1 -0.8114870 0.5147714 1 0.5492064
## 2 206.0606 3.3899 1 -0.7977632 0.5090986 1 0.5498513
## 3 212.1212 3.3899 1 -0.7840394 0.5034491 1 0.5505074
## 4 218.1818 3.3899 1 -0.7703156 0.4978239 1 0.5511750
## 5 224.2424 3.3899 1 -0.7565919 0.4922237 1 0.5518545
## 6 230.3030 3.3899 1 -0.7428681 0.4866494 1 0.5525464
## LL PredictedProb
## 1 0.1393812 0.3075737
## 2 0.1423880 0.3105042
## 3 0.1454429 0.3134499
## 4 0.1485460 0.3164108
## 5 0.1516973 0.3193867
## 6 0.1548966 0.3223773
ggplot(newdata3, aes(x = gre, y = PredictedProb)) +
geom_ribbon(aes(ymin = LL, ymax = UL, fill = rank), alpha = .2) +
geom_line(aes(colour = rank), size=1)
總結?
原文鏈接:https://blog.csdn.net/weixin_46587777/article/details/124924042
相關推薦
- 2023-04-20 Error in render: “TypeError: data.slice is not a f
- 2022-05-17 ubuntu No package ‘libzmq‘ found
- 2022-04-19 Python的閉包和裝飾器你真的了解嗎_python
- 2022-12-24 TS裝飾器bindThis優雅實現React類組件中this綁定_React
- 2022-04-12 jieba.posseg.cut分詞結果與jieba.cut不一致
- 2022-07-01 ASP.NET中的Web控件介紹_基礎應用
- 2022-11-23 iOS開發學習?ViewController使用示例詳解_IOS
- 2022-09-19 Tomcat日志自動分割的三種方法_Tomcat
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支