Advanced text mining techniques in R allow us to extract deeper insights from text data. Two important techniques are topic modeling and sentiment analysis.
Topic modeling is an unsupervised learning technique that involves discovering hidden themes or topics within a set of documents. One popular algorithm for topic modeling is Latent Dirichlet Allocation (LDA). The stm package in R provides a comprehensive framework for fitting and visualizing LDA models. The package also allows us to incorporate covariates into the model to explore how different variables may affect the topic distribution.
Here’s an example of fitting an LDA model using the stm package:
library(stm)
# Load data
data("news")
doclist <- news$content
# Create document-term matrix
processed <- textProcessor(doclist, metadata = news)
out <- prepDocuments(processed$documents, processed$vocab, lower.thresh = 5)
# Fit LDA model
K <- 10 # number of topics
lda <- stm(out$documents, out$vocab, K = K, init.type = "Spectral")
# Explore topics
plotModels(lda, type = "topic", topics = 1:K)
Sentiment analysis is a technique for quantifying the emotional tone of a piece of text. The syuzhet package in R provides several sentiment analysis algorithms, including the AFINN and NRC lexicons. The package allows us to calculate sentiment scores for individual words or entire documents.
Here’s an example of performing sentiment analysis using the syuzhet package:
library(syuzhet)
# Load data
data("inaugTexts")
doclist <- inaugTexts$text
# Calculate sentiment scores
sentiment <- get_nrc_sentiment(doclist)
# Visualize sentiment over time
library(ggplot2)
library(reshape2)
sentiment_df <- data.frame(sentiment, year = inaugTexts$year)
sentiment_melted <- melt(sentiment_df, id.vars = "year", variable.name = "sentiment")
ggplot(sentiment_melted, aes(x = year, y = value, color = sentiment)) +
geom_line() +
ggtitle("Sentiment in Presidential Inauguration Speeches")
Advanced text mining techniques like topic modeling and sentiment analysis can provide deeper insights into text data and help us understand the underlying patterns and themes.