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
auto_numeric <- auto_clean |>
select(wheel_base, length, width, curb_weight, engine_size,
horsepower, city_mpg, highway_mpg)
auto_scaled <- scale(auto_numeric)
# Step 2 +3 : Find Directions of Maximum Variation and rank them
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
pca$rotation
## PC1 PC2 PC3 PC4 PC5
## wheel_base 0.3144622 -0.58325043 -0.1400494 -0.181761340 0.061969778
## length 0.3632918 -0.34403449 -0.1619823 -0.177442376 -0.589394926
## width 0.3628955 -0.25507029 0.1376091 0.809456560 0.293278038
## curb_weight 0.3911193 -0.09103041 0.1278216 -0.143468257 -0.003281696
## engine_size 0.3498786 0.11092441 0.6233518 -0.440961264 0.380154518
## horsepower 0.3324751 0.46636804 0.3311621 0.233287261 -0.553362040
## city_mpg -0.3495267 -0.38328744 0.4566544 0.005625945 -0.063787053
## highway_mpg -0.3596409 -0.30232298 0.4605478 0.103790754 -0.328564753
## PC6 PC7 PC8
## wheel_base 0.70494325 0.03057073 -0.081003965
## length -0.44153531 -0.36223845 0.135568383
## width -0.15608310 -0.13669176 0.005588183
## curb_weight -0.31120431 0.83061977 -0.122595116
## engine_size -0.07765246 -0.35955977 -0.049355129
## horsepower 0.42475591 0.06042025 0.132635371
## city_mpg 0.01025487 0.16044025 0.701742046
## highway_mpg -0.01840879 -0.02366389 -0.668975943
#Plot the scatter plot with arrows
plot(pca$x[,1], pca$x[,2],
xlab = "PC1",
ylab = "PC2")
arrows(0, 0,
pca$rotation[,1] * 3,
pca$rotation[,2] * 3)
text(pca$rotation[,1] * 3,
pca$rotation[,2] * 3,
labels = rownames(pca$rotation))
#Plot the rotation arrows
plot(pca$rotation[,1], pca$rotation[,2],
xlab = "PC1",
ylab = "PC2")
arrows(0, 0,
pca$rotation[,1],
pca$rotation[,2])
text(pca$rotation[,1],
pca$rotation[,2],
labels = rownames(pca$rotation))
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 represents the main difference between larger, heavier, more powerful cars and smaller, more fuel-efficient cars. In the rotation plot, variables such as curb weight, engine size, horsepower, length, and width point in one direction, while city MPG and highway MPG point in the opposite direction. This shows that cars with more size and power tend to have lower 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 appears to separate cars based more on horsepower when compared with vehicle size and fuel efficiency. Horsepower has a strong positive loading on PC2, while wheel base, length, width, city MPG, and highway MPG load in the opposite direction. This suggests that PC2 focuses differences between cars that are focused on performance and cars that are larger or more fuel-efficient.
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 do not appear to form completely separate clusters because there is a lot of overlap in the PC1 and PC2 plot. This suggests that the two fuel types share many similar characteristics. However, cars with higher horsepower and engine size tend to fall in a different direction from cars with higher city and highway MPG.
#How much variation is explained by PC1 and PC2?
# PC1 explains that ~75% of the total variation in the data, while PC2 explains ~12%. PC1 and PC2 appear to explain ~88% of the total variation. This indicates that the first two principal components summarize most of the information from the original variables and provides a good representation of the automobile dataset.
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?