Logistic Regression Using a Built-in R Dataset
Experiment: Logistic Regression Using a Built-in R Dataset
1. Experiment Title
Implementation of Binary Logistic Regression Using the Built-in mtcars Dataset in R
2. Aim
To perform Binary Logistic Regression using the built-in mtcars dataset in R and predict the transmission type of a car based on its characteristics.
3. Objectives
After completing this experiment, students should be able to:
- Explore a built-in dataset in R.
- Understand the concept of Binary Logistic Regression.
- Identify dependent and independent variables.
-
Build a Logistic Regression model using
glm(). - Predict probabilities and class labels.
- Create and interpret a confusion matrix.
- Calculate classification accuracy.
- Predict the class of a new observation.
4. Theory
4.1 Logistic Regression
Logistic Regression is used when the dependent variable is categorical.
When the dependent variable has two possible classes, it is called Binary Logistic Regression.
Examples include:
- Yes / No
- Pass / Fail
- Disease / No Disease
- Purchase / No Purchase
- Automatic / Manual
Unlike linear regression, Logistic Regression predicts the probability of belonging to a particular class.
4.2 Logistic Regression Model
For Binary Logistic Regression, the dependent variable is represented as:
The logistic model is based on the logit function:
where:
- = Probability of belonging to Class 1
- = Independent variables
- = Intercept
- = Regression coefficients
The probability is obtained using the sigmoid function:
The predicted probability always lies between 0 and 1.
5. Built-in Dataset: mtcars
The built-in R dataset:
mtcars
contains information about 32 automobiles.
Some important variables are:
| Variable | Description |
|---|---|
mpg | Miles per gallon |
cyl | Number of cylinders |
hp | Horsepower |
wt | Weight |
am | Transmission type |
The variable am represents:
| Value | Transmission |
|---|---|
| 0 | Automatic |
| 1 | Manual |
6. Problem Statement
A car manufacturer wants to predict whether a car has a Manual or Automatic transmission based on its:
-
Horsepower (
hp) -
Weight (
wt)
Using the built-in mtcars dataset:
-
Dependent Variable:
am -
Independent Variables:
hpandwt
Perform Binary Logistic Regression and evaluate the model.
7. R Program
# ------------------------------------------------- # Binary Logistic Regression Using mtcars Dataset # ------------------------------------------------- # Load the built-in dataset data(mtcars) # ------------------------------------------------- # Step 1: Explore the Dataset # ------------------------------------------------- cat("First Few Records of mtcars Dataset:\n") head(mtcars) cat("\nStructure of Dataset:\n") str(mtcars) cat("\nSummary Statistics:\n") summary(mtcars) # ------------------------------------------------- # Step 2: Select Required Variables # ------------------------------------------------- car_data <- mtcars[, c("am", "hp", "wt")] cat("\nSelected Data:\n") print(car_data) # ------------------------------------------------- # Step 3: Convert Transmission into a Factor # ------------------------------------------------- car_data$am <- factor( car_data$am, levels = c(0, 1), labels = c("Automatic", "Manual") ) cat("\nTransmission Type:\n") print(car_data$am) # ------------------------------------------------- # Step 4: Build Logistic Regression Model # ------------------------------------------------- model <- glm( am ~ hp + wt, data = car_data, family = binomial ) # ------------------------------------------------- # Step 5: Display Model Summary # ------------------------------------------------- cat("\nLogistic Regression Model Summary:\n") summary(model) # ------------------------------------------------- # Step 6: Predict Probabilities # ------------------------------------------------- predicted_probability <- predict( model, type = "response" ) cat("\nPredicted Probabilities:\n") print(predicted_probability) # ------------------------------------------------- # Step 7: Convert Probabilities into Classes # ------------------------------------------------- predicted_class <- ifelse( predicted_probability >= 0.5, "Manual", "Automatic" ) cat("\nPredicted Transmission Type:\n") print(predicted_class) # ------------------------------------------------- # Step 8: Add Predictions to Dataset # ------------------------------------------------- car_data$Predicted_Probability <- predicted_probability car_data$Predicted_Class <- predicted_class cat("\nActual and Predicted Results:\n") print(car_data) # ------------------------------------------------- # Step 9: Create Confusion Matrix # ------------------------------------------------- confusion_matrix <- table( Actual = car_data$am, Predicted = predicted_class ) cat("\nConfusion Matrix:\n") print(confusion_matrix) # ------------------------------------------------- # Step 10: Calculate Accuracy # ------------------------------------------------- accuracy <- sum(diag(confusion_matrix)) / sum(confusion_matrix) cat( "\nAccuracy =", round(accuracy * 100, 2), "%\n" ) # ------------------------------------------------- # Step 11: Predict Transmission for a New Car # ------------------------------------------------- new_car <- data.frame( hp = 120, wt = 3.0 ) # Predict probability new_probability <- predict( model, newdata = new_car, type = "response" ) # Convert probability to class new_class <- ifelse( new_probability >= 0.5, "Manual", "Automatic" ) cat("\nPrediction for New Car\n") cat("Horsepower =", 120, "\n") cat("Weight =", 3.0, "\n") cat( "Probability of Manual Transmission =", round(new_probability, 4), "\n" ) cat( "Predicted Transmission =", new_class, "\n" )Output
First Few Records of mtcars Dataset: Structure of Dataset: 'data.frame': 32 obs. of 11 variables: $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ... $ cyl : num 6 6 4 6 8 6 8 4 4 6 ... $ disp: num 160 160 108 258 360 ... $ hp : num 110 110 93 110 175 105 245 62 95 123 ... $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ... $ wt : num 2.62 2.88 2.32 3.21 3.44 ... $ qsec: num 16.5 17 18.6 19.4 17 ... $ vs : num 0 0 1 1 0 1 0 1 1 1 ... $ am : num 1 1 1 0 0 0 0 0 0 0 ... $ gear: num 4 4 4 3 3 3 3 4 4 4 ... $ carb: num 4 4 1 1 2 1 4 2 2 4 ... Summary Statistics: Selected Data: am hp wt Mazda RX4 1 110 2.620 Mazda RX4 Wag 1 110 2.875 Datsun 710 1 93 2.320 Hornet 4 Drive 0 110 3.215 Hornet Sportabout 0 175 3.440 Valiant 0 105 3.460 Duster 360 0 245 3.570 Merc 240D 0 62 3.190 Merc 230 0 95 3.150 Merc 280 0 123 3.440 Merc 280C 0 123 3.440 Merc 450SE 0 180 4.070 Merc 450SL 0 180 3.730 Merc 450SLC 0 180 3.780 Cadillac Fleetwood 0 205 5.250 Lincoln Continental 0 215 5.424 Chrysler Imperial 0 230 5.345 Fiat 128 1 66 2.200 Honda Civic 1 52 1.615 Toyota Corolla 1 65 1.835 Toyota Corona 0 97 2.465 Dodge Challenger 0 150 3.520 AMC Javelin 0 150 3.435 Camaro Z28 0 245 3.840 Pontiac Firebird 0 175 3.845 Fiat X1-9 1 66 1.935 Porsche 914-2 1 91 2.140 Lotus Europa 1 113 1.513 Ford Pantera L 1 264 3.170 Ferrari Dino 1 175 2.770 Maserati Bora 1 335 3.570 Volvo 142E 1 109 2.780 Transmission Type: [1] Manual Manual Manual Automatic Automatic Automatic [7] Automatic Automatic Automatic Automatic Automatic Automatic [13] Automatic Automatic Automatic Automatic Automatic Manual [19] Manual Manual Automatic Automatic Automatic Automatic [25] Automatic Manual Manual Manual Manual Manual [31] Manual Manual Levels: Automatic Manual Logistic Regression Model Summary: Predicted Probabilities: Mazda RX4 Mazda RX4 Wag Datsun 710 8.423355e-01 4.047825e-01 9.702408e-01 Hornet 4 Drive Hornet Sportabout Valiant 4.172803e-02 6.938812e-02 4.988159e-03 Duster 360 Merc 240D Merc 230 2.480412e-01 9.265579e-03 4.099813e-02 Merc 280 Merc 280C Merc 450SE 1.119071e-02 1.119071e-02 5.486846e-04 Merc 450SL Merc 450SLC Cadillac Fleetwood 8.500812e-03 5.690614e-03 9.787752e-08 Lincoln Continental Chrysler Imperial Fiat 128 3.445824e-08 1.124127e-07 9.699832e-01 Honda Civic Toyota Corolla Toyota Corona 9.995459e-01 9.983241e-01 9.210955e-01 Dodge Challenger AMC Javelin Camaro Z28 1.553207e-02 3.041008e-02 3.585962e-02 Pontiac Firebird Fiat X1-9 Porsche 914-2 2.815112e-03 9.963800e-01 9.923618e-01 Lotus Europa Ford Pantera L Ferrari Dino 9.999782e-01 9.433828e-01 9.437365e-01 Maserati Bora Volvo 142E 8.960342e-01 5.856710e-01 Predicted Transmission Type: Mazda RX4 Mazda RX4 Wag Datsun 710 "Manual" "Automatic" "Manual" Hornet 4 Drive Hornet Sportabout Valiant "Automatic" "Automatic" "Automatic" Duster 360 Merc 240D Merc 230 "Automatic" "Automatic" "Automatic" Merc 280 Merc 280C Merc 450SE "Automatic" "Automatic" "Automatic" Merc 450SL Merc 450SLC Cadillac Fleetwood "Automatic" "Automatic" "Automatic" Lincoln Continental Chrysler Imperial Fiat 128 "Automatic" "Automatic" "Manual" Honda Civic Toyota Corolla Toyota Corona "Manual" "Manual" "Manual" Dodge Challenger AMC Javelin Camaro Z28 "Automatic" "Automatic" "Automatic" Pontiac Firebird Fiat X1-9 Porsche 914-2 "Automatic" "Manual" "Manual" Lotus Europa Ford Pantera L Ferrari Dino "Manual" "Manual" "Manual" Maserati Bora Volvo 142E "Manual" "Manual" Actual and Predicted Results: am hp wt Predicted_Probability Mazda RX4 Manual 110 2.620 8.423355e-01 Mazda RX4 Wag Manual 110 2.875 4.047825e-01 Datsun 710 Manual 93 2.320 9.702408e-01 Hornet 4 Drive Automatic 110 3.215 4.172803e-02 Hornet Sportabout Automatic 175 3.440 6.938812e-02 Valiant Automatic 105 3.460 4.988159e-03 Duster 360 Automatic 245 3.570 2.480412e-01 Merc 240D Automatic 62 3.190 9.265579e-03 Merc 230 Automatic 95 3.150 4.099813e-02 Merc 280 Automatic 123 3.440 1.119071e-02 Merc 280C Automatic 123 3.440 1.119071e-02 Merc 450SE Automatic 180 4.070 5.486846e-04 Merc 450SL Automatic 180 3.730 8.500812e-03 Merc 450SLC Automatic 180 3.780 5.690614e-03 Cadillac Fleetwood Automatic 205 5.250 9.787752e-08 Lincoln Continental Automatic 215 5.424 3.445824e-08 Chrysler Imperial Automatic 230 5.345 1.124127e-07 Fiat 128 Manual 66 2.200 9.699832e-01 Honda Civic Manual 52 1.615 9.995459e-01 Toyota Corolla Manual 65 1.835 9.983241e-01 Toyota Corona Automatic 97 2.465 9.210955e-01 Dodge Challenger Automatic 150 3.520 1.553207e-02 AMC Javelin Automatic 150 3.435 3.041008e-02 Camaro Z28 Automatic 245 3.840 3.585962e-02 Pontiac Firebird Automatic 175 3.845 2.815112e-03 Fiat X1-9 Manual 66 1.935 9.963800e-01 Porsche 914-2 Manual 91 2.140 9.923618e-01 Lotus Europa Manual 113 1.513 9.999782e-01 Ford Pantera L Manual 264 3.170 9.433828e-01 Ferrari Dino Manual 175 2.770 9.437365e-01 Maserati Bora Manual 335 3.570 8.960342e-01 Volvo 142E Manual 109 2.780 5.856710e-01 Predicted_Class Mazda RX4 Manual Mazda RX4 Wag Automatic Datsun 710 Manual Hornet 4 Drive Automatic Hornet Sportabout Automatic Valiant Automatic Duster 360 Automatic Merc 240D Automatic Merc 230 Automatic Merc 280 Automatic Merc 280C Automatic Merc 450SE Automatic Merc 450SL Automatic Merc 450SLC Automatic Cadillac Fleetwood Automatic Lincoln Continental Automatic Chrysler Imperial Automatic Fiat 128 Manual Honda Civic Manual Toyota Corolla Manual Toyota Corona Manual Dodge Challenger Automatic AMC Javelin Automatic Camaro Z28 Automatic Pontiac Firebird Automatic Fiat X1-9 Manual Porsche 914-2 Manual Lotus Europa Manual Ford Pantera L Manual Ferrari Dino Manual Maserati Bora Manual Volvo 142E Manual Confusion Matrix: Predicted Actual Automatic Manual Automatic 18 1 Manual 1 12 Accuracy = 93.75 % Prediction for New Car Horsepower = 120 Weight = 3 Probability of Manual Transmission = 0.2624 Predicted Transmission = Automatic
8. Explanation of the Program
Step 1: Loading the Dataset
data(mtcars)
The mtcars dataset is already available in R.
The first few records can be displayed using:
head(mtcars)
Step 2: Selecting Variables
car_data <- mtcars[, c("am", "hp", "wt")]
We select only the variables required for this experiment:
-
am→ Transmission type -
hp→ Horsepower -
wt→ Vehicle weight
Step 3: Converting the Output Variable
The original am variable contains:
0 → Automatic 1 → Manual
It is converted into a factor for easier interpretation:
car_data$am <- factor( car_data$am, levels = c(0, 1), labels = c("Automatic", "Manual") )
9. Building the Logistic Regression Model
The model is created using:
model <- glm( am ~ hp + wt, data = car_data, family = binomial )
The function glm() means Generalized Linear Model.
The argument:
family = binomial
specifies that Binary Logistic Regression is being used.
The expression:
am ~ hp + wt
means:
Predict the transmission type using horsepower and vehicle weight.
10. Predicting Probabilities
The model predicts the probability that a car belongs to the positive class:
predicted_probability <- predict( model, type = "response" )
Since the positive class is Manual, the values represent:
Probability that the car has a Manual transmission.
For example:
0.80
means approximately:
80% predicted probability of Manual transmission.
11. Converting Probability into a Class
A threshold of 0.5 is used.
predicted_class <- ifelse( predicted_probability >= 0.5, "Manual", "Automatic" )
| Probability | Prediction |
|---|---|
| ≥ 0.5 | Manual |
| < 0.5 | Automatic |
12. Confusion Matrix
The confusion matrix compares:
- Actual transmission type
- Predicted transmission type
confusion_matrix <- table( Actual = car_data$am, Predicted = predicted_class )
A good model will have more observations along the main diagonal, representing correct predictions.
13. Accuracy
Accuracy is calculated as:
In R:
accuracy <- sum(diag(confusion_matrix)) / sum(confusion_matrix)
14. Predicting a New Car
The program predicts the transmission type of a new car with:
| Variable | Value |
|---|---|
| Horsepower | 120 |
| Weight | 3.0 |
The model calculates the probability of a Manual transmission and then classifies it as:
- Manual, or
- Automatic
using the threshold of 0.5.
15. Important Functions Used
| Function | Purpose |
|---|---|
data() | Loads a built-in dataset |
head() | Displays the first few records |
str() | `Displays dataset structure |
summary() | Displays statistical summary |
factor() | Converts categorical data into factors |
glm() | Builds a generalized linear model |
predict() | Predicts probabilities |
ifelse() | Converts probabilities into classes |
table() | Creates a confusion matrix |
16. Result
Binary Logistic Regression was successfully implemented using the built-in
mtcarsdataset in R. A model was developed to predict whether a car has a Manual or Automatic transmission based on its horsepower and weight. The model was used to calculate predicted probabilities, classify observations, generate a confusion matrix, calculate classification accuracy, and predict the transmission type of a new car.
Comments
Post a Comment