# Text analysis of Trump tweets

## Introduction

This post is to proclaim the MathematicaVsR at GitHub project “Text analysis of Trump tweets” in which we compare Mathematica and R over text analyses of Twitter messages made by Donald Trump (and his staff) before the USA president elections in 2016.

The project follows and extends the exposition and analysis of the R-based blog post "Text analysis of Trump’s tweets confirms he writes only the (angrier) Android half" by David Robinson at VarianceExplained.org; see .

The blog post  links to several sources that claim that during the election campaign Donald Trump tweeted from his Android phone and his campaign staff tweeted from an iPhone. The blog post  examines this hypothesis in a quantitative way (using various R packages.)

The hypothesis in question is well summarized with the tweet:

Every non-hyperbolic tweet is from iPhone (his staff).
Every hyperbolic tweet is from Android (from him). pic.twitter.com/GWr6D8h5ed
— Todd Vaziri (@tvaziri) August 6, 2016

This conjecture is fairly well supported by the following mosaic plots, :

We can see the that Twitter messages from iPhone are much more likely to be neutral, and the ones from Android are much more polarized. As Christian Rudder (one of the founders of OkCupid, a dating website) explains in the chapter "Death by a Thousand Mehs" of the book "Dataclysm", , having a polarizing image (online persona) is a very good strategy to engage online audience:

[…] And the effect isn’t small — being highly polarizing will in fact get you about 70 percent more messages. That means variance allows you to effectively jump several "leagues" up in the dating pecking order — […]

(The mosaic plots above were made for the Mathematica-part of this project. Mosaic plots and weekday tags are not used in .)

## Concrete steps

The Mathematica-part of this project does not follow closely the blog post . After the ingestion of the data provided in , the Mathematica-part applies alternative algorithms to support and extend the analysis in .

The sections in the R-part notebook correspond to some — not all — of the sections in the Mathematica-part.

The following list of steps is for the Mathematica-part.

1. Data ingestion
• The blog post  shows how to do in R the ingestion of Twitter data of Donald Trump messages.

• That can be done in Mathematica too using the built-in function `ServiceConnect`, but that is not necessary since  provides a link to the ingested data used :

• Which leads to the ingesting of an R data frame in the Mathematica-part using RLink.

• We have to extract device tags for the messages — each message is associated with one of the tags "Android", "iPad", or "iPhone".

• Using the message time-stamps each message is associated with time tags corresponding to the creation time month, hour, weekday, etc.

• Here is summary of the data at this stage: 3. Time series and time related distributions

• We can make several types of time series plots for general insight and to support the main conjecture.

• Here is a Mathematica made plot for the same statistic computed in  that shows differences in tweet posting behavior: • Here are distributions plots of tweets per weekday: 4. Classification into sentiments and Facebook topics

• Using the built-in classifiers of Mathematica each tweet message is associated with a sentiment tag and a Facebook topic tag.

• In  the results of this step are derived in several stages.

• Here is a mosaic plot for conditional probabilities of devices, topics, and sentiments: 5. Device-word association rules

• Using Association rule learning device tags are associated with words in the tweets.

• In the Mathematica-part these associations rules are not needed for the sentiment analysis (because of the built-in classifiers.)

• The association rule mining is done mostly to support and extend the text analysis in  and, of course, for comparison purposes.

• Here is an example of derived association rules together with their most important measures: In  the sentiments are derived from computed device-word associations, so in  the order of steps is 1-2-3-5-4. In Mathematica we do not need the steps 3 and 5 in order to get the sentiments in the 4th step.

## Comparison

Using Mathematica for sentiment analysis is much more direct because of the built-in classifiers.

The R-based blog post  uses heavily the "pipeline" operator `%>%` which is kind of a recent addition to R (and it is both fashionable and convenient to use it.) In Mathematica the related operators are `Postfix` (`//`), `Prefix` (`@`), `Infix` (`~~`), `Composition` (`@*`), and `RightComposition` (`/*`).

Making the time series plots with the R package "ggplot2" requires making special data frames. I am inclined to think that the Mathematica plotting of time series is more direct, but for this task the data wrangling codes in Mathematica and R are fairly comparable.

Generally speaking, the R package "arules" — used in this project for Associations rule learning — is somewhat awkward to use:

• it is data frame centric, does not work directly with lists of lists, and

• requires the use of factors.

The Apriori implementation in “arules” is much faster than the one in “AprioriAlgorithm.m” — “arules” uses a more efficient algorithm implemented in C.

 David Robinson, "Text analysis of Trump’s tweets confirms he writes only the (angrier) Android half", (2016), VarianceExplained.org.

 Anton Antonov, "Mosaic plots for data visualization", (2014), MathematicaForPrediction at WordPress.

 Christian Rudder, Dataclysm, Crown, 2014. ASIN: B00J1IQUX8 .

