Wikipedia API

library(devtools)
Warning: package 'devtools' was built under R version 4.3.2
Loading required package: usethis
Warning: package 'usethis' was built under R version 4.3.2
devtools::install_github("ironholds/pageviews")
Skipping install of 'pageviews' from a github remote, the SHA1 (d32c629c) has not changed since last install.
  Use `force = TRUE` to force installation
library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.3.2
Warning: package 'lubridate' was built under R version 4.3.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.2     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.2     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.0
✔ purrr     1.0.1     
── 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
Warning: package 'DT' was built under R version 4.3.2
library(infer)            # for some stats like t_test
Warning: package 'infer' was built under R version 4.3.2
library(devtools)

This is stats stuff, telling me what libraries to use.

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 is me telling the program to look at the number of views of the “gun control” article on Wikipedia from the last few years.

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

This is those views charted by frequency.

violence <- article_pageviews(article = "Gun violence in the United States", start = as.Date("2017-1-1"), end = as.Date("2023-12-31"))

glimpse(violence)
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_violence_in_the_United_States", "Gun_violence_in_the_…
$ 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> 877, 1033, 1173, 1186, 1298, 2281, 2275, 1491, 1503, 2460,…

This is me telling the program to look at the number of views of the “gun violence in the United States” article on Wikipedia from the last few years.

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

This is those views charted by frequency.

guns <- bind_rows(gun_control, violence) 

This is me turning the gun control article and the gun violence article into one term, “guns”.

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

This is a graph of views of the “gun control” article (red) against the “gun violence in the United States” article (teal).

guns |> 
  pivot_wider(names_from = article, values_from = views)
# A tibble: 2,556 × 8
   project   language access   agent granularity date                Gun_control
   <chr>     <chr>    <chr>    <chr> <chr>       <dttm>                    <dbl>
 1 wikipedia en       all-acc… all-… daily       2017-01-01 00:00:00         285
 2 wikipedia en       all-acc… all-… daily       2017-01-02 00:00:00         393
 3 wikipedia en       all-acc… all-… daily       2017-01-03 00:00:00         434
 4 wikipedia en       all-acc… all-… daily       2017-01-04 00:00:00         544
 5 wikipedia en       all-acc… all-… daily       2017-01-05 00:00:00         635
 6 wikipedia en       all-acc… all-… daily       2017-01-06 00:00:00         620
 7 wikipedia en       all-acc… all-… daily       2017-01-07 00:00:00         431
 8 wikipedia en       all-acc… all-… daily       2017-01-08 00:00:00         464
 9 wikipedia en       all-acc… all-… daily       2017-01-09 00:00:00         610
10 wikipedia en       all-acc… all-… daily       2017-01-10 00:00:00         595
# ℹ 2,546 more rows
# ℹ 1 more variable: Gun_violence_in_the_United_States <dbl>

This is the combined “guns” pageviews every day for the past few years.

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 is a scatterplot showing the correlation of views of the violence (bottom) and control (side) articles.

top <- top_articles(start = as.Date("2023-07-03"))
top |> 
  select(article, views) |>
  filter(!article == "Main_Page", !article == "Special:Search") |> 
  slice_max(views, n = 10) |> 
  datatable()

This is showing me what the top 10 most searched Wikipedia articles were on the day after the Baltimore shooting, which was July 2nd, 2023.

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 = "purple") +
  coord_flip() +
  scale_y_continuous(labels = scales::comma) +
  labs(y = "Number of Views", x = "Article", title = "Top Wikipedia articles, Jul.3, 2023")

This purple graph is a visual representation of the same information.

top <- top_articles(start = as.Date("2023-10-30"))
top |> 
  select(article, views) |>
  filter(!article == "Main_Page", !article == "Special:Search") |> 
  slice_max(views, n = 10) |> 
  datatable()

This is showing me what the top 10 most searched Wikipedia articles were on the day after the Ybor City (Tampa) shooting, which was October 29th, 2023.

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 = "pink") +
  coord_flip() +
  scale_y_continuous(labels = scales::comma) +
  labs(y = "Number of Views", x = "Article", title = "Top Wikipedia articles, Oct. 30, 2023")

This pink graph is a visual representation of the same information.

ybor <- article_pageviews(article = "Gun_control",
                           start = as.Date("2023-10-22"),
                           end = as.Date("2023-11-5"))

ybor <- ybor |> 
  mutate(day = -7:7) |> 
  mutate(event = "Tampa")

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

This is a graph of views of the Gun Control article for seven days before and after the Ybor City shooting.

mary <- article_pageviews(article = "Gun_control",
                           start = as.Date("2023-6-25"),
                           end = as.Date("2023-7-9"))

