Wiki API

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
gun_control <- article_pageviews(article = "Gun control", start = as.Date("2017-1-1"), end = as.Date("2023-12-31"))

glimpse(gun_control)
Rows: 2,556
Columns: 8
$ project     <chr> "wikipedia", "wikipedia", "wikipedia", "wikipedia", "wikip…
$ language    <chr> "en", "en", "en", "en", "en", "en", "en", "en", "en", "en"…
$ article     <chr> "Gun_control", "Gun_control", "Gun_control", "Gun_control"…
$ access      <chr> "all-access", "all-access", "all-access", "all-access", "a…
$ agent       <chr> "all-agents", "all-agents", "all-agents", "all-agents", "a…
$ granularity <chr> "daily", "daily", "daily", "daily", "daily", "daily", "dai…
$ date        <dttm> 2017-01-01, 2017-01-02, 2017-01-03, 2017-01-04, 2017-01-0…
$ views       <dbl> 285, 393, 434, 544, 635, 620, 431, 464, 610, 595, 686, 688…

This program shows the number of views of the “gun control” article on Wikipedia during the stated years.

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

This graph visually shows the views of the Gun Control article on Wikipedia.

gun_control |> 
  slice_max(views, n = 10)
     project language     article     access      agent granularity       date
1  wikipedia       en Gun_control all-access all-agents       daily 2022-05-25
2  wikipedia       en Gun_control all-access all-agents       daily 2018-02-16
3  wikipedia       en Gun_control all-access all-agents       daily 2018-02-15
4  wikipedia       en Gun_control all-access all-agents       daily 2022-05-26
5  wikipedia       en Gun_control all-access all-agents       daily 2018-02-22
6  wikipedia       en Gun_control all-access all-agents       daily 2017-10-03
7  wikipedia       en Gun_control all-access all-agents       daily 2018-02-20
8  wikipedia       en Gun_control all-access all-agents       daily 2018-02-19
9  wikipedia       en Gun_control all-access all-agents       daily 2018-02-21
10 wikipedia       en Gun_control all-access all-agents       daily 2018-02-18
   views
1   9666
2   6549
3   6117
4   5587
5   5142
6   4342
7   4233
8   4229
9   3914
10  3760

This chart shows the top 10 views of the Gun control article on Wikipedia over these years.

Violence <- article_pageviews(article = "Gun violence in the United States", start = as.Date("2017-1-1"), end = as.Date("2023-12-31"))
Violence |> 
  ggplot(aes(x = date, y = views)) +
  geom_line()

This graph visually shows the views of the Gun Violence article on Wikipedia.

guns <- bind_rows(gun_control, Violence)
guns |> 
  ggplot(aes(x = date, y = views, color = article)) +
  geom_line()

This graph visibly shows the views of each Wiki article overlapped.

cor.test(Violence$views, gun_control$views)

    Pearson's product-moment correlation

data:  Violence$views and gun_control$views
t = 76.949, df = 2554, p-value < 2.2e-16
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 0.8237752 0.8471673
sample estimates:
      cor 
0.8358503 

This test shows a statistically significant correlation with views of these two articles; when they view one, they likely also view the other.

guns |> 
  pivot_wider(names_from = article, values_from = views) |> 
  ggplot(aes(x = Gun_violence_in_the_United_States, y = Gun_control)) +          # create scatterplot
  geom_point() +
  geom_smooth(method = lm) +                                  # create regression line
  labs(x = "Views of the Wikipedia Gun violence in the US article", 
       y = "Views of the Wikipedia Gun control article", 
       title = "Relationship between Wikipedia article views")
`geom_smooth()` using formula = 'y ~ x'

This scatter plot shows the correlation of views of the gun violence article and the gun control article.

top <- top_articles(start = as.Date("2018-02-15"))
top |> 
  select(article, views) |>
  filter(!article == "Main_Page", !article == "Special:Search") |> 
  slice_max(views, n = 10) |> 
  datatable(class = 'cell-border stripe') |> 
  formatStyle("article", backgroundColor = "lightblue")

These were the top 10 searched articles the day after the Feb 14th, 2018 Parkland High School Mass Shooting.

top |> 
  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 = "lightblue") +
  coord_flip() +
  scale_y_continuous(labels = scales::comma) +
  labs(y = "Number of Views",
       x = "Article",
       title = "Top Wikipedia Articles, Feb. 15, 2018")

This bar graph gives a visual representation of the top 10 searched articles the day after the Parkland High School Mass Shooting

topM <- top_articles(start = as.Date("2023-02-15"))
topM |> 
  select(article, views) |>
  filter(!article == "Main_Page", !article == "Special:Search") |> 
  slice_max(views, n = 10) |> 
  datatable(class = 'cell-border stripe') |> 
  formatStyle("article", backgroundColor = "lightpink")

This chart shows the top searched Wikipedia articles the day after the Michigan State University Mass Shooting on Feb 14th, 2023.

topM |> 
  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 = "lightpink") +
  coord_flip() +
  scale_y_continuous(labels = scales::comma) +
  labs(y = "Number of Views",
       x = "Article",
       title = "Top Wikipedia Articles, Feb. 15, 2023")

This bar graph gives a visual representation of the top 10 searched articles the day after the Michigan State University Mass Shooting.

