Show code
library(tidyverse)
library(readxl)
library(knitr)
library(kableExtra)
# Import the original dataset
ev_reviews <- read_excel(
"Data/Data Notes - Dataset.xlsx",
sheet = "Original data"
)Approach
The goal of this project is to explore sentiment analysis using R and Tidyverse. I will reproduce the sentiment analysis presented in Chapter 2 of the book “Text Mining with R” by Julia Silge and David Robinson.
To extend the original analysis, I will use a different text dataset related to electric vehicle reviews and include an additional sentiment lexicon.
As an electrical engineer, I am interested in understanding how people describe electric vehicle performance, battery life, driving range, and charging systems.
This project will help me understand how different sentiment lexicons can influence the results of text analysis.
What are the most common positive and negative words in electric vehicle reviews?
What is the overall sentiment of electric vehicle reviews?
How do sentiment analysis results vary across different sentiment lexicons?
How do the results from the electric vehicle dataset differ from the original Chapter 2 analysis?
The dataset selected for this project is the Expert Review Dataset on Electric Quadricycles, available on Zenodo.
The dataset contains 95 reviews of small electric vehicles. The original data includes three variables:
The Excel file contains five sheets. For this project, I will use only the “Original data” sheet because I want to perform my own sentiment analysis using R.
Source: https://zenodo.org/records/14501242
library(tidyverse)
library(readxl)
library(knitr)
library(kableExtra)
# Import the original dataset
ev_reviews <- read_excel(
"Data/Data Notes - Dataset.xlsx",
sheet = "Original data"
)The original dataset was imported from an Excel file. The following tables show the dataset dimensions, variable descriptions, and a preview of the data.
# Create a dataset summary
dataset_summary <- tibble(
Description = c(
"Number of observations",
"Number of variables",
"Vehicle brands or models",
"Data format",
"Selected sheet"
),
Value = c(
nrow(ev_reviews),
ncol(ev_reviews),
n_distinct(ev_reviews$Brand, na.rm = TRUE),
"Excel (.xlsx)",
"Original data"
)
)
dataset_summary %>%
kable(
caption = "Table 1. Dataset Overview",
align = c("l", "c")
) %>%
kable_styling(
bootstrap_options = c("striped", "hover", "condensed"),
full_width = FALSE,
position = "center"
)| Description | Value |
|---|---|
| Number of observations | 95 |
| Number of variables | 3 |
| Vehicle brands or models | 12 |
| Data format | Excel (.xlsx) |
| Selected sheet | Original data |
# Describe the original variables
variable_description <- tibble(
Variable = names(ev_reviews),
Type = sapply(ev_reviews, class),
Description = c(
"Electric vehicle brand or model",
"Original source of the review",
"Full text of the vehicle review"
)
)
variable_description %>%
kable(
caption = "Table 2. Variable Description",
align = "l"
) %>%
kable_styling(
bootstrap_options = c("striped", "hover", "condensed"),
full_width = FALSE,
position = "center"
)| Variable | Type | Description |
|---|---|---|
| Brand | character | Electric vehicle brand or model |
| URL | character | Original source of the review |
| Text | character | Full text of the vehicle review |
# Display the first six observations
ev_reviews %>%
mutate(
URL = str_trunc(URL, 45),
Text = str_trunc(Text, 100)
) %>%
head(6) %>%
kable(
caption = "Table 3. Electric Vehicle Reviews",
align = "l"
) %>%
kable_styling(
bootstrap_options = c("striped", "hover", "condensed"),
full_width = TRUE
) %>%
column_spec(1, bold = TRUE) %>%
column_spec(3, width = "15em")| Brand | URL | Text |
|---|---|---|
| Microlino | https://www.theverge.com/24093189/microlin... | Wow, what an entrance!” said a well-coiffed woman as other patrons seated at the sidewalk cafe, n... |
| Microlino | https://www.frandroid.com/survoltes/voitur... | The highly anticipated new electric car Microlino is finally arriving on the French market. The l... |
| Microlino | https://insideevs.com/reviews/714373/micro... | Americans don't like tiny cars. Some fear they may not be safe. Some think they'll look silly dri... |
| Microlino | https://www.electrifying.com/reviews/micro... | The Microlino is brainchild of Merlin Outober, who has steered the project through turbulent wate... |
| Microlino | https://www.caranddriver.com/reviews/a4375... | The city car is dead. While never particularly popular in the U.S., cars like the original Mini, ... |
| Microlino | https://www.theautopian.com/the-microlino-... | The Microlino ‘Electric BMW Isetta’ Is A Huge Piece Of Crap But I Still Love It. It’s been over s... |
The dataset contains 95 observations and three variables. The Text column will be the main variable for sentiment analysis, while the Brand column will help compare reviews across different electric vehicles.
Before performing sentiment analysis, I will check the quality of the dataset. This includes checking for missing values, duplicate records, and empty review texts.
# Check missing values by variable
missing_summary <- tibble(
Variable = names(ev_reviews),
`Missing Values` = colSums(is.na(ev_reviews)),
Percentage = round(
colMeans(is.na(ev_reviews)) * 100, 2
)
)
missing_summary %>%
kable(
caption = "Table 4. Missing Values",
align = c("l", "c", "c")
) %>%
kable_styling(
bootstrap_options = c("striped", "hover", "condensed"),
full_width = FALSE,
position = "center"
)| Variable | Missing Values | Percentage |
|---|---|---|
| Brand | 0 | 0 |
| URL | 0 | 0 |
| Text | 0 | 0 |
# Count reviews by vehicle brand or model
vehicle_summary <- ev_reviews %>%
count(Brand, name = "Reviews", sort = TRUE) %>%
mutate(
Percentage = round(Reviews / sum(Reviews) * 100, 2)
)
vehicle_summary %>%
kable(
caption = "Table 5. Number of Reviews by Vehicle",
align = c("l", "c", "c")
) %>%
kable_styling(
bootstrap_options = c("striped", "hover", "condensed"),
full_width = FALSE,
position = "center"
) %>%
column_spec(1, bold = TRUE)| Brand | Reviews | Percentage |
|---|---|---|
| Microlino | 19 | 20.00 |
| XEV YOYO | 15 | 15.79 |
| Citroen Ami | 12 | 12.63 |
| Opel Rocks-e | 12 | 12.63 |
| Silence S04 | 11 | 11.58 |
| City Transformer | 7 | 7.37 |
| Fiat Topolino | 5 | 5.26 |
| Estrima Birò | 4 | 4.21 |
| DoGood | 3 | 3.16 |
| Eli Zero | 3 | 3.16 |
| SWAPA | 2 | 2.11 |
| Solar Squad EV | 2 | 2.11 |
The number of reviews may vary across vehicles. This is important to consider when comparing sentiment analysis results.
The original analysis is based on Chapter 2 of the book “Text Mining with R” by Julia Silge and David Robinson.
The authors use Jane Austen’s books to demonstrate how sentiment analysis works with tidy data. They use three sentiment lexicons: AFINN, Bing, and NRC.
I will reproduce the original analysis to understand how these methods work.
To extend the original analysis, I will use a different dataset containing 95 electric vehicle reviews.
I will also include the Loughran-McDonald lexicon as an additional sentiment lexicon.
This extension will allow me to compare sentiment results using different types of text and sentiment lexicons.
The analysis will be developed in R using Tidyverse and tidytext.
The following steps will be used:
lexicon_summary <- tibble(
Lexicon = c(
"AFINN",
"Bing",
"NRC",
"Loughran-McDonald"
),
Description = c(
"Uses numerical sentiment scores from -5 to 5",
"Classifies words as positive or negative",
"Classifies words by sentiment and emotion",
"Classifies words using financial sentiment categories"
),
Purpose = c(
"Original analysis",
"Original analysis",
"Original analysis",
"Proposed extension"
)
)
lexicon_summary %>%
kable(
caption = "Table 6. Sentiment Lexicons",
align = "l"
) %>%
kable_styling(
bootstrap_options = c("striped", "hover", "condensed"),
full_width = TRUE,
position = "center"
) %>%
column_spec(1, bold = TRUE)| Lexicon | Description | Purpose |
|---|---|---|
| AFINN | Uses numerical sentiment scores from -5 to 5 | Original analysis |
| Bing | Classifies words as positive or negative | Original analysis |
| NRC | Classifies words by sentiment and emotion | Original analysis |
| Loughran-McDonald | Classifies words using financial sentiment categories | Proposed extension |
Each lexicon uses a different method to score and classify words. For this reason, I will use normalized values to make the sentiment scores comparable.
I expect to identify common positive and negative words in electric vehicle reviews.
I also expect to find different sentiment patterns across vehicle brands and models.
The comparison between the four sentiment lexicons may show how different methods can produce different results using the same text.
This project has some limitations:
These limitations will be considered when interpreting the final results.
Silge, J., & Robinson, D. (2017). Text Mining with R: A Tidy Approach. O’Reilly Media. https://www.tidytextmining.com/
Chapter 2: Sentiment Analysis with Tidy Data. https://www.tidytextmining.com/sentiment.html
Expert Review Dataset on Electric Quadricycles. Zenodo. https://zenodo.org/records/14501242
Tidytext R Package. https://juliasilge.github.io/tidytext/
Tidyverse. https://www.tidyverse.org/
Loughran-McDonald Sentiment Word Lists. https://sraf.nd.edu/loughranmcdonald-master-dictionary/
ChatGPT was used to help understand the assignment requirements, organize the planned approach, improve English writing, and provide guidance for the R code.
I selected the electric vehicle topic based on my professional background and will run, review, and verify the data transformations, sentiment analysis, visualizations, and final conclusions.