Przejdź do głównej treści

Introduction to Factor Analysis in Python

In this tutorial, you'll learn the basics of factor analysis and how to implement it in python.
· 10 min przeczytaj

Odkryj z AI

Factor Analysis (FA) is an exploratory data analysis method used to search influential underlying factors or latent variables from a set of observed variables. It helps in data interpretations by reducing the number of variables. It extracts maximum common variance from all variables and puts them into a common score.

Factor analysis is widely utilized in market research, advertising, psychology, finance, and operation research. Market researchers use factor analysis to identify price-sensitive customers, identify brand features that influence consumer choice, and helps in understanding channel selection criteria for the distribution channel.

Factor Analysis

Factor analysis is a linear statistical model. It is used to explain the variance among the observed variable and condense a set of the observed variable into the unobserved variable called factors. Observed variables are modeled as a linear combination of factors and error terms (Source). Factor or latent variable is associated with multiple observed variables, who have common patterns of responses. Each factor explains a particular amount of variance in the observed variables. It helps in data interpretations by reducing the number of variables.

Factor analysis is a method for investigating whether a number of variables of interest X1, X2,……., Xl, are linearly related to a smaller number of unobservable factors F1, F2,..……, Fk.

Source: This image is recreated from an image that I found in factor analysis notes. The image gives a full view of factor analysis.

Assumptions:

  1. There are no outliers in data.
  2. Sample size should be greater than the factor.
  3. There should not be perfect multicollinearity.
  4. There should not be homoscedasticity between the variables.

Types of Factor Analysis

  • Exploratory Factor Analysis: It is the most popular factor analysis approach among social and management researchers. Its basic assumption is that any observed variable is directly associated with any factor.
  • Confirmatory Factor Analysis (CFA): Its basic assumption is that each factor is associated with a particular set of observed variables. CFA confirms what is expected on the basic.

How does Factor Analysis Work?

The primary objective of factor analysis is to reduce the number of observed variables and find unobservable variables. These unobserved variables help the market researcher to conclude the survey. This conversion of the observed variables to unobserved variables can be achieved in two steps:

  • Factor Extraction: In this step, the number of factors and approach for extraction selected using variance partitioning methods such as principal components analysis and common factor analysis.
  • Factor Rotation: In this step, rotation tries to convert factors into uncorrelated factors — the main goal of this step to improve the overall interpretability. There are lots of rotation methods that are available such as: Varimax rotation method, Quartimax rotation method, and Promax rotation method.

Terminology

What is a factor?

A factor is a latent variable which describes the association among the number of observed variables. The maximum number of factors are equal to a number of observed variables. Every factor explains a certain variance in observed variables. The factors with the lowest amount of variance were dropped. Factors are also known as latent variables or hidden variables or unobserved variables or Hypothetical variables.

What are the factor loadings?

The factor loading is a matrix which shows the relationship of each variable to the underlying factor. It shows the correlation coefficient for observed variable and factor. It shows the variance explained by the observed variables.

What is Eigenvalues?

Eigenvalues represent variance explained each factor from the total variance. It is also known as characteristic roots.

What are Communalities?

Commonalities are the sum of the squared loadings for each variable. It represents the common variance. It ranges from 0-1 and value close to 1 represents more variance.

What is Factor Rotation?

Rotation is a tool for better interpretation of factor analysis. Rotation can be orthogonal or oblique. It re-distributed the commonalities with a clear pattern of loadings.

Choosing the Number of Factors

Kaiser criterion is an analytical approach, which is based on the more significant proportion of variance explained by factor will be selected. The eigenvalue is a good criterion for determining the number of factors. Generally, an eigenvalue greater than 1 will be considered as selection criteria for the feature.

The graphical approach is based on the visual representation of factors' eigenvalues also called scree plot. This scree plot helps us to determine the number of factors where the curve makes an elbow.

Source

