Sentiment Analysis on LOTR

#install.packages('tidytext')
#install.packages('textdata')
#install.packages('wordcloud')
#install.packages('tm')
library(tidytext)
## Warning: package 'tidytext' was built under R version 4.3.3
library(textdata)
## Warning: package 'textdata' was built under R version 4.3.3
library(wordcloud)
## Warning: package 'wordcloud' was built under R version 4.3.3
## Loading required package: RColorBrewer
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
library(stringr)
library(tm)
## Warning: package 'tm' was built under R version 4.3.3
## Loading required package: NLP
## 
## Attaching package: 'NLP'
## 
## The following object is masked from 'package:ggplot2':
## 
##     annotate
#install.packages('devtools')

#install.packages('reticulate')
library(reticulate)
## Warning: package 'reticulate' was built under R version 4.3.3
#install_miniconda()
devtools::install_github("farach/huggingfaceR")
## Skipping install of 'huggingfaceR' from a github remote, the SHA1 (a9dc96af) has not changed since last install.
##   Use `force = TRUE` to force installation

LOTR Script, with the movie, the character, and the line, thanks to github.com/tianyigu

LOTR <-read_csv("https://raw.githubusercontent.com/GuillermoCharlesSchneider/DATA-607/main/HW10/lotrscript.csv")
## Rows: 2390 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): char, dialog, movie
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
get_sentiments("afinn")
## # A tibble: 2,477 × 2
##    word       value
##    <chr>      <dbl>
##  1 abandon       -2
##  2 abandoned     -2
##  3 abandons      -2
##  4 abducted      -2
##  5 abduction     -2
##  6 abductions    -2
##  7 abhor         -3
##  8 abhorred      -3
##  9 abhorrent     -3
## 10 abhors        -3
## # ℹ 2,467 more rows

Movies split into individual words

LOTR$dialog <- removePunctuation(LOTR$dialog) #Remove all punctuation marks
LOTR$dialog <- stripWhitespace(LOTR$dialog) #Remove excess whitespace
LOTR$dialog <- tolower(LOTR$dialog) #Make all characters lowercase
LOTR$dialog <- removeNumbers(LOTR$dialog)

tidy_LOTR <- LOTR %>%
  group_by(movie) %>%
  ungroup() %>%
  unnest_tokens(word, dialog)

Joyful Words in The Return of the King

nrc_joy <- get_sentiments("nrc") %>% 
  filter(sentiment == "joy")

tidy_LOTR %>%
  filter(movie == "The Return of the King Movie Script") %>%
  inner_join(nrc_joy) %>%
  count(word, sort = TRUE)
## Joining with `by = join_by(word)`
## # A tibble: 70 × 2
##    word         n
##    <chr>    <int>
##  1 merry       15
##  2 precious    15
##  3 hope        11
##  4 love        11
##  5 peace        9
##  6 tree         9
##  7 save         8
##  8 victory      8
##  9 good         7
## 10 journey      6
## # ℹ 60 more rows

Sentiment change through the books

tidy_LOTR %>% 
  count(char) %>%
  arrange(desc(n)) %>%
  slice(1:15) %>%
  ggplot(aes(x=reorder(char, n), y=n)) +
  geom_bar(stat="identity", aes(fill=n), show.legend=FALSE) + 
  geom_label(aes(label=n)) +
  scale_fill_gradient(low="red", high="blue") +
  labs(x="Character", y="Lines of dialogue",
       title="Lines of dialogue per character (absolute values)") +  
  coord_flip() +
  theme_bw()

Just The Two Towers df by word

twoTowers <- tidy_LOTR %>% 
  filter(movie == "The Two Towers Movie Script")

twoTowers
## # A tibble: 10,174 × 3
##    char     movie                       word       
##    <chr>    <chr>                       <chr>      
##  1 GANDALF: The Two Towers Movie Script saurons    
##  2 GANDALF: The Two Towers Movie Script wrath      
##  3 GANDALF: The Two Towers Movie Script will       
##  4 GANDALF: The Two Towers Movie Script be         
##  5 GANDALF: The Two Towers Movie Script terrible   
##  6 GANDALF: The Two Towers Movie Script his        
##  7 GANDALF: The Two Towers Movie Script retribution
##  8 GANDALF: The Two Towers Movie Script swift      
##  9 GANDALF: The Two Towers Movie Script the        
## 10 GANDALF: The Two Towers Movie Script battle     
## # ℹ 10,164 more rows

Comparing the sentiment packages

afinn <- twoTowers %>% 
  inner_join(get_sentiments("afinn")) %>% 
  group_by(char) %>% 
  summarise(sentiment = sum(value)) %>% 
  mutate(method = "AFINN")
## Joining with `by = join_by(word)`
bing_and_nrc <- bind_rows(
  twoTowers %>% 
    inner_join(get_sentiments("bing")) %>%
    mutate(method = "Bing et al."),
  twoTowers %>% 
    inner_join(get_sentiments("nrc") %>% 
                 filter(sentiment %in% c("positive", 
                                         "negative"))
    ) %>%
    mutate(method = "NRC")) 
## Joining with `by = join_by(word)`
## Joining with `by = join_by(word)`
## Warning in inner_join(., get_sentiments("nrc") %>% filter(sentiment %in% : Detected an unexpected many-to-many relationship between `x` and `y`.
## ℹ Row 502 of `x` matches multiple rows in `y`.
## ℹ Row 464 of `y` matches multiple rows in `x`.
## ℹ If a many-to-many relationship is expected, set `relationship =
##   "many-to-many"` to silence this warning.

Bing Sentiment, individual words, all books

LOTR_word_counts <- tidy_LOTR %>%
  inner_join(get_sentiments("bing")) %>%
  count(word, sentiment, sort = TRUE) %>%
  ungroup()
