Logical Data and Subsetting

Question 1 (With Your TA)

(1 point) Read in the chocopie.csv file into an object called chocopie. Print out the first 6 rows and verify it matches the lab manual.

chocopie <- read.csv("chocopie.csv")
head(chocopie)

Question 2 (On Your Own)

(1 point) Save a subset of the chocopie dataset that contains only data with taste ratings of 4 or more. Save this into an object called chocopie_subset_1 and print the first 6 rows.

chocopie_subset_1 <- chocopie[chocopie$taste >= 4, ]
head(chocopie_subset_1)

Question 3 (On Your Own)

(1 point) Save a subset of the chocopie dataset that contains only data with taste ratings exactly 2 and weights of 34 or less. Save this into an object called chocopie_subset_2 and print the first 6 rows.

taste_and_weight_index <- chocopie$taste == 2 & chocopie$weight <= 34
chocopie_subset_2 <- chocopie[taste_and_weight_index, ]
head(chocopie_subset_2)

Question 4 (On Your Own)

(1 point) Save a subset of the chocopie dataset that contains only data with taste ratings of 3 or taste ratings of 5. Save this into an object called chocopie_subset_3 and print the first 6 rows.

three_five_taste_index <- chocopie$taste == 3 | chocopie$taste == 5
chocopie_subset_3 <- chocopie[three_five_taste_index, ]
head(chocopie_subset_3)

The sample() Function

Question 5 (On Your Own)

(1 point) Set the seed to 5891 and draw 10 samples from the numbers 1 through 4, each having equal probability.

set.seed(5891)
sample(1:4, size = 10, replace = TRUE)
##  [1] 4 2 1 4 1 2 3 1 3 4

Question 6 (On Your Own)

(1 point) Set the seed to 712 and draw 8 samples from the numbers 10 through 20, each having equal probability.

set.seed(712)
sample(10:20, size = 8, replace = TRUE)
## [1] 15 20 14 11 14 15 12 13

The for Loop

Question 7 (On Your Own)

(1 point) Use a for loop to print your full name three times.

for(i in 1:3) {
print("Nathan Vinh Tong")
}
## [1] "Nathan Vinh Tong"
## [1] "Nathan Vinh Tong"
## [1] "Nathan Vinh Tong"

Question 8 (On Your Own)

(1 point) Use a for loop to print the calculation of \((x + 1)^4\) for the numbers 3 through 5.

for(i in 3:5) {
print((i + 1)^4)
}
## [1] 256
## [1] 625
## [1] 1296

Hypothesis Testing with Non-Parametric Bootstrap

Question 9 (With Your TA)

(1 point) State the null and alternative hypotheses. Write it in \(\LaTeX\) code.

\[H_0: E[X] = 35 g \\ H_1: E[X] \neq 35 g\]

Question 10 (With Your TA)

(1 point) Use the re-centering technique to construct \(F_{\text{emp}}\) such that it obeys \(H_0\). Print the mean to verify \(H_0\) is true.

F_emp <-  chocopie$weight - mean(chocopie$weight) + 35
mean(F_emp)
## [1] 35

Question 11 (With Your TA)

(1 point) Using a loop and the sample() function like the example above, create \(\overline{X}_{\text{emp}}\). Set \(M = 1000\), and use 3194 as a seed. Print a histogram of this sampling distribution.

M <- 1000 # number of bootstrap samples
n <- nrow(chocopie) # number of observations in sample

set.seed(3194)

X_bar_emp <- numeric(M) # preallocate results vector

for (i in 1:M) {
  # 1. Construct bootstrap sample
  bootstrap_sample <- sample(F_emp, n, replace = TRUE)
  
  # 2. calculate bootstrap mean
  X_bar_emp[i] <- mean(bootstrap_sample)
}

histogram(X_bar_emp)

Question 12 (With Your TA)

(1 point) Calculate and print \(\overline{x}_{\text{obs}}\).

x_bar = mean(chocopie$weight)
x_bar
## [1] 35.10026

