0% found this document useful (0 votes)
16 views4 pages

Beginner's PCA Guide in R: Corrections & Tips

This guide offers a corrected and beginner-friendly approach to performing Principal Component Analysis (PCA) in R, detailing essential steps such as setting the working directory, reading data, conducting suitability tests, and generating PCA results. It highlights common errors and their corrections, emphasizes best practices, and provides instructions for visualizing results and saving the final dataset. Additional resources for interpreting PCA results and advanced visualizations are also mentioned.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

Beginner's PCA Guide in R: Corrections & Tips

This guide offers a corrected and beginner-friendly approach to performing Principal Component Analysis (PCA) in R, detailing essential steps such as setting the working directory, reading data, conducting suitability tests, and generating PCA results. It highlights common errors and their corrections, emphasizes best practices, and provides instructions for visualizing results and saving the final dataset. Additional resources for interpreting PCA results and advanced visualizations are also mentioned.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Corrected Beginner-Friendly Guide to PCA in R

July 13, 2025

1 Introduction
This guide provides a corrected and beginner-friendly explanation of performing Principal
Component Analysis (PCA) in R, including suitability tests, scree plots, and component
score generation. Corrections to common errors are highlighted, and best practices are
emphasized.

2 Set Working Directory


Sets and confirms the working directory where PCA data is stored.
1 setwd ( " C : / Users / oralc / Desktop / PCA " )
2 getwd ()

Purpose:

• setwd(): Sets the working directory for file operations.

• getwd(): Confirms the current working directory.

3 Read and Attach Data


Loads the dataset and checks column names.
1 PC = read . csv ( " pcadata . csv " , header = TRUE )
2 names ( PC )
3 attach ( PC )

Note:

• header = TRUE: First row contains column names.

• attach() is not recommended due to potential variable conflicts. Use PC$VariableName


instead for safer access.

1
4 Correlation Matrix & Normality Tests
Computes the correlation matrix and visualizes it appropriately.
1 r = cor ( PC )
2 View ( r )
3

4 par ( mar = c (1 , 1 , 1 , 1) )
5 hist ( as . vector ( r ) ) # Histogram of correlation coefficients
6 qqnorm ( as . vector ( r ) ) # QQ plot for normality

Corrections:
• Original: hist(r) and qqnorm(r) used a matrix, which is incorrect.

• Fixed: Convert matrix to vector with [Link](r) for proper visualization.

5 Install & Load Packages


Installs and loads required packages for PCA and diagnostics.
1 install . packages ( " psy " )
2 install . packages ( " psych " )
3 install . packages ( " GPArotation " )
4 library ( psy )
5 library ( psych )
6 library ( GPArotation )

Purpose:
• psy, psych: For Bartletts Test, KMO, and PCA functions.

• GPArotation: For rotated PCA solutions (e.g., Varimax).

6 Suitability Tests
Tests whether the dataset is suitable for PCA.
1 cortest . bartlett ( PC ) # Bartletts Test of Sphericity
2 KMO ( PC ) # Kaiser - Meyer - Olkin ( KMO ) Test

Explanation:
• Bartletts Test: p < 0.05 indicates variables are correlated, suitable for PCA.

• KMO Test: KMO > 0.6 suggests adequate sampling adequacy.

7 Scree Plot for Factor Selection


Visualizes the number of components to retain.
1 scree ( PC ) # Scree plot from psych package
2 fa . parallel ( PC ) # Parallel analysis for optimal components

2
Correction:

• Original: [Link](PC) from psy is obsolete.

• Fixed: Use scree() or [Link]() from psych for better visualization and
component selection.

8 Unrotated PCA with 15 Components


Performs PCA without rotation for comparison.
1 model1 = pca ( PC , nfactors = 15 , rotate = " none " )
2 model1 $ loadings

Explanation:

• Extracts 15 principal components without rotation.

• Useful for initial exploration but less interpretable without rotation.

9 PCA with 4 Factors & Varimax Rotation


