Factor Analysis Action Set

Estimating the Number of Factors or Components

This section contains PROC CAS code.

Note: Input data must be accessible in your CAS session, either as a CAS table or as a transient-scope table. A CAS table has a two-level name: the first level is your CAS engine libref, and the second level is the table name. You refer to this table in the CAS procedure by specifying only the second level. For more information about two-level names, see Chapter 2, Shared Concepts. A transient-scope table is called directly from the action and exists in memory for the duration of the action. For more information about accessing data, see SAS Viya: System Programming Guide. For more information about PROC CAS and programming in CASL, see SAS Cloud Analytic Services: CASL Programmer’s Guide and SAS Cloud Analytic Services: CASL Reference.

This example uses the simulated testdata data set that is described in the examples for the Principal Component Analysis Action Set in SAS Visual Statistics software. The data table is the result of multiplying two low-rank matrices, bold upper A and bold upper B, and adding random noise.

To create the data table testdata in your CAS session, you can modify the DATA step that is used in the Principal Component Analysis Action Set documentation to create the data table in your CAS engine libref. For this example, it is assumed that your CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref.

In this example, you want to determine how many latent factors are needed to explain the variation in the input data. The following statements perform this analysis:

proc cas;
factorAnalysis.faNFactors /
    criteria={{type="eigenvalue"}, {threshold={0.9}, type="proportion"}},
    table="testdata";
run;

When you use the faNFactors action, you must specify values for the table and criteria parameters. You specify the input data table to use for the analysis in the table parameter. You use the criteria parameter to specify one or more criteria that the action uses to determine the number of factors or components to retain.

In this example, you specify two criteria. The first criterion is the eigenvalue criterion. This criterion is based on the eigenvalues of the reduced correlation matrix (that is, the correlation matrix of the input data, but with the diagonal elements replaced by the prior communality estimates). By default, any eigenvalue greater than 1 is assumed to indicate the existence of a latent factor. You can use the threshold subparameter of the criteria parameter to specify one or more threshold values to use instead of the default value.

The second criterion that you specify is the proportion criterion. This criterion is based on the cumulative fraction of the common variance of the input data set that is explained by the latent factors that correspond to each eigenvalue. In this example, you use the threshold subparameter of the criteria parameter to specify a threshold value of 0.9. At this threshold, the number of factors that are suggested by the proportion criterion is the smallest number of factors that are necessary to explain 90% of the common variance of the input data set.

By default, the faNFactors action uses squared multiple correlations (SMC) for the prior communality estimates. This means that the prior communality estimate for each variable is the squared multiple correlation of that variable with all other variables. The prior communalities are placed on the diagonal of the data correlation matrix before the eigenvalues of that matrix are computed. You can use the priors parameter to specify different prior communality estimates.

The output from this analysis is displayed by default. It is presented in Output 10.1.1 through Output 10.1.5.

The "Number of Observations" table in Output 10.1.1 summarizes the number of observations that are read from the input data table and the number of observations that are used for the analysis. For this analysis, these two numbers are the same. The two numbers would be different if some observations were discarded from the analysis. The faNFactors action uses only complete cases for analysis, so a record is discarded if any field contains a missing value.

Output 10.1.1: Number of Observations

Results from factorAnalysis.faNFactors

Number of Observations Read1000000
Number of Observations Used1000000


The "Simple Statistics" table in Output 10.1.2 summarizes the means and standard deviations of the variables that are included in the analysis.

Output 10.1.2: Simple Statistics

Simple Statistics
VariableMeanStandard
Deviation
x1-0.001522.03668
x2-0.001303.04372
x3-0.001571.25845
x4-0.001711.79254
x50.002141.47301
x6-0.001661.52154
x7-0.000515871.83471
x80.000693732.30642
x90.003282.13588
x10-0.005413.40214


The primary output of the analysis is shown in the "Determination of Number of Factors" table in Output 10.1.3. In this analysis, the results for both of the specified criteria suggest that three latent factors are sufficient to describe the input data.

In general, when you specify multiple criteria, the faNFactors action must combine the results from all the active criteria to form a final estimate for the number of factors. By default, the faNFactors action uses the minimum number of factors among all the active criteria. You can use the nFactors parameter to specify a different method to obtain a final estimate. You can use the status subparameter of the criterion parameter to change a criterion from active to inactive.

A footnote at the bottom of the table summarizes the minimum and maximum number of factors that are determined by the active criteria. It also summarizes the mean and median values of these numbers. In this analysis, the two criteria suggest the same number of latent factors, so all four summary values are the same. Thus the final result of this analysis is that three factors should be retained. You can use the following code to directly access this final result:

