--- title: "QUBES Buffers and Managing Pool-Breeding Amphibians" author: "Liz Buikema" date: "Spring 2022" output: html_document --- #### First we will need to load in all of the necessary packages to get this document to run properly. If you cannot get a library to load correctly, try loading them with the "install.packages" function (ex: install.packages(ggplot2)). #### Within this chunk we will also be loading in our CSV file, make sure this is located in the correct folder so that R can find it. ```{r} library(tidyverse) library(ggplot2) library(ggfortify) BuffAmphs <- read.csv("Data-QUBESBuffAmphs.csv") ``` ```{r} #Check to make sure your data loaded correctly summary(BuffAmphs) ``` #### What are these variable? wetland- unique ID to the vernal pool assigned by the authors. year- when the sample was collect. treatment- Which treatment the vernal pool was assigned (categorical variable) 30m or 100m. species- pool-breeding amphibian being counted SALA for spotted salamanders or FROG for wood frogs. TOTAL.adults- all breeding adult amphibians; male and female (response variable). MEAN.hydro - The mean hydroperiod of each vernal pool (continuous variable). #### The code below allows us to choose what treatment will be used as the reference either 30m or 100m. This will be important when we look at the GLM output below. We will change this by making 30m our reference. By default, R chooses the reference alphanumerically, which would be 100m treatment for our data set. ```{r} BuffAmphs$treatment <- relevel(factor(BuffAmphs$treatment), ref="30m") ``` #### Now, let's filter the data to include only the species we want to look at, in this case we are looking at salamanders (SALA). ```{r} BuffAmphs %>% filter(species == "SALA") ->BuffSala #Check to make sure you did what you think you did head(BuffSala) ``` ```{r} #Exploration of the salamander data, plot a histogram of TOTAL.adults within our filtered dataset. BuffSala %>% ggplot(aes(TOTAL.adults))+ geom_histogram()+ xlab("Total Salamanders") + ylab("Count Frequency") ``` **QUESTION: Is this data normally distributed? Why or why not?** ```{r} #GLM without Poisson distribution. wrong <- glm(TOTAL.adults ~ MEAN.hydro*treatment, data=BuffSala) #GLM data summary summary(wrong) ``` ```{r} #Model diagnostics of the data, for people who like to have visuals #want to know how to read these graphs? Go here: https://youtu.be/upJJmfSbBuQ autoplot(wrong) ``` #### Let's plot this data, the graph below is similar to the one found in our paper (fig. 5) ```{r} #GGplot of Salamander- MEAN.hydro, TOTAL.adults, and treatment BuffSala %>% ggplot(aes(x = MEAN.hydro, y = TOTAL.adults, colour = treatment)) + geom_point() + geom_smooth(method= "glm", se=TRUE)+ ggtitle("Total Adult Salamanders - WRONG")+ xlab("Mean Hydroperiod")+ ylab("Total Breeding Adults")+ theme_bw() ``` **QUESTION: Based on the graph and its corresponding R output what can we say about the data?** #### Now let's check out the GLM with the Poisson distribution ```{r} #GLM with the Poisson log transformation SALAPoisson <- glm(TOTAL.adults ~ MEAN.hydro*treatment, family = poisson(link = "log"), data=BuffSala) #GLM data summary summary(SALAPoisson) ``` ```{r} #graphical output of the data, for people who like to have visuals #want to know how to read these graphs? Go here: https://youtu.be/upJJmfSbBuQ autoplot(SALAPoisson) ``` ```{r} #Lets build a better plot for our data that takes the GLM with Poisson and a log link function into account BuffSala %>% ggplot(aes( x=MEAN.hydro, y=TOTAL.adults, colour = treatment))+ geom_point()+ stat_smooth(method= "glm", method.args = list(family="poisson"), se=T)+ ggtitle("Total Adult Salamanders")+ xlab("Mean Hydroperiod")+ ylab("Total Breeding Adults")+ theme_bw() ``` **QUESTION:Do these results seem like a better fit to the data when compared to the GLM without the Poisson distribution? What does this mean for the salamanders?** ### ON YOUR OWN - Frog Data To run these analyses, use a unique operator (ex: -> BuffFrog). Running these commands multiple times with the same names may confuse R/you. ```{r} #filter data to only include wood frog (FROG) data. Make sure you assign it to its own unique operator (above that was '-> BuffSala') #Check to make sure you did what you think you did ``` ```{r} #Exploration of the frog data, plot a histogram of TOTAL.adults (total breeding adults frogs) #Is this data normally distributed? ``` ```{r} #lets look at the GLM model, should you use the formula with or without the the Poisson distribution? #summary to view output ``` ```{r} #graphical output of the data, for people who like to have visuals ``` ```{r} #plot for our data that takes the GLM with Poisson and a log link function into account ```