Load necessary libraries

library(tm)
## Loading required package: NLP
library(SnowballC)
library(wordcloud)
## Loading required package: RColorBrewer
library(RColorBrewer)
library(tidytext)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
## 
## Attaching package: 'ggplot2'
## The following object is masked from 'package:NLP':
## 
##     annotate
library(textdata)
library(caret)
## Loading required package: lattice
library(e1071)
library(topicmodels)
library(readxl)

Dataset

df <- read_excel("Prabowo-atasi-stunting.xlsx")
df
## # A tibble: 101 × 12
##    created_at             id_str full_text quote_count reply_count retweet_count
##    <chr>                   <dbl> <chr>           <dbl>       <dbl>         <dbl>
##  1 Mon Oct 16 07:25:31 … 1.71e18 @prabowo…           0           0             0
##  2 Mon Oct 16 07:21:43 … 1.71e18 @prabowo…           0           0             0
##  3 Mon Oct 16 07:28:54 … 1.71e18 @prabowo…           0           0             0
##  4 Mon Oct 16 07:36:27 … 1.71e18 @prabowo…           0           0             0
##  5 Mon Oct 16 07:25:21 … 1.71e18 @prabowo…           0           0             0
##  6 Mon Oct 16 07:21:46 … 1.71e18 @prabowo…           0           0             0
##  7 Mon Oct 16 07:25:29 … 1.71e18 @prabowo…           0           0             0
##  8 Mon Oct 16 07:22:01 … 1.71e18 @prabowo…           0           0             0
##  9 Mon Oct 16 07:23:53 … 1.71e18 @prabowo…           0           0             0
## 10 Mon Oct 16 07:25:18 … 1.71e18 @prabowo…           0           0             0
## # ℹ 91 more rows
## # ℹ 6 more variables: favorite_count <dbl>, lang <chr>, user_id_str <dbl>,
## #   conversation_id_str <dbl>, username <chr>, tweet_url <chr>

a) Pre-processing

Create a corpus

corpus <- VCorpus(VectorSource(df$full_text))

Pre-processing

corpus_clean <- tm_map(corpus, content_transformer(tolower))
corpus_clean <- tm_map(corpus_clean, removePunctuation)
corpus_clean <- tm_map(corpus_clean, removeNumbers)
corpus_clean <- tm_map(corpus_clean, removeWords, stopwords("en"))
corpus_clean <- tm_map(corpus_clean, stripWhitespace)
corpus_clean <- tm_map(corpus_clean, stemDocument)

b) Description of the most frequently occurring words and word cloud

dtm <- TermDocumentMatrix(corpus_clean) matrix <- as.matrix(dtm) word_freqs <- sort(rowSums(matrix), decreasing = TRUE) word_freqs_df <- data.frame(word = names(word_freqs), freq = word_freqs)

Word cloud

set.seed(1234) wordcloud(words = word_freqs_df\(word, freq = word_freqs_df\)freq, min.freq = 1, max.words = 100, random.order = FALSE, rot.per = 0.35, colors = brewer.pal(8, “Dark2”))

c) Word association

findAssocs(dtm, terms = names(word_freqs)[1], corlimit = 0.25)

d) Topic grouping - using Latent Dirichlet Allocation (LDA)

num_topics <- 5 lda <- LDA(dtm, k = num_topics) topics <- as.matrix(topics(lda)) topic_words <- terms(lda, 6)

Perform sentiment analysis

df$text_clean <- sapply(corpus_clean, as.character) df <- df %>% unnest_tokens(word, text_clean) df <- df %>% inner_join(get_sentiments(“bing”), by = “word”) df <- df %>% group_by(id_str) %>% summarise(sentiment = sum(score))

Add sentiment column

df\(Y <- ifelse(df\)sentiment > 0, ‘positive’, ifelse(df$sentiment < 0, ‘negative’, ‘neutral’))

Classification algorithm - Naive Bayes

set.seed(123) df\(Y <- as.factor(df\)Y) trainIndex <- createDataPartition(df$Y, p = 0.8, list = FALSE) trainData <- df[trainIndex,] testData <- df[-trainIndex,]

model <- naiveBayes(Y ~ ., data = trainData) predictions <- predict(model, testData)

Determine the accuracy

confusionMatrix(predictions, testData\(Y)\)overall[‘Accuracy’]