factorAnalysis_faNFactors result=r /
    criteria={{type="eigenvalue"},
              {threshold={0.9, 0.95, 0.99}, type="proportion"}},
    table="testdata";
run;
print r.nFactorsByGroup;
run;

Output 10.1.3: Number of Factors

Determination of Number of Factors
CriterionDescriptionFactors
1Minimum Eigenvalue (Eigenvalue > 1.00)3
2Total Proportion (Threshold = 0.90)3
Min = 3, Max = 3, Mean = 3, Median = 3


Additional information about the eigenvalue and proportion criteria is presented in the "Eigenvalues of the Reduced Correlation Matrix" table in Output 10.1.4. For the proportion criteria, the number of factors is the minimum number required in order for the cumulative common variance to meet or exceed the user-specified threshold. The table shows that three factors are sufficient to explain approximately 93% of the cumulative common variance in the original data. Thus the proportion criterion, with the threshold parameter set to 0.90, produces an estimate of three factors. For the eigenvalue criterion, the third-largest eigenvalue is larger than 1.5, whereas the fourth-largest eigenvalue is much smaller than 1.0. For this reason, the eigenvalue criterion that is specified in this analysis suggests that three factors should be retained.

Output 10.1.4: Eigenvalues and Variance Explained

Eigenvalues of the Reduced Correlation Matrix
 EigenvalueDifferenceProportionCumulative
15.8172063.9168100.58450.5845
21.9003960.3338050.19090.7754
31.5665920.8881250.15740.9328
40.6784670.6792780.06821.0009
5-0.0008110.000256-0.00011.0009
6-0.0010670.000543-0.00011.0008
7-0.0016110.000076516-0.00021.0006
8-0.0016870.000100-0.00021.0004
9-0.0017880.000670-0.00021.0002
10-0.002457 -0.00021.0000


Finally, the faNFactors action displays the table shown in Output 10.1.5, which shows the amount of time (in seconds) that it took to perform the various tasks of the analysis.

Output 10.1.5: Analysis Timing

Task Timing
TaskSecondsPercent
Initializing0.0327.94%
Parsing0.000.02%
Calculating Correlations0.0868.79%
Determining Number of Factors0.000.29%
Miscellaneous0.002.85%
Cleaning Up0.000.12%
Total0.12100.00%


In addition to estimating the number of latent factors, you can also use the faNFactors action to estimate the number of principal components that are needed to approximate an input data set. For example, you might want to determine how many components you need to retain to approximate a prespecified proportion of the total variance in the original data set. The following statements perform this analysis:

proc cas;
factorAnalysis.faNFactors /
    criteria={{threshold={0.9, 0.95, 0.99}, type="proportion"}},
    priors="one",
    table="testdata";
run;

These statements demonstrate how you can specify three different threshold values for the proportion criterion. Each of these threshold values is applied separately to estimate the number of factors in the input data set. This syntax is equivalent to specifying the proportion criterion three times, using a different threshold value each time. The specification of multiple threshold values is a useful technique to assess the degree to which the analysis results are robust to different threshold values.

In this analysis, the priors parameter is used to specify that the prior communality estimates for all variables should be set to 1. When you specify this parameter, the faNFactors action estimates the number of principal components that are needed to approximate the input data set.

The "Number of Observations" and "Simple Statistics" tables for this analysis are identical to the corresponding tables in the previous analysis. The "Determination of Number of Factors" table is shown in Output 10.1.6. Of the three specified threshold values, the first value produces an estimate that three factors should be retained, and the remaining two criteria produce estimates that four factors should be retained.

Output 10.1.6: Number of Factors

Results from factorAnalysis.faNFactors

Determination of Number of Factors
CriterionDescriptionFactors
1Total Proportion (Threshold = 0.90)3
2Total Proportion (Threshold = 0.95)4
3Total Proportion (Threshold = 0.99)4
Min = 3, Max = 4, Mean = 4, Median = 4


Additional information about the number of components that are suggested by these three criteria is presented in the "Eigenvalues of the Correlation Matrix" table in Output 10.1.7. As in the previous analysis, the proportion criterion examines the cumulative variance explained by eigenvalues and corresponding principal components. For this analysis, the principal components that correspond to the three largest eigenvalues explain just under 93% of the total variance of the input data set. Thus, an estimate of three components is obtained when you set the threshold value to 0.9. When you add a fourth component, the cumulative variance explained exceeds 99%, so that four components are estimated when the threshold subparameter is specified as 0.95 or 0.99.

Output 10.1.7: Eigenvalues and Variance Explained

