The Automobile Dataset from the UCI Machine Learning Repository contains 205 car samples with 26 attributes, including numerical features like engine displacement, horsepower, curb weight, and miles per gallon (MPG), plus categorical features like car make or fuel type. For PCA, we’ll focus on numerical features, and you can use a categorical feature fuel-type (gas vs. diesel) for visualization.
Source: https://archive.ics.uci.edu/dataset/10/automobile
Download the dataset and import it here
library(tidyverse)
library(broom)
#Set working directory
# Load data, treating "?" as NA
auto_data <- read_csv("automobile.csv", na = "?")
Clean & filter: Keep 8 numerical columns relevant to car. Features chosen: wheel_base, length, width, curb_weight, engine_size, horsepower, city_mpg, highway_mpg
auto_clean <- auto_data |>
select(fuel_type, wheel_base, length, width, curb_weight, engine_size,
horsepower, city_mpg, highway_mpg) |>
mutate(fuel_type = as.factor(fuel_type)) |> # Ensure fuel_type is a factor
drop_na() # Drop rows with NAs to ensure PCA works
# Step 1: Standardize data
# Step 2 +3 : Find Directions of Maximum Variation and rank them
auto_scaled <- scale(auto_clean |> select(-fuel_type))
pca <- prcomp(auto_scaled)
summary(pca)
## Importance of components:
## PC1 PC2 PC3 PC4 PC5 PC6 PC7
## Standard deviation 2.4610 1.0025 0.69446 0.41938 0.32904 0.2953 0.25527
## Proportion of Variance 0.7571 0.1256 0.06028 0.02199 0.01353 0.0109 0.00815
## Cumulative Proportion 0.7571 0.8827 0.94296 0.96495 0.97848 0.9894 0.99753
## PC8
## Standard deviation 0.14068
## Proportion of Variance 0.00247
## Cumulative Proportion 1.00000
#Plot the scatter plot with arrows
scores <- as.data.frame(pca$x)
scores$fuel_type <- auto_clean$fuel_type
ggplot(scores, aes(PC1, PC2, color = fuel_type)) +
geom_point() +
theme_minimal()
#Plot the rotation arrows
biplot(pca)
Describe the pattern captured by the first principal component (PC1). For example, does it reflect differences in car performance (e.g., horsepower, engine size) or efficiency (e.g., MPG)? Use the rotation plot to support your interpretation.
PC1 shows the difference in car size, weight, engine power, and fuel efficiency.
Explain what the second principal component (PC2) highlights. For instance, does it separate cars based on size (e.g., length, width) or other characteristics? Refer to the rotation plot for evidence.
PC2 shows the differences in horsepower and other car features.
Based on the PC1 vs. PC2 scatter plot, do gas and diesel cars form distinct clusters? What do the clusters (or overlap) suggest about similarities or differences in their features (e.g., fuel efficiency, performance)? Use the rotation plot to identify key driving features.
Gas and diesel cars shows some difference, the most diesel cars appear lower on the graph.
#How much variation is explained by PC1 and PC2?
summary(pca)$importance[2, 1:2] * 100
## PC1 PC2
## 75.705 12.563
Report the percentage of variation explained by PC1 and PC2 using the PCA output. What does this tell you about how well these components summarize the dataset?
PC1 explains 75.705% and PC2 explains 12.563% of the variation. Together, they explains 88.268% of the total variation.