# Finding local extrema in noisy data using Quantile Regression

## Introduction

This blog post (article) describes an algorithm for finding local extrema in noisy data using Quantile Regression. The problem formulation and a solution for it using polynomial model fitting (through LinearModelFit) were taken from Mathematica StackExchange — see “Finding Local Minima / Maxima in Noisy Data”, .

The proposed Quantile Regression algorithm is a version of the polynomial fitting solution (proposed by Leonid Shifrin in ) and has the following advantages: (i) requires less parameter tweaking, and (ii) more importantly it is more robust with very noisy and oscillating data. That robustness is achieved by using two regression fitted curves: one close to the local minima and another close to the local maxima, computed for low and high quantiles respectively. (Quantile Regression is uniquely able to do that.)

The code for this blog post is available at .

A complete version of this blog post is available as a PDF document here: Finding local extrema in noisy data using Quantile Regression.pdf.

## The problem

Here is the problem formulation from .

Problem 1: We have a list of pairs of numbers representing measurements of a scalar variable on a regular time grid. The measurements have noise in them.

As a data example for this problem the author of the question in  has provided the following code:

```temptimelist = Range/10;
tempvaluelist = Sinc[#] &@temptimelist + RandomReal[{-1, 1}, 200]*0.02;
data1 = Transpose[{temptimelist, tempvaluelist}];
ListPlot[data1, PlotRange -> All, Frame -> True, ImageSize -> 500]``` In this article we are going to consider a more general problem hinted in the discussion of the solutions in .

Problem 2: Assume that the data in Problem 1 is collected several times and the noise is present in both the measured values and the time of measurement. Also the data can be highly oscillatory in nature.

Consider this data generation as an example for Problem 2:

```n = 1000;
xs = N@Rescale[Range[n], {1, n}, {0, 60}];
data2 = Flatten[
Table[Transpose[{xs + 0.1 RandomReal[{-1, 1}, Length[xs]],
Map[5 Sinc[#] + Sin[#] + 4 Sin[1/4 #] &, xs] +
1.2 RandomVariate[SkewNormalDistribution[0, 1, 0.9],
Length[xs]]}], {10}], 1];
ListPlot[data2, PlotRange -> All, Frame -> True, ImageSize -> 500]``` Note that this data has 10000 points and it is much larger than the data for Problem 1.

## Extrema location approximation by model fitting followed by nearest neighbors search

Several solutions are given in . A couple are using wavelets or Gaussian filtering for de-noising. The one we are going to focus on in this article is using polynomial fitting for extrema location approximation and then finding the actual data extrema by Nearest Neighbors (NN’s) search. It is provided by Leonid Shifrin.

Let us list the steps of that algorithm:

1. Fit a polynomial through the data (using LinearModelFit).
2. Find the local extrema of the fitted polynomial. (We will call them fit estimated extrema.)
3. Around each of the fit estimated extrema find the most extreme point in
the data by nearest neighbors search (by using Nearest).

We are going to refer to this algorithm as LMFFindExtrema. Its implementation is available here: QuantileRegressionForLocalExtrema.m, .

Here is the application of the algorithm to the data example of Problem 1: (The continuous line shows the fitted curve.)

Here is the application of the algorithm to the data example of Problem 2: It does not help to just increase the number of basis polynomials and the number of NN’s examined points: We can also see that increasing the number of examined NN’s for each of the fit estimated extrema would make some the final result points to be “borrowed” from neighboring peaks in the data.

Experiments with fitting trigonometric basis functions showed very good fit to the data but the calculations of the fit estimated extrema were very slow.

## Using Quantile Regression

It is trivial to rewrite the algorithm LFMFindExtrema to use Quantile Regression instead of linear model fitting of polynomials. We are going to use the Mathematica package  implementation described in [3,4]. The function QuantileRegression provided by  uses a B-spline basis  to find the quantile regression curves (also known as regression quantiles).

We are going to call this algorithm QRFindExtrema. Again its implementation is available here: QuantileRegressionForLocalExtrema.m, .

The algorithm QRFindExtrema has the following parameters: the data, number of B-spline knots, interpolation order, quantiles (corresponding to the curves to be fitted).

QRFindExtrema returns a list of regression quantile functions and a list of lists with extrema estimates.

More importantly though, QRFindExtrema can use two curves for finding the local extrema: one for local minima, and one for local maxima. (This feature is justified below.)

