WikipediaAPI_TB

install.packages("pageviews_0.5.0.tar", repos = NULL, type = "source")
Installing package into 'C:/Users/thoma/AppData/Local/R/win-library/4.3'
(as 'lib' is unspecified)
Warning in install.packages("pageviews_0.5.0.tar", repos = NULL, type =
"source"): installation of package 'pageviews_0.5.0.tar' had non-zero exit
status
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.1
✔ 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("2018-1-1"), end = as.Date("2024-2-10"))

gun_control <- article_pageviews(article = "Gun_control", start = as.Date("2018-1-1"), end = as.Date("2024-2-10"))

madness <- bind_rows(gun_control, mass_shootings) 

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

  1. We can see that as the mass shootings in the United States page receives more views that gun control, while much smaller, has an increase in views as well.
madness |> 
  pivot_wider(names_from = article, values_from = views) |> 
  ggplot(aes(x = Mass_shootings_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'

  1. The scatterplot shows that when the number of views for gun violence increases, typically the amount of views for gun control is proportiantely more.
topalabama <- top_articles(start = as.Date("2023-4-16"))
  
topalabama |> 
  select(article, views) |>
  filter(!article == "Main_Page", !article == "Special:Search") |> 
  slice_max(views, n = 10) |> 
  datatable()
topalabama |> 
  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 = "yellow") + 
  coord_flip() + 
  scale_y_continuous(labels = scales::comma) + 
  labs(y = "Number of Views", x = "Article", title = "Top Wikipedia articles, Apr. 16, 2023")

  1. The table and graph above show the 10 most viewed pages on Wikipedia on April 16, 2023, one day after the mass shooting in Dadeville, Alabama.
topmaine <- top_articles(start = as.Date("2023-10-26"))
  
topmaine |> 
  select(article, views) |>
  filter(!article == "Main_Page", !article == "Special:Search") |> 
  slice_max(views, n = 10) |> 
  datatable()
topmaine |> 
  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) + 
  labs(y = "Number of Views", x = "Article", title = "Top Wikipedia articles, Oct. 26, 2023")

  1. The table and graph above show the 10 most viewed pages on Wikipedia on October 26, 2023, one day after the mass shooting in Lewiston, Maine.
alabama <- article_pageviews(article = "Gun_control",
                           start = as.Date("2023-4-8"),
                           end = as.Date("2023-4-22"))

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

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

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

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

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

shootings <- bind_rows(alabama, maine)

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 amount of views on the gun control article before Alabama’s mass shooting was at its highest point right before and then gradually regained traction right after. The amount of views on the gun control article before Maine’s mass shooting hardly registered before th shooting and reached its peak right after the incident.
shootings |> 
  mutate(after_event = (day > 0)) 
     project language     article     access      agent granularity       date
1  wikipedia       en Gun_control all-access all-agents       daily 2023-04-08
2  wikipedia       en Gun_control all-access all-agents       daily 2023-04-09
3  wikipedia       en Gun_control all-access all-agents       daily 2023-04-10
4  wikipedia       en Gun_control all-access all-agents       daily 2023-04-11
5  wikipedia       en Gun_control all-access all-agents       daily 2023-04-12
6  wikipedia       en Gun_control all-access all-agents       daily 2023-04-13
7  wikipedia       en Gun_control all-access all-agents       daily 2023-04-14
8  wikipedia       en Gun_control all-access all-agents       daily 2023-04-15
9  wikipedia       en Gun_control all-access all-agents       daily 2023-04-16
10 wikipedia       en Gun_control all-access all-agents       daily 2023-04-17
11 wikipedia       en Gun_control all-access all-agents       daily 2023-04-18
12 wikipedia       en Gun_control all-access all-agents       daily 2023-04-19
13 wikipedia       en Gun_control all-access all-agents       daily 2023-04-20
14 wikipedia       en Gun_control all-access all-agents       daily 2023-04-21
15 wikipedia       en Gun_control all-access all-agents       daily 2023-04-22
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    597  -7 alabama       FALSE
2    590  -6 alabama       FALSE
3    722  -5 alabama       FALSE
4    922  -4 alabama       FALSE
5    796  -3 alabama       FALSE
6    654  -2 alabama       FALSE
7    527  -1 alabama       FALSE
8    391   0 alabama       FALSE
9    532   1 alabama        TRUE
10   572   2 alabama        TRUE
11   595   3 alabama        TRUE
12   659   4 alabama        TRUE
13   666   5 alabama        TRUE
14   556   6 alabama        TRUE
15   364   7 alabama        TRUE
16   350  -7   maine       FALSE
17   357  -6   maine       FALSE
18   358  -5   maine       FALSE
19   333  -4   maine       FALSE
20   351  -3   maine       FALSE
21   417  -2   maine       FALSE
22   492  -1   maine       FALSE
23   548   0   maine       FALSE
24   778   1   maine        TRUE
25   651   2   maine        TRUE
26   484   3   maine        TRUE
27   553   4   maine        TRUE
28   620   5   maine        TRUE
29   524   6   maine        TRUE
30   559   7   maine        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      1.05  23.7   0.306 two.sided       54.2    -52.7     161.
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        525.  179.     16
2 TRUE         580.   97.2    14
  1. The average number of views of the Wikipedia Gun Control article in the 7 days prior to the two shootings (M = 525.3, SD = 179.08) was not statistically significantly different from the average number of views in the 7 days after the shooting (M = 579.5, SD = 97.20), t(23.7) = 1.04, p = .306.