wiki

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 <- article_pageviews(article = "Mass shootings in the United States", start = as.Date("2017-01-01"), end = as.Date("2023-12-31"))

glimpse(mass_shootings)
Rows: 2,554
Columns: 8
$ project     <chr> "wikipedia", "wikipedia", "wikipedia", "wikipedia", "wikip…
$ language    <chr> "en", "en", "en", "en", "en", "en", "en", "en", "en", "en"…
$ article     <chr> "Mass_shootings_in_the_United_States", "Mass_shootings_in_…
$ 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> 13, 9, 7, 13, 21, 54, 49, 32, 16, 19, 24, 14, 9, 11, 6, 21…
mass_shootings |> 
  ggplot(aes(x = date, y = views)) +
  geom_line()

gun_control <- article_pageviews(article = "Gun control", start = as.Date("2017-01-01"), 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…
gun_control |> 
  ggplot(aes(x = date, y = views)) +
  geom_line()

mass <- bind_rows(gun_control, mass_shootings) 

1.

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

  1. Here you can see that the mass shootings article received a lot more views than the gun control article but that the views on each seem to increase at the same time.
mass |> 
  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: Mass_shootings_in_the_United_States <dbl>

2.

mass |> 
  pivot_wider(names_from = article, values_from = views)|>
  ggplot(aes(x = Mass_shootings_in_the_United_States, y = Gun_control))+
  geom_point()+
   geom_smooth(method = lm) +                                  # create regression line
  labs(x = "Views of the Wikipedia Mass shootings in the US article", 
       y = "Views of the Wikipedia Gun control 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. Here a scatter plot confirms my guess that the views on the articles are correlated.
Q2 <- top_articles(start = as.Date("2017-10-03"))
Q2 %>% 
  select(article, views) |>
  filter(!article == "Main_Page", !article == "Special:Search") %>% 
  datatable(class= "cell-border stripe")
Q2 %>% 
   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)+
  labs(y = "Number of Views", x = "Article", title = "Top Wikipedia articles, Oct. 3, 2017")

  1. It looks like the shooting in Las Vegas that occured Oct. 2nd of 2017 was the 5th most popular article the day after.
Q3 <- top_articles(start = as.Date("2023-10-26"))
Q3 %>% 
  select(article, views) |>
  filter(!article == "Main_Page", !article == "Special:Search") %>% 
  datatable(class= "cell-border stripe")
Q3 %>% 
     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)+
  labs(y = "Number of Views", x = "Article", title = "Top Wikipedia articles, Oct. 26th, 2023")

3 cont’d. It looks like the day after the shooting in Lewstown, Maine on October 25th 2023, the article on the shootings was 3rd most viewed.

lasvegas <- article_pageviews(article = "Gun_control",
                           start = as.Date("2017-9-26"),
                           end = as.Date("2017-10-10"))
lasvegas <- lasvegas %>% 
  mutate(day = -7:7) %>% 
  mutate(event = "Las vegas")

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

Lewistown <- article_pageviews(article = "Gun_control",
                           start = as.Date("2023-10-18"),
                           end = as.Date("2023-11-1"))
Lewistown <- Lewistown %>% 
  mutate(day = -7:7) %>% 
  mutate(event = "Lewistown")

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

shootings <- bind_rows(lasvegas, Lewistown)

4.

shootings <- bind_rows(lasvegas, Lewistown)


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. You can see here the Las Vegas shooting received a lot more views than the other shooting.
shootings |> 
  mutate(after_event = (day > 0)) 
     project language     article     access      agent granularity       date
