Youtube Scraping

Author

smith

Motivation

Whenever I want to get information on topics I like in a time when I do not feel like using something like using YouTube as it usually has other people who like the same things I do. This is especially true when looking through the comments. However, it has become harder to look at the comments with newer YouTube updates. But commenting is so fun to do! I want to know what gets the most likes so that in the future, I might want to get insight on what does and does not get likes. I want to see if there is a relationship between elements of a comment and the number of likes it gets. So I scraped values of Username of Commenter, The Comment, Time, Likes, and the # of Replies.

I will try to answer this through testing what features of a comment or traits within each feature are related with an increase in likes or replies.

Changing our Data

We have to load our libraries.

library(tidyverse) # The tidyverse collection of packages
Warning: package 'tidyverse' was built under R version 4.5.3
Warning: package 'tidyr' was built under R version 4.5.3
Warning: package 'readr' was built under R version 4.5.3
Warning: package 'purrr' was built under R version 4.5.3
Warning: package 'dplyr' was built under R version 4.5.3
Warning: package 'forcats' was built under R version 4.5.3
Warning: package 'lubridate' was built under R version 4.5.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.1     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.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(readr)     # To quickly read csv files
library(ggplot2)    # To make graphs easily

Let’s read in our data that we scraped. I scraped a YouTube video (https://www.youtube.com/watch?v=YmHZI03a_Yo) of a sound test I usually use if I want to listen to music when I am home so that I know my headphones work.

youtube <- read_csv("https://myxavier-my.sharepoint.com/:x:/g/personal/smithj188_xavier_edu/IQAiM3rmCmKwT7sZGWzxN2eSAQHsoQGJ5R784DHv8s-kUYM?e=rqqkk4&download=1")
Rows: 180 Columns: 5
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (5): username, body, time, comments, likes

ℹ 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.

Lets perform changes on our columns, like changing our “Likes”, and change the “K” or “M” to 1000 and 1000000. We are assuming we do not know the exact number.

# Likes
youtube <- youtube %>%
  mutate(likes = case_when(
    str_detect(likes, "K") ~
      as.numeric(str_remove(likes, "K")) * 1000,
    str_detect(likes, "K") ~
      as.numeric(str_remove(likes, "M")) * 1000000,
    TRUE ~ as.numeric(likes)
    ))
Warning: There were 2 warnings in `mutate()`.
The first warning was:
ℹ In argument: `likes = case_when(...)`.
Caused by warning:
! NAs introduced by coercion
ℹ Run `dplyr::last_dplyr_warnings()` to see the 1 remaining warning.

Now we change our “time” to days, as that would be easy enough to not have to use many decimal values. I will also assume that leap years do not exist (if something is over 4 years old) because of convenience. (I am sorry that it is just a bunch of if/else commands)

# Time 
youtube <- youtube %>%
  mutate(time = str_remove(time, "\\(edited\\)")) %>%
  mutate(time = case_when(
    str_detect(time, "years ago") ~
      as.numeric(str_remove(time, "years ago")) * 365,
    str_detect(time, "year ago") ~
      as.numeric(str_remove(time, "year ago")) * 365,
    str_detect(time, "months ago") ~
      as.numeric(str_remove(time, "months ago")) * 30,
    str_detect(time, "month ago") ~
      as.numeric(str_remove(time, "month ago")) * 30,
    str_detect(time, "weeks ago") ~
      as.numeric(str_remove(time, "weeks ago")) * 7,
    str_detect(time, "week ago") ~
      as.numeric(str_remove(time, "week ago")) * 7,
    str_detect(time, "days ago") ~
      as.numeric(str_remove(time, "days ago")),
    str_detect(time, "day ago") ~
      as.numeric(str_remove(time, "day ago")),
    str_detect(time, "hours ago") ~
      as.numeric(str_remove(time, "hours ago")) / 24,
    str_detect(time, "hour ago") ~
      as.numeric(str_remove(time, "hour ago")) / 24,
    str_detect(time, "minutes ago") ~
      as.numeric(str_remove(time, "minutes ago")) / 1440,
    str_detect(time, "minute ago") ~
      as.numeric(str_remove(time, "minute ago")) / 1440
  ))
Warning: There were 12 warnings in `mutate()`.
The first warning was:
ℹ In argument: `time = case_when(...)`.
Caused by warning:
! NAs introduced by coercion
ℹ Run `dplyr::last_dplyr_warnings()` to see the 11 remaining warnings.

Finally, we move to replies.

# Replies
youtube <- youtube %>%
  mutate(comments = str_remove(comments, " replies")) %>%
  mutate(comments = str_remove(comments, " reply")) %>%
  mutate(comments = ifelse(comments == "No", 0,
                        as.numeric(comments)))
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `comments = ifelse(comments == "No", 0, as.numeric(comments))`.
Caused by warning in `ifelse()`:
! NAs introduced by coercion

Tests and Summaries

First of all, I want to see the summaries of Likes and Comments.

# Here are the summaries of both columns.
summary(youtube$likes) 
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
    0.00     1.00     4.00   336.61    34.75 11000.00 
# On average you would have 4 likes! Not bad.
summary(youtube$comments)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.000   0.000   0.000   1.944   1.000  40.000 
# On average, you would have 0 replies on a comment as a median, but more likely closer to 1.

Lets look at the distribution of both Likes and Comments respectively;

youtube %>%
  ggplot(aes(x = likes)) +
    geom_histogram() + 
    labs(x = "Likes", y = "Count", title = "Distribution of Likes in a YouTube comment section")
`stat_bin()` using `bins = 30`. Pick better value `binwidth`.

youtube %>%
  ggplot(aes(x = comments)) +
  geom_histogram() + 
  labs(x = "Likes", y = "Count", title = "Distribution of Comments in a YouTube comment section")
`stat_bin()` using `bins = 30`. Pick better value `binwidth`.

That’s cool. But what traits make a comment special for people: I want to know if the length of a comment contributes to how many likes a comment gets. I will log10 Likes because it is skewed heavily to the right.

youtube$body_length <- nchar(youtube$body)
youtube %>%
  ggplot(aes(x = log10(likes), y = body_length)) +
    geom_point() +
    geom_smooth() +
    labs(x = "Log10(Likes)", y = "Comment Body Length", title = "Relationship between Likes and Comment Body Length")
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Warning: Removed 31 rows containing non-finite outside the scale range
(`stat_smooth()`).

From the graph, it looks like there is not much of a relationship between it. This might be due to the variability of each comment. A very long, heartfelt comment could get lots of likes, but a very short, very funny comment could also get a lot of likes as well.

Let us try to see if there is a relationship between Likes and Days since the post. Since both are skewed heavily to the right, I will log10 both of them.

youtube %>%
  ggplot(aes(x = log10(time), y = log10(likes))) +
    geom_point() +
    geom_smooth() +
    labs(x = "Time (in Log10(Days)",
         y = "Log10(Likes)",
         title = "Relationship between Time and Likes")
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Warning: Removed 31 rows containing non-finite outside the scale range
(`stat_smooth()`).

There does seem to be a positive relationship between days and likes. and even just looking at the graph, we see that average number of likes should increase by time. Though since we took the log10 of them, it will probably have a smaller, log10 relationship as shown through here.

Lastly, I want to know if there is a relationship between replies and the number of likes a comment has. Again, I will log10 the likes.

youtube %>%
  ggplot(aes(x = log10(likes), y = comments)) +
    geom_point() +
    geom_smooth() +
    labs(x = "Log10(Likes)", y = "Replies", title = "Relationship between Likes and Replies")
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Warning: Removed 31 rows containing non-finite outside the scale range
(`stat_smooth()`).

There does seem to be a positive relationship, but the fact that there are so many comments without any replies with very few high-end comments with replies that it becomes hard to accept just by looking at the data.

summary(lm(youtube$likes ~ youtube$comments))

Call:
lm(formula = youtube$likes ~ youtube$comments)

Residuals:
    Min      1Q  Median      3Q     Max 
-4436.8  -204.0   -98.7   -80.4  8355.9 

Coefficients:
                 Estimate Std. Error t value Pr(>|t|)    
(Intercept)        101.15      89.06   1.136    0.258    
youtube$comments   121.09      14.24   8.504 7.29e-15 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1136 on 178 degrees of freedom
Multiple R-squared:  0.2889,    Adjusted R-squared:  0.2849 
F-statistic: 72.31 on 1 and 178 DF,  p-value: 7.286e-15

Our adjusted r-squared value is a really low number for us to fully go forward with there being at least a linear relationship between likes and comments.

Conclusion

Given this is also only one video, it might not be very representative of the whole population. But this gives us a greater understanding that there are so many factors that go into making a YouTube comment, as well as if the comment can do well.