Here is my sample mean R Markdown using the provided template:
myexp <- rexp(n = 10000) # using default value rate = 1
mu <- mean(myexp)
mu
## [1] 0.9872559
sigma <- sd(myexp)
sigma
## [1] 0.9924715
library("psych")
describe(myexp)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 10000 0.99 0.99 0.68 0.82 0.7 0 10.13 10.13 2.01 6.15 0.01
hist(x = myexp,
main = "Histogram of Exponential Distribution",
xlab = ""
)
z <- matrix(data = rep(x = 0,
times = 10000
),
nrow = 10000,
ncol = 1)
z[1:16]
## [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
for (i in 1:10000) {
z[i, ] <- mean(sample(x = myexp,
size = 100,
replace = TRUE
)
)
}
z[1:16]
## [1] 0.8985843 0.8257281 1.0360877 0.8974596 0.9021136 1.1602104 0.9943663
## [8] 0.9416140 0.9776398 0.9918970 1.0167873 0.9001889 1.0851677 0.9955479
## [15] 0.8510772 1.0026971
describe(z)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 10000 0.99 0.1 0.98 0.99 0.1 0.62 1.39 0.77 0.18 0.08 0
hist(z, xlab = "", main = "Histogram of Sample Mean (n = 100)")
z <- matrix(data = rep(x = 0,
times = 40000
),
nrow = 10000,
ncol = 4)
n <- c(2, 6, 30, 100)
for (j in 1:4) {
for (i in 1:10000) {
z[i, j] <- mean(sample(x = myexp,
size = n[j],
replace = TRUE
)
)
}
}
colnames(z) <- c("Sample size=2", "Sample size=6", "Sample size=30", "Sample size=1000")
summary(z)
## Sample size=2 Sample size=6 Sample size=30 Sample size=1000
## Min. :0.001167 Min. :0.09649 Min. :0.3903 Min. :0.6679
## 1st Qu.:0.472370 1st Qu.:0.69211 1st Qu.:0.8633 1st Qu.:0.9198
## Median :0.818893 Median :0.93273 Median :0.9775 Median :0.9853
## Mean :0.990067 Mean :0.98872 Mean :0.9891 Mean :0.9893
## 3rd Qu.:1.342000 3rd Qu.:1.22701 3rd Qu.:1.1029 3rd Qu.:1.0554
## Max. :5.815846 Max. :3.36991 Max. :1.9892 Max. :1.3976
par(mfrow=c(3,2))
for (k in 1:4) {
hist(x = z[, k],
main = "Histogram of Sample Mean of Exponential Distrubiton",
xlim = c(0, 4),
xlab = paste0("Sample Size ", n[k], " (Column ", k, " from matrix)")
)
}
z[1:16]
## [1] 0.4129484 0.9675341 0.2373205 0.5690741 2.4523290 0.8890556 0.2504277
## [8] 0.2487795 0.4223267 1.8644084 0.5551531 1.3317931 0.9179174 1.5341162
## [15] 0.7408917 2.2423570
mu
## [1] 0.9872559
colMeans(z)
## Sample size=2 Sample size=6 Sample size=30 Sample size=1000
## 0.9900670 0.9887239 0.9891306 0.9893067
sigma
## [1] 0.9924715
apply(X = z,
MARGIN = 2,
FUN = sd)
## Sample size=2 Sample size=6 Sample size=30 Sample size=1000
## 0.70789344 0.40662069 0.18121879 0.09966068
apply(X = z,
MARGIN = 2,
FUN = sd) * c(sqrt(2),
sqrt(6),
sqrt(30),
sqrt(1000)
)
## Sample size=2 Sample size=6 Sample size=30 Sample size=1000
## 1.0011125 0.9960132 0.9925762 3.1515474
sigma
## [1] 0.9924715
Here is is a similar calculation done for sample median:
# create the matrix for storage
medians <- matrix(data = rep(x = 0,
times = 40000),
nrow = 10000,
ncol = 4)
# calculate the sample medians and store in the matrix
for (j in 1:4) {
for (i in 1:10000) {
medians[i, j] <- median(sample(x = myexp,
size = n[j],
replace = TRUE
)
)
}
}
colnames(medians) <- c("Sample size=2", "Sample size=6", "Sample size=30", "Sample size=1000") # assign column names
par(mfrow=c(3,2))
hist(x = myexp,
main = "Histogram of Exponential Distribution (lambda=1)",
xlab = ""
)
for (k in 1:4) {
hist(x = medians[, k],
main = paste0("Histogram of Sample Median (n = ", n[k], ")"),
xlim = c(0, 4),
xlab = paste0("Sample Size ", n[k], " (Column ", k, " from matrix)")
)
}
apply(X = medians, MARGIN = 2, FUN = sd)
## Sample size=2 Sample size=6 Sample size=30 Sample size=1000
## 0.69463145 0.39365384 0.17832252 0.09888932
apply(X = medians,
MARGIN = 2,
FUN = sd) * c(sqrt(2),
sqrt(6),
sqrt(30),
sqrt(1000)
)
## Sample size=2 Sample size=6 Sample size=30 Sample size=1000
## 0.9823572 0.9642511 0.9767127 3.1271548