Tutorials › Exam PA - Predictive Analytics › Centering and Scaling with prcomp

Exam PA · Principal Components Analysis · Tutorial 624 of 966

Centering and Scaling with prcomp

Use prcomp’s centering and scaling options deliberately, and understand how those choices affect the variation used in PCA.

Advanced 10 min read

What You'll Learn

  • Explain the defaults for center and scale. in prcomp
  • Distinguish covariance-based PCA from correlation-based PCA
  • Calculate centered values and sample-standardized values
  • Use explicit numeric centers and scales in prcomp
  • Recognize when an uncentered analysis answers a different question
  • Check for constant columns and other scaling problems

Why the Centering and Scaling Settings Matter

In principal components analysis (PCA), the matrix supplied to the calculation determines which directions count as having the greatest variation. With R’s prcomp function, the arguments center and scale. control how that matrix is prepared. The defaults center each column but do not scale it. That is often sensible when variables use comparable units, but can make a variable with large numerical values dominate when units or spreads differ substantially.

The choice is a modeling decision, not merely an R syntax detail. Centering makes the analysis about variation around column means. Scaling gives columns comparable variance, which is useful when relative patterns matter more than original units. Neither choice is automatically right for every application.

Definition: For an \(n \times p\) data matrix \(X\), let \(x_{ij}\) denote the value of variable \(j\) for observation \(i\). Centering column \(j\) subtracts its mean \(\bar{x}_j\) from every entry. Scaling after centering divides each centered entry by a positive scale \(s_j\), commonly the sample standard deviation of that column. With the usual settings, prcomp(X) uses center = TRUE and scale. = FALSE: it centers the columns but leaves their original units and scales unchanged.

When center = TRUE and scale. = TRUE, prcomp divides each centered column by its sample standard deviation, computed with denominator \(n-1\). The resulting columns have sample mean zero and sample variance one, provided none is constant. This is the standardization used for correlation-based PCA. With center = TRUE and scale. = FALSE, the components instead reflect the covariance structure in the original units.

The arguments can also take numeric vectors. A numeric center supplies one center per column, and a numeric scale. supplies one nonzero divisor per column. These values let an analyst use defined reference values or meaningful scales instead of the data’s estimated means and standard deviations. The vectors must correspond to the columns in the same order.

What the prcomp Settings Do

CallTransformationTypical interpretation
prcomp(X)Subtract each column mean; do not divide by a scaleCovariance-based PCA in the original units
prcomp(X, center = TRUE, scale. = TRUE)Subtract each mean and divide by each sample standard deviationCorrelation-based PCA, giving each variable unit sample variance
prcomp(X, center = FALSE, scale. = FALSE)Do not subtract means or divide by scalesAnalysis of variation relative to the raw zero point; not ordinary centered PCA
prcomp(X, center = c(...), scale. = c(...))Subtract the supplied centers and divide by the supplied scalesPCA using explicitly defined reference values and units

The transformation can be written entry by entry as \(z_{ij}=(x_{ij}-c_j)/s_j\), where \(c_j\) is the chosen center and \(s_j\) is the chosen divisor. If scaling is turned off, take \(s_j=1\). If centering is turned off, take \(c_j=0\). The resulting matrix \(Z\) is the matrix whose variation prcomp analyzes.

Why Standardization Produces a Correlation Matrix

The relationship between standardization and correlation-based PCA follows directly from the sample covariance formula. Suppose each column is centered by its sample mean and divided by its sample standard deviation \(s_j\). The sample covariance between transformed columns \(j\) and \(k\) is

$$ \frac{1}{n-1}\sum_{i=1}^{n} \frac{x_{ij}-\bar{x}_j}{s_j} \frac{x_{ik}-\bar{x}_k}{s_k} = \frac{\operatorname{Cov}(X_j,X_k)}{s_js_k} = \operatorname{Corr}(X_j,X_k). $$

In particular, setting \(j=k\) gives a sample variance of one for each standardized column. Thus the sample covariance matrix of the standardized data is the sample correlation matrix of the original variables. This is why the settings center = TRUE and scale. = TRUE are described as correlation-based PCA. The result assumes nonconstant columns, so every \(s_j\) is positive.

Worked Example: Comparing Covariance-Based and Correlation-Based PCA

A hypothetical portfolio data set has four observations on annual claim count and average claim amount, recorded in thousands of dollars. The columns are claim count \(C=(0,1,1,2)\) and claim amount \(A=(10,20,20,30)\). Calculate the centered and standardized columns, then compare the two common prcomp settings.

Method. First calculate the column means and sample standard deviations. Then compare the covariance matrix left by the default settings with the covariance matrix after standardization.

The means are \(\bar C=(0+1+1+2)/4=1\) and \(\bar A=(10+20+20+30)/4=20\). Thus the centered columns are \(C-\bar C=(-1,0,0,1)\) and \(A-\bar A=(-10,0,0,10)\).