Eigenvalues of the Correlation Matrix
 EigenvalueDifferenceProportionCumulative
15.8218713.9168580.58220.5822
21.9050130.3334750.19050.7727
31.5715370.8887750.15720.9298
40.6827620.6772920.06830.9981
50.0054700.0010720.00050.9987
60.0043980.0011980.00040.9991
70.0032000.0007940.00030.9994
80.0024060.0006230.00020.9997
90.0017830.0002250.00020.9998
100.001559 0.00021.0000


Estimating the Number of Factors or Components

This section contains Lua code that creates the same testdata table and performs the same analysis as shown in the CASL version of this example. For more information about coding in Lua, see Getting Started with SAS Viya for Lua and SAS Viya: System Programming Guide.

Because this example uses simulated data that are produced from DATA step code, you can submit the runCode action on your Lua client to create the simulated data in your CAS session.

This example uses the simulated testdata data set that is described in the examples for the Principal Component Analysis Action Set in SAS Visual Statistics software. The data table is the result of multiplying two low-rank matrices, bold upper A and bold upper B, and adding random noise.

To run this example code, you must make the testdata data accessible to the CAS server. One way to do this is to convert the testdata data set from the Principal Component Analysis Action Set documentation to a comma-separated-value (CSV) file and then load the CSV file into CAS.

In this example, you want to determine how many latent factors are needed to explain the variation in the input data. The following statements perform this analysis:

s:loadactionset{actionset="factorAnalysis"}
s:factorAnalysis_faNFactors{
   table='testdata',
   criteria={
      {type='eigenvalue'},
      {type='proportion', threshold={0.9}}
   }
   }

When you use the faNFactors action, you must specify values for the table and criteria parameters. You specify the input data table to use for the analysis in the table parameter. You use the criteria parameter to specify one or more criteria that the action uses to determine the number of factors or components to retain.

In this example, you specify two criteria. The first criterion is the eigenvalue criterion. This criterion is based on the eigenvalues of the reduced correlation matrix (that is, the correlation matrix of the input data, but with the diagonal elements replaced by the prior communality estimates). By default, any eigenvalue greater than 1 is assumed to indicate the existence of a latent factor. You can use the threshold subparameter of the criteria parameter to specify one or more threshold values to use instead of the default value.

The second criterion that you specify is the proportion criterion. This criterion is based on the cumulative fraction of the common variance of the input data set that is explained by the latent factors that correspond to each eigenvalue. In this example, you use the threshold subparameter of the criteria parameter to specify a threshold value of 0.9. At this threshold, the number of factors that are suggested by the proportion criterion is the smallest number of factors that are necessary to explain 90% of the common variance of the input data set.

By default, the faNFactors action uses squared multiple correlations (SMC) for the prior communality estimates. This means that the prior communality estimate for each variable is the squared multiple correlation of that variable with all other variables. The prior communalities are placed on the diagonal of the data correlation matrix before the eigenvalues of that matrix are computed. You can use the priors parameter to specify different prior communality estimates.

The output from this analysis is displayed by default. It is presented in Output 10.1.8 through Output 10.1.12.

The "Number of Observations" table in Output 10.1.8 summarizes the number of observations that are read from the input data table and the number of observations that are used for the analysis. For this analysis, these two numbers are the same. The two numbers would be different if some observations were discarded from the analysis. The faNFactors action uses only complete cases for analysis, so a record is discarded if any field contains a missing value.

Output 10.1.8: Number of Observations

RowIdDescriptionValue
NREADNumber of Observations Read1000000
NUSEDNumber of Observations Used1000000


The "Simple Statistics" table in Output 10.1.9 summarizes the means and standard deviations of the variables that are included in the analysis.

Output 10.1.9: Simple Statistics

VariableMeanStdDev
x1-0.001522.03668
x2-0.001303.04372
x3-0.001571.25845
x4-0.001711.79254
x50.002141.47301
x6-0.001661.52154
x7-0.000515871.83471
x80.000693732.30642
x90.003282.13588
x10-0.005413.40214


The primary output of the analysis is shown in the "Determination of Number of Factors" table in Output 10.1.10. In this analysis, the results for both of the specified criteria suggest that three latent factors are sufficient to describe the input data.

In general, when you specify multiple criteria, the faNFactors action must combine the results from all the active criteria to form a final estimate for the number of factors. By default, the faNFactors action uses the minimum number of factors among all the active criteria. You can use the nFactors parameter to specify a different method to obtain a final estimate. You can use the status subparameter of the criterion parameter to change a criterion from active to inactive.

