Saturday, May 5, 2012

An Intuitive Approach to Text Mining with SAS IML

Text Mining- in Plain English: Turning Text into Numbers

With Twitter, Facebook, email, online forums, open response surveys, customer and reader comments on web pages and news articles etc. there is a lot of information available to companies and organizations in the form of text. Without hiring experts to read through all of the thousands of pages worth of text available and making subjective claims about its meaning, text mining allows us to take otherwise unusable 'qualitative' data and convert it into quantitative measures that we can use for various types of reporting and modeling. In the example below I demonstrate how the mathematical technique of singular value decomposition  (SVD) can be used to do this.

Sometimes to assess statistical techniques, or to even understand them at a basic level, you need data with properties you understand. With numerical data, that typically might call for simulation. But since we are dealing with text, I just made some up that will work to clearly demonstrate the effectiveness of SVD.

Below you will see 10 hypothetical comments to the question- 'What is your take on pink slime?' Each person's response is considered a document, and I have classified each document as type 'H' or 'S' as explained below.  The goal will be to see if we can use a basic application of SVD to convert these comments into numbers and use them to predict what type of person  made which type of comments (i.e. does a comment belong in category H or S) based on clustering or some type of predictive model. This gets way beyond simply classifying comments by doing a key word search. SVD allows us to not only classify documents by the specific words they contain, but also by how similar they are.

 The purpose of this exercise is to provide intuition for text mining and the application of singular value decomposition. The text above is made up, and specifically designed to produce the results I'm after. I'm not making any claims one way or the other about how realistic this is. But suppose these are potential customers and we want to be able to distinguish between the hippies, who favor a more local nostalgic food supply from centuries past  (designated as class or type 'H') and the animal scientists, designated as type 'S.' Obviously comments of class H are critical of modern agriculture and technology.  We might want to make these distinctions for PR, marketing, lobbying,or educational outreach purposes.  

After cleaning up the text (which many software programs like SAS Enterprise Miner provide excellent tools for doing so)by eliminating parts of speech, articles, etc. we can form a term-document frequency matrix as follows:




Singular Value Decomposition

Singular Value Decomposition (SVD) is a concept from linear algebra based on the following matrix equation:

A = USV which states that a rectangular matrix A can be decomposed into 3 other matrix components:

U  consists of the orthonormal eigenvectors of AA’,  where U’U =  I (recall from linear algebra, orthogonal vectors of unit length are ‘orthonormal’)
V consists of the orthonormal eigenvectors of A’A
S  is a diagonal matrix consisting of the square root of the eigenvalues of U or V (which are equal). The values in S depict the variance of linearly independent components along each dimension similarly to the way eigenvalues depict variance explained by ‘factors’ or components in principle components analysis (PCA).

SVD provides the mathematical foundation for text mining.

A  term document matrix A can be decomposed as in:
A = USV’

If the term document matrix A were a collection of individuals’ textual responses or comments to a survey question (or Facebook post etc. ) then each individual’s response would be considered a ‘document’. The individual words they used in their response are the ‘terms.’ The term document frequency matrix consists of rows that represent each term and columns that represent each ‘document’ or individual. 

The  vectors in U can be used to score documents in the term-document matrix, as in U’A.  (this may be analogous to the way values of x are scored by eigenvectors in PCA)  As a result, a single numerical value or ‘score’ can be assigned to each person’s textual response (i.e. each document) for each SVD dimension (i.e. each kept independent vector in U). Thus the text is converted into numerical scores (via the transformation U’A) that can then be used in predictive modeling (as predictor variables) or  clustering can be utilized to cluster the individual textual responses (or ‘documents’).  So ultimately SVD converts  otherwise unuseful  text into numeric SVD scores or more interpretable clusters.  Likewise, using  the weights from the eigenvectors that comprise  V to score the terms in the term document matrix A, as in AV’ we can score  cluster like terms as ‘topics’. 

Thus SVD of A gives allows us to derive the following scores:

U’A = SVD document vectors 
AV’ = SVD term vectors

 This can be loosely demonstrated in using PROC IML in SAS. Specifying the term document frequency matrix in PROC IML and implementing SVD produces:









 
