--- title: "2.5: Module 2 Wrap-Up" author: "Ellen Bledsoe, Lily McMullen" format: html: toc: true --- ```{r} #| include: false knitr::opts_chunk$set(echo = TRUE) ``` # What is causing the food poisoning? ## Learning Outcomes - Students will be able to apply descriptive statistics and data visualization skills to investigate a problem. - Students will be able to create a new variable using `mutate()`. - Students will be able to use `group_by()` and `summarize()` to compare groups. - Students will be able to select and interpret appropriate plot types to identify patterns across environmental variables. At the beginning of Module 2, we set out to discover what was causing food poisoning among our colleagues at our Antarctic base. Let's put everything we've learned about descriptive statistics and data visualization to use to try to hunt down what the problem is. ## Set-up Let's load the package and data we will need. ### Packages ```{r} #| message: false library(tidyverse) ``` ### Data First, we need our dataset! ```{r} #| message: false sick_fish <- read_csv("data/fish_sick_data.csv") ``` Let's check out our data and remind ourselves what we are working with. ```{r} head(sick_fish) ``` ## Which Fish? In the previous lesson, we plotted the number of sick fish. Let's remind ourselves what that looked like. Make a plot that compares the numbers of sick fish per species. We actually have a few options! ```{r} # Write your code here ``` ::: instructor-only **Answer:** ```{r} ggplot(sick_fish, aes(num_sick, fill = species)) + geom_histogram(alpha = 0.5, position = "identity", bins = 10) + labs(x = "Number of Sick Fish in Tanks", y = "Frequency", fill = "Species") + theme_classic() ``` ::: ### Density Wait a second! Take a look back at the data. There is a "number of fish" column, indicating the total number of fish in the tank, and it looks like those numbers can differ pretty widely. We should probably take into account how many fish there are in the tank to begin with. 12 sick fish out of 50 is probably a bigger deal than 12 sick fish out of 100! What we need to do is calculate a density: the number of sick fish divided by the total number of fish in the tank. ```{r} # take into account the number of fish in the tank: density of sick fish sick_fish <- sick_fish %>% mutate(density = num_sick/num_fish) ``` #### Small Groups Let's make sure our conclusions about trout being the true culprits still hold when we account for the total number of fish in the tank. Work in small groups to do the following: - find the average number and standard deviation of sick fish for both species - make a plot that compares the distributions of sick fish numbers for both species (you have multiple options here!) ```{r} # Write your code here ``` ::: instructor-only **Answer:** ```{r} sick_fish %>% group_by(species) %>% summarize(mean_sick_fish = mean(density), sd_sick_fish = sd(density)) ``` ```{r} # boxplot option ggplot(sick_fish, aes(species, density, color = species)) + geom_boxplot() + geom_jitter(alpha = 0.5, width = 0.1) + labs(x = "Species", y = "Density of Sick Fish in Tanks", color = "Species") + theme_light() ``` ```{r} # histogram option ggplot(sick_fish, aes(density, fill = species)) + geom_histogram(alpha = 0.5, bins = 15) ``` **Instructor Note:** Trout have a mean sick fish density of 0.193, roughly 5.7× higher than tilapia (0.034). The boxplot makes this immediately visible. ::: Uh oh... the trout densities look even worse than just the number of sick fish. We should create a data frame that only contains trout to work with for the rest of our analyses. Take a few minutes to work on that; call it `sick_trout`. ```{r} # Write your code here ``` ::: instructor-only **Answer:** ```{r} sick_trout <- sick_fish %>% filter(species == "trout") sick_trout ``` ::: ## What Environmental Factor? Take a look back at the data frame. Which columns are environmental variables that could be driving the issues? Are those columns numeric or categorical? What plot type have we talked about that might help us find a relationship between density and each of these variables (one at a time...)? ::: instructor-only **Instructor Note:** The environmental columns in the trout data are `avg_daily_temp`, `num_fish`, `day_length`, and `tank_volume`. All are numeric, so scatter plots with `density` on the y-axis are the right choice. ::: In small groups, make plots using the `density` column in the `sick_trout` data to try to figure out which environmental factor is causing problems in the trout. Make one plot per variable so you can look at each relationship on its own. ```{r} # Write your code here ``` ::: instructor-only **Answer:** ```{r} ggplot(sick_trout, aes(x = avg_daily_temp, y = density)) + geom_point() + labs(x = "Average Daily Temperature (°C)", y = "Density of Sick Fish") + theme_light() ggplot(sick_trout, aes(x = num_fish, y = density)) + geom_point() + labs(x = "Number of Fish in Tank", y = "Density of Sick Fish") + theme_light() ggplot(sick_trout, aes(x = day_length, y = density)) + geom_point() + labs(x = "Day Length (hours)", y = "Density of Sick Fish") + theme_light() ggplot(sick_trout, aes(x = tank_volume, y = density)) + geom_point() + labs(x = "Tank Volume", y = "Density of Sick Fish") + theme_light() ``` ::: What do we think is the environmental driver causing issues with the trout? ::: instructor-only **Instructor Note:** This is a discussion question, so keep it open and let students reason from their plots. Use the points below to guide the conversation rather than handing over the answer. `avg_daily_temp` shows a positive relationship between temperature and sick fish density. As temperature goes up, density tends to go up. This makes biological sense, since rainbow trout are cold-water fish and even modest warming toward the upper end of their tolerance range stresses their immune function. `num_fish` shows a slight negative trend: as the number of fish in a tank goes up, density drops a little. This is most likely a dilution effect in the density calculation (more total fish in the denominator) rather than a real biological pattern, and the scatter is wide. `day_length` shows essentially no relationship `tank_volume` is nearly constant across the trout tanks (everything sits right around 400). There is no variation to drive anything, which means we can rule it out as a cause. Temperature is the primary driver. Students haven't learned how to fit a trend line yet, but if you want to make the pattern more obvious you can add `geom_smooth(method = "lm")` to the temperature plot as a preview of the regression work coming in Module 3. Close by asking what they would recommend to the Antarctic base team, which sets up that transition. :::