shhh = suppressPackageStartupMessages # It's a library, so shhh!
suppressWarnings(shhh(library(ggplot2)))
suppressWarnings(shhh(library('DATA606')))          # Load the package
##
## Welcome to CUNY DATA606 Statistics and Probability for Data Analytics
## This package is designed to support this course. The text book used
## is OpenIntro Statistics, 3rd Edition. You can read this by typing
## vignette('os3') or visit www.OpenIntro.org.
##
## The getLabs() function will return a list of the labs available.
##
## The demo(package='DATA606') will list the demos that are available.
suppressWarnings(shhh(library(knitr)))

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

v1 = 65
v2 = 77
n = 25
SampleMean = (v1+v2)/2
SampleMean
## [1] 71
MarginofError = (v2-v1)/2
MarginofError
## [1] 6
DF = 25-1
t = qt(.95, DF)
t
## [1] 1.710882
SD = (MarginofError/t)*5
SD
## [1] 17.53481

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

## (a) Raina wants to use a 90% confidence interval. How large a sample should she collect?

ME = 25
SD = 250
z = qnorm(0.95)
x = ((SD*z)/ME)^2
x
## [1] 270.5543

## (b) 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.

The z-score is dependent on sample size…as Z-score rises, samplesize decreases. Luke’s sample should be larger than Raina’s.

## (c) Calculate the minimum required sample size for Luke.

ME = 25
SD = 250
z = qnorm(0.995)
s = ((SD*z)/ME)^2
s
## [1] 663.4897

# 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 difference in scores are shown below.

## (a) Is there a clear difference in the average reading and writing scores?

No big difference is apparent. Minor differences can merely be sample variability.

## (b) Are the reading and writing scores of each student independent of each other?

An individual student scores are not independent of each other category.

## (c) 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 : There is no evident difference in the average scores of students in the reading and writing exam.
HA : The difference of average in between reading and writing does NOT equal zero. ##(d) Check the conditions required to complete this test. The obersvations are independent(200 students is less than 10% of the student population) and the distrubtion is normal with no skew(sample is larger than 30 observations). ##(e) The average observed difference in scores is x ̄read

s = 200
DF = 199
SD = 8.887
DOM = -0.545
SE = SD /sqrt(s)
T  = (DOM) /(SE)
PV = pt(T,df=DF)
PV
## [1] 0.1934182

The P-value fails to be smaller than 5%. We reject the alternative Hypothesis in favor of the null.

## (f) What type of error might we have made? Explain what the error means in the context of the application.

A type II error could have been made. A false negative where there may have been differences in scores that we failed to observe.

## (g) Based on the results of this hypothesis test, would you expect a confidence interval for the average differences between the reading and writing scores to include 0? Explain your reasoning.

We have failed to reject the null hypothesis which included the value of 0, so we would expect a confidence interval to include 0.

# 5.32 Fuel efciency of manual and automatic cars, Part I. Each year the US Environ- mental Protection Agency (EPA) releases fuel economy data on cars manufactured in that year. Below are summary statistics on fuel e

H0 : The data provides no difference between the average fuel efficiency of cars with manual and automatic transmissions in terms of their average city mileage?
HA : The data provides differences between the average fuel efficiency of cars with manual and automatic transmissions in terms of their average city mileage?

S = 26
AMean = 16.12
MMean = 19.85
ASD = 3.58
MSD = 4.51
DOM = AMean - MMean
SE = sqrt((ASD^2/x)+(MSD^2/S))
T = (DOM)/(SE)
P = pt(T,df=25)*2
P
## [1] 0.0003880563

p-value is less than 0.05. We reject H0 in favor of HA. Which means we’ve detected a difference in automatic vs manual car fuel efficiency.

# 5.48 Work hours and education. The General Social Survey collects data on demographics, education, and work, among many other characteristics of US residents.47 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.

## (a) Write hypotheses for evaluating whether the average number of hours worked varies across the five groups.

H0 : The difference of ALL averages is equal
HA : There is one average that is not equal to the other ones.

## (b) Check conditions and describe any assumptions you must make to proceed with the test.

Observations independent of each other. Samples are 10% or less of the population. Sample sizes greater than 30 to prevent too much skew.

## (c) Below is part of the output associated with this test. Fill in the empty cells.

K = 5
N = 1172
MSG = 501.54
SSE = 267382
P = 0.0682

# Df
DFG = K-1
DFE = N-K
DFT = DFG + DFE
DF = c(DFG, DFE, DFT)

# Sum Sq
SSG = DFG * MSG
SST = SSG + SSE
SS = c(SSG, SSE, SST)

# Mean Sq
MSE = SSE / DFE
MS = c(MSG, MSE, "")

# F-value
FV = MSG / MSE

# Table
table1 = data.frame(DF, SS, MS, c(FV, "", ""), c(P, "",""))
colnames(table1) = c("Df", "Sum Sq", "Mean Sq", "F Value", "Pr(>F)")
rownames(table1) = c("degree", "Residuals", "Total")

table1
##             Df    Sum Sq         Mean Sq          F Value Pr(>F)
## degree       4   2006.16          501.54 2.18899245274551 0.0682
## Residuals 1167 267382.00 229.11910882605
## Total     1171 269388.16

## (d) What is the conclusion of the test?

Since the p-value is higher than 0.05 we fail to reject the Null hypothesis. There is no significant difference between the 5 groups.

# Appendix

shhh = suppressPackageStartupMessages # It's a library, so shhh!
suppressWarnings(shhh(library(ggplot2)))
suppressWarnings(shhh(library(knitr)))
v1 = 65
v2 = 77
n = 25
SampleMean = (v1+v2)/2
SampleMean
MarginofError = (v2-v1)/2
MarginofError
DF = 25-1
t = qt(.95, DF)
t

SD = (MarginofError/t)*5
SD
ME = 25
SD = 250
z = qnorm(0.95)
x = ((SD*z)/ME)^2
x
ME = 25
SD = 250
z = qnorm(0.995)
s = ((SD*z)/ME)^2
s
s = 200
DF = 199
SD = 8.887
DOM = -0.545
SE = SD /sqrt(s)
T  = (DOM) /(SE)
PV = pt(T,df=DF)
PV
S = 26
AMean = 16.12
MMean = 19.85
ASD = 3.58
MSD = 4.51
DOM = AMean - MMean
SE = sqrt((ASD^2/x)+(MSD^2/S))
T = (DOM)/(SE)
P = pt(T,df=25)*2
P
K = 5
N = 1172
MSG = 501.54
SSE = 267382
P = 0.0682

# Df
DFG = K-1
DFE = N-K
DFT = DFG + DFE
DF = c(DFG, DFE, DFT)

# Sum Sq
SSG = DFG * MSG
SST = SSG + SSE
SS = c(SSG, SSE, SST)

# Mean Sq
MSE = SSE / DFE
MS = c(MSG, MSE, "")

# F-value
FV = MSG / MSE

# Table
table1 = data.frame(DF, SS, MS, c(FV, "", ""), c(P, "",""))
colnames(table1) = c("Df", "Sum Sq", "Mean Sq", "F Value", "Pr(>F)")
rownames(table1) = c("degree", "Residuals", "Total")

table1