Reflection

My understanding of how R may be used to arrange and examine social media data has improved as a result of working on this Bluesky research. I imported my Excel information into R, cleaned the post text, computed engagement metrics, found frequent words, examined sentiment, and made an interactive Plotly graphic. AI was useful since it gave me a foundation for my programming and clarified how various features operated.

But I discovered that code produced by AI is not always accurate and has to be verified. To ensure that the code fit my dataset and that the outcomes made sense, I had to exercise my own discretion. While AI can assist with computations and recommendations, human judgment is still crucial for analyzing data, identifying trends, and determining what information is most essential.Overall, AI made the process easier, but I still needed to review the work and understand the results myself.

Load Packages

library(readxl)
library(dplyr)
library(tidytext)
library(stringr)
library(plotly)


# Import Excel file
social_media <- read_excel("social_media_analysis.xlsx", sheet = "All Posts")

# View the data
head(social_media)
## # A tibble: 6 × 12
##   `Author name`     `Author handle`     `Post text` Likes Reposts Replies Quotes
##   <chr>             <chr>               <chr>       <dbl>   <dbl>   <dbl>  <dbl>
## 1 Real Madrid Live  realmadrid-fans.bs… "⚽ One to …    25       1       0      0
## 2 FC Barcelona news fcbarcelona-news.b… "⚽ One to …     4       1       0      0
## 3 swoosh supply     swooshsupply.bsky.… "Nike SB's…     4       1       0      0
## 4 > 𝘉𝘢𝘦.            hnkbae.bsky.social  "Ah, sim, …     4       0       0      0
## 5 goods             goods.wouldcop.com  "Nike Tech…     4       0       1      0
## 6 <NA>              sneakerscouts.bsky… "Nike Air …     4       0       0      0
## # ℹ 5 more variables: Bookmarks <dbl>, `Created at` <chr>, Tags <chr>,
## #   Links <chr>, `Likes + reposts` <dbl>
names(social_media)
##  [1] "Author name"     "Author handle"   "Post text"       "Likes"          
##  [5] "Reposts"         "Replies"         "Quotes"          "Bookmarks"      
##  [9] "Created at"      "Tags"            "Links"           "Likes + reposts"
# Clean the post text
social_media <- social_media %>%
  mutate(
    post_text_clean = `Post text` %>%
      str_to_lower() %>%
      str_replace_all("https?://\\S+", " ") %>%
      str_replace_all("@\\S+", " ") %>%
      str_replace_all("#\\S+", " ") %>%
      str_replace_all("[^a-zA-Z\\s]", " ") %>%
      str_squish()
  )

# Basic engagement statistics
engagement_stats <- social_media %>%
  summarise(
    total_posts = n(),
    total_likes = sum(Likes, na.rm = TRUE),
    total_reposts = sum(Reposts, na.rm = TRUE),
    total_replies = sum(Replies, na.rm = TRUE),
    total_quotes = sum(Quotes, na.rm = TRUE),
    total_bookmarks = sum(Bookmarks, na.rm = TRUE),
    average_likes = mean(Likes, na.rm = TRUE),
    average_reposts = mean(Reposts, na.rm = TRUE),
    average_engagement = mean(`Likes + reposts`, na.rm = TRUE)
  )

engagement_stats
## # A tibble: 1 × 9
##   total_posts total_likes total_reposts total_replies total_quotes
##         <int>       <dbl>         <dbl>         <dbl>        <dbl>
## 1         100          76             3             9            2
## # ℹ 4 more variables: total_bookmarks <dbl>, average_likes <dbl>,
## #   average_reposts <dbl>, average_engagement <dbl>
# Top 10 posts by engagement
top_posts <- social_media %>%
  arrange(desc(`Likes + reposts`)) %>%
  select(`Author name`, `Post text`, Likes, Reposts, `Likes + reposts`) %>%
  slice_head(n = 10)

