### CLASS 6 ###
### Statistical laws ###
## Law of large numbers ##
# height ~ N(168, sigma = 2)
# take a random sample of n = 10 from height
# and plot a histogram
sample1 <- rnorm(10, mean = 168, sd = 2)
sample1
hist(sample1)
# take a random sample of n = 100 from height
# and plot a histogram
sample2 <- rnorm(100, mean = 168, sd = 2)
sample2
hist(sample2)
# take a random sample of n = 1600 from height
# and plot a histogram
sample3 <- rnorm(1600, mean = 168, sd = 2)
sample3
hist(sample3)
# compare sample means
mean(sample1)
mean(sample2)
mean(sample3) # closest to 168
## Central limit theorem ##
# generate a population with a uniform distribution
# and look
pop <- runif(10000)
hist(pop)
# let's take 100 samples of 100 elements each
nsamp <- 100
n <- 100
# get a table, each row is a sample of n=100
samples <- matrix(sample(pop), ncol = n, byrow=TRUE)
head(samples) # first rows of this table
# calculate a mean of each sample
# average by each row in samples
# histogram resembles a normal distribution
means <- rowMeans(samples)
means
hist(means)
# compare population mean and mean of means
# compare population sd and sd of means / sqrt(n)
mean(pop)
mean(means)
sd(pop) / 10
sd(means)