# Set wroking directory setwd("C:\\Users\\User\\Desktop\\Current paper drafts\\Pheasants and inverts") search() library(glmmTMB) library(arm) library(MuMIn) library(MCS) library(lme4) library(lmtest) library(nlme) library(mgcv) library(sjPlot) library(influence.ME) library(nlme) library(lmerTest) library(psych) library(car) library(LMERConvenienceFunctions) library(influence.ME) library(glmmTMB) library(sjPlot) library(effects) library(lsmeans) citation() citation(package = lme4, lib.loc = lib.loc) Shifted_Invert_Data <- read.csv("Invert_Data_For_Publishing.csv") Shifted_Invert_Data$Year<-as.factor(Shifted_Invert_Data$Year) Shifted_Invert_Data$Survey.number <- as.factor(as.character(Shifted_Invert_Data$Survey.number)) Shifted_Invert_Data$Percentage.Enhnaced<-as.numeric(Shifted_Invert_Data$Percentage.Enhnaced) detach(Shifted_Invert_Data) attach(Shifted_Invert_Data) # Each heading contains the Base, Density, and Enhanced models for each category. The Invertebrate-Habitat models are at the bottom, # but only the first of the Invertebrate-Habitat models has the predframe for predicting % change as only the first one showed any significance. ##----------------------------- Total Biomass ----------------------------------------- ##### ##Potential DVs Dep_Var <-Shifted_Invert_Data$Matched.Total.Biomass.in Dep_Var <-Shifted_Invert_Data$Matched.Total.Number.in Dep_Var <- Shifted_Invert_Data$Slug.Number.in Dep_Var <- Dep_Var + 0.000001 Dep_Var <- Shifted_Invert_Data$Beetle.Number Dep_Var <- Dep_Var + 0.000001 Dep_Var <- Shifted_Invert_Data$Arachnid.Number Dep_Var <- Dep_Var + 0.000001 Dep_Var <- Shifted_Invert_Data$Detritivore.Number Dep_Var <- Dep_Var + 0.000001 ##For this equation, I've assigned pen as random terms but specified a full model Tot.Count_Base <- glmer( Dep_Var ~ Year + Survey.number + Trap.Location + Survey.number:Trap.Location ##+ Year:Survey.number + Year:Trap.Location ##+ Year:Survey.number:Trap.Location + (1 |Pen.names.Yr) , family = Gamma (link = log) , data = Shifted_Invert_Data , na.action = na.fail , control=glmerControl(optimizer="bobyqa", optCtrl=list(maxfun=2e5)) ) summary(Tot.Count_Base) drop1(Tot.Count_Base, test = "Chisq") ##Check residuals etc qqPlot(residuals(Tot.Count_Base)) plot(residuals(Tot.Count_Base)~fitted(Tot_Bio_Base)) ##Dharma package for checking model residuals by simulation require(lsmeans) lsm <- lsmeans(Tot.Count_Base, ~ Survey.number*Trap.Location) summary(lsm, type = "response") lsmip(lsm, Trap.Location ~ Survey.number, type = "response") ##Top answer from https://stats.stackexchange.com/questions/181034/post-hoc-test-of-interaction-factor-in-binomial-glmm-with-proportions ##Produce model output table tab_model(Tot.Count_Base) theme_set(theme_sjplot2()) + labs(x="Task", y= "Pecks") plot_model(Tot.Count_Base, type = "pred", terms = c( "Survey.number", "Trap.Location"), xlab = "Start and end of training", ylab = "%correct choices") ##Potential DVs Dep_Var <-Shifted_Invert_Data$Matched.Total.Biomass.in Dep_Var <-Shifted_Invert_Data$Matched.Total.Number.in Dep_Var <- Shifted_Invert_Data$Slug.Number.in Dep_Var <- Dep_Var + 0.000001 Dep_Var <- Shifted_Invert_Data$Beetle.Number Dep_Var <- Dep_Var + 0.000001 Dep_Var <- Shifted_Invert_Data$Arachnid.Number Dep_Var <- Dep_Var + 0.000001 Dep_Var <- Shifted_Invert_Data$Detritivore.Number Dep_Var <- Dep_Var + 0.000001 ##Analysis of enhanced bird contribution models excluding Year as a fixed factor Tot.Count_Enh <- glmer (Dep_Var ~ Survey.number + Trap.Location + Percentage.Enhnaced + Survey.number:Trap.Location + Survey.number:Percentage.Enhnaced + Trap.Location:Percentage.Enhnaced +Survey.number:Trap.Location:Percentage.Enhnaced + (1 |Pen.names.Yr) + (1 |Year) , family = Gamma (link = log) , data = Shifted_Invert_Data , na.action = na.fail ##, nAGQ=0 , control=glmerControl(optimizer="bobyqa", optCtrl=list(maxfun=2e5)) ) summary(Tot.Count_Enh) drop1(Tot.Count_Enh, test = "Chisq") ##Produce model output table tab_model(Tot.Count_Enh) ##Analysis of released bird density models excluding Year as a fixed factor Tot.Count_Den <- glmer (Dep_Var ~ Survey.number + Trap.Location + True.Density + Survey.number:Trap.Location + Survey.number:True.Density + Trap.Location:True.Density +Survey.number:Trap.Location:True.Density + (1 |Pen.names.Yr) + (1 |Year) , family = Gamma (link = log) , data = Shifted_Invert_Data , na.action = na.fail , control=glmerControl(optimizer="bobyqa", optCtrl=list(maxfun=2e5)) ) summary(Tot.Count_Den) drop1(Tot.Count_Den, test = "Chisq") ##Produce model output table tab_model(Tot.Count_Den) ##to change reference catefgory [relevel(survey.number,ref=2) ##library(multcomp) ##pairwise<-glht(Tot_Bio_Base,mcp(Survey.number="Tukey")) ##pairwise theme_set(theme_sjplot2()) plot_model(Tot.Count_Base, type = "pred", terms = c( "Survey.number", "Trap.Location", "Year")) require(lsmeans) lsm <- lsmeans(Tot_Bio_Base, ~ Survey.number*Trap.Location) summary(lsm, type = "response") lsmip(lsm, Trap.Location ~ Survey.number, type = "response") ##Top answer from https://stats.stackexchange.com/questions/181034/post-hoc-test-of-interaction-factor-in-binomial-glmm-with-proportions ##Generate means for each category pdat <- expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3")) pred <- predict (Tot_Bio_Base, newdata = expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3")), re.form = ~0, type = "response",se.fit=T) predframe <- data.frame (pdat, preds = pred) predframe Tot.Bio_Den <- glmer(Dep_Var ~ Survey.number + Survey.number:Year + True.Density:Survey.number + Survey.number:Trap.Location:Year + Trap.Location:Survey.number:True.Density + (1 |Pen.names.Yr) , family = Gamma (link = log) , nAGQ=0 , data = Shifted_Invert_Data , na.action = na.fail) summary(Tot.Bio_Den) Tot.Bio_Enh <- glmer( (Dep_Var) ~ Survey.number + Survey.number:Year + Percentage.Enhnaced:Survey.number + Survey.number:Trap.Location:Year + Trap.Location:Survey.number:Percentage.Enhnaced + (1 |Pen.names.Yr) , family = Gamma (link = log) , nAGQ=0 , data = Shifted_Invert_Data , na.action = na.fail) summary(Tot.Bio_Enh) # Values for % results ## For this equation, I've removed year pdat <- expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3")) pred <- predict (Tot_Bio_Base, newdata = expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3")), re.form = ~0, type = "response",se.fit=T) predframe <- data.frame (pdat, preds = pred) predframe # For density specific results pdat <- expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3"), True.Density = c(0.2,0.3), Year = c("1","2")) pred <- predict (Tot.Bio_Den, newdata = expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3"), True.Density = c(0.2,0.3), Year = c("1","2")), re.form = ~0, type = "response",se.fit=T) predframe <- data.frame (pdat, preds = pred) predframe # For Enhanced specific results pdat <- expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3"), Percentage.Enhnaced = c(0,50), Year = c("1","2")) pred <- predict (Tot.Bio_Enh, newdata = expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3"), Percentage.Enhnaced = c(0,50), Year = c("1","2")), re.form = ~0, type = "response",se.fit=T) predframe <- data.frame (pdat, preds = pred) predframe # ------------------------------ Total Count ---------------------------------------------------------------- ##### Dep_Var <- Shifted_Invert_Data$Matched.Total.Number.in Tot.Count_Base <- glmer( Dep_Var ~ Survey.number + Survey.number:Year + Survey.number:Trap.Location:Year + (1 |Pen.names.Yr) , family = Gamma (link = log) , nAGQ=0 , data = Shifted_Invert_Data , na.action = na.fail) summary(Tot.Count_Base) Tot.Count_Den <- glmer(Dep_Var ~ Survey.number + Survey.number:Year + True.Density:Survey.number + Survey.number:Trap.Location:Year + Trap.Location:Survey.number:True.Density + (1 |Pen.names.Yr) , family = Gamma (link = log) , nAGQ=0 , data = Shifted_Invert_Data , na.action = na.fail) summary(Tot.Count_Den) Tot.Count_Enh <- glmer( (Dep_Var) ~ Survey.number + Survey.number:Year + Percentage.Enhnaced:Survey.number + Survey.number:Trap.Location:Year + Trap.Location:Survey.number:Percentage.Enhnaced + (1 |Pen.names.Yr) , family = Gamma (link = log) , nAGQ=0 , data = Shifted_Invert_Data , na.action = na.fail) summary(Tot.Count_Enh) # Values for % results pdat <- expand.grid(Trap.Location = c("Exterior", "Interior"), Year = c("1","2"), Survey.number = c("1","2","3")) pred <- predict (Tot.Count_Base, newdata = expand.grid(Trap.Location = c("Exterior", "Interior"), Year = c("1","2"), Survey.number = c("1","2","3")), re.form = ~0, type = "response",se.fit=T) predframe <- data.frame (pdat, preds = pred) predframe # For density specific results pdat <- expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3"), True.Density = c(0.2,0.3), Year = c("1","2")) pred <- predict (Tot.Count_Den, newdata = expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3"), True.Density = c(0.2,0.3), Year = c("1","2")), re.form = ~0, type = "response",se.fit=T) predframe <- data.frame (pdat, preds = pred) predframe # For Enhanced specific results pdat <- expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3"), Percentage.Enhnaced = c(0,50), Year = c("1","2")) pred <- predict (Tot.Count_Enh, newdata = expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3"), Percentage.Enhnaced = c(0,50), Year = c("1","2")), re.form = ~0, type = "response",se.fit=T) predframe <- data.frame (pdat, preds = pred) predframe #--------------------------------Slug Count-------------------------- ##### Dep_Var <- Shifted_Invert_Data$Slug.Number.in Dep_Var <- Dep_Var + 0.000001 Slug_Count_Base <- glmer( sqrt (Dep_Var) ~ Survey.number + Survey.number:Year + Survey.number:Trap.Location:Year + (1 |Pen.names.Yr) , family = Gamma (link = log) , nAGQ=0 , data = Shifted_Invert_Data , na.action = na.fail) summary(Slug_Count_Base) Slug_Count_Den <- glmer(sqrt (Dep_Var) ~ Survey.number + Survey.number:Year + True.Density:Survey.number + Survey.number:Trap.Location:Year + Trap.Location:Survey.number:True.Density + (1 |Pen.names.Yr) , family = Gamma (link = log) , nAGQ=0 , data = Shifted_Invert_Data , na.action = na.fail) summary(Slug_Count_Den) Slug_Count_Enh <- glmer( sqrt (Dep_Var) ~ Survey.number + Survey.number:Year + Percentage.Enhnaced:Survey.number + Survey.number:Trap.Location:Year + Trap.Location:Survey.number:Percentage.Enhnaced + (1 |Pen.names.Yr) , family = Gamma (link = log) , nAGQ=0 , data = Shifted_Invert_Data , na.action = na.fail) summary(Slug_Count_Enh) # Values for % results pdat <- expand.grid(Trap.Location = c("Exterior", "Interior"), Year = c("1","2"), Survey.number = c("1","2","3")) pred <- predict (Slug_Count_Base, newdata = expand.grid(Trap.Location = c("Exterior", "Interior"), Year = c("1","2"), Survey.number = c("1","2","3")), re.form = ~0, type = "response",se.fit=T) predframe <- data.frame (pdat, preds = pred) predframe # For density specific results pdat <- expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3"), True.Density = c(0.2,0.3), Year = c("1","2")) pred <- predict (Slug_Count_Den, newdata = expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3"), True.Density = c(0.2,0.3), Year = c("1","2")), re.form = ~0, type = "response",se.fit=T) predframe <- data.frame (pdat, preds = pred) predframe # For Enhanced specific results # For Enhanced specific results pdat <- expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3"), Percentage.Enhnaced = c(0,50), Year = c("1","2")) pred <- predict (Slug_Count_Enh, newdata = expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3"), Percentage.Enhnaced = c(0,50), Year = c("1","2")), re.form = ~0, type = "response",se.fit=T) predframe <- data.frame (pdat, preds = pred) predframe #-----------------------------Beetle Count------------------------------------- ##### Dep_Var <- Shifted_Invert_Data$Beetle.Number Dep_Var <- Dep_Var + 0.000001 Beetle_Count_Base <- glmer(Dep_Var ~ Survey.number + Survey.number:Year + Survey.number:Trap.Location:Year + (1 |Pen.names.Yr) , family = Gamma (link = log) , nAGQ=0 , data = Shifted_Invert_Data , na.action = na.fail) summary(Beetle_Count_Base) Beetle_Count_Den <- glmer(Dep_Var ~ Survey.number + Survey.number:Year + True.Density:Survey.number + Survey.number:Trap.Location:Year + Trap.Location:Survey.number:True.Density + (1 |Pen.names.Yr) , family = Gamma (link = log) , nAGQ=0 , data = Shifted_Invert_Data , na.action = na.fail) summary(Beetle_Count_Den) Beetle_Count_Enh <- glmer( (Dep_Var) ~ Survey.number + Survey.number:Year + Percentage.Enhnaced:Survey.number + Survey.number:Trap.Location:Year + Trap.Location:Survey.number:Percentage.Enhnaced + (1 |Pen.names.Yr) , family = Gamma (link = log) , nAGQ=0 , data = Shifted_Invert_Data , na.action = na.fail) summary(Beetle_Count_Enh) # Values for % results pdat <- expand.grid(Trap.Location = c("Exterior", "Interior"), Year = c("1","2"), Survey.number = c("1","2","3")) pred <- predict (Beetle_Count_Base, newdata = expand.grid(Trap.Location = c("Exterior", "Interior"), Year = c("1","2"), Survey.number = c("1","2","3")), re.form = ~0, type = "response",se.fit=T) predframe <- data.frame (pdat, preds = pred) predframe # For density specific results pdat <- expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3"), True.Density = c(0.2,0.3), Year = c("1","2")) pred <- predict (Beetle_Count_Den, newdata = expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3"), True.Density = c(0.2,0.3), Year = c("1","2")), re.form = ~0, type = "response",se.fit=T) predframe <- data.frame (pdat, preds = pred) predframe # For Enhanced specific results pdat <- expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3"), Percentage.Enhnaced = c(0,50), Year = c("1","2")) pred <- predict (Beetle_Count_Enh, newdata = expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3"), Percentage.Enhnaced = c(0,50), Year = c("1","2")), re.form = ~0, type = "response",se.fit=T) predframe <- data.frame (pdat, preds = pred) predframe #-----------------------------Arachnid Count----------------------------------------- ##### Dep_Var <- Shifted_Invert_Data$Arachnid.Number Dep_Var <- Dep_Var + 0.000001 Arachnid_Count_Base <- glmer(sqrt (Dep_Var) ~ Survey.number + Survey.number:Year + Survey.number:Trap.Location:Year + (1 |Pen.names.Yr) , family = Gamma (link = log) , nAGQ=0 , data = Shifted_Invert_Data , na.action = na.fail) summary(Arachnid_Count_Base) Arachnid_Count_Den <- glmer( sqrt (Dep_Var) ~ Survey.number + Survey.number:Year + True.Density:Survey.number + Survey.number:Trap.Location:Year + Trap.Location:Survey.number:True.Density + (1 |Pen.names.Yr) , family = Gamma (link = log) , nAGQ=0 , data = Shifted_Invert_Data , na.action = na.fail) summary(Arachnid_Count_Den) Arachnid_Count_Enh <- glmer( sqrt(Dep_Var) ~ Survey.number + Survey.number:Year + Percentage.Enhnaced:Survey.number + Survey.number:Trap.Location:Year + Trap.Location:Survey.number:Percentage.Enhnaced + (1 |Pen.names.Yr) , family = Gamma (link = log) , nAGQ=0 , data = Shifted_Invert_Data , na.action = na.fail) summary(Arachnid_Count_Enh) # Values for % results pdat <- expand.grid(Trap.Location = c("Exterior", "Interior"), Year = c("1","2"), Survey.number = c("1","2","3")) pred <- predict (Arachnid_Count_Base, newdata = expand.grid(Trap.Location = c("Exterior", "Interior"), Year = c("1","2"), Survey.number = c("1","2","3")), re.form = ~0, type = "response",se.fit=T) predframe <- data.frame (pdat, preds = pred) predframe # For density specific results pdat <- expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3"), True.Density = c(0.2,0.3), Year = c("1","2")) pred <- predict (Arachnid_Count_Den, newdata = expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3"), True.Density = c(0.2,0.3), Year = c("1","2")), re.form = ~0, type = "response",se.fit=T) predframe <- data.frame (pdat, preds = pred) predframe # For Enhanced specific results pdat <- expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3"), Percentage.Enhnaced = c(0,50), Year = c("1","2")) pred <- predict (Arachnid_Count_Enh, newdata = expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3"), Percentage.Enhnaced = c(0,50), Year = c("1","2")), re.form = ~0, type = "response",se.fit=T) predframe <- data.frame (pdat, preds = pred) predframe #--------------------------Detritivore Count--------------------------------------- ##### Dep_Var <- Shifted_Invert_Data$Detritivore.Number Dep_Var <- Dep_Var + 0.000001 Det_Count_Base <- glmer( sqrt (Dep_Var) ~ Survey.number + Survey.number:Year + Survey.number:Trap.Location:Year + (1 |Pen.names.Yr) , family = Gamma (link = log) , nAGQ=0 , data = Shifted_Invert_Data , na.action = na.fail) summary(Det_Count_Base) Det_Count_Den <- glmer( sqrt (Dep_Var) ~ Survey.number + Survey.number:Year + True.Density:Survey.number + Survey.number:Trap.Location:Year + Trap.Location:Survey.number:True.Density + (1 |Pen.names.Yr) , family = Gamma (link = log) , nAGQ=0 , data = Shifted_Invert_Data , na.action = na.fail) summary(Det_Count_Den) Det_Count_Enh <- glmer( sqrt (Dep_Var) ~ Survey.number + Survey.number:Year + Percentage.Enhnaced:Survey.number + Survey.number:Trap.Location:Year + Trap.Location:Survey.number:Percentage.Enhnaced + (1 |Pen.names.Yr) , family = Gamma (link = log) , nAGQ=0 , data = Shifted_Invert_Data , na.action = na.fail) summary(Det_Count_Enh) # Values for % results pdat <- expand.grid(Trap.Location = c("Exterior", "Interior"), Year = c("1","2"), Survey.number = c("1","2","3")) pred <- predict (Det_Count_Base, newdata = expand.grid(Trap.Location = c("Exterior", "Interior"), Year = c("1","2"), Survey.number = c("1","2","3")), re.form = ~0, type = "response",se.fit=T) predframe <- data.frame (pdat, preds = pred) predframe # For density specific results pdat <- expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3"), True.Density = c(0.2,0.3), Year = c("1","2")) pred <- predict (Det_Count_Den, newdata = expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3"), True.Density = c(0.2,0.3), Year = c("1","2")), re.form = ~0, type = "response",se.fit=T) predframe <- data.frame (pdat, preds = pred) predframe # For Enhanced specific results pdat <- expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3"), Percentage.Enhnaced = c(0,50), Year = c("1","2")) pred <- predict (Det_Count_Enh, newdata = expand.grid(Trap.Location = c("Exterior", "Interior"), Survey.number = c("1","2","3"), Percentage.Enhnaced = c(0,50), Year = c("1","2")), re.form = ~0, type = "response",se.fit=T) predframe <- data.frame (pdat, preds = pred) predframe