In this analysis, the two criteria suggest the same number of latent factors. Thus the final result of this analysis is that three factors should be retained. You can access this final result by specifying the "nFactorsByGroup" key in the results dictionary that is produced by the faNFactors action.

Output 10.1.10: Number of Factors

CriterionDescriptionNFactorsThresholdSimulationsSeedStatus
1Minimum Eigenvalue (Eigenvalue > 1.00)31.0__1
2Total Proportion (Threshold = 0.90)30.9__1


Additional information about the eigenvalue and proportion criteria is presented in the "Eigenvalues of the Reduced Correlation Matrix" table in Output 10.1.11. For the proportion criteria, the number of factors is the minimum number required in order for the cumulative common variance to meet or exceed the user-specified threshold. The table shows that three factors are sufficient to explain approximately 93% of the cumulative common variance in the original data. Thus the proportion criterion, with the threshold parameter set to 0.90, produces an estimate of three factors. For the eigenvalue criterion, the third-largest eigenvalue is larger than 1.5, whereas the fourth-largest eigenvalue is much smaller than 1.0. For this reason, the eigenvalue criterion that is specified in this analysis suggests that three factors should be retained.

Output 10.1.11: Eigenvalues and Variance Explained

NumberEigenvalueDifferenceProportionCumulative
15.8172063.9168100.58450.5845
21.9003960.3338050.19090.7754
31.5665920.8881250.15740.9328
40.6784670.6792780.06821.0009
5-0.0008110.000256-0.00011.0009
6-0.0010670.000543-0.00011.0008
7-0.0016110.000076516-0.00021.0006
8-0.0016870.000100-0.00021.0004
9-0.0017880.000670-0.00021.0002
10-0.002457_-0.00021.0000


Finally, the faNFactors action displays the table shown in Output 10.1.12, which shows the amount of time (in seconds) that it took to perform the various tasks of the analysis.

Output 10.1.12: Analysis Timing

RowIdTaskTimeRelTime
INITInitializing0.0327.94%
PARSEParsing0.000.02%
CORRCalculating Correlations0.0868.79%
NFACTDetermining Number of Factors0.000.29%
MISCMiscellaneous0.002.85%
CLEANUPCleaning Up0.000.12%
TOTALTotal0.12100.00%


In addition to estimating the number of latent factors, you can also use the faNFactors action to estimate the number of principal components that are needed to approximate an input data set. For example, you might want to determine how many components you need to retain to approximate a prespecified proportion of the total variance in the original data set. The following statements perform this analysis:

s:loadactionset{actionset="factorAnalysis"}
s:factorAnalysis_faNFactors{
   table='testdata',
   criteria={
      {type='proportion', threshold={0.90,0.95,0.99}},
   },
   priors='one'
   }

These statements demonstrate how you can specify three different threshold values for the proportion criterion. Each of these threshold values is applied separately to estimate the number of factors in the input data set. This syntax is equivalent to specifying the proportion criterion three times, using a different threshold value each time. The specification of multiple threshold values is a useful technique to assess the degree to which the analysis results are robust to different threshold values.

In this analysis, the priors parameter is used to specify that the prior communality estimates for all variables should be set to 1. When you specify this parameter, the faNFactors action estimates the number of principal components that are needed to approximate the input data set.

The "Number of Observations" and "Simple Statistics" tables for this analysis are identical to the corresponding tables in the previous analysis. The "Determination of Number of Factors" table is shown in Output 10.1.13. Of the three specified threshold values, the first value produces an estimate that three factors should be retained, and the remaining two criteria produce estimates that four factors should be retained.

Output 10.1.13: Number of Factors

CriterionDescriptionNFactorsThresholdSimulationsSeedStatus
1Total Proportion (Threshold = 0.90)30.90__1
2Total Proportion (Threshold = 0.95)40.95__1
3Total Proportion (Threshold = 0.99)40.99__1


Additional information about the number of components that are suggested by these three criteria is presented in the "Eigenvalues of the Correlation Matrix" table in Output 10.1.14. As in the previous analysis, the proportion criterion examines the cumulative variance explained by eigenvalues and corresponding principal components. For this analysis, the principal components that correspond to the three largest eigenvalues explain just under 93% of the total variance of the input data set. Thus, an estimate of three components is obtained when you set the threshold value to 0.9. When you add a fourth component, the cumulative variance explained exceeds 99%, so that four components are estimated when the threshold subparameter is specified as 0.95 or 0.99.

Output 10.1.14: Eigenvalues and Variance Explained

