1. A 90% confidence interval for a population mean is (65, 77). The population distribution is approximately normal and the population standard deviation is unknown. This confidence interval is based on a simple random sample of 25 observations. Calculate the sample mean, the margin of error, and the sample standard deviation.
n <- 25
x1 <- 65
x2 <- 77

# Sample mean is 71
(x2 + x1) / 2
## [1] 71
# Margin of error is 6
ME <- (x2 - x1) /2
ME
## [1] 6
# Sample standard deviation is 17.53
degree_of_freedom <- n - 1
t_value <- qt(0.95, degree_of_freedom)
round((ME / t_value) * sqrt(n), 2)
## [1] 17.53
  1. SAT scores of students at an Ivy League college are distributed with a standard deviation of 250 points. Two statistics students, Raina and Luke, want to estimate the average SAT score of students at this college as part of a class project. They want their margin of error to be no more than 25 points.
  1. Raina wants to use a 90% confidence interval. How large a sample should she collect?
  2. Luke wants to use a 99% confidence interval. Without calculating the actual sample size, determine whether his sample should be larger or smaller than Raina’s, and explain your reasoning.
  3. Calculate the minimum required sample size for Luke.
#a She should collect a sample size of 271
z <- 1.645 
ME <- 25
sd <- 250
round(((z * sd) / ME) ^ 2) # using the margin of error formula to solve for n
## [1] 271
#b Luke's sample should be larger than Raina's because it would require a higher z number multiplied by the standard deviation and then squared.

#c The minimum required sample size for Luke is 663
z <-    2.575
ME <- 25
sd <- 250
round(((z * sd) / ME) ^ 2)
## [1] 663
  1. The National Center of Education Statistics conducted a survey of high school seniors, collecting test data on reading, writing, and several other subjects. Here we examine a simple random sample of 200 students from this survey. Side-by-side box plots of reading and writing scores as well as a histogram of the di???erences in scores are shown below.
  1. Is there a clear difference in the average reading and writing scores?
  2. Are the reading and writing scores of each student independent of each other?
  3. Create hypotheses appropriate for the following research question: is there an evident difference in the average scores of students in the reading and writing exam?
  4. Check the conditions required to complete this test.
  5. The average observed difference in scores is x_read - x_write = -0.545, and the standard deviation of the differences is 8.887 points. Do these data provide convincing evidence of a difference between the average scores on the two exams?
  6. What type of error might we have made? Explain what the error means in the context of the application.
  7. Based on the results of this hypothesis test, would you expect a confidence interval for the average difference between the reading and writing scores to include 0? Explain your reasoning.
#a Yes, there is no clear difference in the average reading and writing scores because the distribution is symmetrical.
#b Yes, the reading and writing scores of each student are independent of each other. 
#c Null Hypothesis: difference in the average scores of students in the reading and writing exam is zeo.
#- Alternate Hypothesis: difference in the average scores of students in the reading and writing exam is not zero.
#d The following conditions are met: 
#- independent withing and between groups
#- sample of size at least 30
#- the distribution is symmetric
#- less than 10% of all student
#e Since the p-value is more than 0.05, we fail to reject the null hypothesis i.e. there is no mean difference. 
n <- 200
observed_difference <- -0.545
degree_of_freedom <- n - 1
sd_difference <- 8.887
SE <- sd_difference / sqrt(n)
t_value <- (observed_difference - 0) / SE
pt(t_value, degree_of_freedom)
## [1] 0.1934182
#f We might have made a Type II error because we did NOT reject the null hypothesis. In this context, we should have noticed that we had convincing data that there is a difference in the reading and writing average scores.
#g Since we fail to reject the null hpytehsis, we would expect a confidence interval for the average difference between the reading and writing scores to include 0.
  1. Each year the US Environmental Protection Agency (EPA) releases fuel economy data on cars manufactured in that year. Below are summary statistics on fuel efficiency (in miles/gallon) from random samples of cars with manual and automatic transmissions manufactured in 2012. Do these data provide strong evidence of a difference between the average fuel efficiency of cars with manual and automatic transmissions in terms of their average city mileage? Assume that conditions for inference are satisfied.
# Null Hypothesis: difference between the average fuel efficiency of cars with manual and automatic transmissions is zero
# Alternate Hypothesis: difference between the average fuel efficiency of cars with manual and automatic transmissions is not zero
# Since p-value is less than 0.05, we reject the null hypothesis i.e. there is a difference between the average fuel efficiency of cars with manual and automatic transmissions in terms of their average city mileage.
n <- 26
degree_of_freedom <- n - 1
sd_automatic <- 3.58
sd_manual <- 4.51
mean_automatic <- 16.12
mean_manual <- 19.85
observed_difference <- mean_manual - mean_automatic
sd_observed_difference <- sqrt(((sd_automatic)^2/n) + ((sd_manual)^2/n))
t_value <- (observed_difference - 0) / sd_observed_difference
2 * pt(t_value, degree_of_freedom, lower.tail = FALSE)
## [1] 0.002883615
  1. The General Social Survey collects data on demographics, education, and work, among many other characteristics of US residents. Using ANOVA, we can consider educational attainment levels for all 1,172 respondents at once. Below are the distributions of hours worked by educational attainment and relevant summary statistics that will be helpful in carrying out this analysis.
  1. Write hypotheses for evaluating whether the average number of hours worked varies across the five groups.
  2. Check conditions and describe any assumptions you must make to proceed with the test.
  3. Below is part of the output associated with this test. Fill in the empty cells.
  4. What is the conclusion of the test?
#a Null Hypothesis: The difference of all averages is equal
#- Alternate Hypothesis: There is at least one average that is not equal
#b The following conditions are met: 
#- independent between and within groups
#- random sample of less than 10% of population
#- each group is nearly normalized. boxplot verifies this
#- the variablity across groups should be equal. standard deviation for each group is quite similar.
#c 
mean <- c(38.67, 39.6, 41.39, 42.55, 40.85)
sd <- c(15.81, 14.97, 18.1, 13.62, 15.51)
n <- c(121, 546, 97, 253, 155)
educational_attainment <- data.frame(mean, sd, n)
total_n <- sum(educational_attainment$n)
k <- length(educational_attainment$mean)
degree_of_freedom <- k - 1
df_residual <- total_n - k
MSG <- 501.54
SSE <- 267382
df_total <- degree_of_freedom + df_residual
Prf <- 0.0682
f_statistic <- qf( 1 - Prf, degree_of_freedom , df_residual)
MSE <- MSG / f_statistic
SSG <- degree_of_freedom * MSG
SST <- SSG + SSE
answers <- data.frame(c(degree_of_freedom, df_residual, df_total), 
           c(SSG, SSE, SST),
           c(MSG, MSE, NA),
           c(f_statistic, NA, NA),
           c(Prf, NA, NA))
colnames(answers) <- c("Df", "Sum Sq", "Mean Sq", "F Value", "Pr(>F)")
rownames(answers) <- c("degree", "Residuals", "Total")
answers
##             Df    Sum Sq  Mean Sq  F Value Pr(>F)
## degree       4   2006.16 501.5400 2.188931 0.0682
## Residuals 1167 267382.00 229.1255       NA     NA
## Total     1171 269388.16       NA       NA     NA
#d Since the p-value is more than 0.05, we fail to reject the null hypothesis i.e. the mean difference between groups are equal.