R Script Problem (Wrong Solutions)
R Script Problem (Wrong Solutions)
40057291
Question 1 (3 pts.) Given the population set of scores {3,4,5,6,7,8,9}, use base R
graphics or ggplot to generate a bar graph representing the sampling distribution of
mean for samples of N = 4. Assume sampling is one at a time with replacement.
Give your graph a descriptive title, and be sure to include appropriate labels for the
yand x-axes.
Input:
scores <- c(3, 4, 5, 6, 7, 8, 9)
N <- 4
num_samples <- 1000
samp.dist.m <- data.frame(expand.grid(rep(list(scores), N)))
samp.dist.m <- samp.dist.m[, c(2, 1)]
colnames(samp.dist.m) <- c('score.1', 'score.2')
samp.dist.m$mean <- rowMeans(samp.dist.m)
t <- table(round(samp.dist.m$mean, 2))
print(t)
require(latex2exp)
pct <- as.numeric(t) / sum(t)
xlab <- names(t)
df <- data.frame(xlab, pct)
p <- ggplot(data = df, aes(x = xlab, y = pct)) +
geom_bar(stat = "identity", fill = 'skyblue3') +
labs(x = TeX("$\\bar{\\textit{X}}$"),
y = TeX("$\\textit{p}(\\bar{\\textit{X}})$"),
title = "Sampling distribution of means (n=4)") +
theme_classic() +
scale_y_continuous(breaks = seq(0, 0.24, .04)) +
theme(axis.title.x = element_text(margin =
unit(c(3.5,0,0,0), "mm"), size = 11),
axis.title.y = element_text(margin =
unit(c(0,3.5,0,0), "mm"), size = 11),
axis.text = element_text(size = 9.5))
ggsave(filename = "hw6plot.png", plot = p, width = 5, height =
4, units = "in", dpi = 400)
Output:
Quintus Zhou Psyc 278 L01
40057291
Question 2 (2 pts.) A professor has been teaching statistics for many years. His
records show that the overall mean for final exam scores is 73, with a standard
deviation of 11. The professor believes that this year’s class is superior to his
previous ones. The mean for final exam scores for this year’s class of 68 students is
76. Using 𝛼 = 0.051-tail, state your conclusion and report Zobt and the associated
p-value.
Input:
N <- 68
Mu <- 73
Sigma <- 11
Xbar.Obt <- 76
alpha <- 0.05
Xbar.Obt.SE <- Sigma / sqrt(N)
Zobt <- (Xbar.Obt - Mu) / Xbar.Obt.SE
Zcrit <- qnorm(alpha, lower.tail = FALSE)
reject_null <- Zobt > Zcrit
p_value <- (pnorm(Zobt, lower.tail = FALSE)
print(round(Zobt, 2))
print(round(Zcrit, 2))
Quintus Zhou Psyc 278 L01
40057291
print(round(p_value, 2))
Output:
Since the absolute value of Zobt (2.25) is greater than the critical Z-value (1.64) and
the p=0.01, we reject the null hypothesis. There is sufficient evidence to conclude
that this year's class performs superior to previous ones in terms of final exam
scores.
a. If he tests 25 cars, what is the power to detect a mean increase of 2.0 miles per
gallon? Report your answer to exactly four decimal places. (1 pt.)
Input:
x1 <- 24.7
s <- 4.8
n <- 25
alpha <- 0.051
mu_real <- 2.0
x2 <- x1 + mu_real
sem <- s / sqrt(n)
zcrit <- qnorm(p = alpha, lower.tail = TRUE)
xcrit <- x1 + zcrit * sem
zobt <- (x2 - x1) / sem
power_a <- pnorm(zobt - zcrit, lower.tail = TRUE)
print(round(power_a, 4))
Output:
0.9999
Input:
n <- 75
Quintus Zhou Psyc 278 L01
40057291
sem <- s / sqrt(n)
xcrit <- x1 + zcrit * sem
zobt <- (x2 - x1) / sem
power_b <- pnorm(zobt - zcrit, lower.tail = TRUE)
print(round(power_b, 4))
Output:
1.0000
c. Again using only 25 cars, what is the probability the manufacturer would make a
type-II error rate if there was a mean increase of 2.0 miles per gallon? Report your
answer to exactly four decimal places. (1 pt.)
Input:
beta <- 1 - power_a
print(round(beta, 4))
Output:
1e-04
d. How many cars should he use if he wants to have a 95% chance of detecting a
mean increase of 2.0 miles per gallon? Use 𝛼 = 0.051-tail and report your answer to
the nearest integer that satisfies the power requirement. (1 pt.)