Lab - 3 - Template 2
Lab - 3 - Template 2
STATS 13 – Kim
Rishit Jain 506244202
Winter 2025
Table of Contents
Logical Data and Subsetting ...................................................................................... 2
Question 1 (With Your TA)....................................................................................... 2
Question 2 (On Your Own) ...................................................................................... 2
Question 3 (On Your Own) ...................................................................................... 2
Question 4 (On Your Own) ...................................................................................... 3
The sample() Function............................................................................................. 3
Question 5 (On Your Own) ...................................................................................... 3
Question 6 (On Your Own) ...................................................................................... 3
The for Loop ............................................................................................................ 4
Question 7 (On Your Own) ...................................................................................... 4
Question 8 (On Your Own) ...................................................................................... 4
Hypothesis Testing with Non-Parametric Bootstrap ..................................................... 4
Question 9 (With Your TA)....................................................................................... 4
Question 10 (With Your TA) ..................................................................................... 4
Question 11 (With Your TA) ..................................................................................... 5
Question 12 (With Your TA) ..................................................................................... 5
Question 13 (With Your TA) ..................................................................................... 6
Question 14 (With Your TA) ..................................................................................... 6
Question 15 (On Your Own) .................................................................................... 6
Question 16 (On Your Own) .................................................................................... 6
On Your Own ............................................................................................................ 6
Question 17 (On Your Own) .................................................................................... 6
Question 18 (On Your Own) .................................................................................... 7
Question 19 (On Your Own) .................................................................................... 7
Question 20 (On Your Own) .................................................................................... 8
Question 21 (On Your Own) .................................................................................... 8
Question 22 (On Your Own) .................................................................................... 9
Question 23 (On Your Own) .................................................................................... 9
Question 24 (On Your Own) .................................................................................... 9
## [1] 1 1 2 3 3 2 4 3 4 2
## [1] 23 19 14 27 22 26 20
The for Loop
Question 7 (On Your Own)
(1 point) Use a for loop to print your full name three times.
# Type your CODE in here
for(i in 1:3) {
print("Rishit Jain")
}
## [1] 27
## [1] 64
## [1] 125
## [1] 216
## [1] 343
## [1] 512
## [1] 35
Question 11 (With Your TA)
(1 point) Using a loop and the sample() function like the example above, create 𝑋emp . Set
𝑀 = 1000, and use 7947 as a seed. Print a histogram of this sampling distribution.
# Type your CODE in here
M <- 1000
n <- length(chocopie$weight)
X_emp_weight <- numeric(M)
set.seed(7947)
for(i in 1:M) {
bootstrap_sample_weight <- sample(F_emp_weight, size = n, replace = TRUE)
X_emp_weight[i] <- mean(bootstrap_sample_weight)
}
library(mosaic)
histogram(X_emp_weight)
## [1] 35.10026
Question 13 (With Your TA)
(1 point) Calculate and print the 𝑝-value.
# Type your CODE in here
d <- abs(35 - x_obs_weight)
upper_ex <- 35 + d
lower_ex <- 35 - d
## [1] 0.061
## 2.5% 97.5%
## 34.88867 35.10209
(−∞, 34.88867]⋃[35.10209, ∞)
Since our p-value is greater than 0.05, we fail to reject the null hypothesis at 𝛼= 0.05.
On Your Own
Question 17 (On Your Own)
(1 point) State the null and alternative hypotheses. Write it in $\LaTeX$ code.
$$ H_0: E[X] = 6.5 \\ H_1: E[X] \neq 6.6 $$
Question 18 (On Your Own)
(1 point) Use the re-centering technique to construct 𝐹emp such that it obeys 𝐻0 . Print the
mean to verify 𝐻0 is true.
# Type your CODE in here
F_emp_diameter <- chocopie$diameter - mean(chocopie$diameter) + 6.5
mean(F_emp_diameter)
## [1] 6.5
set.seed(5820)
for (i in 1:M_diam) {
bootstrap_sample_diameter <- sample(F_emp_diameter, size = n_diam, replace
= TRUE)
X_emp_diameter[i] <- mean(bootstrap_sample_diameter)
}
histogram(X_emp_diameter)
Question 20 (On Your Own)
(1 point) Calculate and print 𝑥obs .
# Type your CODE in here
x_obs_diameter <- mean(chocopie$diameter)
x_obs_diameter
## [1] 6.55702
## [1] 0.012
Question 22 (On Your Own)
(1 point) Calculate and print the critical values and state the critical regions. Write your
regions using $\LaTeX$ code.
# Type your CODE in here
critical_vals_diam <- quantile(X_emp_diameter, probs = c(0.005, 0.995))
critical_vals_diam
## 0.5% 99.5%
## 6.447072 6.562284
(−∞, 6.447072]⋃[6.562284, ∞)