-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmedal_analysis.R
63 lines (45 loc) · 1.91 KB
/
medal_analysis.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
load("data/medals.Rdata")
library(ggplot2)
library(ggthemes)
library(plotly)
library(DT)
# 01 - create vars --------------------------------------------------------
medals$gdp_per_capita <- round(medals$gdp / medals$pop, 1)
medals$equivalent_medals <- 4 * medals$gold + 2 *medals$silver + 1 * medals$bronze
medals$log_gdp <- round(log(medals$gdp), 2)
medals$log_pop <- round(log(medals$pop), 2)
# 02 - explore ------------------------------------------------------------
summary(medals)
p <- ggplot(medals, aes(x = equivalent_medals)) +
geom_histogram(bins = max(medals$equivalent_medals)) +
theme_hc()
ggplotly(p)
p <- ggplot(medals, aes(x = gdp, y = equivalent_medals, size = pop, label = country)) +
geom_point() +
theme_hc()
ggplotly(p)
p <- ggplot(medals, aes(x = log_gdp, y = equivalent_medals, size = pop, label = country)) +
geom_point() +
theme_hc()
ggplotly(p)
p <- ggplot(medals, aes(x = gdp_per_capita, y = equivalent_medals, size = pop, label = country)) +
geom_point() +
theme_hc()
ggplotly(p)
# 03 - fit ----------------------------------------------------------------
form <- equivalent_medals ~ gdp + gdp_per_capita
glm <- glm(form, family = "poisson", data = medals)
medals$expected_medals <- round(predict.glm(glm, medals, type = "response"))
medals$performance <- round(medals$equivalent_medals / medals$expected_medals, 2)
# 04 - visualize ----------------------------------------------------------
p <- ggplot(medals, aes(x = log_gdp, y = equivalent_medals, size = pop, label = country, color = performance)) +
geom_point() +
theme_hc() +
scale_colour_gradient(low = "red", high = "green")
ggplotly(p)
p <- ggplot(medals, aes(x = gdp_per_capita, y = equivalent_medals, size = pop, label = country, color = performance)) +
geom_point() +
scale_colour_gradient(low = "red", high = "green") +
theme_hc()
ggplotly(p)
datatable(medals)