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("/Users/danielmedlin/Downloads/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
auto_scaled <- auto_clean |>
select(-fuel_type) |>
scale()
# Step 2 +3 : Find Directions of Maximum Variation and rank them
auto_pca <- prcomp(auto_scaled)
summary(auto_pca)
## Importance of components:
## PC1 PC2 PC3 PC4 PC5 PC6 PC7
## Standard deviation 2.461 1.0025 0.69446 0.41938 0.32904 0.2953 0.25527
## Proportion of Variance 0.757 0.1256 0.06028 0.02199 0.01353 0.0109 0.00815
## Cumulative Proportion 0.757 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
biplot(auto_pca)
#Plot the rotation arrows
plot(auto_pca$x[,1], auto_pca$x[,2],
xlab = "PC1",
ylab = "PC2",
main = "PCA Biplot")
arrows(0,0,
auto_pca$rotation[,1] *5,
auto_pca$rotation[,2] *5)
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 looks to represent the difference between heavier, more powerful cars and smaller, more fuel-efficient cars. Variables like horsepower, engine size, curb weight, length, and width load in one direction on PC1, while MPG and highway MPG go in the opposite direction. This suggests cars scoring higher in PC1 are larger, more heavy cars that comsume more fuel.
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.
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. PC2 seems to hightlight the differences in horsepower related to some of the vehicle’s size characteristics. Horsepower goes in a positive direction while variables like wheelbase and other size characteristics also go negative. PC2 looks to figure out cars based on performance that size characteristics can’t explain fully.
#How much variation is explained by PC1 and PC2?
summary(auto_pca)$importance[2, 1:2]
## PC1 PC2
## 0.75705 0.12563
sum(summary(auto_pca)$importance[2, 1:2])
## [1] 0.88268
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 around 75.71% of the variation, while PC2 explains around 12.56% of the variation. Combined, they both explain around 88.27% of the variation in the dataset. This means the first two principal components summarize the original number of components very well, since it kept most of the original variance.