This is a Notebook for Twitter Sentiment Analysis on CVS Health and CVS Aetna merger.

# Load Requried Packages
library("SnowballC")
library("tm")
library("twitteR")
library("syuzhet")
library("wordcloud")

Setting up Twitter OAuth

consumer_key <-  'elVqMFVWgCUQBEDOR7vsfFsI7'
consumer_secret <- '5LizGPvZIZoHYojLhYFrfFXm2KjjIeCPvFwNiIhcOfdnAGzrKO'
access_token <- '380945634-TLTP7C0llQjRu4S5jPJvHKadGJnQ0V8m9DPrAhur'
access_secret <- 'XX26mpkPytRrlKi38tQDdE36RgE9s04KA8Q2FEsPYg14B'
setup_twitter_oauth(consumer_key,consumer_secret,access_token,access_secret)
## [1] "Using direct authentication"
tweets <- userTimeline("CVSHealth", n=200)

n.tweet <- length(tweets)
#save text
tweets_text <- sapply(tweets, function(x) x$getText())

#create corpus - Constructs a text document collection (corpus).
tweets_text_corpus <- Corpus(VectorSource(tweets_text))
tweets_text_corpus <- tm_map(tweets_text_corpus, function(x) iconv(enc2utf8(x), sub = "byte"))
## Warning in tm_map.SimpleCorpus(tweets_text_corpus, function(x)
## iconv(enc2utf8(x), : transformation drops documents
tweets_text_corpus <- tm_map(tweets_text_corpus, content_transformer(tolower)) 
## Warning in tm_map.SimpleCorpus(tweets_text_corpus,
## content_transformer(tolower)): transformation drops documents
tweets_text_corpus <- tm_map(tweets_text_corpus, removePunctuation)
## Warning in tm_map.SimpleCorpus(tweets_text_corpus, removePunctuation):
## transformation drops documents
tweets_text_corpus <- tm_map(tweets_text_corpus, function(x)removeWords(x,stopwords()))
## Warning in tm_map.SimpleCorpus(tweets_text_corpus, function(x)
## removeWords(x, : transformation drops documents
wordcloud(as.character(tweets_text_corpus))
## Warning in tm_map.SimpleCorpus(corpus, removePunctuation): transformation
## drops documents
## Warning in tm_map.SimpleCorpus(corpus, function(x) removeWords(x,
## stopwords())): transformation drops documents
## Warning in wordcloud(as.character(tweets_text_corpus)): cvshealth could not
## be fit on page. It will not be plotted.

tweets.df <- twListToDF(tweets) 
 
# head(tweets.df)
# 
# head(tweets.df$text)

We checked the Twitter Sentiments to see how that is affecting the deal : http://rpubs.com/SShivakumar/TwitterSentimentAnalysis

tweets.df2 <- gsub("http.*","",tweets.df$text)
 
tweets.df2 <- gsub("https.*","",tweets.df2)
 
tweets.df2 <- gsub("#.*","",tweets.df2)
 
tweets.df2 <- gsub("@.*","",tweets.df2)
word.df <- as.vector(tweets.df2)
 
emotion.df <- get_nrc_sentiment(word.df)
 
emotion.df2 <- cbind(tweets.df2, emotion.df) 
 
head(emotion.df2)
##                                                                                                              tweets.df2
## 1                                                                                                                      
## 2                                                                                                                     .
## 3                                                                                                                     .
## 4                                                                                                                     .
## 5 Our pharmacists see every day how the opioid epidemic impacts communities. We recognize how critical it is to utili… 
## 6                                                                                                     Learn more about 
##   anger anticipation disgust fear joy sadness surprise trust negative
## 1     0            0       0    0   0       0        0     0        0
## 2     0            0       0    0   0       0        0     0        0
## 3     0            0       0    0   0       0        0     0        0
## 4     0            0       0    0   0       0        0     0        0
## 5     1            1       1    1   0       1        1     0        1
## 6     0            0       0    0   0       0        0     0        0
##   positive
## 1        0
## 2        0
## 3        0
## 4        0
## 5        0
## 6        1
sent.value <- get_sentiment(word.df)
 
most.positive <- word.df[sent.value == max(sent.value)]
 
most.positive
## [1] "Learn how Coram CVS Specialty Infusion Services (Coram) &amp; our high-touch model helps provide one-on-one support for… "
most.negative <- word.df[sent.value <= min(sent.value)] 

