Blog Entry 4: Logistic regression with Iris dataset

Title: Logistic regression with Iris dataset

Overview

In this blog post, we explored the application of logistic regression using the famous iris dataset. Logistic regression is a powerful statistical method used for binary classification tasks, where the goal is to predict the probability of an observation belonging to a particular class. We began by loading the iris dataset, which contains measurements of iris flowers along with their species. After visualizing the dataset, we preprocessed the data by encoding the target variable into binary values, splitting the data into training and testing sets, and training a logistic regression model using the training data. We then evaluated the model’s performance on the testing set by computing the confusion matrix. Finally, we discussed some real-life applications of logistic regression, highlighting its versatility in solving various classification problems.

#Loading libraries
library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
#install.packages("caret")
library(caret)
## Loading required package: lattice
#Loading iris dataset
data(iris)

#Exploring the structure of the dataset
str(iris)
## 'data.frame':    150 obs. of  5 variables:
##  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
#Visualization of the dataset
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  labs(title = "Sepal Length vs Sepal Width by Species")

#Data preprocessing

#Lets encode the target variable (Species) into binary values
iris$Species <- as.factor(ifelse(iris$Species == "setosa", "setosa", "other"))

#Splitting the data into training and testing sets
set.seed(123)
trainIndex <- createDataPartition(iris$Species, p = .8, 
                                  list = FALSE, 
                                  times = 1)
iris_train <- iris[ trainIndex,]
iris_test  <- iris[-trainIndex,]

#Training the logistic regression model
log_model <- glm(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, 
                  data = iris_train, family = "binomial")
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
#Model summary
summary(log_model)
## 
## Call:
## glm(formula = Species ~ Sepal.Length + Sepal.Width + Petal.Length + 
##     Petal.Width, family = "binomial", data = iris_train)
## 
## Coefficients:
##                Estimate Std. Error z value Pr(>|z|)
## (Intercept)     -14.762 538087.652       0        1
## Sepal.Length     11.391 150807.997       0        1
## Sepal.Width       7.663  62644.225       0        1
## Petal.Length    -19.949 121563.142       0        1
## Petal.Width     -22.004 167910.701       0        1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 1.5276e+02  on 119  degrees of freedom
## Residual deviance: 2.6529e-09  on 115  degrees of freedom
## AIC: 10
## 
## Number of Fisher Scoring iterations: 25
#Making predictions on the testing set
predictions <- predict(log_model, iris_test, type = "response")

#Converting predicted probabilities to class labels
predicted_classes <- ifelse(predictions > 0.5, "setosa", "other")

#Evaluating model performance
confusionMatrix(table(predicted_classes, iris_test$Species))
## Confusion Matrix and Statistics
## 
##                  
## predicted_classes other setosa
##            other     20      0
##            setosa     0     10
##                                      
##                Accuracy : 1          
##                  95% CI : (0.8843, 1)
##     No Information Rate : 0.6667     
##     P-Value [Acc > NIR] : 5.215e-06  
##                                      
##                   Kappa : 1          
##                                      
##  Mcnemar's Test P-Value : NA         
##                                      
##             Sensitivity : 1.0000     
##             Specificity : 1.0000     
##          Pos Pred Value : 1.0000     
##          Neg Pred Value : 1.0000     
##              Prevalence : 0.6667     
##          Detection Rate : 0.6667     
##    Detection Prevalence : 0.6667     
##       Balanced Accuracy : 1.0000     
##                                      
##        'Positive' Class : other      
## 
#Real-life applications
#Logistic regression can be used in various real-life scenarios such as predicting customer churn, detecting fraudulent transactions, or classifying email spam.

Conclusion

Logistic regression is a valuable tool in the field of statistics and machine learning, particularly for binary classification tasks. In this blog post, we demonstrated how logistic regression can be applied using the iris dataset to predict the species of iris flowers based on their measurements. By training a logistic regression model and evaluating its performance, we gained insights into the predictive capabilities of the model. Furthermore, we discussed real-life applications of logistic regression, emphasizing its significance in solving practical classification problems across different domains. Overall, this blog post serves as a practical introduction to logistic regression and its application in real-life scenarios, showcasing its relevance and usefulness in statistical analysis and predictive modeling.

LS0tDQp0aXRsZTogIkJsb2cgNCBEYXRhIDYyMSINCmF1dGhvcjogIkxhdXJhIEIiDQpkYXRlOiAiYHIgU3lzLkRhdGUoKWAiDQpvdXRwdXQ6IG9wZW5pbnRybzo6bGFiX3JlcG9ydA0KLS0tDQoNCiMjIEJsb2cgRW50cnkgNDogTG9naXN0aWMgcmVncmVzc2lvbiB3aXRoIElyaXMgZGF0YXNldA0KDQoNClRpdGxlOiBMb2dpc3RpYyByZWdyZXNzaW9uIHdpdGggSXJpcyBkYXRhc2V0DQoNCiMjIyBPdmVydmlldw0KDQpJbiB0aGlzIGJsb2cgcG9zdCwgd2UgZXhwbG9yZWQgdGhlIGFwcGxpY2F0aW9uIG9mIGxvZ2lzdGljIHJlZ3Jlc3Npb24gdXNpbmcgdGhlIGZhbW91cyBpcmlzIGRhdGFzZXQuIExvZ2lzdGljIHJlZ3Jlc3Npb24gaXMgYSBwb3dlcmZ1bCBzdGF0aXN0aWNhbCBtZXRob2QgdXNlZCBmb3IgYmluYXJ5IGNsYXNzaWZpY2F0aW9uIHRhc2tzLCB3aGVyZSB0aGUgZ29hbCBpcyB0byBwcmVkaWN0IHRoZSBwcm9iYWJpbGl0eSBvZiBhbiBvYnNlcnZhdGlvbiBiZWxvbmdpbmcgdG8gYSBwYXJ0aWN1bGFyIGNsYXNzLiBXZSBiZWdhbiBieSBsb2FkaW5nIHRoZSBpcmlzIGRhdGFzZXQsIHdoaWNoIGNvbnRhaW5zIG1lYXN1cmVtZW50cyBvZiBpcmlzIGZsb3dlcnMgYWxvbmcgd2l0aCB0aGVpciBzcGVjaWVzLiBBZnRlciB2aXN1YWxpemluZyB0aGUgZGF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