Skip to contents

Performs Principal Component Analysis (PCA) on targeted quantitative agronomic parameters. Supports dynamic trait mapping to convert trait abbreviations into full descriptive names, and computes modern multivariate metrics including Kaiser-Guttman retention rules, eigenvector loadings, and percentage trait contributions.

Usage

analyze_pca(
  data,
  traits,
  scale = TRUE,
  reporting_level = 2,
  trait_lookup = NULL
)

Arguments

data

A data frame containing genotype information and trait columns.

traits

A character vector specifying the exact trait column names to include.

scale

Logical. If TRUE (default), variables are standardized to unit variance.

reporting_level

Integer. Control output verbosity: 0 (silent), 1 (summary), or 2 (exhaustive full reporting). Defaults to 2.

trait_lookup

An optional named character vector for mapping trait abbreviations to full descriptive labels (e.g., c("PH" = "Plant Height")).

Value

A structured named list containing 6 multivariate components:

pca_object

The raw prcomp output object.

eigenvalues

Data frame of eigenvalues, variance percentages, cumulative variance, and Kaiser retention decision.

loadings

Data frame of eigenvector loadings matrix.

contributions

Data frame of percentage contributions of each trait across components.

cos2

Data frame representing quality of representation (\(Cos^2\)) for each trait.

scores

Data frame of principal component scores assigned to individual genotypes.

Examples

library(AgriDataTools)
data("gv_data", package = "AgriDataTools")

# Define your custom trait mapping before running (Edit names as needed)
custom_traits_map <- c(
   "PH"   = "Plant Height",
   "SL"   = "Spike Length",
   "PL"   = "Peduncle Length",
   "NOT"  = "Number of Tillers",
   "NOSS" = "Number of Spikelets per Spike",
   "TGW"  = "Thousand Grain Weight",
   "GYPM" = "Grain Yield per Meter"
)

# Run Modern PCA with full trait names
pca_results <- analyze_pca(
   data = gv_data,
   traits = names(custom_traits_map),
   trait_lookup = custom_traits_map
)
#> =====================================================================================
#> AGRIDATATOOLS PACKAGED ENGINE v0.1.0 - MODERN PCA DECOMPOSITION PIPELINE
#> Analysis Inception:  2026-08-17 12:02:15.052407 
#> -------------------------------------------------------------------------------------
#> 
#> ---------------------------------------------------------------------------
#>  1. PRINCIPAL COMPONENT EIGENVALUE & VARIANCE SUMMARY (Kaiser Rule)
#> ---------------------------------------------------------------------------
#>  Component Eigenvalue Variance_Percent Cumulative_Percent Retain_Kaiser
#>        PC1     1.8928          27.0395            27.0395 Yes (EV >= 1)
#>        PC2     1.4711          21.0151            48.0546 Yes (EV >= 1)
#>        PC3     1.1068          15.8110            63.8656 Yes (EV >= 1)
#>        PC4     0.9211          13.1585            77.0241            No
#>        PC5     0.6630           9.4719            86.4960            No
#>        PC6     0.5910           8.4435            94.9395            No
#>        PC7     0.3542           5.0605           100.0000            No
#> 
#> ---------------------------------------------------------------------------
#>  2. TRAIT EIGENVECTOR LOADINGS MATRIX
#> ---------------------------------------------------------------------------
#>                                   PC1     PC2     PC3     PC4     PC5     PC6
#> Plant Height                  -0.5584  0.1740 -0.2200 -0.1935  0.4327  0.2659
#> Spike Length                  -0.0640 -0.4276  0.6447 -0.2935 -0.0298  0.5568
#> Peduncle Length               -0.4309  0.2793 -0.1406 -0.4513 -0.6784  0.0453
#> Number of Tillers              0.0784 -0.5320 -0.1955 -0.6335  0.1333 -0.4970
#> Number of Spikelets per Spike -0.3215 -0.5076 -0.0278  0.4725 -0.4630 -0.1548
#> Thousand Grain Weight         -0.2454  0.3200  0.6884 -0.0217  0.0751 -0.5819
#> Grain Yield per Meter         -0.5733 -0.2567 -0.0578  0.2182  0.3375 -0.0872
#>                                   PC7
#> Plant Height                   0.5605
#> Spike Length                  -0.0205
#> Peduncle Length               -0.2248
#> Number of Tillers              0.0806
#> Number of Spikelets per Spike  0.4203
#> Thousand Grain Weight          0.1370
#> Grain Yield per Meter         -0.6580
#> 
#> ---------------------------------------------------------------------------
#>  3. TRAIT CONTRIBUTIONS TO COMPONENTS (% Contribution)
#> ---------------------------------------------------------------------------
#>                                 PC1   PC2   PC3   PC4   PC5   PC6   PC7
#> Plant Height                  31.18  3.03  4.84  3.75 18.72  7.07 31.41
#> Spike Length                   0.41 18.28 41.56  8.62  0.09 31.00  0.04
#> Peduncle Length               18.57  7.80  1.98 20.37 46.02  0.20  5.06
#> Number of Tillers              0.61 28.30  3.82 40.13  1.78 24.70  0.65
#> Number of Spikelets per Spike 10.34 25.76  0.08 22.32 21.43  2.40 17.67
#> Thousand Grain Weight          6.02 10.24 47.39  0.05  0.56 33.87  1.88
#> Grain Yield per Meter         32.87  6.59  0.33  4.76 11.39  0.76 43.29
#> =====================================================================================
#>