Computational Provenance & Reproducibility Record

RAISINS - R and AI Solutions for INferential Statistics · Online Statistical Analysis Platform for Agricultural Research

Computational Provenance & Reproducibility Record Principal Component Analysis · 2.0.0 · DOI 10.5281/zenodo.21616137

Computational Provenance & Reproducibility Record

RAISINS · Principal Component Analysis Module

This Computational Provenance Record documents the statistical computing environment, software dependencies, computational provenance, and bibliographic references associated with the RAISINS Principal Component Analysis module. It is intended to support computational reproducibility and software transparency. Detailed statistical methodology, mathematical derivations, and user guidance are provided separately in the official module documentation.

Any issues or updates required please comment here

Go to the App from here

1 Module Metadata

Parameter Specification
Module Principal Component Analysis
Module Version 2.0.0
DOI 10.5281/zenodo.21616137
Document Type Computational workflow
Statistical Engine R
R Version 4.5.2
Reproducibility Execution Environment: Posit Connect · GCR · renv-locked

2 Statistical Dependency Manifest

Package Version Repository Core Statistical Functions
stats 4.5.2 Base R prcomp(), kmeans(), dist(), hclust()
factoextra 1.0.7 CRAN get_eigenvalue(), get_pca_var(), get_pca_ind(), fviz_nbclust()
cluster 2.1.8.1 CRAN silhouette()
scales 1.4.0 CRAN rescale()
plotly 4.10.4 CRAN plot_ly(), 3-D scatter rendering

3 Statistical Function Registry

Analytical Role Primary Function(s)
Principal component extraction stats::prcomp(center = TRUE, scale = TRUE)
Eigenvalues & variance explained factoextra::get_eigenvalue()
Variable/individual coordinates factoextra::get_pca_var(), factoextra::get_pca_ind()
Index score construction res.pca$x (PC scores), scales::rescale() for 0-1 scaling
Cluster overlay on PCA scores (biplot / 3-D scatter) stats::kmeans(); optimal k via cluster::silhouette() (fallback: WSS elbow)
Correlation circle factoextra::get_pca_var()$coord (variable coordinates scaled to unit circle)

4 Default Methods & Parameters

Step Setting Notes
Input data Treatment / genotype means matrix One label column (ID) + numeric traits; requires n > p
Centering / Scaling center = TRUE, scale = TRUE Applied internally by prcomp(); variables are z-standardised prior to decomposition
Sign convention Component scores (x) and loadings (rotation) are sign-reversed after extraction For consistent, human-readable orientation of PC1/PC2 in plots and index scores; does not alter variance explained
Eigenvalues factoextra::get_eigenvalue() Reports eigenvalue, % variance, cumulative % variance per PC
Loadings res.pca$rotation Sign of loadings on PC1/PC2 used to auto-generate variable interpretation text
Index score PC1 and PC2 scores (res.pca$x[,1], res.pca$x[,2]) Each rescaled to [0, 1] via scales::rescale(); default selection cutoff for “top/bottom” treatments is 75th percentile
Cluster overlay (biplot / 3-D scatter) stats::kmeans() on PC1-PC2 (biplot) or PC1-PC3 (3-D scatter) scores k is user-adjustable; auto-suggested value below
Optimal k (cluster overlay) Average silhouette width, evaluated pairwise across (PC1,PC2), (PC1,PC3), (PC2,PC3); first valid result used Falls back to a WSS-elbow rule (which.min(diff(wss))) on (PC1,PC2) if silhouette evaluation fails; falls back to k = 2 if both fail
Correlation circle Variable coordinates from get_pca_var()$coord, optionally rescaled by an arrow-scale factor Radius-1 reference circle; axis labels annotated with % variance explained by PC1/PC2

5 R Code for Key Analytical Steps

The code blocks below demonstrate the exact computation used to obtain every result reported by the RAISINS Principal Component Analysis module, using the USArrests dataset (Murder, Assault, UrbanPop, Rape; 50 U.S. states) built into the datasets package.

5.1 Principal Component Extraction

data(USArrests)
X <- USArrests                        # Murder, Assault, UrbanPop, Rape (all numeric)

res.pca <- prcomp(X, center = TRUE, scale = TRUE)   # PCA via SVD, z-standardised internally

# Sign convention (RAISINS default): reverse sign for consistent orientation
res.pca$x        <- -res.pca$x
res.pca$rotation <- -res.pca$rotation

res.pca$x                              # PC scores (individuals)
res.pca$rotation                       # loadings (variables)
res.pca$sdev                           # standard deviations of the PCs

5.2 Eigenvalues, Loadings, and Index Score

library(factoextra)
library(scales)

# Eigenvalues and % variance explained
eig.val <- get_eigenvalue(res.pca)
rownames(eig.val) <- paste0("PC", seq_len(nrow(eig.val)))
colnames(eig.val) <- c("Eigen value", "Variance(%)", "Cumulative variance")

e1 <- eig.val[1, "Variance(%)"]        # % variance explained by PC1
e2 <- eig.val[2, "Variance(%)"]        # % variance explained by PC2

