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)
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)
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)
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)
sample() Functionset.seed(5891)
sample(1:4, size = 10, replace = TRUE)
## [1] 4 2 1 4 1 2 3 1 3 4
set.seed(712)
sample(10:20, size = 8, replace = TRUE)
## [1] 15 20 14 11 14 15 12 13
for Loopfor 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"
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
\[H_0: E[X] = 35 g \\ H_1: E[X] \neq 35 g\]
F_emp <- chocopie$weight - mean(chocopie$weight) + 35
mean(F_emp)
## [1] 35
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)
x_bar = mean(chocopie$weight)
x_bar
## [1] 35.10026
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
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])\)
Our p-value was 0.065 which is greater than \(\alpha = 0.05\); therefore, we fail to reject \(H_0\).
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.
\[H_0: E[X] = 6.5 cm \\ H_1: E[X] \neq 6.5 cm\]
F_emp <- chocopie$diameter - mean(chocopie$diameter) + 6.5
mean(F_emp)
## [1] 6.5
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)
x_bar = mean(chocopie$diameter)
x_bar
## [1] 6.55702
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
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])\)
Our p-value was 0.008 which is less than \(\alpha = 0.01\); therefore, we reject \(H_0\).
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.