library(ggplot2) library(ggfortify)
library(dplyr)
df<-read.csv(“C:/Users/Kavyadinesh/Downloads/wine.csv”) df
numeric_data <- df[, 3:9] # Selecting numeric columns only
numeric_data_cleaned <- numeric_data %>% mutate_if(is.factor, as.numeric)
pca_result <- prcomp(numeric_data_cleaned, scale. = TRUE)
summary(pca_result)
autoplot(pca_result, data = df, label = TRUE, label.size = 3, loadings = TRUE, loadings.label = TRUE, loadings.label.size = 3) + labs(title = “PCA Biplot”)
variance_explained <- pca_result\(sdev^2 / sum(pca_result\)sdev^2)
pc_df <- data.frame(PC = paste0(“PC”, 1:length(variance_explained)), Variance = variance_explained)
scree_plot <- ggplot(pc_df, aes(x = PC, y = Variance)) + geom_point() + geom_line(aes(group = 1)) + # Ensure the line connects points labs(title = “Scree Plot”, x = “Principal Component”, y = “Proportion of Variance Explained”) + theme_minimal() + theme(axis.text.x = element_text(angle = 45, hjust = 1)) # Adjust x-axis text for readability
print(scree_plot)