estuaries <- read.csv("Estuaries.csv", header = T)
library(ggplot2)
library(lme4)
## Loading required package: Matrix
library(lmerTest)
## Warning: package 'lmerTest' was built under R version 4.3.2
## 
## Attaching package: 'lmerTest'
## The following object is masked from 'package:lme4':
## 
##     lmer
## The following object is masked from 'package:stats':
## 
##     step
library(performance)
## Warning: package 'performance' was built under R version 4.3.2
estu.lmer<-lmer(Total ~ Modification + (1|Estuary), data=estuaries)
summary(estu.lmer)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: Total ~ Modification + (1 | Estuary)
##    Data: estuaries
## 
## REML criterion at convergence: 394.7
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -2.3859 -0.7142  0.2766  0.5240  2.0456 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  Estuary  (Intercept) 55.12    7.424   
##  Residual             86.07    9.277   
## Number of obs: 54, groups:  Estuary, 7
## 
## Fixed effects:
##                      Estimate Std. Error      df t value Pr(>|t|)    
## (Intercept)            40.973      4.727   5.090   8.668 0.000309 ***
## ModificationPristine  -14.473      6.230   5.018  -2.323 0.067611 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr)
## MdfctnPrstn -0.759
r2(estu.lmer)
## # R2 for Mixed Models
## 
##   Conditional R2: 0.553
##      Marginal R2: 0.267
ggplot(estuaries, aes(x = Modification, y = Total)) +
  geom_boxplot()+
  geom_jitter(aes(color=Estuary))

ggplot(estuaries, aes(x = Modification, y = Total)) +
  geom_boxplot()+
  geom_jitter()+
  facet_wrap(~Estuary)

estu.lmer.site<-lmer(Total ~ Modification + (1|Estuary/Site), data=estuaries) #implicit nesting
#summary(lmer(Total ~ Modification + (1|Estuary) + (1|Estuary:Site), data=estuaries)) #explicit nesting
summary(estu.lmer.site)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: Total ~ Modification + (1 | Estuary/Site)
##    Data: estuaries
## 
## REML criterion at convergence: 386.6
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.8686 -0.6687  0.1504  0.6505  1.9816 
## 
## Random effects:
##  Groups       Name        Variance Std.Dev.
##  Site:Estuary (Intercept) 49.85    7.061   
##  Estuary      (Intercept) 47.59    6.899   
##  Residual                 43.65    6.607   
## Number of obs: 54, groups:  Site:Estuary, 27; Estuary, 7
## 
## Fixed effects:
##                      Estimate Std. Error      df t value Pr(>|t|)    
## (Intercept)            41.053      4.739   5.143   8.662 0.000294 ***
## ModificationPristine  -14.553      6.232   5.028  -2.335 0.066497 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr)
## MdfctnPrstn -0.760
r2(estu.lmer.site)
## # R2 for Mixed Models
## 
##   Conditional R2: 0.774
##      Marginal R2: 0.270
check_model(estu.lmer.site)

ggplot(estuaries, aes(x = Modification, y = Total)) +
  geom_boxplot()+
  geom_jitter(aes(color=as.factor(Site)))+
  facet_wrap(~Estuary)

ggplot(estuaries, aes(x = Modification, y = Total)) +
  geom_violin()+
  geom_jitter(aes(color=Estuary, shape=as.factor(Site)))

estuaries$HydroidPres <- estuaries$Hydroid > 0
hydro.glmm.site<-glmer(HydroidPres ~ Modification + (1|Estuary/Site), family=binomial, data=estuaries) #implicit nesting
summary(hydro.glmm.site)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: binomial  ( logit )
## Formula: HydroidPres ~ Modification + (1 | Estuary/Site)
##    Data: estuaries
## 
##      AIC      BIC   logLik deviance df.resid 
##     57.1     65.0    -24.5     49.1       50 
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -1.06063 -0.25028 -0.05443  0.25475  0.98719 
## 
## Random effects:
##  Groups       Name        Variance Std.Dev.
##  Site:Estuary (Intercept) 14.45    3.802   
##  Estuary      (Intercept)  1.13    1.063   
## Number of obs: 54, groups:  Site:Estuary, 27; Estuary, 7
## 
## Fixed effects:
##                      Estimate Std. Error z value Pr(>|z|)  
## (Intercept)            -5.710      2.419  -2.360   0.0183 *
## ModificationPristine    6.534      3.140   2.081   0.0375 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr)
## MdfctnPrstn -0.888
r2(hydro.glmm.site)
## # R2 for Mixed Models
## 
##   Conditional R2: 0.888
##      Marginal R2: 0.358
library(performance)
check_model(hydro.glmm.site)

library(DHARMa)
## Warning: package 'DHARMa' was built under R version 4.3.2
## This is DHARMa 0.4.6. For overview type '?DHARMa'. For recent changes, type news(package = 'DHARMa')
simulateResiduals(hydro.glmm.site, plot = T)

## Object of Class DHARMa with simulated residuals based on 250 simulations with refit = FALSE . See ?DHARMa::simulateResiduals for help. 
##  
## Scaled residual values: 0.6382768 0.3853122 0.8086934 0.01626812 0.9424474 0.9426142 0.5324421 0.3949867 0.82084 0.4799594 0.8009645 0.03378411 0.3224195 0.3157432 0.602992 0.6115054 0.276876 0.8050706 0.7838545 0.5106198 ...