0% found this document useful (0 votes)
19 views9 pages

Lab - 3 - Template 2

The document outlines Lab 3 for STATS 13, focusing on data manipulation and hypothesis testing using R. It includes various questions related to logical data subsetting, the sample() function, for loops, and non-parametric bootstrap methods. Each section contains specific tasks to be completed, with code snippets and expected outputs provided.

Uploaded by

rishit jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views9 pages

Lab - 3 - Template 2

The document outlines Lab 3 for STATS 13, focusing on data manipulation and hypothesis testing using R. It includes various questions related to logical data subsetting, the sample() function, for loops, and non-parametric bootstrap methods. Each section contains specific tasks to be completed, with code snippets and expected outputs provided.

Uploaded by

rishit jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Lab 3

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

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.
# Type your CODE in here
chocopie <- read.csv("chocopie.csv")
head(chocopie)

## flavor weight diameter taste


## 1 Green Tea 33.52 7.22 3
## 2 Green Tea 36.76 6.24 4
## 3 Chocolate 34.63 6.08 3
## 4 Chocolate 33.52 6.99 2
## 5 Milk Tea 35.78 7.35 5
## 6 Green Tea 34.49 6.99 3

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.
# Type your CODE in here
chocopie_subset_1 <- chocopie[chocopie$taste >= 4, ]
head(chocopie_subset_1)

## flavor weight diameter taste


## 2 Green Tea 36.76 6.24 4
## 5 Milk Tea 35.78 7.35 5
## 7 Milk Tea 35.90 7.20 5
## 8 Milk Tea 33.47 5.71 5
## 9 Milk Tea 35.85 6.16 5
## 11 Green Tea 36.49 6.16 4

Question 3 (On Your Own)


(1 point) Save a subset of the chocopie dataset that contains only data with taste ratings
exactly 3 and weights of 35 or less. Save this into an object called chocopie_subset_2 and
print the first 6 rows.
# Type your CODE in here
chocopie_subset_2 <- chocopie[chocopie$taste == 3 & chocopie$weight <= 35, ]
head(chocopie_subset_2)
## flavor weight diameter taste
## 1 Green Tea 33.52 7.22 3
## 3 Chocolate 34.63 6.08 3
## 6 Green Tea 34.49 6.99 3
## 26 Green Tea 33.97 6.50 3
## 32 Chocolate 33.92 5.74 3
## 33 Chocolate 33.57 6.37 3

Question 4 (On Your Own)


(1 point) Save a subset of the chocopie dataset that contains only data with taste ratings of
2 or taste ratings of 4. Save this into an object called chocopie_subset_3 and print the first
6 rows.
# Type your CODE in here
chocopie_subset_3 <- chocopie[chocopie$taste == 2 | chocopie$taste == 4, ]
head(chocopie_subset_3)

## flavor weight diameter taste


## 2 Green Tea 36.76 6.24 4
## 4 Chocolate 33.52 6.99 2
## 10 Chocolate 34.12 6.06 2
## 11 Green Tea 36.49 6.16 4
## 15 Chocolate 35.42 7.14 2
## 18 Green Tea 36.23 6.38 4

The sample() Function


Question 5 (On Your Own)
(1 point) Set the seed to 1464 and draw 10 samples from the numbers 1 through 4, each
having equal probability.
# Type your CODE in here
set.seed(1464)
sample(1:4, size = 10, replace = TRUE)

## [1] 1 1 2 3 3 2 4 3 4 2

Question 6 (On Your Own)


(1 point) Set the seed to 8535 and draw 7 samples from the numbers 10 through 30, each
having equal probability.
# Type your CODE in here
set.seed(8535)
sample(10:30, size = 7, replace = TRUE)

## [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] "Rishit Jain"


## [1] "Rishit Jain"
## [1] "Rishit Jain"

Question 8 (On Your Own)


(1 point) Use a for loop to print the calculation of (𝑥 − 2)3 for the numbers 5 through 10.
# Type your CODE in here
for (x in 5:10) {
val <- (x - 2)^3
print(val)
}

## [1] 27
## [1] 64
## [1] 125
## [1] 216
## [1] 343
## [1] 512

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

Question 10 (With Your TA)


(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_weight <- chocopie$weight - mean(chocopie$weight) + 35
mean(F_emp_weight)

## [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)

Question 12 (With Your TA)


(1 point) Calculate and print 𝑥obs .
# Type your CODE in here
x_obs_weight <- mean(chocopie$weight)
x_obs_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

upper_sum <- sum(X_emp_weight >= upper_ex)


lower_sum <- sum(X_emp_weight <= lower_ex)

p_value_weight <- (upper_sum + lower_sum) / M


p_value_weight

## [1] 0.061

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).
# Type your CODE in here
critical_vals_weight <- quantile(X_emp_weight, probs = c(0.025, 0.975))
critical_vals_weight

## 2.5% 97.5%
## 34.88867 35.10209

(−∞, 34.88867]⋃[35.10209, ∞)

Question 15 (On Your Own)


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

Since our p-value is greater than 0.05, we fail to reject the null hypothesis at 𝛼= 0.05.

Question 16 (On Your Own)


(1 point) What is the conclusion?
We do not have sufficient evidence to conclude that the mean weight of the chocopies is
different from 35 grams.

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

Question 19 (On Your Own)


(1 point) Using a loop and the sample() function like the example above, create 𝑋emp . Set
𝑀 = 1000, and use 5820 as a seed. Print a histogram of this sampling distribution.
# Type your CODE in here
M_diam <- 1000
n_diam <- length(chocopie$diameter)
X_emp_diameter <- numeric(M_diam)

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

Question 21 (On Your Own)


(1 point) Calculate and print the 𝑝-value.
# Type your CODE in here
d_diam <- abs(6.5 - x_obs_diameter)
upper_ex_diam <- 6.5 + d_diam
lower_ex_diam <- 6.5 - d_diam

upper_sum_diam <- sum(X_emp_diameter >= upper_ex_diam)


lower_sum_diam <- sum(X_emp_diameter <= lower_ex_diam)

p_value_diam <- (upper_sum_diam + lower_sum_diam) / M_diam


p_value_diam

## [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, ∞)

Question 23 (On Your Own)


(1 point) What is the decision? You may use any scale to make your decision.
Since the p-value exceeds 0.01, we fail to reject the null hypothesis at 𝛼= 0.01

Question 24 (On Your Own)


(1 point) What is the conclusion?
We do not have sufficient evidence at the 1% significance level to conclude that the mean
diameter differs from 6.5 cm.

You might also like