【R语言实战】——Logistic回归模型

🍉CSDN小墨&晓末:https://blog.csdn.net/jd1813346972

个人介绍: 研一|统计学|干货分享
         擅长Python、Matlab、R等主流编程软件
         累计十余项国家级比赛奖项,参与研究经费10w、40w级横向

文章目录

  • [1. 数据背景:](#1. 数据背景:)
  • [2 数据读取及预处理](#2 数据读取及预处理)
  • [3 建立Logistic回归模型](#3 建立Logistic回归模型)
  • [4 模型预测](#4 模型预测)

该篇文章主要展示了利用R语言建立Logistic回归模型,并对新数据进行预测。

1. 数据背景:

(Sports2.csv)是用于客观性分析的体育文章数据.使用Amazon Mechanical Turk对1000篇体育文章标记了objective(客观)或subjective(主观),这是因变量的两个水平。试以该数据中的Label为因变量,PRP和VBN作为自变量做logistic回归,并对新的样本PRP=20和VBN=5进行判别其Label。

2 数据读取及预处理

运行程序:

复制代码
e2<- read.csv('G:\\Sports2.csv')
#Label赋值(二分类)
e2[,1] <- as.character(e2[,1])
e2[,1] <- gsub("objective",0,e2[,1]) #objective为0
e2[,1] <- gsub("subjective",1,e2[,1])#subjective为1
e2[,1] <- as.numeric(e2[,1])
str(e2)

运行结果:

复制代码
## 'data.frame':    1000 obs. of  9 variables:
##  $ Label     : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ PRP       : int  2 5 0 2 9 6 2 7 10 15 ...
##  $ VBN       : int  0 9 2 1 6 1 3 10 4 6 ...
##  $ imperative: int  0 0 0 1 1 0 0 3 3 3 ...
##  $ Quotes    : int  0 7 0 3 4 6 2 9 10 0 ...
##  $ past      : int  11 13 8 13 34 24 13 21 59 73 ...
##  $ CC        : int  7 1 8 7 33 17 1 5 49 13 ...
##  $ JJS       : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ WRB       : int  0 0 0 0 0 0 0 0 0 0 ...

3 建立Logistic回归模型

运行程序:

复制代码
attach(e2)
e2_glm<-glm(Label~PRP+VBN,family = binomial(link = "logit"))
summary(e2_glm)

运行结果:

复制代码
## 
## Call:
## glm(formula = Label ~ PRP + VBN, family = binomial(link = "logit"))
## 
## Deviance Residuals: 
##    Min      1Q  Median      3Q     Max  
## -3.046  -0.713  -0.541   0.847   2.089  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept) -2.06267    0.13795  -14.95   <2e-16 ***
## PRP          0.02991    0.01077    2.78   0.0055 ** 
## VBN          0.09711    0.00932   10.41   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 1312.5  on 999  degrees of freedom
## Residual deviance: 1026.9  on 997  degrees of freedom
## AIC: 1033
## 
## Number of Fisher Scoring iterations: 4

由此得到初步的Logistic模型:

P = e − 2.0267 − 0.02991 X 1 + 0.09711 X 2 1 + e − 2.0267 − 0.02991 X 1 + 0.09711 X 2 P=\frac{e^{-2.0267-0.02991X_1+0.09711X_2}}{1+e^{-2.0267-0.02991X_1+0.09711X_2}} P=1+e−2.0267−0.02991X1+0.09711X2e−2.0267−0.02991X1+0.09711X2

即:

L o g i t ( P ) = − 2.0267 − 0.02991 X 1 + 0.09711 X 2 Logit(P)=-2.0267-0.02991X_1+0.09711X_2 Logit(P)=−2.0267−0.02991X1+0.09711X2

由数据结果可以看出,在0.01的显著性水平下,自变量PRP和VBN均通过显著性检验。

4 模型预测

运行程序:

复制代码
detach(e2)
xb<-predict(e2_glm,data.frame(PRP=20,VBN=5))
p=exp(xb)/(1+exp(xb));p

运行结果:

复制代码
##      1 
## 0.2731

所以结果为0,属于objective (客观)。

相关推荐
wuhen_n1 小时前
JavaScript内置数据结构
开发语言·前端·javascript·数据结构
努力学算法的蒟蒻1 小时前
day79(2.7)——leetcode面试经典150
算法·leetcode·职场和发展
不会代码的小测试1 小时前
UI自动化-POM封装
开发语言·python·selenium·自动化
2401_841495641 小时前
【LeetCode刷题】二叉树的层序遍历
数据结构·python·算法·leetcode·二叉树··队列
AC赳赳老秦1 小时前
2026国产算力新周期:DeepSeek实战适配英伟达H200,引领大模型训练效率跃升
大数据·前端·人工智能·算法·tidb·memcache·deepseek
roman_日积跬步-终至千里1 小时前
【Java并发】Java 线程池实战:警惕使用CompletableFuture.supplyAsync
java·开发语言·网络
lsx2024061 小时前
C++ 基本的输入输出
开发语言
CodeSheep程序羊1 小时前
拼多多春节加班工资曝光,没几个敢给这个数的。
java·c语言·开发语言·c++·python·程序人生·职场和发展
独好紫罗兰1 小时前
对python的再认识-基于数据结构进行-a002-列表-列表推导式
开发语言·数据结构·python
2401_841495642 小时前
【LeetCode刷题】二叉树的直径
数据结构·python·算法·leetcode·二叉树··递归