library(mvtnorm)
library(ggplot2)
library(matlib)
set.seed(123)
n <- 100
mu <- c(1, 2)
sigma <- matrix(c(1, 0.5, 0.5, 2), nrow = 2)
data <- rmvnorm(n, mean = mu, sigma = sigma)
pca_result <- prcomp(data)
pc_scores <- pca_result$x
pc_loadings <- pca_result$rotation
variance_ratio <- pca_result$sdev^2 / sum(pca_result$sdev^2)
cat("Explained Variance Ratio:", variance_ratio, "\n")
## Explained Variance Ratio: 0.7308674 0.2691326
pca_plot <- ggplot(data.frame(pc_scores), aes(x = PC1, y = PC2)) +
geom_point() +
xlab("Principal Component 1") +
ylab("Principal Component 2") +
ggtitle("PCA Scatter Plot")
pca_plot + stat_ellipse(level = 0.95, col = "blue", type = "t")
