R逻辑回归示例图解

Logistic回归是一种回归模型, 其中响应变量具有分类值, 例如true / false或0/1。因此, 我们可以测量二进制响应的概率。

y = 1 /(1 + e ^-(b0 + b1 x1 + b2 x2 +⋯))

glm()函数具有以下语法。

``glm(formula, data, family)``

S.No Parameter Description
1. formula 它是一个表示变量与变量之间的关系的符号。
2. data 它是提供变量值的数据集。
3. family 一个R对象, 它指定模型的详细信息, 其值对于逻辑回归是二项式的。

建立逻辑回归

``````#Loading library
library(mlbench)
#Using BreastCancer dataset
data(BreastCancer, package = "mlbench")
breast_canc = BreastCancer[complete.cases(BreastCancer), ]
#Displaying the information related to dataset with the str() function.
str(breast_canc)``````

``````#Dividing dataset into training and test dataset.
set.seed(100)
#Creating partitioning.
Training_Ratio <- createDataPartition(b_canc\$Class, p=0.7, list = F)
#Creating training data.
Training_Data <- b_canc[Training_Ratio, ]
str(Training_Data)
#Creating test data.
Test_Data <- b_canc[-Training_Ratio, ]
str(Test_Data)``````

``````#Creating Regression Model
glm(Class ~ Cell.shape, family="binomial", data = Training_Data)``````

``````#Creating Regression Model
model<-glm(Class ~ Cell.shape, family="binomial", data = Training_Data)
#Using summary function
print(summary(model))``````