Factor Analysis Vs. Principle Component Analysis

  • PCA components explain the maximum amount of variance while factor analysis explains the covariance in data.
  • PCA components are fully orthogonal to each other whereas factor analysis does not require factors to be orthogonal.
  • PCA component is a linear combination of the observed variable while in FA, the observed variables are linear combinations of the unobserved variable or factor.
  • PCA components are uninterpretable. In FA, underlying factors are labelable and interpretable.
  • PCA is a kind of dimensionality reduction method whereas factor analysis is the latent variable method.
  • PCA is a type of factor analysis. PCA is observational whereas FA is a modeling technique.

Source

Factor Analysis in python using factor_analyzer package

Import Required Libraries

# Import required libraries
import pandas as pd
from sklearn.datasets import load_iris
from factor_analyzer import FactorAnalyzer
import matplotlib.pyplot as plt

Loading Data

Let's perform factor analysis on BFI (dataset based on personality assessment project), which were collected using a 6 point response scale: 1 Very Inaccurate, 2 Moderately Inaccurate, 3 Slightly Inaccurate 4 Slightly Accurate, 5 Moderately Accurate, and 6 Very Accurate. You can also download this dataset from the following the link: https://vincentarelbundock.github.io/Rdatasets/datasets.html

df= pd.read_csv("bfi.csv")

Preprocess Data

df.columns
Index(['A1', 'A2', 'A3', 'A4', 'A5', 'C1', 'C2', 'C3', 'C4', 'C5', 'E1', 'E2',
       'E3', 'E4', 'E5', 'N1', 'N2', 'N3', 'N4', 'N5', 'O1', 'O2', 'O3', 'O4',
       'O5', 'gender', 'education', 'age'],
      dtype='object')
# Dropping unnecessary columns
df.drop(['gender', 'education', 'age'],axis=1,inplace=True)
# Dropping missing values rows
df.dropna(inplace=True)
df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 2436 entries, 0 to 2799
Data columns (total 25 columns):
A1    2436 non-null float64
A2    2436 non-null float64
A3    2436 non-null float64
A4    2436 non-null float64
A5    2436 non-null float64
C1    2436 non-null float64
C2    2436 non-null float64
C3    2436 non-null float64
C4    2436 non-null float64
C5    2436 non-null float64
E1    2436 non-null float64
E2    2436 non-null float64
E3    2436 non-null float64
E4    2436 non-null float64
E5    2436 non-null float64
N1    2436 non-null float64
N2    2436 non-null float64
N3    2436 non-null float64
N4    2436 non-null float64
N5    2436 non-null float64
O1    2436 non-null float64
O2    2436 non-null int64
O3    2436 non-null float64
O4    2436 non-null float64
O5    2436 non-null float64
dtypes: float64(24), int64(1)
memory usage: 494.8 KB
df.head()
 A1A2A3A4A5C1C2C3C4C5...N1N2N3N4N5O1O2O3O4O5
02.04.03.04.04.02.03.03.04.04.0...3.04.02.02.03.03.063.04.03.0
12.04.05.02.05.05.04.04.03.04.0...3.03.03.05.05.04.024.03.03.0
25.04.05.04.04.04.05.04.02.05.0...4.05.04.02.03.04.025.05.02.0
34.04.06.05.05.04.04.03.05.05.0...2.05.02.04.01.03.034.03.05.0
42.03.03.04.05.04.04.05.03.02.0...2.03.04.04.03.03.034.03.03.0

5 rows × 25 columns

Adequacy Test

Before you perform factor analysis, you need to evaluate the “factorability” of our dataset. Factorability means "can we found the factors in the dataset?". There are two methods to check the factorability or sampling adequacy:

  • Bartlett’s Test
  • Kaiser-Meyer-Olkin Test

Bartlett’s test of sphericity checks whether or not the observed variables intercorrelate at all using the observed correlation matrix against the identity matrix. If the test found statistically insignificant, you should not employ a factor analysis.

from factor_analyzer.factor_analyzer import calculate_bartlett_sphericity
chi_square_value,p_value=calculate_bartlett_sphericity(df)
chi_square_value, p_value
(18146.065577234807, 0.0)

In this Bartlett ’s test, the p-value is 0. The test was statistically significant, indicating that the observed correlation matrix is not an identity matrix.

