PCA 2D/3D Visualizer

Center data, compute principal directions, and analyze variance capture.

Loading interactive simulation...

Change the units and PCA gives a different answer 🖖

PCA is built on the covariance matrix, and covariance carries units. Record a person’s height in metres and their weight in kilograms and you get one set of components. Switch height to millimetres and the numbers for that variable grow a thousandfold, its variance grows a millionfold, and the first principal component swings round to point almost entirely along it. Nothing about the data changed β€” only the ruler did. This is why real analyses standardise every variable to zero mean and unit variance first, which amounts to running PCA on the correlation matrix rather than the covariance matrix. Unlike regression, PCA has no way to tell a large number from an important one.

The direction your data leans 🖖

PCA hunts for the single line your cloud of points stretches along most, then measures where each point sits on it. The explained-variance percentage tells you how much of the spread that one line keeps. Here, once PC1 climbs past about 85%, the tool announces that a flat 1D summary loses almost nothing. Switch between the sample sets to watch a tightly correlated cloud hand nearly all its variance to PC1.

Not the same as a regression line 🖖

It is tempting to read PC1 as the ordinary line of best fit, but the two optimize different things. Linear regression minimizes the vertical distances to the line, along the y axis; PCA minimizes the perpendicular distances to it. On the same points, the two lines genuinely diverge. Karl Pearson published exactly this perpendicular line of closest fit back in 1901, three decades before the term principal component was coined.

Problems solved in full

  1. Six points with 99.49% of the variance on PC1 and 0.51% on PC2 8 steps

    Six points: (2, 1), (3, 2), (4, 2.2), (5, 3.2), (6, 3.9), (7, 5). The panel reports 99.49% of the variance on PC1 and 0.51% on PC2, which reads like a cloud that is essentially a line. Work out how far off that line the points actually sit.

    1. Centre first, because everything below is about spread and nothing about position. The x-values run 2 to 7 in unit steps, so their mean sits at the middle of the range and the centred x-column is symmetric: Β±0.5, Β±1.5, Β±2.5. Only y needs arithmetic.

    2. The covariance divides the summed products by n βˆ’ 1 = 5, not by 6. One degree of freedom has already been spent estimating the mean that was just subtracted, and with only six points that correction is worth 20%.

    3. Two numbers are enough to get both eigenvalues, and neither of them requires a direction. The trace is the total variance; the determinant measures how far the matrix is from singular. Against a trace of 5.581667, a determinant of 0.156933 is a matrix that is very nearly rank 1.

    4. Solve the characteristic quadratic. Ξ»1 comes out about 200 times Ξ»2 β€” the shape of this cloud compressed into one comparison.

    5. The explained-variance rows are that ratio and nothing else. So 99.49% is a statement about Ξ»1 and Ξ»2, and it inherits what they are: variances, which carry the units of the data squared. Hold that thought.

    6. The direction is the eigenvector, and for a 2Γ—2 covariance matrix with a non-zero off-diagonal you can write it down without solving anything: (Ξ»1 βˆ’ Syy, Sxy) already points along PC1. Scale it to unit length and it is the printed vector.

    7. Turn that a quarter turn to get v2, then project the first centred point onto it. The result, 0.031179, is the second number in the scores table. So that entire column β€” which the panel prints without ever saying what it is β€” is the signed distance of each point from the PC1 line, and the worst of the six is βˆ’0.236855.

    8. Now the question the percentages were hiding. Ξ»2 is a variance, so it is a squared distance, and the typical miss is its square root. Compare that with the square root of Ξ»1, which is the spread along the line.

    Answer

    The points sit about 7.13% of the cloud's length off the PC1 line, not 0.51%. Both figures describe the same six points; one is a ratio of squares and the other a ratio of lengths, and the square root between them is worth a factor of 14 here. That gap is the trap in every rule of the form "keep 99% of the variance": to hold the perpendicular miss under 1% of the spread you need PC2 below 0.01% of the variance, a hundred times stricter than the rule sounds. In data units the miss is √λ2 = 0.168104, and it is not hiding β€” it is drawn on the chart as the grey segment from each green point to its blue projection, and the segments are plainly not of zero length.

  2. PC1 of slope 0.769 and a least squares fit of 0.763 6 steps

    PC1 is printed as (0.792687, 0.609629), a direction of slope 0.769. Fitting y to x by least squares, from the very same covariance entries, gives 2.67/3.5 = 0.763. Two lines through one cloud of six points. Show which is the steeper β€” and that it is always that one.

    1. Least squares on y minimises the vertical gaps, and the minimiser is Sxy/Sxx. Note what is in the denominator: the spread of x alone. The fit takes x as given and asks only what y does in response.

    2. PC1's slope is read straight off the printed unit vector, since a direction (a, b) has slope b/a. It sits about 6 thousandths above the regression slope β€” small, and not zero.

    3. To compare the two properly, write PC1's slope the way the eigenvector defines it rather than as a ratio of decimals. The numerator is the same Sxy in both, so the whole difference lives in the denominator: Sxx for the regression, Ξ»1 βˆ’ Syy for PC1.

    4. Expand Ξ»1. Whether PCA has the smaller denominator has now become a question about a single square root.

    5. And that square root is smaller than the trace exactly when the determinant is positive, which holds for any cloud that is not a perfect straight line. PC1's denominator is therefore always the smaller one, so PC1 is always the steeper of the two. Nothing about these six points was used.

    6. Swap the roles and regress x on y instead. That is a third line, and rewriting its slope for the same y-against-x picture puts it at 0.779650. PC1 does not move under the swap β€” the covariance matrix is symmetric in x and y β€” so the argument of the previous step now runs the other way, and PC1 is the shallower of this pair.

    Answer

    Three lines through one cloud: 0.762857 < 0.769067 < 0.779650, with PC1 strictly in the middle. The two regressions disagree by 2.2% on a cloud the panel calls 99.49% one-dimensional, and they disagree because they answer different questions β€” predict y from x, or predict x from y. PC1 answers neither. It minimises perpendicular distance, so relabelling the axes rotates the picture and leaves the line exactly where it was, and its residuals are the second score column rather than a vertical gap. The three collapse into one only when det C = 0, which is to say when the points really are collinear and there is no longer anything to disagree about. Between those extremes, PCA is the line to use when both coordinates carry measurement error, and the wrong line when you genuinely want a prediction: the regression slope is shallower on purpose, because pulling toward flat is what makes the squared prediction error smallest.

Learning path

Matrices that move things

References (2)

Example problems

  • correlated 2D cloud - A tight positive cloud, r = 0.989, and PC1 keeps 99.49% of the variance. A one-dimensional summary loses half a percent.
  • clustered 2D - Two tight clumps rather than a line - and PC1 still reports 99.51%, the same as the correlated cloud. A high explained variance does not mean the data is linear.
  • balanced spread - The only preset under the 85% mark, at 62.83%, and the only one where PC1 and the regression line visibly disagree: slope -1.00 against -0.26.
  • outlier leverage - Four points in a clump and one at (5.2, 4.8). That single point produces the highest PC1 on the tool, 99.67% - the structure is the outlier.
  • negative slope - A near-perfect negative line: r = -0.9986 and PC1 at 99.93%, the tightest here. PCA reads the direction, and the sign of the slope makes no difference to it.