# Correspondence Analysis of Mexican Discourses

**--Jean Arreola--**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

## Correspondence Analysis

Correspondence analysis is a multivariate statistical technique that summarizes a set of categorical data in a two dimensional form. It’s like the equivalent of Principal Component Analysis but for categorical data.

Correspondence analysis is usually applied to contigency tables. In this post, we will apply it to a frequency matrix (term document matrix from bag of words representation).

The analysis can be done by row or by column. Below is an implementation of correspondence analysis, where row and column analysis are done at the same time.

```
<span class="n">correspondence</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">ct</span><span class="p">,</span><span class="w"> </span><span class="n">ind</span><span class="p">){</span><span class="w">
</span><span class="c1">#Parameters</span><span class="w">
</span><span class="c1">#ct : contingency table (or frequency table)</span><span class="w">
</span><span class="c1">#ind: which eigenvectors (first eigenvector is ommited)</span><span class="w">
</span><span class="n">n</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">sum</span><span class="p">(</span><span class="n">ct</span><span class="p">)</span><span class="w">
</span><span class="n">rows</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">nrow</span><span class="p">(</span><span class="n">ct</span><span class="p">)</span><span class="w">
</span><span class="n">cols</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">ncol</span><span class="p">(</span><span class="n">ct</span><span class="p">)</span><span class="w">
</span><span class="c1">#Correspondence Matrix</span><span class="w">
</span><span class="n">F_fisher</span><span class="o"><-</span><span class="p">(</span><span class="n">ct</span><span class="p">)</span><span class="o">/</span><span class="n">n</span><span class="w">
</span><span class="c1">#Relative frequencies</span><span class="w">
</span><span class="n">rtot</span><span class="o"><-</span><span class="p">(</span><span class="n">apply</span><span class="p">(</span><span class="n">ct</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="n">sum</span><span class="p">))</span><span class="o">/</span><span class="n">n</span><span class="w">
</span><span class="n">ctot</span><span class="o"><-</span><span class="n">apply</span><span class="p">(</span><span class="n">ct</span><span class="p">,</span><span class="m">2</span><span class="p">,</span><span class="n">sum</span><span class="p">)</span><span class="o">/</span><span class="n">n</span><span class="w">
</span><span class="n">Dr</span><span class="o"><-</span><span class="n">diag</span><span class="p">(</span><span class="n">rtot</span><span class="p">)</span><span class="w">
</span><span class="n">Dc</span><span class="o"><-</span><span class="n">diag</span><span class="p">(</span><span class="n">ctot</span><span class="p">)</span><span class="w">
</span><span class="n">Z</span><span class="o"><-</span><span class="p">(</span><span class="nf">sqrt</span><span class="p">(</span><span class="n">solve</span><span class="p">(</span><span class="n">Dr</span><span class="p">)))</span><span class="o">%*%</span><span class="n">F_fisher</span><span class="o">%*%</span><span class="p">(</span><span class="nf">sqrt</span><span class="p">(</span><span class="n">solve</span><span class="p">(</span><span class="n">Dc</span><span class="p">)))</span><span class="w">
</span><span class="c1">#Eigenvalues and eigenvector are obtained with SVD</span><span class="w">
</span><span class="n">dvalsing</span><span class="o"><-</span><span class="n">svd</span><span class="p">(</span><span class="n">Z</span><span class="p">)</span><span class="w">
</span><span class="c1">#Two dimensional representation</span><span class="w">
</span><span class="c1">#Row analysis</span><span class="w">
</span><span class="n">Cr</span><span class="o"><-</span><span class="p">(</span><span class="nf">sqrt</span><span class="p">(</span><span class="n">solve</span><span class="p">(</span><span class="n">Dr</span><span class="p">)))</span><span class="o">%*%</span><span class="n">Z</span><span class="o">%*%</span><span class="n">dvalsing</span><span class="o">$</span><span class="n">v</span><span class="p">[,</span><span class="n">ind</span><span class="p">]</span><span class="w">
</span><span class="c1">#Column analysis</span><span class="w">
</span><span class="n">Cc</span><span class="o"><-</span><span class="p">(</span><span class="nf">sqrt</span><span class="p">(</span><span class="n">solve</span><span class="p">(</span><span class="n">Dc</span><span class="p">)))</span><span class="o">%*%</span><span class="n">t</span><span class="p">(</span><span class="n">Z</span><span class="p">)</span><span class="o">%*%</span><span class="n">dvalsing</span><span class="o">$</span><span class="n">u</span><span class="p">[,</span><span class="n">ind</span><span class="p">]</span><span class="w">
</span><span class="nf">return</span><span class="p">(</span><span class="nf">list</span><span class="p">(</span><span class="s2">"Cr"</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Cr</span><span class="p">,</span><span class="w"> </span><span class="s2">"Cc"</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Cc</span><span class="p">))</span><span class="w">
</span><span class="p">}</span><span class="w">
</span>
```

## Mexican discourses

In this post we will analize the discourses of mexican politicians, in particular, candidates for Mexico presidency. We have 11 discourses in total:

- Roberto Madrazo Pintado (PRI 2006)
- Andres Manuel Lopez Obrador (PRD 2006) (PRD 2012) (MORENA 2018)
- Enrique Peña Nieto (PRI 2012 before and after being elected)
- Josefina Vazquez Mota (PAN 2012)
- Felipe Calderon (PAN 2006)
- Ricardo Anaya Cortes (PAN 2018)
- Jose Antonio Meade Kuribreña (PRI 2018)
- Margarita Ester Zavala Gomez del Campo (Independiente 2018)

Our objective is to find patterns in the two dimensional of the discourses, that reflect information of the actual Mexico context regarding politics.

## Putting it all together

We will use the bag of words representation for the discourses. The most frequent 500 words will be chosen for the analysis, and our final term document matrix will be a 11 x 500 matrix.

Next, we see the results of the correspondence analysis appplied to our term document matrix:

## Insights

We can see that Ricardo Anaya and Roberto Madrazo are the furthest. That means in this context that they use words in their discourses that the other candidates don’t use frequently.

The three discourses from Andres Manuel are near from each other, and that was expected. And Margarita Zavala is close to Josefina Vazquez Mota. That makes sense, as their campaings are based on the idea of a woman in the presidency, so it’s logical that they use similar words in their discourses.

Another interesting insight is the closeness between Felipe Calderon and Margarita Zavala. It turns out that the team that helped Zavala in her campaign were former collaborators of Felipe Calderon, so maybe she was advised in the same way that Calderon. Check this new here.

The final insight was the closeness between Margarita Zavala and Jose Antonio Meade. Recently, Zavala has resigned from her candidacy, and, surprisingly, Jorge Camacho (former campaign chief from Zavala campaign) has anounced that he intends to vote for Meade. Perhaps he intends to vote for the candidate with the most similar ideas, and that would explain the closeness in our analysis. Check this new here.

## Final thoughts

Correspondence analysis has proven to be useful in finding patterns on frequencey matrices. We saw how some of the political news can be reflected in a discourse analysis. For future work, we can use MDS in the term frequency matrix to obtain “data points” and train a classificator! But correspondence analysis is good for a initial representation.

## Discourses

Discourses obtained from animalpolitico.com

**leave a comment**for the author, please follow the link and comment on their blog:

**--Jean Arreola--**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.