Wikipedia Views Assignment

install.packages("pageviews_0.5.0.tar", repos = NULL, type = "source")
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.0
✔ 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(pageviews)        # This package gets data on Wikipedia viewing
library(DT)               # DT stands for datatable, and creates interactive tables
library(infer)            # for some stats like t_test
library(devtools)
Loading required package: usethis
Mass_shootings <- pageviews::article_pageviews(article = "Mass shootings in the United States", start = as.Date("2017-1-1"), end = as.Date("2023-12-31"))

Mass_shootings |> 
  ggplot(aes(x = date, y = views)) +
  geom_line()

This graph shows the spikes in viewing activity over time for the article “Mass shootings in the United States”.

Gun_control <- pageviews::article_pageviews(article = "Gun control", start = as.Date("2017-1-1"), end = as.Date("2023-12-31"))

Gun_control |> 
  ggplot(aes(x = date, y = views)) +
  geom_line()

This graph shows the spikes in viewing activity over time for the article “Gun control”.

shootings <- bind_rows(Gun_control, Mass_shootings)

shootings |> 
  ggplot(aes(color = article, x = date, y = views)) +
  geom_line()

This graph compares the spikes in viewing activity between the two articles “Gun control” and “Mass shootings in the United States” over time. The mass shooting article had more views overall in comparison to “Gun control”.

top_maine <- top_articles(start = as.Date("2023-10-26"))

top_texas <- top_articles(start = as.Date("2022-5-25"))

top_maine |> 
  select(article, views) |>
  filter(!article == "Main_Page", !article == "Special:Search") |>
  datatable()
top_maine |> 
  select(article, views) |>
  filter(!article == "Main_Page", !article == "Special:Search") |>
  top_n(10, views) |>
  ggplot(aes(x = fct_rev(as_factor(article)), y = views)) +
  geom_col(fill = "blue") +
  coord_flip() +
  scale_y_continuous (labels = scales::comma)

This graph shows the most viewd articles the day after the mass shooting in Lewiston, ME. The articles related to this event, “2023 Lewiston shootings” and “Lewiston, Maine”, were the third and fourth most viewed articles, respectively, the day after the shooting.

top_texas |> 
  select(article, views) |>
  filter(!article == "Main_Page", !article == "Special:Search") |>
  datatable()
top_texas |> 
  select(article, views) |>
  filter(!article == "Main_Page", !article == "Special:Search") |>
  top_n(10, views) |>
  ggplot(aes(x = fct_rev(as_factor(article)), y = views)) +
  geom_col(fill = "red") +
  coord_flip() +
  scale_y_continuous (labels = scales::comma)

This graph shows the most viewed articles the day after the Ulvade, TX mass shooting. The articles related to that event, “Robb Elementary School shooting” and “Ulvade, Texas”, were the fourth and and sixth most viewed articles the day after the mass shooting. It is also interesting to note that articles about Sandy Hook and a list of school shootings were also popular the day after the Ulvade mass shooting.

maine <- article_pageviews(article = "Gun_control",
                           start = as.Date("2023-10-18"),
                           end = as.Date("2023-11-1"))

texas <- article_pageviews(article = "Gun_control",
                           start = as.Date("2022-5-17"),
                           end = as.Date("2022-5-31"))

maine <- maine |> 
  mutate(day = -7:7) |> 
  mutate(event = "Maine")

texas <- texas |> 
  mutate(day = -7:7) |> 
  mutate(event = "Texas")
maine |> 
  ggplot(aes(x = day, y = views)) +
  geom_line()

This graph shows the viewing activity of the article “Gun control” seven days before and after the mass shooting in Lewiston, ME on 10/25/23. There was a spike in activity on day after the mass shooting occured.

texas |> 
  ggplot(aes(x = day, y = views)) +
  geom_line()

This graph shows the viewing activity of the article “Gun control” seven days before and after the mass school shooting in Ulvade, TX on 5/24/22. There was a spike in activity the day after the school shooting occured.

shootings2 <- bind_rows(maine, texas)

shootings2 |> 
  ggplot(aes(x = day, y = views, color = event)) +
  geom_line() +
  theme_minimal() +
  labs(x = "Days before/after Shooting", 
       y = "Wikipedia Views", 
       color = "Event", 
       title = "Views of the Wikipedia Gun Control Article before and after Two Mass Shootings")

This graph compares the viewing activity of the article “Gun control” seven days before and after the mass shootings in Lewiston, ME and Ulvade, TX. While both events sparked a spike in activity, the Texas shooting was more poular and had many more views following the mass shooting event.

shootings2 |> 
  mutate(after_event = (day > 0))
     project language     article     access      agent granularity       date
