---
title: "ButterflyResourceUse"
author: "mfh46"
date: '2023-02-10'
output: html_document
---
## Package loading
```{r}
library(tidyverse)
library(dplyr) # needed for na.rm function, for example
library(iNEXT) # for accumulation curves
library(vegan) # for community analysis
library(lme4) # to run glmer function
library(MASS) # for glm.nb function
library(FactoMineR) # for PCA
library(factoextra) # for PCA biplot with this function: fviz_pca_biplot
library(cowplot)
library(gridExtra)
library(fossil) # for Chao1 and ACE index
library(see) # for visualizations, particularly checking model assumptions
library(Rcpp) # to help package "see" running
library(performance) # to check overdispersion for GLM models using this function: check_overdispersion(model)
```
```{r}
# citing and referencing libraries
citation("tidyverse")
citation("dplyr")
citation("vegan")
citation("iNEXT")
citation("lme4")
citation("MASS")
citation("FactoMineR")
citation("factoextra")
citation("cowplot")
citation("gridExtra")
citation("fossil")
```
# Data Loading
### Basic & Environmental data
```{r}
ButterflyData <- read_csv("data/BantingButterflies_ButterflyData.csv")
Env1 <- read_csv("data/BantingButterflies_EnvData.csv")
Env2 <- read_csv("data/BantingButterflies_EnvDataMore.csv")
```
# GLM analyses
# Abundance
```{r}
## PC1
Btfly_Abund1 = BtflyGLM %>%
  ggplot(aes(x = PC1, y = BtflyCvedArea_Rounded, shape = PlantationType, fill = PlantationType, colour = PlantationType)) +
  scale_x_continuous("PC1") +
  scale_y_continuous("Butterfly density/ 500 meter square") +
  geom_point(size=5.5) +
  scale_fill_manual(name = "Plantation types", values = c("#00BA38", "#F8766D", "#619CFF"), labels = c("Immature monoculture","Immature polyculture", "Mature monoculture")) +
  scale_colour_manual(values=c("black", "black", "black")) + 
  scale_shape_manual(values = c(21, 24, 22)) +
  theme_classic() +
  theme(axis.title.x = element_text(size = 20), axis.text.x = element_text(size = 20), axis.title.y = element_text(size = 20), axis.text.y = element_text(size = 20)) + theme(strip.text.x = element_text(size=20)) +
  panel_border(colour = "black", size = 1.5) +
  theme(legend.position = "none") +
  geom_text(x = 2.3, y = 60, label = expression(paste(italic("P "), "= 0.596")), size = 8)
ggplot2::ggsave("Btfly_Abund1.png", width = 6, height = 5)

## PC2
Btfly_Abund2 = BtflyGLM %>%
  ggplot(aes(x = PC2, y = BtflyCvedArea_Rounded, shape = PlantationType, fill = PlantationType, colour = PlantationType)) +
  scale_x_continuous("PC2") +
  scale_y_continuous("Butterfly density/ 500 meter square") +
  geom_point(size=5.5) +
  geom_smooth(aes(shape = NULL, fill = NULL, colour = NULL), method = glm.nb, formula = y ~ x, level = 0.95) + # even without specifiying the "level = 0.95", the default is already 0.95 to represent confidence intervals
  scale_fill_manual(name = "Plantation types", values = c("#00BA38", "#F8766D", "#619CFF"), labels = c("Immature monoculture","Immature polyculture", "Mature monoculture")) +
  scale_colour_manual(values=c("black", "black", "black")) + 
  scale_shape_manual(values = c(21, 24, 22)) +
  theme_classic() +
  theme(axis.title.x = element_text(size = 20), axis.text.x = element_text(size = 20), axis.title.y = element_text(size = 20), axis.text.y = element_text(size = 20)) + theme(strip.text.x = element_text(size=20)) +
  panel_border(colour = "black", size = 1.5) +
  theme(legend.position = "none") +
  geom_text(x = 2, y = 140, label = expression(paste(italic("P "), "< 0.001 ***")), size = 8)
ggplot2::ggsave("Btfly_Abund2.png", width = 6, height = 5)

## PC3
Btfly_Abund3 = BtflyGLM %>%
  ggplot(aes(x = PC3, y = BtflyCvedArea_Rounded, shape = PlantationType, fill = PlantationType, colour = PlantationType)) +
  scale_x_continuous("PC3") +
  scale_y_continuous("Butterfly density/ 500 meter square") +
  geom_point(size=5.5) +
  geom_smooth(aes(shape = NULL, fill = NULL, colour = NULL), method = glm.nb, formula = y ~ x, level = 0.95) + # even without specifiying the "level = 0.95", the default is already 0.95 to represent confidence intervals
  scale_fill_manual(name = "Plantation types", values = c("#00BA38", "#F8766D", "#619CFF"), labels = c("Immature monoculture","Immature polyculture", "Mature monoculture")) +
  scale_colour_manual(values=c("black", "black", "black")) + 
  scale_shape_manual(values = c(21, 24, 22)) +
  theme_classic() +
  theme(axis.title.x = element_text(size = 20), axis.text.x = element_text(size = 20), axis.title.y = element_text(size = 20), axis.text.y = element_text(size = 20)) + theme(strip.text.x = element_text(size=20)) +
  panel_border(colour = "black", size = 1.5) +
  theme(legend.position = "none") +
  geom_text(x = 2.5, y = 60, label = expression(paste(italic("P "), "= 0.030 *")), size = 8)
ggplot2::ggsave("Btfly_Abund3.png", width = 6, height = 5)

## PC4
Btfly_Abund4 = BtflyGLM %>%
  ggplot(aes(x = PC4, y = BtflyCvedArea_Rounded, shape = PlantationType, fill = PlantationType, colour = PlantationType)) +
  scale_x_continuous("PC4") +
  scale_y_continuous("Butterfly density/ 500 meter square") +
  geom_point(size=5.5) +
  geom_smooth(aes(shape = NULL, fill = NULL, colour = NULL), method = glm.nb, formula = y ~ x, level = 0.95) + # even without specifiying the "level = 0.95", the default is already 0.95 to represent confidence intervals
  scale_fill_manual(name = "Plantation types", values = c("#00BA38", "#F8766D", "#619CFF"), labels = c("Immature monoculture","Immature polyculture", "Mature monoculture")) +
  scale_colour_manual(values=c("black", "black", "black")) + 
  scale_shape_manual(values = c(21, 24, 22)) +
  theme_classic() +
  theme(axis.title.x = element_text(size = 20), axis.text.x = element_text(size = 20), axis.title.y = element_text(size = 20), axis.text.y = element_text(size = 20)) + theme(strip.text.x = element_text(size=20)) +
  panel_border(colour = "black", size = 1.5) +
  theme(legend.position = "none") +
  geom_text(x = 2.2, y = 60, label = expression(paste(italic("P "), "= 0.044 *")), size = 8)
ggplot2::ggsave("Btfly_Abund4.png", width = 6, height = 5)
```
```{r}
hist(BtflyGLM$BtflyCvedArea_Rounded)
BtflyGLM_Abund <- lm(BtflyCvedArea_Rounded ~ PC1 + PC2 + PC3 + PC4, data = BtflyGLM)
summary(BtflyGLM_Abund)
plot(BtflyGLM_Abund)
```
```{r}
BtflyGLM_Abund1 <- glm(BtflyCvedArea_Rounded ~ PC1 + PC2 + PC3 + PC4, family = poisson(link = "log"), data = BtflyGLM)
summary(BtflyGLM_Abund1)
plot(BtflyGLM_Abund1)
check_model(BtflyGLM_Abund1)
```
```{r}
# checking overdispersion of poisson family using function from performance package
check_overdispersion(BtflyGLM_Abund1)
# p-value < 0.001 
# overdispersed, so negative binomial models are chosen
```
### Log-likelihood ratio tests
#### for relative importance of each covariate
```{r}
# Full model & null model
BtflyGLM_Abund1 <- glm.nb(BtflyCvedArea_Rounded ~ PC1 + PC2 + PC3 + PC4, link = log, data = BtflyGLM)
BtflyGLM_Abund0 <- glm.nb(BtflyCvedArea_Rounded ~ 1, link = log, data = BtflyGLM)
anova(BtflyGLM_Abund1,BtflyGLM_Abund0)
# p-value = 0.0001570169
```
```{r}
# dropping covariates one by one
BtflyGLM_Abund1.1 <- glm.nb(BtflyCvedArea_Rounded ~ PC2 + PC3 + PC4, link = log, data = BtflyGLM)
BtflyGLM_Abund2 <- glm.nb(BtflyCvedArea_Rounded ~ PC1 + PC3 + PC4, link = log, data = BtflyGLM, control=glm.control(maxit=100)) # maxit is to just increase the number of iterations
BtflyGLM_Abund3 <- glm.nb(BtflyCvedArea_Rounded ~ PC1 + PC2 + PC4, link = log, data = BtflyGLM, control=glm.control(maxit=100))
BtflyGLM_Abund4 <- glm.nb(BtflyCvedArea_Rounded ~ PC1 + PC2 + PC3, link = log, data = BtflyGLM)
```
```{r}
anova(BtflyGLM_Abund1,BtflyGLM_Abund1.1) # p-value = 0.5966296
anova(BtflyGLM_Abund1,BtflyGLM_Abund2) # p-value < 0.001
anova(BtflyGLM_Abund1,BtflyGLM_Abund3) # p-value = 0.03059486
anova(BtflyGLM_Abund1,BtflyGLM_Abund4) # p-value = 0.04441865
```
# gridExtra
```{r}
# PC1
grid.arrange(Btfly_Abund1, Btfly_Chao1.1, Btfly_ACE.1, ncol=3)
grid1 <- arrangeGrob(Btfly_Abund1, Btfly_Chao1.1, Btfly_ACE.1, ncol=3) 
ggsave(file="grid1.png", grid1, width = 20, height = 5) 

# PC2
grid.arrange(Btfly_Abund2, Btfly_Chao1.2, Btfly_ACE.2, ncol=3)
grid2 <- arrangeGrob(Btfly_Abund2, Btfly_Chao1.2, Btfly_ACE.2, ncol=3) 
ggsave(file="grid2.png", grid2, width = 20, height = 5) 

# PC3
grid.arrange(Btfly_Abund3, Btfly_Chao1.3, Btfly_ACE.3, ncol=3)
grid3 <- arrangeGrob(Btfly_Abund3, Btfly_Chao1.3, Btfly_ACE.3, ncol=3) 
ggsave(file="grid3.png", grid3, width = 20, height = 5) 

# PC4
grid.arrange(Btfly_Abund4, Btfly_Chao1.4, Btfly_ACE.4, ncol=3)
grid4 <- arrangeGrob(Btfly_Abund4, Btfly_Chao1.4, Btfly_ACE.4, ncol=3) 
ggsave(file="grid4.png", grid4, width = 20, height = 5) 
```