Final Project

library(knitr)
library(tidyverse)
library(lmtest)
library(bbmle)
library(MASS)
library(car)
library(pscl)
library(conflicted)
library(lme4) 
library(ggplot2)
library(ggthemes) 
library(sjPlot)
library(extrafont)
loadfonts(device = "win")
conflict_prefer("select","dplyr")
conflict_prefer("filter","dplyr")
conflict_prefer("summarize", "dplyr")

PART 1 – Data selection and cleaning.

  • Choose a butterfly species to work with. The folder “butterfly club data” includes 100 .csv files, each giving information for a different species. The species are identified by 4-letter codes, which are identified by name in the “butterfly species codes” spreadsheet. You can see photos of the butterflies on the MBC website (https://www.massbutterflies.org/butterfly-info.asp), and also read about many of them on a eBook, Butterflies of Massachusetts (written by MBC member Sharon Stichter: https://www.butterfliesofmassachusetts.net/).

    • a) State which butterfly species you are going to analyze, and briefly (1-2 sentences) explain why.
      • I chose Callophrys gryneus, which is colloquially known as the Juniper Hairstreak. I chose this species because it once flourished in all of New England, but is now very localized southern New England. It is now “critically imperilled” in Maine and possibly extirpated in New Hampshire. It’s also my favorite color (olive green).
  • Each data file has ~20,000 rows and 6 columns. Each row is a “trip” recorded in the MCB database. The columns are: (1) listlength is the number of species seen on a particular trip; (2) present is a column coding whether that species was seen on each trip (1 = species seen, 0 = species not seen); (3) listyear is the year of the trip; (4) yearday is the day of year on which each trip took place, e.g., Jan 1 is day 1, April 1 is day 91, June 1 is day 152, etc. (5) listplace is the Massachusetts township in which each trip took place; (6) ltqty is the number of individual butterflies of that species seen on that trip.

    • b) To do the analyses, you will need to clean the data in three ways:
        1. For some reason, the “present” column occasionally includes values greater than 1. For your analysis, you should replace all values > 1 with a 1.
        1. The database includes some records from years before the club started making regular trips. For this analysis, filter the data to include only records collected between 1992 and 2010.
        1. The database also includes some records where individual club members reported single interesting butterfly species, without really searching an area. To remove these records, filter your data to include only trips where the listlength is greater than 5 species.
judu <- read.csv("/Users/emilybreeden/Desktop/BIO 427/Project 3/JUDUlist.csv")

# part b) 1.
judu$present[which(judu$present>1)] <- 1
range(judu$present)
## [1] 0 1
#part b) 2.
judu <- judu[judu$listyear>1991,]
range(judu$listyear)
## [1] 1992 2010
#part b) 3.
judu <- judu[judu$listlength>5,]
range(judu$listlength)
## [1]  6 84
  • c) To analyze whether temperature affects butterfly phenology (i.e., the days on which a species is present), you need to merge the data on sightings of your butterfly with temperature data. Temperature is summarized for each year using cumulative growing degree days (GDD) from the beginning of the year until June 1. (See https://en.wikipedia.org/wiki/Growing_degree-day or the Hodgson et al. 2010 and Cayton et al. 2015 papers on Canvas for an extended description of the relationship between GDD and temperature in general, and applied to butterfly phenology in particular.)
    • Merge the Jan-Jun GDD for each species with the butterfly sightings data for your species. Call the new column “GDD”.
gdd <- read.csv("/Users/emilybreeden/Desktop/BIO 427/Project 3/GDD_MA.csv") %>%
  rename(GDD = GDD_JanJun)

judu2 <- judu %>%
  rename(year = listyear) %>%
  left_join(gdd, by = "year")

PART 3 – Analysis of the number of individuals seen on each trip.

  • a) For this analysis, you will use the “ltqty” column of the data, i.e., the number of individuals of your species seen on each trip. Choose and briefly justify a probability distribution for these data.
    • The data for the ltqty column is discrete and numerical, so I will run a Poisson model. However, the mean and variance are not equal (i.e., the data are overdispersed). Therefore, I will also run a negative binomial to attempt to account for the overdispersion. For fun, I will also run a negative binomial model.
      • Based on the results of the AIC, the negative binomial is the best fit for the data (by a long shot). Therefore, I will use it for my analysis.
