Research Methods and Data Analysis Summative Assessment - Supplementary R Code

Long term physiological effects of catch and release practices – A study of the Caribbean reef shark, Biodiversity and Conservation, Tamara Walker, Nottingham Trent University, Brackenhurst Campus, Nottingham, UK, tamara.walker2024@my.ntu.ac.uk

Annotated r code provided to replicate the data and media produced in the Biodiversity Conservation summative assessment.

sharks <- read.csv("sharks.csv") #Loads the sharks dataset from the working directory
sharksub <- read.csv("sharksub.csv") #Loads the sharksub dataset from the working directory
library(tidyverse)
library(ggplot2)
library(ggthemes) 
library(knitr)
library(tibble)
# Loads the relevant libraries required to produce code
str(sharks) # Uses the structure function to check the number of observations is equal to the number of unique identifiers
str(sharksub) # Uses the structure function to check the number of observations is equal to the number of unique identifiers
colSums(is.na(sharks)) # Checks the sharks file for any missing data
colSums(is.na(sharksub)) # Checks the sharksub file for any missing data
mod1 <- lm(water~air, data = sharks) # Creates a linear model of surface water temperature and ambient air temperature 
summary(mod1) # Provides a summary of residuals and coefficients
ggplot(sharks, aes(x = air, y = water)) + # Produces a scatter plot of the variables air and water 
geom_point(colour = "blue", alpha = .3) + # Changes the points to the colour blue and size to 3
geom_smooth(method = "lm", se = FALSE) + # Adds a smooth regression line to the plot 
labs(x = "Ambient air temperature (C)", y = "Surface water temperature (C)") + # Amends the label names
(scale_fill_brewer(palette = "Dark2")) # Changes to a colour blind friendly palette
mod2 <- lm(blotch~air, data = sharks) # Creates a linear model of ambient air temperature and time taken to blotch
summary(mod2) # Provides a summary of residuals and coefficients
ggplot(sharks, aes(x = air, y = blotch)) +# Produces a scatter plot of the variables air and time taken to blotch
geom_point(colour = "blue", alpha = .3) + # Changes the points to the colour blue and size to 3
geom_smooth(method = "lm", se = FALSE) + # Adds a smooth regression line to the plot 
labs(x = "Ambient air temp (C)", y = "Time taken to blotch (s)") + # Amends the label names
(scale_fill_brewer(palette = "Dark2")) # Changes to a colour blind friendly palette
mod3 <- lm(blotch~water, data = sharks) # Creates a linear model of water surface temperature and time taken to blotch. 
summary(mod3) # Provides a summary of residuals and coefficients
ggplot(sharks, aes(x = water, y = blotch)) + # Produces a scatter plot of the variables water and time taken to blotch
geom_point(colour = "blue", alpha = .3) + # Changes the points to the colour blue and size to 3
geom_smooth(method = "lm", se = FALSE) + # Adds a smooth regression line to the plot 
labs(x = "Surface water temperature (C)", y = "Time taken to blotch (s)") +
(scale_fill_brewer(palette = "Dark2")) # Changes to a colour blind friendly palette
mod6 <- lm(blotch~BPM, data = sharks) # Creates a linear model of heart rate and time taken to blotch. 
summary(mod6) # Provides a summary of residuals and coefficients
mod7 <- lm(blotch~weight, data = sharks) # Creates a linear model of weight and time taken to blotch. 
summary(mod7) # Provides a summary of residuals and coefficients
mod8 <- lm(blotch~length, data = sharks) # Creates a linear model of length and time taken to blotch. 
summary(mod8) # Provides a summary of residuals and coefficients
mod9 <- lm(blotch~meta, data = sharks) # Creates a linear model of cortisol level and time taken to blotch. 
summary(mod9) # Provides a summary of residuals and coefficients
mod10 <- lm(blotch~depth, data = sharks) # Creates a linear model of depth and time taken to blotch
summary(mod10) # Provides a summary of residuals and coefficients
ggplot(sharks, aes(x = depth, y = blotch)) + # Produces a scatter plot of the variables depth and time taken to blotch 
geom_point(colour = "blue", alpha = .3,) + # Changes the points to the colour blue and size to 3  
geom_smooth(method = "lm", se = FALSE) + # Adds a smooth regression line to the plot 
  labs(x = "Water depth of capture (m)", y = "Time taken to blotch (s)") + # Amends the label names