Kaiser-Meyer-Olkin (KMO) Test measures the suitability of data for factor analysis. It determines the adequacy for each observed variable and for the complete model. KMO estimates the proportion of variance among all the observed variable. Lower proportion id more suitable for factor analysis. KMO values range between 0 and 1. Value of KMO less than 0.6 is considered inadequate.

from factor_analyzer.factor_analyzer import calculate_kmo
kmo_all,kmo_model=calculate_kmo(df)
kmo_model
0.8486452309468382

The overall KMO for our data is 0.84, which is excellent. This value indicates that you can proceed with your planned factor analysis.

Choosing the Number of Factors

For choosing the number of factors, you can use the Kaiser criterion and scree plot. Both are based on eigenvalues.

# Create factor analysis object and perform factor analysis
fa = FactorAnalyzer()
fa.analyze(df, 25, rotation=None)
# Check Eigenvalues
ev, v = fa.get_eigenvalues()
ev
 Original_Eigenvalues
05.134311
12.751887
22.142702
31.852328
41.548163
51.073582
60.839539
70.799206
80.718989
90.688089
100.676373
110.651800
120.623253
130.596563
140.563091
150.543305
160.514518
170.494503
180.482640
190.448921
200.423366
210.400671
220.387804
230.381857
240.262539

Here, you can see only for 6-factors eigenvalues are greater than one. It means we need to choose only 6 factors (or unobserved variables).

# Create scree plot using matplotlib
plt.scatter(range(1,df.shape[1]+1),ev)
plt.plot(range(1,df.shape[1]+1),ev)
plt.title('Scree Plot')
plt.xlabel('Factors')
plt.ylabel('Eigenvalue')
plt.grid()
plt.show()

The scree plot method draws a straight line for each factor and its eigenvalues. Number eigenvalues greater than one considered as the number of factors.

Here, you can see only for 6-factors eigenvalues are greater than one. It means we need to choose only 6 factors (or unobserved variables).

Performing Factor Analysis

# Create factor analysis object and perform factor analysis
fa = FactorAnalyzer()
fa.analyze(df, 6, rotation="varimax")
fa.loadings
 Factor1Factor2Factor3Factor4Factor5Factor6
A10.0407830.0952200.048734-0.113057-0.5309870.161216
A20.2355380.0331310.1337140.0637340.661141-0.006244
A30.343008-0.0096210.1213530.0339900.6059330.160106
A40.219717-0.0815180.235140-0.1253380.4045940.086356
A50.414458-0.1496160.1063820.0309770.4696980.236519
C10.077248-0.0043580.5545820.1901240.0075110.095035
C20.0383700.0683300.6745450.0875930.0570550.152775
C30.031867-0.0399940.551164-0.0113380.1012820.008996
C4-0.0662410.216283-0.638475-0.143846-0.1026170.318359
C5-0.1808120.284187-0.5448380.025837-0.0599550.132423
E1-0.5904510.0222800.053915-0.071205-0.1308510.156583
E2-0.6845780.233624-0.088497-0.045561-0.1167160.115065
E30.556774-0.0008950.1033900.2411800.1793960.267291
E40.658395-0.1367880.113798-0.1078080.2411430.158513
E50.5075350.0344900.3098130.2008210.0788040.008747
N10.0680110.805806-0.051264-0.074977-0.174849-0.096266
N20.0229580.789832-0.0374770.006726-0.141134-0.139823
N3-0.0656870.725081-0.059039-0.010664-0.0191840.062495
N4-0.3450720.578319-0.1621740.0629160.0004030.147551
N5-0.1616750.523097-0.025305-0.1618920.0901250.120049
O10.225339-0.0200040.1332010.4794770.0051780.218690
O2-0.0019820.156230-0.086047-0.4966400.0439890.134693
O30.3259540.0118510.0938800.5661280.0766420.210777
O4-0.1777460.207281-0.0056710.3492270.1336560.178068
O5-0.0142210.063234-0.047059-0.576743-0.0575610.135936
  • Factor 1 has high factor loadings for E1,E2,E3,E4, and E5 (Extraversion)
  • Factor 2 has high factor loadings for N1,N2,N3,N4, and N5 (Neuroticism)
  • Factor 3 has high factor loadings for C1,C2,C3,C4, and C5 (Conscientiousness)
  • Factor 4 has high factor loadings for O1,O2,O3,O4, and O5 (Opennness)
  • Factor 5 has high factor loadings for A1,A2,A3,A4, and A5 (Agreeableness)
  • Factor 6 has none of the high loagings for any variable and is not easily interpretable. Its good if we take only five factors.

