• This code makes and compares multiple generalized linear models of the experiment data with jellyfish bimass vs. Oithona density.
  • A best fit model is chosen, and the results are visualized.
  • Link to report here: https://rpubs.com/HailaSchultz/exp_GLM-oithona

load packages

library(MASS)
library(ggplot2)
library(ggeffects)
library(dplyr)
library(scales)

Set up data

Read database into R. If current database changes, just change the path

Database <- read.csv("/Users/hailaschultz/Dropbox/Other studies/Aurelia project/Data Analysis/data/current_data/Final_Aurelia_Database_Jan11_2023.csv")

Add control tanks

First, read in the file that designates different samples to tanks with jellies other, tanks with zero jellies sampled at the beginning of the experiment remove, and tanks with zero jellies sampled at the end of the experiment zero

Control_tanks <- read.csv("/Users/hailaschultz/Dropbox/Other studies/Aurelia project/Data Analysis/data/current_data/Control_Tanks.csv")

Add control tank data to the database file

Database$Control_info <- Control_tanks$c1[match(Database$Sample.Code, Control_tanks$Sample.Code)]

Subset data

Subset to trials with large jellies where the number of jellies in the tanks was manipulated

Exp_numb_trials<- subset(Database, Trial.Type=='Number')
Exp_numb_large<-subset(Exp_numb_trials,Jelly.Size=="Large")

remove aug 22 2019 - the jellies we used for this experiment were younger/smaller than the other experiments, so I decided to omit it from analysis

Exp_sub<-subset(Exp_numb_large,Sample.Date!="08/22/2019")

remove tanks sampled at the beginning of experiment - we decided not to use these tanks because they were different than the tanks with zero jellies sampled at the end of the experiment and didn’t add anything beneficial

Exp_sub<-subset(Exp_sub,Control_info!="remove")

Prepare data

#convert sample year to a factor
Exp_sub$Sample.Year<- as.factor(Exp_sub$Sample.Year)
#Rename Vars (get the dots out)
Exp_sub$Jelly.Mass<-Exp_sub$Jelly.Mass..g.
Exp_sub$Density<-Exp_sub$Density....m3.
#calculate jelly density
Exp_sub$Jelly.Density<-Exp_sub$Jelly.Mass/Exp_sub$Vol.Filtered..m3.
#add 1 to jelly  density for analyses that prohibit zeroes
Exp_sub$Jelly.Density <-Exp_sub$Jelly.Density
colnames(Exp_sub)
##  [1] "BugSampleID"          "Project"              "Sample.Code"         
##  [4] "Sampling.Group"       "Station"              "Site"                
##  [7] "Site.Name"            "Basin"                "Sub.Basin"           
## [10] "Latitude"             "Longitude"            "Sample.Date"         
## [13] "Sample.Year"          "Sample.Month"         "Sample.Time"         
## [16] "Tow.Type"             "Mesh.Size"            "Station.Depth..m."   
## [19] "Flow.meter..revs."    "Broad.Group"          "Mid.Level.Group"     
## [22] "X1st.Word.Taxa"       "Genus.species"        "Life.History.Stage"  
## [25] "Total.Ct"             "Density....m3."       "Vol.Filtered..m3."   
## [28] "Jelly.Mass..g."       "Number.of.Jellies"    "Trial.Time"          
## [31] "Trial.Type"           "Jelly.Size"           "Jelly.Density....m3."
## [34] "Jelly.Density..g.m3." "Location"             "Control_info"        
## [37] "Jelly.Mass"           "Density"              "Jelly.Density"

Subset data to Oithona and add multiple entries per tank:The mean of jelly density is taken because this should be the same number for all taxa in each tank.

Oithona<-subset(Exp_sub, Genus.species == "OITHONA")
Oithona <- Oithona %>%
  group_by(Sample.Code, Station,Sample.Date,Sample.Year,Control_info) %>%
  summarise(
    CopDensity = sum(Density),
    Jelly.Density = mean(Jelly.Density))

