| Category |
Assignment |
Subject |
Computer Science |
| University |
University of Otago |
Module Title |
STAT 260 Visualisation and Modelling in R |
STAT260 Assignment 2
Question 1 [1 mark]
Call the data into R. To ensure that the variables Sex and Colour are automatically converted to factors, you need to include the option stringsAsFactors = TRUE when you do so. Have a look at the first few rows of data using the head function and familiarise yourself with the data.
Answer:
setwd("C:/Users/10784/Desktop/STAT260")
donkey <- read.csv("donkey.csv", stringsAsFactors = TRUE)
donkey <- read.csv("donkey.csv", stringsAsFactors = TRUE)
Question 2 [3 marks]
Plot the body weight (y-axis) against the heart girth (x-axis). Comment on what the plot indicates about the relationship between body weight and heart girth. Does this suggest that heart girth could be used reliably as a proxy or substitute for body weight?
Answer:
plot(donkey$Heartgirth, donkey$Bodywt,
main = "Body Weight vs Heart Girth",
xlab = "Heart Girth (cm)",
ylab = "Body Weight (kg)",
pch = 16)
There is a strong positive relationship between heart girth and body weight. This suggests heart girth could be used as a reliable proxy for body weight since, as heart girth increases, body weight increases predictably.
Question 3 [6 marks]
Plot body weight (y-axis) against length (x-axis). Provide a second plot that also has body weight (y-axis) against length (x-axis), but plots the points with a different character for females and males. Ensure that you include a legend. Comment on what the two plots indicate about the relationship between body weight and length.
Answer:
# Plot 1: Body weight vs length
plot(donkey$Length, donkey$Bodywt,
main = "Body Weight vs Length",
xlab = "Length (cm)",
ylab = "Body Weight (kg)",
pch = 16, col = "red")
# Plot 2: With different symbols for Sex
plot(donkey$Length, donkey$Bodywt,
main = "Body Weight vs Length by Sex",
xlab = "Length (cm)",
ylab = "Body Weight (kg)",
pch = ifelse(donkey$Sex == "Male", 1, 3),
col = "black")
legend("topleft", legend = c("Male", "Female"), pch = c(1, 3))
Both Plot Both plot show a positive relationship between body weight and length. The second plot shows that males tend to be both longer and heavier than females on average, but the male and female donkeys tend to follow a
similar trend.
Question 4 [4 marks]
Plot the girth at the umbilicus (y-axis) against the age (x-axis). Include information about the colour of a donkey on the plot by having a different colour for each of the three categories. Comment on what the plot shows.
Answer:
# Assign colours based on Colour variable
colours <- as.numeric(donkey$Colour)
# Plot umbilicus girth vs age with different colours
plot(donkey$Age, donkey$Umbgirth,
col = colours,
pch = 16,
main = "Umbilicus Girth vs Age by Colour",
xlab = "Age (years)",
ylab = "Umbilicus Girth (cm)")
legend("topleft", legend = levels(donkey$Colour), col = 1:3, pch = 16)
There is a general increase in umbilical girth with age. The plot shows that the brown and grey donkeys appear to dominate the higher girth.
Question 5 [4 marks]
The built-in R dataset ChickWeight (Cool, more chickens!), which you can access directly from the R prompt, has information from an experiment on the effect of diet on early growth of chicks (you can find more information about the dataset with help(ChickWeight)). We are
interested in visualising the relationship between chick weight and time. We are particularly interested in how this relationship might vary based on the diet the chick has. Produce a plot that shows this.
Answer
# Load ChickWeight and plot weight vs time, coloured by diet
library(datasets)
plot(ChickWeight$Time, ChickWeight$Weight,
col = as.numeric(ChickWeight$Diet),
pch = 16,
main = "Chick Weight Over Time by Diet",
xlab = "Time (days)",)
ylab = "Weight (grams)")
legend("topleft", legend = levels(ChickWeight$Diet),
col = 1:4, pch = 16, title = "Diet")
Chick weight increases over time, but the rate of growth varies by diet. Diet 3 and 4 show steeper increases, indicating faster growth. Visualising all diets together highlights their differing effects across time.