Question 13 (With Your TA)

(1 point) Calculate and print the \(p\)-value.

d <- x_bar - 35 # distance from text-statistic to H_0
upper_ex <- 35 + d
lower_ex <- 35 - d

# Count number of samples in these regions
upper_sum <- sum(X_bar_emp >= upper_ex)
lower_sum <- sum(X_bar_emp <= lower_ex)

# Calculate proportion
pvalue <- (upper_sum + lower_sum)/M
pvalue
## [1] 0.065

Question 14 (With Your TA)

(1 point) Calculate and print the critical values and state the critical regions. Write your regions using \(\LaTeX\) code (your TA will teach you).

alpha <- 0.05
crit_val <- quantile(X_bar_emp, probs = c(alpha / 2, 1 - alpha / 2))
crit_val
##     2.5%    97.5% 
## 34.88835 35.10875

Our critical region is \((-\infty,34.888]\cup[35.109,\infty])\)

Question 15 (On Your Own)

(1 point) What is the decision? You may use any scale to make your decision.

Our p-value was 0.065 which is greater than \(\alpha = 0.05\); therefore, we fail to reject \(H_0\).

Question 16 (On Your Own)

(1 point) What is the conclusion?

Since we fail to reject \(H_0\), we cannot prove that the mean weight of Professor Kim’s chocopies are different from what is advertised.

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 cm \\ H_1: E[X] \neq 6.5 cm\]

Question 18 (On Your Own)

(1 point) Use the re-centering technique to construct \(F_{\text{emp}}\) such that it obeys \(H_0\). Print the mean to verify \(H_0\) is true.

F_emp <- chocopie$diameter - mean(chocopie$diameter) + 6.5
mean(F_emp)
## [1] 6.5

Question 19 (On Your Own)

(1 point) Using a loop and the sample() function like the example above, create \(\overline{X}_{\text{emp}}\). Set \(M = 1000\), and use 7845 as a seed. Print a histogram of this sampling distribution.

M <- 1000 # number of bootstrap samples
n <- nrow(chocopie) # number of observations in sample

set.seed(7845)

X_bar_emp <- numeric(M) # preallocate results vector

for (i in 1:M) {
  # 1. Construct bootstrap sample
  bootstrap_sample <- sample(F_emp, n, replace = TRUE)
  
  # 2. calculate bootstrap mean
  X_bar_emp[i] <- mean(bootstrap_sample)
}

histogram(X_bar_emp)

Question 20 (On Your Own)

(1 point) Calculate and print \(\overline{x}_{\text{obs}}\).

x_bar = mean(chocopie$diameter)
x_bar
## [1] 6.55702

Question 21 (On Your Own)

(1 point) Calculate and print the \(p\)-value.

d <- x_bar - 6.5 # distance from text-statistic to H_0
upper_ex <- 6.5 + d
lower_ex <- 6.5 - d

# Count number of samples in these regions
upper_sum <- sum(X_bar_emp >= upper_ex)
lower_sum <- sum(X_bar_emp <= lower_ex)

# Calculate proportion
pvalue <- (upper_sum + lower_sum)/M
pvalue
## [1] 0.008

Question 22 (On Your Own)

(1 point) Calculate and print the critical values and state the critical regions. Write your regions using \(\LaTeX\) code.

alpha <- 0.01
crit_val <- quantile(X_bar_emp, probs = c(alpha / 2, 1 - alpha / 2))
crit_val
##     0.5%    99.5% 
## 6.443965 6.553090

Our critical region is \((-\infty,6.444]\cup[6.553,\infty])\)

Question 23 (On Your Own)

(1 point) What is the decision? You may use any scale to make your decision.

Our p-value was 0.008 which is less than \(\alpha = 0.01\); therefore, we reject \(H_0\).

Question 24 (On Your Own)

(1 point) What is the conclusion?

Since we reject \(H_0\), we can say that the average diameter of Professor Kim’s chocopies are not the same as what is advertised.