Below are the solutions to these exercises on Statistical testing and sampling

############### # # # Exercise 1 # # # ############### set.seed(123) lifespans <- data.frame(index = 1:200, lifespans = rgamma(200, shape = 2, rate = 1.2)*50) library(ggplot2) library(dplyr) lifespans %>% ggplot(aes(x = lifespans)) + stat_density()

############### # # # Exercise 2 # # # ############### library(MASS) mle <- fitdistr(lifespans$lifespans, "gamma", lower = 0.01) mle$loglik

## [1] -1041.934

############### # # # Exercise 3 # # # ############### mle$estimate

## shape rate ## 2.06384001 0.02704474

############### # # # Exercise 4 # # # ############### shape_se <- mle$sd[1] ############### # # # Exercise 5 # # # ############### test_stat <- (mle$estimate[1]-2)/shape_se test_stat

## shape ## 0.3329652

print(paste("|",round(test_stat,2),"|", "<|1.96", ", indicating there is no evidence at the 5% level to suggest the Shape MLE differs from 2", sep = ""))

## [1] "|0.33|<|1.96, indicating there is no evidence at the 5% level to suggest the Shape MLE differs from 2"

############### # # # Exercise 6 # # # ############### 1-pnorm(test_stat)

## shape ## 0.3695803

############### # # # Exercise 7 # # # ############### durations = c(35.8, 33.4, 34.9, 17.9, 35.6, 10.3, 14.9, 28.3, 39.2, 25.4, 23.4, 7.1, 38.9, 9.2, 8.1) var(durations)

## [1] 144.2583

############### # # # Exercise 8 # # # ############### n <- length(durations) single_sample <- sample(durations, size = n, replace = TRUE) var(single_sample)

## [1] 199.114

############### # # # Exercise 9 # # # ############### B <- 100 test_stats <- vector(length = n) for (i in 1:B){ boot_sample <- sample(durations, size = n, replace = TRUE) test_stats[i] <- mean(boot_sample) } ############### # # # Exercise 10 # # # ############### quantile(test_stats, c(0.025, 0.975))

## 2.5% 97.5% ## 19.96400 30.74583

**What's next:**

- Explore all our (>1000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below

Muhammad says

Shouldn’t the solution for Exercise 6 be (1-pnorm(test_stat))*2 since the alternative hypothesis is that the shape parameter differs from 2?

> (1-pnorm(test_stat))*2

shape

0.7391577