|
| 1 | +--- |
| 2 | +title: "Homework 1: Insights on Poverty" |
| 3 | +date: "February 4, 2016" |
| 4 | +output: html_document |
| 5 | +--- |
| 6 | + |
| 7 | +**This homework is due Sunday February 14, 2016 at 11:59 PM. When complete, submit your code in the R Markdown file and the knitted HTML file on Canvas.** |
| 8 | + |
| 9 | +# Background |
| 10 | + |
| 11 | +This HW is based on Hans Rosling talks [New Insights on Poverty](https://www.ted.com/talks/hans_rosling_reveals_new_insights_on_poverty?language=en) and [The Best Stats You've Ever Seen](https://www.ted.com/talks/hans_rosling_shows_the_best_stats_you_ve_ever_seen). |
| 12 | + |
| 13 | +The assignment uses data to answer specific question about global health and economics. The data contradicts commonly held preconceived notions. For example, Hans Rosling starts his talk by asking: (paraphrased) "for each of the six pairs of countries below, which country do you think had the highest child mortality in 2015?" |
| 14 | + |
| 15 | +1. Sri Lanka or Turkey |
| 16 | +2. Poland or South Korea |
| 17 | +3. Malaysia or Russia |
| 18 | +4. Pakistan or Vietnam |
| 19 | +5. Thailand or South Africa |
| 20 | + |
| 21 | +Most people get them wrong. Why is this? In part it is due to our preconceived notion that the world is divided into two groups: the |
| 22 | +_Western world_ versus the _third world_, characterized by "long life,small family" and "short life, large family" respectively. In this homework we will use data visualization to gain insights on this topic. |
| 23 | + |
| 24 | + |
| 25 | +# Problem 1 |
| 26 | + |
| 27 | +The first step in our analysis is to download and organize the data. The necessary data to answer these question is available on the [gapminder](http://www.gapminder.org/data/) website. |
| 28 | + |
| 29 | +## Problem 1.1 |
| 30 | + |
| 31 | +We will use the following datasets: |
| 32 | + |
| 33 | +1. [Childhood mortality](http://spreadsheets.google.com/pub?key=0ArfEDsV3bBwCcGhBd2NOQVZ1eWowNVpSNjl1c3lRSWc&output=csv) |
| 34 | +2. [Life expectancy](http://spreadsheets.google.com/pub?key=phAwcNAVuyj2tPLxKvvnNPA&output=csv) |
| 35 | +3. [Fertility](http://spreadsheets.google.com/pub?key=phAwcNAVuyj0TAlJeCEzcGQ&output=csv) |
| 36 | +4. [Population](http://spreadsheets.google.com/pub?key=phAwcNAVuyj0XOoBL_n5tAQ&output=csv) |
| 37 | +5. [Total GDP](http://spreadsheets.google.com/pub?key=pyj6tScZqmEfI4sLVvEQtHw&output=csv) |
| 38 | + |
| 39 | +Create five `tbl_df` table objects, one for each of the tables provided in the above files. Hints: Use the `read_csv` function. Because these are only temporary files, give them short names. |
| 40 | +```{r,include=FALSE} |
| 41 | +library(tidyr) |
| 42 | +library(dplyr) |
| 43 | +library(readr) |
| 44 | +library(ggplot2) |
| 45 | +theme_set(theme_bw()) ##optional |
| 46 | +
|
| 47 | +t1 <-read_csv("http://spreadsheets.google.com/pub?key=0ArfEDsV3bBwCcGhBd2NOQVZ1eWowNVpSNjl1c3lRSWc&output=csv") |
| 48 | +t2 <- read_csv("http://spreadsheets.google.com/pub?key=phAwcNAVuyj2tPLxKvvnNPA&output=csv") |
| 49 | +t3 <- read_csv("http://spreadsheets.google.com/pub?key=phAwcNAVuyj0TAlJeCEzcGQ&output=csv") |
| 50 | +t4 <- read_csv("http://spreadsheets.google.com/pub?key=phAwcNAVuyj0XOoBL_n5tAQ&output=csv") |
| 51 | +t5 <- read_csv("http://spreadsheets.google.com/pub?key=pyj6tScZqmEfI4sLVvEQtHw&output=csv") |
| 52 | +### To keep track of what these are we will define a vector of values |
| 53 | +value_names <- c("child_mortality","life_expectancy","fertility","population","gdp") |
| 54 | +``` |
| 55 | + |
| 56 | + |
| 57 | +## Problem 1.2 |
| 58 | + |
| 59 | + Write a function called `my_func` that takes a table as an argument and returns the column name. For each of the five tables, what is the name of the column containing the country names? Print out the tables or look at them with `View` to determine the column. |
| 60 | + |
| 61 | +```{r} |
| 62 | +my_func <- function(x){ |
| 63 | + names(x)[1] |
| 64 | +} |
| 65 | +
|
| 66 | +answer <- c( my_func(t1), my_func(t2), my_func(t3), my_func(t4), my_func(t5) ) |
| 67 | +
|
| 68 | +answer |
| 69 | +``` |
| 70 | + |
| 71 | +## Problem 1.3 |
| 72 | + |
| 73 | +In the previous problem we noted that gapminder is inconsistent in naming their country column. Fix this by assigning a common name to this column in the various tables. |
| 74 | + |
| 75 | +```{r} |
| 76 | +the_name <- "country" |
| 77 | +names(t1)[1] <- the_name |
| 78 | +names(t2)[1] <- the_name |
| 79 | +names(t3)[1] <- the_name |
| 80 | +names(t4)[1] <- the_name |
| 81 | +names(t5)[1] <- the_name |
| 82 | +``` |
| 83 | + |
| 84 | +## Problem 1.4 |
| 85 | + |
| 86 | +Notice that in these tables, years are represented by columns. We want to create a tidy dataset in which each row is a unit or observation and our 5 values of interest, including the year for that unit, are in the columns. The unit here is a country/year pair and each unit gets values: |
| 87 | + |
| 88 | +```{r} |
| 89 | +value_names |
| 90 | +``` |
| 91 | + |
| 92 | +We call this the _long_ format. Use the `gather` function from the `tidyr` package to create a new table for childhood mortality using the long format. Call the new columns `year` and `child_mortality` |
| 93 | + |
| 94 | +```{r} |
| 95 | +gather(t1, key=year, value=child_mortality, -country, convert = TRUE) |
| 96 | +``` |
| 97 | + |
| 98 | +Now redefine the remaining tables in this way. |
| 99 | + |
| 100 | +```{r} |
| 101 | +t1 <- gather(t1, key=year, value=child_mortality, -country, convert = TRUE) |
| 102 | +t2 <- gather(t2, key=year, value=life_expectancy, -country, convert = TRUE) |
| 103 | +t3 <- gather(t3, key=year, value=fertility, -country, convert = TRUE) |
| 104 | +t4 <- gather(t4, key=year, value=population, -country, convert = TRUE) |
| 105 | +t5 <- gather(t5, key=year, value=gdp, -country, convert = TRUE) |
| 106 | +``` |
| 107 | + |
| 108 | + |
| 109 | +## Problem 1.5 |
| 110 | + |
| 111 | +Now we want to join all these files together. Make one consolidated table containing all the columns |
| 112 | + |
| 113 | +```{r} |
| 114 | +dat <- t1 %>% full_join(t2) %>% full_join(t3) %>% full_join(t4) %>% full_join(t5) |
| 115 | +``` |
| 116 | + |
| 117 | +## Problem 1.6 |
| 118 | + |
| 119 | +Add a column to the consolidated table containing the continent for each country. Hint: We have created a file that maps countries to continents [here](https://github.com/datasciencelabs/data/blob/master/homework_data/continent-info.tsv). Hint: Learn to use the `left_join` function. |
| 120 | + |
| 121 | +```{r} |
| 122 | +map <- read_delim("https://raw.githubusercontent.com/datasciencelabs/data/master/homework_data/continent-info.tsv",col_names=c("country","continent"),delim ="\t") |
| 123 | +dat <- left_join(dat, map, continent = continents) |
| 124 | +``` |
| 125 | + |
| 126 | +Advanced solution: |
| 127 | + |
| 128 | +```{r, eval=FALSE} |
| 129 | +t1 <-read_csv("http://spreadsheets.google.com/pub?key=0ArfEDsV3bBwCcGhBd2NOQVZ1eWowNVpSNjl1c3lRSWc&output=csv") |
| 130 | +t2 <- read_csv("http://spreadsheets.google.com/pub?key=phAwcNAVuyj2tPLxKvvnNPA&output=csv") |
| 131 | +t3 <- read_csv("http://spreadsheets.google.com/pub?key=phAwcNAVuyj0TAlJeCEzcGQ&output=csv") |
| 132 | +t4 <- read_csv("http://spreadsheets.google.com/pub?key=phAwcNAVuyj0XOoBL_n5tAQ&output=csv") |
| 133 | +t5 <- read_csv("http://spreadsheets.google.com/pub?key=pyj6tScZqmEfI4sLVvEQtHw&output=csv") |
| 134 | +### To keep track of what these are we will define a vector of values |
| 135 | +value_names <- c("child_mortality","life_expectancy","fertility","population","gdp") |
| 136 | +l <- list(t1,t2,t3,t4,t5) |
| 137 | +rm(t1,t2,t3,t4,t5) |
| 138 | +gc() |
| 139 | +fix_table <- function(tab, val_name){ |
| 140 | + names(tab)[1] <- "country" |
| 141 | + tab <- gather(tab, key=year, value=y, -country, convert = TRUE) |
| 142 | + names(tab)[which(names(tab)=="y")] <- val_name |
| 143 | + return(tab) |
| 144 | +} |
| 145 | +for(i in seq_along(l) ) l[[i]] <-fix_table(l[[i]], value_names[i]) |
| 146 | +dat <- Reduce(full_join, l) |
| 147 | +
|
| 148 | +dat <- left_join(dat, map, continent = continents) |
| 149 | +``` |
| 150 | + |
| 151 | + |
| 152 | +# Problem 2 |
| 153 | + |
| 154 | +Report the child mortalilty rate in 2015 for these 5 pairs: |
| 155 | + |
| 156 | +1. Sri Lanka or Turkey |
| 157 | +2. Poland or South Korea |
| 158 | +3. Malaysia or Russia |
| 159 | +4. Pakistan or Vietnam |
| 160 | +5. Thailand or South Africa |
| 161 | + |
| 162 | +```{r} |
| 163 | +countries <- c("Sri Lanka","Turkey", "Poland", "South Korea", "Malaysia", "Russia","Pakistan","Vietnam","Thailand","South Africa") |
| 164 | +answer <- filter(dat, country%in%countries & year==2015) %>% select(country, child_mortality) |
| 165 | +### optionally we can plot them next to each other |
| 166 | +answer <- bind_cols( slice( answer, match(countries[ seq(1,9,2)], country)), |
| 167 | + slice( answer, match(countries[ seq(2,10,2)], country))) |
| 168 | +answer |
| 169 | +``` |
| 170 | + |
| 171 | +# Problem 3 |
| 172 | + |
| 173 | +To examine if in fact there was a long-life-in-a-small-family and short-life-in-a-large-family dichotomy, we will visualize the average number of children per family (fertility) and the life expectancy for each country. |
| 174 | + |
| 175 | +## Problem 3.1 |
| 176 | + |
| 177 | +Use `ggplot2` to create a plot of life expectancy versus fertility for 1962 for Africa, Asia, Europe, and the Americas. Use color to denote continent and point size to denote population size: |
| 178 | + |
| 179 | +```{r} |
| 180 | +p <- ggplot( filter(dat, year==1962 & continent != "Oceania"), aes(x=fertility, y=life_expectancy)) |
| 181 | +p + geom_point( aes( color=continent, size=population)) |
| 182 | +``` |
| 183 | + |
| 184 | +Do you see a dichotomy? Explain. |
| 185 | + |
| 186 | +## Problem 3.2 |
| 187 | + |
| 188 | +Now we will annotate the plot to show different types of countries. |
| 189 | + |
| 190 | +Learn about OECD and OPEC. Add a couple of columns to your consolidated tables containing a logical vector that tells if a country is OECD and OPEC respectively. It is ok to base membership on 2015. |
| 191 | + |
| 192 | +```{r} |
| 193 | +oecd <- c("Australia","Austria","Belgium","Canada","Chile","Country","Czech Republic","Denmark","Estonia","Finland","France","Germany","Greece","Hungary", |
| 194 | + "Iceland","Ireland","Israel","Italy","Japan","Korea","Luxembourg","Mexico","Netherlands","New Zealand","Norway","Poland","Portugal", |
| 195 | + "Slovak Republic","Slovenia","Spain","Sweden","Switzerland","Turkey","United Kingdom","United States") |
| 196 | +
|
| 197 | +opec <- c("Algeria", "Angola", "Ecuador", "Iran", "Iraq", "Kuwait", "Libya","Nigeria", |
| 198 | + "Qatar", "Saudi Arabia", "United Arab Emirates", "Venezuela") |
| 199 | +
|
| 200 | +dat <- mutate(dat, oecd = country %in% oecd, opec = country %in% opec) |
| 201 | +``` |
| 202 | + |
| 203 | +### Problem 3.3 |
| 204 | + |
| 205 | +Make the same plot as in Problem 3.1, but this time use color to annotate the OECD countries and OPEC countries. For countries that are not part of these two organization annotate if they are from Africa, Asia, or the Americas. |
| 206 | + |
| 207 | +```{r} |
| 208 | +group <- ifelse( dat$country%in%oecd , "OECD", dat$continent ) |
| 209 | +group <- ifelse( dat$country%in%opec , "OPEC", group ) |
| 210 | +group <- ifelse( group%in%c("Europe","Oceania"), NA, group) |
| 211 | +
|
| 212 | +##add group and avoid NAs |
| 213 | +dat2 <- mutate(dat, group=group) %>% |
| 214 | + filter(!is.na(group) & !is.na(population) & !is.na(fertility) & !is.na(life_expectancy)) |
| 215 | +
|
| 216 | +p <- ggplot( mutate(dat2, group = group) %>% filter(year==1962), |
| 217 | + aes(x=fertility, y=life_expectancy)) |
| 218 | +p + geom_point( aes( color=group, size=population)) |
| 219 | +``` |
| 220 | + |
| 221 | +How would you describe the dichotomy? |
| 222 | + |
| 223 | + |
| 224 | +### Problem 3.4 |
| 225 | + |
| 226 | +Explore how this figure changes across time. Show us 4 figures that demonstrate how this figure changes through time. |
| 227 | + |
| 228 | +```{r} |
| 229 | +years <- c(1960, 1980, 2000, 2015) |
| 230 | +p <- ggplot( mutate(dat2, group = group) %>% filter(year%in%years), |
| 231 | + aes(x=fertility, y=life_expectancy)) |
| 232 | +p + geom_point( aes( color=group, size=population)) + facet_wrap(~year) |
| 233 | +``` |
| 234 | + |
| 235 | +Would you say that the same dichotomy exists today? Explain: |
| 236 | + |
| 237 | +Answer: The dichotomy today is not as clear. If there is a dichotomy it is mainly between sub-saharan Africa and the rest of the world. The transition has been relatively smooth. Extra points: You can see the effects of the AIDS epidemic which may explain why life expectancy is down for some countries in 2000 compared to 1980. |
| 238 | + |
| 239 | +## Problem 3.5 (Optional) |
| 240 | + |
| 241 | +Make an animation with the `gganimate` package. |
| 242 | + |
| 243 | +```{r, eval=FALSE} |
| 244 | +library(gganimate) |
| 245 | +p <- ggplot( filter(dat2, year>1950), |
| 246 | + aes(x=fertility, y=life_expectancy)) + coord_cartesian(ylim = c(30, 85)) + |
| 247 | + geom_point( aes( color=group, size=population, frame=year)) |
| 248 | +gg_animate(p, "output.mp4") |
| 249 | +``` |
| 250 | + |
| 251 | + |
| 252 | +# Problem 4 |
| 253 | +Having time as a third dimension made it somewhat difficult to see specific country trends. Let's now focus on specific countries. |
| 254 | + |
| 255 | +## Problem 4.1 |
| 256 | +Let's compare France and its former colony Tunisia. Make a plot of fertility versus year with color denoting the country. Do the same for life expecancy. How would you compare Tunisia's improvement compared to France's in the past 60 years? Hint: use `geom_line` |
| 257 | + |
| 258 | +```{r} |
| 259 | +countries <- c("Tunisia","France") |
| 260 | +p1 <- ggplot(filter(dat2, country %in% countries & year > 1959), |
| 261 | + aes(year,fertility,group=country,color=group)) + geom_line() + theme(legend.position="none") |
| 262 | +p2 <- ggplot(filter(dat2, country %in% countries & year > 1959), |
| 263 | + aes(year,life_expectancy,group=country,color=group)) + geom_line() + theme(legend.position="none") |
| 264 | +
|
| 265 | +library(cowplot) ##this is optional |
| 266 | +g_legend<-function(a.gplot){ |
| 267 | + tmp <- ggplot_gtable(ggplot_build(a.gplot)) |
| 268 | + leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box") |
| 269 | + legend <- tmp$grobs[[leg]] |
| 270 | + return(legend) |
| 271 | +} |
| 272 | +pleg <- ggplot( filter(dat2,country%in%countries & year > 1959), aes(year,fertility,group=country,color=group)) + |
| 273 | + geom_line() |
| 274 | +pleg = g_legend(pleg) |
| 275 | +
|
| 276 | +plot_grid(p1, p2, pleg, ncol = 3) |
| 277 | +``` |
| 278 | + |
| 279 | +## Problem 4.2 |
| 280 | + |
| 281 | +Do the same, but this time compare Vietnam to the OECD countries. |
| 282 | + |
| 283 | +```{r} |
| 284 | +countries <- c("Vietnam",oecd) |
| 285 | +p1 <- ggplot( filter(dat2, country%in%countries & year > 1959), |
| 286 | + aes(year,fertility,group=country,color=group)) + geom_line() + theme(legend.position="none") |
| 287 | +p2 <- ggplot( filter(dat2, country%in%countries & year > 1959), |
| 288 | + aes(year,life_expectancy,group=country,color=group)) + geom_line() + theme(legend.position="none") |
| 289 | +library(cowplot) ##this is optional |
| 290 | +g_legend<-function(a.gplot){ |
| 291 | + tmp <- ggplot_gtable(ggplot_build(a.gplot)) |
| 292 | + leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box") |
| 293 | + legend <- tmp$grobs[[leg]] |
| 294 | + return(legend) |
| 295 | +} |
| 296 | +pleg <- ggplot( filter(dat2,country%in%countries & year > 1959), aes(year,fertility,group=country,color=group)) + geom_line() |
| 297 | +pleg = g_legend(pleg) |
| 298 | +
|
| 299 | +plot_grid(p1, p2, pleg,ncol = 3) |
| 300 | +
|
| 301 | +``` |
| 302 | + |
| 303 | + |
| 304 | +# Problem 5 |
| 305 | + |
| 306 | +We are now going to examine GDP per capita per day. |
| 307 | + |
| 308 | +## Problem 5.1 |
| 309 | + |
| 310 | +Create a smooth density estimate of the distribution of GDP per capita per day across countries in 1970. Include OECD, OPEC, Asia, Africa, and the Americas in the computation. When doing this we want to weigh countries with larger populations more. We can do this using the "weight" argument in `geom_density`. |
| 311 | + |
| 312 | +```{r,warning=FALSE} |
| 313 | +dat2 <- mutate(dat, group=group) %>% |
| 314 | + filter(!is.na(group) & !is.na(population) & !is.na(gdp)) |
| 315 | +
|
| 316 | +ggplot(filter(dat2, year==1970 & group%in%c("OECD","OPEC","Asia","Africa","Americas")), |
| 317 | + aes(x = gdp/population/365)) + |
| 318 | + geom_density(aes(weight=population/sum(population))) + scale_x_log10() |
| 319 | +``` |
| 320 | + |
| 321 | +## Problem 5.2 |
| 322 | + |
| 323 | +Now do the same but show each of the five groups separately. |
| 324 | + |
| 325 | +```{r,warning=FALSE} |
| 326 | +dat2 <- mutate(dat, group=group) %>% |
| 327 | + filter(!is.na(group) & !is.na(population) & !is.na(gdp)) |
| 328 | +
|
| 329 | +filter(dat2, year==1970 & group%in%c("OECD","OPEC","Asia","Africa","Americas")) %>% |
| 330 | + ggplot(aes(x = gdp/population/365)) + |
| 331 | + geom_density(aes(weight=population/sum(population), fill=group), alpha=0.3) + scale_x_log10() |
| 332 | +``` |
| 333 | + |
| 334 | + |
| 335 | +## Problem 5.3 |
| 336 | + |
| 337 | +Visualize these densities for several years. Show a couple of of them. Summarize how the distribution has changed through the years. |
| 338 | + |
| 339 | +```{r,warning=FALSE} |
| 340 | +ggplot(filter(dat2, year%in%c(1970,1990,2010) & group%in%c("OECD","OPEC","Asia","Africa","Americas")), |
| 341 | + aes(x = gdp/population/365)) + |
| 342 | + geom_density(aes(weight=population/sum(population), fill=group), alpha=0.3) + scale_x_log10() + |
| 343 | + facet_grid(year~.) |
| 344 | +
|
| 345 | +``` |
| 346 | + |
| 347 | + |
| 348 | + |
0 commit comments