| plot.princomp.BData | R Documentation |
A versatile plotting function for visualizing principal component analysis results on distributional data. Provides multiple plot types for comprehensive exploration of the PCA results, including the unique ability to visualize loadings as functional perturbations of the mean distribution.
## S3 method for class 'princomp.BData'
plot(
x,
type = c("screeplot", "variance", "scores", "eigenfunctions", "loadings", "biplot",
"covariance", "reconstruction", "boxplot"),
choices = 1:2,
scale = 1,
pc.biplot = FALSE,
main = NULL,
col = NULL,
lwd = 1,
lty = 1,
legend = TRUE,
labels = FALSE,
...,
npcs = min(10, length(x$sdev)),
as = c("PDF", "density", "logPDF", "logDensity", "clr", "coef"),
scale.sdev = 1
)
x |
An object of class 'princomp.BData' as returned by
|
type |
Character string specifying the type of plot to produce:
|
choices |
Integer vector of length 2 specifying which principal components to plot (for 'type="scores"' and 'type="biplot"'). Default is 'c(1, 2)'. |
scale |
Scaling factor for loadings in biplot (passed to
|
pc.biplot |
Logical. If ‘TRUE', use Gabriel’s biplot scaling. Default is 'FALSE'. |
main |
Character string for plot title. If 'NULL', an appropriate default is generated based on the plot type. |
col |
Colors for plotting. For loadings plot: first color is for the mean, subsequent colors for each PC. For scores plot: color for points/labels. |
lwd |
Line width for functional plots (loadings). Default is 1. |
lty |
Line type (currently not directly used; solid for positive direction, dashed for negative). |
legend |
Logical. If 'TRUE' (default), add a legend to the loadings plot. |
labels |
For 'type="scores"': Controls labeling of observations:
This helps identify which distribution each point represents. |
... |
Additional graphical parameters passed to underlying plot functions. |
npcs |
Number of principal components to display in screeplot, variance plot, or loadings plot. Default is 'min(10, length(x$sdev))'. |
as |
For loadings plot: how to display the distributions. One of:
|
scale.sdev |
Numeric scaling factor for the loadings plot. Controls how far from the mean the perturbed distributions are shown:
|
## Understanding the Eigenfunctions Plot
The 'type="eigenfunctions"' plot is the most important visualization for interpreting SFPCA results on distributional data. It answers the question: "How does the distribution shape change as we move along each principal component?"
The plot shows:
Black thick line: The mean distribution (center of the data).
Colored solid lines: Distributions at +'scale.sdev' standard deviations from the mean along each PC direction (positive perturbation).
Colored dashed lines: Distributions at -'scale.sdev' standard deviations from the mean along each PC direction (negative perturbation).
For example, if PC1 captures variation in the "location" of distributions, the PC1+ line will be shifted right and PC1- will be shifted left compared to the mean. If PC1 captures variation in "spread", PC1+ will be wider and PC1- will be narrower.
## Choosing the Number of Components
Use the screeplot and variance plot to decide how many components are meaningful:
Screeplot: Look for an "elbow" where the variance drops off. Components before the elbow are typically retained.
Variance plot: Choose enough components to explain a target percentage (e.g., 80% or 90%) of total variance.
Invisibly returns the data used for plotting (varies by plot type).
Matthias Templ
princomp.BData for performing the PCA.
biplot.princomp.BData for detailed control over biplots.
screeplot.princomp.BData for the standalone screeplot function.
Other PCA:
biplot.princomp.BData(),
plot.princomp.BData.grouped(),
plot.princomp.BData.multivariate(),
predict.princomp.BData(),
princomp.BData(),
screeplot.princomp.BData(),
summary.princomp.BData()
## Setup: Create PCA result for examples
meanDist <- BDataPDFs(list(dunif), basis = B2Unif01Basis7)
gaussProc <- B2GaussianDistribution(meanDist, function(x,y) exp(-4*abs(x-y)^1.7))
pcaResult <- princomp(as.BDataSplines(simulate(gaussProc, 30, seed = 42)))
## Screeplot: Decide how many components to keep
plot(pcaResult, type = "screeplot")
# Interpretation: The bars show the percentage of total variance explained by
# each PC. Here, PC1 and PC2 dominate, suggesting that the main structure in
# the data can be captured by just two components. The "elbow" after PC2 or
# PC3 indicates where additional components add little explanatory power.
## Cumulative variance: What percentage is explained?
plot(pcaResult, type = "variance")
# Interpretation: This shows how total explained variance accumulates as more
# components are included. The dashed reference lines help identify how many
# components are needed to reach common thresholds (80%, 90%, 95%). Here,
# approximately 3 components explain over 90% of the variance.
## Score plot: Explore structure in the data
plot(pcaResult, type = "scores")
# Interpretation: Each point is one distribution projected onto PC1 and PC2.
# The spread along PC1 (x-axis) shows variation in the first mode; spread
# along PC2 (y-axis) shows the second mode. Clusters indicate groups of
# similar distributions; outliers are distributions with unusual shapes.
## Score plot with labels: Identify individual distributions
plot(pcaResult, type = "scores", labels = TRUE)
# Using labels=TRUE replaces points with observation numbers (1, 2, 3, ...),
# allowing you to identify which specific distribution each position
# represents. This is useful for tracking outliers or interesting cases
# back to the original data.
## Eigenfunctions plot: THE KEY INTERPRETIVE PLOT
plot(pcaResult, type = "eigenfunctions", npcs = 2)
# Interpretation: The black curve is the mean (average) distribution. For
# each PC, solid lines show how distributions look when they score HIGH on
# that component, dashed lines show LOW scores. Compare the colored curves
# to the black mean to see what "high PC1" or "low PC2" means in terms of
# actual distribution shape (e.g., shifted location, changed spread, altered
# skewness, or different modality).
## More extreme perturbations (2 standard deviations)
plot(pcaResult, type = "eigenfunctions", npcs = 2, scale.sdev = 2)
# Using scale.sdev=2 shows more extreme deviations from the mean, making
# the differences between PC directions more visible.
## Show loadings as log-PDFs (useful for seeing tail behavior)
plot(pcaResult, type = "loadings", npcs = 2, as = "logPDF")
# The log-PDF representation emphasizes differences in the tails of the
# distributions, which can be important for detecting outlier behavior.
## Show loadings in CLR (centered log-ratio) representation
plot(pcaResult, type = "loadings", npcs = 2, as = "clr")
# The CLR representation shows the additive structure in Bayes space.
# Values are centered around zero, making it easy to see which parts of the
# distribution are above or below the geometric mean. This is the natural
# representation for PCA in Bayes space.
## Covariance surface: See which parts of the domain co-vary
plot(pcaResult, type = "covariance")
# Interpretation: This heatmap shows functional covariance Cov(f(s), f(t))
# across the domain. High values (red) indicate that when one part of the
# distribution is above average, the other tends to be as well. The diagonal
# shows variance at each point. Off-diagonal patterns reveal correlation
# structure (e.g., if left and right tails move together or oppositely).
## Reconstruction: How well do k PCs approximate the data?
plot(pcaResult, type = "reconstruction", npcs = 2)
# Shows all distributions reconstructed using only the first 2 PCs.
# Compare to the original data to assess information loss.
# If reconstructions look similar to originals, 2 PCs are sufficient.
plot(pcaResult, type = "reconstruction", npcs = 1)
# Using only 1 PC shows more loss of detail - useful for understanding
# what information each additional PC captures.
## Boxplot: Distribution of scores across PCs
plot(pcaResult, type = "boxplot", npcs = 4)
# Shows the spread of observations along each PC direction.
# Useful for identifying outliers (points beyond whiskers) and comparing
# the amount of variation captured by each component. The percentage
# labels show variance explained by each PC.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.