NumberEigenvalueDifferenceProportionCumulative
15.8218713.9168580.58220.5822
21.9050130.3334750.19050.7727
31.5715370.8887750.15720.9298
40.6827620.6772920.06830.9981
50.0054700.0010720.00050.9987
60.0043980.0011980.00040.9991
70.0032000.0007940.00030.9994
80.0024060.0006230.00020.9997
90.0017830.0002250.00020.9998
100.001559_0.00021.0000


Estimating the Number of Factors or Components

This section contains Python code that creates the same testdata table and performs the same analysis as shown in the CASL version of this example. For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.

Because this example uses simulated data that are produced from DATA step code, you can submit the runCode action on your Python client to create the simulated data in your CAS session.

This example uses the simulated testdata data set that is described in the examples for the Principal Component Analysis Action Set in SAS Visual Statistics software. The data table is the result of multiplying two low-rank matrices, bold upper A and bold upper B, and adding random noise.

To run this example code, you must make the testdata data accessible to the CAS server. One way to do this is to convert the testdata data set from the Principal Component Analysis Action Set documentation to a comma-separated-value (CSV) file and then load the CSV file into CAS.

In this example, you want to determine how many latent factors are needed to explain the variation in the input data. The following statements perform this analysis:

s.loadactionset(actionset='factorAnalysis')
s.factoranalysis.fanfactors(
    criteria=[dict(type='eigenvalue'),
              dict(threshold=[0.9], type='proportion')],
    table='testdata')

When you use the faNFactors action, you must specify values for the table and criteria parameters. You specify the input data table to use for the analysis in the table parameter. You use the criteria parameter to specify one or more criteria that the action uses to determine the number of factors or components to retain.

In this example, you specify two criteria. The first criterion is the eigenvalue criterion. This criterion is based on the eigenvalues of the reduced correlation matrix (that is, the correlation matrix of the input data, but with the diagonal elements replaced by the prior communality estimates). By default, any eigenvalue greater than 1 is assumed to indicate the existence of a latent factor. You can use the threshold subparameter of the criteria parameter to specify one or more threshold values to use instead of the default value.

The second criterion that you specify is the proportion criterion. This criterion is based on the cumulative fraction of the common variance of the input data set that is explained by the latent factors that correspond to each eigenvalue. In this example, you use the threshold subparameter of the criteria parameter to specify a threshold value of 0.9. At this threshold, the number of factors that are suggested by the proportion criterion is the smallest number of factors that are necessary to explain 90% of the common variance of the input data set.

By default, the faNFactors action uses squared multiple correlations (SMC) for the prior communality estimates. This means that the prior communality estimate for each variable is the squared multiple correlation of that variable with all other variables. The prior communalities are placed on the diagonal of the data correlation matrix before the eigenvalues of that matrix are computed. You can use the priors parameter to specify different prior communality estimates.

The output from this analysis is displayed by default. It is presented in Output 10.1.15 through Output 10.1.19.

The "Number of Observations" table in Output 10.1.15 summarizes the number of observations that are read from the input data table and the number of observations that are used for the analysis. For this analysis, these two numbers are the same. The two numbers would be different if some observations were discarded from the analysis. The faNFactors action uses only complete cases for analysis, so a record is discarded if any field contains a missing value.

Output 10.1.15: Number of Observations

RowIdDescriptionValue
NREADNumber of Observations Read1000000
NUSEDNumber of Observations Used1000000


The "Simple Statistics" table in Output 10.1.16 summarizes the means and standard deviations of the variables that are included in the analysis.

Output 10.1.16: Simple Statistics

VariableMeanStdDev
x1-0.001522.03668
x2-0.001303.04372
x3-0.001571.25845
x4-0.001711.79254
x50.002141.47301
x6-0.001661.52154
x7-0.000515871.83471
x80.000693732.30642
x90.003282.13588
x10-0.005413.40214


The primary output of the analysis is shown in the "Determination of Number of Factors" table in Output 10.1.17. In this analysis, the results for both of the specified criteria suggest that three latent factors are sufficient to describe the input data.

In general, when you specify multiple criteria, the faNFactors action must combine the results from all the active criteria to form a final estimate for the number of factors. By default, the faNFactors action uses the minimum number of factors among all the active criteria. You can use the nFactors parameter to specify a different method to obtain a final estimate. You can use the status subparameter of the criterion parameter to change a criterion from active to inactive.

In this analysis, the two criteria suggest the same number of latent factors. Thus the final result of this analysis is that three factors should be retained. You can access this final result by specifying the "nFactorsByGroup" key in the results dictionary that is produced by the faNFactors action.

Output 10.1.17: Number of Factors