# Loadings (variable contributions to each PC)
load <- as.data.frame(res.pca$rotation)

# Index score - PC1 and PC2 scores rescaled to [0, 1]
index1 <- res.pca$x[, 1]
index2 <- res.pca$x[, 2]

index1_scaled <- rescale(index1, to = c(0, 1))
index2_scaled <- rescale(index2, to = c(0, 1))

index_1 <- data.frame(`Index Score` = index1, `Scaled Index` = index1_scaled, check.names = FALSE)
index_2 <- data.frame(`Index Score` = index2, `Scaled Index` = index2_scaled, check.names = FALSE)

5.3 Cluster Overlay for Biplot / 3-D Scatter (Optimal k)

library(cluster)

# Use the first 3 PCs (as displayed on the 2D biplot / 3D scatter)
df <- as.data.frame(res.pca$x[, 1:3])

compute_sil <- function(mat) {
  kmax <- min(10, nrow(mat) - 1)
  sil  <- rep(NA, kmax)
  for (k in 2:kmax) {
    km <- kmeans(mat, centers = k)
    s  <- cluster::silhouette(km$cluster, dist(mat))
    sil[k] <- mean(s[, 3])
  }
  which.max(sil)
}

k12 <- compute_sil(df[, c(1, 2)])      # PC1 vs PC2 (2D biplot default)
k13 <- compute_sil(df[, c(1, 3)])      # PC1 vs PC3
k23 <- compute_sil(df[, c(2, 3)])      # PC2 vs PC3

suggested_k <- na.omit(c(k12, k13, k23))[1]

# Fallback: WSS elbow on (PC1, PC2) if silhouette evaluation fails
if (length(suggested_k) == 0) {
  wss <- sapply(1:5, function(k) sum(kmeans(df[, 1:2], centers = k)$withinss))
  suggested_k <- which.min(diff(wss))
}

# Final fallback
if (length(suggested_k) == 0 || is.na(suggested_k) || suggested_k < 2) suggested_k <- 2

# Cluster assignment used on the 2D biplot
km_biplot <- kmeans(df[, c("PC1", "PC2")], centers = suggested_k)

5.4 Biplot Coordinates and Correlation Circle

library(factoextra)

pca_var <- get_pca_var(res.pca)        # variable coordinates, contributions, cos2
pca_ind <- get_pca_ind(res.pca)        # individual coordinates, cos2

df_var <- as.data.frame(pca_var$coord[, 1:2])          # variable arrow endpoints (PC1, PC2)
df_var$contrib <- rowSums(pca_var$contrib[, 1:2])       # combined contribution to PC1+PC2
df_var$cos2    <- rowSums(pca_var$cos2[, 1:2])           # combined quality of representation

df_ind <- as.data.frame(pca_ind$coord[, 1:2])            # individual (treatment) coordinates
df_ind$cos2 <- rowSums(pca_ind$cos2[, 1:2])
df_ind$cluster <- factor(km_biplot$cluster)               # cluster overlay from previous step

# Correlation circle uses the same variable coordinates (PC1, PC2),
# plotted against a unit reference circle
circle_var <- as.data.frame(pca_var$coord[, 1:2])

Explore the entire Principal Component Analysis module in preview mode using our demo datasets. To submit suggestions or report a workflow issue, please use the discussion section below, or visit the official RAISINS website.

6 RAISINS Native Statistical Framework

RAISINS uses R for all its statistical computations. Every package used to generate major results is listed and demonstrated with examples, so results can be reproduced independently. These results are then organized and formatted on the RAISINS website along with visualisation to make them easier to use and interpret. RAISINS also has its own custom-built statistical tools for managing workflows, validating results, and generating reports. Details of these are not fully covered here, they’re shared with outside researchers only on request, and are subject to licensing terms.

7 Package References

R Core Team. (2025). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. https://www.R-project.org/

Kassambara, A., & Mundt, F. (2020). factoextra: Extract and Visualize the Results of Multivariate Data Analyses (R package version 1.0.7). https://doi.org/10.32614/CRAN.package.factoextra

Maechler, M., Rousseeuw, P., Struyf, A., Hubert, M., & Hornik, K. (2025). cluster: “Finding Groups in Data”: Cluster Analysis Extended Rousseeuw et al. (R package version 2.1.8.1). https://doi.org/10.32614/CRAN.package.cluster

Wickham, H., Pedersen, T. L., & Seidel, D. (2023). scales: Scale Functions for Visualization (R package version 1.3.0). https://doi.org/10.32614/CRAN.package.scales

Sievert, C. (2024). plotly: Create Interactive Web Graphics via ‘plotly.js’ (R package version 4.10.4). https://doi.org/10.32614/CRAN.package.plotly

Allaire, J. J., Xie, Y., Dervieux, C., McPherson, J., Luraschi, J., Ushey, K., Atkins, A., Wickham, H., Cheng, J., Chang, W., & Iannone, R. (2026). rmarkdown: Dynamic Documents for R (R package version 2.29). https://github.com/rstudio/rmarkdown

Feedback & Discussion