# R code on page 2
mu <- 2.8571
n <- 20
set.seed(1234)
set1 <- 4*matrix(data=rbeta(n = 1000*n, shape1 = 5, shape2 = 2), nrow = 1000, ncol = n)
means <- apply(X = set1, MARGIN = 1, FUN = mean)
SDs <- apply(X = set1, MARGIN = 1, FUN = sd)
lower <- means - qt(p=.975, df = 20 - 1) * SDs/sqrt(n)
upper <- means + qt(p=.975, df = 20 - 1) * SDs/sqrt(n)
count <- sum(lower < mu & upper > mu)
count
count/1000
## R code for the more skewed distribution
mu <- 3.2787
n <- 20
N <- 10000
set.seed(1111)
set1 <- 4*matrix(data=rbeta(n = N*n, shape1 = 5, shape2 = 1.1), nrow = N, ncol = n)
# R code on page 4, and page 5 for power
# sample size
n <- 30
# actual population mean
# according to the given hypothesis this has to be less than
# mu_a
pop_mean <- 2
# actual population standard deviation
pop_sd <- sqrt(0.8)
# value of mu_0 for the null hypothesis
mu_0 <- 2.4
# effect size
effect.size = abs(mu_0 - pop_mean)
# difference in means
diff.in.means <- pop_mean - mu_0
# significance level
alpha <- 0.01
# calculate power using power.t.test
power.t.test(n = n, d = effect.size, sd = pop_sd,
sig.level = alpha, type = "one.sample", alternative = "one.sided")
# critical value
crit_Val <- qt(p = 0.99, df = n - 1)
# power is P(T < -t_(n-1,alpha))
1 - pt(q = crit_Val, df = n-1, ncp = sqrt(n) *effect.size / pop_sd)
# confirm the above power using simulations
# simulate N samples of size n each
# number of simulated samples
N <- 10000
# set a seed for reproducibility
set.seed(4167)
set1 <- set1 <- 4*matrix(data=rbeta(n = N*n, shape1 = 2, shape2 = 2), nrow = N, ncol =
n)
# get the mean of each sample
means <- apply(X= set1, MARGIN = 1, FUN = mean)
# get the SD of each sample
SDs <- apply(X = set1, MARGIN = 1, FUN = sd)
# get the test statistic for each sample
tstat <- ((means - mu_0)/(SDs/sqrt(n)))
# sum how many times we got an abs value of test statistic less than critical value
sum(abs(tstat) > abs(crit_Val))
# divide this by the number of simulations to get the power
sum(abs(tstat) > abs(crit_Val))/N