## Joining with `by = join_by(word)`
LOTR_word_counts
## # A tibble: 501 × 3
##    word     sentiment     n
##    <chr>    <chr>     <int>
##  1 master   positive     58
##  2 dead     negative     48
##  3 good     positive     46
##  4 merry    positive     41
##  5 death    negative     35
##  6 precious positive     35
##  7 well     positive     33
##  8 like     positive     31
##  9 great    positive     27
## 10 right    positive     27
## # ℹ 491 more rows

Contribution to sentiment

LOTR_word_counts %>%
  group_by(sentiment) %>%
  slice_max(n, n = 10) %>% 
  ungroup() %>%
  mutate(word = reorder(word, n)) %>%
  ggplot(aes(n, word, fill = sentiment)) +
  geom_col(show.legend = FALSE) +
  facet_wrap(~sentiment, scales = "free_y") +
  labs(x = "Contribution to sentiment",
       y = NULL)

Love a good word cloud

tidy_LOTR %>%
  anti_join(stop_words) %>%
  count(word) %>%
  with(wordcloud(word, n, max.words = 100))
## Joining with `by = join_by(word)`

trying hugging face

https://heartbeat.comet.ml/using-hugging-face-transformers-for-sentiment-analysis-in-r-6b100c025a2e

i got too lost in the sauce. that was fun tho! i dont fully understand exactly what this package is doing. it seems cool, using the already trained nlp model, check the model results against the predicted scores. its also running python in R?

#hf_python_depends('transformers')
library(huggingfaceR)

distilBERT <- hf_load_pipeline(
  model_id = "distilbert-base-uncased-finetuned-sst-2-english", 
  task = "text-classification"
  )
## 
## 
## distilbert-base-uncased-finetuned-sst-2-english is ready for text-classification
reticulate::py_install("transformers")
## + "C:/Users/guill/AppData/Local/r-miniconda/condabin/conda.bat" "install" "--yes" "--prefix" "C:/Users/guill/AppData/Local/r-miniconda/envs/huggingfaceR" "-c" "conda-forge" "transformers"
transformers <- import("transformers")
model_name <- "nlptown/bert-base-multilingual-uncased-sentiment"
tokenizer <- transformers$BertTokenizer$from_pretrained(model_name)
model <- transformers$BertForSequenceClassification$from_pretrained(model_name)
inputs <- lapply(LOTR$dialog, tokenizer$encode_plus, add_special_tokens = TRUE, 
                 return_tensors = "pt")
torch <- import("torch")
outputs <- lapply(inputs, function(input) {
  with(torch$no_grad(), {
    model(input_ids = input$input_ids, token_type_ids = input$token_type_ids, 
          attention_mask = input$attention_mask)$logits
  })
})
predictions <- sapply(outputs, function(output) {
  torch$argmax(output)$item() 
})
threshold <- 2.5
LOTR$binary_scores <- ifelse(predictions >= threshold, 1, 0)
LOTR %>% 
  mutate(sentiment = ifelse(LOTR$binary_scores > 0, "Positive", "Negative")) %>%
  group_by(sentiment) %>%
  summarise(count = n()) 