Here is the application of QRFindExtrema to the example data of Problem 1: Here is application of QRFindExtrema to the data of Problem 2: We can see that the regression quantile for 0.5 is too flat to get good judgment of the local extrema. We can get better results if we increase the number of knots for the B-spline basis built by QuantileRegression. We can see that results look “almost right”, the horizontal locations of the peaks are apparently more-or-less correctly identified, but the result extrema are too close to the fitted curve. Just increasing the number of examined NN’s for the fit estimated extrema does not produce good results because points from neighboring peaks are being chosen as final extrema estimates. In order to solve this problem we use two regression quantiles. For local minima we use a regression quantile for a low quantile number, say, 0.02; for the local maxima we use a regression quantile for a large quantile number, say, 0.98 . The use of two (or more) curves to be fitted is an unique capability of Quantile Regression. With this algorithm feature by construction the lower regression quantile is close to the local minima and the higher regression quantile is close to the local maxima.

Also, since we find two regression quantile curves we can use two nearest neighbors finding functions: one with the points below the low regression quantile, and one with the points above the high regression quantile. The implementation in  takes an option specification for should the nearest neighbor functions for finding the extrema be constructed using all data points or just the outliers (the points outside of the found regression quantiles).

## More examples

More timid noisy and oscillating data with around 10,000 points. ## References

 Mathematica StackExchange discussion. “Finding Local Minima / Maxima in Noisy Data”,
URL: http://mathematica.stackexchange.com/questions/23828/finding-local-minima-maxima-in-noisy-data/ .

 Anton Antonov, Quantile regression Mathematica package, source code at GitHub, https://github.com/antononcube/MathematicaForPrediction, package QuantileRegression.m, (2013).

 Anton Antonov, Quantile regression through linear programming, usage guide at GitHub, https://github.com/antononcube/MathematicaForPrediction, in the directory “Documentation”, (2013).

 Anton Antonov, Quantile regression through linear programming, “Mathematica for prediction algorithms” blog at WordPress.com, 12/16/2013.

 Anton Antonov, Quantile regression with B-splines, “Mathematica for prediction algorithms” blog at WordPress.com, 1/1/2014.

 Anton Antonov, QuantileRegressionForLocalExtrema Mathematica package, source code at GitHub, https://github.com/antononcube/MathematicaForPrediction, package QuantileRegressionForLocalExtrema.m, (2015).
The package is in the directory “Applications”.

# Directional quantile envelopes in 3D

## Introduction

This blog post was mostly made as a response to a comment of my previous blog post “Directional quantile envelopes”, :

This looks extremely useful and elegant – any plans on generalizing to 3 (or more) dimensions?
–Jan Liphardt

Since I did say in the previous blog post that the algorithm can be easily extended to 3D and higher dimensions, I decided to illustrate that with an example. The 3D implementation is really easy using Mathematica’s geometric computation functions (i) for derivation of rotation matrices and (ii) for regions manipulation. (The latter are new in version 10.)

The algorithm shown below is a slightly modified version of the algorithm in “Directional quantile envelopes”. Instead of the step that does the intersection of directional quantile lines with Reduce, we can use ImplicitRegion to specify the region enclosed by the directional quantile planes. We can use other built-in functions for region predicates and manipulation to derive, visualize, or utilize the directional quantile regions found by the algorithm. (All these are exemplified below.)

## Data generation

Let us first generate some data.

```npoints = 10000; data = {RandomReal[{0, 2 Pi}, npoints], RandomReal[{-Pi, Pi}, npoints], RandomVariate[PoissonDistribution[4.8], npoints]}; data = MapThread[#3*{Cos[#1] Cos[#2], Sin[#1] Cos[#2], Sin[#2]} &, data];```

Let us plot the data

```Block[{qs = 12}, qs = Map[Quantile[#, Range[0, 1, 1/(qs - 1)]] &, Transpose[data]]; ListPointPlot3D[data, PlotRange -> All, PlotTheme -> "Detailed", FaceGrids -> {{{0, 0, -1}, Most[qs]}, {{0, 1, 0}, qs[[{1, 3}]]}, {{-1, 0, 0}, Rest[qs]}}, ImageSize -> 700] ]``` On the plot the grid lines are derived from the quantiles of x, y and z coordinates of the data set.

## Algorithm application step-by-step

1. Standardize the data
1.1. This step is generally not needed and in this implementation would complicate things.

2. Create a set of uniform angles. (Here we generate vectors that define 3D directions.)

