Midterm Exam 1 - Open Book Section
(R) - Part 2
2023-03-10
Instructions
The R Markdown/Jupyter Notebook file includes the questions, the empty code chunk sections for your code, and
the text blocks for your responses. Answer the questions below by completing the R Markdown/Jupyter Notebook
file. You may make slight adjustments to get the file to knit/convert but otherwise keep the formatting the same.
Once you’ve finished answering the questions, submit your responses in a single knitted file as HTML only.
There are 14 questions total, each worth between 3 and 5 points. Partial credit may be given if your code is
correct, but your conclusion is incorrect or vice versa.
Next Steps:
1. Save the .Rmd/.ipnyb file in your R working directory - the same directory where you will download the
“beer_new.csv” data file into. Having both files in the same directory will help in reading the “beer_new.csv”
file.
2. Read the question and create the R code necessary within the code chunk section immediately below each
question. Knitting this file will generate the output and insert it into the section below the code chunk.
3. Type your answer to the questions in the text block provided immediately after the response prompt.
4. Once you’ve finished answering all questions, knit this file and submit the knitted file as HTML on Canvas.
,Mock Example Question
This will be the exam question - each question is already copied from Canvas and inserted into individual text
blocks below. You do not need to copy/paste the questions from the online Canvas exam.
# Example code chunk area. Enter your code below the comment
Mock Response to Example Question: This is the section where you type your written answers to the question.
Depending on the question asked, your typed response may be a number, a list of variables, a few sentences, or a
combination of these elements.
Ready? Let’s begin.
Background
In this exam, you will be considering various attributes of beer and using the given data to predict its quality.
The data in “beer_new.csv” contains 1599 data points with the following 12 attributes:
1. humulone: amount of humulones which is an alpha acid and contributes to the bitterness of beer
2. lupulones: amount of lupulones which is a beta acid and contributes to the taste of beer
3. potassium: amount of potassium
4. sodium: amount of sodium
5. chlorides: amount of chlorides
6. calcium: amount of calcium
7. magnesium: amount of magnesium
8. density: density
9. pH: pH
10. sulphates: amount of sulphates
11. percent_alcohol: percent alcohol content
12. quality: score between 0 and 1000
Question 1: Correlation Between Two Specific
Predictors (4 points)
Calculate the correlation between the ‘percent_alcohol’ and ‘density’ variables in the beer_new.csv dataset.
Interpret the result.
options(warn=-1)
# Calculate the correlation between 'percent_alcohol' and 'density'
# Read the data set
beer <- read.csv("beer_new.csv", head = TRUE)
skim(beer)
Data summary
Name beer
Number of rows 1599
Number of columns 12
Column type frequency:
, numeric 12
Group variables None
Variable type: numeric
skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
humulone 0 1 8.34 1.82 4.63 7.11 7.95 9.27 16.67
lupulones 0 1 0.53 0.18 0.12 0.39 0.52 0.64 1.46
potassium 0 1 0.27 0.19 0.00 0.09 0.26 0.42 0.99
sodium 0 1 2.54 1.40 0.91 1.89 2.19 2.60 15.61
chlorides 0 1 0.09 0.05 0.01 0.07 0.08 0.09 0.59
calcium 0 1 15.89 10.53 1.01 7.27 13.69 21.51 78.99
magnesium 0 1 46.46 33.27 5.82 21.78 37.88 62.20 296.58
density 0 1 1.00 0.05 0.84 0.96 1.00 1.03 1.15
pH 0 1 3.31 0.23 2.58 3.16 3.31 3.46 4.40
sulphates 0 1 0.66 0.17 0.33 0.55 0.62 0.73 2.03
percent_alcohol 0 1 10.42 1.21 7.94 9.48 10.22 11.20 15.06
quality 0 1 563.43 93.41 236.83 497.19 556.47 621.93 963.50
arrange(desc(estimate))
## # A tibble: 66 × 5
## column1 column2 estimate n p.value
## <chr> <chr> <dbl> <int> <dbl>
## 1 magnesium calcium 0.653 1599 0
## 2 potassium humulone 0.649 1599 0
## 3 quality percent_alcohol 0.369 1599 0
## 4 sulphates chlorides 0.365 1599 0
## 5 sulphates potassium 0.314 1599 0
## 6 quality sulphates 0.250 1599 0
## 7 quality potassium 0.213 1599 0
## 8 magnesium sodium 0.200 1599 6.66e-16
## 9 chlorides potassium 0.198 1599 1.33e-15
## 10 calcium sodium 0.189 1599 2.58e-14
Answer to Question 1:
(R) - Part 2
2023-03-10
Instructions
The R Markdown/Jupyter Notebook file includes the questions, the empty code chunk sections for your code, and
the text blocks for your responses. Answer the questions below by completing the R Markdown/Jupyter Notebook
file. You may make slight adjustments to get the file to knit/convert but otherwise keep the formatting the same.
Once you’ve finished answering the questions, submit your responses in a single knitted file as HTML only.
There are 14 questions total, each worth between 3 and 5 points. Partial credit may be given if your code is
correct, but your conclusion is incorrect or vice versa.
Next Steps:
1. Save the .Rmd/.ipnyb file in your R working directory - the same directory where you will download the
“beer_new.csv” data file into. Having both files in the same directory will help in reading the “beer_new.csv”
file.
2. Read the question and create the R code necessary within the code chunk section immediately below each
question. Knitting this file will generate the output and insert it into the section below the code chunk.
3. Type your answer to the questions in the text block provided immediately after the response prompt.
4. Once you’ve finished answering all questions, knit this file and submit the knitted file as HTML on Canvas.
,Mock Example Question
This will be the exam question - each question is already copied from Canvas and inserted into individual text
blocks below. You do not need to copy/paste the questions from the online Canvas exam.
# Example code chunk area. Enter your code below the comment
Mock Response to Example Question: This is the section where you type your written answers to the question.
Depending on the question asked, your typed response may be a number, a list of variables, a few sentences, or a
combination of these elements.
Ready? Let’s begin.
Background
In this exam, you will be considering various attributes of beer and using the given data to predict its quality.
The data in “beer_new.csv” contains 1599 data points with the following 12 attributes:
1. humulone: amount of humulones which is an alpha acid and contributes to the bitterness of beer
2. lupulones: amount of lupulones which is a beta acid and contributes to the taste of beer
3. potassium: amount of potassium
4. sodium: amount of sodium
5. chlorides: amount of chlorides
6. calcium: amount of calcium
7. magnesium: amount of magnesium
8. density: density
9. pH: pH
10. sulphates: amount of sulphates
11. percent_alcohol: percent alcohol content
12. quality: score between 0 and 1000
Question 1: Correlation Between Two Specific
Predictors (4 points)
Calculate the correlation between the ‘percent_alcohol’ and ‘density’ variables in the beer_new.csv dataset.
Interpret the result.
options(warn=-1)
# Calculate the correlation between 'percent_alcohol' and 'density'
# Read the data set
beer <- read.csv("beer_new.csv", head = TRUE)
skim(beer)
Data summary
Name beer
Number of rows 1599
Number of columns 12
Column type frequency:
, numeric 12
Group variables None
Variable type: numeric
skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
humulone 0 1 8.34 1.82 4.63 7.11 7.95 9.27 16.67
lupulones 0 1 0.53 0.18 0.12 0.39 0.52 0.64 1.46
potassium 0 1 0.27 0.19 0.00 0.09 0.26 0.42 0.99
sodium 0 1 2.54 1.40 0.91 1.89 2.19 2.60 15.61
chlorides 0 1 0.09 0.05 0.01 0.07 0.08 0.09 0.59
calcium 0 1 15.89 10.53 1.01 7.27 13.69 21.51 78.99
magnesium 0 1 46.46 33.27 5.82 21.78 37.88 62.20 296.58
density 0 1 1.00 0.05 0.84 0.96 1.00 1.03 1.15
pH 0 1 3.31 0.23 2.58 3.16 3.31 3.46 4.40
sulphates 0 1 0.66 0.17 0.33 0.55 0.62 0.73 2.03
percent_alcohol 0 1 10.42 1.21 7.94 9.48 10.22 11.20 15.06
quality 0 1 563.43 93.41 236.83 497.19 556.47 621.93 963.50
arrange(desc(estimate))
## # A tibble: 66 × 5
## column1 column2 estimate n p.value
## <chr> <chr> <dbl> <int> <dbl>
## 1 magnesium calcium 0.653 1599 0
## 2 potassium humulone 0.649 1599 0
## 3 quality percent_alcohol 0.369 1599 0
## 4 sulphates chlorides 0.365 1599 0
## 5 sulphates potassium 0.314 1599 0
## 6 quality sulphates 0.250 1599 0
## 7 quality potassium 0.213 1599 0
## 8 magnesium sodium 0.200 1599 6.66e-16
## 9 chlorides potassium 0.198 1599 1.33e-15
## 10 calcium sodium 0.189 1599 2.58e-14
Answer to Question 1: