library(tools)
library(usethis)
library(devtools)Wikipedia API assignment
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)── 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)
library(DT)
library(infer)
library(devtools)# install.packages("devtools")
devtools::install_github("r-lib/conflicted")Skipping install of 'conflicted' from a github remote, the SHA1 (321d77ce) has not changed since last install.
Use `force = TRUE` to force installation
mass_shooting <- article_pageviews(article = "Mass shootings in the United States", start = as.Date("2017-1-1"), end = as.Date("2023-12-31"))
glimpse(mass_shooting)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_shooting %>%
slice_max(views, n =10) %>%
select (article, date) article date
1 Mass_shootings_in_the_United_States 2022-05-25
2 Mass_shootings_in_the_United_States 2019-08-04
3 Mass_shootings_in_the_United_States 2019-08-05
4 Mass_shootings_in_the_United_States 2022-05-26
5 Mass_shootings_in_the_United_States 2019-08-06
6 Mass_shootings_in_the_United_States 2018-02-15
7 Mass_shootings_in_the_United_States 2023-10-26
8 Mass_shootings_in_the_United_States 2018-02-16
9 Mass_shootings_in_the_United_States 2022-05-27
10 Mass_shootings_in_the_United_States 2021-03-23
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…
guns <- bind_rows(gun_control, mass_shooting) - Look up views of a new Wikipedia article called “Mass shootings in the United States” over several years, graph it over time, do the same with the “Gun control” article again, and overlay the two in a single graph.
guns |>
ggplot(aes(x = date, y = views, color = article)) +
geom_line() +
labs(y = "Number of Views", x = "Date", title = "Mass Shootings and Gun Control Compared")- Create a scatterplot of views of the two articles
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: Mass_shootings_in_the_United_States <dbl>
guns |>
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) +
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()`).
- Find two different mass shootings in the US that received a lot of attention and look up the dates. Use different ones than the ones I used in the example. To see whether people’s attention was on those shootings, for each shooting, create a table (using datatable() and removing special pages) and a bar graph of the top article views of the day after each mass shooting happened.
El Paso shooting
el_paso <-top_articles(start = as.Date("2019-8-04"))el_paso %>%
select(article, views) |>
filter(!article == "Main_Page", !article == "Special:Search") |>
slice_max(views, n = 10) |>
datatable()el_paso %>%
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, Nov. 20, 2023")Robb Elementary shooting
uvalde <-top_articles(start = as.Date("2022-5-25"))uvalde %>%
select(article, views) |>
filter(!article == "Main_Page", !article == "Special:Search") |>
slice_max(views, n = 10) |>
datatable()uvalde %>%
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, May 25th 2022")- Find the views of the Gun control article 7 days before and 7 days after the two shootings, combine the data, and graph it over time
uvalde1<-
article_pageviews(article = "Gun_control",
start = as.Date("2022-5-18"),
end = as.Date("2022-6-01"))
uvalde1_edit<-
uvalde1 |>
mutate(day = -7:7) |>
mutate(event = "uvalde")
uvalde1_edit |>
ggplot(aes(x = day, y = views)) +
geom_line()el_paso1 <-
article_pageviews(article = "Gun_control",
start = as.Date("2019-7-28"),
end = as.Date("2019-8-11"))
el_paso1_edit <- el_paso1 |>
mutate(day = -7:7) |>
mutate(event = "el paso")
el_paso1_edit |>
ggplot(aes(x = day, y = views)) +
geom_line()shootings <- bind_rows(el_paso1_edit, uvalde1_edit)
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")- Conduct a t-test to see if views of the gun control article were higher in the 7 days after the shootings compared to the 7 days prior.
shootings |>
mutate(after_event = (day > 0)) project language article access agent granularity date
1 wikipedia en Gun_control all-access all-agents daily 2019-07-28
2 wikipedia en Gun_control all-access all-agents daily 2019-07-29
3 wikipedia en Gun_control all-access all-agents daily 2019-07-30
4 wikipedia en Gun_control all-access all-agents daily 2019-07-31
5 wikipedia en Gun_control all-access all-agents daily 2019-08-01
6 wikipedia en Gun_control all-access all-agents daily 2019-08-02
7 wikipedia en Gun_control all-access all-agents daily 2019-08-03
8 wikipedia en Gun_control all-access all-agents daily 2019-08-04
9 wikipedia en Gun_control all-access all-agents daily 2019-08-05
10 wikipedia en Gun_control all-access all-agents daily 2019-08-06
11 wikipedia en Gun_control all-access all-agents daily 2019-08-07
12 wikipedia en Gun_control all-access all-agents daily 2019-08-08
13 wikipedia en Gun_control all-access all-agents daily 2019-08-09
14 wikipedia en Gun_control all-access all-agents daily 2019-08-10
15 wikipedia en Gun_control all-access all-agents daily 2019-08-11
16 wikipedia en Gun_control all-access all-agents daily 2022-05-18
17 wikipedia en Gun_control all-access all-agents daily 2022-05-19
18 wikipedia en Gun_control all-access all-agents daily 2022-05-20
19 wikipedia en Gun_control all-access all-agents daily 2022-05-21
20 wikipedia en Gun_control all-access all-agents daily 2022-05-22
21 wikipedia en Gun_control all-access all-agents daily 2022-05-23
22 wikipedia en Gun_control all-access all-agents daily 2022-05-24
23 wikipedia en Gun_control all-access all-agents daily 2022-05-25
24 wikipedia en Gun_control all-access all-agents daily 2022-05-26
25 wikipedia en Gun_control all-access all-agents daily 2022-05-27
26 wikipedia en Gun_control all-access all-agents daily 2022-05-28
27 wikipedia en Gun_control all-access all-agents daily 2022-05-29
28 wikipedia en Gun_control all-access all-agents daily 2022-05-30
29 wikipedia en Gun_control all-access all-agents daily 2022-05-31
30 wikipedia en Gun_control all-access all-agents daily 2022-06-01
views day event after_event
1 325 -7 el paso FALSE
2 677 -6 el paso FALSE
3 556 -5 el paso FALSE
4 878 -4 el paso FALSE
5 466 -3 el paso FALSE
6 323 -2 el paso FALSE
7 614 -1 el paso FALSE
8 3238 0 el paso FALSE
9 3434 1 el paso TRUE
10 2748 2 el paso TRUE
11 1892 3 el paso TRUE
12 1479 4 el paso TRUE
13 1358 5 el paso TRUE
14 955 6 el paso TRUE
15 927 7 el paso TRUE
16 553 -7 uvalde FALSE
17 491 -6 uvalde FALSE
18 457 -5 uvalde FALSE
19 360 -4 uvalde FALSE
20 365 -3 uvalde FALSE
21 623 -2 uvalde FALSE
22 1177 -1 uvalde FALSE
23 9666 0 uvalde FALSE
24 5587 1 uvalde TRUE
25 2977 2 uvalde TRUE
26 1998 3 uvalde TRUE
27 1774 4 uvalde TRUE
28 1638 5 uvalde TRUE
29 1720 6 uvalde TRUE
30 1223 7 uvalde 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.23 23.4 0.233 two.sided 824. -566. 2214.
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 1298. 2339. 16
2 TRUE 2122. 1241. 14
The average number of views of the Wikipedia Gun Control article in the 7 days prior to the two shootings (M = 1298, SD = 2339.4) was not statistically significantly different from the average number of views in the 7 days after the shooting (M = 2122.1, SD = 1241.3), t(23.4) = 1.22, p = .232.