```nqs = 20; dirs = N@Flatten[ Table[{Cos[\[Theta]] Cos[\[Phi]], Sin[\[Theta]] Cos[\[Phi]], Sin[\[Phi]]}, {\[Theta], 2 \[Pi]/(10 nqs), 2 \[Pi], 2 \[Pi]/nqs}, {\[Phi], -\[Pi], \[Pi], 2 \[Pi]/nqs}], 1];```

`Graphics3D[Map[Arrow[{{0, 0, 0}, #}] &, dirs]]` 3. Rotate the data for each direction vector, a ∈ A. (Here we just generate the rotation matrices.)

```In:= rmats = RotationMatrix[{{1, 0, 0}, #}] & /@ dirs; Length[rmats]```

``` ```

`Out= 420`

4. Find the q-quantile of the z-coordinates of the data rotated at direction a. Denote that point with Z[a,q].

```In:= qs = {0.95}; qDirPoints = Flatten[Map[Function[{m}, Quantile[(m.Transpose[data])[], qs]], rmats]];```

5. Find the points z[a,q] corresponding to Z[a,q] in the original coordinate system.
5.1. In this implementation this step is redundant, see the next step.

6. Find the quantile planes intersections
6.1. Instead of doing this step we simply use ImplicitRegion.

```In:= qRegion = ImplicitRegion[ MapThread[(#1.{x, y, z})[] <= #2 &, {rmats, qDirPoints}], {x, y, z}];```

`In:= Shallow[qRegion]`

``` ```

```Out//Shallow= ImplicitRegion[ LessEqual[<>] && LessEqual[<>] && LessEqual[<>] && LessEqual[<>] && LessEqual[<>] && LessEqual[<>] && LessEqual[<>] && LessEqual[<>] && LessEqual[<>] && LessEqual[<>] && <>, {x, y, z}]```

## Wrapping it in a function

Here is a definition of a function that wraps all of the algorithms steps in the previous section.

``` Clear[QuantileEnvelopeRegion] QuantileEnvelopeRegion[points_?MatrixQ, quantile_?NumberQ, numberOfDirections_Integer] := Block[{nd = numberOfDirections, dirs, rmats, qDirPoints, qRegion}, dirs = N@Flatten[ Table[{Cos[\[Theta]] Cos[\[Phi]], Sin[\[Theta]] Cos[\[Phi]], Sin[\[Phi]]}, {\[Theta], 2 \[Pi]/(10 nd), 2 \[Pi], 2 \[Pi]/nd}, {\[Phi], -\[Pi], \[Pi], 2 \[Pi]/nd}], 1]; rmats = RotationMatrix[{{1, 0, 0}, #}] & /@ dirs; qDirPoints = Flatten[Map[ Function[{m}, Quantile[(m.Transpose[points])[], quantile]], rmats]]; qRegion = ImplicitRegion[ MapThread[(#1.{x, y, z})[] <= #2 &, {rmats, qDirPoints}], {x, y, z}]; qRegion ] /; Dimensions[points][] == 3 && 0 < quantile <= 1; ```

## Visualizing

From the implicit region specification we can generate an approximation of the boundary surface using the built-in function BoundaryDiscretizeRegion.

```qRegion = QuantileEnvelopeRegion[data, 0.95, 20]; qRegion2 = QuantileEnvelopeRegion[data, 0.8, 20];```

```qSurface = BoundaryDiscretizeRegion[qRegion]; qSurface2 = BoundaryDiscretizeRegion[qRegion2]; ```

`Grid[{{qSurface, qSurface2}}]` Now we can visualize the quantile surface together with the data points:

