Y <- c(2, 5, 1, 8, 9, 2) #Creating a vector
print(Y) #Showing vector
## [1] 2 5 1 8 9 2
mean(Y)
## [1] 4.5
#install.packages("combinat")
library(combinat)
choose(6, 4) #Number of possible combinations
## [1] 15
combn(Y, 4) #Showing all combinations of 4 units
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
## [1,] 2 2 2 2 2 2 2 2 2 2 5 5 5 5 1
## [2,] 5 5 5 5 5 5 1 1 1 8 1 1 1 8 8
## [3,] 1 1 1 8 8 9 8 8 9 9 8 8 9 9 9
## [4,] 8 9 2 9 2 2 9 2 2 2 9 2 2 2 2
combn(Y, 4, mean) #Estimating the arithmetic mean of all samples
## [1] 4.00 4.25 2.50 6.00 4.25 4.50 5.00 3.25 3.50 5.25 5.75 4.00 4.25 6.00 5.00
est_average <- combn(Y, 4, mean)
mean(est_average)
## [1] 4.5
var_est_average = 1/15 * sum((est_average - 4.5)^2)
var_est_average
## [1] 0.9583333
SE_est_average = sqrt(var_est_average)
SE_est_average
## [1] 0.978945
CLT
Y <- seq(1, 100, 1)
hist(Y,
prob = TRUE,
main = "Distribution of Y",
ylab = "Density",
xlab = "Y",
breaks = seq(0, 100, 1))

n <- 10 #Sample size
#Making 1000 samples of size 10 from uniform distribution
est_average <- rep(0, 1000)
for (i in 1:1000) {
est_average[i] <- mean(runif(n, min = 0, max = 100))
}
hist(est_average,
prob = TRUE,
breaks = seq(0, 100, 1),
main = "Distribution of sample arithmetic means",
ylab = "Density",
xlab = "Sample arithmetic mean")
curve(dnorm(x, mean = mean(est_average), sd = sd(est_average)),
add = TRUE, #Curve is added to previous graph
lwd = 2, #Thickness
col = "red") #Colour

mean(est_average) #The arithmetic mean of sample averages
## [1] 49.64162
library(ggplot2)
ggplot(NULL, aes(c(-4, 4))) +
theme_bw()+
geom_hline(yintercept=0)+
geom_line(stat = "function", fun = dnorm, col="black", linetype = 1) + #Normal distribution (full curve)
geom_line(stat = "function", fun = dt, args = list(df=30), col="red", linetype = 2) + #t-dist., df=30 (longer lines)
geom_line(stat = "function", fun = dt, args = list(df=5), col="blue", linetype = 3) + #t-dist., df=5 (dots)
ylab("Density") +
xlab("")

Confidence interval - graphically
set.seed(10) #Initial seed of sampling
point_estimate <- numeric(10000) #Vector for point estimates
lower_b <- numeric(10000) #Vector for lower boundaries
upper_b <- numeric(10000) #Vector for upper boundaries
Mu = 3 #Population arithmetic mean
SD = 2 #Population standard deviation
n = 100 #Sample size
# 10000-times we make a sample of 100 units, for each sample we estimate confidence interval for alfa = 0.05. Since we have population standard devaiation, z-distribution is used.
for(i in 1:10000) {
Y <- rnorm(n, mean = Mu, sd = SD)
point_estimate[i] <- mean(Y)
lower_b[i] <- mean(Y) - 1.96 * SD/sqrt(n)
upper_b[i] <- mean(Y) + 1.96 * SD/sqrt(n)
}
# Histogram of distribution of sample estimates of arithmetic mean
hist(point_estimate,
breaks = seq(1, 5, 0.01),
xlab = "Estimates",
ylab = "Frequency",
main = "Distribution of estimates")

#Joining lower and upper boundaries
Confid_interval <- cbind(lower_b, upper_b)
head(Confid_interval)
## lower_b upper_b
## [1,] 2.334902 3.118902
## [2,] 2.418075 3.202075
## [3,] 2.665504 3.449504
## [4,] 3.005678 3.789678
## [5,] 2.507109 3.291109
## [6,] 2.747071 3.531071
#Checking the proportion of samples that miss population arithmetic mean (Mu=3)
mean((Confid_interval[, 1] < 3 & 3 < Confid_interval[, 2]))
## [1] 0.9517
#Finding which samples miss population arithmetic mean
ID <- which(!((Confid_interval[1:100, 1] < 3 & 3 < Confid_interval[1:100, 2])))
ID
## [1] 4 10 17 37 38
#Defining a graph
plot(NULL,
xlim = c(1, 5),
ylim = c(1, 100),
ylab = "Sample",
xlab = expression(mu),
main = "Confidence interval for the arithmetic mean")
#Gray colour showd "good" samples, "bad" samples are coloured red.
colors <- rep(gray(0.8), 100)
colors[ID] <- "red"
#Defining a vertical line at Mu=3
abline(v = 3, lty = 2)
#Graphically showing first 100 confidence intervals
for(j in 1:100) {
lines(c(Confid_interval[j, 1], Confid_interval[j, 2]),
c(j, j),
col = colors[j],
lwd = 2)
}
