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
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 .)
- The Mathematica part: PDF file, Markdown file.
The R part consists of :
the blog post , and
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.
- 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:
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:
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:
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.
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
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.
 David Robinson, "Text analysis of Trump’s tweets confirms he writes only the (angrier) Android half", (2016), VarianceExplained.org.
 Christian Rudder, Dataclysm, Crown, 2014. ASIN: B00J1IQUX8 .