Elon Musk

Elon Musk

James Lee

A frequency and sentiment analysis on Elon’s “Musky” relationship with Twitter

Twitter is a powerful microblogging tool that has transformed conversation since its inception- for better or worse. There is so much data available because of its millions of globally active users to harvest and utilize.

Elon Musk, CEO of Tesla and SpaceX, has been the subject of controversies because of his recent tweets. I thought since he is one of the famous men alive, analyzing his conduct and reputation on Twitter would be interesting.

I divided my project into several components: Analysis on Musk’s tweeting frequency, sentiment analysis on Musk’s tweets, sentiment analysis on tweets about Musk, with word clouds as products.

I did my project with R and Twitter’s API. Link to set it up API here.

The code below established the connection between twitter and R Studios:

twitter_token<-create_token(app = "joysentiment1", api_key, api_secret, accessToken, accessTokenSecret, set_renv = TRUE)

I went on to extract Elon Musk’s 5,000 tweets afterwards.

em_tweets<-get_timeline("ElonMusk",n=5000)

Part 1: Tweets Frequency

I first looked at Elon Musk’s tweeting habits over the past couple years. He joined Twitter in 2009 but only his tweets since the year 2016 are available.

Elon Musk’s 5000 Tweets Frequency
ggplot(data = em_tweets,
aes(x=em_tweets$month,group=factor(year(created_at)), color=factor(year(created_at)))) +
geom_line(stat="count") +
geom_point(stat="count") +
labs(x="Month", colour="Year",y="Number of tweets") +
theme_classic()

Elon Musk notably started tweeting more frequently in the months of May and June. Some of his most memorable tweets this past May include the announcement of his idea to create a Yelp for journalists.

Yelp for journalists…

I created another visualization of his tweets frequency over the course of 12 months, which unsurprisingly also that he is most active on Twitter when in the months of May, June and July.

Elon Musk’s Tweets Frequency over 12 months
ggplot(data = em_tweets, aes(x = em_tweets$month)) +
geom_bar(aes(fill = ..count..)) +
xlab("Month") + ylab("Number of tweets") +
theme_classic()

This gives rise to an important question: Why has Musk been spending more time tweeting than he has in the past?

Part Two: Sentiment analysis on Musk’s tweets

Sentiment analysis is the process of quantifying and categorizing words towards a particular emotion/product. I was introduced to the concept in class and became more interested after reading about it here.

I went to pursue a sentiment analysis on Elon Musk’s 5,000 tweets and was curious to see how this sentiment changed this past year. I took advantage of the “NRC” lexicon, which categorizes words into sentiments such as “happy”, “sad”, etc.

I saw that Elon Musk’s tweets are dominantly rooted in positive, trust and anticipation sentiments in my first visualization.

Sentiment Analysis On Elon Musk’s Tweets
ggplot(data=primaryscores,aes(x=sentiment,y=Score))+
geom_bar(aes(fill=sentiment),stat = "identity")+
xlab("Sentiments")+ylab("Scores")+
ggtitle("Total sentiment based on scores")+
theme(legend.position="none")+
theme_minimal()

I also wanted to see how his sentiment has changed over the past couple months in 2018.

Sentiment Analysis on Elon Musk’s Tweets the past year
ggplot(data=monthly_sentiment,aes(x=month,y=value,group=sentiment))+
geom_line(aes(color=factor(sentiment)))+
geom_point(aes(color=factor(sentiment)))+
labs(x="Month", colour="Sentiment",y="Value")+
theme_minimal()

The positive sentiments spiked between the months of April and June. But we can see that anger, negative, anticipation and trust sentiments also experienced a similar increase. This finding makes sense, given his many public Twitter quarrels.

An example of a public dispute

Elon frequently tweets about his life experiences in a positive manner, especially about his companies: Tesla and SpaceX.

An example of a positive tweet

Another aspect I wanted to cover is his vocabulary usage. The most effective way to do so was by creating a word cloud, something I realized after I read this guide.

This proved to be the most challenging part of this task as I had to create a list of stop words that include and are not limited to: just, like, way, also, actually.

Stop words are used to remove words that don’t add value into the analysis. It was difficult to completely remove all the stop words since these words are the backbone of his tweets.

Word cloud for Elon Musk’s tweets
wordcloud(em_text_corpus,min.freq=1,max.words=50,scale=c(2.5,1),colors=brewer.pal(8,"Accent"),random.color=T,random.order=F)

Elon Musk uses his twitter account to talk about his work in Tesla, The Boring Company, and SpaceX as evident in this word cloud. His pride in his companies and optimistic outlook on the future is reflected in his tweets.

Part 3: Sentiment analysis on tweets about Elon Musk

The beauty of Twitter is the ability to join in on other people’s conversations. I took advantage of this feature to see how Elon Musk is depicted by most users.

I did so by doing another sentiment analysis. I went to mine 1,000 tweets.

elon_raw<-searchTwitter("elon",lang="en",n=1000)

I performed another sentiment analysis but this time by assigning scores to each word: Positive words have scores of greater than 0 and negative words have scores of less than 0. The distribution of scores of tweets about Elon is as follows:

I created another word cloud by implementing the technique I had above to see what the popular words are.

Word Cloud on tweets about Elon Musk
wordcloud(elon_text_corpus,min.freq=1,max.words=50,scale=c(2.5,1),colors=brewer.pal(8,"Accent"),random.color=T,random.order=F)

It appears that Twitter users don’t recriprocate Elon Musk’s positive sentiments. Most of Twitter users talk about his recent pursuits, scandals, and personal life, instead of his work in his companies. It is the case of the cliche saying, two sides to a coin.

The effectiveness of Twitter depends on the user itself. While some argue that Elon Musk must soon change his tweeting habits if he doesn’t want to hurt his companies, this fate is still uncertain. It is evident that in spite of his positive tweets, most people don’t necessarily feel the same way.

This project was fun and challenging to do. I’ll upload the extended code on my GitHub. If you want to learn more about it and the code I used for sentiment analysis, I’ve included some references at the bottom of the page.

I initially had difficult setting up the API, creating the list of stop words and implementing the code. There are limitations to my findings so feel free to give me feed back if you ike.

To conclude, here’s one of Elon Musk’s very best.

I guess he’s funny sometimes

Important Packages:

library(wordcloud)
library(httr)
library(rtweet)
library(twitteR)
library(plyr)
library(ggplot2)
library(devtools)
library(tm)
library(dplyr)
library(stringr)
library(tidytext)
library(lubridate)

Reference:

Setting up code for sentiment analysis

https://www.slideshare.net/ajayohri/twitter-analysis-by-kaify-rais

Sample project

https://analyzecore.com/2014/04/28/twitter-sentiment-analysis/


Report Page