mary <- mary |> 
  mutate(day = -7:7) |> 
  mutate(event = "Baltimore")

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

This is a graph of views of the Gun Control article for seven days before and after the Ybor City shooting.

shootings <- bind_rows(ybor, mary)

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 is a graph of views of the Gun Control article for seven days before and after the Ybor City shooting (teal) and the Baltimore shooting (red).

shootings |> 
  mutate(after_event = (day > 0)) 
     project language     article     access      agent granularity       date
1  wikipedia       en Gun_control all-access all-agents       daily 2023-10-22
2  wikipedia       en Gun_control all-access all-agents       daily 2023-10-23
3  wikipedia       en Gun_control all-access all-agents       daily 2023-10-24
4  wikipedia       en Gun_control all-access all-agents       daily 2023-10-25
5  wikipedia       en Gun_control all-access all-agents       daily 2023-10-26
6  wikipedia       en Gun_control all-access all-agents       daily 2023-10-27
7  wikipedia       en Gun_control all-access all-agents       daily 2023-10-28
8  wikipedia       en Gun_control all-access all-agents       daily 2023-10-29
9  wikipedia       en Gun_control all-access all-agents       daily 2023-10-30
10 wikipedia       en Gun_control all-access all-agents       daily 2023-10-31
11 wikipedia       en Gun_control all-access all-agents       daily 2023-11-01
12 wikipedia       en Gun_control all-access all-agents       daily 2023-11-02
13 wikipedia       en Gun_control all-access all-agents       daily 2023-11-03
14 wikipedia       en Gun_control all-access all-agents       daily 2023-11-04
15 wikipedia       en Gun_control all-access all-agents       daily 2023-11-05
16 wikipedia       en Gun_control all-access all-agents       daily 2023-06-25
17 wikipedia       en Gun_control all-access all-agents       daily 2023-06-26
18 wikipedia       en Gun_control all-access all-agents       daily 2023-06-27
19 wikipedia       en Gun_control all-access all-agents       daily 2023-06-28
20 wikipedia       en Gun_control all-access all-agents       daily 2023-06-29
21 wikipedia       en Gun_control all-access all-agents       daily 2023-06-30
22 wikipedia       en Gun_control all-access all-agents       daily 2023-07-01
23 wikipedia       en Gun_control all-access all-agents       daily 2023-07-02
24 wikipedia       en Gun_control all-access all-agents       daily 2023-07-03
25 wikipedia       en Gun_control all-access all-agents       daily 2023-07-04
26 wikipedia       en Gun_control all-access all-agents       daily 2023-07-05
27 wikipedia       en Gun_control all-access all-agents       daily 2023-07-06
28 wikipedia       en Gun_control all-access all-agents       daily 2023-07-07
29 wikipedia       en Gun_control all-access all-agents       daily 2023-07-08
30 wikipedia       en Gun_control all-access all-agents       daily 2023-07-09
   views day     event after_event
1    351  -7     Tampa       FALSE
2    417  -6     Tampa       FALSE
3    492  -5     Tampa       FALSE
4    548  -4     Tampa       FALSE
5    778  -3     Tampa       FALSE
6    651  -2     Tampa       FALSE
7    484  -1     Tampa       FALSE
8    553   0     Tampa       FALSE
9    620   1     Tampa        TRUE
10   524   2     Tampa        TRUE
11   559   3     Tampa        TRUE
12   435   4     Tampa        TRUE
13   372   5     Tampa        TRUE
14   366   6     Tampa        TRUE
15   381   7     Tampa        TRUE
16   315  -7 Baltimore       FALSE
17   364  -6 Baltimore       FALSE
18   318  -5 Baltimore       FALSE
19   331  -4 Baltimore       FALSE
20   371  -3 Baltimore       FALSE
21   322  -2 Baltimore       FALSE
22   343  -1 Baltimore       FALSE
23   335   0 Baltimore       FALSE
24   336   1 Baltimore        TRUE
25   373   2 Baltimore        TRUE
26   308   3 Baltimore        TRUE
27   360   4 Baltimore        TRUE
28   330   5 Baltimore        TRUE
29   304   6 Baltimore        TRUE
30   293   7 Baltimore        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    -0.883  27.3   0.385 two.sided      -38.6    -128.     51.1
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        436.   137.    16
2 TRUE         397.   101.    14

This t-test showed that the average number of views of the Wikipedia Gun Control article in the seven days prior to the two shootings (M = 435.81, SD = 137.43) was not statistically significantly different from the average number of views in the seven days after the shooting (M = 397.21, SD = 101.12), t(27.26) = -0.88, p = .38.