r Sys.Date()Reflection
The AI gave me a working structure quickly. It loaded the data, cleaned the text, counted words, ran the bing sentiment analysis, and built a plotly chart. However, the first version was too generic to trust. It did not know our file used latin1 encoding, and it did not handle link-only posts, duplicates, or posts in other languages. The bing lexicon also misread movie language, such as counting “like” as positive when it was only a verb and “horror” as negative when it is just a genre. My results improved after I rewrote the prompt with the column names, file encoding, and exact cleaning steps.Human judgment is what makes the analysis trustworthy. I had to choose the right dataset, decide what counts as noise, verify the code with spot checks, and decide whether the results made sense for movie posts. The AI can write code, but it cannot tell whether an answer is meaningful for our research question. I liked this lab design because it forced me to critique the AI’s output instead of copying it. One thing I would add is a short checklist for verifying AI code, since knowing what to check was the hardest part.
AI Disclosure: The R code in this document was written with the help of an AI assistant (Claude by Anthropic), as required by this lab. I reviewed, tested, and revised the code and results myself.
{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
{r packages} library(tidyverse) library(tidytext) library(plotly)
```{r read-data} posts <- read_csv( “Gian Abundo -Scrape Movies 11-3(Sheet1).csv”, locale = locale(encoding = “latin1”) )
glimpse(posts) nrow(posts)
3. Clean the data
```{r clean-data}
posts_clean <- posts %>%
filter(!is.na(text)) %>% # drop missing text
distinct(text, .keep_all = TRUE) %>% # drop duplicate posts
mutate(
post_id = row_number(),
text_clean = text %>%
str_replace_all("\\\\n", " ") %>% # literal "\n"
str_replace_all("https?://\\S+|www\\.\\S+", " ") %>% # URLs
str_replace_all("@\\S+", " ") %>% # @mentions
str_replace_all("[[:punct:]]", " ") %>% # punctuation
str_replace_all("[0-9]+", " ") %>% # numbers
str_to_lower() %>%
str_squish()
) %>%
filter(text_clean != "") # drop posts that were only links
cat("Rows before cleaning:", nrow(posts), "\n")
cat("Rows after cleaning: ", nrow(posts_clean), "\n")
```{r word-freq} data(“stop_words”)
words <- posts_clean %>% select(post_id, text_clean) %>% unnest_tokens(word, text_clean) %>% anti_join(stop_words, by = “word”) %>% filter(nchar(word) > 2)
top_words <- words %>% count(word, sort = TRUE) %>% slice_head(n = 15)
top_words
5. Sentiment analysis (bing lexicon)
```{r sentiment}
sentiment_words <- words %>%
inner_join(get_sentiments("bing"), by = "word")
# Overall positive vs. negative word counts
sentiment_summary <- sentiment_words %>%
count(sentiment) %>%
mutate(percent = round(100 * n / sum(n), 1))
sentiment_summary
# Net sentiment per post (positive words minus negative words)
post_sentiment <- sentiment_words %>%
count(post_id, sentiment) %>%
pivot_wider(names_from = sentiment, values_from = n, values_fill = 0) %>%
mutate(net_sentiment = positive - negative)
summary(post_sentiment$net_sentiment)
```{r plotly-chart} top_sentiment_words <- sentiment_words %>% count(word, sentiment, sort = TRUE) %>% group_by(sentiment) %>% slice_max(n, n = 10, with_ties = FALSE) %>% ungroup()
plot_ly( data = top_sentiment_words, x = ~n, y = ~reorder(word, n), color = ~sentiment, colors = c(negative = “#d95f02”, positive = “#1b9e77”), type = “bar”, orientation = “h” ) %>% layout( title = “Top Positive and Negative Words in BlueSky Movie Posts”, xaxis = list(title = “Word count”), yaxis = list(title = ““) )
7. Verify the results
Spot checks and sanity checks on the AI-written code.
```{r verify-counts}
# Sanity check 1: sentiment counts should add up to total matched words
sum(sentiment_summary$n) == nrow(sentiment_words)
# Sanity check 2: no stopwords or URL fragments left in the top words
top_words$word
# Sanity check 3: hand-check the top 10 sentiment-coded words in context
sentiment_words %>% count(word, sentiment, sort = TRUE) %>% head(10)
{r verify-spot-check} # Spot check: compare raw text, cleaned text, and net sentiment for 5 random posts set.seed(9) posts_clean %>% slice_sample(n = 5) %>% select(post_id, text, text_clean) %>% left_join(post_sentiment, by = "post_id")
{r verify-hand-calc} # Hand calculation: pick one post, count its positive/negative words by eye, # then compare with the code's result. check_id <- 1 posts_clean %>% filter(post_id == check_id) %>% pull(text) sentiment_words %>% filter(post_id == check_id)
Notes on limitations (edit with your own findings):
bing lexicon is general-purpose. A word like “like”
is coded positive even when it isn’t, and “horror” is coded negative
even though it is just a movie genre.