Why Projection Must Use the Fitted PCA
After fitting a principal components analysis, a new observation can be expressed in the coordinate system defined by that fit. This is useful when adding records to an existing analysis or creating component-based features for later work. The essential rule is that a new row must be transformed using the training fit's stored centering, scaling, and rotation—not values recalculated from the new row or a new batch.
In R, predict() for an object created by prcomp() applies those stored transformations to new observations. The result is a set of component scores: coordinates that describe each new observation relative to the component directions learned from the training data. This tutorial is aligned with the SOA Exam PA (Predictive Analytics) syllabus.
The Projection Calculation
Suppose the fitted analysis used variables in the order \(X_1,\ldots,X_p\). With centering and scaling, the transformed value for variable \(j\) in a new observation is \(z_j=(x_j-c_j)/s_j\). With centering but no scaling, it is \(z_j=x_j-c_j\). If centering was disabled, there is no center to subtract. The stored rotation matrix has one column per component returned by the fit; its \(k\)th column contains the coefficients for component \(k\).
Here \(v_{jk}\) is the entry in row \(j\), column \(k\) of the rotation matrix. The calculation is a row-vector times a matrix. Each component score is therefore a dot product of the transformed observation and one component's coefficient vector.
The reason this gives the correct coordinates follows from the orthonormality of the fitted component directions. If \(V\) is the full square rotation matrix, then \(V^\mathsf{T}V=I\), and a transformed row can be written as \(z=(zV)V^\mathsf{T}\). Thus \(zV\) gives its coordinates along those directions. If the fit retains fewer than \(p\) components, \(zV\) gives coordinates only along the retained directions; it does not provide coordinates for omitted components.
New data must contain the same numeric variables used for the fit, with columns in the same order.
Subtract the fitted centers and divide by the fitted scales only as specified by the fitted object.
Compute the row of scores as the transformed data matrix multiplied by the fitted rotation matrix.
Read each score as a coordinate along a fitted direction, then check the arithmetic and the requested component columns.
The practical R call is predict(fit, newdata = newdata), where fit is the fitted prcomp object. For a data frame, predict.prcomp converts the supplied columns to a matrix and uses their positions with the stored centers, scales, and rotation. It does not reorder columns to match variable names. A data frame with correct names but incorrect column order can therefore produce incorrect scores without an obvious warning.
The stored scale depends on the original fit. When center = TRUE and scale. = TRUE, the scale for a variable is its sample standard deviation. When center = FALSE and scale. = TRUE, R's scaling uses the square root of the sum of squared values divided by \(n-1\), not \(n\). When scale. is a numeric vector, its supplied values are the divisors. Do not substitute a newly calculated center or scale during projection.
Worked Example: Projecting with Stored Centers, Scales, and Rotation
A fitted PCA uses variables \(M,N\), in that order. Its stored centers are \(c_M=10\) and \(c_N=20\), its stored numeric scales are \(s_M=2\) and \(s_N=5\), and its rotation matrix is \(V=\begin{pmatrix}0.8&-0.6\\0.6&0.8\end{pmatrix}\). For a new observation \(M=14,N=25\), calculate both scores. Then determine the first score if the same values are supplied in the reversed column order \(N,M\).
Method. Apply the fit's stored center and scale in the fitted variable order, then multiply by the stored rotation. The numeric scales here are explicitly supplied divisors; they are not recalculated from the new observation.
Correct column order. The transformed values are \(z_M=(14-10)/2=4/2=2\) and \(z_N=(25-20)/5=5/5=1\). Thus \(z=(2,1)\). The first score is \(2(0.8)+1(0.6)=1.6+0.6=2.2\). The second score is \(2(-0.6)+1(0.8)=-1.2+0.8=-0.4\). The projected score row is therefore \((2.2,-0.4)\).
Reversed columns. If the values are supplied positionally as \(N=25,M=14\), the function treats 25 as the first fitted variable and 14 as the second. The transformed row becomes
\(((25-10)/2,(14-20)/5)=(15/2,-6/5)=(7.5,-1.2)\).
Its first score is \(7.5(0.8)+(-1.2)(0.6)=6-0.72=5.28\). This is not the desired projection of \(M=14,N=25\). The correction is to reorder the data frame to \(M,N\) before calling predict().
Check. With the correct order, the first score is the dot product \(2(0.8)+1(0.6)=2.2\), matching the calculation above. The columns of the stated rotation are orthogonal unit vectors, so the rotation is consistent with a two-component coordinate transformation. The common mistake is assuming that matching column names makes reversed columns safe.
R Scaling When Centering Is Disabled
The denominator in the stored scale matters. For a variable with training values \(x_1,\ldots,x_n\), when centering is disabled and scaling is requested, the scale used by prcomp is
\(\sqrt{\sum_{i=1}^{n}x_i^2/(n-1)}\). This differs from a root mean square with denominator \(n\). A wrong denominator changes every projected score for that variable.
Worked Example: A One-Variable Projection Without Centering
A one-variable training matrix contains the values \(3,4,0\). PCA is fitted with center = FALSE and scale. = TRUE. The fitted rotation is 1. Project the new value 5.
Method. Use the scale stored by this no-centering fit, then multiply by the one-variable rotation. There is no mean subtraction because centering was disabled.
There are \(n=3\) training values. The sum of squares is \(3^2+4^2+0^2=9+16+0=25\). The stored scale is \(s=\sqrt{25/(3-1)}=\sqrt{25/2}=5/\sqrt{2}\), approximately \(3.5355\).
The transformed new value is \(5/s=5/(5/\sqrt{2})=\sqrt{2}\), approximately \(1.4142\). The one-variable rotation is 1, so the component score is \(1.4142\), rounded to four decimal places.
Check. Multiplying the score by the scale returns the original value: \(\sqrt{2}(5/\sqrt{2})=5\). Using \(n=3\) in the scale denominator would instead give \(\sqrt{25/3}\) and a different, incorrect score. The denominator for this prcomp setting is \(n-1\).
Interpreting Scores for Several New Observations
A score's sign and magnitude are relative to the directions and preprocessing learned by the fitted PCA. A positive score means the transformed observation lies in the positive direction of that component; a negative score means it lies in the opposite direction. A score near zero indicates little displacement along that direction. These are coordinate interpretations, not labels such as “good,” “high risk,” or “unusual” unless the application independently defines such a meaning.
When all components are retained, orthonormality provides a useful check: the sum of squared scores equals the squared length of the transformed observation. When only some components are retained, the sum of squared retained scores cannot exceed that squared length. This check is about the coordinates in transformed space; it does not undo centering or scaling.
Worked Example: Comparing Two Three-Variable Score Rows
A fitted PCA uses variables \(A,B,C\) in that order, with centers \((50,8,12)\) and scales \((10,2,4)\). It retains the following three orthonormal rotation columns: \(v_1=(1,1,0)/\sqrt{2}\), \(v_2=(1,-1,2)/\sqrt{6}\), and \(v_3=(1,-1,-1)/\sqrt{3}\). Project observations \(U=(70,8,16)\) and \(W=(40,10,12)\), and compare their coordinates.
Observation \(U\). Its transformed row is \(((70-50)/10,(8-8)/2,(16-12)/4)=(2,0,1)\). The component scores are the dot products: \(PC1=(2+0)/\sqrt{2}=\sqrt{2}\), approximately \(1.4142\); \(PC2=(2-0+2)/\sqrt{6}=4/\sqrt{6}\), approximately \(1.6330\); and \(PC3=(2-0-1)/\sqrt{3}=1/\sqrt{3}\), approximately \(0.5774\).
Observation \(W\). Its transformed row is \(((40-50)/10,(10-8)/2,(12-12)/4)=(-1,1,0)\). Its scores are \(PC1=(-1+1)/\sqrt{2}=0\), \(PC2=(-1-1+0)/\sqrt{6}=-2/\sqrt{6}\), approximately \(-0.8165\), and \(PC3=(-1-1-0)/\sqrt{3}=-2/\sqrt{3}\), approximately \(-1.1547\).
Interpretation and check. \(U\) has a positive coordinate on all three displayed directions, while \(W\) has zero score on the first and negative scores on the other two. For \(U\), the squared transformed length is \(2^2+0^2+1^2=5\); the squared scores sum to \(2+16/6+1/3=2+8/3+1/3=5\). For \(W\), the transformed squared length is \((-1)^2+1^2+0^2=2\); its squared scores sum to \(0+4/6+4/3=2\). The equalities verify the projections. The common mistake is interpreting a score's sign without reference to the fitted direction; signs describe coordinates, not an absolute judgment.
Practical Checks and Common Traps
Projection is not a new PCA fit. Re-running prcomp() on the new observations would estimate new centers, scales, and directions, so the resulting scores would be in a different coordinate system. To compare new records with earlier scores, use the original fitted object. Similarly, do not standardize the new data with its own sample means and standard deviations: even if that seems natural for a new batch, it does not reproduce the fitted transformation.
Before interpreting output, confirm the variable identities and order, confirm whether the fit centered or scaled, and check whether the requested output includes all available components or only selected columns. If the input is a matrix, column positions still matter. If it is a data frame, names help you reorder it correctly, but do not rely on names to make the prediction function reorder it automatically. New observations also need usable numeric values; a missing or nonnumeric value cannot be treated as an ordinary complete row for the matrix multiplication.
- Wrong column order: reorder new data explicitly to the fit's variable order before prediction.
- Recomputing preprocessing: use the fitted centers and scales rather than statistics from new data.
- Wrong scale denominator: with centering disabled and scaling enabled, use \(n-1\) in the scale calculation.
- Reading scores as original units: scores are coordinates in component space, not measurements in the original variables' units.
- Confusing projection with refitting: prediction applies existing component directions; it does not estimate new ones.
prcomp object apply its stored preprocessing and rotation. The resulting scores are coordinates relative to that fit.
Check Your Understanding
For each problem, apply the stated fitted preprocessing before calculating the score.
- A one-variable PCA was fitted to training values \(2,4,6\) with centering enabled and scaling disabled. Its rotation is 1. What is the score for a new value of 7? (Answer: 3)
- A one-variable PCA was fitted to \(-2,1,5\) with centering disabled and scaling enabled; its rotation is 1. Project the value 2. (Answer: \(2/\sqrt{15}\approx0.5164\))
- A fitted two-variable PCA has centers \((10,20)\), numeric scales \((2,5)\), and first rotation column \((0.8,0.6)\), with variables ordered \(M,N\). Find the first score for \(M=12,N=15\). (Answer: 0.2)
- Use the fit described in the first worked example. If a data frame supplies the columns in order \(N,M\) with values \(25,14\), what first score does the positional calculation produce, and what should be done to obtain the intended projection? (Answer: 5.28; reorder the columns to \(M,N\) before prediction)
- A fitted two-variable PCA has first rotation column \((0.6,0.8)\) and second column \((-0.8,0.6)\). A new observation, after the fit's preprocessing, has transformed row \((1,2)\). Give both component scores. (Answer: \((2.2,0.4)\))