Posted in

How to do principal component analysis in IML?

Principal Component Analysis (PCA) is a powerful statistical technique used for data compression, visualization, and dimensionality reduction. In the field of computational analytics, conducting PCA efficiently can significantly enhance data – driven decision – making processes. As an IML (Interactive Matrix Language) supplier, I am well – versed in guiding users on how to perform PCA in IML. This blog will walk you through the step – by – step process of implementing PCA in IML, highlighting its benefits and practical applications. IML

Understanding Principal Component Analysis

Before delving into the implementation details, let’s briefly review what PCA is. PCA aims to transform a set of correlated variables into a set of uncorrelated variables called principal components. The first principal component accounts for the largest possible variance in the data, the second principal component accounts for the next largest variance and is orthogonal to the first one, and so on. The general goal is to capture most of the data’s variability using a smaller number of components, reducing the overall dimensionality of the dataset without losing too much information.

Prerequisites for PCA in IML

To perform PCA in IML, you need to have a fundamental understanding of matrices and basic statistical concepts. Also, ensure that you have installed SAS with the IML module. Since IML operates on matrices, it is crucial to prepare your data in matrix form. Your raw data should be organized in a matrix where each row represents an observation, and each column represents a variable.

Step – by – Step Process of PCA in IML

Data Preparation

The first step in any data analysis task is to load and preprocess the data. In IML, you can read data from various sources, such as CSV files or SAS datasets. Here is an example of reading data from a SAS dataset:

PROC IML;
    /* Read data from a SAS dataset */
    USE your_sas_dataset;
    READ ALL VAR _NUM_ INTO rawData;
    CLOSE your_sas_dataset;
    
    /* Standardize the data */
    stdData = STD(rawData);
QUIT;

In this code snippet, we first use the USE statement to open a SAS dataset. The READ statement reads all numeric variables from the dataset into a matrix named rawData. Then, we close the dataset using the CLOSE statement. Since PCA is sensitive to the scale of variables, it is usually a good practice to standardize the data. The STD function standardizes each column in the rawData matrix, resulting in a new matrix stdData.

Calculating the Covariance Matrix

In PCA, the covariance matrix plays a vital role. It describes the relationships between different variables in your dataset. In IML, you can easily calculate the covariance matrix of the standardized data using the following code:

PROC IML;
    /* Assume stdData is already defined from the previous step */
    covMatrix = COV(stdData);
    PRINT covMatrix;
QUIT;

The COV function computes the covariance matrix of the stdData matrix. Printing the covariance matrix can help you gain insights into the relationships between variables in your dataset.

Eigenvalue and Eigenvector Computation

The next crucial step is to calculate the eigenvalues and eigenvectors of the covariance matrix. Eigenvalues represent the amount of variance explained by each principal component, and eigenvectors define the direction of the principal components. In IML, you can use the EIGEN function:

PROC IML;
    /* Assume covMatrix is already defined */
    call eigen(eigenVals, eigenVecs, covMatrix);
    PRINT eigenVals;
    PRINT eigenVecs;
QUIT;

The eigen function computes the eigenvalues (eigenVals) and eigenvectors (eigenVecs) of the covMatrix. By printing these values, you can understand the contribution of each eigenvector to the overall variance.

Sorting Eigenvalues and Eigenvectors

It is necessary to sort the eigenvalues in descending order and re – arrange the corresponding eigenvectors accordingly. This step ensures that the first principal component corresponds to the largest eigenvalue.

PROC IML;
    /* Assume eigenVals and eigenVecs are already defined */
    n = nrow(eigenVals);
    sortedIdx = T(COPYSORTIDX(eigenVals, descending = 1));
    sortedVals = eigenVals[sortedIdx];
    sortedVecs = eigenVecs[ , sortedIdx];
    PRINT sortedVals;
    PRINT sortedVecs;
QUIT;

In this code, we first find the number of eigenvalues using nrow. Then, the COPYSORTIDX function is used to obtain the indices that would sort the eigenvalues in descending order. We use these indices to sort both the eigenvalues and eigenvectors.

Selecting Principal Components

To determine how many principal components to retain, you can look at the proportion of variance explained by each component. The proportion of variance explained by the $i$-th principal component is given by $\frac{\lambda_i}{\sum_{j = 1}^{p}\lambda_j}$, where $\lambda_i$ is the $i$-th eigenvalue and $p$ is the total number of variables.

PROC IML;
    /* Assume sortedVals is already defined */
    totalVariance = SUM(sortedVals);
    propVariance = sortedVals / totalVariance;
    cumPropVariance = cumsum(propVariance);
    PRINT propVariance;
    PRINT cumPropVariance;
    
    /* Decide on the number of components, e.g., keep 90% of the variance */
    numComponents = LOC(cumPropVariance >= 0.9)[1];
    selectedVecs = sortedVecs[ , 1:numComponents];
QUIT;

In this code, we calculate the total variance explained by all components, then the proportion of variance for each component. The cumsum function is used to compute the cumulative proportion of variance. We then decide to keep enough components to explain at least 90% of the variance in this example.

Transforming the Data

Finally, we can transform the standardized data into the principal component space using the selected eigenvectors.

PROC IML;
    /* Assume stdData and selectedVecs are already defined */
    pcaData = stdData * selectedVecs;
    PRINT pcaData;
QUIT;

The resulting pcaData matrix contains the data in the principal component space, with reduced dimensionality.

Benefits of Using IML for PCA

There are several reasons why using IML for PCA is advantageous. Firstly, IML is highly efficient in handling matrix operations. Since PCA involves multiple matrix – based computations such as calculating the covariance matrix and performing eigen – decomposition, IML can execute these tasks quickly.

Secondly, IML provides a high – level programming environment. You can write complex algorithms for PCA without getting too involved in low – level programming details. This makes it easier for data analysts and researchers to implement custom PCA procedures.

Lastly, IML integrates well with other SAS procedures. You can seamlessly combine PCA results with other statistical analyses to gain deeper insights into your data.

Practical Applications of PCA in IML

PCA has numerous practical applications. In finance, it can be used to analyze stock price movements. By reducing the dimensionality of a large number of stock – related variables, you can identify underlying market trends more easily.

In bioinformatics, PCA can help in analyzing gene expression data. With thousands of genes measured in a single experiment, PCA can reduce the data to a more manageable size, making it easier to identify patterns and relationships between genes.

Conclusion

As an IML supplier, I am committed to providing high – quality support for users interested in performing PCA and other advanced statistical analyses in IML. The step – by – step process outlined in this blog should give you a solid foundation for implementing PCA in your own projects. By leveraging the power of IML, you can efficiently reduce the dimensionality of your data, visualize complex relationships, and make more informed decisions.

PET Transfer Film If you are interested in exploring PCA further in IML, or if you have specific requirements for data analysis, please feel free to contact us for a procurement discussion. Our team of experts is ready to assist you in finding the most suitable solutions for your needs.

References

  • SAS Institute Inc. "SAS/IML User’s Guide."
  • Jolliffe, I. T. "Principal Component Analysis." Springer, 2002.

Hangzhou Weshare Import & Export Co., Ltd.
As one of the most professional iml manufacturers and suppliers in China, we’re featured by quality products and good service. Please rest assured to wholesale customized iml made in China here from our factory. Contact us for free sample.
Address: Room B-308, XingShang Development Building, No.1243 MoGanShan Rd, HangZhou, China
E-mail: grace@weshare-china.com
WebSite: https://www.weshareprint.com/