-
Notifications
You must be signed in to change notification settings - Fork 1
/
07-wdi.Rmd
530 lines (342 loc) · 15.2 KB
/
07-wdi.Rmd
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# `WDI` {#wdidata}
## Reviews and Previews
```{r message=FALSE}
library(tidyverse)
library(gapminder)
library(maps)
library(WDI)
library(readxl)
```
* We have used `tidyverse` and `gapminder` already.
* If you have not installed `WDI`, install it.
* `maps` and `readxl` are bundled in `tidyverse` but need to be attached by `library`.
### Gapminder Package Data
```{r}
df <- gapminder
df
```
### `gdpPercap` of ASEAN countries
```{r eval=FALSE}
asean <- c("Brunei", "Cambodia", "Laos", "Myanmar",
"Philippines", "Indonesia", "Malaysia", "Singapore")
df %>% filter(country %in% asean) %>%
ggplot(aes(x = year, y = gdpPercap, col = country)) + geom_line()
```
```{r echo=FALSE}
asean <- c("Cambodia", "Myanmar",
"Philippines", "Indonesia", "Malaysia", "Singapore")
df %>% filter(country %in% asean) %>%
ggplot(aes(x = year, y = gdpPercap, col = country)) + geom_line()
```
```{r echo=TRUE}
df %>% filter(country %in% asean) %>%
ggplot(aes(x = gdpPercap, y = lifeExp, col = country)) + geom_point()
```
```{r echo=TRUE}
df %>% filter(country %in% asean) %>%
ggplot(aes(x = gdpPercap, y = lifeExp, col = country)) +
geom_point() + coord_trans(x = "log10", y = "identity")
```
$\log_{10}{100}$ = `r log10(100)`, $\log_{10}{1000}$ = `r log10(1000)`, $\log_{10}{10000}$ = `r log10(10000)`
```{r gapminder-combined, eval=FALSE}
df2007 <- df %>% filter(country %in% asean, year == 2007)
df %>% filter(country %in% asean) %>%
ggplot(aes(x = gdpPercap, y = lifeExp, col = country))+
geom_line() + geom_label(data = df2007, aes(label = country), position = position_jitter()) + geom_point() +
coord_trans(x = "log10", y = "identity") +
theme(axis.text.x = element_text(angle = 90, vjust = 1, hjust=1), legend.position = "none") +
labs(title = "Life Expectancy vs GDP Per Capita of ASEAN Countries",
subtitle = "Data: gapminder package", x = "GDP per Capita", y = "Life Expectancy")
```
```{r echo=FALSE}
df2007 <- df %>% filter(country %in% asean, year == 2007)
df %>% filter(country %in% asean) %>%
ggplot(aes(x = gdpPercap, y = lifeExp, col = country))+
geom_line() + geom_label(data = df2007, aes(label = country), position = position_jitter(width = 1, height = 1)) + geom_point() +
coord_trans(x = "log10", y = "identity") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1), legend.position = "none") +
labs(title = "Life Expectancy vs GDP Per Capita of ASEAN Countries",
subtitle = "Data: gapminder package", x = "GDP per Capita", y = "Life Expectancy")
```
```{r echo=FALSE}
library(tidyverse)
library(maps)
world_map <- map_data("world")
df %>%
ggplot(aes(map_id = country)) +
geom_map(aes(fill = gdpPercap), map = world_map) + expand_limits(x = world_map$long, y = world_map$lat) +
labs(title = "Gapminder Package Data", subtitle="World Map of GDP per Capita Data")
```
The `gapminder` is a handy package to look at a worldview using three indicators, i.e., life expectancy, population, and GDP per capita. However, some countries are missing, and the data source is unclear.
### World Bank: World Development Indicators (WDI)
* SP.DYN.LE00.IN: Life expectancy at birth, total (years)
* NY.GDP.PCAP.KD: GDP per capita (constant 2015 US$)
* SP.POP.TOTL: Population, total
```{r eval=FALSE}
df_wdi <- WDI(
country = "all",
indicator = c(lifeExp = "SP.DYN.LE00.IN", pop = "SP.POP.TOTL", gdpPercap = "NY.GDP.PCAP.KD")
)
```
```{r echo=FALSE, message=FALSE, eval=FALSE}
write_csv(df_wdi, "./data/df_wdi.csv")
```
```{r echo=FALSE, message=FALSE}
df_wdi <- read_csv("./data/df_wdi.csv")
```
```{r}
df_wdi
```
```{r cash=TRUE, eval=FALSE}
df_wdi_extra <- WDI(
country = "all",
indicator = c(lifeExp = "SP.DYN.LE00.IN", pop = "SP.POP.TOTL", gdpPercap = "NY.GDP.PCAP.KD"),
extra = TRUE
)
```
```{r echo=FALSE, message=FALSE, eval=FALSE}
write_csv(df_wdi_extra, "./data/df_wdi_extra.csv")
```
```{r echo=FALSE, message=FALSE}
df_wdi_extra <- read_csv("./data/df_wdi_extra.csv")
```
```{r}
df_wdi_extra
```
## Exploratory Data Analysis
### What is EDA (Posit Primers: [Visualise Data](https://posit.cloud/learn/primers/3.1))
1. EDA is an iterative cycle that helps you understand what your data says. When you do EDA, you:
2. Generate questions about your data
3. Search for answers by visualising, transforming, and/or modeling your data
Use what you learn to refine your questions and/or generate new questions
EDA is an important part of any data analysis. You can use EDA to make discoveries about the world; or you can use EDA to ensure the quality of your data, asking questions about whether the data meets your standards or not.
## Open and Public Data, World Bank
### [Open Government Data Toolkit](http://opendatatoolkit.worldbank.org): [Open Data Defined](http://opendatatoolkit.worldbank.org/en/essentials.html)
The term **Open Data** has a very precise meaning. Data or content is open if anyone is free to use, re-use or redistribute it, subject at most to measures that preserve provenance and openness.
1. The data must be _legally open_, which means they must be placed in the public domain or under liberal terms of use with minimal restrictions.
2. The data must be _technically open_, which means they must be published in electronic formats that are machine readable and non-proprietary, so that anyone can access and use the data using common, freely available software tools. Data must also be publicly available and accessible on a public server, without password or firewall restrictions. To make Open Data easier to find, most organizations create and manage Open Data catalogs.
## World Bank: WDI - World Development Indicaters
* World Bank: https://www.worldbank.org
* [Who we are](https://www.worldbank.org/en/who-we-are):
- To end extreme poverty: By reducing the share of the global population that lives in extreme poverty to 3 percent by 2030.
- To promote shared prosperity: By increasing the incomes of the poorest 40 percent of people in every country.
* World Bank Open Data: https://data.worldbank.org
- Data Bank, World Development Indicators, etc.
* [World Development Indicators (WDI)](https://datatopics.worldbank.org/world-development-indicators/) : the World Bank’s premier compilation of cross-country comparable data on development; 1400 time series indicators
- Themes: Poverty and Inequality, People, Environment, Economy, States and Markets, Global Links
- Open Data & DataBank: Explore data, Query database
- Bulk Download: Excel, CSV
- API Documentation
## R Package [WDI](https://CRAN.R-project.org/package=WDI)
* [WDI](https://CRAN.R-project.org/package=WDI): World Development Indicators and Other World Bank Data
* Search and download data from over 40 databases hosted by the World Bank, including the World Development Indicators ('WDI'), International Debt Statistics, Doing Business, Human Capital Index, and Sub-national Poverty indicators.
* Version: 2.7.4
* Materials: [README](https://cran.r-project.org/web/packages/WDI/readme/README.html) - _usage_
- [NEWS](https://cran.r-project.org/web/packages/WDI/news/news.html) - _version history_
* Published: 2021-04-06
* README: https://cran.r-project.org/web/packages/WDI/readme/README.html
* Reference manual: [WDI.pdf](https://cran.r-project.org/web/packages/WDI/WDI.pdf)
## Function WDI
* **Usage**
```
WDI(country = "all",
indicator = "NY.GDP.PCAP.KD",
start = 1960,
end = 2020,
extra = FALSE,
cache = NULL)
```
* **Arguments** See Help!
- country: Vector of countries (ISO-2 character codes, e.g. "BR", "US", "CA", or "all")
- indicator: If you supply a named vector, the indicators will be automatically renamed: `c('women_private_sector' = 'BI.PWK.PRVS.FE.ZS')`
## Function WDIsearch
```{r}
library(WDI)
```
```{r cache=TRUE}
WDIsearch(string = "NY.GDP.PCAP.KD",
field = "indicator", cache = NULL)
```
```{r cache=TRUE}
WDIsearch(string = "population",
field = "name", short=FALSE, cache = NULL)
```
```
WDIsearch(string = "NY.GDP.PCAP.KD",
field = "indicator", short = FALSE, cache = NULL)
```
```
WDIsearch(string = "gdp",
field = "name", short = TRUE, cache = NULL)
```
## WDIcache
Download an updated list of available WDI indicators from the World Bank website. Returns a list for use in the WDIsearch function.
```{r widcache, eval=FALSE}
wdi_cache <- WDIcache()
```
Downloading all series information from the World Bank website can take time. The WDI package ships with a local data object with information on all the series available on 2012-06-18. You can update this database by retrieving a new list using `WDIcache`, and then feeding the resulting object to `WDIsearch` via the cache argument.
```{r echo=FALSE, message=FALSE, eval=FALSE}
write_rds(wdi_cache, "./data/wdi_cache.RData", refhook = NULL)
```
```{r echo=FALSE, message=FALSE}
wdi_cache <- read_rds("./data/wdi_cache.RData", refhook = NULL)
```
```{r}
glimpse(wdi_cache)
```
```{r}
WDIsearch(string = "gdp",
field = "name", short = FALSE, cache = wdi_cache)
```
## World Development Indicators - Summary
Find indicators:
1. `WDIsearch(string = "gdp", field = "name", short = FALSE, cache = NULL)`
- `WDIsearch(string = "gdp", field = "name", short = FALSE, cache = wdi_cache)`
- `WDIsearch(string = "NY.GDP.PCAP.KD", field = "indicator", short = FALSE, cache = NULL)`
2. [WDI](https://datatopics.worldbank.org/world-development-indicators/): Data Themes
3. Browse by Indicators: https://data.worldbank.org/indicator
- Featured Indicators or All Indicators
- Obtain the indicator from the detail or the URL
### Example: CO2 emissions (metric tons per capita)
* ID: EN.ATM.CO2E.PC
* URL: https://data.worldbank.org/indicator/EN.ATM.CO2E.PC
```{r}
WDIsearch(string = "EN.ATM.CO2E.PC", field = "indicator",
short = FALSE, cache = wdi_cache)
```
```{r}
WDIsearch(string = "EN.ATM.CO2E.PC", field = "indicator",
short = FALSE, cache = wdi_cache) %>% pull(description)
```
* Source: Climate Watch. 2020. GHG Emissions. Washington, DC: World Resources Institute. Available at: climatewatchdata.org/ghg-emissions. See SP.POP.TOTL for the denominator's source.
```{r cash=TRUE, eval=FALSE}
co2pcap <- WDI(country = "all", indicator = "EN.ATM.CO2E.PC", start = 1960, end = NULL, extra = TRUE, cache = wdi_cache)
```
```{r echo=FALSE, message=FALSE, eval=FALSE}
write_csv(co2pcap, "./data/co2pcap.csv")
```
```{r echo=FALSE, message=FALSE}
co2pcap <- read_csv("./data/co2pcap.csv")
```
```{r}
co2pcap
```
```{r}
co2pcap %>% filter(country %in% c("World", "Japan", "United States", "China")) %>%
ggplot(aes(x = year, y = EN.ATM.CO2E.PC, color = country)) +
geom_line()
```
```{r}
co2pcap %>% filter(!is.na(EN.ATM.CO2E.PC)) %>% pull(year) %>% summary()
```
```{r}
co2pcap %>%
filter(country %in% c("World", "Japan", "United States", "China"), year %in% 1990:2019) %>%
ggplot(aes(x = year, y = EN.ATM.CO2E.PC, color = country)) +
geom_line()
```
```{r}
co2pcap %>%
filter(income != "Aggregates", year == 2019) %>%
ggplot(aes(x = income, y = EN.ATM.CO2E.PC, fill = income)) +
geom_boxplot()
```
```{r}
co2pcap %>%
filter(income != "Aggregates", year == 2019, !is.na(EN.ATM.CO2E.PC)) %>%
ggplot(aes(x = income, y = EN.ATM.CO2E.PC, fill = income)) +
geom_boxplot()
```
* What is `boxplot`: https://vimeo.com/222358034
```{r}
co2pcap %>%
filter(income != "Aggregates", year == 2019, !is.na(EN.ATM.CO2E.PC)) %>%
group_by(income) %>%
summarize(min = min(EN.ATM.CO2E.PC), med = median(EN.ATM.CO2E.PC), max = max(EN.ATM.CO2E.PC), IQR = IQR(EN.ATM.CO2E.PC), n = n())
```
```{r}
co2pcap %>%
filter(income != "Aggregates", year == 2019, !is.na(EN.ATM.CO2E.PC)) %>%
filter(!income %in% c("High income", "Low income", "Lower middle income", "Upper middle income"))
```
```{r}
co2pcap %>%
filter(income != "Aggregates", year == 2019) %>%
filter(income == "Not classified")
```
```{r echo=TRUE, eval=FALSE}
co2pcap %>%
filter(income != "Aggregates", year == 2019) %>%
ggplot(aes(map_id = country)) +
geom_map(aes(fill = income), map = world_map) + expand_limits(x = world_map$long, y = world_map$lat) +
labs(title = "Income Levels in 2019")
```
```{r echo=FALSE}
co2pcap %>%
filter(income != "Aggregates", year == 2019) %>%
ggplot(aes(map_id = country)) +
geom_map(aes(fill = income), map = world_map) + expand_limits(x = world_map$long, y = world_map$lat) +
labs(title = "Income Levels in 2019")
```
```{r}
co2pcap %>% distinct(country)
```
```{r}
world_map %>% distinct(region)
```
```{r}
world_map0 <- world_map %>%
mutate(region = case_when(region == "Macedonia" ~ "North Macedonia",
region == "Ivory Coast" ~ "Cote d'Ivoire",
region == "Democratic Republic of the Congo" ~ "Congo, Dem. Rep.",
region == "Republic of Congo" ~ "Congo, Rep.",
region == "UK" ~ "United Kingdom",
region == "USA" ~ "United States",
region == "Laos" ~ "Lao PDR",
region == "Slovakia" ~ "Slovak Republic",
region == "Saint Lucia" ~ "St. Lucia",
region == "Kyrgyzstan" ~ "Kyrgyz Republic",
region == "Micronesia" ~ "Micronesia, Fed. Sts.",
region == "Swaziland" ~ "Eswatini",
region == "Virgin Islands" ~ "Virgin Islands (U.S.)",
region == "Russia" ~ "Russian Federation",
region == "Egypt" ~ "Egypt, Arab Rep.",
region == "South Korea" ~ "Korea, Rep.",
region == "North Korea" ~ "Korea, Dem. People's Rep.",
region == "Iran" ~ "Iran, Islamic Rep.",
region == "Brunei" ~ "Brunei Darussalam",
region == "Venezuela" ~ "Venezuela, RB",
region == "Yemen" ~ "Yemen, Rep.",
region == "Bahamas" ~ "Bahamas, The",
region == "Syria" ~ "Syrian Arab Republic",
region == "Turkey" ~ "Turkiye",
region == "Cape Verde" ~ "Cabo Verde",
region == "Gambia" ~ "Gambia, The",
region == "Czech Republic" ~ "Czechia",
TRUE ~ region))
```
```{r}
write_csv(world_map0, "./data/world_map0.csv")
```
```{r eval=FALSE}
map0_url <- "https://icu-hsuzuki.github.io/da4r2022_note/data/world_map0.csv"
world_map0 <- read_csv(map0_url)
```
```{r}
co2pcap %>% filter(income != "Aggregates", year == 2019) %>%
anti_join(world_map0, by = c("country"="region"))
```
```{r}
world_map0 %>% anti_join(co2pcap, by = c("region"="country")) %>% distinct(region) %>% arrange(region)
```
```{r}
world_map0 %>% left_join(iso3166, by = c("region" = "ISOname")) %>%
filter(is.na(a2)) %>% distinct(region)
```
```{r echo=FALSE}
co2pcap %>%
filter(income != "Aggregates", year == 2019) %>%
ggplot(aes(map_id = country)) +
geom_map(aes(fill = income), map = world_map0) + expand_limits(x = world_map$long, y = world_map$lat) +
labs(title = "Income Levels in 2019")
```