5.6 Working backwards, Part II. 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.
# We input the confidence interval and calculate the mean and margin of error
Q6.up <- 77
Q6.lo <- 65
Q6.me <- (Q6.up - Q6.lo) / 2
Q6.xbar <- Q6.up - Q6.me

# We calculate the degrees of freedom and t-score
Q6.n <- 25
Q6.df <- Q6.n - 1
Q6.t <- qt(.05, df = Q6.df)

# We calculate the standard deviation
Q6.sd <- (Q6.up - Q6.xbar) * sqrt(Q6.n) / Q6.t

cat(" Sample mean: ", Q6.xbar, "\n", "Margin of error: ", Q6.me, "\n", "Sample SD: ", round(abs(Q6.sd), 3))
##  Sample mean:  71 
##  Margin of error:  6 
##  Sample SD:  17.535
5.14 SAT scores. 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?
# We input the z-score, sd, and mean
Q7a.z <- 1.645
Q7.sd <- 250
Q7.me <- 25

# We calculate sample size based on the above inputs
Q7a.n <- ((Q7a.z * Q7.sd) / Q7.me)^2

cat(" Raina's target sample size: ", round(Q7a.n))
##  Raina's target sample size:  271
  1. 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.

It should be larger, as the 99% confidence interval is intended to be more precise and so requires more observation for stability.

  1. Calculate the minimum required sample size for Luke.
# We input the z-score for 99% CI and use same sd / mean 
Q7b.z <- 2.576

# We calculate sample size based on the above inputs
Q7b.n <- ((Q7b.z * Q7.sd) / Q7.me)^2

cat(" Luke's target sample size: ", round(Q7b.n))
##  Luke's target sample size:  664
5.20 High School and Beyond, Part I. 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 differences in scores are shown below.
  1. Is there a clear difference in the average reading and writing scores?

Yes, a visual check reveals that the mean of the reading score is around 49, and that of the writing score is around 55.

  1. Are the reading and writing scores of each student independent of each other?

In all likelihood, the reading and writing scores for a given senior are probably not independent, but the scores across the sample of students are.

  1. 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?

H0: Q20.xbar.read - Q20.xbar.write = 0

HA: Q20.xbar.read - Q20.xbar.write != 0

  1. Check the conditions required to complete this test.

The sample is random, probably comprises less than 10% of the population, there is independence between groups, and the distribution is nearly normal - based on these assumptions, this seems to meet the necessary conditions.

  1. The average observed difference in scores is xread-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?

No they do not. The p-value exceeds .05, so we cannot reject the null hypothesis that there is no difference between average reading and writing scores.

# We input sample size, degress of freedom, difference in sample means, and standard deviation
Q20.n <- 200
Q20.df <- Q20.n - 1
Q20.xbar.diff <- -.545
Q20.sd <- 8.887

# We calculate standard error, t-score, and p-value
Q20.se <- Q20.sd / sqrt(Q20.n)
Q20.t <- (Q20.xbar.diff - 0) / Q20.se
Q20.pt <- pt(Q20.t, df = Q20.df)
cat(" P-value: ", Q20.pt)
##  P-value:  0.1934182
  1. What type of error might we have made? Explain what the error means in the context of the application.

We did not reject the null hypothesis with the above p-value, so there is a risk we commited a Type II error i.e. did not recognize a meaningful difference in the averages of the reading and writing scores.

  1. 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.

Yes - in fact, that was our H0.

5.32 Fuel efficiency of manual and automatic cars, Part I. 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.

Assuming conditions for inference met…

H0: Q32.xbar.auto - Q32.xbar.man = 0

HA: Q32.xbar.auto - Q32.xbar.man != 0

# We input the mean and standard deviation for manual and automatic transmissions, as well as the samples size and degrees of freedom (which is the same size for both)
Q32.xbar.man <- 19.85
Q32.sd.man <- 4.51
Q32.xbar.auto <- 16.12
Q32.sd.auto <- 3.58
Q32.n <- 26
Q32.df.auto <- Q32.n - 1