1  wikipedia       en Gun_control all-access all-agents       daily 2017-09-26
2  wikipedia       en Gun_control all-access all-agents       daily 2017-09-27
3  wikipedia       en Gun_control all-access all-agents       daily 2017-09-28
4  wikipedia       en Gun_control all-access all-agents       daily 2017-09-29
5  wikipedia       en Gun_control all-access all-agents       daily 2017-09-30
6  wikipedia       en Gun_control all-access all-agents       daily 2017-10-01
7  wikipedia       en Gun_control all-access all-agents       daily 2017-10-02
8  wikipedia       en Gun_control all-access all-agents       daily 2017-10-03
9  wikipedia       en Gun_control all-access all-agents       daily 2017-10-04
10 wikipedia       en Gun_control all-access all-agents       daily 2017-10-05
11 wikipedia       en Gun_control all-access all-agents       daily 2017-10-06
12 wikipedia       en Gun_control all-access all-agents       daily 2017-10-07
13 wikipedia       en Gun_control all-access all-agents       daily 2017-10-08
14 wikipedia       en Gun_control all-access all-agents       daily 2017-10-09
15 wikipedia       en Gun_control all-access all-agents       daily 2017-10-10
16 wikipedia       en Gun_control all-access all-agents       daily 2023-10-18
17 wikipedia       en Gun_control all-access all-agents       daily 2023-10-19
18 wikipedia       en Gun_control all-access all-agents       daily 2023-10-20
19 wikipedia       en Gun_control all-access all-agents       daily 2023-10-21
20 wikipedia       en Gun_control all-access all-agents       daily 2023-10-22
21 wikipedia       en Gun_control all-access all-agents       daily 2023-10-23
22 wikipedia       en Gun_control all-access all-agents       daily 2023-10-24
23 wikipedia       en Gun_control all-access all-agents       daily 2023-10-25
24 wikipedia       en Gun_control all-access all-agents       daily 2023-10-26
25 wikipedia       en Gun_control all-access all-agents       daily 2023-10-27
26 wikipedia       en Gun_control all-access all-agents       daily 2023-10-28
27 wikipedia       en Gun_control all-access all-agents       daily 2023-10-29
28 wikipedia       en Gun_control all-access all-agents       daily 2023-10-30
29 wikipedia       en Gun_control all-access all-agents       daily 2023-10-31
30 wikipedia       en Gun_control all-access all-agents       daily 2023-11-01
   views day     event after_event
1    486  -7 Las vegas       FALSE
2    450  -6 Las vegas       FALSE
3    423  -5 Las vegas       FALSE
4    399  -4 Las vegas       FALSE
5    284  -3 Las vegas       FALSE
6    317  -2 Las vegas       FALSE
7   3554  -1 Las vegas       FALSE
8   4342   0 Las vegas       FALSE
9   3469   1 Las vegas        TRUE
10  2838   2 Las vegas        TRUE
11  1966   3 Las vegas        TRUE
12  1189   4 Las vegas        TRUE
13  1221   5 Las vegas        TRUE
14  1713   6 Las vegas        TRUE
15  1476   7 Las vegas        TRUE
16   350  -7 Lewistown       FALSE
17   357  -6 Lewistown       FALSE
18   358  -5 Lewistown       FALSE
19   333  -4 Lewistown       FALSE
20   351  -3 Lewistown       FALSE
21   417  -2 Lewistown       FALSE
22   492  -1 Lewistown       FALSE
23   548   0 Lewistown       FALSE
24   778   1 Lewistown        TRUE
25   651   2 Lewistown        TRUE
26   484   3 Lewistown        TRUE
27   553   4 Lewistown        TRUE
28   620   5 Lewistown        TRUE
29   524   6 Lewistown        TRUE
30   559   7 Lewistown        TRUE

5.

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      1.13  27.5   0.266 two.sided       447.    -361.    1255.
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        841.  1223.    16
2 TRUE        1289.   931.    14

The average number of views of the Wikipedia Gun Control article in the 7 days prior to the two shootings (M = 841.3, SD = 1223.3) was not statistically significantly different from the average number of views in the 7 days after the shooting (M = 1288.6, SD = 930.8), t(27.5) = 1.13, p = .266.