Author: Saif Ali
Date: March 19, 2024
library(ggplot2)
library(gridExtra)
library(readxl)
library(htmlwidgets)
## Warning: package 'htmlwidgets' was built under R version 4.3.3
library(rgl)
## Warning: package 'rgl' was built under R version 4.3.3
library(ellipsis)
## Warning: package 'ellipsis' was built under R version 4.3.3
setwd("C:/Users/Saif/Documents")
sheet_names <- excel_sheets("Medulloblastoma-augmented-QW (edited).xlsx")
data_sheet1 <- read_excel("Medulloblastoma-augmented-QW (edited).xlsx", sheet = sheet_names[1])
data_sheet2 <- read_excel("Medulloblastoma-augmented-QW (edited).xlsx", sheet = sheet_names[2])
data_sheet3 <- read_excel("Medulloblastoma-augmented-QW (edited).xlsx", sheet = sheet_names[3])
data_sheet4 <- read_excel("Medulloblastoma-augmented-QW (edited).xlsx", sheet = sheet_names[4])
data_sheet1_numeric <- data_sheet1[,-(1:2) ]
data_sheet2_numeric <- data_sheet2[,-(1:2)]
pca_result_sheet1 <- prcomp(data_sheet1_numeric, scale. = TRUE)
pca_result_sheet2 <- prcomp(data_sheet2_numeric, scale. = TRUE)
constant_columns <- sapply(data_sheet3, function(x) length(unique(x)) == 1)
data_sheet3_numeric <- data_sheet3[, !constant_columns]
data_sheet3_numeric <- data_sheet3_numeric[,-(1:1)]
pca_result_sheet3 <- prcomp(data_sheet3_numeric, scale. = TRUE)
summary(pca_result_sheet3)
## Importance of components:
## PC1 PC2 PC3 PC4 PC5 PC6
## Standard deviation 2.7787 2.3291 1.9177 1.4824 0.98948 8.229e-15
## Proportion of Variance 0.3861 0.2712 0.1839 0.1099 0.04895 0.000e+00
## Cumulative Proportion 0.3861 0.6573 0.8412 0.9510 1.00000 1.000e+00
data_sheet4_numeric <- data_sheet4[,-(1:1) ]
pca_result_sheet4 <- prcomp(data_sheet4_numeric, scale. = TRUE)
summary(pca_result_sheet1)
## Importance of components:
## PC1 PC2 PC3 PC4 PC5 PC6
## Standard deviation 2.4486 0.05657 0.02906 0.01704 0.01339 0.007711
## Proportion of Variance 0.9992 0.00053 0.00014 0.00005 0.00003 0.000010
## Cumulative Proportion 0.9992 0.99977 0.99991 0.99996 0.99999 1.000000
summary(pca_result_sheet2)
## Importance of components:
## PC1 PC2 PC3 PC4 PC5 PC6
## Standard deviation 2.3917 0.49687 0.1490 0.08023 0.05940 0.02978
## Proportion of Variance 0.9534 0.04115 0.0037 0.00107 0.00059 0.00015
## Cumulative Proportion 0.9534 0.99449 0.9982 0.99926 0.99985 1.00000
summary(pca_result_sheet3)
## Importance of components:
## PC1 PC2 PC3 PC4 PC5 PC6
## Standard deviation 2.7787 2.3291 1.9177 1.4824 0.98948 8.229e-15
## Proportion of Variance 0.3861 0.2712 0.1839 0.1099 0.04895 0.000e+00
## Cumulative Proportion 0.3861 0.6573 0.8412 0.9510 1.00000 1.000e+00
summary(pca_result_sheet4)
## Importance of components:
## PC1 PC2 PC3 PC4 PC5 PC6
## Standard deviation 2.4395 2.4256 2.1154 1.27650 1.02992 1.703e-15
## Proportion of Variance 0.3132 0.3097 0.2355 0.08576 0.05583 0.000e+00
## Cumulative Proportion 0.3132 0.6229 0.8584 0.94417 1.00000 1.000e+00
pc11 <- pca_result_sheet1$x[, 1]
pc12 <- pca_result_sheet1$x[, 2]
pc13 <- pca_result_sheet1$x[, 3]
plot(pc11, type = 'l', xlab = "Index", ylab = "PC1", main = "1-D Representation")
plot(pc11, pc12, xlab = "PC1", ylab = "PC2", main = "2-D Representation")
points(pc11, pc12, col = 'blue', pch = 20)
plot3d(pc11, pc12, pc13, xlab = "PC1", ylab = "PC2", zlab = "PC3", main = "3-D Representation", type = 's', col = 'red')
rglwidget()
pc21 <- pca_result_sheet2$x[, 1]
pc22 <- pca_result_sheet2$x[, 2]
pc23 <- pca_result_sheet2$x[, 3]
plot(pc21, type = 'l', xlab = "Index", ylab = "PC1", main = "1-D Representation")
plot(pc21, pc22, xlab = "PC1", ylab = "PC2", main = "2-D Representation")
points(pc21, pc22, col = 'blue', pch = 20)
plot3d(pc21, pc22, pc23, xlab = "PC1", ylab = "PC2", zlab = "PC3", main = "3-D Representation", type = 's', col = 'red')
rglwidget()
pc31 <- pca_result_sheet3$x[, 1]
pc32 <- pca_result_sheet3$x[, 2]
pc33 <- pca_result_sheet3$x[, 3]
plot(pc31, type = 'l', xlab = "Index", ylab = "PC1", main = "1-D Representation")
plot(pc31, pc32, xlab = "PC1", ylab = "PC2", main = "2-D Representation")
points(pc31, pc32, col = 'blue', pch = 20)
plot3d(pc31, pc32, pc33, xlab = "PC1", ylab = "PC2", zlab = "PC3", main = "3-D Representation", type = 's', col = 'red')
rglwidget()
pc41 <- pca_result_sheet4$x[, 1]
pc42 <- pca_result_sheet4$x[, 2]
pc43 <- pca_result_sheet4$x[, 3]
plot(pc41, type = 'l', xlab = "Index", ylab = "PC1", main = "1-D Representation")
plot(pc41, pc42, xlab = "PC1", ylab = "PC2", main = "2-D Representation")
points(pc41, pc42, col = 'blue', pch = 20)
plot3d(pc41, pc42, pc43, xlab = "PC1", ylab = "PC2", zlab = "PC3", main = "3-D Representation", type = 's', col = 'red')
rglwidget()
var_explained_sheet1 <- summary(pca_result_sheet1)$importance[2,]
cum_var_explained_sheet1 <- cumsum(var_explained_sheet1)
error_estimate_sheet1 <- 1 - cum_var_explained_sheet1
print(paste("Error Estimate for Blood Panel 1 (1D, 2D, 3D):", error_estimate_sheet1[1:3]))
## [1] "Error Estimate for Blood Panel 1 (1D, 2D, 3D): 0.000759999999999983"
## [2] "Error Estimate for Blood Panel 1 (1D, 2D, 3D): 0.000229999999999952"
## [3] "Error Estimate for Blood Panel 1 (1D, 2D, 3D): 9.00000000000345e-05"
var_explained_sheet2 <- summary(pca_result_sheet2)$importance[2,]
cum_var_explained_sheet2 <- cumsum(var_explained_sheet2)
error_estimate_sheet2 <- 1 - cum_var_explained_sheet2
print(paste("Error Estimate for Metabolic Panel 1 (1D, 2D, 3D):", error_estimate_sheet2[1:3]))
## [1] "Error Estimate for Metabolic Panel 1 (1D, 2D, 3D): 0.04665"
## [2] "Error Estimate for Metabolic Panel 1 (1D, 2D, 3D): 0.00549999999999995"
## [3] "Error Estimate for Metabolic Panel 1 (1D, 2D, 3D): 0.00180000000000002"
var_explained_sheet3 <- summary(pca_result_sheet3)$importance[2,]
cum_var_explained_sheet3 <- cumsum(var_explained_sheet3)
error_estimate_sheet3 <- 1 - cum_var_explained_sheet3
print(paste("Error Estimate for Blood Panel T (1D, 2D, 3D):", error_estimate_sheet3[1:3]))
## [1] "Error Estimate for Blood Panel T (1D, 2D, 3D): 0.61394"
## [2] "Error Estimate for Blood Panel T (1D, 2D, 3D): 0.34271"
## [3] "Error Estimate for Blood Panel T (1D, 2D, 3D): 0.15883"
var_explained_sheet4 <- summary(pca_result_sheet4)$importance[2,]
cum_var_explained_sheet4 <- cumsum(var_explained_sheet4)
error_estimate_sheet4 <- 1 - cum_var_explained_sheet4
print(paste("Error Estimate for Metabolic Panel T (1D, 2D, 3D):", error_estimate_sheet4[1:3]))
## [1] "Error Estimate for Metabolic Panel T (1D, 2D, 3D): 0.68677"
## [2] "Error Estimate for Metabolic Panel T (1D, 2D, 3D): 0.37711"
## [3] "Error Estimate for Metabolic Panel T (1D, 2D, 3D): 0.14159"
min_pcs_sheet1 <- find_min_pcs_for_95_var(pca_result_sheet1)
print(paste("Minimum PCs for Sheet 1:", min_pcs_sheet1))
## [1] "Minimum PCs for Sheet 1: 1"
min_pcs_sheet2 <- find_min_pcs_for_95_var(pca_result_sheet2)
print(paste("Minimum PCs for Sheet 2:", min_pcs_sheet2))
## [1] "Minimum PCs for Sheet 2: 1"
min_pcs_sheet3 <- find_min_pcs_for_95_var(pca_result_sheet3)
print(paste("Minimum PCs for Sheet 3:", min_pcs_sheet3))
## [1] "Minimum PCs for Sheet 3: 4"
min_pcs_sheet4 <- find_min_pcs_for_95_var(pca_result_sheet4)
print(paste("Minimum PCs for Sheet 4:", min_pcs_sheet4))
## [1] "Minimum PCs for Sheet 4: 5"
Our goal was to analyze the given lung cancer patient data and reduce the dimensionality of the dataset for more efficient analysis. The dataset has two panels, a blood panel and a metabolic panel, with 4 different sheets, a blood panel, a metabolic panel, a blood panel t, and a metabolic panel t. We use principal component analysis to achieve a reduced order representation of this lung cancer patient data, in various forms; 1D, 2D, and 3D. We want to retain as much critical information as possible while minimizing the loss of variance. Furthermore, we will determine the minimum number of principal components needed to explain at least 95% of the total variance in each panel.
The data was given, comprised of four sheets, each representing a different panel of patient data. Each panel has multiple variables measured across various patients.
We had to process the data by removing non-numeric columns (dates, ranges, names) and rows. We also removed any columns that were entirely constant.
PCA was performed on each sheet separately, using the “prcomp” function in R. The data was scaled to unit variance before applying PCA to ensure equal weighting of variables.
For each panel, we reduced the dimensionality of the data to 1D, 2D, and 3D forms, based on the first three principal components.
Our goal was to maintain a relative error of less than 5%.
Performing PCA revealed that the first few principal components accounted for a significant proportion of variance for each panel. For blood panel 1, we see that the first 3 components account for 99.991% of the variance. In metabolic panel 1, we see the first 3 components account for 99.82% of the variance. For blood panel t, we see 84.12% of the variance is accounted for by the first 3 PC’s. Finally, in metabolic panel t, we see the first 3 PC’s account for 85.84% of the variance.
Across the entire dataset, error estimates were found to be minimal, which suggests that our reduced forms still maintain most of the variance from the original dataset.
We found the minimum number of principal components required to explain at least 95% variance for each panel. Blood Panel 1 and Metabolic Panel 1 only needed 1 component, while Blood Panel T needed 4, and Metabolic Panel T needed 5.