scale_fill_brewer(palette = "Dark2") # Changes to a colour blind friendly palette
new_depth <- data.frame(depth = c(40,42.5,55,58)) #Creates a dataframe tab
new_depth$predicted <- predict(mod10, new_depth) # Creates a new column in new_blotch called predicted and uses mod5 to calculate the value
kable(new_depth, col.names=c("Hypothetical depth of capture (m)","Predicted time to blotch (s)")) #Creates a table and changes the column headings
sharks$tvar <- sharks$air - sharks$water # Creates a new field 'tvar' calculating the variation between ambient air temperature and surface water temperature.
mod4 <- lm(blotch~tvar, data = sharks) # Creates a linear model of the variance between air temperature and surface water temperature and time taken to blotch. 
summary(mod4) # Provides a summary of residuals and coefficients
ggplot(sharks, aes(x = tvar, y = blotch)) + # Produces a scatter plot of the variables temperature variance between air and water and time taken to blotch 
geom_point(colour = "blue", alpha = .3) + # Changes the points to the colour blue and size to 3
geom_smooth(method = "lm", se = FALSE) + # Adds a smooth regression line to the plot 
labs(x = "Temperature variance air/water (C)", y = "Time taken to blotch (s)") + # Amends the label names
(scale_fill_brewer(palette = "Dark2")) # Changes to a colour blind friendly palette
mod5 <- lm(blotch2~blotch1, data = sharksub) # Creates a linear model of of time taken to blotch on first capture and time taken to blotch on second capture 
summary(mod5) # Provides a summary of residuals and coefficients
ggplot(sharksub, aes(x = blotch1, y = blotch2)) + # Produces a scatter plot of the variables blotch1 and blotch2
geom_point(colour = "blue", alpha = .3,) + # Changes the points to the colour blue and size to 3
geom_smooth(method = "lm", se = FALSE) + # Adds a smooth regression line to the plot 
labs(x = "First capture", y = "Second capture", title = "Time taken to blotch (seconds)" + # Amends the label names and adds a title
scale_fill_brewer(palette = "Dark2")) # Changes to a colour blind friendly palette
ggplot(sharksub, aes(x = blotch1, y = blotch2, colour = sex)) + # Produces a scatter plot of the variables blotch1 and blotch2, coloured by sex
geom_point(alpha = .3,) + # Changes the points to size to 3
geom_smooth(method = "lm", se = FALSE) + # Adds smooth regression lines to the plot 
labs(x = "First capture", y = "Second capture", title = "Time taken to blotch (seconds)" + # Amends the label names and adds a title
scale_fill_brewer(palette = "Dark2")) # Changes to a colour blind friendly palette
ggplot(sharks, aes(x = sex, y = blotch, colour = sex,))  + # Creates a plot of time taken to blotch by sex
  geom_boxplot() + geom_jitter(alpha = .3,) + # Creates a boxplot showing all residuals and point size 3
  labs(x = "Sex", y = "Time taken to blotch (s)" + # Changes the axis titles
  scale_fill_brewer(palette = "Dark2")) # Changes to a colour blind friendly palette
t.test(blotch ~ sex, data = sharks) # Produces a Welch Two Sample t-test for time time to blotch between males and females. Significant - on average males delayed reaction to stress compared to females 
new_blotch <- data.frame(blotch1 = c(36.5,37.5,38.5,39.5)) #Creates a data frame called new_blotch, containing 1 column and user defined values
new_blotch$predicted <- predict(mod5, new_blotch) # Creates a new column in new_blotch called predicted and uses mod5 to calculate the value
kable(new_blotch, col.names=c("Hypothetical time to blotch at first capture (s)","Predicted time to blotch at second capture (s)")) # Creates a table and changes the column names
mean(sharks$blotch)
min(sharks$blotch)
max(sharks$blotch)
mean(sharks$air)
min(sharks$air)
max(sharks$air)
mean(sharks$water)
min(sharks$water)
max(sharks$water)
mean(sharksub$blotch2)
min(sharksub$blotch2)
max(sharksub$blotch2)
mean(sharks$tvar)
min(sharks$tvar)
max(sharks$tvar)
range(sharks$depth)