Control geometric means

Calculate the geometric mean of the zero-jelly tanks to use as a control.

Oithona$Control_info_combined <- paste(Oithona$Sample.Date, Oithona$Control_info)
Oithona2<- Oithona %>%
  group_by(Control_info,Control_info_combined,Sample.Date) %>%
  summarise(
    ave = exp(mean(log(CopDensity))))
Oithona3<-subset(Oithona2,Control_info=="zero")
# match the geometric mean of zero jelly tanks to each experiment
Oithona$Control<- Oithona3$ave[match(Oithona$Sample.Date, Oithona3$Sample.Date)]

Examine Data

Histograms of Jellyfish Density and Copepod Density

hist(Oithona$CopDensity) 

hist(Oithona$Jelly.Density) 

lograrithmic distribution for both

See if years are different

boxplot(CopDensity~Sample.Year, data=Oithona)

variability for 2019 was higher than 2020

Plot Jellyfish Density against zooplankton density

ggplot(Oithona, aes(x=Jelly.Density, y=CopDensity)) + geom_point(aes(colour=Sample.Date))

Family Options

Negative Binomial Distribution

#round to integers
Oithona$CopDensity_round<-round(as.numeric(Oithona$CopDensity), 0)
Oithona$Control_round<-round(as.numeric(Oithona$Control), 0)
#run model
Oithona_model1 <- glm.nb(CopDensity_round~Jelly.Density, data = Oithona)
summary(Oithona_model1)
## 
## Call:
## glm.nb(formula = CopDensity_round ~ Jelly.Density, data = Oithona, 
##     init.theta = 0.8237634633, link = log)
## 
## Coefficients:
##                 Estimate Std. Error z value Pr(>|z|)    
## (Intercept)    8.976e+00  1.860e-01  48.262  < 2e-16 ***
## Jelly.Density -2.452e-04  7.403e-05  -3.313 0.000924 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for Negative Binomial(0.8238) family taken to be 1)
## 
##     Null deviance: 89.236  on 64  degrees of freedom
## Residual deviance: 76.834  on 63  degrees of freedom
## AIC: 1246.8
## 
## Number of Fisher Scoring iterations: 1
## 
## 
##               Theta:  0.824 
##           Std. Err.:  0.125 
## 
##  2 x log-likelihood:  -1240.810
plot(Oithona_model1)

## Gaussian distribution

Oithona_model2<-glm(CopDensity~Jelly.Density, data= Oithona, family="gaussian")
summary(Oithona_model2)
## 
## Call:
## glm(formula = CopDensity ~ Jelly.Density, family = "gaussian", 
##     data = Oithona)
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   8307.0329  1115.8388   7.445 3.34e-10 ***
## Jelly.Density   -1.4440     0.4441  -3.251  0.00185 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 43706614)
## 
##     Null deviance: 3215481567  on 64  degrees of freedom
## Residual deviance: 2753516682  on 63  degrees of freedom
## AIC: 1332
## 
## Number of Fisher Scoring iterations: 2
plot(Oithona_model2)

Gamma distribution

Oithona_model3<-glm(CopDensity~Jelly.Density, data= Oithona, family="Gamma")
summary(Oithona_model3)
## 
## Call:
## glm(formula = CopDensity ~ Jelly.Density, family = "Gamma", data = Oithona)
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   1.032e-04  2.029e-05   5.088 3.51e-06 ***
## Jelly.Density 7.869e-08  2.079e-08   3.784 0.000346 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for Gamma family taken to be 0.9687105)
## 
##     Null deviance: 108.407  on 64  degrees of freedom
## Residual deviance:  88.172  on 63  degrees of freedom
## AIC: 1243.5
## 
## Number of Fisher Scoring iterations: 6
plot(Oithona_model3)

Gamma is the best so far - move forward with it

Add other parameters