mean(judu2$ltqty)
## [1] 0.9643188
var(judu2$ltqty)
## [1] 21.52429
  • b) Analyze trends in abundance of your species through time using lqty as the response variable and year as a continuous predictor variable. Report the lines of code used to run your analysis, the slope coefficient(s) for the effect of year, and relevant test statistics for deciding whether the abundance is changing through time.
    • Based on the results of my negative binomial model, the Juniper Hairstreak is increasing in abundance over time. year_0 has a positive coefficient (0.0525) and a confidence interval that does not intercept 0 (0.0323, 0.0727). This, in conjunction with the results of the marginal hypothesis test (Chisq = 26.022, Df = 1, p = 3.375e-07), show that the effects of year_0 are both positive and significant.
m3b <- glm(ltqty ~ year_0, family = poisson, data = judu2)
m3b.1 <- glm.nb(ltqty ~ year_0, data = judu2)
m3b.2 <- glm(ltqty ~ year_0, data = judu2)
ICtab(m3b, m3b.1, m3b.2, base = T)
##       AIC     dAIC    df
## m3b.1 10923.8     0.0 3 
## m3b   34489.2 23565.3 2 
## m3b.2 38228.5 27304.7 3
coef(m3b.1)
## (Intercept)      year_0 
## -0.60683258  0.05250531
confint(m3b.1)
## Waiting for profiling to be done...
##                   2.5 %      97.5 %
## (Intercept) -0.83378723 -0.37250662
## year_0       0.03236747  0.07266623
Anova(m3b.1)
## Analysis of Deviance Table (Type II tests)
## 
## Response: ltqty
##        LR Chisq Df Pr(>Chisq)    
## year_0   26.022  1  3.375e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## `geom_smooth()` using formula = 'y ~ x'

  • c) Both species presence and the number seen are metrics of abundance. Did you reach the same conclusion by analyzing presence and number seen? If not, which analysis do you think gives a better answer? Decide whether your species is significantly increasing, significantly decreasing or not changing in abundance, and briefly explain how your statistical models relate to your conclusion.
    • I reached the same conclusions when analyzing presence and number seen. The results of the model for presence showed that the Juniper Hairstreak population was increasing over time. Further, the results of the model for number seen (ltqty) also showed that the Juniper Hairstreak was increasing over time. The effect/coefficient of year_0 in both models was not only positive, but also statistically significant as the confidence intervals for these coefficients did not intercept 0. Further, both of these predictors had p-values less than 0.05. Therefore, the Juniper Hairstreak is significantly increasing over time.

PART 4 – Analysis of phenology (sighting dates)

  • a) For this analysis, you will use only trips on which the species was seen. Create a new data frame by filtering your data to include only trips with present = 1. Report the line of code used to create this new data frame, and the number of trips on which your species was seen (i.e., the number of rows in your data frame). Turn in this second data frame with your data.
    • The number of trips on which my species was seen was 1031.
judu_present <- judu2 %>%
  filter(present == 1)
range(judu_present$present)
## [1] 1 1
length(judu_present$present)
## [1] 1031
  • b) Choose a probability distribution for sighting dates, i.e., the yearday column. Briefly, justify your choice.
    • I would use a poisson distribution, because the data for the yearday column is discrete quantitative data. However, the mean of the yearday column does not equal it’s variance indicating these data are overdispersed. Therefore, I will also run a negative binomial model and test which one is better using AIC. For fun, I will also run a model with a normal distribution.
      • Based on the AIC comparison done in 4c, the negative binomial model was a better fit for the data. Thus, I will use a negative binomial for the rest of my analysis.
mean(judu_present$yearday)
## [1] 141.9709
var(judu_present$yearday) 
## [1] 211.9331
  • c) Test whether the average sighting date is changing through time using a GLM with the probability distribution chosen above, and listyear as a continuous predictor variable. Report the line(s) of code used in your analysis, the R output of your code, and the test statistics used to reach your conclusion. Be sure to report both whether the average date is increasing (later phenology) or decreasing (earlier phenology) and whether this change is statistically significant.
    • Based on the results of my negative binomial model (m2c.1), the average sighting date is decreasing over time, which is supported by year_0’s negative coefficient value (-0.00323). This decrease is statistically significant, as the confidence interval for this coefficient does not intercept 0 (-0.00448, -0.00198).
