← Knowledge

Public EcoRouter Knowledge · AI-generated content · September 9, 2026

breakdown of how to do Principal Component Analysis (PCA) and what kind data it needs and when to use it, pro and cons a

The question

breakdown of how to do Principal Component Analysis (PCA) and what kind data it needs and when to use it, pro and cons and how to perform in R and visualization options

Balanced

1. What is PCA & How It Works

Principal Component Analysis (PCA) is an unsupervised dimensionality-reduction technique. It transforms a large set of correlated variables into a smaller set of uncorrelated variables called Principal Components (PCs), while retaining as much of the original variation as possible.

The Core Mechanics
  1. Standardize the data: Scale variables to have a mean of 0 and a standard deviation of 1 so that features with large units do not dominate.
  2. Compute the Covariance/Correlation Matrix: Measure how every variable relates to every other variable.
  3. Calculate Eigenvectors and Eigenvalues:
    • Eigenvectors determine the directions of the new feature space (the axes).
    • Eigenvalues determine their magnitude (how much variance each axis explains).
  4. Sort and Select: Order components by eigenvalue magnitude. PC1 captures the most variance, PC2 captures the second most (orthogonal/perpendicular to PC1), and so on.
  5. Project Data: Transform original data points onto these new principal component axes.

2. What Kind of Data Does It Need?

  • Continuous / Numerical Data: PCA relies on variance and covariance, which are meaningful only for numeric data. (For categorical data, use Multiple Correspondence Analysis instead).
  • Linearly Correlated Features: PCA looks for linear relationships. If variables are non-linearly related, non-linear methods (like t-SNE or UMAP) are more appropriate.
  • No Missing Values: PCA cannot handle NA values natively; impute or remove them first.
  • Normalized / Scaled Features: Variables must be on comparable scales.

3. When to Use It

  • High Multicollinearity: When features are strongly correlated, PCA consolidates them into orthogonal (independent) predictors.
  • Exploratory Data Visualization: Compressing 10+ dimensions into 2D or 3D plots to spot clusters, trends, and outliers.
  • Preprocessing for Machine Learning: Reducing feature count to reduce training time and mitigate the curse of dimensionality.
  • Noise Filtering: Discarding the lowest-variance components, which often capture random noise.

4. Pros and Cons

ProsCons
Removes multicollinearity: Components are completely uncorrelated.Loss of interpretability: PC1 might be $0.4(\text{Age}) + 0.6(\text{Income}) - 0.2(\text{Debt})$, making direct business/physical interpretation difficult.
Reduces computational complexity: Speeds up model training.Information loss: Discarding components means losing some data variance.
Simplifies visualization: Enables 2D/3D projection of complex datasets.Sensitive to outliers: Outliers distort the covariance matrix and component directions.
Noise reduction: Lower components often contain pure noise.Linear assumption only: Fails to capture complex non-linear structures.

5. How to Perform PCA in R

The standard, most stable function in R is prcomp() (which uses Singular Value Decomposition).

R
# 1. Load sample data (using the built-in iris dataset)
data(iris)
numeric_data <- iris[, 1:4] # Select numeric columns

# 2. Run PCA with standardization (center = TRUE, scale. = TRUE)
pca_result <- prcomp(numeric_data, center = TRUE, scale. = TRUE)

# 3. View variance explained by each component
summary(pca_result)

# 4. View loadings (how original variables contribute to each PC)
pca_result$rotation

# 5. Access the transformed coordinates (scores)
head(pca_result$x)

6. Visualization Options in R

To visualize PCA results effectively, the factoextra and ggplot2 packages provide the cleanest graphics.

A. Scree Plot (Determining How Many Components to Keep)

A scree plot shows the percentage of variance explained by each PC to help identify an "elbow."

R
# install.packages("factoextra")
library(factoextra)

# Scree plot
fviz_eig(pca_result, addlabels = TRUE, ylim = c(0, 80))
B. Biplot (Variables + Observations)

A biplot overlays the individual observations with vectors showing how each original variable contributes to the principal components.

R
# Biplot colored by groups
fviz_pca_biplot(pca_result, 
                geom.ind = "point",
                col.ind = iris$Species,   # Color observations by group
                palette = "jco",
                addEllipses = TRUE,       # Concentration ellipses
                legend.title = "Species",
                repel = TRUE)             # Avoid text overlapping
C. Variable Correlation Circle

Shows how variables correlate with each other and with the principal components.

R
fviz_pca_var(pca_result,
             col.var = "contrib",       # Color by contribution to PC
             gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
             repel = TRUE)
D. Base R Alternative (No extra packages)
R
# Quick base R biplot
biplot(pca_result, cex = 0.7)

# Quick base R scree plot
plot(pca_result, type = "l", main = "Scree Plot")

Answering this used an estimated 75% less than EcoRouter’s configured frontier baseline

Routed to
Balanced
Tokens
206 in / 2,046 out
Cost
$0.0078
Baseline
$0.03

Figures recorded by EcoRouter when this answer was generated, and fixed at that moment. Cost comparisons are against a configured reference model, not a measurement of electricity, carbon or water.

Ask about this

Ask EcoRouter a follow-up using this Knowledge as context. Nothing becomes public unless you choose to publish it.

Ask a follow-up
11 views

Comments

No comments yet.