CriterionDescriptionNFactorsThresholdSimulationsSeedStatus
1Minimum Eigenvalue (Eigenvalue > 1.00)31.0__1
2Total Proportion (Threshold = 0.90)30.9__1


Additional information about the eigenvalue and proportion criteria is presented in the "Eigenvalues of the Reduced Correlation Matrix" table in Output 10.1.18. For the proportion criteria, the number of factors is the minimum number required in order for the cumulative common variance to meet or exceed the user-specified threshold. The table shows that three factors are sufficient to explain approximately 93% of the cumulative common variance in the original data. Thus the proportion criterion, with the threshold parameter set to 0.90, produces an estimate of three factors. For the eigenvalue criterion, the third-largest eigenvalue is larger than 1.5, whereas the fourth-largest eigenvalue is much smaller than 1.0. For this reason, the eigenvalue criterion that is specified in this analysis suggests that three factors should be retained.

Output 10.1.18: Eigenvalues and Variance Explained

NumberEigenvalueDifferenceProportionCumulative
15.8172063.9168100.58450.5845
21.9003960.3338050.19090.7754
31.5665920.8881250.15740.9328
40.6784670.6792780.06821.0009
5-0.0008110.000256-0.00011.0009
6-0.0010670.000543-0.00011.0008
7-0.0016110.000076516-0.00021.0006
8-0.0016870.000100-0.00021.0004
9-0.0017880.000670-0.00021.0002
10-0.002457_-0.00021.0000


Finally, the faNFactors action displays the table shown in Output 10.1.19, which shows the amount of time (in seconds) that it took to perform the various tasks of the analysis.

Output 10.1.19: Analysis Timing

RowIdTaskTimeRelTime
INITInitializing0.0327.94%
PARSEParsing0.000.02%
CORRCalculating Correlations0.0868.79%
NFACTDetermining Number of Factors0.000.29%
MISCMiscellaneous0.002.85%
CLEANUPCleaning Up0.000.12%
TOTALTotal0.12100.00%


In addition to estimating the number of latent factors, you can also use the faNFactors action to estimate the number of principal components that are needed to approximate an input data set. For example, you might want to determine how many components you need to retain to approximate a prespecified proportion of the total variance in the original data set. The following statements perform this analysis:

s.loadactionset(actionset='factorAnalysis')
s.factoranalysis.fanfactors(
    criteria=[dict(threshold=[0.9, 0.95, 0.99], type='proportion')],
    priors='one',
    table='testdata')

These statements demonstrate how you can specify three different threshold values for the proportion criterion. Each of these threshold values is applied separately to estimate the number of factors in the input data set. This syntax is equivalent to specifying the proportion criterion three times, using a different threshold value each time. The specification of multiple threshold values is a useful technique to assess the degree to which the analysis results are robust to different threshold values.

In this analysis, the priors parameter is used to specify that the prior communality estimates for all variables should be set to 1. When you specify this parameter, the faNFactors action estimates the number of principal components that are needed to approximate the input data set.

The "Number of Observations" and "Simple Statistics" tables for this analysis are identical to the corresponding tables in the previous analysis. The "Determination of Number of Factors" table is shown in Output 10.1.20. Of the three specified threshold values, the first value produces an estimate that three factors should be retained, and the remaining two criteria produce estimates that four factors should be retained.

Output 10.1.20: Number of Factors

CriterionDescriptionNFactorsThresholdSimulationsSeedStatus
1Total Proportion (Threshold = 0.90)30.90__1
2Total Proportion (Threshold = 0.95)40.95__1
3Total Proportion (Threshold = 0.99)40.99__1


Additional information about the number of components that are suggested by these three criteria is presented in the "Eigenvalues of the Correlation Matrix" table in Output 10.1.21. As in the previous analysis, the proportion criterion examines the cumulative variance explained by eigenvalues and corresponding principal components. For this analysis, the principal components that correspond to the three largest eigenvalues explain just under 93% of the total variance of the input data set. Thus, an estimate of three components is obtained when you set the threshold value to 0.9. When you add a fourth component, the cumulative variance explained exceeds 99%, so that four components are estimated when the threshold subparameter is specified as 0.95 or 0.99.

Output 10.1.21: Eigenvalues and Variance Explained

NumberEigenvalueDifferenceProportionCumulative
15.8218713.9168580.58220.5822
21.9050130.3334750.19050.7727
31.5715370.8887750.15720.9298
40.6827620.6772920.06830.9981
50.0054700.0010720.00050.9987
60.0043980.0011980.00040.9991
70.0032000.0007940.00030.9994
80.0024060.0006230.00020.9997
90.0017830.0002250.00020.9998
100.001559_0.00021.0000