## # A tibble: 2 × 2
##   sentiment count
##   <chr>     <int>
## 1 Negative   1235
## 2 Positive   1155
LOTR$sentimentresult <- ifelse(LOTR$binary_scores >0 , "Positive", "Negative")
LOTR$sentimentresult
##    [1] "Negative" "Negative" "Negative" "Negative" "Negative" "Negative"
##    [7] "Positive" "Negative" "Positive" "Positive" "Negative" "Negative"
##   [13] "Negative" "Positive" "Positive" "Positive" "Positive" "Negative"
##   [19] "Negative" "Negative" "Positive" "Positive" "Positive" "Positive"
##   [25] "Negative" "Negative" "Positive" "Negative" "Positive" "Positive"
##   [31] "Positive" "Negative" "Positive" "Positive" "Positive" "Positive"
##   [37] "Negative" "Negative" "Positive" "Positive" "Positive" "Negative"
##   [43] "Positive" "Positive" "Negative" "Positive" "Positive" "Negative"
##   [49] "Positive" "Negative" "Negative" "Positive" "Negative" "Negative"
##   [55] "Positive" "Positive" "Negative" "Positive" "Positive" "Negative"
##   [61] "Positive" "Negative" "Negative" "Positive" "Positive" "Positive"
##   [67] "Positive" "Positive" "Positive" "Negative" "Positive" "Positive"
##   [73] "Positive" "Positive" "Negative" "Positive" "Positive" "Positive"
##   [79] "Negative" "Negative" "Negative" "Positive" "Negative" "Negative"
##   [85] "Positive" "Positive" "Negative" "Negative" "Negative" "Positive"
##   [91] "Positive" "Negative" "Negative" "Negative" "Negative" "Negative"
##   [97] "Negative" "Negative" "Negative" "Negative" "Positive" "Positive"
##  [103] "Negative" "Negative" "Positive" "Positive" "Negative" "Negative"
##  [109] "Positive" "Positive" "Positive" "Negative" "Negative" "Negative"
##  [115] "Negative" "Negative" "Negative" "Positive" "Positive" "Negative"
##  [121] "Positive" "Positive" "Positive" "Negative" "Positive" "Negative"
##  [127] "Negative" "Negative" "Negative" "Negative" "Positive" "Positive"
##  [133] "Negative" "Negative" "Negative" "Positive" "Positive" "Positive"
##  [139] "Positive" "Negative" "Negative" "Positive" "Negative" "Negative"
##  [145] "Positive" "Positive" "Negative" "Negative" "Negative" "Negative"
##  [151] "Positive" "Positive" "Negative" "Positive" "Positive" "Negative"
##  [157] "Positive" "Positive" "Positive" "Negative" "Negative" "Negative"
##  [163] "Positive" "Positive" "Negative" "Negative" "Negative" "Negative"
##  [169] "Positive" "Positive" "Positive" "Positive" "Negative" "Positive"
##  [175] "Negative" "Negative" "Positive" "Positive" "Negative" "Positive"
##  [181] "Negative" "Negative" "Negative" "Negative" "Negative" "Positive"
##  [187] "Negative" "Negative" "Negative" "Negative" "Negative" "Positive"
##  [193] "Negative" "Negative" "Negative" "Negative" "Negative" "Negative"
##  [199] "Positive" "Negative" "Positive" "Positive" "Negative" "Negative"
##  [205] "Positive" "Positive" "Positive" "Negative" "Negative" "Negative"
##  [211] "Positive" "Negative" "Negative" "Positive" "Negative" "Negative"
##  [217] "Positive" "Positive" "Positive" "Positive" "Negative" "Negative"
##  [223] "Negative" "Negative" "Negative" "Positive" "Negative" "Negative"
##  [229] "Negative" "Positive" "Negative" "Positive" "Positive" "Positive"
##  [235] "Negative" "Negative" "Positive" "Negative" "Positive" "Negative"
##  [241] "Positive" "Negative" "Positive" "Negative" "Positive" "Negative"
##  [247] "Negative" "Negative" "Positive" "Negative" "Negative" "Negative"
##  [253] "Positive" "Negative" "Negative" "Negative" "Positive" "Positive"
##  [259] "Negative" "Negative" "Positive" "Positive" "Negative" "Positive"
##  [265] "Positive" "Positive" "Negative" "Negative" "Positive" "Negative"
##  [271] "Positive" "Positive" "Positive" "Positive" "Positive" "Positive"
##  [277] "Negative" "Positive" "Positive" "Negative" "Negative" "Negative"
##  [283] "Positive" "Negative" "Positive" "Positive" "Negative" "Negative"
##  [289] "Negative" "Positive" "Positive" "Positive" "Positive" "Positive"
##  [295] "Positive" "Negative" "Negative" "Positive" "Negative" "Positive"
##  [301] "Negative" "Negative" "Negative" "Positive" "Positive" "Positive"
##  [307] "Positive" "Negative" "Negative" "Positive" "Negative" "Positive"
##  [313] "Negative" "Negative" "Positive" "Positive" "Negative" "Positive"
##  [319] "Negative" "Positive" "Negative" "Negative" "Positive" "Negative"
##  [325] "Negative" "Negative" "Positive" "Negative" "Negative" "Positive"
##  [331] "Positive" "Positive" "Negative" "Negative" "Positive" "Positive"
##  [337] "Negative" "Positive" "Negative" "Positive" "Negative" "Negative"
##  [343] "Positive" "Positive" "Positive" "Positive" "Negative" "Negative"
##  [349] "Negative" "Negative" "Positive" "Negative" "Negative" "Positive"
##  [355] "Negative" "Negative" "Negative" "Positive" "Positive" "Negative"
##  [361] "Negative" "Negative" "Negative" "Negative" "Positive" "Positive"
##  [367] "Negative" "Positive" "Positive" "Positive" "Positive" "Positive"
##  [373] "Negative" "Negative" "Positive" "Positive" "Negative" "Negative"
##  [379] "Negative" "Negative" "Negative" "Negative" "Positive" "Negative"
##  [385] "Negative" "Negative" "Positive" "Positive" "Negative" "Negative"
##  [391] "Negative" "Positive" "Negative" "Negative" "Negative" "Negative"
##  [397] "Negative" "Positive" "Positive" "Positive" "Positive" "Negative"
##  [403] "Negative" "Positive" "Positive" "Positive" "Negative" "Positive"
##  [409] "Positive" "Positive" "Positive" "Positive" "Negative" "Negative"
##  [415] "Negative" "Positive" "Negative" "Negative" "Positive" "Negative"
##  [421] "Negative" "Negative" "Negative" "Negative" "Negative" "Negative"
##  [427] "Positive" "Negative" "Positive" "Negative" "Negative" "Negative"
##  [433] "Positive" "Positive" "Negative" "Negative" "Negative" "Negative"
##  [439] "Positive" "Negative" "Negative" "Positive" "Positive" "Negative"
##  [445] "Positive" "Negative" "Negative" "Positive" "Negative" "Negative"
##  [451] "Negative" "Negative" "Negative" "Negative" "Positive" "Negative"
##  [457] "Negative" "Negative" "Positive" "Negative" "Positive" "Negative"
##  [463] "Positive" "Positive" "Positive" "Negative" "Negative" "Negative"
##  [469] "Positive" "Negative" "Negative" "Negative" "Positive" "Positive"
##  [475] "Positive" "Positive" "Negative" "Positive" "Negative" "Negative"
##  [481] "Positive" "Negative" "Positive" "Negative" "Positive" "Positive"
##  [487] "Negative" "Negative" "Negative" "Negative" "Positive" "Negative"
##  [493] "Negative" "Negative" "Negative" "Negative" "Negative" "Negative"
##  [499] "Negative" "Negative" "Positive" "Negative" "Negative" "Positive"
##  [505] "Positive" "Negative" "Negative" "Negative" "Negative" "Negative"
##  [511] "Positive" "Negative" "Negative" "Negative" "Negative" "Negative"
##  [517] "Negative" "Positive" "Negative" "Positive" "Negative" "Positive"
##  [523] "Positive" "Positive" "Negative" "Negative" "Positive" "Negative"
##  [529] "Negative" "Positive" "Positive" "Positive" "Negative" "Negative"
##  [535] "Negative" "Negative" "Negative" "Positive" "Positive" "Negative"
##  [541] "Negative" "Negative" "Positive" "Negative" "Negative" "Positive"
##  [547] "Positive" "Positive" "Positive" "Negative" "Positive" "Positive"
##  [553] "Positive" "Positive" "Positive" "Positive" "Positive" "Negative"
##  [559] "Negative" "Negative" "Negative" "Positive" "Positive" "Positive"
##  [565] "Positive" "Negative" "Negative" "Negative" "Negative" "Positive"
##  [571] "Positive" "Positive" "Negative" "Positive" "Negative" "Positive"
##  [577] "Positive" "Negative" "Positive" "Positive" "Negative" "Positive"
##  [583] "Negative" "Negative" "Negative" "Positive" "Negative" "Negative"
##  [589] "Negative" "Negative" "Negative" "Positive" "Negative" "Negative"
##  [595] "Positive" "Negative" "Negative" "Negative" "Positive" "Positive"
##  [601] "Positive" "Negative" "Positive" "Negative" "Positive" "Positive"
##  [607] "Negative" "Positive" "Positive" "Positive" "Positive" "Positive"
##  [613] "Positive" "Negative" "Positive" "Positive" "Positive" "Positive"
##  [619] "Positive" "Positive" "Positive" "Negative" "Negative" "Negative"
##  [625] "Positive" "Positive" "Positive" "Negative" "Positive" "Positive"
##  [631] "Positive" "Positive" "Negative" "Positive" "Negative" "Negative"
##  [637] "Negative" "Negative" "Positive" "Positive" "Positive" "Negative"
##  [643] "Negative" "Positive" "Positive" "Positive" "Negative" "Negative"
##  [649] "Positive" "Negative" "Positive" "Positive" "Negative" "Positive"
##  [655] "Positive" "Positive" "Negative" "Negative" "Negative" "Negative"
##  [661] "Positive" "Negative" "Positive" "Positive" "Negative" "Negative"
##  [667] "Negative" "Negative" "Negative" "Negative" "Negative" "Positive"
##  [673] "Negative" "Negative" "Positive" "Negative" "Negative" "Positive"
##  [679] "Negative" "Negative" "Negative" "Negative" "Negative" "Positive"
##  [685] "Positive" "Positive" "Negative" "Positive" "Negative" "Negative"
##  [691] "Positive" "Positive" "Positive" "Positive" "Negative" "Negative"
##  [697] "Negative" "Negative" "Negative" "Positive" "Positive" "Positive"
##  [703] "Negative" "Positive" "Negative" "Positive" "Positive" "Negative"
##  [709] "Negative" "Negative" "Positive" "Positive" "Positive" "Positive"
##  [715] "Negative" "Negative" "Positive" "Negative" "Positive" "Positive"
##  [721] "Negative" "Positive" "Positive" "Positive" "Negative" "Positive"
##  [727] "Positive" "Positive" "Positive" "Negative" "Negative" "Positive"
##  [733] "Positive" "Negative" "Negative" "Positive" "Positive" "Negative"
##  [739] "Negative" "Positive" "Positive" "Positive" "Positive" "Positive"
##  [745] "Positive" "Positive" "Negative" "Positive" "Positive" "Positive"
##  [751] "Negative" "Positive" "Positive" "Negative" "Negative" "Negative"
##  [757] "Negative" "Negative" "Negative" "Negative" "Negative" "Positive"
##  [763] "Negative" "Negative" "Positive" "Negative" "Positive" "Negative"
##  [769] "Negative" "Negative" "Negative" "Positive" "Negative" "Positive"
##  [775] "Negative" "Negative" "Positive" "Positive" "Positive" "Positive"
##  [781] "Positive" "Negative" "Positive" "Negative" "Positive" "Positive"
##  [787] "Negative" "Positive" "Negative" "Negative" "Negative" "Negative"
##  [793] "Positive" "Positive" "Negative" "Negative" "Positive" "Positive"
##  [799] "Positive" "Positive" "Negative" "Negative" "Negative" "Negative"
##  [805] "Positive" "Positive" "Negative" "Negative" "Positive" "Negative"
##  [811] "Negative" "Negative" "Positive" "Negative" "Negative" "Positive"
##  [817] "Positive" "Negative" "Negative" "Negative" "Negative" "Positive"
##  [823] "Negative" "Negative" "Negative" "Positive" "Negative" "Positive"
##  [829] "Positive" "Positive" "Positive" "Positive" "Negative" "Positive"
##  [835] "Positive" "Positive" "Positive" "Negative" "Positive" "Negative"
##  [841] "Negative" "Positive" "Negative" "Negative" "Negative" "Positive"
##  [847] "Positive" "Positive" "Positive" "Positive" "Positive" "Negative"
##  [853] "Negative" "Negative" "Positive" "Positive" "Positive" "Negative"
##  [859] "Negative" "Positive" "Positive" "Negative" "Positive" "Positive"
##  [865] "Negative" "Negative" "Positive" "Positive" "Negative" "Positive"
##  [871] "Negative" "Negative" "Positive" "Negative" "Negative" "Negative"
##  [877] "Negative" "Positive" "Negative" "Negative" "Positive" "Positive"
##  [883] "Positive" "Negative" "Negative" "Negative" "Positive" "Negative"
##  [889] "Positive" "Positive" "Negative" "Negative" "Positive" "Positive"
##  [895] "Negative" "Negative" "Negative" "Negative" "Negative" "Negative"
##  [901] "Positive" "Negative" "Positive" "Positive" "Positive" "Negative"
##  [907] "Negative" "Negative" "Negative" "Negative" "Negative" "Negative"
##  [913] "Negative" "Positive" "Positive" "Negative" "Negative" "Negative"
##  [919] "Negative" "Negative" "Negative" "Negative" "Negative" "Negative"
##  [925] "Positive" "Positive" "Positive" "Positive" "Negative" "Negative"
##  [931] "Negative" "Positive" "Negative" "Positive" "Negative" "Negative"
##  [937] "Negative" "Negative" "Positive" "Positive" "Positive" "Positive"
##  [943] "Negative" "Positive" "Negative" "Positive" "Positive" "Negative"
##  [949] "Positive" "Positive" "Positive" "Negative" "Negative" "Positive"
##  [955] "Negative" "Negative" "Positive" "Negative" "Negative" "Positive"
##  [961] "Negative" "Positive" "Negative" "Negative" "Positive" "Positive"
##  [967] "Positive" "Positive" "Negative" "Positive" "Positive" "Negative"
##  [973] "Negative" "Positive" "Positive" "Positive" "Positive" "Positive"
##  [979] "Negative" "Negative" "Negative" "Negative" "Negative" "Negative"
##  [985] "Negative" "Negative" "Negative" "Positive" "Negative" "Negative"
##  [991] "Negative" "Negative" "Positive" "Negative" "Negative" "Positive"
##  [997] "Positive" "Positive" "Negative" "Negative" "Negative" "Positive"
## [1003] "Positive" "Positive" "Positive" "Negative" "Negative" "Positive"
## [1009] "Positive" "Positive" "Positive" "Negative" "Negative" "Positive"
## [1015] "Negative" "Positive" "Negative" "Negative" "Positive" "Negative"
## [1021] "Positive" "Negative" "Positive" "Negative" "Positive" "Negative"
## [1027] "Positive" "Positive" "Negative" "Positive" "Negative" "Positive"
## [1033] "Negative" "Positive" "Positive" "Positive" "Negative" "Positive"
## [1039] "Negative" "Negative" "Negative" "Positive" "Negative" "Positive"
## [1045] "Positive" "Negative" "Positive" "Negative" "Negative" "Negative"
## [1051] "Positive" "Negative" "Positive" "Negative" "Negative" "Negative"
## [1057] "Positive" "Negative" "Negative" "Negative" "Positive" "Negative"
## [1063] "Positive" "Positive" "Positive" "Positive" "Negative" "Positive"
## [1069] "Positive" "Negative" "Positive" "Negative" "Negative" "Negative"
## [1075] "Negative" "Negative" "Positive" "Positive" "Negative" "Positive"
## [1081] "Negative" "Positive" "Positive" "Positive" "Negative" "Negative"
## [1087] "Negative" "Negative" "Positive" "Positive" "Negative" "Negative"
## [1093] "Negative" "Negative" "Negative" "Positive" "Negative" "Positive"
## [1099] "Negative" "Negative" "Negative" "Positive" "Negative" "Positive"
## [1105] "Positive" "Negative" "Positive" "Negative" "Positive" "Positive"
## [1111] "Positive" "Negative" "Negative" "Positive" "Positive" "Positive"
## [1117] "Positive" "Negative" "Negative" "Negative" "Positive" "Negative"
## [1123] "Negative" "Negative" "Positive" "Positive" "Positive" "Positive"
## [1129] "Positive" "Negative" "Positive" "Negative" "Negative" "Negative"
## [1135] "Positive" "Negative" "Negative" "Negative" "Negative" "Positive"
## [1141] "Negative" "Negative" "Positive" "Negative" "Positive" "Negative"
## [1147] "Negative" "Negative" "Negative" "Negative" "Positive" "Negative"
## [1153] "Positive" "Negative" "Positive" "Positive" "Negative" "Positive"
## [1159] "Negative" "Negative" "Positive" "Positive" "Positive" "Negative"
## [1165] "Negative" "Negative" "Negative" "Positive" "Negative" "Negative"
## [1171] "Positive" "Negative" "Negative" "Negative" "Positive" "Negative"
## [1177] "Positive" "Negative" "Positive" "Negative" "Negative" "Positive"
## [1183] "Negative" "Negative" "Negative" "Positive" "Positive" "Negative"
## [1189] "Negative" "Positive" "Positive" "Positive" "Positive" "Negative"
## [1195] "Negative" "Negative" "Negative" "Positive" "Positive" "Negative"
## [1201] "Positive" "Positive" "Negative" "Negative" "Positive" "Positive"
## [1207] "Positive" "Negative" "Negative" "Negative" "Negative" "Negative"
## [1213] "Negative" "Positive" "Negative" "Negative" "Positive" "Positive"
## [1219] "Positive" "Negative" "Positive" "Negative" "Negative" "Positive"
## [1225] "Negative" "Negative" "Positive" "Positive" "Positive" "Positive"
## [1231] "Negative" "Negative" "Positive" "Positive" "Negative" "Negative"
## [1237] "Negative" "Negative" "Negative" "Negative" "Positive" "Positive"
## [1243] "Negative" "Negative" "Positive" "Positive" "Positive" "Positive"
## [1249] "Negative" "Positive" "Negative" "Negative" "Positive" "Negative"
## [1255] "Negative" "Negative" "Negative" "Positive" "Negative" "Positive"
## [1261] "Positive" "Positive" "Negative" "Positive" "Positive" "Positive"
## [1267] "Positive" "Positive" "Negative" "Positive" "Negative" "Negative"
## [1273] "Negative" "Negative" "Positive" "Positive" "Positive" "Negative"
## [1279] "Positive" "Negative" "Negative" "Positive" "Positive" "Positive"
## [1285] "Negative" "Negative" "Positive" "Negative" "Positive" "Positive"
## [1291] "Negative" "Negative" "Positive" "Positive" "Positive" "Negative"
## [1297] "Negative" "Positive" "Negative" "Negative" "Positive" "Positive"
## [1303] "Negative" "Negative" "Positive" "Negative" "Negative" "Negative"
## [1309] "Negative" "Negative" "Negative" "Negative" "Negative" "Positive"
## [1315] "Positive" "Positive" "Positive" "Negative" "Negative" "Negative"
## [1321] "Negative" "Negative" "Negative" "Positive" "Positive" "Negative"
## [1327] "Negative" "Negative" "Negative" "Positive" "Negative" "Positive"
## [1333] "Positive" "Positive" "Positive" "Positive" "Positive" "Positive"
## [1339] "Positive" "Positive" "Negative" "Positive" "Negative" "Negative"
## [1345] "Negative" "Positive" "Positive" "Positive" "Positive" "Positive"
## [1351] "Positive" "Negative" "Negative" "Negative" "Negative" "Negative"
## [1357] "Positive" "Positive" "Positive" "Positive" "Negative" "Negative"
## [1363] "Positive" "Negative" "Negative" "Negative" "Positive" "Negative"
## [1369] "Positive" "Positive" "Negative" "Positive" "Positive" "Negative"
## [1375] "Negative" "Negative" "Negative" "Positive" "Negative" "Negative"
## [1381] "Negative" "Negative" "Positive" "Negative" "Positive" "Positive"
## [1387] "Positive" "Positive" "Positive" "Negative" "Positive" "Negative"
## [1393] "Negative" "Negative" "Positive" "Negative" "Negative" "Negative"
## [1399] "Positive" "Positive" "Positive" "Positive" "Positive" "Positive"
## [1405] "Positive" "Negative" "Positive" "Positive" "Positive" "Negative"
## [1411] "Negative" "Positive" "Positive" "Negative" "Positive" "Positive"
## [1417] "Negative" "Positive" "Positive" "Positive" "Positive" "Negative"
## [1423] "Negative" "Positive" "Negative" "Negative" "Negative" "Negative"
## [1429] "Positive" "Positive" "Positive" "Positive" "Positive" "Negative"
## [1435] "Positive" "Positive" "Positive" "Positive" "Negative" "Positive"
## [1441] "Positive" "Positive" "Positive" "Positive" "Negative" "Negative"
## [1447] "Positive" "Negative" "Negative" "Positive" "Positive" "Positive"
## [1453] "Negative" "Negative" "Positive" "Positive" "Positive" "Negative"
## [1459] "Negative" "Positive" "Positive" "Positive" "Negative" "Negative"
## [1465] "Positive" "Negative" "Positive" "Negative" "Positive" "Positive"
## [1471] "Positive" "Positive" "Positive" "Positive" "Positive" "Negative"
## [1477] "Positive" "Positive" "Positive" "Negative" "Negative" "Negative"
## [1483] "Negative" "Negative" "Negative" "Positive" "Positive" "Negative"
## [1489] "Positive" "Negative" "Negative" "Negative" "Negative" "Negative"
## [1495] "Positive" "Positive" "Negative" "Negative" "Positive" "Negative"
## [1501] "Negative" "Positive" "Positive" "Positive" "Negative" "Positive"
## [1507] "Negative" "Negative" "Positive" "Positive" "Negative" "Positive"
## [1513] "Positive" "Negative" "Negative" "Negative" "Positive" "Positive"
## [1519] "Negative" "Negative" "Negative" "Negative" "Positive" "Negative"
## [1525] "Negative" "Negative" "Positive" "Negative" "Negative" "Negative"
## [1531] "Positive" "Positive" "Negative" "Positive" "Negative" "Positive"
## [1537] "Negative" "Positive" "Positive" "Positive" "Positive" "Negative"
## [1543] "Negative" "Positive" "Positive" "Positive" "Positive" "Positive"
## [1549] "Positive" "Negative" "Positive" "Positive" "Negative" "Negative"
## [1555] "Negative" "Positive" "Positive" "Positive" "Positive" "Negative"
## [1561] "Negative" "Positive" "Negative" "Negative" "Negative" "Positive"
## [1567] "Negative" "Positive" "Negative" "Negative" "Negative" "Positive"
## [1573] "Positive" "Negative" "Positive" "Positive" "Positive" "Negative"
## [1579] "Negative" "Positive" "Negative" "Negative" "Positive" "Negative"
## [1585] "Negative" "Negative" "Negative" "Negative" "Negative" "Positive"
## [1591] "Negative" "Negative" "Negative" "Positive" "Positive" "Positive"
## [1597] "Positive" "Negative" "Negative" "Positive" "Positive" "Positive"
## [1603] "Positive" "Positive" "Negative" "Negative" "Negative" "Negative"
## [1609] "Positive" "Positive" "Negative" "Negative" "Positive" "Negative"
## [1615] "Negative" "Negative" "Positive" "Positive" "Negative" "Negative"
## [1621] "Positive" "Positive" "Positive" "Positive" "Positive" "Negative"
## [1627] "Negative" "Negative" "Negative" "Negative" "Negative" "Positive"
## [1633] "Negative" "Negative" "Positive" "Negative" "Negative" "Negative"
## [1639] "Positive" "Negative" "Negative" "Negative" "Positive" "Negative"
## [1645] "Positive" "Negative" "Negative" "Negative" "Negative" "Negative"
## [1651] "Positive" "Negative" "Negative" "Negative" "Positive" "Negative"
## [1657] "Negative" "Negative" "Negative" "Negative" "Negative" "Negative"
## [1663] "Positive" "Negative" "Negative" "Negative" "Negative" "Negative"
## [1669] "Positive" "Negative" "Positive" "Positive" "Negative" "Negative"
## [1675] "Negative" "Negative" "Positive" "Positive" "Negative" "Positive"
## [1681] "Negative" "Positive" "Negative" "Negative" "Positive" "Negative"
## [1687] "Negative" "Positive" "Positive" "Positive" "Positive" "Negative"
## [1693] "Negative" "Positive" "Negative" "Negative" "Negative" "Negative"
## [1699] "Positive" "Negative" "Positive" "Positive" "Negative" "Negative"
## [1705] "Negative" "Positive" "Negative" "Positive" "Negative" "Negative"
## [1711] "Negative" "Negative" "Negative" "Positive" "Negative" "Negative"
## [1717] "Positive" "Positive" "Positive" "Positive" "Positive" "Positive"
## [1723] "Positive" "Positive" "Positive" "Positive" "Negative" "Positive"
## [1729] "Negative" "Positive" "Negative" "Positive" "Positive" "Positive"
## [1735] "Negative" "Negative" "Positive" "Positive" "Positive" "Positive"
## [1741] "Negative" "Negative" "Positive" "Negative" "Positive" "Positive"
## [1747] "Negative" "Positive" "Positive" "Positive" "Negative" "Positive"
## [1753] "Negative" "Positive" "Positive" "Positive" "Negative" "Negative"
## [1759] "Positive" "Negative" "Positive" "Negative" "Negative" "Negative"
## [1765] "Negative" "Positive" "Negative" "Negative" "Negative" "Negative"
## [1771] "Negative" "Positive" "Positive" "Negative" "Positive" "Positive"
## [1777] "Positive" "Negative" "Positive" "Negative" "Positive" "Negative"
## [1783] "Positive" "Negative" "Positive" "Positive" "Positive" "Positive"
## [1789] "Negative" "Negative" "Negative" "Positive" "Positive" "Negative"
## [1795] "Negative" "Negative" "Negative" "Negative" "Positive" "Negative"
## [1801] "Positive" "Positive" "Positive" "Negative" "Negative" "Negative"
## [1807] "Positive" "Negative" "Negative" "Negative" "Positive" "Negative"
## [1813] "Positive" "Positive" "Positive" "Negative" "Positive" "Negative"
## [1819] "Positive" "Positive" "Positive" "Positive" "Negative" "Negative"
## [1825] "Negative" "Positive" "Negative" "Positive" "Positive" "Negative"
## [1831] "Negative" "Negative" "Positive" "Positive" "Positive" "Negative"
## [1837] "Negative" "Negative" "Negative" "Negative" "Negative" "Positive"
## [1843] "Negative" "Positive" "Positive" "Negative" "Positive" "Negative"
## [1849] "Positive" "Positive" "Positive" "Negative" "Positive" "Positive"
## [1855] "Positive" "Positive" "Positive" "Negative" "Positive" "Negative"
## [1861] "Negative" "Negative" "Negative" "Positive" "Positive" "Positive"
## [1867] "Positive" "Positive" "Positive" "Negative" "Positive" "Positive"
## [1873] "Positive" "Negative" "Negative" "Negative" "Positive" "Negative"
## [1879] "Positive" "Positive" "Positive" "Negative" "Negative" "Negative"
## [1885] "Negative" "Negative" "Positive" "Negative" "Positive" "Negative"
## [1891] "Positive" "Negative" "Negative" "Negative" "Positive" "Negative"
## [1897] "Negative" "Negative" "Positive" "Positive" "Positive" "Positive"
## [1903] "Positive" "Positive" "Positive" "Negative" "Positive" "Negative"
## [1909] "Negative" "Negative" "Positive" "Negative" "Positive" "Negative"
## [1915] "Negative" "Negative" "Negative" "Positive" "Negative" "Negative"
## [1921] "Positive" "Negative" "Positive" "Positive" "Negative" "Positive"
## [1927] "Negative" "Positive" "Negative" "Negative" "Negative" "Negative"
## [1933] "Positive" "Positive" "Negative" "Negative" "Positive" "Negative"
## [1939] "Negative" "Negative" "Negative" "Positive" "Positive" "Negative"
## [1945] "Negative" "Positive" "Positive" "Negative" "Negative" "Negative"
## [1951] "Negative" "Positive" "Negative" "Negative" "Negative" "Negative"
## [1957] "Positive" "Negative" "Negative" "Negative" "Negative" "Positive"
## [1963] "Positive" "Positive" "Positive" "Positive" "Negative" "Negative"
## [1969] "Negative" "Negative" "Positive" "Positive" "Negative" "Negative"
## [1975] "Positive" "Negative" "Positive" "Positive" "Positive" "Positive"
## [1981] "Negative" "Negative" "Positive" "Positive" "Positive" "Positive"
## [1987] "Negative" "Negative" "Negative" "Negative" "Negative" "Positive"
## [1993] "Negative" "Positive" "Positive" "Positive" "Negative" "Negative"
## [1999] "Positive" "Negative" "Negative" "Negative" "Negative" "Negative"
## [2005] "Negative" "Negative" "Positive" "Negative" "Positive" "Positive"
## [2011] "Positive" "Positive" "Negative" "Positive" "Positive" "Positive"
## [2017] "Negative" "Positive" "Positive" "Positive" "Negative" "Positive"
## [2023] "Positive" "Negative" "Positive" "Positive" "Positive" "Positive"
## [2029] "Positive" "Negative" "Negative" "Positive" "Positive" "Positive"
## [2035] "Positive" "Negative" "Negative" "Negative" "Negative" "Positive"
## [2041] "Positive" "Positive" "Positive" "Negative" "Negative" "Positive"
## [2047] "Positive" "Negative" "Positive" "Positive" "Negative" "Positive"
## [2053] "Positive" "Negative" "Positive" "Negative" "Positive" "Positive"
## [2059] "Positive" "Negative" "Negative" "Positive" "Positive" "Positive"
## [2065] "Positive" "Negative" "Positive" "Negative" "Negative" "Positive"
## [2071] "Negative" "Positive" "Positive" "Negative" "Negative" "Positive"
## [2077] "Positive" "Positive" "Positive" "Positive" "Negative" "Positive"
## [2083] "Positive" "Negative" "Positive" "Positive" "Positive" "Negative"
## [2089] "Positive" "Negative" "Negative" "Negative" "Positive" "Positive"
## [2095] "Negative" "Negative" "Negative" "Negative" "Positive" "Positive"
## [2101] "Positive" "Positive" "Positive" "Negative" "Negative" "Positive"
## [2107] "Positive" "Positive" "Positive" "Positive" "Negative" "Positive"
## [2113] "Positive" "Negative" "Positive" "Positive" "Negative" "Positive"
## [2119] "Positive" "Positive" "Positive" "Positive" "Positive" "Negative"
## [2125] "Positive" "Positive" "Positive" "Negative" "Negative" "Positive"
## [2131] "Positive" "Negative" "Positive" "Positive" "Positive" "Positive"
## [2137] "Positive" "Negative" "Negative" "Positive" "Negative" "Positive"
## [2143] "Positive" "Positive" "Negative" "Negative" "Positive" "Negative"
## [2149] "Positive" "Positive" "Positive" "Positive" "Negative" "Positive"
## [2155] "Positive" "Positive" "Positive" "Negative" "Negative" "Positive"
## [2161] "Negative" "Negative" "Positive" "Positive" "Positive" "Positive"
## [2167] "Negative" "Positive" "Positive" "Negative" "Positive" "Negative"
## [2173] "Positive" "Positive" "Positive" "Negative" "Negative" "Positive"
## [2179] "Positive" "Positive" "Negative" "Positive" "Negative" "Negative"
## [2185] "Positive" "Positive" "Positive" "Negative" "Negative" "Positive"
## [2191] "Negative" "Positive" "Positive" "Negative" "Positive" "Negative"
## [2197] "Negative" "Negative" "Negative" "Negative" "Negative" "Positive"
## [2203] "Negative" "Negative" "Negative" "Negative" "Negative" "Negative"
## [2209] "Positive" "Negative" "Positive" "Positive" "Positive" "Positive"
## [2215] "Positive" "Negative" "Negative" "Positive" "Negative" "Negative"
## [2221] "Negative" "Negative" "Negative" "Negative" "Negative" "Positive"
## [2227] "Negative" "Negative" "Positive" "Negative" "Positive" "Positive"
## [2233] "Negative" "Positive" "Negative" "Negative" "Positive" "Negative"
## [2239] "Negative" "Positive" "Negative" "Positive" "Positive" "Positive"
## [2245] "Positive" "Positive" "Positive" "Positive" "Positive" "Positive"
## [2251] "Negative" "Positive" "Positive" "Positive" "Positive" "Negative"
## [2257] "Negative" "Negative" "Positive" "Positive" "Positive" "Negative"
## [2263] "Negative" "Negative" "Positive" "Negative" "Positive" "Negative"
## [2269] "Positive" "Positive" "Positive" "Positive" "Negative" "Positive"
## [2275] "Positive" "Negative" "Negative" "Negative" "Negative" "Positive"
## [2281] "Negative" "Negative" "Positive" "Positive" "Negative" "Positive"
## [2287] "Positive" "Positive" "Negative" "Negative" "Positive" "Negative"
## [2293] "Negative" "Negative" "Negative" "Positive" "Negative" "Negative"
## [2299] "Positive" "Negative" "Positive" "Positive" "Negative" "Positive"
## [2305] "Negative" "Negative" "Negative" "Positive" "Positive" "Positive"
## [2311] "Positive" "Positive" "Positive" "Positive" "Negative" "Negative"
## [2317] "Positive" "Negative" "Positive" "Negative" "Positive" "Negative"
## [2323] "Negative" "Negative" "Positive" "Positive" "Positive" "Negative"
## [2329] "Negative" "Negative" "Positive" "Positive" "Negative" "Positive"
## [2335] "Positive" "Negative" "Negative" "Positive" "Positive" "Positive"
## [2341] "Negative" "Positive" "Positive" "Negative" "Positive" "Negative"
## [2347] "Positive" "Positive" "Negative" "Positive" "Negative" "Positive"
## [2353] "Negative" "Negative" "Negative" "Positive" "Negative" "Negative"
## [2359] "Positive" "Negative" "Negative" "Negative" "Negative" "Negative"
## [2365] "Positive" "Positive" "Negative" "Positive" "Negative" "Negative"
## [2371] "Positive" "Negative" "Negative" "Positive" "Positive" "Positive"
## [2377] "Negative" "Positive" "Negative" "Negative" "Positive" "Negative"
## [2383] "Negative" "Positive" "Positive" "Positive" "Positive" "Positive"
## [2389] "Negative" "Negative"

I think i must’ve implemented this confusion matrix wrong?

confusion_matrix <- table(unlist(predictions), unlist(LOTR$binary_scores))
accuracy <- sum(diag(confusion_matrix)) / sum(confusion_matrix)
precision <- diag(confusion_matrix) / colSums(confusion_matrix)
recall <- diag(confusion_matrix) / rowSums(confusion_matrix)
## Warning in diag(confusion_matrix)/rowSums(confusion_matrix): longer object
## length is not a multiple of shorter object length
confusion_matrix
##    
##       0   1
##   0 764   0
##   1 110   0
##   2 361   0
##   3   0 219
##   4   0 936
accuracy
## [1] 0.3196653
precision
##         0         1 
## 0.6186235 0.0000000
recall
##         0         1         2         3         4 
## 1.0000000 0.0000000 2.1163435 0.0000000 0.8162393