library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.1 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.3 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
auto_data <- read_csv("automobile.csv", na = "?")
## Rows: 205 Columns: 26
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (10): make, fuel_type, aspiration, num_doors, body_style, drive_wheels, ...
## dbl (16): symboling, normalized_losses, wheel_base, length, width, height, c...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
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)) |> drop_na()
auto_scaled <- scale(auto_clean[, -1])
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
Scatter Plot:
pca_scores <- as.data.frame(pca$x)
pca_scores$fuel_type <- auto_clean$fuel_type
ggplot(pca_scores, aes(x = PC1, y = PC2, color = fuel_type)) +
geom_point() +
labs(
title = "PCA of Automobile Data",
x = "PC1",
y = "PC2", color = "Fuel Type" ) + theme_minimal()
Rotation Plot:
rotation <- as.data.frame(pca$rotation)
rotation$Variable <- rownames(rotation)
ggplot(rotation, aes(x = PC1, y = PC2)) +
geom_segment(
aes(x = 0, y = 0, xend = PC1, yend = PC2),
arrow = arrow(length = unit(0.2, "cm")) ) +
geom_text(aes(label = Variable), vjust = -0.5) +
labs( title = "PCA Rotation Plot",
x = "PC1",
y = "PC2" ) +
theme_minimal()
PC1 shows differences in car size, power, and fuel efficiency. The rotation plot shows that larger cars with more horsepower and bigger engines are different from cars with higher MPG. This suggests that bigger and more powerful cars usually have lower fuel efficiency.
PC2 shows some additional differences between the cars, especially wheelbase and horsepower. Based on the rotation plot, these variables help separate the cars along PC2.
The scatter plot shows that gas and diesel cars overlap, so they do not form completely separate groups. This means that gas and diesel cars can have similar features. Differences in MPG, horsepower, engine size, and car size may help explain some of the separation.
variation <- summary(pca)$importance[2, 1:2] * 100
variation
## PC1 PC2
## 75.705 12.563
PC1 explains about 75.7% of the variation and PC2 explains about 12.6%. Together, they explain about 88.3% of the variation. This means that PC1 and PC2 do a good job of summarizing most of the information in the dataset.