# Random sampling
load('./data/L1/example1_1.RData')
sample(example1_1$Names, 10, replace = TRUE)
# Exercise 3
normData <- replicate(100000, {
xbar <- mean(rnorm(30, 2))
})
plot(density(normData), xlab = 'x', main = 'Sampling distribution of sample mean (normal)')
curve(dnorm(x, 2, sqrt(1/30)), add = T, col = 2)
# ecdf
plot(ecdf(example1_1$Grade))
# pdf of chisq distribution
dev.new()
curve(dchisq(x, 9), from = 0, to = 10, col = 1,
xlab = "x", ylab = "f(x)", lwd = 2, ylim = c(0,0.5))
curve(dchisq(x, 6), from = 0, to = 10, col = 2, add = TRUE, lwd = 2)
curve(dchisq(x, 5), from = 0, to = 10, col = 3, add = TRUE, lwd = 2)
curve(dchisq(x, 4), from = 0, to = 10, col = 4, add = TRUE, lwd = 2)
curve(dchisq(x, 3), from = 0, to = 10, col = 5, add = TRUE, lwd = 2)
curve(dchisq(x, 2), from = 0, to = 10, col = 6, add = TRUE, lwd = 2)
curve(dchisq(x, 1), from = 0, to = 10, col = 7, add = TRUE, lwd = 2)
legend("topright", legend = c(paste0("df = ", c(1:5, 6, 9))),
col = 7:1,
lty = 1, lwd = 2)
# pdf of t distribution
dev.new()
curve(dt(x, 30), from = -5, to = 5, col = 2,
xlab = "x", ylab = "f(x)", lwd = 2)
curve(dt(x, 10), from = -5, to = 5, col = 3, add = TRUE, lwd = 2)
curve(dt(x, 5), from = -5, to = 5, col = 4, add = TRUE, lwd = 2)
curve(dt(x, 1), from = -5, to = 5, col = 5, add = TRUE, lwd = 2)
curve(dnorm(x), from = -5, to = 5, col = 1, add = TRUE, lwd = 2)
legend("topleft", legend = c(paste0("df = ", c(1, 5, 10, 30)), 'Normal'),
col =5:1,
lty = 1, lwd = 2)
# pdf of F distribution
curve(df(x, 1, 1), from = 0, to = 5, col = 1,
xlab = "x", ylab = "f(x)", lwd = 2, ylim = c(0, 2.2))
curve(df(x, 2, 1), from = 0, to = 5, col = 2, add = TRUE, lwd = 2)
curve(df(x, 5, 2), from = 0, to = 5, col = 3, add = TRUE, lwd = 2)
curve(df(x, 10, 1), from = 0, to = 5, col = 4, add = TRUE, lwd = 2)
curve(df(x, 100, 100), from = 0, to = 5, col = 5, add = TRUE, lwd = 2)
legend("topright", legend = paste0("df1 = ", c(1, 2, 5, 10, 100), ', ', "df2 = ", c(1, 1, 2, 1, 100)),
col = 1:5,
lty = 1, lwd = 2)