Estimating the Number of Factors or Components

This section contains R code that creates the same testdata table and performs the same analysis as shown in the CASL version of this example. For more information about coding in R, see Getting Started with SAS Viya for R and SAS Viya: System Programming Guide.

Because this example uses simulated data that are produced from DATA step code, you can submit the runCode action on your R client to create the simulated data in your CAS session.

This example uses the simulated testdata data set that is described in the examples for the Principal Component Analysis Action Set in SAS Visual Statistics software. The data table is the result of multiplying two low-rank matrices, bold upper A and bold upper B, and adding random noise.

To run this example code, you must make the testdata data accessible to the CAS server. One way to do this is to convert the testdata data set from the Principal Component Analysis Action Set documentation to a comma-separated-value (CSV) file and then load the CSV file into CAS.

In this example, you want to determine how many latent factors are needed to explain the variation in the input data. The following statements perform this analysis:

m <- cas.builtins.loadActionSet(s, actionset='factorAnalysis')
cas.factorAnalysis.faNFactors(s,
    criteria=list(list(type='eigenvalue'),
                  list(threshold=c(0.9), type='proportion')),
    table='testdata')

When you use the faNFactors action, you must specify values for the table and criteria parameters. You specify the input data table to use for the analysis in the table parameter. You use the criteria parameter to specify one or more criteria that the action uses to determine the number of factors or components to retain.

In this example, you specify two criteria. The first criterion is the eigenvalue criterion. This criterion is based on the eigenvalues of the reduced correlation matrix (that is, the correlation matrix of the input data, but with the diagonal elements replaced by the prior communality estimates). By default, any eigenvalue greater than 1 is assumed to indicate the existence of a latent factor. You can use the threshold subparameter of the criteria parameter to specify one or more threshold values to use instead of the default value.

The second criterion that you specify is the proportion criterion. This criterion is based on the cumulative fraction of the common variance of the input data set that is explained by the latent factors that correspond to each eigenvalue. In this example, you use the threshold subparameter of the criteria parameter to specify a threshold value of 0.9. At this threshold, the number of factors that are suggested by the proportion criterion is the smallest number of factors that are necessary to explain 90% of the common variance of the input data set.

By default, the faNFactors action uses squared multiple correlations (SMC) for the prior communality estimates. This means that the prior communality estimate for each variable is the squared multiple correlation of that variable with all other variables. The prior communalities are placed on the diagonal of the data correlation matrix before the eigenvalues of that matrix are computed. You can use the priors parameter to specify different prior communality estimates.

The output from this analysis is displayed by default. It is presented in Output 10.1.22 through Output 10.1.26.

The "Number of Observations" table in Output 10.1.22 summarizes the number of observations that are read from the input data table and the number of observations that are used for the analysis. For this analysis, these two numbers are the same. The two numbers would be different if some observations were discarded from the analysis. The faNFactors action uses only complete cases for analysis, so a record is discarded if any field contains a missing value.

Output 10.1.22: Number of Observations

RowIdDescriptionValue
NREADNumber of Observations Read1000000
NUSEDNumber of Observations Used1000000


The "Simple Statistics" table in Output 10.1.23 summarizes the means and standard deviations of the variables that are included in the analysis.

Output 10.1.23: Simple Statistics

VariableMeanStdDev
x1-0.001522.03668
x2-0.001303.04372
x3-0.001571.25845
x4-0.001711.79254
x50.002141.47301
x6-0.001661.52154
x7-0.000515871.83471
x80.000693732.30642
x90.003282.13588
x10-0.005413.40214


The primary output of the analysis is shown in the "Determination of Number of Factors" table in Output 10.1.24. In this analysis, the results for both of the specified criteria suggest that three latent factors are sufficient to describe the input data.

In general, when you specify multiple criteria, the faNFactors action must combine the results from all the active criteria to form a final estimate for the number of factors. By default, the faNFactors action uses the minimum number of factors among all the active criteria. You can use the nFactors parameter to specify a different method to obtain a final estimate. You can use the status subparameter of the criterion parameter to change a criterion from active to inactive.

In this analysis, the two criteria suggest the same number of latent factors. Thus the final result of this analysis is that three factors should be retained. You can access this final result by specifying the "nFactorsByGroup" key in the results dictionary that is produced by the faNFactors action.

Output 10.1.24: Number of Factors

CriterionDescriptionNFactorsThresholdSimulationsSeedStatus
1Minimum Eigenvalue (Eigenvalue > 1.00)31.0__1
2Total Proportion (Threshold = 0.90)30.9__1


