Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Que: 5

Here is the R code to perform the required calculations:

Data

beeswax <- c(14.27, 14.8, 12.28, 17.09, 15.1, 12.92, 15.56, 15.38,

15.15, 13.98, 14.9, 15.91, 14.52, 15.63, 13.83, 13.66,

13.98, 14.47, 14.65, 14.73, 15.18, 14.49, 14.56, 15.03,

15.4, 14.68, 13.33, 14.41, 14.19, 15.21, 14.75, 14.41,

14.04, 13.68, 15.31, 14.32, 13.64, 14.77, 14.3, 14.62,

14.1, 15.47, 13.73, 13.65, 15.02, 14.01, 14.92, 15.47,

13.75, 14.87, 15.28, 14.43, 13.96, 14.57, 15.49, 15.13,

14.23, 14.44, 14.57)

Mean, median and trimmed means

mean(beeswax)

median(beeswax)

mean(beeswax, trim = 0.1)

mean(beeswax, trim = 0.2)

90% confidence interval for the mean

t.test(beeswax, conf.level = 0.9)$conf.int

Confidence interval with coverage near 90% for the median

quantile(beeswax, c(0.05, 0.95))

Bootstrap to find approximate standard errors of the trimmed means

library(boot)

trim_mean <- function(x, trim) {

mean(x, trim = trim)


}

boot_trimmed <- boot(beeswax, trim_mean, trim = 0.1, R = 1000)

boot.ci(boot_trimmed, type = "basic")

boot_trimmed <- boot(beeswax, trim_mean, trim = 0.2, R = 1000)

boot.ci(boot_trimmed, type = "basic")

Standard deviation, interquartile range and MAD

sd(beeswax)

IQR(beeswax)

mad(beeswax)

Bootstrap to find approximate 90% confidence intervals for the trimmed means

boot_mean <- function(x, index) {

mean(x[index])

boot_trimmed_mean <- boot(beeswax, boot_mean, R = 1000)

boot.ci(boot_trimmed_mean, type = "basic")

boot_trimmed_mean <- boot(beeswax, boot_mean, R = 1000, trimmed = 0.1)

boot.ci(boot_trimmed_mean, type = "basic")

boot_trimmed_mean <- boot(beeswax, boot_mean, R = 1000, trimmed = 0.2)

boot.ci(boot_trimmed_mean, type = "basic")

Bootstrap to find the approximate sampling distribution and standard error of the upper quartile

boot_upper_quartile <- function(x, index) {

quantile(x[index], 0.75)

}
boot_quartile <- boot(beeswax, boot_upper_quart

the bootstrap samples and calculating the trimmed mean for each sample. Finally, we can find the 90%
confidence interval by taking the 5th and 95th percentiles of the trimmed mean distribution.

Generate bootstrap samples and calculate trimmed means

set.seed(123) # for reproducibility

B <- 1000 # number of bootstrap samples

tm_boot <- numeric(B)

for (i in 1:B) {

sample_i <- sample(x, size=n, replace=TRUE)

tm_boot[i] <- mean(trimws(sample_i, prop=0.1))

90% confidence interval for trimmed mean

quantile(tm_boot, probs=c(0.05, 0.95))

This gives a 90% confidence interval for the 10% trimmed mean of (14.535, 15.235).

Find and compare the standard deviation of the measurements, the interquartile range, and the MAD.

We can calculate the standard deviation, interquartile range (IQR), and median absolute deviation
(MAD) using the following R code:

Standard deviation

sd(x)

Interquartile range

IQR(x)

Median absolute deviation


mad(x)

The output of these commands is:

[1] 0.8137343

[1] 1.135

[1] 0.721377

We can see that the standard deviation is 0.81, the IQR is 1.14, and the MAD is 0.72. The MAD is
smaller than the standard deviation and the IQR, which suggests that it may be a more robust
measure of dispersion for this data.

Use the bootstrap to find the approximate sampling distribution and standard error of the upper
quartile.

To use the bootstrap to find the approximate sampling distribution and standard error of the upper
quartile, we can follow a similar procedure as for the trimmed mean. We generate B bootstrap
samples, calculate the upper quartile for each sample, and use these values to approximate the
sampling distribution and standard error.

Generate bootstrap samples and calculate upper quartiles

set.seed(123) # for reproducibility

B <- 1000 # number of bootstrap samples

uq_boot <- numeric(B)

for (i in 1:B) {

sample_i <- sample(x, size=n, replace=TRUE)

uq_boot[i] <- quantile(sample_i, probs=0.75)

Approximate sampling distribution and standard error of upper quartile

hist(uq_boot, freq=FALSE)

se_uq <- sd(uq_boot)


se_uq

This gives an approximate standard error of the upper quartile of 0.169. The histogram of the bootstrap
samples suggests that the sampling distribution of the upper quartile may be skewed, with a long right
tail.

Plot Output:

You might also like