gregoryowens — Sep 10, 2013, 4:46 PM
library(lattice)
library(knitr)
#Gapminder data in tab delimited format
gDat <- read.delim("gapminderDataFiveYear.txt")
xyplot(lifeExp ~ year, data = gDat) #Life expectancy plotted against year
xyplot(lifeExp ~ gdpPercap, gDat) #Life expectancy plotted against gdp
#Life expectancy plotted against gdp for Colombia
xyplot(lifeExp ~ gdpPercap, gDat, subset = country == "Colombia")
#life expectancy plotted against gdp for Colombia with a regression line (points and a regression line)
xyplot(lifeExp ~ gdpPercap, gDat, subset = country == "Colombia", type = c("p","r"))
#Life expectancy plotted against gdp divide by continent in 2007
xyplot(lifeExp ~ gdpPercap | continent, gDat, subset = year == 2007)
#Life expectancy plotted against gdp for 2007, color coded by continent
xyplot(lifeExp ~ gdpPercap, gDat, group = continent, subset = year == 2007,
auto.key = TRUE)
densityplot(~lifeExp, gDat)
barchart(table(gDat$continent))
dotplot(table(gDat$continent), type = "h", col.line = NA)
dotplot(table(gDat$continent), type = c("p", "h"), col.line = NA)
xyplot(lifeExp ~ gdpPercap, gDat, subset = year == 2007, group = continent,
auto.key = TRUE)
xyplot(lifeExp ~ gdpPercap | continent, gDat, subset = year == 2007)
subset(gDat, subset = country == "Uruguay")
country year pop continent lifeExp gdpPercap
1621 Uruguay 1952 2252965 Americas 66.07 5717
1622 Uruguay 1957 2424959 Americas 67.04 6151
1623 Uruguay 1962 2598466 Americas 68.25 5603
1624 Uruguay 1967 2748579 Americas 68.47 5445
1625 Uruguay 1972 2829526 Americas 68.67 5703
1626 Uruguay 1977 2873520 Americas 69.48 6504
1627 Uruguay 1982 2953997 Americas 70.81 6920
1628 Uruguay 1987 3045153 Americas 71.92 7452
1629 Uruguay 1992 3149262 Americas 72.75 8137
1630 Uruguay 1997 3262838 Americas 74.22 9230
1631 Uruguay 2002 3363085 Americas 75.31 7727
1632 Uruguay 2007 3447496 Americas 76.38 10611
subset(gDat, subset = country == "Mexico", select = c(country, year, lifeExp))
country year lifeExp
985 Mexico 1952 50.79
986 Mexico 1957 55.19
987 Mexico 1962 58.30
988 Mexico 1967 60.11
989 Mexico 1972 62.36
990 Mexico 1977 65.03
991 Mexico 1982 67.41
992 Mexico 1987 69.50
993 Mexico 1992 71.45
994 Mexico 1997 73.67
995 Mexico 2002 74.90
996 Mexico 2007 76.19
xyplot(lifeExp ~ year, gDat, subset = country == "Colombia", type = c("p", "r"))
(minYear <- min(gDat$year))
[1] 1952
myFit <- lm(lifeExp ~ I(year - minYear), gDat, subset = country == "Colombia")
summary(myFit)
Call:
lm(formula = lifeExp ~ I(year - minYear), data = gDat, subset = country ==
"Colombia")
Residuals:
Min 1Q Median 3Q Max
-2.784 -0.382 0.184 0.841 1.803
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 53.4271 0.7122 75.0 4.3e-15 ***
I(year - minYear) 0.3808 0.0219 17.4 8.5e-09 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 1.31 on 10 degrees of freedom
Multiple R-squared: 0.968, Adjusted R-squared: 0.965
F-statistic: 301 on 1 and 10 DF, p-value: 8.54e-09
with(subset(gDat, subset = country == "Colombia"), cor(lifeExp, gdpPercap))
[1] 0.9515