Required Packages

Make sure you have all the packages installed from “How to run basic analysis in R” plus the following:

install.packages(c("SentimentAnalysis", "textclean", "tidytext", 
										"tm", "tokenizers"))

Merging datasets before you begin

If you need to merge datasets before you can begin (e.g., you’ve got some results from LIWC and some from Evaluative Lexicon and you want to put them together) you can have a look at the merge section in “How to run basic analysis in R

Remember to follow the basic rules about importing data from Qualtrics into R (i.e., deleting the second header line from the excel data before importing it)

Basic Sentiment Analysis

Load in the relevant packages

If you want to run an analysis on the text variables once you’ve got them, you will probably need to load in the following packages

library(dplyr)
library(effectsize)
library(emmeans)
library(mediation)
library(ggplot2)
library(SentimentAnalysis)

Pre-processing of the text

There is always some pre-processing you need to do to text before analyzing it, like stemming and removing stopwords. You will see this below when we get into custom text analysis. For now, all you need to know is that SentimentAnalysis does these steps automatically for us.

Analyzing the text

The Sentiment Analysis R package can give you a variety of outputs, namely:

VariableName Dictionary Description
WordCount NA how many words are in the text?
SentimentGI Harvard-IV dictionary
NegativityGI Harvard-IV dictionary
PositivityGI Harvard-IV dictionary
SentimentHE Henry’s Financial dictionary (Henry 2008)
NegativityHE Henry’s Financial dictionary (Henry 2008)
PositivityHE Henry’s Financial dictionary (Henry 2008)
SentimentLM Loughran-McDonald Financial dictionary (Loughran and McDonald 2011)
NegativityLM Loughran-McDonald Financial dictionary (Loughran and McDonald 2011)
PositivityLM Loughran-McDonald Financial dictionary (Loughran and McDonald 2011)
RatioUncertaintyLM Loughran-McDonald Financial dictionary (Loughran and McDonald 2011)
SentimentQDAP QDAP dictionary from the package qdapDictionaries
NegativityQDAP QDAP dictionary from the package qdapDictionaries
PositivityQDAP QDAP dictionary from the package qdapDictionaries

Because SentimentAnalyzer can give you so much, it’s best to specify what you want. For example, if I’m interested in getting overall sentiment based on each of the four dictionaries and creating a column for each in my dataframe, I would write:

dta$sentiment.GI <- analyzeSentiment(dta$Text)$SentimentGI
dta$sentiment.LM <- analyzeSentiment(dta$Text)$SentimentLM
dta$sentiment.QDAP <- analyzeSentiment(dta$Text)$SentimentQDAP
dta$sentiment.HE <- analyzeSentiment(dta$Text)$SentimentHE
dta$sentiment.X ← analyzeSentiment (dta$Text) $SentimentX
the name of the new variable in my existing dataset (called “dta”) the function from the SentimentAnalaysis package the name of the variable in your dataset that contains the text you want analyzed the name of the function from the SentimentAnalysis package that you wish to run, chosen from the table above

Words in pink are variables you can re-name in this code to whatever you want