library(gapminder)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
life_data <- gapminder$lifeExp
sample_means <- replicate(1000, mean(sample(life_data, size = 30, replace = TRUE)))
hist(sample_means, main = "Sampling Distribution of Sample Mean", xlab = "Mean Life Expectancy")

means_10 <- replicate(1000, mean(sample(life_data, 10, replace = TRUE)))
means_100 <- replicate(1000, mean(sample(life_data, 100, replace = TRUE)))
hist(means_10, main="n = 10")

hist(means_100, main="n = 100")

simulate_means <- function(n, reps = 1000) {
replicate(reps, mean(sample(life_data, size = n, replace = TRUE)))
}
sample_sizes <- c(10, 30, 50, 100)
par(mfrow = c(1,1))
for (n in sample_sizes) {
hist(simulate_means(n), main = paste("n =10", n))}




library(gapminder)
head(gapminder)
## # A tibble: 6 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Afghanistan Asia 1957 30.3 9240934 821.
## 3 Afghanistan Asia 1962 32.0 10267083 853.
## 4 Afghanistan Asia 1967 34.0 11537966 836.
## 5 Afghanistan Asia 1972 36.1 13079460 740.
## 6 Afghanistan Asia 1977 38.4 14880372 786.
library(ggplot2)
summary(gapminder)
## country continent year lifeExp
## Afghanistan: 12 Africa :624 Min. :1952 Min. :23.60
## Albania : 12 Americas:300 1st Qu.:1966 1st Qu.:48.20
## Algeria : 12 Asia :396 Median :1980 Median :60.71
## Angola : 12 Europe :360 Mean :1980 Mean :59.47
## Argentina : 12 Oceania : 24 3rd Qu.:1993 3rd Qu.:70.85
## Australia : 12 Max. :2007 Max. :82.60
## (Other) :1632
## pop gdpPercap
## Min. :6.001e+04 Min. : 241.2
## 1st Qu.:2.794e+06 1st Qu.: 1202.1
## Median :7.024e+06 Median : 3531.8
## Mean :2.960e+07 Mean : 7215.3
## 3rd Qu.:1.959e+07 3rd Qu.: 9325.5
## Max. :1.319e+09 Max. :113523.1
##
ggplot(gapminder, aes(x = gdpPercap, y = lifeExp)) + geom_point()

cor(gapminder$lifeExp, gapminder$gdpPercap, method = "pearson")
## [1] 0.5837062
cor(gapminder$lifeExp, gapminder$gdpPercap, method = "spearman")
## [1] 0.8264712
europe <- gapminder %>% filter(continent == "Europe") %>% pull(lifeExp)
africa <- gapminder %>% filter(continent == "Africa") %>% pull(lifeExp)
t.test(europe, africa)
##
## Welch Two Sample t-test
##
## data: europe and africa
## t = 49.551, df = 981.2, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 22.12595 23.95076
## sample estimates:
## mean of x mean of y
## 71.90369 48.86533
model <- lm(lifeExp ~ gdpPercap, data = gapminder)
summary(model)
##
## Call:
## lm(formula = lifeExp ~ gdpPercap, data = gapminder)
##
## Residuals:
## Min 1Q Median 3Q Max
## -82.754 -7.758 2.176 8.225 18.426
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.396e+01 3.150e-01 171.29 <2e-16 ***
## gdpPercap 7.649e-04 2.579e-05 29.66 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 10.49 on 1702 degrees of freedom
## Multiple R-squared: 0.3407, Adjusted R-squared: 0.3403
## F-statistic: 879.6 on 1 and 1702 DF, p-value: < 2.2e-16
model_multi <- lm(lifeExp ~ gdpPercap + year + continent, data = gapminder)
summary(model_multi)
##
## Call:
## lm(formula = lifeExp ~ gdpPercap + year + continent, data = gapminder)
##
## Residuals:
## Min 1Q Median 3Q Max
## -28.4264 -4.0725 0.2154 4.4853 19.9977
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -5.207e+02 1.979e+01 -26.31 <2e-16 ***
## gdpPercap 2.967e-04 1.996e-05 14.87 <2e-16 ***
## year 2.874e-01 1.000e-02 28.73 <2e-16 ***
## continentAmericas 1.433e+01 4.936e-01 29.03 <2e-16 ***
## continentAsia 9.506e+00 4.567e-01 20.81 <2e-16 ***
## continentEurope 1.940e+01 5.173e-01 37.49 <2e-16 ***
## continentOceania 2.059e+01 1.469e+00 14.01 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 6.884 on 1697 degrees of freedom
## Multiple R-squared: 0.717, Adjusted R-squared: 0.716
## F-statistic: 716.6 on 6 and 1697 DF, p-value: < 2.2e-16