m4c <- glm(yearday ~ year_0, family = poisson, data = judu_present) 
m4c.1 <- glm.nb(yearday ~ year_0, data = judu_present) 
m4c.2 <- glm(yearday ~ year_0, data = judu_present) 
ICtab(m4c, m4c.1, m4c.2, base = T) 
##       AIC    dAIC   df
## m4c.1 8419.2    0.0 3 
## m4c.2 8427.7    8.5 3 
## m4c   8498.9   79.7 2
coef(m4c.1)
## (Intercept)      year_0 
##  4.99126767 -0.00323073
confint(m4c.1)
## Waiting for profiling to be done...
##                    2.5 %       97.5 %
## (Intercept)  4.976191642  5.006329236
## year_0      -0.004478294 -0.001982971
## `geom_smooth()` using formula = 'y ~ x'

  • d) Test whether the average sighting date depends on spring temperatures using a GLM with the probability distribution chosen above, and GDD as a continuous predictor variable. Report the line(s) of code used in your analysis, the R output of your code, and the test statistics used to reach your conclusion. Be sure to report both whether the average date is higher (later phenology) or lower (earlier phenology) in years with higher GDD, and whether this change is statistically significant.
    • Based on the results of my negative binomial model (m4d), the average sighting dates are decreasing as GDD is increasing, as the variable GDD has a negative coefficient value (-0.000678). The effects of this variable are significant, as the confidence interval for the coefficient does not intercept 0 (-0.000829, -0.000528) and the results of the marginal hypothesis test support this conclusion (Chisq = 78.141, Df = 1, p = 2.2e-16).
m4d <- glm.nb(yearday ~ GDD, data = judu_present)
Anova(m4d)
## Analysis of Deviance Table (Type II tests)
## 
## Response: yearday
##     LR Chisq Df Pr(>Chisq)    
## GDD   78.141  1  < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
coef(m4d)
##   (Intercept)           GDD 
##  5.0994123439 -0.0006785231
confint(m4d)
## Waiting for profiling to be done...
##                     2.5 %       97.5 %
## (Intercept)  5.0669671891  5.131857717
## GDD         -0.0008291785 -0.000527947
## `geom_smooth()` using formula = 'y ~ x'

  • e) Not surprisingly, the climate is generally getting warmer through time. Is variation in GDD sufficient to explain trends in phenology, or is something more going on? To evaluate this possibility, fit a GLM to the yearday data, using both year and GDD as continuous predictor variables. If year is still statistically significant even after accounting for GDD in each year, there may be something more going on. Report the lines of R code you used to run this model, the relevant test statistics, and your conclusions.
    • Variation in GDD is not sufficient to explain the later phenology of the Juniper Hairstreak. Based on the results of a marginal hypothesis test for the model with both year_0 and GDD as predictors, both of the predictors are significant. The results for year_0 are as follows: Chisq = 5.479, Df = 1, p = 0.01924. The results for GDD are as follows: Chisq = 56.863, Df = 1, p = 4.674e-14. Thus, while GDD has a lower p-value, the p-value for year_0 is still below the 0.05 cut off. Therefore, both variables are signficant predictors and more variables are contributing to phenology trends then just GDD.
m4e <- glm.nb(yearday ~ year_0 + GDD, data = judu_present)
Anova(m4e)
## Analysis of Deviance Table (Type II tests)
## 
## Response: yearday
##        LR Chisq Df Pr(>Chisq)    
## year_0    5.479  1    0.01924 *  
## GDD      56.863  1  4.674e-14 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
ICtab(m4e, m4d, m4c.1, base = T)
##       AIC    dAIC   df
## m4e   8365.9    0.0 4 
## m4d   8369.3    3.5 3 
## m4c.1 8419.2   53.4 3

PART 5 – conclusions

  • Briefly, summarize what you have learned about your butterfly species from these analyses. You can include 1-2 sentences about the basic biology if you like. Be sure to include 2-4 sentences about your conclusions based on the statistical models.
    • The Juniper Hairstreak (Callophrys gryneus) is increasing in abundance over time. Most of the variation of presence over time is accounted for by the location being surveyed, rather than the year during which surverying occurred. This makes sense as the Juniper Hairstreak’s host plant, the Easter Red Cedar (Juniperus virginiana), is found in high concentration in the coastal regions of Massachusetts and sparsely in the rest of the state. However, the population has begun to emerge earlier in the year over time. This is likely due to the warming climate, as most butterfly species use temperature as an indicator of when to end diapause and begin mating. Thus, if the climate is warming earlier and earlier, the butterflies will emerge from diapause earlier and earlier. This is further supported by the population emerging earlier as GDD (Growing Degree Days) increases. However, there are likely other variables impacting the earlier phenology, as GDD does not account for all of the variation in phenology.