Wiki assignment pt.3

library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.2.3
Warning: package 'ggplot2' was built under R version 4.2.3
Warning: package 'tibble' was built under R version 4.2.3
Warning: package 'tidyr' was built under R version 4.2.3
Warning: package 'readr' was built under R version 4.2.3
Warning: package 'purrr' was built under R version 4.2.3
Warning: package 'dplyr' was built under R version 4.2.3
Warning: package 'stringr' was built under R version 4.2.3
Warning: package 'forcats' was built under R version 4.2.3
Warning: package 'lubridate' was built under R version 4.2.3
── 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
Warning: package 'DT' was built under R version 4.2.3
library(infer)            # for some stats like t_test
Warning: package 'infer' was built under R version 4.2.3
library(devtools)
Warning: package 'devtools' was built under R version 4.2.3
Loading required package: usethis
Warning: package 'usethis' was built under R version 4.2.3
gun_control <- article_pageviews(article = "Gun control", start = as.Date("2017-1-1"), end = as.Date("2023-12-31"))

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

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

PROBLEM SECTION

guns |> 
  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 Mass,.... 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()`).

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

October 25, 2023 (2023 - 10 - 25) in Lewiston, Maine. Shooter open-fired at a bowling alley with 18 dead and 13 injured, after he killed himself.

maine |> 
  mutate(day = -14:14) |> 
  mutate(event = "Maine") |> datatable() 
maine <- maine |> 
  mutate(day = -14:14) |> 
  mutate(event = "Maine")
maine |> 
  ggplot(aes(x = day, y = views)) +
  geom_line()

alabama <- article_pageviews(article = "Gun_control",
                           start = as.Date("2023-04-01"),
                           end = as.Date("2023-04-29"))

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

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

April 15, 2023 in Dadevile, Alabama (2023 - 04 - 15) shooter open-fired at a birthday party with 4 dead, 32 injured, and 6 suspects taken into custody.

shootings <- bind_rows(maine, alabama)

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

second <- top_articles(start = as.Date("2023-10-25"))
second |> 
  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, April 16, 2023")

The Alabama shooting took place on April 15th, so I put in April 16th to see what the top article views of the day were after it took place. It does not appear to be in the top article views.

second |> 
  select(article, views) |>
  filter(!article == "Main_Page", !article == "Special:Search") |> datatable()

These are the top article views for the Alabama shooting day.

first <- top_articles(start = as.Date("2023-10-25"))
first |> 
  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, October 26, 2023")

These are the top article views for October 26th, the day following the Maine shooting. Once again, the shooting and gun violence does not appear to be in the top 10 list.

first |> 
  select(article, views) |>
  filter(!article == "Main_Page", !article == "Special:Search") |> datatable()

This is a table for the Maine shooting day.

gun_control <- article_pageviews(article = "Gun control", start = as.Date("2023-4-8"), end = as.Date("2023-4-22"))

glimpse(gun_control)
Rows: 15
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> 2023-04-08, 2023-04-09, 2023-04-10, 2023-04-11, 2023-04-1…
$ views       <dbl> 597, 590, 722, 922, 796, 654, 527, 391, 532, 572, 595, 659…

For the 7 days before and 7 days after the April 15th shooting in Alabama.

gun_control <- article_pageviews(article = "Gun control", start = as.Date("2023-10-18"), end = as.Date("2023-11-1"))

glimpse(gun_control)
Rows: 15
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> 2023-10-18, 2023-10-19, 2023-10-20, 2023-10-21, 2023-10-2…
$ views       <dbl> 350, 357, 358, 333, 351, 417, 492, 548, 778, 651, 484, 553…

For the 7 days before and 7 days after the October 25th Maine shooting.

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")
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")
shootings <- bind_rows(maine, alabama)

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

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

This t-test shows that the average number of views for the Wikipedia Gun Control article in the 7 days before the two shootings (M = 525.3, SD = 179.1) was not statistically significantly different from the average number of views in the 7 days folowing the shooting (M = 579.5, SD = 97.2), t(23.7) = 1.05, p = .306.