1  wikipedia       en Gun_control all-access all-agents       daily 2023-10-18
2  wikipedia       en Gun_control all-access all-agents       daily 2023-10-19
3  wikipedia       en Gun_control all-access all-agents       daily 2023-10-20
4  wikipedia       en Gun_control all-access all-agents       daily 2023-10-21
5  wikipedia       en Gun_control all-access all-agents       daily 2023-10-22
6  wikipedia       en Gun_control all-access all-agents       daily 2023-10-23
7  wikipedia       en Gun_control all-access all-agents       daily 2023-10-24
8  wikipedia       en Gun_control all-access all-agents       daily 2023-10-25
9  wikipedia       en Gun_control all-access all-agents       daily 2023-10-26
10 wikipedia       en Gun_control all-access all-agents       daily 2023-10-27
11 wikipedia       en Gun_control all-access all-agents       daily 2023-10-28
12 wikipedia       en Gun_control all-access all-agents       daily 2023-10-29
13 wikipedia       en Gun_control all-access all-agents       daily 2023-10-30
14 wikipedia       en Gun_control all-access all-agents       daily 2023-10-31
15 wikipedia       en Gun_control all-access all-agents       daily 2023-11-01
16 wikipedia       en Gun_control all-access all-agents       daily 2022-05-17
17 wikipedia       en Gun_control all-access all-agents       daily 2022-05-18
18 wikipedia       en Gun_control all-access all-agents       daily 2022-05-19
19 wikipedia       en Gun_control all-access all-agents       daily 2022-05-20
20 wikipedia       en Gun_control all-access all-agents       daily 2022-05-21
21 wikipedia       en Gun_control all-access all-agents       daily 2022-05-22
22 wikipedia       en Gun_control all-access all-agents       daily 2022-05-23
23 wikipedia       en Gun_control all-access all-agents       daily 2022-05-24
24 wikipedia       en Gun_control all-access all-agents       daily 2022-05-25
25 wikipedia       en Gun_control all-access all-agents       daily 2022-05-26
26 wikipedia       en Gun_control all-access all-agents       daily 2022-05-27
27 wikipedia       en Gun_control all-access all-agents       daily 2022-05-28
28 wikipedia       en Gun_control all-access all-agents       daily 2022-05-29
29 wikipedia       en Gun_control all-access all-agents       daily 2022-05-30
30 wikipedia       en Gun_control all-access all-agents       daily 2022-05-31
   views day event after_event
1    350  -7 Maine       FALSE
2    357  -6 Maine       FALSE
3    358  -5 Maine       FALSE
4    333  -4 Maine       FALSE
5    351  -3 Maine       FALSE
6    417  -2 Maine       FALSE
7    492  -1 Maine       FALSE
8    548   0 Maine       FALSE
9    778   1 Maine        TRUE
10   651   2 Maine        TRUE
11   484   3 Maine        TRUE
12   553   4 Maine        TRUE
13   620   5 Maine        TRUE
14   524   6 Maine        TRUE
15   559   7 Maine        TRUE
16   581  -7 Texas       FALSE
17   553  -6 Texas       FALSE
18   491  -5 Texas       FALSE
19   457  -4 Texas       FALSE
20   360  -3 Texas       FALSE
21   365  -2 Texas       FALSE
22   623  -1 Texas       FALSE
23  1177   0 Texas       FALSE
24  9666   1 Texas        TRUE
25  5587   2 Texas        TRUE
26  2977   3 Texas        TRUE
27  1998   4 Texas        TRUE
28  1774   5 Texas        TRUE
29  1638   6 Texas        TRUE
30  1720   7 Texas        TRUE
shootings2 |> 
  mutate(after_event = (day > 0)) |> 
  t_test(views ~ after_event)
Warning: The statistic is based on a difference or ratio; by default, for
difference-based statistics, the explanatory variable is subtracted in the
order "TRUE" - "FALSE", or divided in the order "TRUE" / "FALSE" for
ratio-based statistics. To specify this order yourself, supply `order =
c("TRUE", "FALSE")`.
# A tibble: 1 × 7
  statistic  t_df p_value alternative estimate lower_ci upper_ci
      <dbl> <dbl>   <dbl> <chr>          <dbl>    <dbl>    <dbl>
1      2.34  13.1  0.0354 two.sided      1621.     129.    3113.
shootings2 |> 
  mutate(after_event = (day > 0)) |> 
  group_by(after_event) |> 
  summarize(Mean = mean(views),
            StdDev = sd(views),
            N = n())
# A tibble: 2 × 4
  after_event  Mean StdDev     N
  <lgl>       <dbl>  <dbl> <int>
1 FALSE        488.   207.    16
2 TRUE        2109.  2580.    14

The average number of views of the Wikipedia Gun Control article in the 7 days prior to the two shootings in Ulvade, TX and Lewiston, ME (M = 488.3, SD = 207.02) was statistically significantly different from the average number of views in the 7 days after the shooting (M = 2109.2, SD = 2580.20), t(13.15) = 2.34, p = .035.