How to use this document. Run the code along with me. Comments start with
#. Each section mirrors the live lab; code chunks show the command and its output together.
Today’s plan
rm(list = ls()) # clean environment
# Load the packages
library(dplyr)
library(ggplot2)
library(stats)In the live session we set the working directory with
setwd(...). In this document the data fileLife Expectancy Data.csvsits next to the.Rmd, so nosetwd()is needed.
The dataset is the WHO Life Expectancy data.
data <- read.csv("Life Expectancy Data.csv")
# In RStudio you would run View(data); here we preview the first rows.
knitr::kable(head(data), caption = "First rows of the Life Expectancy data")| Country | Year | Status | Life.expectancy | Adult.Mortality | infant.deaths | Alcohol | percentage.expenditure | Hepatitis.B | Measles | BMI | under.five.deaths | Polio | Total.expenditure | Diphtheria | HIV.AIDS | GDP | Population | thinness..1.19.years | thinness.5.9.years | Income.composition.of.resources | Schooling |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Afghanistan | 2015 | Developing | 65.0 | 263 | 62 | 0.01 | 71.279624 | 65 | 1154 | 19.1 | 83 | 6 | 8.16 | 65 | 0.1 | 584.25921 | 33736494 | 17.2 | 17.3 | 0.479 | 10.1 |
| Afghanistan | 2014 | Developing | 59.9 | 271 | 64 | 0.01 | 73.523582 | 62 | 492 | 18.6 | 86 | 58 | 8.18 | 62 | 0.1 | 612.69651 | 327582 | 17.5 | 17.5 | 0.476 | 10.0 |
| Afghanistan | 2013 | Developing | 59.9 | 268 | 66 | 0.01 | 73.219243 | 64 | 430 | 18.1 | 89 | 62 | 8.13 | 64 | 0.1 | 631.74498 | 31731688 | 17.7 | 17.7 | 0.470 | 9.9 |
| Afghanistan | 2012 | Developing | 59.5 | 272 | 69 | 0.01 | 78.184215 | 67 | 2787 | 17.6 | 93 | 67 | 8.52 | 67 | 0.1 | 669.95900 | 3696958 | 17.9 | 18.0 | 0.463 | 9.8 |
| Afghanistan | 2011 | Developing | 59.2 | 275 | 71 | 0.01 | 7.097109 | 68 | 3013 | 17.2 | 97 | 68 | 7.87 | 68 | 0.1 | 63.53723 | 2978599 | 18.2 | 18.2 | 0.454 | 9.5 |
| Afghanistan | 2010 | Developing | 58.8 | 279 | 74 | 0.01 | 79.679367 | 66 | 1989 | 16.7 | 102 | 66 | 9.20 | 66 | 0.1 | 553.32894 | 2883167 | 18.4 | 18.4 | 0.448 | 9.2 |
#> Country Year Status Life.expectancy
#> Length:2938 Min. :2000 Length:2938 Min. :36.30
#> Class :character 1st Qu.:2004 Class :character 1st Qu.:63.10
#> Mode :character Median :2008 Mode :character Median :72.10
#> Mean :2008 Mean :69.22
#> 3rd Qu.:2012 3rd Qu.:75.70
#> Max. :2015 Max. :89.00
#> NA's :10
#> Adult.Mortality infant.deaths Alcohol percentage.expenditure
#> Min. : 1.0 Min. : 0.0 Min. : 0.0100 Min. : 0.000
#> 1st Qu.: 74.0 1st Qu.: 0.0 1st Qu.: 0.8775 1st Qu.: 4.685
#> Median :144.0 Median : 3.0 Median : 3.7550 Median : 64.913
#> Mean :164.8 Mean : 30.3 Mean : 4.6029 Mean : 738.251
#> 3rd Qu.:228.0 3rd Qu.: 22.0 3rd Qu.: 7.7025 3rd Qu.: 441.534
#> Max. :723.0 Max. :1800.0 Max. :17.8700 Max. :19479.912
#> NA's :10 NA's :194
#> Hepatitis.B Measles BMI under.five.deaths
#> Min. : 1.00 Min. : 0.0 Min. : 1.00 Min. : 0.00
#> 1st Qu.:77.00 1st Qu.: 0.0 1st Qu.:19.30 1st Qu.: 0.00
#> Median :92.00 Median : 17.0 Median :43.50 Median : 4.00
#> Mean :80.94 Mean : 2419.6 Mean :38.32 Mean : 42.04
#> 3rd Qu.:97.00 3rd Qu.: 360.2 3rd Qu.:56.20 3rd Qu.: 28.00
#> Max. :99.00 Max. :212183.0 Max. :87.30 Max. :2500.00
#> NA's :553 NA's :34
#> Polio Total.expenditure Diphtheria HIV.AIDS
#> Min. : 3.00 Min. : 0.370 Min. : 2.00 Min. : 0.100
#> 1st Qu.:78.00 1st Qu.: 4.260 1st Qu.:78.00 1st Qu.: 0.100
#> Median :93.00 Median : 5.755 Median :93.00 Median : 0.100
#> Mean :82.55 Mean : 5.938 Mean :82.32 Mean : 1.742
#> 3rd Qu.:97.00 3rd Qu.: 7.492 3rd Qu.:97.00 3rd Qu.: 0.800
#> Max. :99.00 Max. :17.600 Max. :99.00 Max. :50.600
#> NA's :19 NA's :226 NA's :19
#> GDP Population thinness..1.19.years
#> Min. : 1.68 Min. :3.400e+01 Min. : 0.10
#> 1st Qu.: 463.94 1st Qu.:1.958e+05 1st Qu.: 1.60
#> Median : 1766.95 Median :1.387e+06 Median : 3.30
#> Mean : 7483.16 Mean :1.275e+07 Mean : 4.84
#> 3rd Qu.: 5910.81 3rd Qu.:7.420e+06 3rd Qu.: 7.20
#> Max. :119172.74 Max. :1.294e+09 Max. :27.70
#> NA's :448 NA's :652 NA's :34
#> thinness.5.9.years Income.composition.of.resources Schooling
#> Min. : 0.10 Min. :0.0000 Min. : 0.00
#> 1st Qu.: 1.50 1st Qu.:0.4930 1st Qu.:10.10
#> Median : 3.30 Median :0.6770 Median :12.30
#> Mean : 4.87 Mean :0.6276 Mean :11.99
#> 3rd Qu.: 7.20 3rd Qu.:0.7790 3rd Qu.:14.30
#> Max. :28.60 Max. :0.9480 Max. :20.70
#> NA's :34 NA's :167 NA's :163
What is recoding / encoding? Converting categorical
variables (text) into numeric values — for example,
"Developed" → 1 and "Developing" → 0.
Why do we recode?
Create a dummy variable. Use ifelse()
for binary variables.
#> [1] 0 0 0 0 0 0
Simple regression with a binary predictor. Predict life expectancy from developed/developing status.
#>
#> Call:
#> lm(formula = Life.expectancy ~ is_developed, data = data)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -30.811 -4.912 1.288 6.489 21.889
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 67.1115 0.1698 395.28 <2e-16 ***
#> is_developed 12.0864 0.4060 29.77 <2e-16 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Residual standard error: 8.345 on 2926 degrees of freedom
#> (10 observations deleted due to missingness)
#> Multiple R-squared: 0.2325, Adjusted R-squared: 0.2322
#> F-statistic: 886.2 on 1 and 2926 DF, p-value: < 2.2e-16
- Intercept = mean life expectancy for Developing countries (the reference group).
is_developed= difference in mean life expectancy (Developed − Developing).
We test \(H_0: \mu = 70\) against \(H_1: \mu \neq 70\).
Step 1 — Sample mean.
#> [1] 69.22493
Step 2 — One-sample t-test.
#>
#> One Sample t-test
#>
#> data: data$Life.expectancy
#> t = -4.4036, df = 2927, p-value = 1.103e-05
#> alternative hypothesis: true mean is not equal to 70
#> 95 percent confidence interval:
#> 68.87982 69.57004
#> sample estimates:
#> mean of x
#> 69.22493
Step 3 — Extract the 95% CI bounds (for plotting).
#> [1] 68.87982 69.57004
#> attr(,"conf.level")
#> [1] 0.95
Step 4 — Visualize the distribution with CI lines.
ggplot(data, aes(x = Life.expectancy)) +
geom_histogram(binwidth = 2, fill = "steelblue", color = "black") +
geom_vline(xintercept = ci_life, linetype = "dashed", color = "red") +
labs(
title = "Life Expectancy Distribution (95% CI)",
x = "Life Expectancy (years)",
y = "Count"
)We test \(H_0: \mu_{\text{developed}} = \mu_{\text{developing}}\) against \(H_1: \mu_{\text{developed}} \neq \mu_{\text{developing}}\).
#>
#> Welch Two Sample t-test
#>
#> data: Life.expectancy by Status
#> t = 47.868, df = 1807, p-value < 2.2e-16
#> alternative hypothesis: true difference in means between group Developed and group Developing is not equal to 0
#> 95 percent confidence interval:
#> 11.59118 12.58159
#> sample estimates:
#> mean in group Developed mean in group Developing
#> 79.19785 67.11147
In
t.test(y ~ x, data = ...):
y= numeric variable (the thing you’re measuring)x= categorical variable (the grouping factor, must have 2 levels)
Visualize.
ggplot(data, aes(x = Status, y = Life.expectancy)) +
geom_boxplot(fill = "lightblue", color = "black") +
labs(
title = "Life Expectancy by Country Status",
x = "Country Status",
y = "Life Expectancy (years)"
)P1) One-sample t-test: is average GDP different from 5000?
#>
#> One Sample t-test
#>
#> data: data$GDP
#> t = 8.6831, df = 2489, p-value < 2.2e-16
#> alternative hypothesis: true mean is not equal to 5000
#> 95 percent confidence interval:
#> 6922.383 8043.934
#> sample estimates:
#> mean of x
#> 7483.158
P2) One-sample t-test: is average
Schooling different from 10? Create a histogram
with the 95% CI.
#>
#> One Sample t-test
#>
#> data: data$Schooling
#> t = 31.253, df = 2774, p-value < 2.2e-16
#> alternative hypothesis: true mean is not equal to 10
#> 95 percent confidence interval:
#> 11.86777 12.11782
#> sample estimates:
#> mean of x
#> 11.99279
ci_schooling <- t.test(data$Schooling, conf.level = 0.95)$conf.int
ggplot(data, aes(x = Schooling)) +
geom_histogram(binwidth = 1, fill = "skyblue", color = "black") +
geom_vline(xintercept = ci_schooling, linetype = "dashed", color = "red") +
labs(
title = "Schooling Distribution (95% CI)",
x = "Years of Schooling",
y = "Count"
)P3) Two-sample t-test: compare
Adult.Mortality between Developed and Developing.
Create boxplots by group.
#>
#> Welch Two Sample t-test
#>
#> data: Adult.Mortality by Status
#> t = -30.745, df = 2174.9, p-value < 2.2e-16
#> alternative hypothesis: true difference in means between group Developed and group Developing is not equal to 0
#> 95 percent confidence interval:
#> -109.72693 -96.56836
#> sample estimates:
#> mean in group Developed mean in group Developing
#> 79.68555 182.83320
ggplot(data, aes(x = Status, y = Adult.Mortality)) +
geom_boxplot(fill = "lightgreen", color = "black") +
labs(
title = "Adult Mortality by Country Status",
x = "Country Status",
y = "Adult Mortality Rate"
)Key takeaways
ifelse() to create binary (dummy) variables.t.test(x, mu = value) for a one-sample test.t.test(y ~ x, data) for two independent
samples.$conf.int, then draw vertical lines
on histograms.