Goals for this lab.

Setup and packages

As usual, we start by loading our two packages: mosaic and ggformula. To load a package, you use the library() function, wrapped around the name of a package. I’ve put the code to load one package into the chunk below. Add the other package you need.

library(mosaic)
library(ggformula)
# put in the other package that you need here

Loading in data

We’ll load the example data, GSS_clean.csv. It is available at this Url: https://raw.githubusercontent.com/IJohnson-math/Math138/main/GSS_clean.csv. We’ll use the read.csv() function to read in the data.

#load data
GSS <- read.csv("https://raw.githubusercontent.com/IJohnson-math/Math138/main/GSS_clean.csv")

This dataset comes from the General Social Survey (GSS), which is collected by NORC at the University of Chicago. It is a random sample of households from the United States, and has been running since 1972, so it is very useful for studying trends in American life. The data I’ve given you is a subset of the questions asked in the survey, and the data has been cleaned to make it easier to use. But, there are still some messy aspects (which we’ll discover as we analyze it further throughout this class!).

Research question

Suppose we wanted to know for all U.S. workers if the mean number of hours worked in a week is different than 40. We could write our null and alternative hypotheses as

\[ H_0: \mu = 40 \\ H_a: \mu\neq 40 \]

Basic commands to view the data.

In Lab 1, we used the following commands to view parts of the GSS data: glimpse, head, tail. You can also view the data in another tab by clicking on ‘GSS’ in the Environment pane. This allows you to scroll up and down, and left and right to view the data.

You may use these commands on the GSS data to review the data.

head(GSS)
tail(GSS)
glimpse(GSS)

Inference for One Mean

In this lab we will consider the mean of number_of_hours_worked_last_week as our statistic of interest. Looking within the GSS data we see many NA values for the variable number_of_hours_worked_last_week. Let’s start by filtering out the NA values. The command filter is used to keep the observational units that satisfy a given property. In this example the property is !is.na(number_of_hours_worked_last_week); here, the exclamation point, !, is read as “not”, so this command keeps the observational units that do not have NA as an entry for the variable number_of_hours_worked_last_week.

GSS <- filter(GSS, !is.na(number_of_hours_worked_last_week))

Let’s look at the data. Create a histogram of number_of_hours_worked_last_week.

gf_histogram(~number_of_hours_worked_last_week, data=GSS, binwidth=4,  title="Time Worked Last Week by a Random Sample of 1381 US Adults", xlab="Time Worked (hours)")

Now we can compute the mean, the value of our point estimate. We name the statistic xbar (in place of the symbol \(\bar{x}\)).

xbar <- mean(~number_of_hours_worked_last_week, data=GSS)
xbar
## [1] 41.28168

Validity Conditions for a One-sample \(t\)-test

The quantitative variable should have a symmetric distribution, or you should have at least 20 observations and the sample distribution should not be strongly skewed.

When these conditions are met we can use the \(t\)-distribution to approximate the \(p\)-value for our hypothesis test. It’s important to keep in mind that these conditions are rough guidelines and not a guarantee. All theory-based methods are approximations. They will work best when the sample distribution is symmetric, the sample size is large, and there are no large outliers. When in doubt, use a simulation-based method as a cross-check.

Check Validity Conditions: In this example we have \(n=1381\) observations, which is much larger than 20, and our sample distribution is symmetric as seen above in the histogram. Thus the validity conditions for theory-based inference with one mean are satisfied.

Calculating the standardized statistic, the \(t\)-statistic

The standardized statistic, \(t\), is found using the formula \[ t = \frac{\bar{x} - \mu}{SE(\bar{x})} \]

and standard error for the null distribution is given by

\[ SE(\bar{x})=\frac{s}{\sqrt{n}}. \]

Calculate the standardized \(t\)- statistic

#calculate the standard deviation of the sample, s
s <- sd( ~number_of_hours_worked_last_week, data=GSS)

# n is the number of observational units (after filtering)
n=1381

#calculate standard error
SE <- s/sqrt(n)

#mu is the mean of the null hypotheses
mu = 40

#now we can calculate (and display) the standardized statistic
t <- (xbar - mu)/SE
t
## [1] 3.289241

We use the command t.test to calculate a \(p\)-value. As we saw in Lab 1, the options for alternative are “two.sided”, “greater”, “less” depending on the inequality in the alternative hypotheses. We must also enter the null-hypothesis parameter mu (in place of the symbol \(\mu\)).

t.test(~number_of_hours_worked_last_week,  data = GSS, alternative = "two.sided", mu=40)
## 
##  One Sample t-test
## 
## data:  number_of_hours_worked_last_week
## t = 3.2892, df = 1380, p-value = 0.00103
## alternative hypothesis: true mean is not equal to 40
## 95 percent confidence interval:
##  40.51729 42.04607
## sample estimates:
## mean of x 
##  41.28168

Conclusions

Our data is from a random sample of \(n=1381\) US workers collected through the General Social Survey. Since our sample is random, we may generalize our findings to the larger population of US workers. We consider the number of hours worked last week, a quantitative variable, and investigated whether or not the mean number of hours worked last week by US workers is equal to 40.

What can be concluded from the \(t\)-statistic and \(p\)-value?

Our statistic, the sample mean of \(\bar{x}=41.28\) hours worked last week, is 3.29 standard deviations away from the hypothesized mean of 40 hours worked last week. An observed statistic that is more than 3 standard deviations away from the hypothesized mean, as our is here, is very strong evidence against the null hypothesis. We are very unlikely to obtain a random sample of \(n=1381\) people with sample mean of \(\bar{x}\)=41.28 hours worked last week if the true population of US workers worked an average of 40 hours last week.

Similarly, the \(p\)-value of 0.00103 is very small. When a \(p\)-value is less than 0.01, as ours is here, we have very strong evidence against the null hypothesis. Thus, we will reject the null hypothesis (as it is not plausible) and accept the alternative hypothesis that mean number of hours worked by US workers is not equal to 40 hours per week.

Notice that the \(t\)-statistic and \(p\)-value give the same conclusions, as expected.

Instructions to Print Your Lab!

  • Knit to html and you should see the html file in the Files pane.
  • Open the .html file in a Web Browser window.
  • Print from the browser, BUT FIRST select the layout as 2 Pages per sheet. You may also print double-sided to save paper.
  • Bring your printed file to class to turn it in. It is due one week after the Pre-Lab.