For claim count, the sum of squared centered values is \(1+0+0+1=2\), so its sample variance is \(2/(4-1)=2/3\) and its sample standard deviation is \(\sqrt{2/3}\). For claim amount, the sum is \(100+0+0+100=200\), so its sample variance is \(200/3\) and its sample standard deviation is \(\sqrt{200/3}=10\sqrt{2/3}\).

The sample covariance is \(\{(-1)(-10)+0+0+(1)(10)\}/3=20/3\). Therefore the sample covariance matrix, in the order claim count then claim amount, is

$$ \begin{pmatrix} 2/3 & 20/3\\ 20/3 & 200/3 \end{pmatrix}. $$

The default call, prcomp(X), centers but does not scale, so this is the covariance matrix relevant to its PCA. The claim-amount variance is 100 times the claim-count variance: \((200/3)/(2/3)=100\). The amount column therefore contributes much larger numerical variation in the original units.

With scale. = TRUE, the centered claim-count values become \((-1,0,0,1)/\sqrt{2/3}\), and the centered claim-amount values become \((-10,0,0,10)/(10\sqrt{2/3})\). Both columns are \((-\sqrt{3/2},0,0,\sqrt{3/2})\). Their standardized covariance matrix is consequently

$$ \begin{pmatrix} 1 & 1\\ 1 & 1 \end{pmatrix}. $$

Check. Each standardized column has squared values summing to \(3/2+3/2=3\); dividing by \(n-1=3\) gives variance one. The two standardized columns match exactly, so their sample correlation is one. This confirms both the calculation and the effect of scale. = TRUE.

Common mistake avoided: The mean claim count is 1, not 0.5. Using 0.5 would give incorrect centered values and would corrupt the standard deviation and every later calculation. The method here recomputes the mean from all four observations before centering.

Centering Is Not Optional by Default

R’s default does center, even though it leaves the scales alone. Turning centering off changes the question: instead of finding directions of variation around the column means, the calculation is influenced by the columns’ positions relative to zero. This can be inappropriate when zero is an arbitrary reference point rather than a meaningful baseline.

Worked Example: Seeing the Effect of Turning Centering Off

A hypothetical system records two monthly measurements, \(X=(1,2,3)\) and \(Y=(10,11,12)\). Compare the cross-products of the raw matrix with those of the centered matrix, which determine the directions used by unscaled prcomp.

Method. For centered PCA, subtract the sample mean from each column before forming the cross-product matrix. For the uncentered call, retain the raw entries. The cross-product matrix is the matrix product of the data matrix transpose and the data matrix.

The means are \(\bar X=2\) and \(\bar Y=11\). The centered rows are \((-1,-1),(0,0),(1,1)\). Their cross-product matrix is

$$ \begin{pmatrix} (-1)^2+0^2+1^2 & (-1)(-1)+0+1\\ (-1)(-1)+0+1 & (-1)^2+0^2+1^2 \end{pmatrix} = \begin{pmatrix} 2&2\\ 2&2 \end{pmatrix}. $$

The raw rows are \((1,10),(2,11),(3,12)\). Without centering, their cross-product matrix is

$$ \begin{pmatrix} 1^2+2^2+3^2 & 1(10)+2(11)+3(12)\\ 1(10)+2(11)+3(12) & 10^2+11^2+12^2 \end{pmatrix} = \begin{pmatrix} 14&68\\ 68&365 \end{pmatrix}. $$

The raw cross-products are much larger and depend on the chosen zero point, despite the columns having identical centered patterns. The default call prcomp(X) centers and analyzes the centered pattern; prcomp(X, center = FALSE, scale. = FALSE) does not. The latter is appropriate only when variation relative to zero is genuinely the intended analysis.

Check. The centered sample covariance matrix is the centered cross-product matrix divided by \(n-1=2\), namely \(\begin{pmatrix}1&1\\1&1\end{pmatrix}\). This agrees with both columns having sample variance one and covariance one.

Common mistake avoided: Setting center = FALSE just to “keep the original data” does not preserve the usual covariance-PCA interpretation. It changes the analyzed matrix and can make arbitrary offsets influential.

Using Explicit Centers and Scales

A numeric center or scale. is useful when the analysis has a stated reference point or a chosen unit of comparison. For instance, an analyst may want differences expressed per two units of one measurement and per 100 units of another. Such divisors need not equal the sample standard deviations. They encode the analyst’s chosen scale, so their meaning should be documented.

Worked Example: Applying Chosen Centers and Scales

A hypothetical data set contains two numeric measures, \(U=(2,4,6,8)\) and \(V=(100,200,300,400)\). Use centers 5 and 250, and divisors 2 and 100, respectively. Then compare these transformed values with the result of using scale. = TRUE.

Method. Apply \(z=(x-c)/s\) to every entry using the supplied center and divisor. Then calculate the sample standard deviations to determine the unit-standard-deviation transformation.