Performs PCA with Varimax rotation for interpretable results.
1 PCAmodel = pca ( PC , nfactors = 4 , rotate = " varimax " , method = "
regression " , scores = TRUE )
2 PCAmodel
3 PCAmodel $ loadings # Factor loadings
4 PCAmodel $ scores # Component scores

Explanation:

• rotate = "varimax": Orthogonal rotation for clearer factor interpretation.

• method = "regression": Ensures proper factor score estimation.

• scores = TRUE: Generates component scores for each observation.

10 Save Final Dataset


Appends PCA scores to the original dataset and saves it.
1 finalPCAdata = cbind ( PC , PCAmodel $ scores )
2 write . csv ( file = " finalPCAdata . csv " , finalPCAdata )

Purpose:

• Combines original data with PCA scores (PC1, PC2, etc.).

• Saves the enhanced dataset as a CSV file.

3
11 Optional GUI for Beginners
Installs and loads R Commander for a GUI-based interface.
1 install . packages ( " Rcmdr " )
2 library ( Rcmdr )

Correction:

• Original: [Link]("Rcmdr") contained a typo.

• Fixed: Corrected to [Link]("Rcmdr").

12 Summary of Issues and Fixes

Line Issue Fix


qqnorm(r) Matrix passed instead of vector Use qqnorm([Link](r))
hist(r) Matrix passed instead of vector Use hist([Link](r))
[Link]() Obsolete function Use scree() or [Link]()
[Link]() Typo in function name Use [Link]()
attach() Risky for variable conflicts Use PC$colname instead

Table 1: Summary of Issues and Fixes in PCA Script

13 Additional Notes
If you need help interpreting PCA results (e.g., loadings, scree plot, or component scores)
or visualizing them (e.g., biplot or component score plots), let me know! The psych pack-
age also supports advanced visualizations like [Link]() for loadings and scores.

Common questions

Powered by AI

The purpose of generating a scree plot in PCA is to visualize the number of components to retain by displaying the eigenvalues associated with each component and identifying any inflection points. In R, the implementation has evolved from using the obsolete scree.plot() in the psy package to the scree() or fa.parallel() in the psych package, which provide better visualization and component selection methods .

The guide suggests avoiding the 'attach' function because it can lead to variable conflicts by overwriting existing variables in the global environment. Instead, using PC$VariableName for accessing specific variables is a safer practice as it prevents such conflicts .

Using a GUI such as R Commander for PCA operations offers advantages like providing a user-friendly interface for beginners, automating many aspects of data analysis workflows, and reducing potential errors from manual scripting by providing point-and-click options for executing PCA procedures .

Common errors include using matrix objects in functions that require vectors, such as in hist(r) and qqnorm(r), where the corrected practice is to convert the matrix to a vector with as.vector(r). Additionally, the use of scree.plot() from the psy package has been updated to scree() or fa.parallel() from the psych package due to obsolescence. Furthermore, attach() can cause variable conflicts, so using PC$VariableName is recommended .

The suitability criteria for applying PCA include the Bartlett's Test of Sphericity and the Kaiser-Meyer-Olkin (KMO) Test. Bartlett's Test requires a p-value less than 0.05, indicating there are correlations among variables suitable for PCA. The KMO Test suggests a KMO index greater than 0.6 for adequate sampling adequacy .

The use of the regression method during PCA influences factor score estimation by providing a means for estimating scores based on the regression approach, which ensures that the scores are as predictive of the original variables as possible while remaining uncorrelated with each other, enhancing the reliability of the score interpretations .

Varimax rotation improves the interpretability of PCA results by producing orthogonal factors that make the structure of the data clearer and more distinct. This allows for easier interpretation of components as it simplifies the factor loadings, making latent variables more coherent and easier to label .

The 'psy' and 'psych' packages are crucial for implementing Bartlett's Test, the KMO Test, and PCA functions. The 'GPArotation' package facilitates rotated PCA solutions, such as Varimax rotation, which enhances the interpretability of the resulting factors .

The steps for preparing data before performing PCA include setting the working directory with setwd() and confirming it with getwd(), reading and attaching the dataset using read.csv() with header = TRUE for column names, and computing the correlation matrix for the data using cor().

The guide suggests saving the final dataset by appending PCA scores to the original dataset and writing it to a CSV file using cbind() to combine the original data with the PCA scores and write.csv() to save the data. This process ensures the augmented dataset, which includes both raw data and computed component scores, is preserved for further analysis or reporting .

You might also like