most.negative 
## [1] "Our pharmacists see every day how the opioid epidemic impacts communities. We recognize how critical it is to utili… "
sent.value
##   [1]  0.00  0.00  0.00  0.00 -1.35  0.80  0.40  2.30  0.00  0.00  0.00
##  [12] -0.60  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  1.80
##  [23]  0.75  0.00  0.00  0.00 -0.85  0.00  0.00  0.00  0.25  0.00 -0.50
##  [34]  0.00  0.00  1.40  0.00  0.00  3.30  0.00  0.00  0.00  0.50  0.50
##  [45]  0.00  0.50  0.50 -0.85  1.50  0.00  1.00  0.00  0.00  1.80  0.00
##  [56]  0.50  1.80  0.00  0.00  0.00  0.30  0.00  0.00  0.00  1.00  1.10
##  [67]  0.50  0.00  3.30  0.00  1.10  0.00  0.40  0.00  0.80  0.00  0.00
##  [78]  0.00  0.00  0.00  0.00  0.00  0.80  0.00  0.00  0.00  3.25  0.80
##  [89]  0.00  2.00  0.00  0.00  0.00  0.90  0.00  0.00  0.00  0.00  0.00
## [100]  0.00  0.00  2.10  0.00  0.00  1.15  0.00  0.00  1.00  0.00  0.00
## [111]  0.45  0.00  0.00  0.00  1.05  0.00  0.00  0.00  0.00  0.00  1.30
## [122]  2.25  0.00  0.15  0.00  0.00  0.80  0.00  0.50  0.00  1.80  0.00
## [133]  0.00 -0.85  0.80  3.25  0.00  0.00  0.00  1.00  1.55  0.00  1.25
## [144]  0.00  0.00  1.55  0.40  0.00  0.00  0.00  0.00  0.00  0.40  0.00
## [155] -0.20  0.00  0.00  0.00  1.55  0.75  0.00  0.00  2.50  0.00  0.75
## [166]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [177]  0.80  0.60  0.00  0.00  0.00  0.00  0.40
positive.tweets <- word.df[sent.value > 0]

head(positive.tweets)
## [1] "Learn more about "                                                                                                    
## [2] "32 new medication disposal units will be installed in "                                                               
## [3] "Providing Real-Time Benefits to Bring Savings to Patients "                                                           
## [4] "Health Care Consumerism: Making Health Care More Convenient "                                                         
## [5] "Today is the 1st day of summer! When traveling with kids this season, be sure to keep them safe by putting medicine… "
## [6] "News | CVS Health CEO Larry Merlo to Discuss Disruption and Transformation in Health Care During the 2018 Aspen Ide… "
negative.tweets <- word.df[sent.value < 0] 
head(negative.tweets)
## [1] "Our pharmacists see every day how the opioid epidemic impacts communities. We recognize how critical it is to utili… "
## [2] "Have you disposed of unused prescription drugs at a local collection site?"                                           
## [3] "News | CVS Health Expands Safe Drug Disposal at CVS Pharmacy Locations in Arizona to Help Combat Opioid Abuse "       
## [4] "Kevin Hourican, Executive Vice President, CVS Health &amp; President, CVS Pharmacy for "                              
## [5] "News | CVS Health Expands Safe Drug Disposal at CVS Pharmacy Locations in Virginia to Help Combat Opioid Abuse "      
## [6] "Safely disposing of unwanted or unused medication is one element of our company-wide approach to address the… "
neutral.tweets <- word.df[sent.value == 0] 
head(neutral.tweets)
## [1] ""  "." "." "." "." ""
category_senti <- ifelse(sent.value < 0, "Negative", ifelse(sent.value > 0, "Positive", "Neutral"))
 
head(category_senti)
## [1] "Neutral"  "Neutral"  "Neutral"  "Neutral"  "Negative" "Positive"
head(category_senti)
## [1] "Neutral"  "Neutral"  "Neutral"  "Neutral"  "Negative" "Positive"
category_senti2 <- cbind(tweets,category_senti) 

head(category_senti2) #tweets category_senti senti
##      tweets category_senti
## [1,] ?      "Neutral"     
## [2,] ?      "Neutral"     
## [3,] ?      "Neutral"     
## [4,] ?      "Neutral"     
## [5,] ?      "Negative"    
## [6,] ?      "Positive"
sentiment_categories <- table(category_senti)
sentiment_categories
## category_senti
## Negative  Neutral Positive 
##        7      120       56

There is more percentage of positive sentiments in the tweets on both CVSHealth and CVSAetna.

plot(table(category_senti), main="Twitter Sentiments")

pie(sentiment_categories, main="Twitter Sentiments", labels=c("Negative","Neutral","Positive"), cex=0.8)