Principal Component Analysis on a CSV
Operator: Abdulla Al Fahad. How tools are built and checked
Six test scores for 120 students (maths, physics, chemistry, reading, writing, history) reduce to two components that carry 83.8% of the variance: the first alone 50.5% (eigenvalue 3.03) and the second 33.4% (2.00), with each of the other four below 0.33. After varimax rotation the components read as school subjects: maths, physics and chemistry load 0.91 to 0.93 on the first, reading, writing and history 0.88 to 0.92 on the second, with every cross-loading between 0.08 and 0.11.
The page decomposes the correlation matrix, or the covariance matrix if your columns share one unit, with a Jacobi eigen-solver in your browser, and reports Bartlett's test and the Kaiser-Meyer-Olkin measure so you can tell whether the columns are related enough to be worth reducing. Correlation Matrix for CSV shows the pairwise relationships that PCA summarises; Linear Regression for CSV is the tool for predicting an outcome from them.
Key features
- Correlation or covariance PCA with eigenvalues, share of variance and cumulative share
- Scree plot with the eigenvalue-1 line, and the count of components above it (or above the average eigenvalue for covariance)
- Loadings table shaded by size and sign, with communalities for the components kept
- Bartlett's test of sphericity and the Kaiser-Meyer-Olkin measure, overall and per variable
- Optional varimax rotation of the first k components, with rotated loadings and variance
- Biplot of any pair of components, and scores for every row as CSV, plus an XLSX with explained-variance and communality formulas
How PCA is computed here, and how to decide how many components to keep
Tick two to 40 numeric columns. Rows with an empty or non-numeric cell in any ticked column are left out and counted, there must be more usable rows than columns, and up to 100,000 rows are analysed. A ticked column that is constant stops the run, because it has no variance to decompose.
Choose correlation when the columns are in different units or scales, which standardises each to mean 0 and standard deviation 1 so that every variable has the same say. Choose covariance only when the columns share a unit and the larger spread of some should count for more, such as the same measurement taken at several times.
How it works
The matrix is decomposed by cyclic Jacobi rotations, which zero the off-diagonal entries one pair at a time until they fall below 1e-22 of the matrix norm. Eigenvalues are sorted largest first and each eigenvector's sign is set so its largest absolute weight is positive, because the sign of a component is arbitrary. Loadings are the eigenvector times the square root of the eigenvalue, which for a correlation matrix is the correlation between the variable and the component. Scores are the standardised (or centred) data times the eigenvectors, so a component's score variance equals its eigenvalue.
Bartlett's statistic is -(n - 1 - (2p + 5) / 6) times the natural log of the determinant of the correlation matrix, with p(p - 1) / 2 degrees of freedom; a small p says the matrix is not the identity, so there is correlation to summarise. The KMO measure compares the squared correlations with the squared partial correlations taken from the inverse matrix, overall and for each variable. Both need the correlation matrix, so they are not shown for covariance PCA.
Varimax rotates the first k loading columns orthogonally so that the squared loadings in each column are as spread out (a few large, the rest near zero) as possible. After Kaiser normalisation it applies Kaiser's pairwise planar rotations: each pair of columns is turned by the angle that maximises the criterion for that pair, and sweeps repeat until no angle exceeds 1e-10 radians. Every step is an exact rotation, so the result stays orthogonal; reaching the sweep limit before convergence produces a warning. Rotation keeps every variable's communality and the total variance of the k components but redistributes it: in the example 50.5% and 33.4% become 42.6% and 41.3%. Rotated scores are the standardised unrotated scores multiplied by the rotation matrix, so they have unit variance.
Common use cases
- Collapsing six correlated test scores into a quantitative and a verbal component, then using the two score columns in a later comparison between classes.
- Reducing a 14-item satisfaction survey to the few components that carry most of its variance before clustering respondents.
How to use this tool
- Open a table of numeric measurements, or press Load example for the 120 sample students.
- Tick the numeric columns and choose correlation or covariance.
- Read the scree plot, the eigenvalue-1 count, KMO and Bartlett's test.
- Switch on varimax and set k if you want rotated loadings, then pick two components for the biplot.
- Download the scores, loadings or the XLSX.
Example
Excerpt of the example (120 students)
student,maths,physics,chemistry,reading,writing,history
S001,76,85,91,77,76,71
S002,59,54,75,78,80,70
S003,31,32,42,54,51,47
...Correlation PCA, then varimax on 2 components
Eigenvalues 3.029, 2.002, 0.323, 0.254, 0.224, 0.169 (50.5%, 33.4%, 5.4%, 4.2%, 3.7%, 2.8%)
Eigenvalues above 1: 2. KMO 0.752. Bartlett chi-square 461.9, 15 df, p < 0.0001
PC1 loadings: maths 0.746, physics 0.739, chemistry 0.728, reading 0.678, writing 0.695, history 0.673
Varimax RC1: maths 0.925, physics 0.922, chemistry 0.907 | RC2: reading 0.912, writing 0.921, history 0.877
Largest cross-loading 0.106. Variance after rotation 42.6% and 41.3%Common pitfalls
Using covariance PCA on columns in different units lets the column with the largest numbers dominate PC1 regardless of what it measures. Use correlation unless the units match.
The sign of a component is arbitrary. Reloading the same data gives the same signs here, but another program may flip them; interpret by which variables sit together, not by plus or minus.
Rotating more components than you keep, or all of them, spreads variance evenly and defeats the purpose. Rotate only the k components you will report.
- If KMO is under about 0.6, or Bartlett's p is not small, the columns are barely related and the components will be hard to interpret; PCA will still run and will still return numbers.
- When PC2 looks like noise, plot PC1 against PC3. The biplot accepts any two components, and a later component sometimes holds the contrast you care about.
Common errors and how to fix them
- Only 6 rows have a number in every chosen column; PCA needs more rows than columns
- Untick the column with the most blanks, or fill the gaps in the source, so that complete rows outnumber columns.
- "id_code" has the same value in every usable row
- A constant column has zero variance and divides by zero when standardised. Untick it.
Best practices
- Keep each exported score column with the rotation it came from. Unrotated PC scores have variance equal to the eigenvalue and rotated RC scores have variance 1, so mixing them as predictors in one later regression changes what a coefficient means.
Choosing how many components to keep
Three rules are common and they often disagree. The Kaiser rule keeps components with an eigenvalue above 1 for a correlation matrix, meaning they explain more than one original variable's worth of variance; the example keeps two. The scree rule keeps the components before the bend where the eigenvalues level off. A variance rule keeps enough components to reach a target such as 80% cumulative share, which the first two here do at 83.8%.
The Kaiser rule keeps too many components when there are many variables and too few when there are only a handful, so treat it as a starting point. Rotation then tests the choice: if the rotated loadings give each component a clear, nameable set of variables, the number is probably reasonable.
PCA is not factor analysis
PCA describes the total variance of the observed variables with new weighted sums. Factor analysis instead models only the variance the variables share and treats the rest as unique noise, so its communalities start below 1 and its loadings are usually smaller. The two often look similar on well-behaved data, but a PCA loading is a description of your sample, not an estimate of a latent trait.
That is why this page reports communalities for the components kept and calls the output components, not factors. If you need a latent-variable model with a fit test, a dedicated factor analysis is the right tool.
Frequently asked questions
What is the difference between a loading and an eigenvector?
An eigenvector is a unit-length set of weights. A loading is that eigenvector scaled by the square root of the eigenvalue, so for a correlation matrix it equals the correlation between a variable and the component. Scores use the eigenvector, interpretation uses loadings.
Why do the rotated scores have unit variance but the unrotated ones do not?
Unrotated scores follow the prcomp convention, where each component's variance equals its eigenvalue. Rotated scores are built from the standardised unrotated scores, each with variance 1, and an orthogonal rotation preserves that.
How many rows do I need?
More rows than columns is the hard requirement. A practical guide is at least five rows per variable, and fewer than that triggers a warning, since loadings from so few rows change a lot with one added observation.