library(tidyverse)
library(tidymodels)
library(knitr)
Lab 03: Coffee grades
Inference for simple linear regression using mathematical models
Due:
- Friday, September 22, 11:59pm (Tuesday labs)
- Sunday, September 24, 11:59pm (Thursday labs)
Introduction
In today’s lab you will analyze data from over 1,000 different coffees to explore the relationship between a coffee’s aroma and its flavor grade.
Learning goals
By the end of the lab you will…
- be able to use mathematical models to conduct inference for the slope
- be able to assess conditions for simple linear regression
Getting started
Go to the sta210-fa23 organization on GitHub. Click on the repo with the prefix lab-03. It contains the starter documents you need to complete the lab.
Clone the repo and start a new project in RStudio. See the Lab 01 instructions for details on cloning a repo, starting a new project in R, and configuring git.
Packages
The following packages are used in the lab.
Data: Coffee grades
The dataset for this lab comes from the Coffee Quality Database and was obtained from the #TidyTuesday GitHub repo. It includes information about the origin, producer, measures of various characteristics, and the quality measure for over 1,000 coffees. The coffees can be reasonably be treated as a random sample.
This lab will focus on the following variables:
aroma
: Aroma grade, 0 (worst aroma) - 10 (best aroma)flavor
: Flavor grade, 0 (worst flavor) - 10 (best flavor)
Click here for the definitions of all variables in the data set. Click here for more details about how these measures are obtained.
<- read_csv("data/coffee-grades.csv") coffee
Exercises
Make sure to do the following as you complete the assignment:
- Write all code and narrative in your Quarto file. We should be able to read all your code in the rendered PDF.
- Write all narrative in complete sentences.
- Use informative axis titles and labels on all graphs.
- Implement version control in your reproducible workflow. This means throughout the assignment, you should periodically render your Quarto document to produce the updated PDF, commit the changes in the Git pane, and push the updated files to GitHub. You should aim to push changes to GitHub at least three times as you work on the assignment.
Goal: The goal of this analysis is to use linear regression to understand variability in coffee flavor grades based on the aroma grade.
Exercise 1
Visualize the relationship between the aroma and flavor grades. Write two observations from the plot.
Exercise 2
Fit the linear model using aroma grade to understand variability in the flavor grade. Neatly display the model using three digits and include the 95% confidence interval for the model coefficients in the output.
Exercise 3
Interpret the slope in the context of the data.
Assume you are a coffee drinker. Would you drink a coffee represented by the intercept? Why or why not?
Exercise 4
Input the model name in the code below to calculate the regression standard error, \(\hat{\sigma}_\epsilon\). State the definition of this value in the context of the data.
glance(____)$sigma
Exercise 5
Do the data provide evidence of a statistically significant linear relationship between aroma and flavor grades? Conduct a hypothesis test using mathematical models to answer this question. In your response
- State the null and alternative hypotheses in words and in mathematical notation.
- What is the test statistic? State what the test statistic means in the context of this problem.
- What distribution was used to calculate the p-value? Be specific.
- State the conclusion in the context of the data using a threshold of \(\alpha = 0.05\) to make your decision.
Exercise 6
What is the critical value used to calculate the 95% confidence interval displayed in Exercise 2? Show the code and output used to get your response.
Is the confidence interval consistent with the conclusions from the hypothesis test? Briefly explain why or why not.
Exercise 7
Calculate the 95% confidence interval for the mean flavor grade for coffees with aroma grade of 7.5. Interpret this value in the context of the data.
One coffee produced by the Ethiopia Commodity Exchange has an aroma of 7.5. Calculate the the 95% prediction interval for the flavor grade for this coffee. Interpret this value in the context of the data.
How do the predicted values compare? How do the intervals compare? If there are differences in the predictions and/or intervals, briefly explain why.
Exercise 8
We’d like to check the model conditions to assess the reliability of the inferential results. To do so, we will create a data frame called coffee_aug
that includes the residuals and predicted values from the model. Input the name of your model in the code below.
#|eval: false
<- augment(_____$fit) coffee_aug
Make a scatterplot of the residuals (.resid
) vs. fitted values (.fitted
). Use geom_hline()
to add a horizontal dotted line at \(residuals = 0\).
The linearity condition is satisfied if there is random scatter of the residuals (no distinguishable pattern or structure) in the plot of residuals vs. fitted values.
The constant variance condition is satisfied if the vertical spread of the residuals is relatively across the plot.
Is the linearity condition satisfied? Briefly explain why or why not.
Is the constant variance condition satisfied? Briefly explain why or why not.
Exercise 9
The normality condition is satisfied if the distribution of the residuals is approximately normal. This condition can be relaxed if the sample size is sufficiently large \((n > 30)\).
Make a histogram or density plot of the residuals (.resid
). Is the normality condition satisfied? Briefly explain why or why not.
Exercise 10
The independence condition means that knowing one residual will not provide information about another. We often check this by assessing whether the observations are independent based on what we know about the subject matter and how the data were collected.
Is the independence condition satisfied? Briefly explain why or why not.
Submission
In this class, the final PDF documents will be submitted to Gradescope.
Before you wrap up the assignment, make sure all documents are updated on your GitHub repo. We will be checking these to make sure you have been practicing how to commit and push changes.
Remember – you must turn in a PDF file to the Gradescope page before the submission deadline for full credit.
To submit your assignment:
- Go to http://www.gradescope.com and click Log in in the top right corner.
- Click School Credentials -> Duke NetID and log in using your NetID credentials.
- Click on your STA 210 course.
- Click on the assignment, and you’ll be prompted to submit it.
- Mark the pages associated with each exercise. All of the pages of your lab should be associated with at least one question (i.e., should be “checked”).
- Select the first page of your .PDF submission to be associated with the “Workflow & formatting” & “Reproducible report” sections.
Grading (50 pts)
Component | Points |
---|---|
Ex 1 | 4 |
Ex 2 | 3 |
Ex 3 | 4 |
Ex 4 | 3 |
Ex 5 | 8 |
Ex 6 | 5 |
Ex 7 | 8 |
Ex 8 | 6 |
Ex 9 | 4 |
Ex 10 | 2 |
Workflow & formatting | 31 |
Footnotes
The “Workflow & formatting” grade is to assess the reproducible workflow and document format. This includes having at least 3 informative commit messages, a neatly organized document with readable code and your name and the date in the YAML.↩︎