Oithona Final Model

OithonaMod<-glm(CopDensity~Jelly.Density+offset(log(Control)), data= Oithona, family="Gamma"(link="log"))
summary(OithonaMod)
## 
## Call:
## glm(formula = CopDensity ~ Jelly.Density + offset(log(Control)), 
##     family = Gamma(link = "log"), data = Oithona)
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   -2.231e-01  9.413e-02   -2.37   0.0209 *  
## Jelly.Density -1.896e-04  3.747e-05   -5.06 3.89e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for Gamma family taken to be 0.311044)
## 
##     Null deviance: 35.398  on 64  degrees of freedom
## Residual deviance: 26.119  on 63  degrees of freedom
## AIC: 1154.7
## 
## Number of Fisher Scoring iterations: 11
plot(OithonaMod)

Use predict function to predict model values

predictOithona<-ggpredict(
  OithonaMod,
  terms=c("Jelly.Density"),
  ci.lvl = 0.95,
  type = "fe",
  typical = "mean",
  condition = NULL,
  back.transform = TRUE,
  ppd = FALSE,
  vcov.fun = NULL,
  vcov.type = NULL,
  vcov.args = NULL,
  interval = "confidence")

Plot with original values

OithonaPlot<-plot(predictOithona,add.data = TRUE,dot.size=1.5,dot.alpha=0.65)+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), axis.line = element_line(colour = "black"))+theme_classic() +
  scale_x_continuous(breaks=seq(0,7500,1500),expand = c(0, 20), limits = c(-10, 7500)) + 
  scale_y_continuous(breaks=seq(0,30000,6000),expand = c(0, 20), limits = c(-10, 30000))+
  geom_line(size=1.5) +
  geom_ribbon( aes(ymin = conf.low, ymax = conf.high), alpha = .15) +
  xlab(bquote('Jellyfish Biomass ( g /' ~m^3~ ')'))+ylab(bquote('Copepod Density ( individuals /'~m^3~')'))+
  theme(axis.text=element_text(size=15,colour="black"),
        legend.position=c(0.87, 0.8),
        legend.text=element_text(size=15,colour="black"),
        legend.title=element_text(size=15,colour="black"),
        axis.title=element_text(size=15,colour="black"),
        axis.line = element_line(colour = "black"))+theme(plot.title = element_blank())
OithonaPlot

save plot

setwd("/Users/hailaschultz/Dropbox/Other studies/Aurelia project/Data Analysis/output")
ggsave(filename = "Exp_GLM_Oithona.png", plot = OithonaPlot, width = 6, height = 5, device='png', dpi=700)

Plot without original values

OithonaPlot_nodata<-plot(predictOithona)+xlab('Jellyfish Biomass ( g /' ~m^3~ ')')+
  ylab(bquote('Copepod Density ( individuals /'~m^3~')'))+ 
  theme(axis.line = element_line(colour = "black"))+theme_classic() +
  geom_line(size=1) +
  geom_ribbon( aes(ymin = conf.low, ymax = conf.high, color = NULL),
               alpha = .15,show.legend=FALSE) +
  scale_x_continuous(breaks=seq(0,7500,1500),expand = c(0, 20), limits = c(-10, 7500)) + 
  scale_y_continuous(breaks=seq(0,12000,3000),expand = c(0, 20), limits = c(-10, 12000))+
  theme(axis.text=element_text(size=8,colour="black"),
        axis.title=element_text(size=10),
        plot.title=element_blank())+
  theme(legend.position='none')+
  theme(plot.margin=margin(0.5, 0.5, 0.5, 0.5, unit = "cm"))
OithonaPlot_nodata

save plot

setwd("/Users/hailaschultz/Dropbox/Other studies/Aurelia project/Data Analysis/output")
ggsave(filename = "Exp_GLM_Oithona_nodata.png", plot = OithonaPlot_nodata, width = 6, height = 5, device='png', dpi=700)