There are three questions. Each question contains multiple tasks. To receive a full mark in this part, you should correctly solve all tasks and add appropriate labels to your graphical summaries.
Do not modify the header of this file.
Format: All assignment tasks are marked by the
prompt Task. You should enter your
solution either as embedded R code below
Task or as text after the prompt
Answer. See examples in the Import data section.
Submission: Upon completion, you must render this
worksheet (using Knit in R Studio) into an html file and
submit the html file. Make sure the file extension “html” is in lower
case.
In this assignment, we will use a data set collected from a survey for MATH1005 students at the start of 2022 Semester 2. There are a total of 928 students enrolled in this course.
Download the data file A1MATH1005.csv in your
data folder within your MATH1062 folder. This
R Markdown file (Assignment1Worksheet.Rmd) should also be
saved under your MATH1062 folder.
Then import the csv file into a variable called
data:
### write your code here. Here is a sample solution
data = read.csv("data/A1MATH1005.csv", header = T)
### the following displays the dimenison of the data
dim(data)
## [1] 281 8
If you save the data file and the worksheet correctly, you should be able to load the data file and see that the data contain 281 observations and 8 variables.
Task: How many survey responses are
there? How many variables are there?
Answer: There are 281 survey responses
and 8 variables.
====START OF ASSIGNMENT QUESTIONS====
Task: Display the structure of
data. Then make an appropriate histogram for the variable
Height on the density scale. Here you can use the default
number of class intervals.
Hint: you may need to convert the Height variable
into the correct type in R to generate the histogram. You also need to
perform data cleaning here to get a sensible histogram for this
case.
### write your code here
###Answer:
data = read.csv("data/A1MATH1005.csv", header = T)
#data
data$Height = as.numeric(gsub("[^0-9.]", "", data$Height))
cleaned_height = data$Height[!is.na(data$Height)]
hist(cleaned_height, freq=F, right=F, xlab = "Height (cm)", ylab = "Density", main = "Height Distribution Histogram")
cleaned_height
## [1] 154.0 182.0 156.0 172.0 193.0 167.0 183.0 150.0 181.0 183.0 167.0 188.0
## [13] 164.5 176.0 178.0 176.0 160.0 158.0 176.0 175.0 153.0 174.0 183.0 160.0
## [25] 169.0 175.0 172.0 179.0 171.0 173.0 181.0 175.0 171.0 170.0 170.0 170.0
## [37] 156.0 167.0 170.0 165.0 168.0 170.0 173.0 178.0 180.0 173.0 184.0 180.0
## [49] 180.0 148.0 179.0 165.0 169.0 172.0 174.0 178.0 162.0 180.0 170.0 178.0
## [61] 156.0 186.0 171.0 144.0 166.0 180.0 189.0 178.0 180.0 183.0 183.0 181.0
## [73] 177.0 178.0 176.0 178.0 180.0 173.0 173.0 198.0 160.0 190.0 163.0 181.0
## [85] 175.0 168.0 167.0 181.0 181.0 161.0 182.0 183.0 175.0 178.0 191.0 161.0
## [97] 165.0 150.0 175.0 166.0 185.0 176.0 172.0 183.0 168.0 160.0 171.0 164.0
## [109] 187.0 189.0 164.0 165.0 173.0 170.0 183.0 163.0 162.0 177.0 183.0 172.0
## [121] 172.0 170.0 170.0 186.0 167.0 176.0 181.0 150.0 145.0 172.0 165.0 168.0
## [133] 169.0 177.0 174.0 175.0 173.0 161.0 151.0 183.0 184.0 155.0 158.0 162.0
## [145] 177.0 185.0 172.0 155.0 180.0 170.0 161.0 192.0 169.0 190.5 188.0 181.0
## [157] 191.0 184.0 172.0 175.0 163.0 171.0 180.0 175.0 162.0 164.0 192.0 162.0
## [169] 188.0 178.0 178.0 188.0 185.0 181.0 165.0 175.0 180.0 155.0 174.0 184.0
## [181] 174.0 171.0 175.0 163.0 165.0 168.0 166.0 163.0 176.0 174.0 185.0 170.0
## [193] 156.0 161.0 178.0 178.0 165.0 162.0 163.0 193.0 175.0 180.0 184.0 174.0
## [205] 176.0 158.0 180.0 181.0 181.0 178.0 164.0 181.0 168.0 178.0 164.0 169.0
## [217] 171.0 182.0 182.0 175.0 185.0 170.0 170.0 182.0 176.0 168.0 173.0 164.0
## [229] 189.0 179.0 161.5 180.0 172.0 168.0 181.0 183.0 182.0 182.0 148.0 170.0
## [241] 164.0 180.0 180.0 170.0 168.0 178.0 178.0 160.0 180.0 175.0 178.0 163.0
## [253] 164.0 167.0 183.0 185.0 181.0 180.0 186.0 178.0 183.0 173.0 186.0 167.0
## [265] 176.0 158.0 175.0 175.0 163.0 183.0 182.0 178.0 173.0 189.0 180.0 158.0
## [277] 185.4 158.0 182.0 170.0
Here our overall aim is to use sample mean and median to determine the shape of the distribution of the observed height.
Task: Calculate the sample mean and
sample median of the observed height.
Hint: you need to remove data entries with value NA,
which are missing data. One possible way is to use the function
is.na() to identify data entries with value
NA.
### write your code here
###Answer:
data = read.csv("data/A1MATH1005.csv", header = T)
data$Height = as.numeric(gsub("[^0-9.]", "", data$Height))
cleaned_height = data$Height[!is.na(data$Height)]
mean_height = mean(cleaned_height)
print(paste("Sample Mean:", mean_height))
## [1] "Sample Mean: 173.299642857143"
median_height = median(cleaned_height)
print(paste("Sample Median:", median_height))
## [1] "Sample Median: 175"
Task: Replot the histogram and use
abline to indicate the sample mean and sample median on the
histogram.
### write your code here
###Answer:
data = read.csv("data/A1MATH1005.csv", header = T)
data$Height = as.numeric(gsub("[^0-9.]", "", data$Height))
cleaned_height = data$Height[!is.na(data$Height)]
mean_height = mean(cleaned_height)
median_height = median(cleaned_height)
hist(cleaned_height, freq = FALSE, right = FALSE, xlab = "Height (cm)", ylab = "Density", main = "Height Distribution Histogram")
abline(v = mean(cleaned_height), col = "red")
abline(v = median(cleaned_height), col = "blue")
legend("topright", legend = c("Mean", "Median"), col = c("red", "blue"), lty = 2)
Task: By comparing the sample mean and
sample median, describe the shape of the data.
Answer: The data is slightly negatively
skewed as the mean is below the median. This means that the most
frequent data is concentrated on the right, providing the shape shown
above.
The overall aim of this sub-question is to check your understanding of the box plot and summarizing the spread of a data set using interquartile range.
Task: Calculate the first quartile,
third quartile, and interquartile range (IQR) of the observed
height.
### write your code here
###Answer:
data = read.csv("data/A1MATH1005.csv", header = T)
data$Height = as.numeric(gsub("[^0-9.]", "", data$Height))
cleaned_height = data$Height[!is.na(data$Height)]
mean_height = mean(cleaned_height)
#print(paste("Mean(Q2):", mean_height))
height_IQR = IQR(cleaned_height)
print(paste("IQR:", height_IQR))
## [1] "IQR: 14"
height_q1 = mean_height + (0.5 * height_IQR)
print(paste("Q1:", height_q1))
## [1] "Q1: 180.299642857143"
height_q3 = mean_height - (0.5 * height_IQR)
print(paste("Q3:", height_q3))
## [1] "Q3: 166.299642857143"
Task: Make a box plot for the observed
height, and then use abline to indicate the location of the
sample median and the interquartile range on the box plot.
### write your code here
###
data = read.csv("data/A1MATH1005.csv", header = T)
data$Height = as.numeric(gsub("[^0-9.]", "", data$Height))
cleaned_height = data$Height[!is.na(data$Height)]
mean_height = mean(cleaned_height)
median_height = median(cleaned_height)
height_IQR = IQR(cleaned_height)
height_q1 = quantile(cleaned_height, 0.25)
height_q3 = quantile(cleaned_height, 0.75)
boxplot(cleaned_height, freq = FALSE, right = FALSE, horizontal = T, xlab = "Height (cm)", main = "Height Distribution Boxplot")
abline(v = mean_height, col = "red")
abline(v = median_height, col = "blue")
abline(v = height_q1, col = "green")
abline(v = height_q3, col = "purple")
legend("topright", legend = c("Mean (Q2)", "Median", "Q1", "Q3"), col = c("red", "blue", "green", "purple"), lty = 4)
Here our goal is to understand the height differences between biological sexes using the comparative box plot.
Task: Use tools such as the frequency
table to check how many categories are in the Gender
variable and how many data points are available in each of the
categories. Then, discuss what variables should be included in the
subsequent analysis and what variables should be excluded. You should
state the reason.
You need to put your code below and your discussion below as well.
Hint: the empty gender "" indicates missing
data.
### write your code here
###
data = read.csv("data/A1MATH1005.csv", header = T)
data$Height = as.numeric(gsub("[^0-9.]", "", data$Height))
cleaned_height = data$Height[!is.na(data$Height)]
#table(data$Gender)
gender_clean = subset(data$Gender, data$Gender == "Male" | data$Gender == "Female")
table(gender_clean)
## gender_clean
## Female Male
## 111 167
#gender_male = subset(cleaned_height, data$Gender == "Male")
#gender_female = subset(cleaned_height, data$Gender == "Female")
#table(gender_male)
#gender_clean = factor(gender_clean, levels = c("Female, Male"))
#boxplot(cleaned_height ~ gender_clean, Horizontal = T)
#boxplot(gender_male, freq = FALSE, right = FALSE, horizontal = T, xlab = "Height (cm)", main = "Male Height #Distribution Boxplot")
#boxplot(gender_female, freq = FALSE, right = FALSE, horizontal = T, xlab = "Height (cm)", main = "Female #Height Distribution Boxplot")
#gender_female
Answer: (write your discussion
here)
Task: Make a comparative box plot for
the observed height by splitting it by the variable Gender
(the recorded biological sex).
Hint: after data cleaning and selection, you need to make sure the height variable and the gender variable have the same size
### write your code here
###
Task: What does the comparative box
plot reveal about the height of students for biological males and
biological females?
Answer:
====END OF THE WORKSHEET====