The call is prcomp(X, center = c(5, 250), scale. = c(2, 100)), with columns ordered \(U,V\). For \(U\), the transformed values are \((2-5)/2=-1.5\), \((4-5)/2=-0.5\), \((6-5)/2=0.5\), and \((8-5)/2=1.5\). For \(V\), they are \((100-250)/100=-1.5\), \((200-250)/100=-0.5\), \((300-250)/100=0.5\), and \((400-250)/100=1.5\). The transformed rows are therefore \((-1.5,-1.5),(-0.5,-0.5),(0.5,0.5),(1.5,1.5)\).

The sample means of both transformed columns are zero. Each has sum of squared values \(2.25+0.25+0.25+2.25=5\), so each sample variance is \(5/3\), not one. This is expected: the supplied divisors are chosen scales, not the sample standard deviations.

For comparison, the sample standard deviation of \(U\) is \(\sqrt{(9+1+1+9)/3}=\sqrt{20/3}\), and that of \(V\) is \(\sqrt{(22500+2500+2500+22500)/3}=\sqrt{50000/3}\). Applying scale. = TRUE after centering gives values \((-3,-1,1,3)/\sqrt{20/3}\) in the first column. The second column gives \((-150,-50,50,150)/\sqrt{50000/3}\). Both reduce to \((-\sqrt{27/20},-\sqrt{3/20},\sqrt{3/20},\sqrt{27/20})\), approximately \((-1.1619,-0.3873,0.3873,1.1619)\). Each then has sample variance one.

Check. The two columns have the same standardized values because \(V=50U\) in this data set and both columns were centered at their sample means. The custom divisors produce equal columns too, but with sample variance \(5/3\). This verifies the distinction between chosen units and unit-variance standardization.

Common mistake avoided: A numeric scale. vector is not a request to standardize automatically. Each column is divided by exactly the supplied value; unit sample variance follows only when that value is the corresponding sample standard deviation.

A Practical Choice of Settings

1
Confirm the input.
Supply a numeric matrix with observations in rows and variables in columns. Missing values, infinite values, or nonnumeric columns must be addressed before the PCA calculation.
2
Decide whether the zero point matters.
For ordinary PCA of variation about the data’s means, retain center = TRUE. Turn it off only when an analysis around the raw zero point is intended.
3
Decide whether original spreads should set influence.
Use scale. = FALSE when differences in original units and variance are substantively meaningful and units are comparable. Use scale. = TRUE when variables should contribute on a common unit-variance basis.
4
Check every column before scaling.
A constant column has sample standard deviation zero, so it cannot be divided by that standard deviation. Remove or otherwise handle such a column; do not expect scale. = TRUE to create information that is not present.
5
Record the transformation.
State whether columns were centered, how they were scaled, and why. This makes the PCA specification reproducible and keeps its interpretation tied to the intended question.

A practical R specification makes both choices visible, for example prcomp(X, center = TRUE, scale. = TRUE) or prcomp(X, center = TRUE, scale. = FALSE). Writing the arguments explicitly avoids relying on memory of defaults and communicates whether the intended analysis is correlation-based or covariance-based.

The central trade-off is straightforward: covariance-based PCA preserves original units and allows high-variance variables to exert more influence; correlation-based PCA removes differences in units and marginal variance, so relative patterns across variables have more equal standing. Standardization is not a universal improvement. If a variable’s larger variance is meaningful to the task, scaling it to unit variance may suppress useful information. Conversely, leaving measurements in incomparable units can cause numerical magnitude to drive the result rather than the intended structure.

Key takeaway: prcomp centers by default and does not scale by default. Choose center and scale. based on the reference point and variable comparisons that make sense for the analysis. For standard correlation-based PCA, use center = TRUE and scale. = TRUE; verify that every column has positive sample standard deviation.

Check Your Understanding

For each problem, identify the transformation or calculation required before interpreting any PCA result.

  1. A numeric column is \((2,4,6)\). Find its mean, centered values, and sample standard deviation. Give the centered-and-standardized values to four decimals. (Answer: Mean \(=4\); centered values \((-2,0,2)\); sample standard deviation \(=2\); standardized values \((-1,0,1)\).)
  2. A matrix has columns measured in dollars and years. An analyst wants each column to have sample variance one before PCA. Which prcomp settings should be used, and what covariance matrix does the transformed data represent? (Answer: Use center = TRUE and scale. = TRUE; the transformed sample covariance matrix is the original variables’ sample correlation matrix.)
  3. A column has values \((3,3,3,3)\). What happens if scale. = TRUE is requested, and why? (Answer: The column has sample standard deviation zero, so it cannot be rescaled to unit variance; remove or otherwise handle the constant column before scaling.)
  4. For \(W=(1,3,5)\), apply center \(=2\) and scale. \(=2\). Give the transformed column and its sample variance. (Answer: \((-0.5,0.5,1.5)\); sample variance \(=1\).)
  5. A data matrix has two columns \((0,1,2)\) and \((100,101,102)\). An analyst wants PCA of variation around each column’s mean while retaining the original units. State the call and give the centered columns. (Answer: Use prcomp(X, center = TRUE, scale. = FALSE); the centered columns are \((-1,0,1)\) and \((-1,0,1)\).)
Clinical Biostats