Additional information about the eigenvalue and proportion criteria is presented in the "Eigenvalues of the Reduced Correlation Matrix" table in Output 10.1.25. For the proportion criteria, the number of factors is the minimum number required in order for the cumulative common variance to meet or exceed the user-specified threshold. The table shows that three factors are sufficient to explain approximately 93% of the cumulative common variance in the original data. Thus the proportion criterion, with the threshold parameter set to 0.90, produces an estimate of three factors. For the eigenvalue criterion, the third-largest eigenvalue is larger than 1.5, whereas the fourth-largest eigenvalue is much smaller than 1.0. For this reason, the eigenvalue criterion that is specified in this analysis suggests that three factors should be retained.

Output 10.1.25: Eigenvalues and Variance Explained

NumberEigenvalueDifferenceProportionCumulative
15.8172063.9168100.58450.5845
21.9003960.3338050.19090.7754
31.5665920.8881250.15740.9328
40.6784670.6792780.06821.0009
5-0.0008110.000256-0.00011.0009
6-0.0010670.000543-0.00011.0008
7-0.0016110.000076516-0.00021.0006
8-0.0016870.000100-0.00021.0004
9-0.0017880.000670-0.00021.0002
10-0.002457_-0.00021.0000


Finally, the faNFactors action displays the table shown in Output 10.1.26, which shows the amount of time (in seconds) that it took to perform the various tasks of the analysis.

Output 10.1.26: Analysis Timing

RowIdTaskTimeRelTime
INITInitializing0.0327.94%
PARSEParsing0.000.02%
CORRCalculating Correlations0.0868.79%
NFACTDetermining Number of Factors0.000.29%
MISCMiscellaneous0.002.85%
CLEANUPCleaning Up0.000.12%
TOTALTotal0.12100.00%


In addition to estimating the number of latent factors, you can also use the faNFactors action to estimate the number of principal components that are needed to approximate an input data set. For example, you might want to determine how many components you need to retain to approximate a prespecified proportion of the total variance in the original data set. The following statements perform this analysis:

m <- cas.builtins.loadActionSet(s, actionset='factorAnalysis')
cas.factorAnalysis.faNFactors(s,
    criteria=list(list(threshold=c(0.9, 0.95, 0.99), type='proportion')),
    priors='one',
    table='testdata')

These statements demonstrate how you can specify three different threshold values for the proportion criterion. Each of these threshold values is applied separately to estimate the number of factors in the input data set. This syntax is equivalent to specifying the proportion criterion three times, using a different threshold value each time. The specification of multiple threshold values is a useful technique to assess the degree to which the analysis results are robust to different threshold values.

In this analysis, the priors parameter is used to specify that the prior communality estimates for all variables should be set to 1. When you specify this parameter, the faNFactors action estimates the number of principal components that are needed to approximate the input data set.

The "Number of Observations" and "Simple Statistics" tables for this analysis are identical to the corresponding tables in the previous analysis. The "Determination of Number of Factors" table is shown in Output 10.1.27. Of the three specified threshold values, the first value produces an estimate that three factors should be retained, and the remaining two criteria produce estimates that four factors should be retained.

Output 10.1.27: Number of Factors

CriterionDescriptionNFactorsThresholdSimulationsSeedStatus
1Total Proportion (Threshold = 0.90)30.90__1
2Total Proportion (Threshold = 0.95)40.95__1
3Total Proportion (Threshold = 0.99)40.99__1


Additional information about the number of components that are suggested by these three criteria is presented in the "Eigenvalues of the Correlation Matrix" table in Output 10.1.28. As in the previous analysis, the proportion criterion examines the cumulative variance explained by eigenvalues and corresponding principal components. For this analysis, the principal components that correspond to the three largest eigenvalues explain just under 93% of the total variance of the input data set. Thus, an estimate of three components is obtained when you set the threshold value to 0.9. When you add a fourth component, the cumulative variance explained exceeds 99%, so that four components are estimated when the threshold subparameter is specified as 0.95 or 0.99.

Output 10.1.28: Eigenvalues and Variance Explained

NumberEigenvalueDifferenceProportionCumulative
15.8218713.9168580.58220.5822
21.9050130.3334750.19050.7727
31.5715370.8887750.15720.9298
40.6827620.6772920.06830.9981
50.0054700.0010720.00050.9987
60.0043980.0011980.00040.9991
70.0032000.0007940.00030.9994
80.0024060.0006230.00020.9997
90.0017830.0002250.00020.9998
100.001559_0.00021.0000


Last updated: May 21, 2026