top_posts
## # A tibble: 10 × 5
##    `Author name`      `Post text`                Likes Reposts `Likes + reposts`
##    <chr>              <chr>                      <dbl>   <dbl>             <dbl>
##  1 Real Madrid Live   "⚽ One to watch:\n\nSpotl…    25       1                26
##  2 FC Barcelona news  "⚽ One to watch:\n\nSpotl…     4       1                 5
##  3 swoosh supply      "Nike SB's skate-ready Ai…     4       1                 5
##  4 > 𝘉𝘢𝘦.             "Ah, sim, meus queridos N…     4       0                 4
##  5 goods              "Nike Tech Fleece (M), L …     4       0                 4
##  6 <NA>               "Nike Air DT Max 96 Low '…     4       0                 4
##  7 cat drip           "Nike Tech Fleece (M), L …     3       0                 3
##  8 calmly cashmere 89 "Nike Tech Fleece (M), L …     3       0                 3
##  9 brightly cashmere  "Nike Tech Fleece (M), L …     3       0                 3
## 10 KICKTERMINAL       "#ad\n\n🚨 DROP À SURVEILL…     3       0                 3
# Break posts into individual words
words <- social_media %>%
  select(post_text_clean) %>%
  unnest_tokens(word, post_text_clean)

# Find most common words
data("stop_words")

word_counts_clean <- words %>%
  anti_join(stop_words, by = "word") %>%
  filter(
    str_length(word) > 2,
    !str_detect(word, "^[0-9]+$")
  ) %>%
  count(word, sort = TRUE)

# Display top 20 most common words
word_counts_clean %>%
  slice_head(n = 20)
## # A tibble: 20 × 2
##    word          n
##    <chr>     <int>
##  1 nike        107
##  2 air          21
##  3 nis          19
##  4 cupom        18
##  5 por          18
##  6 moda         17
##  7 chez         15
##  8 masculino    11
##  9 mercado      11
## 10 livre        10
## 11 pour         10
## 12 size          9
## 13 check         8
## 14 heritage      7
## 15 octobre       7
## 16 uplift        7
## 17 white         7
## 18 www           7
## 19 dans          6
## 20 force         6
# Sentiment analysis
sentiment_words <- get_sentiments("bing")

sentiment_results <- words %>%
  inner_join(sentiment_words, by = "word") %>%
  count(sentiment, sort = TRUE)

sentiment_results
## # A tibble: 2 × 2
##   sentiment     n
##   <chr>     <int>
## 1 positive     46
## 2 negative     14
# Calculate sentiment percentages
sentiment_percent <- sentiment_results %>%
  mutate(
    percentage = round(n / sum(n) * 100, 2)
  )

sentiment_percent
## # A tibble: 2 × 3
##   sentiment     n percentage
##   <chr>     <int>      <dbl>
## 1 positive     46       76.7
## 2 negative     14       23.3
# Most common positive and negative words
sentiment_word_counts <- words %>%
  inner_join(sentiment_words, by = "word") %>%
  count(sentiment, word, sort = TRUE)

# Top positive words
sentiment_word_counts %>%
  filter(sentiment == "positive") %>%
  slice_head(n = 10)
## # A tibble: 10 × 3
##    sentiment word           n
##    <chr>     <chr>      <int>
##  1 positive  available      9
##  2 positive  uplift         7
##  3 positive  cool           2
##  4 positive  elite          2
##  5 positive  nice           2
##  6 positive  premier        2
##  7 positive  spotless       2
##  8 positive  well           2
##  9 positive  appreciate     1
## 10 positive  clean          1
# Top negative words
sentiment_word_counts %>%
  filter(sentiment == "negative") %>%
  slice_head(n = 10)
## # A tibble: 10 × 3
##    sentiment word          n
##    <chr>     <chr>     <int>
##  1 negative  dick          2
##  2 negative  disagree      2
##  3 negative  bitch         1
##  4 negative  boycott       1
##  5 negative  defy          1
##  6 negative  evil          1
##  7 negative  freak         1
##  8 negative  hurting       1
##  9 negative  leak          1
## 10 negative  prejudice     1
# Create data for interactive Plotly chart
plot_data <- social_media %>%
  arrange(desc(`Likes + reposts`)) %>%
  slice_head(n = 10) %>%
  mutate(
    post_number = paste0("Post ", row_number())
  )

# Interactive Plotly chart
fig <- plot_ly(
  data = plot_data,
  x = ~post_number,
  y = ~`Likes + reposts`,
  type = "bar",
  text = ~`Likes + reposts`,
  hovertext = ~`Post text`,
  hoverinfo = "text+y"
) %>%
  layout(
    title = "Top 10 Bluesky Posts by Engagement",
    xaxis = list(title = "Post"),
    yaxis = list(title = "Likes + Reposts")
  )

fig