Parkland <- article_pageviews(article = "Gun_control",
                           start = as.Date("2018-2-7"),
                           end = as.Date("2018-2-21"))
Parkland2 <- Parkland |> 
  mutate(day = -7:7) |> 
  mutate(event = "Parkland")
Parkland2 |>
  ggplot(aes(x = day, y = views))+
  geom_line()

This graph shows the spike in gun control article views 7 days before and after the Parkland mass shooting in Florida.

MichiganST <- article_pageviews(article = "Gun_control",
                           start = as.Date("2023-2-7"),
                           end = as.Date("2023-2-21"))

MichiganST <- MichiganST |> 
  mutate(day = -7:7) |> 
  mutate(event = "Michigan")

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

This graph shows the spike in gun control article views 7 days before and after the Michigan State University Shooting.

Shootings <- bind_rows(Parkland2, MichiganST)

Shootings |> 
  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 shows the views of the Wiki Gun Control article the week before and after each of these Feb 14th mass shooting events on different years.

Shootings |> 
  mutate(after_event = (day > 0))
     project language     article     access      agent granularity       date
1  wikipedia       en Gun_control all-access all-agents       daily 2018-02-07
2  wikipedia       en Gun_control all-access all-agents       daily 2018-02-08
3  wikipedia       en Gun_control all-access all-agents       daily 2018-02-09
4  wikipedia       en Gun_control all-access all-agents       daily 2018-02-10
5  wikipedia       en Gun_control all-access all-agents       daily 2018-02-11
6  wikipedia       en Gun_control all-access all-agents       daily 2018-02-12
7  wikipedia       en Gun_control all-access all-agents       daily 2018-02-13
8  wikipedia       en Gun_control all-access all-agents       daily 2018-02-14
9  wikipedia       en Gun_control all-access all-agents       daily 2018-02-15
10 wikipedia       en Gun_control all-access all-agents       daily 2018-02-16
11 wikipedia       en Gun_control all-access all-agents       daily 2018-02-17
12 wikipedia       en Gun_control all-access all-agents       daily 2018-02-18
13 wikipedia       en Gun_control all-access all-agents       daily 2018-02-19
14 wikipedia       en Gun_control all-access all-agents       daily 2018-02-20
15 wikipedia       en Gun_control all-access all-agents       daily 2018-02-21
16 wikipedia       en Gun_control all-access all-agents       daily 2023-02-07
17 wikipedia       en Gun_control all-access all-agents       daily 2023-02-08
18 wikipedia       en Gun_control all-access all-agents       daily 2023-02-09
19 wikipedia       en Gun_control all-access all-agents       daily 2023-02-10
20 wikipedia       en Gun_control all-access all-agents       daily 2023-02-11
21 wikipedia       en Gun_control all-access all-agents       daily 2023-02-12
22 wikipedia       en Gun_control all-access all-agents       daily 2023-02-13
23 wikipedia       en Gun_control all-access all-agents       daily 2023-02-14
24 wikipedia       en Gun_control all-access all-agents       daily 2023-02-15
25 wikipedia       en Gun_control all-access all-agents       daily 2023-02-16
26 wikipedia       en Gun_control all-access all-agents       daily 2023-02-17
27 wikipedia       en Gun_control all-access all-agents       daily 2023-02-18
28 wikipedia       en Gun_control all-access all-agents       daily 2023-02-19
29 wikipedia       en Gun_control all-access all-agents       daily 2023-02-20
30 wikipedia       en Gun_control all-access all-agents       daily 2023-02-21
   views day    event after_event
1    599  -7 Parkland       FALSE
2    678  -6 Parkland       FALSE
3    519  -5 Parkland       FALSE
4    240  -4 Parkland       FALSE
5    319  -3 Parkland       FALSE
6    688  -2 Parkland       FALSE
7    707  -1 Parkland       FALSE
8    910   0 Parkland       FALSE
9   6117   1 Parkland        TRUE
10  6549   2 Parkland        TRUE
11  3612   3 Parkland        TRUE
12  3760   4 Parkland        TRUE
13  4229   5 Parkland        TRUE
14  4233   6 Parkland        TRUE
15  3914   7 Parkland        TRUE
16   529  -7 Michigan       FALSE
17   504  -6 Michigan       FALSE
18   494  -5 Michigan       FALSE
19   516  -4 Michigan       FALSE
20   404  -3 Michigan       FALSE
21   364  -2 Michigan       FALSE
22   478  -1 Michigan       FALSE
23   660   0 Michigan       FALSE
24   651   1 Michigan        TRUE
25   590   2 Michigan        TRUE
26   532   3 Michigan        TRUE
27   419   4 Michigan        TRUE
28   440   5 Michigan        TRUE
29   507   6 Michigan        TRUE
30   597   7 Michigan        TRUE
Shootings |> 
  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      3.35  13.1 0.00513 two.sided      2044.     728.    3360.
Shootings |> 
  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        538.   167.    16
2 TRUE        2582.  2275.    14

This T-Test shows that the average number of views of the Wikipedia Gun Control article in the 7 days prior to these two shooting events (M = 538.06, SD = 167.38) was not statistically significantly different from the average number of views in the 7 days after the shooting (M = 2582.14, SD = 2275.39), t(13.1) = 3.35, p = 0.005.

Naomi Nation, GR Stats Wikipedia API Assignment