Block[{c = 3, opts, pntsgr},
``` opts = {PlotRange -> {{-c, c}, {0, c}, {-c, c}}, Boxed -> True, Axes -> True, ImageSize -> {Automatic, 500}}; pntsgr = Graphics3D[Point[data]]; Grid[{{Show[{qSurface, pntsgr}, opts], Show[{qSurface2, pntsgr}, opts]}}] ]``` ## Check

Now we can answer the question how many data points are inside the quantile directions regions. Again, this is easy with the built-in region functionalities of Mathematica version 10.

```In:= inPoints = Select[data, # \[Element] qRegion &]; Length[inPoints]/Length[data] // N```

``` ```

`Out= 0.5974`

```In:= inPoints = Select[data, # \[Element] qRegion2 &]; Length[inPoints]/Length[data] // N```

``` ```

`Out= 0.1705`

(In these code lines I kept “[\Element]” instead of replacing it with “∈” in order the lines to be copy and pasted. )
As we can see the algorithm would produce a region Subscript[R, q] which contains a much smaller number of points than the requested quantile, q. This property of the algorithm is discussed in . (In the two dimensional space this property is less pronounced.)

Obviously, though, we would expect the bias of the algorithm to be monotonic with respect to the quantiles requested. Given a set of points P and quantiles u and v, 0<u<v<=1, for the respectively produced by the algorithm regions Ru and Rv we have |P ∩ Ru| < |P ∩ Rv| .

## References

 Anton Antonov, Directional quantile envelopes, “Mathematica for prediction algorithms” blog at WordPress.com, 11/3/2014 .

 Linglong Kong, Ivan Mizera, “Quantile tomography: using quantiles with multivariate data”, 2013, arXiv:0805.0056v2 [stat.ME] URL: http://arxiv.org/abs/0805.0056 .

# Directional quantile envelopes

## Introduction

In this blog post I am going to discuss the computation of the so called directional quantile envelopes; see  for definitions, theorems, and concrete examples. The Mathematica package QuantileRegression.m,  has an experimental implementation, the function `QuantileEnvelope`.

This type of implementation investigation is a natural extension of the quantile regression implementations and utilization I have done before. (See the package  and the related guide ; blog posts [3,4].) I was looking for a 2D quantile regression technique that can be used as a tool for finding 2D outliers. From the exposition below it should be obvious that the technique can be generalized in higher dimensions.

The idea of directional quantile envelopes is conceptually simple (and straightforward to come up with). The calculation is also relatively simple: over a set of uniformly distributed directions we find the lines that separate the data according a quantile parameter q, 0<q<1, and with those lines we approximate the enveloping curve for data that corresponds to q.

## The algorithm

Here is the full algorithm. (The figure below can be used as a visual aid).

Parameters: data as a matrix of two columns, a quantile parameter q, 0<q<1.
1. Standardize the data.
1.1. Choose a center of the data (e.g. means of the coordinates) and a scale (e.g. inter-quartile distances).
1.2. Transpose the data to the center and scale the obtained coordinates.
2. Create a set of k uniform angles, A:={i2Pi/k}, 0<=i<=k-1 .
2.1. Denote with a[i]:=i2Pi/k, i∈[0,k-1].
3. Rotate the data for each angle, a[i]∈A.
4. Find the q-quantile of the y-coordinates of the data rotated at angle a. Denote that point with r[a,q].
5. Find the points y[a,q] corresponding to r[a,q] in the original coordinate system.
6. Find the quantile lines intersections
6.1. For each angle a∈A we denote with n[a] the vector corresponding to a.
6.2. For each a∈A we have a line l[a,q] with normal n[a] and passing through y[a,q].
6.3. Find the intersection points P[q] of the lines l[a,q].
6.3.1. Each point p[j]∈P[q], j∈[1,k] is an intersection of lines at consecutive angles.
6.3.2. In other words p[j]=l[a[j-1],q] ∩ l[a[j],q], j∈[1,k].
7. Apply the reverse of the transformation done at step 3 to P[q].
8. Connecting the consecutive points of P[q] gives the directional quantile envelope approximation.

Click this thumbnail for an image with better math notation: ## Algorithm visual aid

Here is an example of an application of the described algorithm. The points in blue are the data points, the points in red are the points y[a,q], the red lines are the lines l[a,q], the points in black are the points P. ## Remarks

Remark 1: The function QuantileEnvelope of  follows the described algorithm. The rotations are done using matrix multiplication; the function Reduce is used to find the intersections between the lines.

Remark 2: A possible alternative algorithm is to use “the usual” quantile regression with polar coordinates. (The article  refers to and briefly discusses such an approach.)

# Example data

In order to demonstrate the function `QuantileEnvelope` let us define a couple of data sets.

The first data set is generated with the function `SkewNormalDistribution`, so the data set name is `sndData`.

```npoints = 12000; data = {RandomReal[{0, 2 [Pi]}, npoints], RandomVariate[SkewNormalDistribution[0.6, 0.3, 4], npoints]}; data = MapThread[#2*{Cos[#1], Sin[#1]} &, data]; rmat = RotationMatrix[-[Pi]/2.5].DiagonalMatrix[{2, 1}]; data = Transpose[rmat.Transpose[data]]; data = TranslationTransform[{-Norm[Abs[#]/3], 0}][#] & /@ data; sndData = Standardize[data];```

Here is a plot of sndData with grid lines derived from the quantiles of x and y coordinates of the data set.

```ListPlot[sndData, PlotRange -> All, AspectRatio -> 1, PlotTheme -> "Detailed", GridLines -> Map[Quantile[#, Range[0, 1, 1/19]] &, Transpose[sndData]], ImageSize -> 700]``` The second uses the function `PoissonDistribution`, so the data set name is `pdData`.

```npoints = 12000; data = RandomReal[NormalDistribution[12, 5.6], npoints]; data = Transpose[{data, data + RandomVariate[PoissonDistribution, npoints] + RandomReal[{-0.2, 0.2}, npoints]}]; data = Select[data, #[] > 0 &]; data[[All, 2]] = Log[data[[All, 2]]]; pdData = data;```

Here is a plot of `pdData` with grid lines derived from the quantiles of x and y coordinates of the data set.

```ListPlot[pdData, PlotRange -> All, AspectRatio -> 1, PlotTheme -> "Detailed", GridLines -> Map[Quantile[#, Range[0, 1, 1/19]] &, Transpose[pdData]], ImageSize -> 700]``` ## Examples of application (with QuantileEnvelope)

Let us demonstrate the function `QuantileEnvelope`. The function arguments are a data matrix, a quantile specification, and number of directions. The quantile specification can a number or a list of numbers. The function returns the intersection points of the quantile lines at different directions.

Here is an application to the dataset `sndData` over 8 different quantiles over 60 different uniformly spread directions.

```In:= qs = {0.7, 0.75, 0.8, 0.85, 0.90, 0.95, 0.98, .99} ; AbsoluteTiming[qsPoints = QuantileEnvelope[sndData, qs, 60];] Out= {0.150104, Null}```

The result is a list of 8 lists of 60 points.

```In:= Dimensions[qsPoints] Out= {8, 60, 2}```

Let us plot the data and the direction quantile envelopes.

```Block[{data = sndData}, Show[{ListPlot[data, AspectRatio -> 1, PlotTheme -> {"Detailed"}, GridLines -> Map[Quantile[#, Range[0, 1, 1/(20 - 1)]] &, Transpose[data]], PlotLegends -> SwatchLegend[ Blend[{Red, Orange}, Rescale[#1, {Min[qs], Max[qs]}, {0, 1}]] & /@ qs, qs]], Graphics[{PointSize[0.005], Thickness[0.0025], MapThread[{Blend[{Red, Orange}, Rescale[#1, {Min[qs], Max[qs]}, {0, 1}]], Tooltip[Line[Append[#2, #2[]]], #1], Point[#2]} &, {qs, qsPoints}]}] }, ImageSize -> 700]]``` Let us similar calculations for the dataset pdData.

```In:= qs = {0.7, 0.75, 0.8, 0.85, 0.90, 0.95, 0.98, 0.99} ; AbsoluteTiming[qsPoints = QuantileEnvelope[pdData, qs, 60];] Out= {0.154227, Null} ```

```In:= Dimensions[qsPoints] Out= {8, 60, 2}```

Here is the plot of the dataset `pdData` and directional quantile envelopes. ## Some more quantile curves

Here are couple of more plots of the quantile lines at different directions with a larger number of directions.

Lines for quantile 0.95 in 40 directions: Lines for quantiles 0.95 and 0.8 in 40 directions: ## References

 Anton Antonov, Quantile regression Mathematica package, source code at GitHub, https://github.com/antononcube/MathematicaForPrediction, package QuantileRegression.m, (2013).

 Anton Antonov, Quantile regression through linear programming, usage guide at GitHub, https://github.com/antononcube/MathematicaForPrediction, in the directory “Documentation”, (2013).

 Anton Antonov, Quantile regression through linear programming, “Mathematica for prediction algorithms” blog at WordPress.com, 12/16/2013.

 Anton Antonov, Quantile regression with B-splines, “Mathematica for prediction algorithms” blog at WordPress.com, 1/1/2014.

 Linglong Kong, Ivan Mizera, “Quantile tomography: using quantiles with multivariate data”, 2013, arXiv:0805.0056v2 [stat.ME] URL: http://arxiv.org/abs/0805.0056 .

# Classification and association rules for census income data

### Introduction

In this blog post I am going to show (some) analysis of census income data — the so called “Adult” data set,  — using three types of algorithms: decision tree classification, naive Bayesian classification, and association rules learning. Mathematica packages for all three algorithms can be found at the project MathematicaForPrediction hosted at GitHub, [2,3,4].

(The census income data set is also used in the description of the R package “arules”, .)

In the census data every record represents a person with 14 attributes, the last element of a record is one of the labels {“>=50K”,”<50K”}. The relationships between the categorical variables in that data set was described in my previous blog post, “Mosaic plots for data visualization”.

For this data the questions I am most interested in are:
Question 1: Which of the variables (age, occupation, sex, etc.) are most decisive for determining the income of a person?
Question 2: Which values for which variables form conditions that would imply high income or low income? (I.e. “>50K” or “<=50K”.)
Question 3: What conclusions or confirmations we can get from answering the previous two questions?

One way to answer Question 1 is to use following steps, .
1. Build a classifier with the training set.
2. Verify using the test set that good classification results are obtained.
3. If the number of variables (attributes) is k for each i, 1<=i<=k :
3.1. Shuffle the values of the i-th column of the test data and find the classification success rates.
4. Compare the obtained k classification success rates between each other and with the success rates obtained by the un-shuffled test data.
5. The variables for which the classification success rates are the worst are the most decisive.

Following these steps with a decision tree classifier, , I found that “marital-status” and “education-num” (years of education) are most decisive to give good prediction for the “>50K” label. Using a naive Bayesian classifier, , the most significant variables are “marital-status” and “relationship”. (More details are given in the sections “Application of decision trees” and “Application of naive Bayesian classifier”.)

One way to answer Question 2 is to find which values of the variables (e.g. “Wife”, “Peru”, “HS-grad”, “Exec-managerial”) associate most frequently with “>50K” and “<=50K” respectively and apply different Bayesian probability statistics on them. This is what the application of Associative rules learning gives, . Another way is to use mosaic plots, [5,9], and prefix trees (also known as “tries”) [6,11,12].

In order to apply Association rule learning we need to make the numerical variables categorical — we need to partition them into non-overlapping intervals. (This derived, “all categorical” data is also amenable to be input data for mosaic plots and prefix trees.)

Insights about the data set using Mosaic Plots can be found in my previous blog post “Mosaic plots for data visualization”, . The use of Mosaic Plots in  is very similar to the Naive Bayesian Classifiers application discussed below.

### Data set

The data set can be found and taken from http://archive.ics.uci.edu/ml/datasets/Census+Income, .

The description of the data set is given in the file “adult.names” of the data folder. The data folder provides two sets with the same type of data “adult.data” and “adult.test”; the former is used for training, the latter for testing.

The total number of records in the file “adult.data” is 32561; the total number of records in the file “adult.test” is 16281.

Since I did not understand the meaning of the column “fnlwgt” I dropped it from the data.

As it was mentioned in the introduction, only 24% of the labels are “>50K”. Also note that 2/3 of the records are for males.

### Scatter plots and mosaic plots

Often scatter plots and mosaic plots can give a good idea of the general patterns that hold in the data. This sub-section has a couple of examples, but presenting extensive plots is beyond the scope of this blog post. Let me point out that it might be very beneficial to use these kind of plots with Mathematica‘s dynamic features (like Manipulate and Tooltip), or make a grid of mosaic plots.

Mosaic plots of the categorical variables of the data can be seen in my previous blog post “Mosaic plots for data visualization”.

Here is a table of the histograms for “age”, “education-num”, and “hours-per-week”: Here is a table with scatter plots for all numerical variables of the data: ### Application of decision trees

The building and classification with decision trees is straightforward. Since the label “>50K” is only a quarter of the records I consider the classification success rates for “>50K” to be more important. I experimented with several sets of parameters for decision tree building. I did not get a classification success rate for “>50K” better than 0.644 . Using pruning based on the Minimal Description Length (MDL) principle did not give better results. (I have to say I find MDL pruning to be an elegant idea, but I am not convinced that it works that
well. I believe decision tree pruning based on test data would produce much better results. Only the MDL decision tree pruning is implemented in .)

The overall classification success rate is in line with the classification success ratios listed in explanation of the data set; see the file “adult.names” in .

Here is a table with the results of the column shuffling experiments described in the introduction (in red is the name of the data column shuffled): Here is a plot of the “>50K” success rates from the table above: We can see from the table and the plot that variables “marital-status”, “education-num”, “capital-gain”, “age”, and “occupation” are very decisive when it comes to determining high income. The variable “marital-status” is significantly more decisive than the others.

While considering the decisiveness of the variable “marital-status” we can bring the following questions:
1. Do people find higher paying jobs after they get married?
2. Are people with high paying jobs more likely to marry and stay married?

Both questions are probably answered with “Yes” and probably that is why “marital-status” is so decisive. It is hard to give quantified answers to these questions just using decision trees on this data — we would need to know the salary and marital status history of the individuals (different data) or to be able to imply it (different algorithm).

We can see the decisiveness of “age”, “education-num”, “occupation”, and “hours-per-week” as natural. Of course one is expected to receive a higher pay if he has studied longer, has a high paying occupation, is older (more experienced), and works more hours per week. Note that this statement explicitly states the direction of the correlation: we do assume that longer years of study bring higher pay. It is certainly a good idea to consider the alternative direction of the correlation, that people first get high paying jobs and that these high paying jobs allow them to get older and study longer.

### Application of naive Bayesian classifiers

The naive Bayesian classifier, , produced better classification results than the decision trees for the label “>50K”: Here is a table with the results of the column shuffling experiments described in the introduction (in red is the name of the data column shuffled): Here is a plot of the “>50K” success rates from the table above: In comparison with the decision tree importance of variables experiments we can notice that:
1. “marital-status” is very decisive and it is the second most decisive variable;
2. the most decisive variable is “relationship” but it correlates with “marital-status”;
3. “age”, “occupation”, “hours-per-week”, “capital-gain”, and “sex” are decisive.

### Shuffled classification rates plots comparison

Here are the two shuffled classification rates plots stacked together for easier comparison: ### Data modification

In order to apply the association rules finding algorithm Apriori, , the data set have to be modified. The modification is to change the numerical variables “age”, “education-num”, and “age” into categorical. I just partitioned them into non-overlapping intervals, labeled the intervals, and assigned the labels according the variable values. Here is the summary of the modified data for just these variables: ### Finding association rules

Using the modified data I found a large number of association rules with the Apriori algorithm, . I used the measure called “confidence” to extract the most significant rules. The confidence of an association rule AC with antecedent A and consequent C is defined to be the ratio P(AC)/P(C). The higher the ratio the more confidence we have in the rule. (If the ratio is 1 we have a logical rule, CA.)

Here is a table showing the rules with highest confidence for the consequent being “>50K”: From the table we can see for example that 2.1% of the data records (or 693 records) show that for a married man who has studied 14 years and originally from USA there is a 0.79 probability that he earns more than \$50000.

Here is a table showing the rules with highest confidence for the consequent being “<=50K”: The association rules in these tables confirm the findings with the classifiers: marital status, age, and education are good predictors of income labels “>50K” and “<=50K”.

### Conclusion

The analysis confirmed (and quantified) what is considered common sense:

Age, education, occupation, and marital status (or relationship kind) are good for predicting income (above a certain threshold).

Using the association rules we see for example that
(1) if a person earns more than \$50000 he is very likely to be a married man with large number of years of education;
(2) single parents, younger than 25 years, who studied less than 10 years, and were never-married make less than \$50000.

### References

 Bache, K. & Lichman, M. (2013). UCI Machine Learning Repository [http://archive.ics.uci.edu/ml]. Irvine, CA: University of California, School of Information and Computer Science. Census Income Data Set, URL: http://archive.ics.uci.edu/ml/datasets/Census+Income .

 Antonov, A., Decision tree and random forest implementations in Mathematica, source code at https://github.com/antononcube/MathematicaForPrediction, package AVCDecisionTreeForest.m, (2013).

 Antonov, A., Implementation of naive Bayesian classifier generation in Mathematica, source code at GitHub, https://github.com/antononcube/MathematicaForPrediction, package NaiveBayesianClassifier.m, (2013).

 Antonov, A., Implementation of the Apriori algorithm in Mathematica, source code at https://github.com/antononcube/MathematicaForPrediction, package AprioriAlgorithm.m, (2013).

 Antonov, A., Mosaic plot for data visualization implementation in Mathematica, source code at GitHub, https://github.com/antononcube/MathematicaForPrediction, package MosaicPlot.m, (2014).

 Antonov, A., Tries with frequencies Mathematica package, source code at GitHub, https://github.com/antononcube/MathematicaForPrediction, package TriesWithFrequencies.m, (2013).

 Hahsler, M. et al., Introduction to arules [Dash] A computational environment for mining association rules and frequent item sets, (2012).

 Breiman, L. et al., Classification and regression trees, Chapman & Hall, 1984.

 Wikipedia, Association rules learning, http://en.wikipedia.org/wiki/Association_rule_learning .

 Antonov, A., Mosaic plots for data visualization, (March, 2014), MathematicaForPrediction at GitHub, URL: https://github.com/antononcube/MathematicaForPrediction/blob/master/Documentation/Mosaic%20plots%20for%20data%20visualization.pdf .

 Wikipedia, Trie, http://en.wikipedia.org/wiki/Trie .

 Antonov, A., Tries, (December, 2013), URL: https://github.com/antononcube/MathematicaForPrediction/blob/master/Documentation/Tries.pdf .

 Antonov, A., Mosaic plots for data visualization, (March, 2014) MathematicaForPrediction at WordPress.