Text Analysis on William Ruto’s tweets with R

Almost There!
Feb 18 · 5 min read

As Kenya nears the 2022 election and a referendum in between the political climate has been rough a couple of these months.

The deputy president of Kenya William Ruto seems to pull more and more people on his bid for presidency come 2022. He resonates with the indigents as he was also born in an impoverished surrounding and through hard work he penetrated to be the second in command.

One of the distinguishing characters is how much of a great orator he is, which has enabled him to attract large following of masses. Today he is actually the most followed person on Twitter in Kenya with 3,7129,58. I tried to explore his tweets to gain insights into his public relations on Twitter and to answer the following:

  1. What are his most retweeted and liked posts?
  2. What are the most frequent words used in his posts?
  3. What mostly does he refer to when the most frequent word is used?
  4. What are the sentiments and emotions of his posts?
  5. What is the polarity (positive/negative) degree of his posts?

Through Twitter API using the rtweet package, I was only able to extract 3200 tweets as per twitter’s guidelines. The user-generated data dates from 24–11–2018 to 13–02–2021. I also removed the retweets as they do not add up as user-generated text. The data has 90 variables and 3200 observations.

Favorites and retweets are among the engagement insights that can be derived after a post is shared on the platform to gauge how viral it has gone. I, therefore, looked into his most-liked and retweeted tweet.

Image for post
Image for post

The most liked tweet apparently was an act of sympathy when Adan Duale had been stripped of his duties as the majority leader in parliament. Though the leader was diligent and instrumental in his job, his firing shocked many but was attributed to his fierce support for the deputy president.

Image for post
Image for post

As you can see there is a high correlation between the most liked and retweeted tweet, that’s how the algorithms work. The most retweeted tweet was when corruption cases around the corona pandemic had spiked on his absence despite him being called corrupt and he decided to ironically tackle his nemesis. It is also worth noting that his engagement was at its top mostly in 2020 this shows a positive regression when it comes to amassing a following.

Text Analysis

Created a corpus then cleaned it by a custom function clean.corpus and created a Term document matrix a mathematical matrix that describes the frequency of terms that occur in a collection of documents.

Image for post

It is quantitative to say that development,empowerment,education and the economy are some of the main things he is instrumental about. The ‘church’ and ‘God’ are evident as his affiliation of which he has been espousal to.

Image for post
Image for post

I looked at word association when the word ‘county’ is used as its the most frequently used. Whenever he is talking about a county 0.4 probably he is referring to Nairobi and 0.37 his Karen office. The probabilities are statistically low but highly likely when it comes to that particular word.

Sentiments scoring

Sentiment Analysis aims to detect positive, neutral, or negative feelings from the text, whereas emotion Analysis aims to detect and recognize types of feelings through the expression of texts, such as anger, disgust, fear, happiness, sadness, and surprise. I tried to access what mood does his tweets possess and among the 3200 tweets, most of them are in a joyous mood.

Image for post
Image for post

For the purpose of detecting the mood of the user, both conventional machine learning algorithms and deep learning techniques can be employed and the classification performances of each model are compared.

Image for post
Image for post

It is fair to say that the deputy president 59% of what he tweets are positive messages, 33% neutral where the number of positive and negative words cancel each other. Negative tweets sum up to a mere 8% with the ratio of positive to negative tweets been 7:1 meaning for every negative tweet ,he tweets 7 postive tweets.

What can be done

With Support-Vector Machine learning (SVM) I can build a model that detects what mood the deputy president is in when he tweets and the polarity.

Tweet classifier: If the I had tweets on different people, build a model train and test with the tweets to classify which tweet belongs to whom and what not.

Can Constraints in psychology be derived for tweets?

Curiosity satisfied!

On the next Blog I will access the political temperatures of Kenya Using the twitter data of the fore-front leaders in the upcoming 2022 presidential race.

Connect Linkedln @antonymaina

Follow twitter @antonymaina

Instagram @antony.k.maina

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…

Sign up for Analytics Vidhya News Bytes

By Analytics Vidhya

Latest news from Analytics Vidhya on our Hackathons and some of our best articles! Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Almost There!

Written by

Making sure only what needs to be known is known, and if what isn’t is known, knowing how to get it unknown. #self improvement/developement writer

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Almost There!

Written by

Making sure only what needs to be known is known, and if what isn’t is known, knowing how to get it unknown. #self improvement/developement writer

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store