Making Sense
of Messy Data

Randi Bolt

Agenda

A cartoon-style vertical illustration shows Tina Kotek standing in an Oregon Employment Department meeting room and asking, “Can your team analyze this survey data?” Three analysts sit around a table with a laptop, notebook, and simple chart printouts. An OED sign is on the wall, and the Oregon State Capitol is visible through the window.

The Challenges

Basic Workflow & Sentiment Analysis

Choosing K

Topic Modeling

The Challenges of Open Text Response Data

Tight timeline

4,000+ open-ended responses

Small review team

Manual review can be inconsistent

Pre-defined categories can shape what reviewers look for

Step 1: Clean the Data

library(tidyverse)
clean_text <- function(raw_data, text) {
  raw_data |>
    mutate(
      text_clean = {{ text }} |>
        str_to_lower() |>
        str_replace_all("<br\\s*/?>", " ") |>
        str_replace_all("[^a-z0-9\\s]", " ") |>
        str_squish()
    )
}

Step 2: Tokenize the Text

library(tidytext)
tokenize_text <- function(clean_data) {
  clean_data |>
    unnest_tokens(word, text_clean) |>
    anti_join(stop_words, by = "word")
}

Step 3: Term frequencies

term_frequencies <- function(tokenize_data) {
  tokenize_data |>
    count(word, sort = TRUE) 
}

Term Frequencies Example

elf <- read_csv("data/elf_reviews_text_only.csv")
clean_elf <- clean_text(elf, review_text)
tokenize_elf <- tokenize_text(clean_elf)
term_frequencies_elf <- term_frequencies(tokenize_elf)
term_frequencies_elf
# A tibble: 4,167 × 2
   word          n
   <chr>     <int>
 1 movie       529
 2 elf         518
 3 christmas   460
 4 film        387
 5 buddy       323
 6 ferrell     298
 7 santa       168
 8 funny       162
 9 watch       143
10 family      126
# ℹ 4,157 more rows

Step 4: Sentiment analysis

library(wordcloud)
sentiment_wordcloud <- function(tokenize_data, lexicon = "bing") {
  tokenize_data |>
    inner_join(get_sentiments(lexicon), by = "word") |>
    count(word, sentiment) |>
    pivot_wider(
      names_from = sentiment, 
      values_from = n, 
      values_fill = 0) |>
    column_to_rownames("word") |>
    comparison.cloud(colors = c("#C0392B", "#2E7D32"))
}

Word Cloud Example

wordcloud_elf <- sentiment_wordcloud(tokenize_elf)
wordcloud_elf

Step 5: Prepare Data for Model

library(stm)
stm_data <- function(tokenize_data) {
  dtm <- tokenize_data |>
    count(row_id, word) |>
    cast_sparse(row_id, word, n)
  readCorpus(
    as.matrix(dtm),
    type = "dtm"
  )
}

Step 6: Choosing K

search_topics <- function(stm_data, k) {
  searchK(
    documents = stm_data$documents,
    vocab = stm_data$vocab,
    K = k
  )
}

Considerations

  1. Where does held-out mostly plateau?
  1. Where does residuals mostly level off?
  1. Where are exclusivity and coherence both reasonable?

Choosing K Example

stm_elf <- stm_data(tokenize_elf)
search_k_elf <- search_topics(stm_elf, 2:10)
plot(search_k_elf)

Step 6: Topic Model

model_data <- function(stm_data, k) {
  stm(
    documents = preped_data$documents,
    vocab = preped_data$vocab,
    K = k
  )
}

Topic Model Example

model_elf <- model_data(stm_elf, k = 8)
plot(model_elf)

Conclusion