# We calculate the difference in sample means
Q32.xbar.diff <- Q32.xbar.auto - Q32.xbar.man

# We calculcate the standard error in the point estimate
Q32.se <- sqrt((Q32.sd.auto^2 / Q32.n)+(Q32.sd.man^2 / Q32.n))

# We calculcate the t-value and p-value
Q32.t <- ((Q32.xbar.diff) - 0) / Q32.se
Q32.pt <- pt(Q32.t, df = Q32.df.auto)

# We multiply by 2 as it's a two-sided test
cat(" P-value: ", 2 * Q32.pt)
##  P-value:  0.002883615

**As the p-value is less than .05, we reject the null hypothesis that there is no meaningful difference between the mean fuel efficiency of manul and automatic transmissions.

5.48 Work hours and education. 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.

H0: Q48.xbar.lhs = Q48.xbar.hs = Q48.xbar.jrc = Q48.xbar.bch = Q48.xbar.grd

HA: At least one Q48.xbar != to others

  1. Check conditions and describe any assumptions you must make to proceed with the test.

We’ll assume the survey is random and probably comprises less than 10% of the population, providing independence within and between groups. Distributions for each group appear symmetrical and likely normal, with similar variability.

  1. Below is part of the output associated with this test. Fill in the empty cells.
# We compose the data table
Q48.1 <- c(38.67, 39.6, 41.39, 42.55, 40.85)
Q48.2 <- c(15.81, 14.97, 18.1, 13.62, 15.51)
Q48.3 <- c(121, 546, 97, 253, 155)
Q48 <- data.frame(Q48.1, Q48.2, Q48.3)
colnames(Q48) <- c("xbar", "sd", "n")
row.names(Q48) <- c("lhs", "hs", "jrc", "bch", "grd")
Q48
##      xbar    sd   n
## lhs 38.67 15.81 121
## hs  39.60 14.97 546
## jrc 41.39 18.10  97
## bch 42.55 13.62 253
## grd 40.85 15.51 155
# We input the mean square, sum of square residuals, and ...?
Q48.msg <- 501.54
Q48.sse <- 267382
Q48.p <- .0682

# We calculate sample size and number of groups
Q48.n <- sum(Q48$n)
Q48.k <- nrow(Q48)

# We calculate degrees of freedom
Q48.dfg <- Q48.k - 1
Q48.dfe <- Q48.n - Q48.k
Q48.dft <- Q48.dfg + Q48.dfe

# We have sse, so we calculate the sums of squares between groups and in total
Q48.ssg <- Q48.dfg * Q48.msg
Q48.sst <- Q48.ssg + Q48.sse

# We have msg, so we calculate the mean square error 
Q48.mse <- Q48.sse / Q48.dfe

# We calculate the f statistic and p-value
Q48.f <- Q48.msg / Q48.mse

# Tabularize the statistics
Q48.anova <- data.frame(c(Q48.dfg, Q48.dfe, Q48.dft), c(Q48.ssg, Q48.sse, Q48.sst), c(Q48.msg, Q48.mse, ""), c(Q48.f, "", ""), c(Q48.p, "", ""))
colnames(Q48.anova) <- c("Df", "Sum Sq", "Mean Sq", "F value", "Pr>F")
rownames(Q48.anova) <- c("degree", "Residuals", "Total")
tbl_df(Q48.anova)
## # A tibble: 3 x 5
##      Df `Sum Sq` `Mean Sq`       `F value`        `Pr>F`
## * <dbl>    <dbl> <fct>           <fct>            <fct> 
## 1    4.    2006. 501.54          2.18899245274551 0.0682
## 2 1167.  267382. 229.11910882605 ""               ""    
## 3 1171.  269388. ""              ""               ""
  1. What is the conclusion of the test?

Since the p-value is greater than .05 (for the 95% confidence level), we fail to reject the null hypothesis i.e. that the means of the groups are the same.