load the package
library(DATA606)
##
## 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.
##
## Attaching package: 'DATA606'
## The following object is masked from 'package:utils':
##
## demo
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.3.3
library(knitr)
5.6.1 One-sample means with the t-distribution
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.
df <- 25-1
paste("Df =", df)
## [1] "Df = 24"
# For the two-sided test, a 90% confidence interval is equivalent to significance level 0.1. In T table, which is equivalent to 95% confidence interval for the one-sided test.
Tscore <- qt(.95, df)
paste("T score =", round(Tscore,2))
## [1] "T score = 1.71"
sample mean
Xe <- (65+77)/2
paste("sample mean =", Xe)
## [1] "sample mean = 71"
margin of error
ME <-77-71
paste("margin of error =", ME)
## [1] "margin of error = 6"
sample standard deviation
sd <- (6/1.71)*sqrt(24)
paste("sd =", round(sd,2))
## [1] "sd = 17.19"
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.
# SAT scores of students Riana is going to sample from this college are independent. Assuming the distribution of the SAT scores is nearly normal, we can use t-distribution.
# SAT scores that Riana is going to sample from this college are independent. Assuming the distribution of the SAT scores is nearly normal, we can use t-distribution.
# t_star*SE <= 25.
# t_star*250/sqrt(n) <= 25
# n>= 100*(t_star)^2
# For two-sided test and sample between 31 and unlimited, when alpha = 0.1, t_star = 1.65 ~1.70.
# n = 100*(t_star)^2
t_starmin<- 1.65
n_min <- 100*(t_starmin)^2
t_starmax = 1.70
n_max <- 100*(t_starmax)^2
x <- c(n_min,n_max)
paste("If Raina wants to use a 90% confidence interval, she must at least collect a sample with size at", round(x[which.min(x)]+0.5), "SAT scores.")
## [1] "If Raina wants to use a 90% confidence interval, she must at least collect a sample with size at 273 SAT scores."
# When n increases, t_star decreases. So n is at least 273.
# If Luke wants 99% confident interval, T score will increase. If he wants to keep the margin error to be the same as what Raina uses, he must use larger sample size because margin error = t_star*sd/sqrt(n) . So if t_star increases, n will increase to make sure the margin error to be the same.
# Because for two-sided test for sample between 31 and unlimited, when alpha = 0.01, t_star = 2.58~2.74.
t_starmin2 <- 2.58
n_min2 <- 100*(t_starmin2)^2
t_starmax2 = 2.74
n_max2 <- 100*(t_starmax2)^2
x2 <- c(n_min2,n_max2)
paste("Beacause n >= 100*(t_star)^2 ,the minimum required sample size for Luke would be", round(x2[which.min(x2)]+0.5))
## [1] "Beacause n >= 100*(t_star)^2 ,the minimum required sample size for Luke would be 666"
5.6.2 Paired data
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.
# The median of writing score is a little bit higher than the reading score but overall there is almost no difference because their medians are so close to each other.
# I would say a student's ability to read and the reading level will influence his/her ability to write and how good of his/her writing is. The reading and writing scores of each student are dependent of each other.
# NULL hypothesis: there is no difference in the average scores of students in the reading and writing exam. H_0: mu_reading - mu_writing = 0
# Alternative hypothesis: there is difference in the average scores of students in the reading and writing exam. H_A: mu_reading - mu_writing != 0
# Conditions for the hypotheses test are:
# The observations are based on a simple random sample (200 high school seniors) from less than 10% of the students of high school seniors.
# The distribution is normal as shown in the figure.
# So we can use t-distribution to do the test.
df <- 200-1
# for two-sided test and alpha = 0.05, confident power is 95%, which is equivalent to one-sided test with alpha = 0.025 and confident power 97.5%
t_star <- qt(.975, df)
SE <- 8.887/sqrt(200)
# The rejection region in the NULL distribution is at ±t_star*SE
margion_error <- c(-t_star * SE, +t_star *SE)
# T_score for lower tail area in the alternative distribution:
mean.diff <- -.545
T <- (mean.diff-0)/SE
pVal <- pt(T, df=199)
paste("t_star =", t_star)
## [1] "t_star = 1.97195654425175"
paste("SE =", SE)
## [1] "SE = 0.628405796440485"
if(pVal > 0.05){
paste("p-Value =", round(pVal,2), ", which is larger than 0.05. So there is no strong evidence to reject H0.")
}else
{
paste("p-Value =", round(pVal,2), ", which is smaller than 0.05. So there is strong evidence to reject H0 and there is difference between the average scores on the two exams.")
}
## [1] "p-Value = 0.19 , which is larger than 0.05. So there is no strong evidence to reject H0."
# Because we did not reject NULL hypothesis, we might have made type 2 error. It means when there is difference between reading score and writing score, we failed to reject the hypothesis that there is no difference between reading score and writing score.
# with 95% confident
a <- mean.diff - t_star * SE
b <- mean.diff + t_star * SE
confidence_interval <- c(a,b)
paste("Because we failed to reject H0, which means there is no difference between reading score and writing score, we would be 95% confident to see a confidence interval to include 0. The confidence interval for the average difference between the reading and writing scores are", round(a, 2), "~", round(b,2))
## [1] "Because we failed to reject H0, which means there is no difference between reading score and writing score, we would be 95% confident to see a confidence interval to include 0. The confidence interval for the average difference between the reading and writing scores are -1.78 ~ 0.69"
5.6.3 Difference of two means
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.
# NULL hypothesis: there is no difference between the average fuel efficiency of cars with manual and automatic transmissions in terms of their average city mileage. H_0: mu_man - mu_aut = 0
# Alternative hypothesis: there is difference between the average fuel efficiency of cars with manual and automatic transmissions in terms of their average city mileage. H_A: mu_man - mu_aut != 0
# Assume that conditions for inference are satisfied, t-distribution could be used to make inference using the point estimate: x_man - x_aut.
x_man <- 19.85
x_aut <- 16.12
x_diff <- x_man - x_aut
sd_man <- 4.51
sd_aut <- 3.58
n_man <- 26
n_aut <- 26
SE <- sqrt(sd_man^2/n_man + sd_aut^2/n_aut)
T2 <- (x_diff-0)/SE
pVal2 <- 2*pt(-T2, df=26-1)
if(pVal2 > 0.05){
paste("p-Value =", round(pVal2,4), "> 0.05", ", there is no strong evidence to reject H0.")
}else{
paste("p-Value =", round(pVal2,4), "< 0.05", ", so we reject H0. There is difference between the average fuel efficiency of cars with manual and automatic transmissions in terms of their average city mileage.")
}
## [1] "p-Value = 0.0029 < 0.05 , so we reject H0. There is difference between the average fuel efficiency of cars with manual and automatic transmissions in terms of their average city mileage."
5.6.4 Power calculations for a difference of means
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.
# H_0: The average hours worked is identical across the five groups. Any observed difference is due to chance. mu_L = mu_H = mu_J = mu_B = mu_G
# H_A: The average hours worked varies by educational attainment levels.
# It seems reasonable to suppose that the samples are independent within and across the groups since the respondents are randomly selected and samples are less than % of population.
# According to the box plot, the bachelor's distribution is skewed and has quite significant outliers. The data in other 4 groups are nearly normal.
# According to the box plot, the variability is similar in groups except the bachelor's group which is deviates. Based on the table, the standard deviation is similar between groups except the Jr Coll group stands out as deviating.
df.question <- data.frame()
e <- c(NA, NA, NA)
f<- c(NA, 267382, NA)
g <- c(501.54, NA,NA)
h <- c(NA, NA, NA)
i <- c(0.0682,NA,NA)
df.question <- data.frame(Df=e, Sum_Sq = f, Mean_Sq= g, F_value = h, "Pr>f" = i)
row.names(df.question) <- c("degree", "Residuals", "Total")
kable(df.question)
Df | Sum_Sq | Mean_Sq | F_value | Pr.f | |
---|---|---|---|---|---|
degree | NA | NA | 501.54 | NA | 0.0682 |
Residuals | NA | 267382 | NA | NA | NA |
Total | NA | NA | NA | NA | NA |
Perform ANOVA analysis
n <- 1172
k <- 5
dfG <- k - 1
dfE <- n-k
Total <- dfG + dfE
meanTotal <- 40.45
dfData <- data.frame(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))
# Compute the SSG
SSG <- sum(dfData$n *(dfData$mean - meanTotal)^2 )
# Compute the MSG
MSG <- SSG/dfG
# Compute the SSE
SSE <- sum((dfData$n-1)*(dfData$sd)^2)
# Compute the MSE
MSE <- SSE/dfE
# Compute the F statistic
F <- MSG /MSE
e1 <- c(dfG, dfE, Total)
f1<- c(SSG, 267382, SSG+267382)
g1 <- c(501.54, MSE,NA)
h1 <- c(round(F,2), NA, NA)
i1 <- c(0.0682,NA,NA)
df.answer <- data.frame(Df=e1, Sum_Sq = f1, Mean_Sq= g1, F_value = h1, "Pr>f" = i1)
row.names(df.answer) <- c("degree", "Residuals", "Total")
kable(df.answer)
Df | Sum_Sq | Mean_Sq | F_value | Pr.f | |
---|---|---|---|---|---|
degree | 4 | 2004.101 | 501.540 | 2.19 | 0.0682 |
Residuals | 1167 | 267382.000 | 229.112 | NA | NA |
Total | 1171 | 269386.101 | NA | NA | NA |
# The p-value is greater than 0.05, so we cannot reject the NA hypothesis and there is no difference between the groups.