Wikipedia API

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.4
✔ 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
  1. Below you will find the process and overlay of two graphs titled Gun_control and Mass_shootings_in_the_United_States. There was a notable difference of views between both.
gun_control <- article_pageviews(article = "Gun control", start = as.Date("2017-1-1"), end = as.Date("2023-12-31"))
Mass_Shootings <- article_pageviews(article = "Mass shootings in the United States" , start = as.Date("2017-1-1"), end = as.Date("2023-12-31"))
guns <- bind_rows(gun_control, Mass_Shootings) 
guns %>% 
  ggplot(aes(x = date, y = views, color = article)) +
  geom_line()

  1. Below you will find a scatterplot of the two articles and their relationship regarding the Wikipedia article view.
guns_pivot <- guns %>%
  pivot_wider(names_from = article, values_from = views)
ggplot(guns_pivot, aes(x = `Gun_control`, y = `Mass_shootings_in_the_United_States`)) +
  geom_point() +
  geom_smooth(method = lm) +  # add a regression line if needed
  labs(x = "Views of the Wikipedia Gun control article",
       y = "Views of the Wikipedia Mass shootings in the United States article",
       title = "Relationship between Wikipedia article views")
`geom_smooth()` using formula = 'y ~ x'
Warning: Removed 2 rows containing non-finite values (`stat_smooth()`).
Warning: Removed 2 rows containing missing values (`geom_point()`).

  1. The Las Vegas shooting in 2017 made headlines. Below is a table and bar graph indicating the amount of views this article received the day after the shooting occurred. Other articles were assessed in comparison.

Vegas:

popular <- top_articles(start = as.Date("2017-10-2"))
popular |> 
  select(article, views) |>
  filter(!article == "Main_Page", !article == "Special:Search") |> 
  slice_max(views, n = 10) |> 
  datatable()
popular |> 
  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)

The Orlando Night Club shooting occurred on June 6th, 2016. Below is a table and bar graph indicating the number of views this article received the following day. Other articles have been assessed for comparison.

top <- top_articles(start = as.Date("2016-6-13"))
top |> 
  select(article, views) |>
  filter(!article == "Main_Page", !article == "Special:Search") |> 
  slice_max(views, n = 10) |> 
  datatable()
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 = "lightgreen")+
  coord_flip()+
scale_y_continuous(labels = scales::comma)

  1. Below is the process and code used to combine the data from the Orlando and Vegas shooting dates and the “Gun Control” article over a span of 14 days (7 days prior, 7 days after). The amount of views of the Gun Control article increased after the shootings occurred. This can be seen in the graph below.
Orlando <- article_pageviews(article = "Gun_control",
                           start = as.Date("2016-6-5"),
                           end = as.Date("2016-6-19"))
Orlando <- Orlando |> 
  mutate(day = -7:7) |> 
  mutate(event = "Orlando")
Orlando |> 
  ggplot(aes(x = day, y = views)) +
  geom_line()

Vegas <- article_pageviews(article = "Gun_control",
                           start = as.Date("2017-9-24"),
                           end = as.Date("2017-10-8"))
Vegas <- Vegas |> 
  mutate(day = -7:7) |> 
  mutate(event = "Vegas")
Vegas |> 
  ggplot(aes(x = day, y = views)) +
  geom_line()

shootings <- bind_rows(Orlando, Vegas)

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")

  1. The t-test below indicates that there was a significant increase in views of the article “Gun Control” the day after the shootings occurred.
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      5.96  16.0 0.0000199 two.sided      1894.    1221.    2568.
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        532.   411.    16
2 TRUE        2426.  1125.    14

The average number of views of the Wikipedia Gun Control article in the 7 days prior to the two shootings (M = 531.8, SD = 410.6) was statistically significantly different from the average number of views in the 7 days after the shooting (M = 2426.1, SD = 1125.4), t(16) = 5.96, p = 1.99e-05.