The document vectors U’A can be depicted as follows:


 
Here is the original text document with customer types/classes and the appended SVD scores. 

 As depicted above, SVD has allowed us to replace all of the text with the quantitative values for the associated SVD scores (derived from U'A). In this case, I'm representing all of the text with just two SVD dimensions. These values can then be used for clustering or entered into a predictive model.


 
It is easy to see that the different documents types (response types H vs S) cluster very well on the dimensions SVD1 and SVD2. In fact, all responses of type S have a value of SVD2 < .5.
The SVD values can also be entered into a regression or other type of predictive model.

 
Consistent with the observed relationship between SVD2 and class ‘S’ and ‘H’ clusters, we see that SVD2 is significant in the regression. In addition, it shows that higher values of SVD2 decrease the probability of being classified as document or response type ‘S’. Yes, this is OLS on a binary dependent variable, but again the purpose is to provide intuition and motivation for using text analytics for predictive modeling. 

This seemed to work ok on a small collection of documents carefully constructed to illustrate the concepts above, but tools like SAS Text Miner in conjunction with SAS Enterprise Miner are designed specifically to do this type of analysis on a much larger scale. I have used both of these tools on much larger document collections and obtained promising results using text topics from SVD in predictive modeling applications.

SAS CODE


proc iml;
       A = {0 0      0      1      0      0      0      0      0      0,
1      1      0      0      0      1      0      0      0      0,
0      0      0      0      0      1      1      1      0      2,
0      0      0      0      0      1      0      0      0      0,
0      1      1      0      0      0      0      0      0      0,
1      0      0      0      0      0      0      0      0      0,
0      0      1      0      1      0      0      0      0      0,
0      0      0      0      1      0      0      0      0      0,
0      0      0      0      1      0      0      0      0      0,
0      0      1      0      0      0      0      0      0      0,
0      0      0      0      0      1      0      1      0      1,
0      0      1      0      0      0      0      0      0      0,
0      1      1      1      0      0      0      0      0      0,
0      0      0      1      0      0      0      0      0      0,
0      0      0      0      0      0      1      1      0      0,
1      0      1      0      0      0      0      0      0      0,
0      0      0      0      0      0      1      0      0      0,
0      0      0      0      0      0      0      0      0      1,
0      0      0      0      0      1      0      0      0      0,
0      0      1      0      0      0      0      0      0      0,
1      0      0      0      1      0      0      0      0      0,
0      1      0      1      0      0      0      0      0      0,
0      1      0      0      0      1      0      0      0      0,
0      0      0      0      0      0      0      1      0      0,
0      0      0      0      0      0      0      0      0      1,
0      0      0      0      0      0      0      0      1      1,
0      0      0      0      0      0      0      0      0      1,
1      0      0      0      1      0      0      0      0      0,
0      0      0      0      0      0      1      1      1      0,
0      0      1      0      0      0      0      0      0      0,
0      0      0      0      0      0      0      0      1      0,
0      0      0      0      0      0      0      0      0      1,
0      0      0      0      0      1      0      1      0      0,
0      0      0      0      0      0      0      0      1      0
};
       print(A); /* print matrix A*/
       n = nrow(A); /* how many rows */
       p = ncol(A); /* how many columns */
       print(n);   
       print(p);
       call svd(u,s,v,A); /* sigular value decomposition of A = usv'*/
       print(u); /* independent eigenvectors of AA' */
       print(s); /*  independent eigenvectors of A'A */
       print(v); /*singular values (sqrt(eigenvalues)) of AA' or A'A */
       uTa = T(u)*A; /* document vectors*/
       avT = A*T(v); /* term vectors */
       print(uTa);
       print(avT);
    /* scoring a data set */
       ID = {1, 2, 3, 4, 5, 6, 7, 8, 9,10}; /* create a document id matrix */
       print(ID);
       docscores =ID||T(uTa); /* combine with svd results */
       print(docscores);
       /* export as a SAS data set */
       varnames = 'svd1':'svd10';
       create svd_scores from docscores[colname = varnames];
       append from docscores;
       close svd_scores;

quit;
run;

 *-------------------------------------------------------------------------------------*
 |  SCORING, CLUSTERING AND PREDICTIVE MODELING
 *-------------------------------------------------------------------------------------*;

* CREATE CLIENT DATA SET;

DATA CLIENTS;
       INPUT ID CLASS $ ;
       CARDS;
1      H
2      H
3      H
4      H
5      H
6      S
7      S
8      S
9      S
10  S
;
RUN;

* MERGE WITH SVD SCORES;

PROC SQL;
       CREATE TABLE CLIENTS_SCORED AS
       SELECT A.ID, A.CLASS, B.SVD2 AS SVD1, B.SVD3 AS SVD2
       FROM CLIENTS A LEFT JOIN SVD_SCORES B
       ON A.ID = B.SVD1;
QUIT;

PROC PRINT DATA = CLIENTS_SCORED;
RUN;


* CLUSTER/ VISUALIZE  CLIENTS BASED ON SVD SCORES;
PROC GPLOT DATA = CLIENTS_SCORED;
       PLOT SVD1*SVD2 = ID;
RUN;
QUIT;


* RECODE FOR NUMERIC DEPENDENT VAR;

DATA CLIENTS_SCORED2;
       SET CLIENTS_SCORED;
       IF CLASS = 'H' THEN Y = 0;
       ELSE Y = 1;
RUN;

* BASIC REGRESSION MODEL;

PROC REG DATA = CLIENTS_SCORED2;
       MODEL Y = SVD1 SVD2;
RUN;
QUIT;

Tuesday, May 1, 2012

SAS Global Forum Followup

Last week I attended SAS Global Forum held in Orlando, Florida. It was a great opportunity to meet some very interesting SAS users, contributors, and developers like David Dickey and  Rick Wicklin. I also had the opportunity to co-present a paper that my colleagues and I wrote that demonstrated one of the great things about SAS, the integrated interoperable enterprise wide platform SAS products provide for business intelligence that surpasses routine reporting and statistical analysis.  There were also several sessions that I attended that I would like to highlight and others I missed.

SAS Global Forum is in many ways like a crash course post graduate education crammed into just about 3 days of presentations.  It is impossible to attend all of the sessions of interest, and even though the papers are all posted online, it is still possible to miss something if you are not careful. But that is what is so great about it! It has so much to offer. My coworkers and I plan to compare notes soon to cover more ground.

One thing about SAS Global Forum is you have to pace yourself. You will come back from this conference with lots of ideas. You can't implement them all, at least not at once. Leftover from last year is utilizing the %GetTweet macro for social network analysis and text mining. And,  I'm only just now scratching the surface of utilizing copulas in SAS, (another paper topic from last year).

This year, my favorite presentation was Handling Missing Data by Maximum Likelihood, given by Paul Allison. Not only is Allison a prolific writer in the area of statistics and SAS, but everything I have ever read from Allison is very clear, concise,and easy to understand. For the most part, I have dealt with missing data imputation in the SAS Enterprise Miner Environment. Being designed for large data sets, multiple imputation is not a default option in the SAS EM environment. It does offer some attractive options beyond mean imputation, including M-estimators. However, I could imagine using the maximum likelihood methods Allison discusses in projects outside of the SAS Enterprise Miner Environment, or prior to importing the data for a project into SAS Enterprise Miner (or even via a code node within the SAS Enterprise Miner Environment).

I attended several sessions related to text mining. My favorites were Analyzing sentiments in Tweets about Wal-Mart’s gender discrimination lawsuit verdict using SAS® Text Miner,  and Classification of Customers’ Textual Responses via Application of Topic Mining .   Both of these papers (as well as last year's %GetTweet article and several more)  were coauthored by Dr. Goutam Chakraborty of the Spears School of Business at Oklahoma State University. Dr. Chakraborty (who also founded the SAS and OSU Data mining and Business Analytics certificate programs)  always had interesting commentary during the question and answer segments and the students always presented very interesting text mining applications. There was also a very good text mining poster, Investigating Host Plant Resistance to Aphid Feeding through SAS® Text Miner, also coauthored by Dr. Chakraborty.

Three other papers that I missed due to scheduling conflicts included PROPENSITY SCORE ANALYSIS AND ASSESSMENT OF PROPENSITY SCORE APPROACHES USING SAS® PROCEDURES , Your “Survival” Guide to Using Time‐Dependent Covariates and Use of Cutoff and SAS Code Nodes in SAS® Enterprise Miner™ to Determine Appropriate Probability Cutoff Point for Decision Making with Binary Target Models . I have explored the concept of matching before, and even got some interesting comments from Andrew Gelman and Joshua Angrist regarding my interpretation, but have yet implemented this in a project. (although there are plenty of applications). I have already began a survival analysis project, and have recently spent some time investigating how to reference time dependent covariates in SAS.   In terms of the paper on probability cutoffs, I have tried to figure this out in SAS Enterprise Miner before, but have not really spent much time with it.  I really need to read all three papers above!

One other paper that I missed, that my wife might be interested in was  Using SAS® and Zip Codes to Create a Nationwide First Responders Directory.

Two other great sessions that I attended incorporated LaTeX and PERL regular expressions.  I'm not sure if I will actually utilize the regular expressions, but the LaTeX capability might enable our office to not only produce reproducible research, but also produce really cool training documents that illustrate SAS code or possibly allow me to create documents for my statistics students demonstrating SAS applications.

I didn't attend any sessions utilizing SAS IML, but I hope to utilize it to develop social network analysis metrics, such as eigenvector centrality. I think IML is a very powerful way to extend the capability of SAS. While I didn't attend a session for this, I truly enjoyed sitting down with Rick Wicklin at the demo hall and having him code through some simulations and extract eigenvectors! Still looking forward to a post from him on how to extract only the leading eigenvector from a large symmetric matrix in IML!

Finally, as a statistics instructor, I'm becoming more interested in utilizing SAS On Demand for educators in my classes. Unfortunately, that's another paper presentation that I missed!

SAS Global Forum is a truly awesome and inspiring experience! Its a lot to take in, and well worth the cost and effort of attending. (PS don't forget the upcoming Analytics 2012 conference also hosted by SAS!)