Let's perform factor analysis for 5 factors.

# Create factor analysis object and perform factor analysis using 5 factors
fa = FactorAnalyzer()
fa.analyze(df, 5, rotation="varimax")
fa.loadings
 Factor1Factor2Factor3Factor4Factor5
A10.0404650.1111260.022798-0.077931-0.428166
A20.2137160.0295880.1390370.0621390.626946
A30.3178480.0093570.1093310.0561960.650743
A40.204566-0.0664760.230584-0.1127000.435624
A50.393034-0.1221130.0878690.0667080.537087
C10.0701840.0104160.5458240.2095840.038878
C20.0332700.0895740.6487310.1154340.102782
C30.023907-0.0308550.557036-0.0051830.111578
C4-0.0649840.240410-0.633806-0.107535-0.037498
C5-0.1763950.290318-0.5624670.036822-0.047525
E1-0.5748350.0428190.033144-0.058795-0.104813
E2-0.6787310.244743-0.102483-0.042010-0.112517
E30.5368160.0241800.0830100.2808770.257906
E40.646833-0.1156140.102023-0.0734220.306101
E50.5040690.0361450.3128990.2137390.090354
N10.0789230.786807-0.045997-0.084704-0.216363
N20.0273010.754109-0.030568-0.010304-0.193744
N3-0.0614300.731721-0.067084-0.004217-0.027712
N4-0.3453880.590602-0.1789020.0752250.005886
N5-0.1612910.537858-0.037309-0.1497690.100931
O10.213005-0.0022240.1150800.5049070.061550
O20.0045600.175788-0.099729-0.4689250.081809
O30.3109560.0267360.0768730.5960070.126889
O4-0.1911960.220582-0.0219060.3690120.155475
O5-0.0053470.085401-0.062730-0.533778-0.010384
# Get variance of each factors
fa.get_factor_variance()
 Factor1Factor2Factor3Factor4Factor5
SS Loadings2.4730902.7096332.0411061.5221531.844498
Proportion Var0.0989240.1083850.0816440.0608860.073780
Cumulative Var0.0989240.2073090.2889530.3498390.423619

Total 42% cumulative Variance explained by the 5 factors.

Pros and Cons of Factor Analysis

Factor analysis explores large dataset and finds interlinked associations. It reduces the observed variables into a few unobserved variables or identifies the groups of inter-related variables, which help the market researchers to compress the market situations and find the hidden relationship among consumer taste, preference, and cultural influence. Also, It helps in improve questionnaire in for future surveys. Factors make for more natural data interpretation.

Results of factor analysis are controversial. Its interpretations can be debatable because more than one interpretation can be made of the same data factors. After factor identification and naming of factors requires domain knowledge.

Conclusion

Congratulations, you have made it to the end of this tutorial!

In this tutorial, you have learned what factor analysis is. The different types of factor analysis, how does factor analysis work, basic factor analysis terminology, choosing the number of factors, comparison of principal component analysis and factor analysis, implementation in python using python FactorAnalyzer package, and pros and cons of factor analysis.

I look forward to hearing any feedback or questions. you can ask the question by leaving a comment and I will try my best to answer it.

If you would like to learn more about factors in Python, take DataCamp's Unsupervised Learning in Python course.

Tematy

Powiązane

Samouczek

Python Exploratory Data Analysis Tutorial

Samouczek

Factor Levels in R

Samouczek

Python Tutorial for Beginners