stat545a-2013-hw03_lee-woo

Presets:

Sys.setenv(lang = "EN")
library("lattice")
library("plyr")
library("xtable")

Load and check variables in Gapminder data: link to the data

dat <- read.delim("http://www.stat.ubc.ca/~jenny/notOcto/STAT545A/examples/gapminder/data/gapminderDataFiveYear.txt")
names(dat)
## [1] "country"   "year"      "pop"       "continent" "lifeExp"   "gdpPercap"

Getting summary statistics of GDP per capita by continent

Code for getting summary statistics of GDP per capita by continents

GetGDPSummary = function(data) {
    tData = data$gdpPercap
    nCtr = length(unique(data$country))
    Minimum = min(tData)
    Medium = quantile(tData, probs = 0.5)
    Maximum = max(tData)
    Average = mean(tData)
    SD = sqrt(var(tData))

    res = c(nCtr, Minimum, Medium, Maximum, Average, SD)
    names(res) = c("# Countries", "Min", "Med", "Max", "Avg", "SD")
    return(res)
}

summCont = ddply(dat, ~continent, GetGDPSummary)

Sort according to average GDP per capita and display the result

summCont = summCont[order(summCont$Avg), ]
print(xtable(summCont, digits = 0), type = "html")
continent # Countries Min Med Max Avg SD
1 Africa 52 241 1192 21951 2194 2828
2 Americas 25 1202 5466 42952 7136 6397
3 Asia 33 331 2647 113523 7902 14045
4 Europe 30 974 12082 49357 14469 9355
5 Oceania 2 10040 17983 34435 18622 6359

It is not appropriate to make comments on countries on Oceania, since it only has two countries. For others, asian countries seem to have the largest variation in GDP per capita among countries. African countries has the smallest variation, and they have the lowest average GDP per capita.

Getting mean life expectancy of continents over time

Code for getting mean life expectancy of continents over time

GetMeanLifeExp = function(data) {
    sumLifeExp = ddply(data, ~continent, summarize, mean(lifeExp))
    res = t(sumLifeExp[, 2])
    names(res) = levels(data$continent)
    return(res)
}

summYear = ddply(dat, ~year, GetMeanLifeExp)
names(summYear) = c("Year", levels(dat$continent))

Display the result

print(xtable(summYear), type = "html")
Year Africa Americas Asia Europe Oceania
1 1952 39.14 53.28 46.31 64.41 69.25
2 1957 41.27 55.96 49.32 66.70 70.30
3 1962 43.32 58.40 51.56 68.54 71.09
4 1967 45.33 60.41 54.66 69.74 71.31
5 1972 47.45 62.39 57.32 70.78 71.91
6 1977 49.58 64.39 59.61 71.94 72.85
7 1982 51.59 66.23 62.62 72.81 74.29
8 1987 53.34 68.09 64.85 73.64 75.32
9 1992 53.63 69.57 66.54 74.44 76.94
10 1997 53.60 71.15 68.02 75.51 78.19
11 2002 53.33 72.42 69.23 76.70 79.74
12 2007 54.81 73.61 70.73 77.65 80.72

Finding countries with low life expectancy relative to its GDP per capita on average over time

Code for getting ratio of life expectancy and GDP per capita

GetRatio = function(data) {
    vRatio = data$lifeExp/data$gdpPercap
    mlifeExp = mean(data$lifeExp)
    mgdpPer = mean(data$gdpPercap)
    mRatio = mean(vRatio)

    res = c(mlifeExp, mgdpPer, mRatio)
    names(res) = c("mlifeExp", "mgdpPer", "mRatio")
    return(res)
}

summRatio = ddply(dat, ~country, GetRatio)

Sort according to average GDP per capita and display the “Top 10” countries

summRatio = summRatio[order(summRatio$mRatio), ]
names(summRatio) = c("Country", "Mean Life Expectancy", "Mean GDP per Capita", 
    "Mean LifeExp/GDPperCap")
print(xtable(head(summRatio, 10), digits = c(0, 0, 2, 0, 5)), type = "html")
Country Mean Life Expectancy Mean GDP per Capita Mean LifeExp/GDPperCap
72 Kuwait 68.92 65333 0.00142
124 Switzerland 75.57 27074 0.00296
135 United States 73.48 26261 0.00314
110 Saudi Arabia 58.68 20262 0.00338
96 Norway 75.84 26747 0.00363
21 Canada 74.90 22411 0.00376
8 Bahrain 65.61 18078 0.00381
35 Denmark 74.37 21672 0.00397
91 Netherlands 75.65 21749 0.00411
6 Australia 74.66 19981 0.00424

Most of the countries with the low ratio are developed countries and those with high life expectancy, which is an indicator that life expectancy would grow slower as life expectancy gets higher when GDP per capita increases. Also, middle east countries such as Kuwait, Saudi Arabia and Bahrain experience low mean life expectancy compared to their GDP per capita.