## package 'tidyverse' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\aste490\AppData\Local\Temp\Rtmpmyt8Zc\downloaded_packages
## package 'dplyr' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'dplyr'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\aste490\AppData\Local\R\win-library\4.4\00LOCK\dplyr\libs\x64\dplyr.dll
## to C:\Users\aste490\AppData\Local\R\win-library\4.4\dplyr\libs\x64\dplyr.dll:
## Permission denied
## Warning: restored 'dplyr'
## 
## The downloaded binary packages are in
##  C:\Users\aste490\AppData\Local\Temp\Rtmpmyt8Zc\downloaded_packages
## package 'detect' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\aste490\AppData\Local\Temp\Rtmpmyt8Zc\downloaded_packages
## package 'MASS' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'MASS'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\aste490\AppData\Local\R\win-library\4.4\00LOCK\MASS\libs\x64\MASS.dll
## to C:\Users\aste490\AppData\Local\R\win-library\4.4\MASS\libs\x64\MASS.dll:
## Permission denied
## Warning: restored 'MASS'
## 
## The downloaded binary packages are in
##  C:\Users\aste490\AppData\Local\Temp\Rtmpmyt8Zc\downloaded_packages
## package 'ggpubr' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\aste490\AppData\Local\Temp\Rtmpmyt8Zc\downloaded_packages
## package 'rstatix' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\aste490\AppData\Local\Temp\Rtmpmyt8Zc\downloaded_packages
## package 'ggcorrplot' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\aste490\AppData\Local\Temp\Rtmpmyt8Zc\downloaded_packages
## package 'lme4' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'lme4'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\aste490\AppData\Local\R\win-library\4.4\00LOCK\lme4\libs\x64\lme4.dll
## to C:\Users\aste490\AppData\Local\R\win-library\4.4\lme4\libs\x64\lme4.dll:
## Permission denied
## Warning: restored 'lme4'
## 
## The downloaded binary packages are in
##  C:\Users\aste490\AppData\Local\Temp\Rtmpmyt8Zc\downloaded_packages
## package 'FSA' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\aste490\AppData\Local\Temp\Rtmpmyt8Zc\downloaded_packages
## package 'factoextra' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\aste490\AppData\Local\Temp\Rtmpmyt8Zc\downloaded_packages
## package 'pheatmap' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\aste490\AppData\Local\Temp\Rtmpmyt8Zc\downloaded_packages
## package 'dendextend' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\aste490\AppData\Local\Temp\Rtmpmyt8Zc\downloaded_packages
## package 'vegan' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'vegan'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\aste490\AppData\Local\R\win-library\4.4\00LOCK\vegan\libs\x64\vegan.dll
## to C:\Users\aste490\AppData\Local\R\win-library\4.4\vegan\libs\x64\vegan.dll:
## Permission denied
## Warning: restored 'vegan'
## 
## The downloaded binary packages are in
##  C:\Users\aste490\AppData\Local\Temp\Rtmpmyt8Zc\downloaded_packages
## package 'caret' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'caret'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\aste490\AppData\Local\R\win-library\4.4\00LOCK\caret\libs\x64\caret.dll
## to C:\Users\aste490\AppData\Local\R\win-library\4.4\caret\libs\x64\caret.dll:
## Permission denied
## Warning: restored 'caret'
## 
## The downloaded binary packages are in
##  C:\Users\aste490\AppData\Local\Temp\Rtmpmyt8Zc\downloaded_packages
## package 'glmnet' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'glmnet'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\aste490\AppData\Local\R\win-library\4.4\00LOCK\glmnet\libs\x64\glmnet.dll
## to C:\Users\aste490\AppData\Local\R\win-library\4.4\glmnet\libs\x64\glmnet.dll:
## Permission denied
## Warning: restored 'glmnet'
## 
## The downloaded binary packages are in
##  C:\Users\aste490\AppData\Local\Temp\Rtmpmyt8Zc\downloaded_packages
## package 'pheatmap' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\aste490\AppData\Local\Temp\Rtmpmyt8Zc\downloaded_packages
## package 'mgcv' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'mgcv'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\aste490\AppData\Local\R\win-library\4.4\00LOCK\mgcv\libs\x64\mgcv.dll
## to C:\Users\aste490\AppData\Local\R\win-library\4.4\mgcv\libs\x64\mgcv.dll:
## Permission denied
## Warning: restored 'mgcv'
## 
## The downloaded binary packages are in
##  C:\Users\aste490\AppData\Local\Temp\Rtmpmyt8Zc\downloaded_packages
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.4.3
## Warning: package 'ggplot2' was built under R version 4.4.2
## Warning: package 'tibble' was built under R version 4.4.2
## Warning: package 'tidyr' was built under R version 4.4.2
## Warning: package 'readr' was built under R version 4.4.3
## Warning: package 'purrr' was built under R version 4.4.2
## Warning: package 'dplyr' was built under R version 4.4.3
## Warning: package 'stringr' was built under R version 4.4.2
## Warning: package 'forcats' was built under R version 4.4.2
## Warning: package 'lubridate' was built under R version 4.4.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
library(ggplot2)
library(MASS)
## Warning: package 'MASS' was built under R version 4.4.3
## 
## Attaching package: 'MASS'
## 
## The following object is masked from 'package:dplyr':
## 
##     select
library(lme4)
## Warning: package 'lme4' was built under R version 4.4.3
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## 
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
library(car)
## Warning: package 'car' was built under R version 4.4.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.4.2
## 
## Attaching package: 'car'
## 
## The following object is masked from 'package:dplyr':
## 
##     recode
## 
## The following object is masked from 'package:purrr':
## 
##     some
library(ggpubr)
## Warning: package 'ggpubr' was built under R version 4.4.3
library(rstatix)
## Warning: package 'rstatix' was built under R version 4.4.3
## 
## Attaching package: 'rstatix'
## 
## The following object is masked from 'package:MASS':
## 
##     select
## 
## The following object is masked from 'package:stats':
## 
##     filter
library(tidyr)
library(corrplot)
## Warning: package 'corrplot' was built under R version 4.4.2
## corrplot 0.95 loaded
library(ggcorrplot)
## Warning: package 'ggcorrplot' was built under R version 4.4.3
## 
## Attaching package: 'ggcorrplot'
## 
## The following object is masked from 'package:rstatix':
## 
##     cor_pmat
library(vegan)
## Warning: package 'vegan' was built under R version 4.4.3
## Loading required package: permute
## Warning: package 'permute' was built under R version 4.4.2
## Loading required package: lattice
library(caret)
## Warning: package 'caret' was built under R version 4.4.3
## 
## Attaching package: 'caret'
## 
## The following object is masked from 'package:vegan':
## 
##     tolerance
## 
## The following object is masked from 'package:purrr':
## 
##     lift
library(glmnet)
## Warning: package 'glmnet' was built under R version 4.4.3
## Loaded glmnet 4.1-8
library(pheatmap)
## Warning: package 'pheatmap' was built under R version 4.4.3
library(mgcv)
## Warning: package 'mgcv' was built under R version 4.4.3
## Loading required package: nlme
## 
## Attaching package: 'nlme'
## 
## The following object is masked from 'package:lme4':
## 
##     lmList
## 
## The following object is masked from 'package:dplyr':
## 
##     collapse
## 
## This is mgcv 1.9-1. For overview type 'help("mgcv-package")'.
# Load the data
setwd("C:/Users/aste490/OneDrive - Washington State Executive Branch Agencies/Documents/Data/2025 Project/This One")
fish_2023 <- read.csv("2023_fish.csv")
fish_2024 <- read.csv("2024_fish.csv")
HU_23 <- read.csv("HU_23.csv")
HU_24 <- read.csv("HU_24.csv")
canopy <- read.csv("canopy.csv")
peri_harvest <- read.csv("peri_prescription.csv")
macros <- read.csv("Macros.csv")
twoyr_peri <- read.csv("Biyearly_peri.csv")
benthic <- read.csv("Benthic.csv")
env <- read.csv("Flow_Year.csv")
all_biomass <- read.csv("all_biomass.csv")

#NOTE THAT BP IS OMITTED DUE TO CONSISTENT DRY 60M BOTH YEARS - NOT USABLE
#periphyton <- ug/100m^2
#identify structure, outliers, distribution, normality

str(fish_2023)
## 'data.frame':    1250 obs. of  5 variables:
##  $ Basin  : chr  "AA1_REACH" "AA1_REACH" "AA1_REACH" "AA1_REACH" ...
##  $ Pass   : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ Species: chr  "CTT" "CTT" "TRT" "CTT" ...
##  $ Length : num  99 81 75 92 89 114 101 94 62 81 ...
##  $ Weight : num  9.6 6.2 5.9 7.9 8.8 16.6 13.3 10.3 3.8 6.9 ...
str(fish_2024)
## 'data.frame':    1172 obs. of  5 variables:
##  $ Basin  : chr  "AA1_REACH" "AA1_REACH" "AA1_REACH" "AA1_REACH" ...
##  $ Pass   : int  1 1 1 2 3 1 1 1 1 1 ...
##  $ Species: chr  "CTT" "CTT" "CTT" "CTT" ...
##  $ Length : int  100 111 96 120 99 53 48 100 39 62 ...
##  $ Weight : num  10.3 15.4 11.3 19.1 12.2 2.5 1.7 13.1 0.9 3.8 ...
str(HU_23)
## 'data.frame':    18 obs. of  5 variables:
##  $ Basin       : chr  "AA1_REACH" "AA2_REACH" "AC_REACH" "AP_REACH" ...
##  $ Reach_Length: num  80.7 119.7 111.2 86.7 98.5 ...
##  $ Total_Area  : num  9.35 38.79 91.28 14.7 69.63 ...
##  $ Area_m2     : num  92.4 271.2 376.1 137.3 360.8 ...
##  $ Yearly_Avg  : num  12166 26491 39297 8264 54385 ...
str(HU_24)
## 'data.frame':    18 obs. of  5 variables:
##  $ Basin       : chr  "AA1_REACH" "AA2_REACH" "AC_REACH" "AP_REACH" ...
##  $ Reach_Length: num  74.2 112.2 105 94.5 103.7 ...
##  $ Total_Area  : num  9.88 44.4 86.61 50.08 178.98 ...
##  $ Area_m2     : num  133 313 349 255 594 ...
##  $ Yearly_Avg  : num  11911 14564 30512 4994 12700 ...
colSums(is.na(fish_2023))
##   Basin    Pass Species  Length  Weight 
##       0       0       0       0       0
colSums(is.na(fish_2024))
##   Basin    Pass Species  Length  Weight 
##       0       0       0       0       0
colSums(is.na(HU_23))
##        Basin Reach_Length   Total_Area      Area_m2   Yearly_Avg 
##            0            0            0            0            0
colSums(is.na(HU_24))
##        Basin Reach_Length   Total_Area      Area_m2   Yearly_Avg 
##            0            0            0            0            0
# Boxplots for fish length and weight
ggplot(fish_2023, aes(y = Length)) + geom_boxplot() + ggtitle("Fish Lengths (2023)")

ggplot(fish_2023, aes(y = Weight)) + geom_boxplot() + ggtitle("Fish Weights (2023)")

ggplot(fish_2024, aes(y = Length)) + geom_boxplot() + ggtitle("Fish Lengths (2024)")

ggplot(fish_2024, aes(y = Weight)) + geom_boxplot() + ggtitle("Fish Weights (2024)")

ggplot(HU_23, aes(y = Yearly_Avg)) + geom_boxplot() + ggtitle("Chlorophyll_Yearly (2023)")

ggplot(HU_24, aes(y = Yearly_Avg)) + geom_boxplot() + ggtitle("Chlorophyll_Yearly (2024)")

#Results: Chlorophyll have a few outliers for both years except for 2024 yearly averages. Fish are all over the place, but are likely true outliers. Checking with z-scores.

# Calculate Z-scores
fish_2023 <- fish_2023 %>% mutate(
  z_length = as.numeric(scale(Length)), 
  z_weight = as.numeric(scale(Weight))
)

fish_2024 <- fish_2024 %>% mutate(
  z_length = as.numeric(scale(Length)),
  z_weight = as.numeric(scale(Weight))
)

HU_23 <- HU_23 %>% mutate(z_chl_yr = scale(Yearly_Avg))
HU_24 <- HU_24 %>% mutate(z_chl_yr = scale(Yearly_Avg))

# Find extreme outliers (Z-score > 3 or < -3)
fish_2023 %>% filter(abs(z_length) > 3 | abs(z_weight) > 3)
##        Basin Pass Species Length Weight z_length   z_weight
## 1   AC_REACH    1     CTT    157   34.1 2.994786  3.6686916
## 2   AZ_REACH    1     CTT    156   36.9 2.959988  4.0335582
## 3   AZ_REACH    1     CTT    162   37.6 3.168773  4.1247749
## 4   AZ_REACH    1     CTT    144   31.0 2.542419  3.2647322
## 5   AZ_REACH    1     CTT    162   47.6 3.168773  5.4278697
## 6   AZ_REACH    1     CTT    190   69.2 4.143102  8.2425547
## 7   AZ_REACH    1     CTT    137   30.6 2.298837  3.2126084
## 8   AZ_REACH    2     CTT    159   43.7 3.064381  4.9196627
## 9   AZ_REACH    3     CTT    150   33.7 2.751203  3.6165678
## 10 BA2_REACH    1     CTT    168   37.9 3.377558  4.1638677
## 11 CA2_REACH    1     CTT    159   38.9 3.064381  4.2941772
## 12 CA2_REACH    1     CTT    194   80.0 4.282291  9.6498972
## 13 CA2_REACH    1     SCP    161    2.7 3.133975 -0.4230263
## 14 CA2_REACH    1     CTT    154   37.8 2.890393  4.1508367
## 15 CA2_REACH    4     CTT    139   30.8 2.368431  3.2386703
## 16  CP_REACH    1     CTT    147   35.4 2.646811  3.8380940
## 17  CP_REACH    1     CTT    143   30.1 2.507621  3.1474537
## 18  CP_REACH    1     CTT    160   47.3 3.099178  5.3887769
## 19  CP_REACH    1     CTT    168   46.1 3.377558  5.2324055
## 20  CP_REACH    1     CTT    144   35.4 2.542419  3.8380940
## 21  CP_REACH    1     CTT    145   34.8 2.577216  3.7599083
## 22  DC_REACH    1     CTT    158   36.0 3.029583  3.9162797
## 23  DZ_REACH    1     CTT    157   44.1 2.994786  4.9717865
## 24  DZ_REACH    1     CTT    184   71.8 3.934317  8.5813594
fish_2024 %>% filter(abs(z_length) > 3 | abs(z_weight) > 3)
##        Basin Pass Species Length Weight z_length  z_weight
## 1   AC_REACH    1     CTT    156   38.0 3.007443  3.380516
## 2   AZ_REACH    1     CTT    150   43.5 2.784609  3.980993
## 3   AZ_REACH    1     CTT    180   73.3 3.898782  7.234483
## 4   AZ_REACH    1     CTT    150   37.8 2.784609  3.358681
## 5   AZ_REACH    1     CTT    189  112.8 4.233034 11.546996
## 6   AZ_REACH    2     CTT    185   68.2 4.084477  6.677678
## 7   AZ_REACH    2     CTT    236  134.0 5.978572 13.861559
## 8  BA2_REACH    1     CTT    152   39.9 2.858887  3.587954
## 9  CA2_REACH    1     CTT    156   38.0 3.007443  3.380516
## 10 CA2_REACH    1     CTT    167   50.0 3.415973  4.690647
## 11  CC_REACH    1     CTT    159   37.2 3.118861  3.293174
## 12  CP_REACH    1     CTT    155   37.5 2.970304  3.325928
## 13  CP_REACH    1     CTT    150   37.3 2.784609  3.304092
## 14  CP_REACH    1     CTT    152   37.7 2.858887  3.347763
## 15 CZ2_REACH    1     CTT    162   49.9 3.230278  4.679729
## 16 DA2_REACH    1     CTT    148   37.3 2.710331  3.304092
## 17  DZ_REACH    1     CTT    175   72.6 3.713086  7.158059
HU_23 %>% filter(abs(z_chl_yr) > 3)
## Warning: Using one column matrices in `filter()` was deprecated in dplyr 1.1.0.
## ℹ Please use one dimensional logical vectors instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## [1] Basin        Reach_Length Total_Area   Area_m2      Yearly_Avg  
## [6] z_chl_yr    
## <0 rows> (or 0-length row.names)
HU_24 %>% filter(abs(z_chl_yr) > 3)
##      Basin Reach_Length Total_Area Area_m2 Yearly_Avg z_chl_yr
## 1 BZ_REACH         86.1       30.4   194.7   54641.64 3.014104
#All extreme outliers for fish data are true biological observations - no transformation or exclusion needed. No extreme outliers for chlorophyll.

#histograms for distribution
ggplot(fish_2023, aes(x = Length)) + geom_histogram(bins = 30, fill = "blue", alpha = 0.5) + ggtitle("Fish Lengths (2023)")

ggplot(fish_2023, aes(x = Weight)) + geom_histogram(bins = 30, fill = "red", alpha = 0.5) + ggtitle("Fish Weights (2023)")

ggplot(fish_2024, aes(x = Length)) + geom_histogram(bins = 30, fill = "lightblue", alpha = 0.5) + ggtitle("Fish Lengths (2024)")

ggplot(fish_2024, aes(x = Weight)) + geom_histogram(bins = 30, fill = "pink", alpha = 0.5) + ggtitle("Fish Weights (2024)")

ggplot(HU_23, aes(x = Yearly_Avg)) + geom_histogram(bins = 30, fill = "purple", alpha = 0.5) + ggtitle("Chlorophyll_Yearly (2023)") 

ggplot(HU_24, aes(x = Yearly_Avg)) + geom_histogram(bins = 30, fill = "orange", alpha = 0.5) + ggtitle("Chlorophyll_Yearly (2024)")

#Results: Fish weights and lengths for both years are right skewed, understandably (majority juveniles). Chlorophyll yearly averages seem to be right skewed as well in 2023, indicating lower values were more common. 2024 chlorophyll data looks a little all over the place - which makes sense due to harvest treatments being completed and disturbances abound.

#Shapiro-wilk normality tests
shapiro.test(fish_2023$Length)
## 
##  Shapiro-Wilk normality test
## 
## data:  fish_2023$Length
## W = 0.91638, p-value < 2.2e-16
shapiro.test(fish_2023$Weight)
## 
##  Shapiro-Wilk normality test
## 
## data:  fish_2023$Weight
## W = 0.67106, p-value < 2.2e-16
shapiro.test(fish_2024$Length)
## 
##  Shapiro-Wilk normality test
## 
## data:  fish_2024$Length
## W = 0.92613, p-value < 2.2e-16
shapiro.test(fish_2024$Weight)
## 
##  Shapiro-Wilk normality test
## 
## data:  fish_2024$Weight
## W = 0.5922, p-value < 2.2e-16
shapiro.test(HU_23$Yearly_Avg)
## 
##  Shapiro-Wilk normality test
## 
## data:  HU_23$Yearly_Avg
## W = 0.78261, p-value = 0.0008716
shapiro.test(HU_24$Yearly_Avg)
## 
##  Shapiro-Wilk normality test
## 
## data:  HU_24$Yearly_Avg
## W = 0.86414, p-value = 0.01427
#Results:All fish data and 2023 chlorophyll data reject null hypothesis of normally distributed data. Normally distributed data is necessary for likely used tests in this analysis - ANOVA, linear correlation, linear regression, etc.

#Quantile plots, why not?

qqnorm(fish_2023$Length)
qqline(fish_2023$Length, col = "blue")

qqnorm(fish_2023$Weight)
qqline(fish_2023$Weight, col = "red")

qqnorm(fish_2024$Length)
qqline(fish_2024$Length, col = "lightblue")

qqnorm(fish_2024$Weight)
qqline(fish_2024$Weight, col = "pink")

qqnorm(HU_23$Yearly_Avg)
qqline(HU_23$Yearly_Avg, col = "purple")

qqnorm(HU_24$Yearly_Avg)
qqline(HU_24$Yearly_Avg, col = "orange" )

#Results: All fish data is right skewed, Chlorophyll 2023 data is also slightly right skewed with a few high outliers, 2024 chlorophyll data looks the closest to normal, although the summed values per reach per 100m shows some outliers at the high. Outliers, although not identified as extreme, are consistent. Az and Cp reaches in 2023 did have consistently high fluorescence values, so this is an ecologically sound representation.

 # Log-transforming all chlorophyll columns for both HU_23 and HU_24 datasets
HU_23 <- HU_23 %>%
  mutate(
    Yearly_Avg = log(Yearly_Avg)     # Log transform of Yearly_Avg
  )

HU_24 <- HU_24 %>%
  mutate(
    Yearly_Avg = log(Yearly_Avg)     # Log transform of Yearly_Avg
  )

#check distribution

ggplot(HU_23, aes(x = Yearly_Avg)) + geom_histogram(bins = 30, fill = "purple", alpha = 0.5) + ggtitle("Chlorophyll_Yearly_log (2023)") 

ggplot(HU_24, aes(x = Yearly_Avg)) + geom_histogram(bins = 30, fill = "orange", alpha = 0.5) + ggtitle("Chlorophyll_Yearly_log (2024)")

shapiro.test(HU_23$Yearly_Avg)
## 
##  Shapiro-Wilk normality test
## 
## data:  HU_23$Yearly_Avg
## W = 0.94407, p-value = 0.3397
shapiro.test(HU_24$Yearly_Avg)
## 
##  Shapiro-Wilk normality test
## 
## data:  HU_24$Yearly_Avg
## W = 0.98096, p-value = 0.9594
#scale periphyton data for comparability.

HU_23$Yearly_Avg <- scale(HU_23$Yearly_Avg)
HU_24$Yearly_Avg <- scale(HU_24$Yearly_Avg)
# Function to calculate length-weight coefficients
coefficients23 <- function(df) {
  # Ensure length and weight are numeric before applying log()
  df$log_lengths <- log(as.numeric(df$Length))
  df$log_weights <- log(as.numeric(df$Weight))

  # Fit linear regression
  model <- lm(log_weights ~ log_lengths, data = df)
  log_a <- coef(model)[1]  # Intercept
  b <- coef(model)[2]      # Slope
  
  # Transform intercept back from log scale
  a <- exp(log_a)
  
  # Return both coefficients and transformed data
  return(list(coefficients = data.frame(a = a, b = b), transformed_data = df))
}

# Apply the function to the dataset
result <- coefficients23(fish_2023)

# Calculate coefficients for each species within each basin
coef_sp_reach23 <- fish_2023 %>%
  group_by(Basin, Species) %>%
  summarise(
    a = coefficients23(cur_data())$coefficients$a,
    b = coefficients23(cur_data())$coefficients$b,
    .groups = "drop"
  )
## Warning: There was 1 warning in `summarise()`.
## ℹ In argument: `a = coefficients23(cur_data())$coefficients$a`.
## ℹ In group 1: `Basin = "AA1_REACH"` and `Species = "CTT"`.
## Caused by warning:
## ! `cur_data()` was deprecated in dplyr 1.1.0.
## ℹ Please use `pick()` instead.
# Ensure length and weight are numeric before performing calculations
fish_2023_wbiomass <- fish_2023 %>%
  mutate(
    Length = as.numeric(Length),   # Convert length to numeric
    Weight = as.numeric(Weight)    # Convert weight to numeric
  ) %>%
  left_join(coef_sp_reach23, by = c("Basin", "Species")) %>%  # Add coefficients (a and b)
  mutate(
    predicted_biomass = a * (Length^b)  # Predict weight
  )

# Summarize fish biomass by Basin and Species FIRST
avg_biomass23 <- fish_2023_wbiomass %>%
  group_by(Basin, Species) %>%
  summarise(
    avg_biomass = mean(predicted_biomass, na.rm = TRUE),  # Average biomass
    total_biomass = sum(predicted_biomass, na.rm = TRUE), # Total biomass
    .groups = "drop"
  )

# Merge summarized biomass data with HU_24
avg_biomass23 <- avg_biomass23 %>%
  left_join(HU_23, by = "Basin") %>%  # Left join ensures no duplicate expansion
  mutate(biomass_reach = (total_biomass / Area_m2)) # Biomass per m^2

# Print to verify unique rows
print(avg_biomass23)
## # A tibble: 49 × 10
##    Basin     Species avg_biomass total_biomass Reach_Length Total_Area Area_m2
##    <chr>     <chr>         <dbl>         <dbl>        <dbl>      <dbl>   <dbl>
##  1 AA1_REACH CTT           9.36         122.           80.7       9.35    92.4
##  2 AA1_REACH TRT           4.85           9.70         80.7       9.35    92.4
##  3 AA2_REACH CTT          10.1          294.          120.       38.8    271. 
##  4 AA2_REACH SCP           2.26          15.8         120.       38.8    271. 
##  5 AA2_REACH TRT           2.39         115.          120.       38.8    271. 
##  6 AC_REACH  CTT          13.4          576.          111.       91.3    376. 
##  7 AC_REACH  TRT           0.927         25.0         111.       91.3    376. 
##  8 AP_REACH  CTT           7.58         106.           86.7      14.7    137. 
##  9 AP_REACH  TRT           3.38          16.9          86.7      14.7    137. 
## 10 AZ_REACH  CTT          22.1          706.           98.5      69.6    361. 
## # ℹ 39 more rows
## # ℹ 3 more variables: Yearly_Avg <dbl[,1]>, z_chl_yr <dbl[,1]>,
## #   biomass_reach <dbl>
#Repeat with 2024 data

# Function to calculate length-weight coefficients
coefficients24 <- function(df) {
  # Ensure length and weight are numeric before applying log()
  df$log_lengths <- log(as.numeric(df$Length))
  df$log_weights <- log(as.numeric(df$Weight))

  # Fit linear regression
  model <- lm(log_weights ~ log_lengths, data = df)
  log_a <- coef(model)[1]  # Intercept
  b <- coef(model)[2]      # Slope
  
  # Transform intercept back from log scale
  a <- exp(log_a)
  
  # Return both coefficients and transformed data
  return(list(coefficients = data.frame(a = a, b = b), transformed_data = df))
}

# Apply the function to the dataset
result <- coefficients24(fish_2024)

# Calculate coefficients for each species within each basin
coef_sp_reach24 <- fish_2024 %>%
  group_by(Basin, Species) %>%
  summarise(
    a = coefficients24(cur_data())$coefficients$a,
    b = coefficients24(cur_data())$coefficients$b,
    .groups = "drop"
  )

# Ensure length and weight are numeric before performing calculations
fish_2024_wbiomass <- fish_2024 %>%
  mutate(
    Length = as.numeric(Length),   # Convert length to numeric
    Weight = as.numeric(Weight)    # Convert weight to numeric
  ) %>%
  left_join(coef_sp_reach24, by = c("Basin", "Species")) %>%  # Add coefficients (a and b)
  mutate(
    predicted_biomass = a * (Length^b)  # Predict weight
  )

# Summarize fish biomass by Basin and Species FIRST
avg_biomass24 <- fish_2024_wbiomass %>%
  group_by(Basin, Species) %>%
  summarise(
    avg_biomass = mean(predicted_biomass, na.rm = TRUE),  # Average biomass
    total_biomass = sum(predicted_biomass, na.rm = TRUE), # Total biomass
    .groups = "drop"
  )

# Merge summarized biomass data with HU_24
avg_biomass24 <- avg_biomass24 %>%
  left_join(HU_24, by = "Basin") %>%  # Left join ensures no duplicate expansion
  mutate(biomass_reach = (total_biomass / Area_m2)) # Biomass per m2

# Print to verify unique rowsprint(avg_biomass24)
# Perform Chi-square test to compare catchability between first and second pass
pass_data23 <- fish_2023 %>%
  filter(Pass %in% c(1, 2, 3, 4)) %>%
  group_by(Basin, Species) %>%
  summarise(catch1 = sum(Pass == 1), catch2 = sum(Pass == 2), catch3 = sum(Pass == 3), catch4 = sum(Pass == 4), .groups = "drop") 

# Perform the chi-square test for each combination of Basin, Species, and habitat unit
pass_comparison23 <- pass_data23 %>%
  mutate(chi_square_test = mapply(function(x, y) chisq.test(c(x, y))$p.value, catch1, catch2))
## Warning: There were 20 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `chi_square_test = mapply(function(x, y) chisq.test(c(x,
##   y))$p.value, catch1, catch2)`.
## Caused by warning in `chisq.test()`:
## ! Chi-squared approximation may be incorrect
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 19 remaining warnings.
# View the results with p-values
print(pass_comparison23)
## # A tibble: 49 × 7
##    Basin     Species catch1 catch2 catch3 catch4 chi_square_test
##    <chr>     <chr>    <int>  <int>  <int>  <int>           <dbl>
##  1 AA1_REACH CTT         12      1      0      0        2.28e- 3
##  2 AA1_REACH TRT          2      0      0      0        1.57e- 1
##  3 AA2_REACH CTT         24      4      1      0        1.57e- 4
##  4 AA2_REACH SCP          5      2      0      0        2.57e- 1
##  5 AA2_REACH TRT         36     11      1      0        2.66e- 4
##  6 AC_REACH  CTT         42      0      1      0        9.13e-11
##  7 AC_REACH  TRT         22      2      2      1        4.46e- 5
##  8 AP_REACH  CTT          9      3      2      0        8.33e- 2
##  9 AP_REACH  TRT          3      2      0      0        6.55e- 1
## 10 AZ_REACH  CTT         22      8      2      0        1.06e- 2
## # ℹ 39 more rows
#Repeat with 2024 data
pass_data24 <- fish_2024 %>%
  filter(Pass %in% c(1, 2, 3)) %>%
  group_by(Basin, Species) %>%
  summarise(catch1 = sum(Pass == 1), catch2 = sum(Pass == 2), catch3 = sum(Pass == 3), .groups = "drop") 

pass_comparison24 <- pass_data24 %>%
  mutate(chi_square_test = mapply(function(x, y) chisq.test(c(x, y))$p.value, catch1, catch2))
## Warning: There were 21 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `chi_square_test = mapply(function(x, y) chisq.test(c(x,
##   y))$p.value, catch1, catch2)`.
## Caused by warning in `chisq.test()`:
## ! Chi-squared approximation may be incorrect
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 20 remaining warnings.
print(pass_comparison24)
## # A tibble: 50 × 6
##    Basin     Species catch1 catch2 catch3 chi_square_test
##    <chr>     <chr>    <int>  <int>  <int>           <dbl>
##  1 AA1_REACH CTT          3      1      1        3.17e- 1
##  2 AA2_REACH CTT         16      3      1        2.86e- 3
##  3 AA2_REACH SCP          5      0      0        2.53e- 2
##  4 AA2_REACH TRT         77      7      3        2.21e-14
##  5 AC_REACH  CTT         59     14      3        1.39e- 7
##  6 AC_REACH  TRT         43      9      0        2.42e- 6
##  7 AP_REACH  CTT          7      3      0        2.06e- 1
##  8 AP_REACH  TRT          7      2      0        9.56e- 2
##  9 AZ_REACH  CTT         21      8      1        1.58e- 2
## 10 AZ_REACH  SCP         17      8      5        7.19e- 2
## # ℹ 40 more rows
#using FSA package, see https://derekogle.com/fishR/examples/oldFishRVignettes/Depletion.pdf

library(FSA)
## Warning: package 'FSA' was built under R version 4.4.3
## Registered S3 methods overwritten by 'FSA':
##   method       from
##   confint.boot car 
##   hist.boot    car
## ## FSA v0.9.6. See citation('FSA') if used in publication.
## ## Run fishR() for related website and fishR('IFAR') for related book.
## 
## Attaching package: 'FSA'
## The following object is masked from 'package:car':
## 
##     bootCase
library(detect)
## Warning: package 'detect' was built under R version 4.4.3
## Loading required package: Formula
## Loading required package: stats4
## Loading required package: pbapply
## Warning: package 'pbapply' was built under R version 4.4.2
## detect 0.4-6      2023-03-08
#create standalone reach length dataframe for both years

# 2023 Reach area DataFrame
Reach_area_23 <- data.frame(
  Basin = c("AA1_REACH", "AA2_REACH", "AC_REACH", "AP_REACH", "AZ_REACH", 
            "BA1_REACH", "BA2_REACH", "BC_REACH", "BZ_REACH", "CA1_REACH", 
            "CA2_REACH", "CP_REACH", "CZ2_REACH", "DA1_REACH", "DA2_REACH", 
            "DC_REACH", "DP_REACH", "DZ_REACH"),
  Reach_area = c(92.37, 271.25, 376.09, 137.32, 360.84, 158.78, 338.36, 98.65, 157.84, 57.04, 580.36, 277.7, 266.65, 56.83, 263.67, 314.49, 119.66, 323.37
)
)

# 2024 Reach area DataFrame
Reach_area_24 <- data.frame(
  Basin = c("AA1_REACH", "AA2_REACH", "AC_REACH", "AP_REACH", "AZ_REACH", 
            "BA1_REACH", "BA2_REACH", "BC_REACH", "BZ_REACH", "CA1_REACH", 
            "CA2_REACH", "CP_REACH", "CZ2_REACH", "DA1_REACH", "DA2_REACH", 
            "DC_REACH", "DP_REACH", "DZ_REACH"),
  Reach_area = c(132.53, 312.94, 348.72, 255.27, 593.88, 178.08, 414.52, 153.61, 86.91, 358.36, 399.94, 247.11, 499.68, 101.21, 353.91, 327.62, 111.36, 410.58
)
)

# View to verify
print(Reach_area_23)
##        Basin Reach_area
## 1  AA1_REACH      92.37
## 2  AA2_REACH     271.25
## 3   AC_REACH     376.09
## 4   AP_REACH     137.32
## 5   AZ_REACH     360.84
## 6  BA1_REACH     158.78
## 7  BA2_REACH     338.36
## 8   BC_REACH      98.65
## 9   BZ_REACH     157.84
## 10 CA1_REACH      57.04
## 11 CA2_REACH     580.36
## 12  CP_REACH     277.70
## 13 CZ2_REACH     266.65
## 14 DA1_REACH      56.83
## 15 DA2_REACH     263.67
## 16  DC_REACH     314.49
## 17  DP_REACH     119.66
## 18  DZ_REACH     323.37
print(Reach_area_24)
##        Basin Reach_area
## 1  AA1_REACH     132.53
## 2  AA2_REACH     312.94
## 3   AC_REACH     348.72
## 4   AP_REACH     255.27
## 5   AZ_REACH     593.88
## 6  BA1_REACH     178.08
## 7  BA2_REACH     414.52
## 8   BC_REACH     153.61
## 9   BZ_REACH      86.91
## 10 CA1_REACH     358.36
## 11 CA2_REACH     399.94
## 12  CP_REACH     247.11
## 13 CZ2_REACH     499.68
## 14 DA1_REACH     101.21
## 15 DA2_REACH     353.91
## 16  DC_REACH     327.62
## 17  DP_REACH     111.36
## 18  DZ_REACH     410.58
#merge with pass catches
depletion_2023 <- merge(Reach_area_23, pass_data23, by = "Basin")

#Summarize captures
capture_summary23 <- depletion_2023 %>%
  group_by(Basin, Species, Reach_area) %>%
  summarise(catch1 = sum(catch1), catch2 = sum(catch2), catch3 = sum(catch3), catch4 = sum(catch4), .groups = "drop")

#Estimate population by removal() method

population_estimates23 <- capture_summary23 %>% rowwise() %>%
  mutate(removal_result = list(removal(c(catch1, catch2, catch3, catch4), type = "CarleStrub"))) %>%
      ungroup()

# Create a data frame to store the 'No' estimate from each removal result
population_estimates23 <- data.frame(
  estimated_population = sapply(population_estimates23$removal_result, function(x) x$est["No"]),
  No_se = sapply(population_estimates23$removal_result, function(x) x$est["No.se"]),
  No_LCI = sapply(population_estimates23$removal_result, function(x) x$est["No.LCI"]),
  No_UCI = sapply(population_estimates23$removal_result, function(x) x$est["No.UCI"]),
  p = sapply(population_estimates23$removal_result, function(x) x$est["p"]),
  p_se = sapply(population_estimates23$removal_result, function(x) x$est["p.se"]),
  p_LCI = sapply(population_estimates23$removal_result, function(x) x$est["p.LCI"]),
  p_UCI = sapply(population_estimates23$removal_result, function(x) x$est["p.UCI"])
)

# Combine the original data with the new 'estimated_population' column
population_estimates23_summary <- cbind(capture_summary23, population_estimates23) %>%
  mutate(
    abundance_per_100m = (estimated_population / Reach_area)  # Normalize to m2
  )

print(population_estimates23_summary)
##        Basin Species Reach_area catch1 catch2 catch3 catch4
## 1  AA1_REACH     CTT      92.37     12      1      0      0
## 2  AA1_REACH     TRT      92.37      2      0      0      0
## 3  AA2_REACH     CTT     271.25     24      4      1      0
## 4  AA2_REACH     SCP     271.25      5      2      0      0
## 5  AA2_REACH     TRT     271.25     36     11      1      0
## 6   AC_REACH     CTT     376.09     42      0      1      0
## 7   AC_REACH     TRT     376.09     22      2      2      1
## 8   AP_REACH     CTT     137.32      9      3      2      0
## 9   AP_REACH     TRT     137.32      3      2      0      0
## 10  AZ_REACH     CTT     360.84     22      8      2      0
## 11  AZ_REACH     SCP     360.84      6      0      0      0
## 12  AZ_REACH     TRT     360.84     12      3      0      0
## 13 BA1_REACH     CTT     158.78     14      2      1      0
## 14 BA1_REACH     TRT     158.78     15      4      1      0
## 15 BA2_REACH     COH     338.36    112     26      7      0
## 16 BA2_REACH     CTT     338.36     24      5      0      0
## 17 BA2_REACH     SCP     338.36      9      0      0      0
## 18 BA2_REACH     TRT     338.36      6      0      0      0
## 19  BC_REACH     CTT      98.65      5      0      0      0
## 20  BC_REACH     TRT      98.65    101     14      9      2
## 21  BZ_REACH     CTT     157.84     12      2      0      0
## 22  BZ_REACH     SCP     157.84      3      0      0      0
## 23  BZ_REACH     TRT     157.84     12      7      0      0
## 24 CA2_REACH     CTT     580.36     37      6      3      4
## 25 CA2_REACH     SCP     580.36      6      0      1      0
## 26 CA2_REACH     TRT     580.36     25      5      5      2
## 27  CP_REACH     CTT     277.70     71      7      2      0
## 28  CP_REACH     SCP     277.70      7      0      0      0
## 29  CP_REACH     TRT     277.70     23      5      2      0
## 30 CZ2_REACH     CTT     266.65     20      9      2      0
## 31 CZ2_REACH     TRT     266.65     17      3      1      0
## 32 DA2_REACH     COH     263.67     73     15      6      0
## 33 DA2_REACH     CTT     263.67      8      2      1      0
## 34 DA2_REACH     RBT     263.67      1      0      0      0
## 35 DA2_REACH     SCP     263.67      5      0      0      0
## 36 DA2_REACH     TRT     263.67      9      1      1      0
## 37  DC_REACH     COH     314.49     44      5      5      3
## 38  DC_REACH     CTT     314.49      3      1      0      0
## 39  DC_REACH     SCP     314.49      5      0      0      0
## 40  DC_REACH     TRT     314.49      6      2      0      0
## 41  DP_REACH     CTT     119.66      2      1      0      0
## 42  DP_REACH     SCP     119.66      5      0      0      0
## 43  DP_REACH     TRT     119.66      8      2      0      0
## 44  DZ_REACH     COH     323.37     38     19      7      7
## 45  DZ_REACH     CTT     323.37      3      3      2      0
## 46  DZ_REACH     RBT     323.37      1      0      0      0
## 47  DZ_REACH     SCP     323.37      7      0      0      0
##    estimated_population      No_se     No_LCI     No_UCI         p       p_se
## 1                    13 0.01844233  12.963854  13.036146 0.9285714 0.06900488
## 2                     2 0.00000000   2.000000   2.000000 1.0000000        NaN
## 3                    29 0.16290596  28.680710  29.319290 0.8285714 0.06557588
## 4                     7 0.13840904   6.728723   7.271277 0.7777778 0.14680496
## 5                    48 0.33134565  47.350574  48.649426 0.7868852 0.05521171
## 6                    43 0.01296130  42.974596  43.025404 0.9555556 0.03074043
## 7                    27 0.35123667  26.311589  27.688411 0.7500000 0.07805259
## 8                    14 0.48961825  13.040366  14.959634 0.6666667 0.12114910
## 9                     5 0.20386998   4.600422   5.399578 0.7142857 0.19070290
## 10                   32 0.46412871  31.090324  32.909676 0.7272727 0.07406157
## 11                    6 0.00000000   6.000000   6.000000 1.0000000        NaN
## 12                   15 0.11050874  14.783407  15.216593 0.8333333 0.09023001
## 13                   17 0.15544273  16.695338  17.304662 0.8095238 0.08904101
## 14                   20 0.25381094  19.502540  20.497460 0.7692308 0.08805827
## 15                  145 0.59396820 143.835844 146.164156 0.7837838 0.03193442
## 16                   29 0.11870851  28.767336  29.232664 0.8529412 0.06191078
## 17                    9 0.00000000   9.000000   9.000000 1.0000000        NaN
## 18                    6 0.00000000   6.000000   6.000000 1.0000000        NaN
## 19                    5 0.00000000   5.000000   5.000000 1.0000000        NaN
## 20                  126 0.64268573 124.740359 127.259641 0.7682927 0.03513542
## 21                   14 0.05918317  13.884003  14.115997 0.8750000 0.08369765
## 22                    3 0.00000000   3.000000   3.000000 1.0000000        NaN
## 23                   19 0.34740979  18.319089  19.680911 0.7307692 0.09564927
## 24                   50 0.86641003  48.301868  51.698132 0.6756757 0.06339025
## 25                    7 0.13840904   6.728723   7.271277 0.7777778 0.14680496
## 26                   37 0.97539631  35.088258  38.911742 0.6379310 0.07719091
## 27                   80 0.13215734  79.740976  80.259024 0.8791209 0.03455596
## 28                    7 0.00000000   7.000000   7.000000 1.0000000        NaN
## 29                   30 0.31085365  29.390738  30.609262 0.7692308 0.07189927
## 30                   31 0.54822575  29.925497  32.074503 0.7045455 0.07758375
## 31                   21 0.17627764  20.654502  21.345498 0.8076923 0.08039497
## 32                   94 0.51172108  92.997045  95.002955 0.7768595 0.04012179
## 33                   11 0.25873003  10.492898  11.507102 0.7333333 0.12525714
## 34                    1 0.00000000   1.000000   1.000000 1.0000000        NaN
## 35                    5 0.00000000   5.000000   5.000000 1.0000000        NaN
## 36                   11 0.16048788  10.685450  11.314550 0.7857143 0.11556448
## 37                   57 0.74828670  55.533385  58.466615 0.7037037 0.05727877
## 38                    4 0.08357177   3.836202   4.163798 0.8000000 0.18687216
## 39                    5 0.00000000   5.000000   5.000000 1.0000000        NaN
## 40                    8 0.11818833   7.768355   8.231645 0.8000000 0.13213857
## 41                    3 0.11707889   2.770530   3.229470 0.7500000 0.23415778
## 42                    5 0.00000000   5.000000   5.000000 1.0000000        NaN
## 43                   10 0.09023001   9.823152  10.176848 0.8333333 0.11050874
## 44                   75 3.33044284  68.472452  81.527548 0.5035461 0.06392365
## 45                    8 0.90134784   6.233391   9.766609 0.5333333 0.18849116
## 46                    1 0.00000000   1.000000   1.000000 1.0000000        NaN
## 47                    7 0.00000000   7.000000   7.000000 1.0000000        NaN
##        p_LCI     p_UCI abundance_per_100m
## 1  0.7933243 1.0638185        0.140738335
## 2        NaN       NaN        0.021652052
## 3  0.7000451 0.9570978        0.106912442
## 4  0.4900453 1.0655102        0.025806452
## 5  0.6786723 0.8950982        0.176958525
## 6  0.8953054 1.0158057        0.114334335
## 7  0.5970197 0.9029803        0.071791327
## 8  0.4292188 0.9041145        0.101951646
## 9  0.3405149 1.0880565        0.036411302
## 10 0.5821147 0.8724307        0.088681964
## 11       NaN       NaN        0.016627868
## 12 0.6564858 1.0101809        0.041569671
## 13 0.6350066 0.9840410        0.107066381
## 14 0.5966397 0.9418218        0.125960448
## 15 0.7211935 0.8463741        0.428537652
## 16 0.7315983 0.9742841        0.085707530
## 17       NaN       NaN        0.026598889
## 18       NaN       NaN        0.017732593
## 19       NaN       NaN        0.050684237
## 20 0.6994285 0.8371568        1.277242777
## 21 0.7109556 1.0390444        0.088697415
## 22       NaN       NaN        0.019006589
## 23 0.5433001 0.9182384        0.120375063
## 24 0.5514331 0.7999183        0.086153422
## 25 0.4900453 1.0655102        0.012061479
## 26 0.4866396 0.7892224        0.063753532
## 27 0.8113924 0.9468493        0.288080663
## 28       NaN       NaN        0.025207058
## 29 0.6283108 0.9101508        0.108030248
## 30 0.5524841 0.8566068        0.116257266
## 31 0.6501211 0.9652636        0.078754922
## 32 0.6982222 0.8554968        0.356506239
## 33 0.4878339 0.9788328        0.041718815
## 34       NaN       NaN        0.003792620
## 35       NaN       NaN        0.018963098
## 36 0.5592121 1.0122165        0.041718815
## 37 0.5914394 0.8159680        0.181245827
## 38 0.4337373 1.1662627        0.012719005
## 39       NaN       NaN        0.015898757
## 40 0.5410132 1.0589868        0.025438011
## 41 0.2910592 1.2089408        0.025071035
## 42       NaN       NaN        0.041785058
## 43 0.6167402 1.0499265        0.083570115
## 44 0.3782580 0.6288342        0.231932461
## 45 0.1638974 0.9027692        0.024739463
## 46       NaN       NaN        0.003092433
## 47       NaN       NaN        0.021647030
#Repeat with 2024 data

#merge with pass catches
depletion_2024 <- merge(Reach_area_24, pass_data24, by = "Basin")

#Summarize captures
capture_summary24 <- depletion_2024 %>%
  group_by(Basin, Species, Reach_area) %>%
  summarise(catch1 = sum(catch1), catch2 = sum(catch2), catch3 = sum(catch3), .groups = "drop")

#Estimate population by removal() method

population_estimates24 <- capture_summary24 %>% rowwise() %>%
  mutate(removal_result = list(removal(c(catch1, catch2, catch3), type = "CarleStrub"))) %>%
      ungroup()

# Create a data frame to store the 'No' estimate from each removal result
population_estimates24 <- data.frame(
  estimated_population = sapply(population_estimates24$removal_result, function(x) x$est["No"]),
  No_se = sapply(population_estimates24$removal_result, function(x) x$est["No.se"]),
  No_LCI = sapply(population_estimates24$removal_result, function(x) x$est["No.LCI"]),
  No_UCI = sapply(population_estimates24$removal_result, function(x) x$est["No.UCI"]),
  p = sapply(population_estimates24$removal_result, function(x) x$est["p"]),
  p_se = sapply(population_estimates24$removal_result, function(x) x$est["p.se"]),
  p_LCI = sapply(population_estimates24$removal_result, function(x) x$est["p.LCI"]),
  p_UCI = sapply(population_estimates24$removal_result, function(x) x$est["p.UCI"])
)

# Combine the original data with the new 'estimated_population' column
population_estimates24_summary <- cbind(capture_summary24, population_estimates24) %>%
  mutate(
    abundance_per_100m = (estimated_population / Reach_area) # m2
  )

print(population_estimates24_summary)
##        Basin Species Reach_area catch1 catch2 catch3 estimated_population
## 1  AA1_REACH     CTT     132.53      3      1      1                    5
## 2  AA2_REACH     CTT     312.94     16      3      1                   20
## 3  AA2_REACH     SCP     312.94      5      0      0                    5
## 4  AA2_REACH     TRT     312.94     77      7      3                   87
## 5   AC_REACH     CTT     348.72     59     14      3                   76
## 6   AC_REACH     TRT     348.72     43      9      0                   52
## 7   AP_REACH     CTT     255.27      7      3      0                   10
## 8   AP_REACH     TRT     255.27      7      2      0                    9
## 9   AZ_REACH     CTT     593.88     21      8      1                   30
## 10  AZ_REACH     SCP     593.88     17      8      5                   33
## 11  AZ_REACH     TRT     593.88     29     14      0                   43
## 12 BA1_REACH     CTT     178.08     14      2      0                   16
## 13 BA1_REACH     TRT     178.08     14      0      0                   14
## 14 BA2_REACH     COH     414.52     83     24      7                  116
## 15 BA2_REACH     CTT     414.52     24      4      1                   29
## 16 BA2_REACH     RBT     414.52      1      0      1                    2
## 17 BA2_REACH     SCP     414.52      9      0      0                    9
## 18 BA2_REACH     TRT     414.52     16      4      0                   20
## 19  BC_REACH     CTT     153.61      5      0      0                    5
## 20  BC_REACH     TRT     153.61    123     18     14                  157
## 21  BZ_REACH     CTT      86.91     18      2      0                   20
## 22  BZ_REACH     SCP      86.91      6      0      0                    6
## 23  BZ_REACH     TRT      86.91     20      5      1                   26
## 24 CA2_REACH     CTT     399.94     31      2      2                   35
## 25 CA2_REACH     SCP     399.94      6      1      0                    7
## 26 CA2_REACH     TRT     399.94     37      7      3                   47
## 27  CP_REACH     CTT     247.11     25      4      3                   32
## 28  CP_REACH     SCP     247.11      8      0      0                    8
## 29  CP_REACH     TRT     247.11     10      3      1                   14
## 30 CZ2_REACH     CTT     499.68     26      2      0                   28
## 31 CZ2_REACH     TRT     499.68     16      1      1                   18
## 32 DA2_REACH     COH     353.91     30      9      2                   41
## 33 DA2_REACH     CTT     353.91      4      4      0                    8
## 34 DA2_REACH     RBT     353.91      1      1      0                    2
## 35 DA2_REACH     SCP     353.91      5      0      1                    6
## 36 DA2_REACH     TRT     353.91      3      2      1                    6
## 37  DC_REACH     COH     327.62     17      3      0                   20
## 38  DC_REACH     CTT     327.62     10      1      0                   11
## 39  DC_REACH     RBT     327.62      1      0      0                    1
## 40  DC_REACH     SCP     327.62      7      0      0                    7
## 41  DC_REACH     TRT     327.62      7      1      1                    9
## 42  DP_REACH     CTT     111.36      2      0      0                    2
## 43  DP_REACH     SCP     111.36     11      0      0                   11
## 44  DP_REACH     TRT     111.36      2      0      1                    3
## 45  DZ_REACH     COH     410.58     18      4      0                   22
## 46  DZ_REACH     CTT     410.58      4      0      0                    4
## 47  DZ_REACH     SCP     410.58      5      0      0                    5
## 48  DZ_REACH     TRT     410.58      5      2      1                    8
##         No_se       No_LCI     No_UCI         p       p_se       p_LCI
## 1  0.78732564   3.45687011   6.543130 0.6250000 0.26244188  0.11062337
## 2  0.45890939  19.10055413  20.899446 0.8000000 0.09178188  0.62011083
## 3  0.00000000   5.00000000   5.000000 1.0000000        NaN         NaN
## 4  0.46541004  86.08781309  87.912187 0.8700000 0.03580077  0.79983178
## 5  0.96113897  74.11620223  77.883798 0.7916667 0.04805695  0.69747678
## 6  0.44223360  51.13323808  52.866762 0.8524590 0.04913707  0.75615214
## 7  0.41883071   9.17910690  10.820893 0.7692308 0.13961024  0.49559974
## 8  0.26107864   8.48829526   9.511705 0.8181818 0.13053932  0.56232945
## 9  0.84087497  28.35191535  31.648085 0.7500000 0.08408750  0.58519154
## 10 3.61894328  25.90700151  40.092998 0.5263158 0.12184994  0.28749429
## 11 0.97403420  41.09092805  44.909072 0.7543860 0.06957387  0.61802368
## 12 0.15524038  15.69573444  16.304266 0.8888889 0.07762019  0.73675611
## 13 0.00000000  14.00000000  14.000000 1.0000000        NaN         NaN
## 14 2.03150305 112.01832718 119.981673 0.7215190 0.04537448  0.63258664
## 15 0.42405500  28.16886747  29.831133 0.8285714 0.07067583  0.69004934
## 16 1.03774904  -0.03395075   4.033951 0.5000000 0.51887452 -0.51697537
## 17 0.00000000   9.00000000   9.000000 1.0000000        NaN         NaN
## 18 0.33583759  19.34177042  20.658230 0.8333333 0.08395940  0.66877594
## 19 0.00000000   5.00000000   5.000000 1.0000000        NaN         NaN
## 20 1.94105630 153.19559956 160.804400 0.7487923 0.03685249  0.67656272
## 21 0.12658632  19.75189537  20.248105 0.9090909 0.06329316  0.78503860
## 22 0.00000000   6.00000000   6.000000 1.0000000        NaN         NaN
## 23 0.58040495  24.86242721  27.137573 0.7878788 0.08291499  0.62536839
## 24 0.35797829  34.29837544  35.701625 0.8536585 0.05966305  0.73672111
## 25 0.12393031   6.75710106   7.242899 0.8750000 0.12393031  0.63210106
## 26 0.81039555  45.41165391  48.588346 0.7833333 0.06233812  0.66115287
## 27 0.79332514  30.44511131  33.554889 0.7619048 0.07933251  0.60641589
## 28 0.00000000   8.00000000   8.000000 1.0000000        NaN         NaN
## 29 0.63282718  12.75968152  15.240318 0.7368421 0.12656544  0.48877841
## 30 0.09272834  27.81825580  28.181744 0.9333333 0.04636417  0.84246123
## 31 0.24677406  17.51633173  18.483668 0.8571429 0.08225802  0.69592010
## 32 0.91645386  39.20378345  42.796217 0.7592593 0.07049645  0.62108876
## 33 0.76870611   6.49336370   9.506636 0.6666667 0.19217653  0.29000759
## 34 0.38435306   1.24668185   2.753318 0.6666667 0.38435306 -0.08665148
## 35 0.37605072   5.26295414   6.737046 0.7500000 0.18802536  0.38147707
## 36 1.00241255   4.03530751   7.964692 0.6000000 0.25060314  0.10882688
## 37 0.22435411  19.56027403  20.439726 0.8695652 0.07478470  0.72298989
## 38 0.08199332  10.83929604  11.160704 0.9166667 0.08199332  0.75596271
## 39 0.00000000   1.00000000   1.000000 1.0000000        NaN         NaN
## 40 0.00000000   7.00000000   7.000000 1.0000000        NaN         NaN
## 41 0.46056619   8.09730686   9.902693 0.7500000 0.15352206  0.44910229
## 42 0.00000000   2.00000000   2.000000 1.0000000        NaN         NaN
## 43 0.00000000  11.00000000  11.000000 1.0000000        NaN         NaN
## 44 0.70881271   1.61075262   4.389247 0.6000000 0.35440636 -0.09462369
## 45 0.30821513  21.39590944  22.604091 0.8461538 0.07705378  0.69513121
## 46 0.00000000   4.00000000   4.000000 1.0000000        NaN         NaN
## 47 0.00000000   5.00000000   5.000000 1.0000000        NaN         NaN
## 48 0.76870611   6.49336370   9.506636 0.6666667 0.19217653  0.29000759
##        p_UCI abundance_per_100m
## 1  1.1393766        0.037727307
## 2  0.9798892        0.063910015
## 3        NaN        0.015977504
## 4  0.9401682        0.278008564
## 5  0.8858566        0.217939894
## 6  0.9487659        0.149116770
## 7  1.0428618        0.039174208
## 8  1.0740342        0.035256787
## 9  0.9148085        0.050515256
## 10 0.7651373        0.055566781
## 11 0.8907482        0.072405200
## 12 1.0410217        0.089847260
## 13       NaN        0.078616352
## 14 0.8104513        0.279841745
## 15 0.9670935        0.069960436
## 16 1.5169754        0.004824858
## 17       NaN        0.021711860
## 18 0.9978907        0.048248577
## 19       NaN        0.032549964
## 20 0.8210218        1.022068876
## 21 1.0331432        0.230123116
## 22       NaN        0.069036935
## 23 0.9503892        0.299160051
## 24 0.9705960        0.087513127
## 25 1.1178989        0.017502625
## 26 0.9055138        0.117517628
## 27 0.9173936        0.129496985
## 28       NaN        0.032374246
## 29 0.9849058        0.056654931
## 30 1.0242054        0.056035863
## 31 1.0183656        0.036023055
## 32 0.8974298        0.115848662
## 33 1.0433257        0.022604617
## 34 1.4199848        0.005651154
## 35 1.1185229        0.016953463
## 36 1.0911731        0.016953463
## 37 1.0161405        0.061046334
## 38 1.0773706        0.033575484
## 39       NaN        0.003052317
## 40       NaN        0.021366217
## 41 1.0508977        0.027470850
## 42       NaN        0.017959770
## 43       NaN        0.098778736
## 44 1.2946237        0.026939655
## 45 0.9971765        0.053582737
## 46       NaN        0.009742316
## 47       NaN        0.012177895
## 48 1.0433257        0.019484631
#note that abundance per 100m is really per m2. its just everywhere and im too lazy to go changing every time its mentioned

#scale abundance/m^2

population_estimates23_summary$abundance_per_100m <- scale(population_estimates23_summary$abundance_per_100m)

population_estimates24_summary$abundance_per_100m <- scale(population_estimates24_summary$abundance_per_100m)
# Correlation test for biomass vs. periphyton  with no species differentiation

# scale biomass data for comparability. not NECESSARY right now but makes the future stuff way better to handle

# Exclude rows with zero or negative biomass

avg_biomass23 <- avg_biomass23 %>%
  filter(biomass_reach > 0)  
avg_biomass24 <- avg_biomass24 %>%
  filter(biomass_reach > 0)

avg_biomass23$biomass_reach_scale <- scale(log(avg_biomass23$biomass_reach))

avg_biomass24$biomass_reach_scale <- scale(log(avg_biomass24$biomass_reach))

#omitting CC REACH due to lack of periphyton data in one of the years

# Aggregate 2023 data by Basin
avg_biomass23_summarized <- avg_biomass23 %>%
  filter(Basin != "CC_REACH")%>%
  group_by(Basin) %>%
  summarize(total_biomass_23 = sum(biomass_reach_scale), Yearly_Avg_23 = mean(Yearly_Avg), biomass_reach = sum(biomass_reach))

# Aggregate 2024 data by Basin
avg_biomass24_summarized <- avg_biomass24 %>%
  filter(Basin != "CC_REACH")%>%
  group_by(Basin) %>%
  summarize(total_biomass_24 = sum(biomass_reach_scale), Yearly_Avg_24 = mean(Yearly_Avg), biomass_reach = sum(biomass_reach))


# Scatter plot for 2023 biomass vs. Yearly_Avg
ggplot(avg_biomass23_summarized, aes(x = Yearly_Avg_23, y = total_biomass_23)) +
  geom_point() +
  geom_smooth(method = "lm", color = "green") +
  theme_minimal() +
  labs(title = "2023 Biomass vs. Yearly_Avg Chlorophyll", x = "Yearly_Avg", y = "Total Fish Biomass")
## `geom_smooth()` using formula = 'y ~ x'

# Scatter plot for 2024 biomass vs. Yearly_Avg
ggplot(avg_biomass24_summarized, aes(x = Yearly_Avg_24, y = total_biomass_24)) +
  geom_point() +
  geom_smooth(method = "lm", color = "purple") +
  theme_minimal() +
  labs(title = "2024 Biomass vs. Yearly_Avg Chlorophyll", x = "Yearly_Avg", y = "Total Fish Biomass")
## `geom_smooth()` using formula = 'y ~ x'

# Correlation test for 2023 biomass vs. Yearly_Avg
cor_2023_avg <- cor.test(avg_biomass23_summarized$total_biomass_23, avg_biomass23_summarized$Yearly_Avg_23, method = "pearson")
print(cor_2023_avg)
## 
##  Pearson's product-moment correlation
## 
## data:  avg_biomass23_summarized$total_biomass_23 and avg_biomass23_summarized$Yearly_Avg_23
## t = 1.9171, df = 14, p-value = 0.07587
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.05131063  0.77623868
## sample estimates:
##       cor 
## 0.4559929
# Correlation test for 2024 biomass vs. Yearly_Avg
cor_2024_avg <- cor.test(avg_biomass24_summarized$total_biomass_24, avg_biomass24_summarized$Yearly_Avg_24, method = "pearson")
print(cor_2024_avg)
## 
##  Pearson's product-moment correlation
## 
## data:  avg_biomass24_summarized$total_biomass_24 and avg_biomass24_summarized$Yearly_Avg_24
## t = 1.0793, df = 14, p-value = 0.2987
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.2533554  0.6795069
## sample estimates:
##       cor 
## 0.2771582
#Statistical significance of correlation test in 2023 but not 2024. is it due to fish or periphyton? What about per species?

# Correlation test with species differentiation

cor_biomassspecies_2023 <- avg_biomass23 %>%
  filter(Species != "RBT")%>%
  group_by(Species) %>%
  summarize(correlation = cor.test(biomass_reach, Yearly_Avg, method = "pearson")$estimate,
            p_value = cor.test(biomass_reach, Yearly_Avg, method = "pearson")$p.value)

cor_biomassspecies_2024 <- avg_biomass24 %>%
  filter(Species != "RBT")%>%
  group_by(Species) %>%
  summarize(correlation = cor.test(biomass_reach, Yearly_Avg, method = "pearson")$estimate,
            p_value = cor.test(biomass_reach, Yearly_Avg, method = "pearson")$p.value)

print(cor_biomassspecies_2023)
## # A tibble: 4 × 3
##   Species correlation  p_value
##   <chr>         <dbl>    <dbl>
## 1 COH         -0.546  0.454   
## 2 CTT          0.759  0.000643
## 3 SCP          0.133  0.714   
## 4 TRT          0.0302 0.915
print(cor_biomassspecies_2024)
## # A tibble: 4 × 3
##   Species correlation p_value
##   <chr>         <dbl>   <dbl>
## 1 COH           0.421  0.579 
## 2 CTT           0.509  0.0442
## 3 SCP          -0.339  0.338 
## 4 TRT           0.212  0.449
ggplot(avg_biomass23, aes(x = biomass_reach, y = Yearly_Avg, color = Species)) +
  geom_point(size = 3, alpha = 0.7) +
  geom_smooth(method = "lm") +  # Adds regression lines per species
  labs(
    x = "Fish Biomass per Reach",
    y = "Chlorophyll Biomass",
    title = "Relationship Between Fish Biomass and Chlorophyll by Species - 2023"
  ) +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

ggplot(avg_biomass24, aes(x = biomass_reach, y = Yearly_Avg, color = Species)) +
  geom_point(size = 3, alpha = 0.7) +
  geom_smooth(method = "lm") +  # Adds regression lines per species
  labs(
    x = "Fish Biomass per Reach",
    y = "Chlorophyll Biomass",
    title = "Relationship Between Fish Biomass and Chlorophyll by Species - 2024"
  ) +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
## Warning in qt((1 - level)/2, df): NaNs produced
## Warning in max(ids, na.rm = TRUE): no non-missing arguments to max; returning
## -Inf

# Left join the 2023 and 2024 aggregated data by Basin
combined_biomass <- left_join(avg_biomass23_summarized %>% mutate(Year = 2023),
                              avg_biomass24_summarized %>% mutate(Year = 2024),
                              by = "Basin")

# Correlation test for fish biomass between 2023 and 2024
cor_biomass_fish <- cor.test(combined_biomass$total_biomass_23, combined_biomass$total_biomass_24, method = "pearson")
print(cor_biomass_fish)
## 
##  Pearson's product-moment correlation
## 
## data:  combined_biomass$total_biomass_23 and combined_biomass$total_biomass_24
## t = 1.1023, df = 14, p-value = 0.2889
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.2478289  0.6826682
## sample estimates:
##      cor 
## 0.282593
# Run the correlation test for periphyton biomass between 2023 and 2024
cor_biomass_peri <- cor.test(combined_biomass$Yearly_Avg_23, combined_biomass$Yearly_Avg_24, method = "pearson")
print(cor_biomass_peri)
## 
##  Pearson's product-moment correlation
## 
## data:  combined_biomass$Yearly_Avg_23 and combined_biomass$Yearly_Avg_24
## t = 1.676, df = 14, p-value = 0.1159
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.1090071  0.7520904
## sample estimates:
##       cor 
## 0.4087872
# Scatter plot for fish
ggplot(combined_biomass, aes(x = total_biomass_23, y = total_biomass_24)) +
  geom_point() +
  geom_smooth(method = "lm", color = "darkblue") +
  theme_minimal() +
  labs(title = "Fish Biomass '23 vs. Fish Biomass '24", x = "Biomass 2023", y = "Biomass 2024")
## `geom_smooth()` using formula = 'y ~ x'

# Scatter plot for periphyton
ggplot(combined_biomass, aes(x = Yearly_Avg_23, y = Yearly_Avg_24)) +
  geom_point() +
  geom_smooth(method = "lm", color = "darkgreen") +
  theme_minimal() +
  labs(title = "Yearly_Avg Chlorophyll '23 vs. Yearly_Avg Chlorophyll '24", x = "Yearly_Avg 2023", y = "Yearly_Avg 2024")
## `geom_smooth()` using formula = 'y ~ x'

#Results: Seems to be that periphyton is not related to the trend of decreasing fish biomass between 2023 and 2024. Wondering if next year we'll see a delayed effect.

# Plot 2023/2024 data
ggplot() +
  geom_point(data = combined_biomass, aes(x = Yearly_Avg_23, y = total_biomass_23, color = "2023")) +
  geom_point(data = combined_biomass, aes(x = Yearly_Avg_24, y = total_biomass_24, color = "2024")) +
  geom_smooth(data = combined_biomass, aes(x = Yearly_Avg_23, y = total_biomass_23, color = "2023"), method = "lm") +
  geom_smooth(data = combined_biomass, aes(x = Yearly_Avg_24, y = total_biomass_24, color = "2024"), method = "lm") +
  labs(x = "Yearly Average Chlorophyll", y = "Total Biomass", color = "Year") +
  scale_color_manual(values = c("2023" = "blue", "2024" = "red")) +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'

# regressions for significance on year based data

# 2023 fish v. peri
lm_2023_avg <- lm(total_biomass_23 ~ Yearly_Avg_23, data = avg_biomass23_summarized)
summary(lm_2023_avg)
## 
## Call:
## lm(formula = total_biomass_23 ~ Yearly_Avg_23, data = avg_biomass23_summarized)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.4993 -0.5324 -0.0287  0.7314  1.5917 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)  
## (Intercept)   -0.006846   0.248357  -0.028   0.9784  
## Yearly_Avg_23  0.466841   0.243517   1.917   0.0759 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9933 on 14 degrees of freedom
## Multiple R-squared:  0.2079, Adjusted R-squared:  0.1514 
## F-statistic: 3.675 on 1 and 14 DF,  p-value: 0.07587
#2024 fish v. peri
lm_2024_avg <- lm(total_biomass_24 ~ Yearly_Avg_24, data = avg_biomass24_summarized)
summary(lm_2024_avg)
## 
## Call:
## lm(formula = total_biomass_24 ~ Yearly_Avg_24, data = avg_biomass24_summarized)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.2930 -0.7870  0.1922  0.9776  1.9296 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)
## (Intercept)   -0.06399    0.40541  -0.158    0.877
## Yearly_Avg_24  0.45964    0.42587   1.079    0.299
## 
## Residual standard error: 1.604 on 14 degrees of freedom
## Multiple R-squared:  0.07682,    Adjusted R-squared:  0.01087 
## F-statistic: 1.165 on 1 and 14 DF,  p-value: 0.2987
#2023 v 2024 fish
lm_fish_biomass <- lm(total_biomass_24 ~ total_biomass_23, data = combined_biomass)
summary(lm_fish_biomass)
## 
## Call:
## lm(formula = total_biomass_24 ~ total_biomass_23, data = combined_biomass)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.4774 -0.6283  0.0431  0.9874  2.1127 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)
## (Intercept)      -4.249e-16  4.004e-01   0.000    1.000
## total_biomass_23  4.227e-01  3.835e-01   1.102    0.289
## 
## Residual standard error: 1.602 on 14 degrees of freedom
## Multiple R-squared:  0.07986,    Adjusted R-squared:  0.01413 
## F-statistic: 1.215 on 1 and 14 DF,  p-value: 0.2889
#2023 v. 2024 peri
lm_peri_biomass <- lm(Yearly_Avg_24 ~ Yearly_Avg_23, data = combined_biomass)
summary(lm_peri_biomass)
## 
## Call:
## lm(formula = Yearly_Avg_24 ~ Yearly_Avg_23, data = combined_biomass)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.64294 -0.51689 -0.00269  0.46245  1.90240 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)
## (Intercept)     0.1337     0.2297   0.582    0.570
## Yearly_Avg_23   0.3775     0.2252   1.676    0.116
## 
## Residual standard error: 0.9188 on 14 degrees of freedom
## Multiple R-squared:  0.1671, Adjusted R-squared:  0.1076 
## F-statistic: 2.809 on 1 and 14 DF,  p-value: 0.1159
#Combine species population estimate data with periphyton data but summarizing to keep chlorophyll values at reach level

peri_abundance_23 <- population_estimates23_summary %>%
  filter(Species != "RBT")%>%
  group_by(Basin, Species) %>%
  left_join(HU_23, by = "Basin")  # Join with habitat data

peri_abundance_24 <- population_estimates24_summary %>%
  filter(Species != "RBT")%>%
  group_by(Basin, Species) %>%
  left_join(HU_24, by = "Basin")  # Join with habitat data

#only using yearly summarized data

cor_abun_2023 <- peri_abundance_23 %>%
  group_by(Species) %>%
  summarize(correlation = cor.test(abundance_per_100m, Yearly_Avg, method = "pearson")$estimate,
            p_value = cor.test(abundance_per_100m, Yearly_Avg, method = "pearson")$p.value)

cor_abun_2024 <- peri_abundance_24 %>%
  group_by(Species) %>%
  summarize(correlation = cor.test(abundance_per_100m, Yearly_Avg, method = "pearson")$estimate,
            p_value = cor.test(abundance_per_100m, Yearly_Avg, method = "pearson")$p.value)

print(cor_abun_2023)
## # A tibble: 4 × 3
##   Species correlation p_value
##   <chr>         <dbl>   <dbl>
## 1 COH         -0.430  0.570  
## 2 CTT          0.634  0.00831
## 3 SCP         -0.221  0.539  
## 4 TRT          0.0221 0.938
print(cor_abun_2024)
## # A tibble: 4 × 3
##   Species correlation p_value
##   <chr>         <dbl>   <dbl>
## 1 COH           0.614 0.386  
## 2 CTT           0.712 0.00199
## 3 SCP          -0.114 0.755  
## 4 TRT           0.300 0.278
#plot correlations

ggplot(peri_abundance_23, aes(x = Yearly_Avg, y = abundance_per_100m)) +
  geom_point() +
  geom_smooth(method = "lm") +
  facet_wrap(~Species) +  
  theme_minimal() +
  labs(title = "2023 Species-Specific Abundance vs. Chlorophyll", x = "Yearly_Avg", y = "Abundance")
## `geom_smooth()` using formula = 'y ~ x'

ggplot(peri_abundance_24, aes(x = Yearly_Avg, y = abundance_per_100m)) +
  geom_point() +
  geom_smooth(method = "lm") +
  facet_wrap(~Species) +  
  theme_minimal() +
  labs(title = "2024 Species-Specific Abundance vs. Chlorophyll", x = "Yearly_Avg", y = "Abundance")
## `geom_smooth()` using formula = 'y ~ x'

#regressions 

lm_abun_2023 <- peri_abundance_23 %>%
  group_by(Species) %>%
  do(model = lm(abundance_per_100m ~ Yearly_Avg, data = .))

lm_abun_2024 <- peri_abundance_24 %>%
  group_by(Species) %>%
  do(model = lm(abundance_per_100m ~ Yearly_Avg, data = .))

# View model summaries
summary(lm_abun_2023$model[[1]])
## 
## Call:
## lm(formula = abundance_per_100m ~ Yearly_Avg, data = .)
## 
## Residuals:
##         1         2         3         4 
##  0.627525  0.030777 -0.651409 -0.006894 
## attr(,"scaled:center")
## [1] 0.1106
## attr(,"scaled:scale")
## [1] 0.1956
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)   0.6067     0.6219   0.976    0.432
## Yearly_Avg   -0.4449     0.6605  -0.673    0.570
## 
## Residual standard error: 0.64 on 2 degrees of freedom
## Multiple R-squared:  0.1849, Adjusted R-squared:  -0.2227 
## F-statistic: 0.4536 on 1 and 2 DF,  p-value: 0.57
summary(lm_abun_2023$model[[2]])
## 
## Call:
## lm(formula = abundance_per_100m ~ Yearly_Avg, data = .)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.38354 -0.16012 -0.00678  0.06387  0.61754 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept) -0.09570    0.06579  -1.455  0.16786   
## Yearly_Avg   0.19808    0.06451   3.070  0.00831 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2632 on 14 degrees of freedom
## Multiple R-squared:  0.4024, Adjusted R-squared:  0.3597 
## F-statistic: 9.428 on 1 and 14 DF,  p-value: 0.008306
summary(lm_abun_2023$model[[3]])
## 
## Call:
## lm(formula = abundance_per_100m ~ Yearly_Avg, data = .)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.053721 -0.025284 -0.009305  0.021348  0.089088 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -0.451360   0.013873 -32.534 8.68e-10 ***
## Yearly_Avg  -0.007768   0.012117  -0.641    0.539    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.04387 on 8 degrees of freedom
## Multiple R-squared:  0.04886,    Adjusted R-squared:  -0.07003 
## F-statistic: 0.411 on 1 and 8 DF,  p-value: 0.5394
summary(lm_abun_2023$model[[4]])
## 
## Call:
## lm(formula = abundance_per_100m ~ Yearly_Avg, data = .)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.6609 -0.5897 -0.4491 -0.2279  5.7538 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)  0.21467    0.43056   0.499    0.626
## Yearly_Avg   0.03255    0.40879   0.080    0.938
## 
## Residual standard error: 1.667 on 13 degrees of freedom
## Multiple R-squared:  0.0004874,  Adjusted R-squared:  -0.0764 
## F-statistic: 0.00634 on 1 and 13 DF,  p-value: 0.9378
summary(lm_abun_2024$model[[1]])
## 
## Call:
## lm(formula = abundance_per_100m ~ Yearly_Avg, data = .)
## 
## Residuals:
##       1       2       3       4 
##  0.4790  0.4409 -0.4236 -0.4963 
## attr(,"scaled:center")
## [1] 0.09079
## attr(,"scaled:scale")
## [1] 0.1562
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)   0.2233     0.3261   0.685    0.564
## Yearly_Avg    0.5631     0.5124   1.099    0.386
## 
## Residual standard error: 0.6518 on 2 degrees of freedom
## Multiple R-squared:  0.3764, Adjusted R-squared:  0.06463 
## F-statistic: 1.207 on 1 and 2 DF,  p-value: 0.3865
summary(lm_abun_2024$model[[2]])
## 
## Call:
## lm(formula = abundance_per_100m ~ Yearly_Avg, data = .)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.42335 -0.23121 -0.00212  0.08400  0.63141 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept) -0.14870    0.07771  -1.913  0.07636 . 
## Yearly_Avg   0.30931    0.08164   3.789  0.00199 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3075 on 14 degrees of freedom
## Multiple R-squared:  0.5063, Adjusted R-squared:  0.471 
## F-statistic: 14.36 on 1 and 14 DF,  p-value: 0.001994
summary(lm_abun_2024$model[[3]])
## 
## Call:
## lm(formula = abundance_per_100m ~ Yearly_Avg, data = .)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.15655 -0.12845 -0.08782  0.08018  0.37086 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -0.34549    0.06319  -5.467 0.000596 ***
## Yearly_Avg  -0.02204    0.06819  -0.323 0.754778    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1951 on 8 degrees of freedom
## Multiple R-squared:  0.01289,    Adjusted R-squared:  -0.1105 
## F-statistic: 0.1045 on 1 and 8 DF,  p-value: 0.7548
summary(lm_abun_2024$model[[4]])
## 
## Call:
## lm(formula = abundance_per_100m ~ Yearly_Avg, data = .)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.0247 -0.7547 -0.4627 -0.0558  5.3400 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)   0.3040     0.4273   0.711    0.489
## Yearly_Avg    0.4966     0.4381   1.133    0.278
## 
## Residual standard error: 1.626 on 13 degrees of freedom
## Multiple R-squared:  0.08992,    Adjusted R-squared:  0.01991 
## F-statistic: 1.284 on 1 and 13 DF,  p-value: 0.2775
#check abundance trends
combined_abundance <- left_join(peri_abundance_23 %>% rename(abundance_per_100m_23 = abundance_per_100m),
                                peri_abundance_24 %>% rename(abundance_per_100m_24 = abundance_per_100m),
                                by = c("Basin", "Species"))

ggplot(combined_abundance, aes(x = abundance_per_100m_23, y = abundance_per_100m_24, color = Species)) +
  geom_point() +
  geom_smooth(method = "lm") +
  theme_minimal() +
  labs(title = "Species-Specific Abundance 2023 vs. 2024", x = "Abundance 2023", y = "Abundance 2024")
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 1 row containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_point()`).

# Note that basins Aa1, Bp, Ca1, Cz, Da2, Dp, and Dz are omitted due to missing data

canopy_peri <- merge(canopy, combined_biomass, by = "Basin")

cor_canopy_peri_23 <- canopy_peri %>%
  summarize(correlation = cor.test(average_closure_23, Yearly_Avg_23, method = "pearson")$estimate,
            p_value = cor.test(average_closure_23, Yearly_Avg_23, method = "pearson")$p.value)

cor_canopy_peri_24 <- canopy_peri %>%
  summarize(correlation = cor.test(average_closure_24, Yearly_Avg_24, method = "pearson")$estimate,
            p_value = cor.test(average_closure_24, Yearly_Avg_24, method = "pearson")$p.value)

print(cor_canopy_peri_23)
##   correlation   p_value
## 1  0.02125142 0.9477331
print(cor_canopy_peri_24)
##   correlation   p_value
## 1  -0.3060507 0.3333091
ggplot(canopy_peri, aes(x = Yearly_Avg_23, y = average_closure_23)) +
  geom_point() +
  geom_smooth(method = "lm") +
  theme_minimal() +
  labs(title = "2023 Canopy Closure % vs. Chlorophyll", x = "Yearly Avg Chl-A '23", y = "Canopy Closure '23 (%)")
## `geom_smooth()` using formula = 'y ~ x'

ggplot(canopy_peri, aes(x = Yearly_Avg_24, y = average_closure_24)) +
  geom_point() +
  geom_smooth(method = "lm") +
  theme_minimal() +
  labs(title = "2024 Canopy Closure % vs. Chlorophyll", x = "Yearly Avg Chl-A '24", y = "Canopy Closure '24 (%)")
## `geom_smooth()` using formula = 'y ~ x'

lm_canopy23 <- lm(average_closure_23 ~ Yearly_Avg_23, data = canopy_peri)

lm_canopy24 <- lm(average_closure_24 ~ Yearly_Avg_24, data = canopy_peri)

summary(lm_canopy23)
## 
## Call:
## lm(formula = average_closure_23 ~ Yearly_Avg_23, data = canopy_peri)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.8424 -1.0224  0.2846  0.7567  1.4360 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   92.09484    0.31531 292.073   <2e-16 ***
## Yearly_Avg_23  0.01932    0.28746   0.067    0.948    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.084 on 10 degrees of freedom
## Multiple R-squared:  0.0004516,  Adjusted R-squared:  -0.0995 
## F-statistic: 0.004518 on 1 and 10 DF,  p-value: 0.9477
summary(lm_canopy24)
## 
## Call:
## lm(formula = average_closure_24 ~ Yearly_Avg_24, data = canopy_peri)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.7978 -0.6950  0.5899  1.3685  3.3491 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    92.2451     0.8134 113.408   <2e-16 ***
## Yearly_Avg_24  -0.8086     0.7954  -1.017    0.333    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.718 on 10 degrees of freedom
## Multiple R-squared:  0.09367,    Adjusted R-squared:  0.003034 
## F-statistic: 1.033 on 1 and 10 DF,  p-value: 0.3333
# Histogram of chlorophyll values - using Avg_Chl for the reach since we don't need to scale it for comparison to other variables. This is just intersite of one variable.

ggplot(peri_harvest, aes(x = Avg_Chl, fill = Harvest_Status)) +
  geom_histogram(bins = 20, alpha = 0.7, position = "identity") +
  theme_minimal()

# Shapiro-Wilk test for normality
shapiro.test(peri_harvest$Avg_Chl)  # If p < 0.05, data is not normal
## 
##  Shapiro-Wilk normality test
## 
## data:  peri_harvest$Avg_Chl
## W = 0.70975, p-value = 6.742e-12
# QQ Plot
qqnorm(peri_harvest$Avg_Chl)
qqline(peri_harvest$Avg_Chl, col = "red")

#normality test miserably fails - lots of low values in general with some outliers. Let's find those outliers.

# Calculate Z-scores

z_peri_harvest <- peri_harvest %>% mutate(z_Avg_Chl = scale(Avg_Chl))

# Find extreme outliers (Z-score > 3 or < -3)
z_peri_harvest %>% filter(abs(z_Avg_Chl) > 3)
##       Site Sample.Date Season Year Avg_Chl Reach.length Chl_100 Harvest_Status
## 1 AZ_Reach    7/3/2023 Summer 2023  233.35         98.5  236.90            PRE
## 2 CP_Reach   6/26/2023 Summer 2023  346.33        111.3  311.17            PRE
##   Prescription z_Avg_Chl
## 1 Thin w/ gaps  3.273518
## 2          VRH  5.321246
#Looks like we need to remove only Bz from 9/2024 - makes sense, this was a mistaken filtration of all 125mL. The other two are true outliers.

peri_harvest <- peri_harvest[-c(20),]

#Tried both sqrt and log transformations, and data still fails normality test.

#Need to account for random effects by basin and investigate a little more closely. using mixed effects due to data not being normal. can handle the uneven replications as well. Both a Kruskal-Wallis and a Wilcox test fail because of lack of consistent pairs and some basins still being in PRE or CON status.

# Ensuring both Harvest_Status and Prescription are factors with appropriate levels
peri_harvest$Harvest_Status <- factor(peri_harvest$Harvest_Status, levels = c("PRE", "POST", "CON"))

peri_harvest$Prescription <- factor(peri_harvest$Prescription, levels = c("Wide thin", "VRH", "Thin w/ gaps", "Control"))

#Setting reference parameters for both harvest status and prescription as the Control sites

peri_harvest$Prescription <- relevel(peri_harvest$Prescription, ref = "Control")

peri_harvest$Harvest_Status <- relevel(peri_harvest$Harvest_Status, ref = "CON")

# LMM with random effect for Basin
lmm_model <- lmer(Avg_Chl ~ Harvest_Status * Prescription + (1 | Site), data = peri_harvest)
## fixed-effect model matrix is rank deficient so dropping 5 columns / coefficients
summary(lmm_model)
## Linear mixed model fit by REML ['lmerMod']
## Formula: Avg_Chl ~ Harvest_Status * Prescription + (1 | Site)
##    Data: peri_harvest
## 
## REML criterion at convergence: 878.2
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.4119 -0.5269 -0.2396  0.2112  5.2226 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  Site     (Intercept)  294.2   17.15   
##  Residual             2584.9   50.84   
## Number of obs: 87, groups:  Site, 20
## 
## Fixed effects:
##                                         Estimate Std. Error t value
## (Intercept)                               54.303     18.550   2.927
## Harvest_StatusPRE                          8.321     23.906   0.348
## Harvest_StatusPOST                        -1.024     29.376  -0.035
## PrescriptionWide thin                    -18.123     32.829  -0.552
## PrescriptionVRH                           -4.974     31.068  -0.160
## Harvest_StatusPRE:PrescriptionWide thin   -5.165     36.247  -0.143
## Harvest_StatusPRE:PrescriptionVRH         -6.285     33.569  -0.187
## 
## Correlation of Fixed Effects:
##             (Intr) Hr_SPRE H_SPOS PrscWt PrsVRH H_SPRt
## Hrvst_StPRE -0.776                                    
## Hrvst_SPOST -0.631  0.594                             
## PrscrptnWdt  0.000 -0.093  -0.538                     
## PrscrptnVRH  0.000 -0.098  -0.569  0.509              
## Hr_SPRE:PWt  0.000 -0.178   0.419 -0.793 -0.396       
## H_SPRE:PVRH  0.000 -0.193   0.452 -0.405 -0.830  0.494
## fit warnings:
## fixed-effect model matrix is rank deficient so dropping 5 columns / coefficients
# Visualize the fixed effects using ggplot
ggplot(peri_harvest, aes(x = Harvest_Status, y = Avg_Chl, color = Prescription)) +
  geom_boxplot() +
  labs(title = "Chlorophyll Levels by Harvest Status and Prescription",
       x = "Harvest Status", y = "Average Chlorophyll") +
  theme_minimal()

#results indicate that chlorophyll varies significantly across sites (529.2 ug/L, SD 23.00 ug/L), with quite a lot that is not really explainable by the model.

#control chl values are low (54.41 ug/L), which seems ecologically sound considering the heavy canopy coverage at most of these sites (~90%)

#Post prescription, we're seeing a very small drop in average chl in the first year - this will likely become more evident as populations adjust to disturbance and new light regimes

#Thin with gaps has the greatest drop in average chl. This is likely more due to immediate disturbance and scouring of periphyton populations in the reach, as well as drastic habitat unit changes. However not statistically significant

#Despite these observations of patterns, all t-values are low except for the intercept, suggesting that there hasn't been a significant effect of treatment or harvest type on chlorophyll values in the first year.
# Histogram of macro biomass by feeding group
ggplot(macros, aes(x = Biomass, fill = Feeding.Group)) +
  geom_histogram(bins = 10, binwidth = 0.25, alpha = 0.7, position = "identity") +
  xlim(0,5) +
  theme_minimal()
## Warning: Removed 30 rows containing non-finite outside the scale range
## (`stat_bin()`).
## Warning: Removed 16 rows containing missing values or values outside the scale range
## (`geom_bar()`).

# Shapiro-Wilk test for normality
shapiro.test(macros$Biomass)  # If p < 0.05, data is not normal
## 
##  Shapiro-Wilk normality test
## 
## data:  macros$Biomass
## W = 0.2247, p-value < 2.2e-16
# QQ Plot
qqnorm(macros$Biomass)
qqline(macros$Biomass, col = "forestgreen")

#Needs transformation
macros <- macros %>%
  mutate(
    Biomass_log = log(Biomass)
  )

# Histogram of macro biomass by feeding group
ggplot(macros, aes(x = Biomass_log, fill = Feeding.Group)) +
  geom_histogram(bins = 10, binwidth = 0.25, alpha = 0.7, position = "identity") +
  xlim(0,5) +
  theme_minimal()
## Warning: Removed 862 rows containing non-finite outside the scale range (`stat_bin()`).
## Removed 16 rows containing missing values or values outside the scale range
## (`geom_bar()`).

# Shapiro-Wilk test for normality
shapiro.test(macros$Biomass_log)  # If p < 0.05, data is not normal
## 
##  Shapiro-Wilk normality test
## 
## data:  macros$Biomass_log
## W = 0.99794, p-value = 0.2112
# QQ Plot
qqnorm(macros$Biomass_log)
qqline(macros$Biomass_log, col = "forestgreen")

# Calculate Z-scores

z_macros <- macros %>% mutate(z_Biomass_log = scale(Biomass_log))

# Find extreme outliers (Z-score > 3 or < -3)
z_macros %>% filter(abs(z_Biomass_log) > 3)
##       Basin Location  Date                       Taxon Stage Insect  Origin
## 1  CP_REACH    reach 45510        Ephemerella tibialis     L insect Aquatic
## 2 DA1_REACH    reach 45526                     Cinygma     L insect Aquatic
## 3 DA1_REACH    reach 45526                     Cinygma     L insect Aquatic
## 4 DA2_REACH    reach 45526 Rhabdomastix (Rhabdomastix)     L insect Aquatic
##   Higher.classification         Order         Family Common.name Length
## 1   Arthropoda: Insecta Ephemeroptera Ephemerellidae    mayflies      7
## 2   Arthropoda: Insecta Ephemeroptera  Heptageniidae    mayflies     12
## 3   Arthropoda: Insecta Ephemeroptera  Heptageniidae    mayflies      8
## 4   Arthropoda: Insecta       Diptera      Tipulidae crane flies     20
##   Abundance  Biomass Stage.1 BCG.Attribute Feeding.Group CA.feeding.group Habit
## 1 17.636684 45.19368       L             3            CG               CG    CL
## 2  6.783340 68.69617       L             4            SC               SC    CL
## 3 13.566680 44.97873       L             4            SC               SC    CL
## 4  3.956948 35.30367       L             2            SH               SH    BU
##   Tolerance WY.HBI PSSB.tolerance CA.tolerance Metals.tolerance BCI.TV
## 1         0      2              0            2               11     48
## 2         0      2              0            2               11     48
## 3         0      2              0            2               11     48
## 4        MI      3              0            3               11     72
##   PSSB.long.lived Voltinism Development Occurrence.in.drift Size.at.maturity
## 1               0         2           2                   2                2
## 2               0         2           1                   2                2
## 3               0         2           1                   2                2
## 4               0         1           2                   1                3
##   Rheophily Thermal.preference      a     b Biomass_log z_Biomass_log
## 1         2                  2 0.0104 2.830    3.810957      3.214113
## 2         2                  2 0.0108 2.754    4.229693      3.477871
## 3         2                  2 0.0108 2.754    3.806190      3.211110
## 4         2                  1 0.0029 2.681    3.563987      3.058548
#relationship with periphyton biomass?

#visualize biomass by feeding type per basin

ggplot(macros, aes(x = Basin, y = Biomass, fill = Feeding.Group)) +
geom_col() +
labs(title = "Biomass by Feeding Category and Basin", y = "Biomass", x = "Feeding Category") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

library(ggplot2)
library(dplyr)
library(scales)
## Warning: package 'scales' was built under R version 4.4.2
## 
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
## 
##     discard
## The following object is masked from 'package:readr':
## 
##     col_factor
# Calculate relative abundance and plot
ggplot(macros, aes(x = Basin, y = Biomass, fill = Feeding.Group)) +
  geom_col(position = "fill") +  # Stacks bars and normalizes to 1
  scale_y_continuous(labels = percent) +  # Y-axis as percentages
  labs(title = "Relative Biomass of Drift Feeding Groups by Basin",
       y = "Relative Biomass", 
       x = "Basin") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

# Calculate relative abundance for each Feeding.Group in each Basin
drift_biosummary <- macros %>%
  group_by(Basin, Feeding.Group) %>%
  summarise(Biomass = sum(Biomass), .groups = "drop") %>%
  group_by(Basin) %>%
  mutate(Relative_Biomass = paste0(round((Biomass / sum(Biomass))*100, 0), "%")) %>%
  select(Basin, Feeding.Group, Relative_Biomass)

# Create a table with relative abundance in percentage format
drift_biosummary_table <- drift_biosummary 

print(drift_biosummary_table)
## # A tibble: 114 × 3
## # Groups:   Basin [20]
##    Basin     Feeding.Group Relative_Biomass
##    <chr>     <chr>         <chr>           
##  1 AA1_REACH CF            9%              
##  2 AA1_REACH CG            46%             
##  3 AA1_REACH PA            1%              
##  4 AA1_REACH PR            3%              
##  5 AA1_REACH SC            8%              
##  6 AA1_REACH SH            4%              
##  7 AA1_REACH UN            27%             
##  8 AA2_REACH CF            21%             
##  9 AA2_REACH CG            26%             
## 10 AA2_REACH PA            2%              
## # ℹ 104 more rows
# Aggregate by Basin and Feeding Category

macro_aggregated <- macros %>%
  group_by(Basin, Feeding.Group) %>% 
  summarise(
    total_biomass = sum(Biomass),
    mean_biomass = mean(Biomass))
## `summarise()` has grouped output by 'Basin'. You can override using the
## `.groups` argument.
peri_macro <- macro_aggregated %>%
  left_join(twoyr_peri, by = "Basin")

#scatter plot of total macroinvertebrate biomass vs periphyton biomass by Basin
ggplot(peri_macro, aes(x = Total_Avg, y = total_biomass, color = Feeding.Group)) +
  geom_point() +
  geom_smooth(method = "lm") +
  labs(title = "Macroinvertebrate Biomass vs Periphyton Biomass by Basin",
       x = "Periphyton Biomass", y = "Total Macroinvertebrate Biomass")
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 13 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 13 rows containing missing values or values outside the scale range
## (`geom_point()`).

library(lme4)

peri_macro <- peri_macro %>%
  mutate(
    total_biomass_log = log(total_biomass),
    mean_biomass_log = log(mean_biomass),
    Total_Avg_log = log(Total_Avg)
  )

#scale

peri_macro$total_biomass_log <- scale(peri_macro$total_biomass_log)

peri_macro$Total_Avg_log <- scale(peri_macro$Total_Avg_log)

peri_macro$mean_biomass_log <- scale(peri_macro$mean_biomass_log)

library(lme4)

# Model with Feeding.Group as a fixed effect and random effects for Basin
model_peri_macro <- lmer(
  total_biomass_log ~ Total_Avg_log * Feeding.Group + (1 | Basin),
  data = peri_macro
)

summary(model_peri_macro)
## Linear mixed model fit by REML ['lmerMod']
## Formula: total_biomass_log ~ Total_Avg_log * Feeding.Group + (1 | Basin)
##    Data: peri_macro
## 
## REML criterion at convergence: 228.4
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -1.92708 -0.58988 -0.04268  0.60981  1.91015 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  Basin    (Intercept) 0.4344   0.6591  
##  Residual             0.3911   0.6254  
## Number of obs: 101, groups:  Basin, 18
## 
## Fixed effects:
##                               Estimate Std. Error t value
## (Intercept)                   -0.02427    0.23913  -0.101
## Total_Avg_log                 -0.33249    0.25412  -1.308
## Feeding.GroupCG                0.72455    0.23677   3.060
## Feeding.GroupMH               -1.20817    0.62748  -1.925
## Feeding.GroupPA               -0.91222    0.24143  -3.778
## Feeding.GroupPR                0.07458    0.26173   0.285
## Feeding.GroupSC                0.33937    0.24230   1.401
## Feeding.GroupSH               -0.64930    0.26188  -2.479
## Feeding.GroupUN                0.14576    0.24064   0.606
## Total_Avg_log:Feeding.GroupCG  0.32607    0.24933   1.308
## Total_Avg_log:Feeding.GroupMH  0.50823    0.73948   0.687
## Total_Avg_log:Feeding.GroupPA  0.51515    0.24932   2.066
## Total_Avg_log:Feeding.GroupPR  0.17930    0.26828   0.668
## Total_Avg_log:Feeding.GroupSC  0.25923    0.25723   1.008
## Total_Avg_log:Feeding.GroupSH -0.29625    0.27887  -1.062
## Total_Avg_log:Feeding.GroupUN  0.32742    0.25748   1.272
## 
## Correlation matrix not shown by default, as p = 16 > 12.
## Use print(x, correlation=TRUE)  or
##     vcov(x)        if you need it
# Plot the relationship for each Feeding Group
ggplot(peri_macro, aes(x = Total_Avg_log, y = total_biomass_log, color = Feeding.Group)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +  # Linear regression line
  facet_wrap(~ Feeding.Group) +  # Create separate plots for each Feeding Group
  labs(title = "Relationship between Biennial Periphyton Biomass and Total Drift Macro Biomass by Feeding Group", 
       x = "Total Average Periphyton Biomass", 
       y = "Drift Total Biomass") +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 13 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Removed 13 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggplot(peri_macro, aes(x = Total_Avg_log, y = total_biomass_log, color = Feeding.Group)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +  # Linear regression line
  facet_wrap(~ Basin) +  # Create separate plots for each Feeding Group
  labs(title = "Relationship between Biennial Periphyton Biomass and Total Drift Macro Biomass by Basin", 
       x = "Total Average Periphyton Biomass", 
       y = "Drift Total Biomass") +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 13 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Removed 13 rows containing missing values or values outside the scale range
## (`geom_point()`).

# Histogram of macro biomass by feeding group
ggplot(macros, aes(x = Abundance, fill = Feeding.Group)) +
  geom_histogram(bins = 10, binwidth = 0.25, alpha = 0.7, position = "identity") +
  xlim(0,5) +
  theme_minimal()
## Warning: Removed 457 rows containing non-finite outside the scale range
## (`stat_bin()`).
## Warning: Removed 16 rows containing missing values or values outside the scale range
## (`geom_bar()`).

# Shapiro-Wilk test for normality
shapiro.test(macros$Abundance)  # If p < 0.05, data is not normal
## 
##  Shapiro-Wilk normality test
## 
## data:  macros$Abundance
## W = 0.34602, p-value < 2.2e-16
# QQ Plot
qqnorm(macros$Abundance)
qqline(macros$Abundance, col = "lightblue3")

#Needs transformation
macros2 <- macros %>%
  mutate(
    Abundance_log = log(Abundance)
  )

# Histogram of macro biomass by feeding group
ggplot(macros2, aes(x = Abundance_log, fill = Feeding.Group)) +
  geom_histogram(bins = 10, binwidth = 0.25, alpha = 0.7, position = "identity") +
  xlim(0,5) +
  theme_minimal()
## Warning: Removed 4 rows containing non-finite outside the scale range (`stat_bin()`).
## Removed 16 rows containing missing values or values outside the scale range
## (`geom_bar()`).

# Shapiro-Wilk test for normality
shapiro.test(macros2$Abundance_log)  # If p < 0.05, data is not normal
## 
##  Shapiro-Wilk normality test
## 
## data:  macros2$Abundance_log
## W = 0.9357, p-value < 2.2e-16
# QQ Plot
qqnorm(macros2$Abundance_log)
qqline(macros2$Abundance_log, col = "lightblue3")

# Calculate Z-scores

z_macros2 <- macros2 %>% mutate(z_Abundance_log = scale(Abundance_log))

# Find extreme outliers (Z-score > 3 or < -3)
z_macros2 %>% filter(abs(z_Abundance_log) > 3)
##        Basin Location  Date                      Taxon Stage Insect      Origin
## 1   AZ_REACH    reach 45572 Baetis tricaudatus complex     L insect     Aquatic
## 2   AZ_REACH    reach 45572                 Brachycera     L insect Terrestrial
## 3   AZ_REACH    reach 45572                 Brachycera     L insect Terrestrial
## 4   BC_REACH    reach 45512                 Glossosoma     L insect     Aquatic
## 5   BC_REACH    reach 45512                 Glossosoma     L insect     Aquatic
## 6   CP_REACH    reach 45510 Baetis tricaudatus complex     L insect     Aquatic
## 7   CP_REACH    reach 45510 Baetis tricaudatus complex     L insect     Aquatic
## 8   CP_REACH    reach 45510 Baetis tricaudatus complex     L insect     Aquatic
## 9   CP_REACH    reach 45510    Tvetenia bavarica group     L insect     Aquatic
## 10  CP_REACH    reach 45510    Tvetenia bavarica group     L insect     Aquatic
## 11  CP_REACH    reach 45510    Tvetenia bavarica group     L insect     Aquatic
## 12 CZ2_REACH    reach 45504 Baetis tricaudatus complex     L insect     Aquatic
##    Higher.classification         Order                       Family
## 1    Arthropoda: Insecta Ephemeroptera                     Baetidae
## 2    Arthropoda: Insecta       Diptera                            x
## 3    Arthropoda: Insecta       Diptera                            x
## 4    Arthropoda: Insecta   Trichoptera              Glossosomatidae
## 5    Arthropoda: Insecta   Trichoptera              Glossosomatidae
## 6    Arthropoda: Insecta Ephemeroptera                     Baetidae
## 7    Arthropoda: Insecta Ephemeroptera                     Baetidae
## 8    Arthropoda: Insecta Ephemeroptera                     Baetidae
## 9    Arthropoda: Insecta       Diptera Chironomidae: Orthocladiinae
## 10   Arthropoda: Insecta       Diptera Chironomidae: Orthocladiinae
## 11   Arthropoda: Insecta       Diptera Chironomidae: Orthocladiinae
## 12   Arthropoda: Insecta Ephemeroptera                     Baetidae
##     Common.name Length Abundance   Biomass Stage.1 BCG.Attribute Feeding.Group
## 1      mayflies    1.5 249.07949 4.2352222       L             4            CG
## 2  higher flies    2.0 162.44314 3.9014401       A             0            UN
## 3  higher flies    2.0 162.44314 3.9014401       L             0            UN
## 4   caddisflies    1.5  88.18342 2.3992679       L             3            SC
## 5   caddisflies    1.5  88.18342 2.3992679       P             3            SC
## 6      mayflies    1.0  84.65608 0.4486772       L             4            CG
## 7      mayflies    1.5 229.27690 3.8985089       L             4            CG
## 8      mayflies    2.0  84.65608 3.2915108       L             4            CG
## 9        midges    2.0  98.76543 1.0906188       L             4            CG
## 10       midges    2.5 112.87478 2.2350046       L             4            CG
## 11       midges    3.0 123.45679 3.9392524       L             4            CG
## 12     mayflies    1.5  77.16049 1.3119982       L             4            CG
##    CA.feeding.group Habit Tolerance WY.HBI PSSB.tolerance CA.tolerance
## 1                CG    CL         0      6              0            6
## 2                UN    BU        HT      0              0           11
## 3                UN    BU        HT      0              0           11
## 4                SC    CL         0      1              0            1
## 5                SC    CL         0      1              0            1
## 6                CG    CL         0      6              0            6
## 7                CG    CL         0      6              0            6
## 8                CG    CL         0      6              0            6
## 9                CG    SP         0      5              0            5
## 10               CG    SP         0      5              0            5
## 11               CG    SP         0      5              0            5
## 12               CG    CL         0      6              0            6
##    Metals.tolerance BCI.TV PSSB.long.lived Voltinism Development
## 1                11     72               0         3           1
## 2                11    110               0         3           2
## 3                11    110               0         3           2
## 4                11     24               0         3           3
## 5                11     24               0         3           3
## 6                11     72               0         3           1
## 7                11     72               0         3           1
## 8                11     72               0         3           1
## 9                 4    108               0         3           1
## 10                4    108               0         3           1
## 11                4    108               0         3           1
## 12               11     72               0         3           1
##    Occurrence.in.drift Size.at.maturity Rheophily Thermal.preference      a
## 1                    3                1         2                  2 0.0053
## 2                    1                2         1                  2 0.0060
## 3                    1                2         1                  2 0.0040
## 4                    1                2         3                  2 0.0082
## 5                    1                2         3                  2 0.0082
## 6                    3                1         2                  2 0.0053
## 7                    3                1         2                  2 0.0053
## 8                    3                1         2                  2 0.0053
## 9                    3                1         2                  2 0.0018
## 10                   3                1         2                  2 0.0018
## 11                   3                1         2                  2 0.0018
## 12                   3                1         2                  2 0.0053
##        b Biomass_log Abundance_log z_Abundance_log
## 1  2.875  1.44343581      5.517772        4.574188
## 2  3.050  1.36134575      5.090328        4.064580
## 3  2.586  1.36134575      5.090328        4.064580
## 4  2.958  0.87516365      4.479419        3.336241
## 5  2.958  0.87516365      4.479419        3.336241
## 6  2.875 -0.80145147      4.438597        3.287573
## 7  2.875  1.36059415      5.434930        4.475422
## 8  2.875  1.19134667      4.438597        3.287573
## 9  2.617  0.08674522      4.592748        3.471354
## 10 2.617  0.80424329      4.726279        3.630553
## 11 2.617  1.37099096      4.815891        3.737391
## 12 2.875  0.27155132      4.345888        3.177042
#visualize abundance by feeding type per basin

ggplot(macros2, aes(x = Basin, y = Abundance, fill = Feeding.Group)) +
geom_col() +
labs(title = "Abundance by Feeding Category and Basin", y = "Abundance", x = "Feeding Category") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

library(ggplot2)
library(dplyr)
library(scales)

# Calculate relative abundance and plot
ggplot(macros, aes(x = Basin, y = Abundance, fill = Feeding.Group)) +
  geom_col(position = "fill") +  # Stacks bars and normalizes to 1
  scale_y_continuous(labels = percent) +  # Y-axis as percentages
  labs(title = "Relative Abundance of Drift Feeding Groups by Basin",
       y = "Relative Abundance", 
       x = "Basin") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

# Calculate relative abundance for each Feeding.Group in each Basin
drift_absummary <- macros %>%
  group_by(Basin, Feeding.Group) %>%
  summarise(Abundance = sum(Abundance), .groups = "drop") %>%
  group_by(Basin) %>%
  mutate(Relative_Abundance = paste0(round((Abundance / sum(Abundance))*100, 0), "%")) %>%
  select(Basin, Feeding.Group, Relative_Abundance)

# Create a table with relative abundance in percentage format
drift_absummary_table <- drift_absummary 

print(drift_absummary_table)
## # A tibble: 114 × 3
## # Groups:   Basin [20]
##    Basin     Feeding.Group Relative_Abundance
##    <chr>     <chr>         <chr>             
##  1 AA1_REACH CF            5%                
##  2 AA1_REACH CG            62%               
##  3 AA1_REACH PA            5%                
##  4 AA1_REACH PR            4%                
##  5 AA1_REACH SC            7%                
##  6 AA1_REACH SH            6%                
##  7 AA1_REACH UN            12%               
##  8 AA2_REACH CF            16%               
##  9 AA2_REACH CG            42%               
## 10 AA2_REACH PA            11%               
## # ℹ 104 more rows
# Aggregate by Basin and Feeding Category

macro_aggregated2 <- macros2 %>%
  group_by(Basin, Feeding.Group) %>% 
  summarise(
    total_abundance = sum(Abundance))
## `summarise()` has grouped output by 'Basin'. You can override using the
## `.groups` argument.
peri_macro2 <- macro_aggregated2 %>%
  left_join(twoyr_peri, by = "Basin")

#scatter plot of total macroinvertebrate abundance vs periphyton abundance by Basin
ggplot(peri_macro2, aes(x = Total_Avg, y = total_abundance, color = Feeding.Group)) +
  geom_point() +
  geom_smooth(method = "lm") +
  labs(title = "Macroinvertebrate Abundance vs Periphyton Biomass by Basin",
       x = "Periphyton Biomass", y = "Total Macroinvertebrate Abundance")
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 13 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 13 rows containing missing values or values outside the scale range
## (`geom_point()`).

peri_macro2 <- peri_macro2 %>%
  mutate(
    total_abundance_log = log(total_abundance),
    Total_Avg_log = log(Total_Avg)
  )

#scale abundance
peri_macro2$total_abundance_log <- scale(peri_macro2$total_abundance_log)

peri_macro2$Total_Avg_log <- scale(peri_macro2$Total_Avg_log)


# Model with Feeding.Group as a fixed effect and random effects for Basin
model_peri_macro2 <- lmer(
  total_abundance_log ~ Total_Avg_log * Feeding.Group + (1 | Basin),
  data = peri_macro2
)

summary(model_peri_macro2)
## Linear mixed model fit by REML ['lmerMod']
## Formula: total_abundance_log ~ Total_Avg_log * Feeding.Group + (1 | Basin)
##    Data: peri_macro2
## 
## REML criterion at convergence: 217.5
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.19154 -0.65683  0.04784  0.53789  2.63320 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  Basin    (Intercept) 0.4059   0.6371  
##  Residual             0.3402   0.5833  
## Number of obs: 101, groups:  Basin, 18
## 
## Fixed effects:
##                                Estimate Std. Error t value
## (Intercept)                    0.003149   0.226603   0.014
## Total_Avg_log                 -0.044310   0.240501  -0.184
## Feeding.GroupCG                1.295045   0.220847   5.864
## Feeding.GroupMH               -1.623826   0.585451  -2.774
## Feeding.GroupPA               -0.414475   0.225209  -1.840
## Feeding.GroupPR               -0.488028   0.244150  -1.999
## Feeding.GroupSC               -0.119888   0.226150  -0.530
## Feeding.GroupSH               -0.216593   0.244290  -0.887
## Feeding.GroupUN               -0.028409   0.224463  -0.127
## Total_Avg_log:Feeding.GroupCG  0.122203   0.232590   0.525
## Total_Avg_log:Feeding.GroupMH  0.155456   0.689947   0.225
## Total_Avg_log:Feeding.GroupPA  0.086776   0.232580   0.373
## Total_Avg_log:Feeding.GroupPR -0.161271   0.250265  -0.644
## Total_Avg_log:Feeding.GroupSC  0.137122   0.240000   0.571
## Total_Avg_log:Feeding.GroupSH -0.401843   0.260159  -1.545
## Total_Avg_log:Feeding.GroupUN  0.084468   0.240221   0.352
## 
## Correlation matrix not shown by default, as p = 16 > 12.
## Use print(x, correlation=TRUE)  or
##     vcov(x)        if you need it
# Plot the relationship for each Feeding Group
ggplot(peri_macro2, aes(x = Total_Avg_log, y = total_abundance_log, color = Feeding.Group)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +  # Linear regression line
  facet_wrap(~ Feeding.Group) +  # Create separate plots for each Feeding Group
  labs(title = "Relationship between Biennial Periphyton Biomass and Total Drift Macro Abundance by Feeding Group", 
       x = "Total Average Periphyton Biomass", 
       y = "Drift Abundance") +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 13 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Removed 13 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggplot(peri_macro2, aes(x = Total_Avg_log, y = total_abundance_log, color = Feeding.Group)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +  # Linear regression line
  facet_wrap(~ Basin) +  # Create separate plots for each Feeding Group
  labs(title = "Relationship between Biennial Periphyton Biomass and Total Drift Macro Abundance by Basin", 
       x = "Total Average Periphyton Biomass", 
       y = "Drift Abundance") +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 13 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Removed 13 rows containing missing values or values outside the scale range
## (`geom_point()`).

# Histogram of macro biomass by feeding group
ggplot(benthic, aes(x = Biomass, fill = Feeding.Group)) +
  geom_histogram(bins = 10, binwidth = 0.25, alpha = 0.7, position = "identity") +
  xlim(0,5) +
  theme_minimal()
## Warning: Removed 572 rows containing non-finite outside the scale range
## (`stat_bin()`).
## Warning: Removed 18 rows containing missing values or values outside the scale range
## (`geom_bar()`).

# Shapiro-Wilk test for normality
shapiro.test(benthic$Biomass)  # If p < 0.05, data is not normal
## 
##  Shapiro-Wilk normality test
## 
## data:  benthic$Biomass
## W = 0.01987, p-value < 2.2e-16
# QQ Plot
qqnorm(benthic$Biomass)
qqline(benthic$Biomass, col = "forestgreen")

#Needs transformation
benthic <- benthic %>%
  mutate(
    Biomass_log = log(Biomass)
  )

# Histogram of macro biomass by feeding group
ggplot(benthic, aes(x = Biomass_log, fill = Feeding.Group)) +
  geom_histogram(bins = 10, binwidth = 0.25, alpha = 0.7, position = "identity") +
  xlim(0,5) +
  theme_minimal()
## Warning: Removed 1907 rows containing non-finite outside the scale range (`stat_bin()`).
## Removed 18 rows containing missing values or values outside the scale range
## (`geom_bar()`).

# Shapiro-Wilk test for normality
shapiro.test(benthic$Biomass_log)  # If p < 0.05, data is not normal
## 
##  Shapiro-Wilk normality test
## 
## data:  benthic$Biomass_log
## W = 0.99327, p-value = 2.609e-11
# QQ Plot
qqnorm(benthic$Biomass_log)
qqline(benthic$Biomass_log, col = "blueviolet")

# Calculate Z-scores

z_benthic <- benthic %>% mutate(z_Biomass_log = scale(Biomass_log))

# Find extreme outliers (Z-score > 3 or < -3)
z_benthic %>% filter(abs(z_Biomass_log) > 3)
##       Basin Location  Date                  Taxon Stage     Insect  Origin
## 1  CC_REACH    reach 45524 Calineuria californica     L     insect Aquatic
## 2  CP_REACH    reach 45510   Pteronarcys princeps     L     insect Aquatic
## 3 CZ2_REACH    reach 45504 Calineuria californica     L     insect Aquatic
## 4 DA1_REACH    reach 45526           Pacifastacus     U non-insect Aquatic
## 5 DA2_REACH    reach 45526           Pacifastacus     U non-insect Aquatic
## 6  DC_REACH    reach 45505           Pacifastacus     U non-insect Aquatic
## 7  DZ_REACH    reach 45502           Pacifastacus     U non-insect Aquatic
##   Higher.classification      Order         Family Common.name Length Abundance
## 1   Arthropoda: Insecta Plecoptera       Perlidae  stoneflies     21   6.66000
## 2   Arthropoda: Insecta Plecoptera Pteronarcyidae  stoneflies     37   2.22000
## 3   Arthropoda: Insecta Plecoptera       Perlidae  stoneflies     25   4.44000
## 4   Crustacea: Decapoda          x      Astacidae    crayfish     30   2.22000
## 5   Crustacea: Decapoda          x      Astacidae    crayfish     28   2.22000
## 6   Crustacea: Decapoda          x      Astacidae    crayfish     12   2.65164
## 7   Crustacea: Decapoda          x      Astacidae    crayfish     30   2.32600
##     Biomass Table3.BCG.Attribute Feeding.Group Table3.CA.feeding.group
## 1  422.4534                    2            PR                      PR
## 2  779.6165                    2            OM                      OM
## 3  465.2511                    2            PR                      PR
## 4 7408.1877                    4            OM                      OM
## 5 5768.5323                    4            OM                      OM
## 6  319.1120                    4            OM                      OM
## 7 7761.9120                    4            OM                      OM
##   Table3.Habit Table3.Tolerance Table3.WY.HBI Table3.PSSB.tolerance
## 1           CL                0             2                     0
## 2           SP               MI             1                     0
## 3           CL                0             2                     0
## 4           SP                0             6                     0
## 5           SP                0             6                     0
## 6           SP                0             6                     0
## 7           SP                0             6                     0
##   Table3.CA.tolerance Table3.Metals.tolerance Table3.BCI.TV
## 1                   2                      11            24
## 2                   0                       1            24
## 3                   2                      11            24
## 4                   6                       3           108
## 5                   6                       3           108
## 6                   6                       3           108
## 7                   6                       3           108
##   Table3.PSSB.long.lived Table3.Voltinism Table3.Development
## 1                      0                1                  2
## 2                      0                1                  2
## 3                      0                1                  2
## 4                      0                1                  3
## 5                      0                1                  3
## 6                      0                1                  3
## 7                      0                1                  3
##   Table3.Occurrence.in.drift Table3.Size.at.maturity Table3.Rheophily
## 1                          1                       3                3
## 2                          1                       3                2
## 3                          1                       3                3
## 4                          1                       3                2
## 5                          1                       3                2
## 6                          1                       3                2
## 7                          1                       3                2
##   Table3.Thermal.preference Table3.a Table3.b Biomass_log z_Biomass_log
## 1                         2   0.0099    2.879    6.046079      3.254630
## 2                         1   0.0324    2.573    6.658802      3.569277
## 3                         2   0.0099    2.879    6.142577      3.304184
## 4                         2   0.0147    3.626    8.910341      4.725491
## 5                         2   0.0147    3.626    8.660173      4.597024
## 6                         2   0.0147    3.626    5.765542      3.110568
## 7                         2   0.0147    3.626    8.956984      4.749443
#visualize biomass by feeding type per basin

ggplot(benthic, aes(x = Basin, y = Biomass, fill = Feeding.Group)) +
geom_col() +
labs(title = "Benthic Biomass by Feeding Category and Basin", y = "Biomass", x = "Feeding Category") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

# Calculate relative abundance and plot
ggplot(benthic, aes(x = Basin, y = Biomass, fill = Feeding.Group)) +
  geom_col(position = "fill") +  # Stacks bars and normalizes to 1
  scale_y_continuous(labels = percent) +  # Y-axis as percentages
  labs(title = "Relative Biomass of Feeding Benthic Groups by Basin",
       y = "Relative Biomass", 
       x = "Basin") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

# Calculate relative abundance for each Feeding.Group in each Basin
benthic_biosummary <- benthic %>%
  group_by(Basin, Feeding.Group) %>%
  summarise(Biomass = sum(Biomass), .groups = "drop") %>%
  group_by(Basin) %>%
  mutate(Relative_Biomass = paste0(round((Biomass / sum(Biomass))*100, 0), "%")) %>%
  select(Basin, Feeding.Group, Relative_Biomass)

# Create a table with relative abundance in percentage format
benthic_biosummary_table <- benthic_biosummary 

print(benthic_biosummary_table)
## # A tibble: 134 × 3
## # Groups:   Basin [20]
##    Basin     Feeding.Group Relative_Biomass
##    <chr>     <chr>         <chr>           
##  1 AA1_REACH CF            4%              
##  2 AA1_REACH CG            8%              
##  3 AA1_REACH DSH           10%             
##  4 AA1_REACH PA            0%              
##  5 AA1_REACH PR            73%             
##  6 AA1_REACH SC            6%              
##  7 AA2_REACH CF            2%              
##  8 AA2_REACH CG            29%             
##  9 AA2_REACH DSH           1%              
## 10 AA2_REACH PA            1%              
## # ℹ 124 more rows
# Aggregate by Basin and Feeding Category

benthic_aggregated <- benthic %>%
  group_by(Basin, Feeding.Group) %>% 
  summarise(
    total_biomass = sum(Biomass),
    mean_biomass = mean(Biomass))
## `summarise()` has grouped output by 'Basin'. You can override using the
## `.groups` argument.
peri_benthic <- benthic_aggregated %>%
  left_join(twoyr_peri, by = "Basin")


peri_benthic$total
## Warning: Unknown or uninitialised column: `total`.
## NULL
#scatter plot of total macroinvertebrate biomass vs periphyton biomass by Basin
ggplot(peri_benthic, aes(x = Total_Avg, y = total_biomass, color = Feeding.Group)) +
  geom_point() +
  geom_smooth(method = "lm") +
  labs(title = "Benthic Macroinvertebrate Biomass vs Periphyton Biomass by Basin",
       x = "Periphyton Biomass", y = "Total Benthic Biomass")
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 13 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning in qt((1 - level)/2, df): NaNs produced
## Warning: Removed 13 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning in max(ids, na.rm = TRUE): no non-missing arguments to max; returning
## -Inf

peri_benthic <- peri_benthic %>%
  mutate(
    total_biomass_log = log(total_biomass),
    mean_biomass_log = log(mean_biomass),
    Total_Avg_log = log(Total_Avg)
  )

#scale biomass
peri_benthic$total_biomass_log <- scale(peri_benthic$total_biomass_log)

peri_benthic$mean_biomass_log <- scale(peri_benthic$mean_biomass_log)

peri_benthic$Total_Avg_log <- scale(peri_benthic$Total_Avg_log)

# Model with Feeding.Group as a fixed effect and random effects for Basin
model_peri_benthic <- lmer(
  total_biomass_log ~ Total_Avg_log * Feeding.Group + (1 | Basin),
  data = peri_benthic
)

summary(model_peri_benthic)
## Linear mixed model fit by REML ['lmerMod']
## Formula: total_biomass_log ~ Total_Avg_log * Feeding.Group + (1 | Basin)
##    Data: peri_benthic
## 
## REML criterion at convergence: 167.5
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.35094 -0.48172 -0.03527  0.48575  2.74866 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  Basin    (Intercept) 0.06861  0.2619  
##  Residual             0.15970  0.3996  
## Number of obs: 121, groups:  Basin, 18
## 
## Fixed effects:
##                                Estimate Std. Error t value
## (Intercept)                    -0.32510    0.11271  -2.884
## Total_Avg_log                   0.24210    0.11415   2.121
## Feeding.GroupCG                 0.79975    0.13331   5.999
## Feeding.GroupDSH                0.06041    0.13331   0.453
## Feeding.GroupHSH               -0.68419    0.31094  -2.200
## Feeding.GroupMH                -1.68420    0.22036  -7.643
## Feeding.GroupOM                 2.05497    0.21329   9.635
## Feeding.GroupPA                -0.71464    0.13331  -5.361
## Feeding.GroupPR                 1.23052    0.13331   9.230
## Feeding.GroupSC                 0.94421    0.13331   7.083
## Total_Avg_log:Feeding.GroupCG   0.01562    0.13501   0.116
## Total_Avg_log:Feeding.GroupDSH -0.25048    0.13501  -1.855
## Total_Avg_log:Feeding.GroupHSH -0.75785    0.23718  -3.195
## Total_Avg_log:Feeding.GroupMH  -0.11267    0.23110  -0.488
## Total_Avg_log:Feeding.GroupOM  -0.47899    0.20359  -2.353
## Total_Avg_log:Feeding.GroupPA   0.04621    0.13501   0.342
## Total_Avg_log:Feeding.GroupPR  -0.03789    0.13501  -0.281
## Total_Avg_log:Feeding.GroupSC   0.01055    0.13501   0.078
## 
## Correlation matrix not shown by default, as p = 18 > 12.
## Use print(x, correlation=TRUE)  or
##     vcov(x)        if you need it
# Plot the relationship for each Feeding Group
ggplot(peri_benthic, aes(x = Total_Avg_log, y = total_biomass_log, color = Feeding.Group)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +  # Linear regression line
  facet_wrap(~ Feeding.Group) +  # Create separate plots for each Feeding Group
  labs(title = "Relationship between Biennial Periphyton Biomass and Benthic Macro Biomass by Feeding Group", 
       x = "Total Average Periphyton Biomass", 
       y = "Benthic Biomass") +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 13 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 13 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggplot(peri_benthic, aes(x = Total_Avg_log, y = total_biomass_log, color = Feeding.Group)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +  # Linear regression line
  facet_wrap(~ Basin) +  # Create separate plots for each Basin
  labs(title = "Relationship between Biennial Periphyton Biomass and Benthic Macro Biomass by Basin", 
       x = "Total Average Periphyton Biomass", 
       y = "Benthic Biomass") +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 13 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Removed 13 rows containing missing values or values outside the scale range
## (`geom_point()`).

 #Histogram of benthic abundance by feeding group

ggplot(benthic, aes(x = Abundance, fill = Feeding.Group)) +
  geom_histogram(bins = 10, binwidth = 0.25, alpha = 0.7, position = "identity") +
  xlim(0,5) +
  theme_minimal()
## Warning: Removed 1433 rows containing non-finite outside the scale range
## (`stat_bin()`).
## Warning: Removed 18 rows containing missing values or values outside the scale range
## (`geom_bar()`).

# Shapiro-Wilk test for normality
shapiro.test(benthic$Abundance)  # If p < 0.05, data is not normal
## 
##  Shapiro-Wilk normality test
## 
## data:  benthic$Abundance
## W = 0.32183, p-value < 2.2e-16
# QQ Plot
qqnorm(benthic$Abundance)
qqline(benthic$Abundance, col = "lightblue3")

#Needs transformation
benthic2 <- benthic %>%
  mutate(
    Abundance_log = log(Abundance)
  )

# Histogram of macro biomass by feeding group
ggplot(benthic2, aes(x = Abundance_log, fill = Feeding.Group)) +
  geom_histogram(bins = 10, binwidth = 0.25, alpha = 0.7, position = "identity") +
  xlim(0,5) +
  theme_minimal()
## Warning: Removed 29 rows containing non-finite outside the scale range (`stat_bin()`).
## Removed 18 rows containing missing values or values outside the scale range
## (`geom_bar()`).

# Shapiro-Wilk test for normality
shapiro.test(benthic2$Abundance_log)  # If p < 0.05, data is not normal
## 
##  Shapiro-Wilk normality test
## 
## data:  benthic2$Abundance_log
## W = 0.86442, p-value < 2.2e-16
# QQ Plot
qqnorm(benthic2$Abundance_log)
qqline(benthic2$Abundance_log, col = "lightblue3")

# Calculate Z-scores

z_benthic2 <- benthic2 %>% mutate(z_Abundance_log = scale(Abundance_log))

# Find extreme outliers (Z-score > 3 or < -3)
z_benthic2 %>% filter(abs(z_Abundance_log) > 3)
##        Basin Location  Date                      Taxon Stage Insect  Origin
## 1  AA2_REACH    reach 45516 Baetis tricaudatus complex     L insect Aquatic
## 2  AA2_REACH    reach 45516                 Glossosoma     L insect Aquatic
## 3  AA2_REACH    reach 45516                 Glossosoma     L insect Aquatic
## 4   AZ_REACH    reach 45572                  Cinygmula     L insect Aquatic
## 5  BA1_REACH    reach 45528               Micropsectra     L insect Aquatic
## 6  BA1_REACH    reach 45528               Micropsectra     L insect Aquatic
## 7  BA2_REACH    reach 45559 Baetis tricaudatus complex     L insect Aquatic
## 8  BA2_REACH    reach 45559 Baetis tricaudatus complex     L insect Aquatic
## 9  BA2_REACH    reach 45559 Baetis tricaudatus complex     L insect Aquatic
## 10 BA2_REACH    reach 45559           Zapada cinctipes     L insect Aquatic
## 11  BC_REACH    reach 45512                 Glossosoma     L insect Aquatic
## 12  BC_REACH    reach 45512                 Glossosoma     L insect Aquatic
## 13  BP_REACH    reach 45517                 Glossosoma     L insect Aquatic
## 14  BP_REACH    reach 45517                 Glossosoma     L insect Aquatic
## 15  BP_REACH    reach 45517                 Glossosoma     L insect Aquatic
## 16  BP_REACH    reach 45517                 Glossosoma     L insect Aquatic
## 17  BP_REACH    reach 45517                 Glossosoma     L insect Aquatic
## 18  BP_REACH    reach 45517                 Glossosoma     L insect Aquatic
## 19  BZ_REACH    reach 45511 Baetis tricaudatus complex     L insect Aquatic
## 20  BZ_REACH    reach 45511           Zapada cinctipes     L insect Aquatic
## 21 CA2_REACH    reach 45573                Optioservus     A insect Aquatic
## 22 CA2_REACH    reach 45573 Baetis tricaudatus complex     L insect Aquatic
## 23 CA2_REACH    reach 45573                  Cinygmula     L insect Aquatic
## 24 CA2_REACH    reach 45573                  Cinygmula     L insect Aquatic
## 25 CA2_REACH    reach 45573                 Glossosoma     L insect Aquatic
## 26 CA2_REACH    reach 45573                 Glossosoma     L insect Aquatic
## 27  CC_REACH    reach 45524               Micropsectra     L insect Aquatic
## 28  CC_REACH    reach 45524               Micropsectra     L insect Aquatic
## 29  CC_REACH    reach 45524 Baetis tricaudatus complex     L insect Aquatic
## 30  CC_REACH    reach 45524     Calineuria californica     L insect Aquatic
## 31  CC_REACH    reach 45524                  Cinygmula     L insect Aquatic
## 32  CP_REACH    reach 45510 Baetis tricaudatus complex     L insect Aquatic
## 33  CP_REACH    reach 45510 Baetis tricaudatus complex     L insect Aquatic
## 34  CP_REACH    reach 45510                 Glossosoma     L insect Aquatic
## 35  CP_REACH    reach 45510                 Glossosoma     L insect Aquatic
## 36 CZ2_REACH    reach 45504 Baetis tricaudatus complex     L insect Aquatic
## 37 CZ2_REACH    reach 45504 Baetis tricaudatus complex     L insect Aquatic
## 38 CZ2_REACH    reach 45504 Baetis tricaudatus complex     L insect Aquatic
## 39 CZ2_REACH    reach 45504     Calineuria californica     L insect Aquatic
## 40  DC_REACH    reach 45505                 Glossosoma     L insect Aquatic
## 41  DC_REACH    reach 45505                 Glossosoma     L insect Aquatic
## 42  DZ_REACH    reach 45502 Baetis tricaudatus complex     L insect Aquatic
##    Higher.classification         Order                                  Family
## 1    Arthropoda: Insecta Ephemeroptera                                Baetidae
## 2    Arthropoda: Insecta   Trichoptera                         Glossosomatidae
## 3    Arthropoda: Insecta   Trichoptera                         Glossosomatidae
## 4    Arthropoda: Insecta Ephemeroptera                           Heptageniidae
## 5    Arthropoda: Insecta       Diptera Chironomidae: Chironominae: Tanytarsini
## 6    Arthropoda: Insecta       Diptera Chironomidae: Chironominae: Tanytarsini
## 7    Arthropoda: Insecta Ephemeroptera                                Baetidae
## 8    Arthropoda: Insecta Ephemeroptera                                Baetidae
## 9    Arthropoda: Insecta Ephemeroptera                                Baetidae
## 10   Arthropoda: Insecta    Plecoptera                              Nemouridae
## 11   Arthropoda: Insecta   Trichoptera                         Glossosomatidae
## 12   Arthropoda: Insecta   Trichoptera                         Glossosomatidae
## 13   Arthropoda: Insecta   Trichoptera                         Glossosomatidae
## 14   Arthropoda: Insecta   Trichoptera                         Glossosomatidae
## 15   Arthropoda: Insecta   Trichoptera                         Glossosomatidae
## 16   Arthropoda: Insecta   Trichoptera                         Glossosomatidae
## 17   Arthropoda: Insecta   Trichoptera                         Glossosomatidae
## 18   Arthropoda: Insecta   Trichoptera                         Glossosomatidae
## 19   Arthropoda: Insecta Ephemeroptera                                Baetidae
## 20   Arthropoda: Insecta    Plecoptera                              Nemouridae
## 21   Arthropoda: Insecta    Coleoptera                                 Elmidae
## 22   Arthropoda: Insecta Ephemeroptera                                Baetidae
## 23   Arthropoda: Insecta Ephemeroptera                           Heptageniidae
## 24   Arthropoda: Insecta Ephemeroptera                           Heptageniidae
## 25   Arthropoda: Insecta   Trichoptera                         Glossosomatidae
## 26   Arthropoda: Insecta   Trichoptera                         Glossosomatidae
## 27   Arthropoda: Insecta       Diptera Chironomidae: Chironominae: Tanytarsini
## 28   Arthropoda: Insecta       Diptera Chironomidae: Chironominae: Tanytarsini
## 29   Arthropoda: Insecta Ephemeroptera                                Baetidae
## 30   Arthropoda: Insecta    Plecoptera                                Perlidae
## 31   Arthropoda: Insecta Ephemeroptera                           Heptageniidae
## 32   Arthropoda: Insecta Ephemeroptera                                Baetidae
## 33   Arthropoda: Insecta Ephemeroptera                                Baetidae
## 34   Arthropoda: Insecta   Trichoptera                         Glossosomatidae
## 35   Arthropoda: Insecta   Trichoptera                         Glossosomatidae
## 36   Arthropoda: Insecta Ephemeroptera                                Baetidae
## 37   Arthropoda: Insecta Ephemeroptera                                Baetidae
## 38   Arthropoda: Insecta Ephemeroptera                                Baetidae
## 39   Arthropoda: Insecta    Plecoptera                                Perlidae
## 40   Arthropoda: Insecta   Trichoptera                         Glossosomatidae
## 41   Arthropoda: Insecta   Trichoptera                         Glossosomatidae
## 42   Arthropoda: Insecta Ephemeroptera                                Baetidae
##       Common.name Length Abundance    Biomass Table3.BCG.Attribute
## 1        mayflies    1.5  160.8662  2.7352872                    4
## 2     caddisflies    1.5  238.2057  6.4810276                    3
## 3     caddisflies    1.5  238.2057  6.4810276                    3
## 4        mayflies    1.5  142.1244  4.6886491                    3
## 5          midges    3.0  115.1514  3.6742445                    4
## 6          midges    3.0  115.1514  3.6742445                    4
## 7        mayflies    1.5  185.7252  3.1579778                    4
## 8        mayflies    2.0  248.9508  9.6794489                    4
## 9        mayflies    2.5  138.3060 10.2139662                    4
## 10     stoneflies    2.0  165.9672  6.3045652                    4
## 11    caddisflies    1.5  159.8400  4.3488784                    3
## 12    caddisflies    1.5  159.8400  4.3488784                    3
## 13    caddisflies    1.0  111.6482  0.9155156                    3
## 14    caddisflies    1.0  111.6482  0.9155156                    3
## 15    caddisflies    1.5  451.6679 12.2888432                    3
## 16    caddisflies    1.5  451.6679 12.2888432                    3
## 17    caddisflies    2.0  380.6190 24.2521945                    3
## 18    caddisflies    2.0  380.6190 24.2521945                    3
## 19       mayflies    2.0  142.1244  5.5259347                    4
## 20     stoneflies    1.5  258.4080  4.4346366                    4
## 21 riffle beetles    3.0  114.5964 94.6810962                    4
## 22       mayflies    1.5  201.5316  3.4267419                    4
## 23       mayflies    1.5  438.6276 14.4702168                    3
## 24       mayflies    2.5  189.6768 25.5484616                    3
## 25    caddisflies    2.0  213.3864 13.5965059                    3
## 26    caddisflies    2.0  213.3864 13.5965059                    3
## 27         midges    5.0  187.3458 22.7573122                    4
## 28         midges    5.0  187.3458 22.7573122                    4
## 29       mayflies    2.0  125.9739  4.8979876                    4
## 30     stoneflies    2.5  151.8147 21.0193027                    2
## 31       mayflies    1.5  119.5137  3.9427276                    3
## 32       mayflies    2.0  462.3372 17.9761195                    4
## 33       mayflies    3.0  189.6768 23.6599428                    4
## 34    caddisflies    2.0  165.9672 10.5750602                    3
## 35    caddisflies    2.0  165.9672 10.5750602                    3
## 36       mayflies    1.5  197.5800  3.3595509                    4
## 37       mayflies    2.0  217.3380  8.4503126                    4
## 38       mayflies    2.5  162.0156 11.9649318                    4
## 39     stoneflies    3.0  134.3544 31.4426529                    2
## 40    caddisflies    1.5  111.3689  3.0300908                    3
## 41    caddisflies    1.5  111.3689  3.0300908                    3
## 42       mayflies    1.5  148.8640  2.5312085                    4
##    Feeding.Group Table3.CA.feeding.group Table3.Habit Table3.Tolerance
## 1             CG                      CG           CL                0
## 2             SC                      SC           CL                0
## 3             SC                      SC           CL                0
## 4             SC                      SC           CL                0
## 5             CG                      CG           CL                0
## 6             CG                      CG           CL                0
## 7             CG                      CG           CL                0
## 8             CG                      CG           CL                0
## 9             CG                      CG           CL                0
## 10           DSH                      SH           SP                0
## 11            SC                      SC           CL                0
## 12            SC                      SC           CL                0
## 13            SC                      SC           CL                0
## 14            SC                      SC           CL                0
## 15            SC                      SC           CL                0
## 16            SC                      SC           CL                0
## 17            SC                      SC           CL                0
## 18            SC                      SC           CL                0
## 19            CG                      CG           CL                0
## 20           DSH                      SH           SP                0
## 21            CG                      SC           CL               MT
## 22            CG                      CG           CL                0
## 23            SC                      SC           CL                0
## 24            SC                      SC           CL                0
## 25            SC                      SC           CL                0
## 26            SC                      SC           CL                0
## 27            CG                      CG           CL                0
## 28            CG                      CG           CL                0
## 29            CG                      CG           CL                0
## 30            PR                      PR           CL                0
## 31            SC                      SC           CL                0
## 32            CG                      CG           CL                0
## 33            CG                      CG           CL                0
## 34            SC                      SC           CL                0
## 35            SC                      SC           CL                0
## 36            CG                      CG           CL                0
## 37            CG                      CG           CL                0
## 38            CG                      CG           CL                0
## 39            PR                      PR           CL                0
## 40            SC                      SC           CL                0
## 41            SC                      SC           CL                0
## 42            CG                      CG           CL                0
##    Table3.WY.HBI Table3.PSSB.tolerance Table3.CA.tolerance
## 1              6                     0                   6
## 2              1                     0                   1
## 3              1                     0                   1
## 4              4                     0                   4
## 5              7                     0                   7
## 6              7                     0                   7
## 7              6                     0                   6
## 8              6                     0                   6
## 9              6                     0                   6
## 10             2                     0                   2
## 11             1                     0                   1
## 12             1                     0                   1
## 13             1                     0                   1
## 14             1                     0                   1
## 15             1                     0                   1
## 16             1                     0                   1
## 17             1                     0                   1
## 18             1                     0                   1
## 19             6                     0                   6
## 20             2                     0                   2
## 21             4                     0                   4
## 22             6                     0                   6
## 23             4                     0                   4
## 24             4                     0                   4
## 25             1                     0                   1
## 26             1                     0                   1
## 27             7                     0                   7
## 28             7                     0                   7
## 29             6                     0                   6
## 30             2                     0                   2
## 31             4                     0                   4
## 32             6                     0                   6
## 33             6                     0                   6
## 34             1                     0                   1
## 35             1                     0                   1
## 36             6                     0                   6
## 37             6                     0                   6
## 38             6                     0                   6
## 39             2                     0                   2
## 40             1                     0                   1
## 41             1                     0                   1
## 42             6                     0                   6
##    Table3.Metals.tolerance Table3.BCI.TV Table3.PSSB.long.lived
## 1                       11            72                      0
## 2                       11            24                      0
## 3                       11            24                      0
## 4                       11            21                      0
## 5                        1           108                      0
## 6                        1           108                      0
## 7                       11            72                      0
## 8                       11            72                      0
## 9                       11            72                      0
## 10                       3            16                      0
## 11                      11            24                      0
## 12                      11            24                      0
## 13                      11            24                      0
## 14                      11            24                      0
## 15                      11            24                      0
## 16                      11            24                      0
## 17                      11            24                      0
## 18                      11            24                      0
## 19                      11            72                      0
## 20                       3            16                      0
## 21                       3           104                      0
## 22                      11            72                      0
## 23                      11            21                      0
## 24                      11            21                      0
## 25                      11            24                      0
## 26                      11            24                      0
## 27                       1           108                      0
## 28                       1           108                      0
## 29                      11            72                      0
## 30                      11            24                      0
## 31                      11            21                      0
## 32                      11            72                      0
## 33                      11            72                      0
## 34                      11            24                      0
## 35                      11            24                      0
## 36                      11            72                      0
## 37                      11            72                      0
## 38                      11            72                      0
## 39                      11            24                      0
## 40                      11            24                      0
## 41                      11            24                      0
## 42                      11            72                      0
##    Table3.Voltinism Table3.Development Table3.Occurrence.in.drift
## 1                 3                  1                          3
## 2                 3                  3                          1
## 3                 3                  3                          1
## 4                 2                  1                          2
## 5                 3                  1                          3
## 6                 3                  1                          3
## 7                 3                  1                          3
## 8                 3                  1                          3
## 9                 3                  1                          3
## 10                2                  1                          1
## 11                3                  3                          1
## 12                3                  3                          1
## 13                3                  3                          1
## 14                3                  3                          1
## 15                3                  3                          1
## 16                3                  3                          1
## 17                3                  3                          1
## 18                3                  3                          1
## 19                3                  1                          3
## 20                2                  1                          1
## 21                1                  3                          2
## 22                3                  1                          3
## 23                2                  1                          2
## 24                2                  1                          2
## 25                3                  3                          1
## 26                3                  3                          1
## 27                3                  1                          3
## 28                3                  1                          3
## 29                3                  1                          3
## 30                1                  2                          1
## 31                2                  1                          2
## 32                3                  1                          3
## 33                3                  1                          3
## 34                3                  3                          1
## 35                3                  3                          1
## 36                3                  1                          3
## 37                3                  1                          3
## 38                3                  1                          3
## 39                1                  2                          1
## 40                3                  3                          1
## 41                3                  3                          1
## 42                3                  1                          3
##    Table3.Size.at.maturity Table3.Rheophily Table3.Thermal.preference Table3.a
## 1                        1                2                         2  0.00530
## 2                        2                3                         2  0.00820
## 3                        2                3                         2  0.00820
## 4                        2                3                         1  0.01080
## 5                        1                2                         2  0.00180
## 6                        1                2                         2  0.02100
## 7                        1                2                         2  0.00530
## 8                        1                2                         2  0.00530
## 9                        1                2                         2  0.00530
## 10                       2                2                         2  0.00560
## 11                       2                3                         2  0.00820
## 12                       2                3                         2  0.00820
## 13                       2                3                         2  0.00820
## 14                       2                3                         2  0.00820
## 15                       2                3                         2  0.00820
## 16                       2                3                         2  0.00820
## 17                       2                3                         2  0.00820
## 18                       2                3                         2  0.00820
## 19                       1                2                         2  0.00530
## 20                       2                2                         2  0.00560
## 21                       1                2                         2  0.00093
## 22                       1                2                         2  0.00530
## 23                       2                3                         1  0.01080
## 24                       2                3                         1  0.01080
## 25                       2                3                         2  0.00820
## 26                       2                3                         2  0.00820
## 27                       1                2                         2  0.00180
## 28                       1                2                         2  0.02100
## 29                       1                2                         2  0.00530
## 30                       3                3                         2  0.00990
## 31                       2                3                         1  0.01080
## 32                       1                2                         2  0.00530
## 33                       1                2                         2  0.00530
## 34                       2                3                         2  0.00820
## 35                       2                3                         2  0.00820
## 36                       1                2                         2  0.00530
## 37                       1                2                         2  0.00530
## 38                       1                2                         2  0.00530
## 39                       3                3                         2  0.00990
## 40                       2                3                         2  0.00820
## 41                       2                3                         2  0.00820
## 42                       1                2                         2  0.00530
##    Table3.b Biomass_log Abundance_log z_Abundance_log
## 1     2.875  1.00623644      5.080573        3.407488
## 2     2.958  1.86887908      5.473134        3.811348
## 3     2.958  1.86887908      5.473134        3.811348
## 4     2.754  1.54514449      4.956703        3.280054
## 5     2.617  1.30134753      4.746248        3.063542
## 6     2.081  1.30134753      4.746248        3.063542
## 7     2.875  1.14993189      5.224268        3.555319
## 8     2.875  2.27000497      5.517255        3.856738
## 9     2.875  2.32375602      4.929469        3.252036
## 10    2.762  1.84127401      5.111790        3.439604
## 11    2.958  1.46991798      5.074173        3.400905
## 12    2.958  1.46991798      5.074173        3.400905
## 13    2.958 -0.08826791      4.715353        3.031758
## 14    2.958 -0.08826791      4.715353        3.031758
## 15    2.958  2.50869180      6.112947        4.469573
## 16    2.958  2.50869180      6.112947        4.469573
## 17    2.958  3.18850711      5.941799        4.293499
## 18    2.958  3.18850711      5.941799        4.293499
## 19    2.875  1.70945242      4.956703        3.280054
## 20    2.762  1.48944568      5.554540        3.895096
## 21    6.180  4.55051436      4.741416        3.058572
## 22    2.875  1.23160992      5.305946        3.639348
## 23    2.754  2.67209252      6.083651        4.439434
## 24    2.754  3.24057710      5.245322        3.576979
## 25    2.958  2.60981284      5.363105        3.698151
## 26    2.958  2.60981284      5.363105        3.698151
## 27    2.617  3.12488651      5.232956        3.564257
## 28    2.081  3.12488651      5.232956        3.564257
## 29    2.875  1.58882443      4.836075        3.155954
## 30    2.879  3.04544119      5.022661        3.347910
## 31    2.754  1.37187277      4.783431        3.101795
## 32    2.875  2.88904418      6.136294        4.493592
## 33    2.875  3.16378344      5.245322        3.576979
## 34    2.958  2.35849841      5.111790        3.439604
## 35    2.958  2.35849841      5.111790        3.439604
## 36    2.875  1.21180729      5.286144        3.618975
## 37    2.875  2.13420343      5.381454        3.717028
## 38    2.875  2.48198002      5.087693        3.414813
## 39    2.879  3.44816534      4.900481        3.222214
## 40    2.958  1.10859260      4.712848        3.029181
## 41    2.958  1.10859260      4.712848        3.029181
## 42    2.875  0.92869686      5.003033        3.327717
#visualize abundance by feeding type per basin

ggplot(benthic2, aes(x = Basin, y = Abundance, fill = Feeding.Group)) +
geom_col() +
labs(title = "Abundance by Feeding Category and Basin", y = "Abundance", x = "Feeding Category") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

library(ggplot2)
library(dplyr)
library(scales)

# Calculate relative abundance and plot
ggplot(benthic, aes(x = Basin, y = Abundance, fill = Feeding.Group)) +
  geom_col(position = "fill") +  # Stacks bars and normalizes to 1
  scale_y_continuous(labels = percent) +  # Y-axis as percentages
  labs(title = "Relative Abundance of Benthic Feeding Groups by Basin",
       y = "Relative Abundance", 
       x = "Basin") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

# Calculate relative abundance for each Feeding.Group in each Basin
benthic_absummary <- benthic %>%
  group_by(Basin, Feeding.Group) %>%
  summarise(Abundance = sum(Abundance), .groups = "drop") %>%
  group_by(Basin) %>%
  mutate(Relative_Abundance = paste0(round((Abundance / sum(Abundance))*100, 0), "%")) %>%
  select(Basin, Feeding.Group, Relative_Abundance)

# Create a table with relative abundance in percentage format
benthic_absummary_table <- benthic_absummary 

print(benthic_absummary_table)
## # A tibble: 134 × 3
## # Groups:   Basin [20]
##    Basin     Feeding.Group Relative_Abundance
##    <chr>     <chr>         <chr>             
##  1 AA1_REACH CF            7%                
##  2 AA1_REACH CG            28%               
##  3 AA1_REACH DSH           21%               
##  4 AA1_REACH PA            2%                
##  5 AA1_REACH PR            33%               
##  6 AA1_REACH SC            9%                
##  7 AA2_REACH CF            7%                
##  8 AA2_REACH CG            40%               
##  9 AA2_REACH DSH           4%                
## 10 AA2_REACH PA            5%                
## # ℹ 124 more rows
# Aggregate by Basin and Feeding Category

benthic_aggregated2 <- benthic2 %>%
  group_by(Basin, Feeding.Group) %>% 
  summarise(
    total_abundance = sum(Abundance))
## `summarise()` has grouped output by 'Basin'. You can override using the
## `.groups` argument.
peri_benthic2 <- benthic_aggregated2 %>%
  left_join(twoyr_peri, by = "Basin")

#scatter plot of total macroinvertebrate abundance vs periphyton abundance by Basin
ggplot(peri_benthic2, aes(x = Total_Avg, y = total_abundance, color = Feeding.Group)) +
  geom_point() +
  geom_smooth(method = "lm") +
  labs(title = "Benthic Macroinvertebrate Abundance vs Periphyton Biomass by Basin",
       x = "Periphyton Biomass", y = "Total Benthic Abundance")
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 13 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning in qt((1 - level)/2, df): NaNs produced
## Warning: Removed 13 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning in max(ids, na.rm = TRUE): no non-missing arguments to max; returning
## -Inf

peri_benthic2 <- peri_benthic2 %>%
  mutate(
    total_abundance_log = log(total_abundance),
    Total_Avg_log = log(Total_Avg)
  )

peri_benthic2$total_abundance_log <- scale(peri_benthic2$total_abundance_log)
peri_benthic2$Total_Avg_log <- scale(peri_benthic2$Total_Avg_log)


# Model with Feeding.Group as a fixed effect and random effects for Basin
model_peri_benthic2 <- lmer(
  total_abundance_log ~ Total_Avg_log * Feeding.Group + (1 | Basin),
  data = peri_benthic2
)

summary(model_peri_benthic2)
## Linear mixed model fit by REML ['lmerMod']
## Formula: total_abundance_log ~ Total_Avg_log * Feeding.Group + (1 | Basin)
##    Data: peri_benthic2
## 
## REML criterion at convergence: 166.9
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -1.96268 -0.63459 -0.01771  0.53631  2.26551 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  Basin    (Intercept) 0.1517   0.3895  
##  Residual             0.1415   0.3761  
## Number of obs: 121, groups:  Basin, 18
## 
## Fixed effects:
##                                Estimate Std. Error t value
## (Intercept)                    -0.24323    0.12772  -1.904
## Total_Avg_log                   0.24144    0.12936   1.866
## Feeding.GroupCG                 1.28229    0.12548  10.219
## Feeding.GroupDSH                0.16715    0.12548   1.332
## Feeding.GroupHSH               -1.51255    0.29492  -5.129
## Feeding.GroupMH                -1.34619    0.20876  -6.449
## Feeding.GroupOM                -1.80991    0.20199  -8.961
## Feeding.GroupPA                -0.37831    0.12548  -3.015
## Feeding.GroupPR                 0.64566    0.12548   5.146
## Feeding.GroupSC                 0.91954    0.12548   7.328
## Total_Avg_log:Feeding.GroupCG   0.06344    0.12708   0.499
## Total_Avg_log:Feeding.GroupDSH -0.21790    0.12708  -1.715
## Total_Avg_log:Feeding.GroupHSH -0.06910    0.22464  -0.308
## Total_Avg_log:Feeding.GroupMH  -0.27534    0.21899  -1.257
## Total_Avg_log:Feeding.GroupOM  -0.32567    0.19273  -1.690
## Total_Avg_log:Feeding.GroupPA   0.14587    0.12708   1.148
## Total_Avg_log:Feeding.GroupPR  -0.05496    0.12708  -0.432
## Total_Avg_log:Feeding.GroupSC   0.17954    0.12708   1.413
## 
## Correlation matrix not shown by default, as p = 18 > 12.
## Use print(x, correlation=TRUE)  or
##     vcov(x)        if you need it
# Plot the relationship for each Feeding Group
ggplot(peri_benthic2, aes(x = Total_Avg_log, y = total_abundance_log, color = Feeding.Group)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +  # Linear regression line
  facet_wrap(~ Feeding.Group) +  # Create separate plots for each Feeding Group
  labs(title = "Relationship between Biennial Periphyton Biomass and Total Benthic Macro Abundance by Feeding Group", 
       x = "Total Average Periphyton Biomass", 
       y = "Benthic Abundance") +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 13 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 13 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggplot(peri_benthic2, aes(x = Total_Avg_log, y = total_abundance_log, color = Feeding.Group)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +  # Linear regression line
  facet_wrap(~ Basin) +  # Create separate plots for each Feeding Group
  labs(title = "Relationship between Biennial Periphyton Biomass and Total Benthic Macro Abundance by Basin", 
       x = "Total Average Periphyton Biomass", 
       y = "Benthic Abundance") +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 13 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Removed 13 rows containing missing values or values outside the scale range
## (`geom_point()`).

# Log-transform the biomasses if they aren't already

library(ggplot2)
library(dplyr)
library(dendextend)
## Warning: package 'dendextend' was built under R version 4.4.3
## Registered S3 method overwritten by 'dendextend':
##   method     from 
##   rev.hclust vegan
## 
## ---------------------
## Welcome to dendextend version 1.19.0
## Type citation('dendextend') for how to cite the package.
## 
## Type browseVignettes(package = 'dendextend') for the package vignette.
## The github page is: https://github.com/talgalili/dendextend/
## 
## Suggestions and bug-reports can be submitted at: https://github.com/talgalili/dendextend/issues
## You may ask questions at stackoverflow, use the r and dendextend tags: 
##   https://stackoverflow.com/questions/tagged/dendextend
## 
##  To suppress this message use:  suppressPackageStartupMessages(library(dendextend))
## ---------------------
## 
## Attaching package: 'dendextend'
## The following object is masked from 'package:permute':
## 
##     shuffle
## The following object is masked from 'package:ggpubr':
## 
##     rotate
## The following object is masked from 'package:stats':
## 
##     cutree
library(cluster)
library(factoextra)
## Warning: package 'factoextra' was built under R version 4.4.3
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
# Fish data - Add DA1/CA1 to line up

new_row <- data.frame(
  Basin = "DA1_REACH",
  total_biomass_24 = 0,
  Yearly_Avg_24 = 0,
  biomass_reach = 0
)
new_row2 <- data.frame(
  Basin = "CA1_REACH",
  total_biomass_24 = 0,
  Yearly_Avg_24 = 0,
  biomass_reach = 0
)

# Insert new_row2 at row 10
avg_biomass24_summarized2 <- bind_rows(
  avg_biomass24_summarized[1:9, ],  # Rows 1 to 9
  new_row2,                         # Insert new_row2 at row 10
  avg_biomass24_summarized[10:nrow(avg_biomass24_summarized), ] # Remaining rows
)

# Insert new_row at row 14 (after the shift)
avg_biomass24_summarized2 <- bind_rows(
  avg_biomass24_summarized2[1:13, ],  # Rows 1 to 13 (now shifted)
  new_row,                           # Insert new_row at row 14
  avg_biomass24_summarized2[14:nrow(avg_biomass24_summarized2), ] # Remaining rows
)

# Reset row indices
rownames(avg_biomass24_summarized2) <- NULL


fish_long <- avg_biomass24_summarized2 %>%
  mutate(
    Category = "Fish",
    Biomass = sqrt(biomass_reach +1), #sqrt transform
    Biomass = ((Biomass - min(Biomass, na.rm = TRUE)) /  # Min-max scaling
              (max(Biomass, na.rm = TRUE) - min(Biomass, na.rm = TRUE))) + 1 )

# Periphyton data
peri_long <- twoyr_peri %>%
  mutate(
    Category = "Periphyton",
    Biomass = sqrt(Total_Avg+1),  # sqrt transformation
    Biomass = ((Biomass - min(Biomass, na.rm = TRUE)) /  # Min-max scaling
              (max(Biomass, na.rm = TRUE) - min(Biomass, na.rm = TRUE)))+1)

# Drift data
drift_long <- macros %>%
  group_by(Basin) %>%  # Group by Basin first!
  summarize(
    Biomass = sum(Biomass, na.rm = TRUE)  # Summing original biomass first
  ) %>%
  ungroup() %>%
  mutate(
    Biomass = sqrt(Biomass+1), 
    Biomass = ((Biomass - min(Biomass, na.rm = TRUE)) /  # Min-max scaling
              (max(Biomass, na.rm = TRUE) - min(Biomass, na.rm = TRUE))
  ) +1,
Category = "Drift"  # Assign category
)
# Benthic data
benthic_long <- benthic %>%
  group_by(Basin) %>%  # Group by Basin first!
  summarize(
    Biomass = sum(Biomass, na.rm = TRUE)  # Summing original biomass first
  ) %>%
  ungroup() %>%
  mutate(
    Biomass = sqrt(Biomass+1),
    Biomass = ((Biomass - min(Biomass, na.rm = TRUE)) /  # Min-max scaling
              (max(Biomass, na.rm = TRUE) - min(Biomass, na.rm = TRUE))
  ) +1,
Category = "Benthic"  # Assign category
)

# Bind rows to create a single long-format dataset
all_data <- bind_rows(fish_long, peri_long, drift_long, benthic_long) %>%
  group_by(Basin, Category)
all_data <- all_data %>%
  select(-X, -X.1, -X.2)  # Remove empty columns

# Create the bar plot
ggplot(all_data, aes(x = Basin, y = Biomass, fill = Category)) +  
  geom_col(position = "dodge", alpha = 0.8) +  # Creates side-by-side bars for each biomass type
  labs(
    title = "Biomass Comparison Across Basins",
    x = "Basin",
    y = "Biomass",
    fill = "Biomass Type"
  ) +
  scale_fill_manual(values = c("darkblue", "forestgreen", "lightblue3", "orange")) +  # Adjust colors
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))  # Rotate x-axis labels for readability

ggplot(all_data, aes(x = Basin, y = Biomass, color = Category, group = Category)) +  
  geom_line(size = 1) +  # Connects data points with lines
  geom_point(size = 3, alpha = 0.8) +  # Adds points for clarity
  labs(
    title = "Biomass Trends Across Basins",
    x = "Basin",
    y = "Biomass",
    color = "Biomass Type"
  ) +
  scale_color_manual(values = c("darkblue", "forestgreen", "lightblue3", "orange")) +  # Adjust colors
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))  # Rotate x-axis labels for readability
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

ggplot(all_data, aes(x = Basin, y = Biomass, fill = Category)) +  
  geom_col(position = "dodge", alpha = 0.8) +  # Creates side-by-side bars for each biomass type
  facet_wrap(~Category, scales = "free_y") +  # Creates a facet for each biomass type with its own y-axis
  labs(
    title = "Biomass Comparison Across Basins",
    x = "Basin",
    y = "Biomass",
    fill = "Biomass Type"
  ) +
  scale_fill_manual(values = c("darkblue", "forestgreen", "lightblue3", "orange")) +  # Adjust colors
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))  # Rotate x-axis labels for readability

ggplot(all_data, aes(x = Basin, y = Biomass, color = Category, group = Category)) +  
  geom_line(size = 1) +  # Connects data points with lines
  geom_point(size = 3, alpha = 0.8) +  # Adds points for clarity
  facet_wrap(~Category, scales = "free_y") +  # Creates a facet for each biomass type with its own y-axis
  labs(
    title = "Biomass Trends Across Basins",
    x = "Basin",
    y = "Biomass",
    color = "Biomass Type"
  ) +
  scale_color_manual(values = c("darkblue", "forestgreen", "lightblue3", "orange")) +  # Adjust colors
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))  # Rotate x-axis labels for readability

# Reshape the data to wide format for clustering
for_clust <- all_data %>%
  group_by(Basin, Category) %>%  # Group by Basin and Category
  summarize(Biomass = mean(Biomass, na.rm = TRUE)) %>%  # Calculate the mean Biomass per group
  pivot_wider(names_from = Category, values_from = Biomass) %>%  # Pivot to wide format
  drop_na()  # Drop rows with NA values
## `summarise()` has grouped output by 'Basin'. You can override using the
## `.groups` argument.
# Ensure all columns except Basin are numeric for clustering
for_clust_numeric <- for_clust %>%
  ungroup() %>%
  select(-Basin) %>%  # Remove Basin column
  mutate(across(everything(), as.numeric))  # Convert all columns to numeric

# Remove rows with any NA values
for_clust_numeric <- na.omit(for_clust_numeric)

# Calculate the distance matrix (Euclidean distance)
dist_matrix <- dist(for_clust_numeric, method = "euclidean")

# Perform hierarchical clustering (Ward's method)
final_clust <- hclust(dist_matrix, method = "ward.D2")

# Determine the optimal number of clusters using the Gap Statistic
gap_stat <- clusGap(for_clust_numeric, FUN = hcut, nstart = 25, K.max = 10, B = 50)
fviz_gap_stat(gap_stat)  # Visualize Gap Statistic to determine the optimal number of clusters

# Cut the dendrogram into the desired number of clusters (4 in this case)
groups <- cutree(final_clust, k = 4)

# Add cluster labels to the original data, preserving 'Basin'
final_data <- cbind(for_clust, cluster = groups)

# View the table of cluster assignments
table(final_data$cluster)
## 
## 1 2 3 4 
## 6 8 1 3
# Print the final data with Basin and cluster assignments
print(final_data)
## # A tibble: 18 × 6
## # Groups:   Basin [18]
##    Basin     Benthic Drift  Fish Periphyton cluster
##    <chr>       <dbl> <dbl> <dbl>      <dbl>   <int>
##  1 AA1_REACH    1.30  1.31  1.22       1.24       1
##  2 AA2_REACH    1.17  1.08  1.60       1.51       2
##  3 AC_REACH     1.16  1.18  2          1.88       2
##  4 AP_REACH     1.04  1.14  1.28       1          1
##  5 AZ_REACH     1.21  1.46  1.75       1.85       2
##  6 BA1_REACH    1.25  1.24  1.60       1.50       2
##  7 BA2_REACH    1.26  1.07  1.78       1.45       2
##  8 BC_REACH     1.24  1.08  1.73       1.48       2
##  9 BZ_REACH     1.28  1.02  1.56       1.89       2
## 10 CA1_REACH    1     1     1          1.30       1
## 11 CA2_REACH    1.28  1.24  1.77       1.54       2
## 12 CP_REACH     1.46  1.90  1.83       2          3
## 13 CZ2_REACH    1.32  1.53  1.36       1.57       1
## 14 DA1_REACH    1.91  2     1          1.16       4
## 15 DA2_REACH    1.82  1.87  1.56       1.07       4
## 16 DC_REACH     1.27  1.31  1.38       1.26       1
## 17 DP_REACH     1.08  1.12  1.19       1.04       1
## 18 DZ_REACH     2     1.42  1.24       1.38       4
# Plot the hierarchical clustering dendrogram
plot(final_clust, labels = final_data$Basin, main = "Trophic Biomass Dendrogram by Basin")
rect.hclust(final_clust, k = 4, border = 2:5)  # Adds rectangles around the 4 clusters

# Create a heatmap of the clustered data
cluster_matrix <- final_data %>%
  ungroup()%>%
  select(-Basin, -cluster) %>%  # Remove Basin and cluster columns for heatmap
  as.data.frame()

# Ensure all columns are numeric
cluster_matrix <- cluster_matrix %>%
  mutate(across(everything(), as.numeric)) 

# Calculate the distance matrix for the heatmap
dist_matrix <- dist(cluster_matrix, method = "euclidean")

#clustering
final_clust_heatmap <- hclust(dist_matrix, method = "ward.D2")

# Create a heatmap of the clustering result
pheatmap(cluster_matrix, 
         clustering_method = "ward.D2", 
         cluster_rows = final_clust_heatmap, 
         cluster_cols = TRUE, 
         show_rownames = TRUE, 
         show_colnames = TRUE, 
         main = "Heatmap of Trophic Biomass Clusters",
         labels_row = final_data$Basin)

library(factoextra)

#visualize environmental variables for distribution - transformation needs

# Identify numeric columns
num_cols <- names(env)[sapply(env, is.numeric)]

# Exclude 'Basin' if it's numeric (e.g., an ID or name)
num_cols <- setdiff(num_cols, "Basin")

# Loop through and plot histograms
for (col in num_cols) {
  print(
    ggplot(env, aes_string(x = col)) +
      geom_histogram(bins = 30, fill = "lightblue", color = "black") +
      theme_minimal() +
      ggtitle(paste("Histogram of", col))
  )
}
## Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
## ℹ Please use tidy evaluation idioms with `aes()`.
## ℹ See also `vignette("ggplot2-in-packages")` for more information.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

# Squareroot transformation to handle true zeroes and min-max scale all numeric columns except 'Basin'

env_scaled <- env %>%
  mutate(across(
    -Basin,  # Exclude Basin column
    ~ sqrt(. + 1),  # Apply square root transformation (adding 1 to handle zeros)
    .names = "{.col}"  # Keep the original column names after the transformation
  )) %>%
  mutate(across(
    -Basin,  # Exclude Basin column
    ~ (. - min(.)) / (max(.) - min(.)),  # Apply min-max scaling
    .names = "{.col}"  # Keep the original column names after scaling
  ))

#Principal component analysis of environmental variables to determine if model reduction is needed/multicollinearity is present

pca_env <- env_scaled[,-c(1)]
pca_matrix <- cor(pca_env)
pca_result <- prcomp(pca_matrix)
summary(pca_result)
## Importance of components:
##                           PC1    PC2     PC3     PC4     PC5     PC6     PC7
## Standard deviation     1.4041 0.8590 0.53872 0.36318 0.24235 0.14574 0.11777
## Proportion of Variance 0.6094 0.2281 0.08971 0.04077 0.01816 0.00657 0.00429
## Cumulative Proportion  0.6094 0.8375 0.92718 0.96795 0.98611 0.99268 0.99696
##                            PC8     PC9   PC10    PC11    PC12     PC13     PC14
## Standard deviation     0.08057 0.05367 0.0178 0.01096 0.00413 0.001216 5.28e-05
## Proportion of Variance 0.00201 0.00089 0.0001 0.00004 0.00001 0.000000 0.00e+00
## Cumulative Proportion  0.99897 0.99986 1.0000 0.99999 1.00000 1.000000 1.00e+00
##                             PC15
## Standard deviation     1.466e-17
## Proportion of Variance 0.000e+00
## Cumulative Proportion  1.000e+00
pca_result$rotation[,1:2]
##                            PC1         PC2
## Avg_Flow           -0.31834065 -0.12029552
## Flow_Variance      -0.30652574 -0.11814831
## Avg_Sand            0.17357840  0.30709036
## Avg_Gravel         -0.13149148  0.44400045
## Avg_Cobble         -0.15650163 -0.27803500
## Avg_Boulder         0.10379102 -0.46689277
## Avg_Bedrock         0.16274930 -0.24502044
## Avg_Organic         0.27931778  0.15600948
## Mean_Slope_Degrees  0.24427454 -0.08039395
## Dist_Pour          -0.31627013  0.20488476
## Dist_Head          -0.36068004 -0.15179631
## Total_length       -0.41021991 -0.02653216
## Reach_length        0.06034157 -0.29978199
## Relative_position   0.04853392 -0.37047483
## Percent_occupied    0.39172270 -0.03398395
fviz_eig(pca_result, addlabels = TRUE)

fviz_cos2(pca_result, choice = "var", axes = 1:2, title = "Combined contribution by variable")

fviz_pca_var(pca_result, col.var = "cos2", gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"), repel = TRUE, title = "PCA Biplot of Scaled Environmental Predictor Variables")

#Reach length can be removed. Only collinear variables are Flow Variance, Avg_Flow and Distance to Headwaters. Opting to remove Flow Variance since Distance to Headwaters significantly different.
# Prep data
merged <- merge(for_clust, env_scaled, by = "Basin")

# Split data ONCE and reuse
set.seed(123)
split_ind <- sample(1:2, size = nrow(merged), replace = TRUE)

# Train control parameters
custom <- trainControl(method = "repeatedcv",
                       number = 5,
                       repeats = 5,
                       verboseIter = TRUE)

### --- FISH BIOMASS MODEL --- ###
fishnet <- merged[,-c(1:3,5)]
train <- fishnet[split_ind == 1,]
test <- fishnet[split_ind == 2,]

set.seed(1234)
en_f <- train(Fish ~ .,
              train,
              method = 'glmnet',
              tuneGrid = expand.grid(alpha = seq(0, 1, length = 10),
                                     lambda = seq(0.0001, 0.2, length = 5)),
              trControl = custom)
## + Fold1.Rep1: alpha=0.0000, lambda=0.2 
## - Fold1.Rep1: alpha=0.0000, lambda=0.2 
## + Fold1.Rep1: alpha=0.1111, lambda=0.2 
## - Fold1.Rep1: alpha=0.1111, lambda=0.2 
## + Fold1.Rep1: alpha=0.2222, lambda=0.2 
## - Fold1.Rep1: alpha=0.2222, lambda=0.2 
## + Fold1.Rep1: alpha=0.3333, lambda=0.2 
## - Fold1.Rep1: alpha=0.3333, lambda=0.2 
## + Fold1.Rep1: alpha=0.4444, lambda=0.2 
## - Fold1.Rep1: alpha=0.4444, lambda=0.2 
## + Fold1.Rep1: alpha=0.5556, lambda=0.2 
## - Fold1.Rep1: alpha=0.5556, lambda=0.2 
## + Fold1.Rep1: alpha=0.6667, lambda=0.2 
## - Fold1.Rep1: alpha=0.6667, lambda=0.2 
## + Fold1.Rep1: alpha=0.7778, lambda=0.2 
## - Fold1.Rep1: alpha=0.7778, lambda=0.2 
## + Fold1.Rep1: alpha=0.8889, lambda=0.2 
## - Fold1.Rep1: alpha=0.8889, lambda=0.2 
## + Fold1.Rep1: alpha=1.0000, lambda=0.2 
## - Fold1.Rep1: alpha=1.0000, lambda=0.2 
## + Fold2.Rep1: alpha=0.0000, lambda=0.2 
## - Fold2.Rep1: alpha=0.0000, lambda=0.2 
## + Fold2.Rep1: alpha=0.1111, lambda=0.2 
## - Fold2.Rep1: alpha=0.1111, lambda=0.2 
## + Fold2.Rep1: alpha=0.2222, lambda=0.2 
## - Fold2.Rep1: alpha=0.2222, lambda=0.2 
## + Fold2.Rep1: alpha=0.3333, lambda=0.2 
## - Fold2.Rep1: alpha=0.3333, lambda=0.2 
## + Fold2.Rep1: alpha=0.4444, lambda=0.2 
## - Fold2.Rep1: alpha=0.4444, lambda=0.2 
## + Fold2.Rep1: alpha=0.5556, lambda=0.2 
## - Fold2.Rep1: alpha=0.5556, lambda=0.2 
## + Fold2.Rep1: alpha=0.6667, lambda=0.2 
## - Fold2.Rep1: alpha=0.6667, lambda=0.2 
## + Fold2.Rep1: alpha=0.7778, lambda=0.2 
## - Fold2.Rep1: alpha=0.7778, lambda=0.2 
## + Fold2.Rep1: alpha=0.8889, lambda=0.2 
## - Fold2.Rep1: alpha=0.8889, lambda=0.2 
## + Fold2.Rep1: alpha=1.0000, lambda=0.2 
## - Fold2.Rep1: alpha=1.0000, lambda=0.2 
## + Fold3.Rep1: alpha=0.0000, lambda=0.2 
## - Fold3.Rep1: alpha=0.0000, lambda=0.2 
## + Fold3.Rep1: alpha=0.1111, lambda=0.2 
## - Fold3.Rep1: alpha=0.1111, lambda=0.2 
## + Fold3.Rep1: alpha=0.2222, lambda=0.2 
## - Fold3.Rep1: alpha=0.2222, lambda=0.2 
## + Fold3.Rep1: alpha=0.3333, lambda=0.2 
## - Fold3.Rep1: alpha=0.3333, lambda=0.2 
## + Fold3.Rep1: alpha=0.4444, lambda=0.2 
## - Fold3.Rep1: alpha=0.4444, lambda=0.2 
## + Fold3.Rep1: alpha=0.5556, lambda=0.2 
## - Fold3.Rep1: alpha=0.5556, lambda=0.2 
## + Fold3.Rep1: alpha=0.6667, lambda=0.2 
## - Fold3.Rep1: alpha=0.6667, lambda=0.2 
## + Fold3.Rep1: alpha=0.7778, lambda=0.2 
## - Fold3.Rep1: alpha=0.7778, lambda=0.2 
## + Fold3.Rep1: alpha=0.8889, lambda=0.2 
## - Fold3.Rep1: alpha=0.8889, lambda=0.2 
## + Fold3.Rep1: alpha=1.0000, lambda=0.2 
## - Fold3.Rep1: alpha=1.0000, lambda=0.2 
## + Fold4.Rep1: alpha=0.0000, lambda=0.2 
## - Fold4.Rep1: alpha=0.0000, lambda=0.2 
## + Fold4.Rep1: alpha=0.1111, lambda=0.2 
## - Fold4.Rep1: alpha=0.1111, lambda=0.2 
## + Fold4.Rep1: alpha=0.2222, lambda=0.2 
## - Fold4.Rep1: alpha=0.2222, lambda=0.2 
## + Fold4.Rep1: alpha=0.3333, lambda=0.2 
## - Fold4.Rep1: alpha=0.3333, lambda=0.2 
## + Fold4.Rep1: alpha=0.4444, lambda=0.2 
## - Fold4.Rep1: alpha=0.4444, lambda=0.2 
## + Fold4.Rep1: alpha=0.5556, lambda=0.2 
## - Fold4.Rep1: alpha=0.5556, lambda=0.2 
## + Fold4.Rep1: alpha=0.6667, lambda=0.2 
## - Fold4.Rep1: alpha=0.6667, lambda=0.2 
## + Fold4.Rep1: alpha=0.7778, lambda=0.2 
## - Fold4.Rep1: alpha=0.7778, lambda=0.2 
## + Fold4.Rep1: alpha=0.8889, lambda=0.2 
## - Fold4.Rep1: alpha=0.8889, lambda=0.2 
## + Fold4.Rep1: alpha=1.0000, lambda=0.2 
## - Fold4.Rep1: alpha=1.0000, lambda=0.2 
## + Fold5.Rep1: alpha=0.0000, lambda=0.2 
## - Fold5.Rep1: alpha=0.0000, lambda=0.2 
## + Fold5.Rep1: alpha=0.1111, lambda=0.2 
## - Fold5.Rep1: alpha=0.1111, lambda=0.2 
## + Fold5.Rep1: alpha=0.2222, lambda=0.2 
## - Fold5.Rep1: alpha=0.2222, lambda=0.2 
## + Fold5.Rep1: alpha=0.3333, lambda=0.2 
## - Fold5.Rep1: alpha=0.3333, lambda=0.2 
## + Fold5.Rep1: alpha=0.4444, lambda=0.2 
## - Fold5.Rep1: alpha=0.4444, lambda=0.2 
## + Fold5.Rep1: alpha=0.5556, lambda=0.2 
## - Fold5.Rep1: alpha=0.5556, lambda=0.2 
## + Fold5.Rep1: alpha=0.6667, lambda=0.2 
## - Fold5.Rep1: alpha=0.6667, lambda=0.2 
## + Fold5.Rep1: alpha=0.7778, lambda=0.2 
## - Fold5.Rep1: alpha=0.7778, lambda=0.2 
## + Fold5.Rep1: alpha=0.8889, lambda=0.2 
## - Fold5.Rep1: alpha=0.8889, lambda=0.2 
## + Fold5.Rep1: alpha=1.0000, lambda=0.2 
## - Fold5.Rep1: alpha=1.0000, lambda=0.2 
## + Fold1.Rep2: alpha=0.0000, lambda=0.2 
## - Fold1.Rep2: alpha=0.0000, lambda=0.2 
## + Fold1.Rep2: alpha=0.1111, lambda=0.2 
## - Fold1.Rep2: alpha=0.1111, lambda=0.2 
## + Fold1.Rep2: alpha=0.2222, lambda=0.2 
## - Fold1.Rep2: alpha=0.2222, lambda=0.2 
## + Fold1.Rep2: alpha=0.3333, lambda=0.2 
## - Fold1.Rep2: alpha=0.3333, lambda=0.2 
## + Fold1.Rep2: alpha=0.4444, lambda=0.2 
## - Fold1.Rep2: alpha=0.4444, lambda=0.2 
## + Fold1.Rep2: alpha=0.5556, lambda=0.2 
## - Fold1.Rep2: alpha=0.5556, lambda=0.2 
## + Fold1.Rep2: alpha=0.6667, lambda=0.2 
## - Fold1.Rep2: alpha=0.6667, lambda=0.2 
## + Fold1.Rep2: alpha=0.7778, lambda=0.2 
## - Fold1.Rep2: alpha=0.7778, lambda=0.2 
## + Fold1.Rep2: alpha=0.8889, lambda=0.2 
## - Fold1.Rep2: alpha=0.8889, lambda=0.2 
## + Fold1.Rep2: alpha=1.0000, lambda=0.2 
## - Fold1.Rep2: alpha=1.0000, lambda=0.2 
## + Fold2.Rep2: alpha=0.0000, lambda=0.2 
## - Fold2.Rep2: alpha=0.0000, lambda=0.2 
## + Fold2.Rep2: alpha=0.1111, lambda=0.2 
## - Fold2.Rep2: alpha=0.1111, lambda=0.2 
## + Fold2.Rep2: alpha=0.2222, lambda=0.2 
## - Fold2.Rep2: alpha=0.2222, lambda=0.2 
## + Fold2.Rep2: alpha=0.3333, lambda=0.2 
## - Fold2.Rep2: alpha=0.3333, lambda=0.2 
## + Fold2.Rep2: alpha=0.4444, lambda=0.2 
## - Fold2.Rep2: alpha=0.4444, lambda=0.2 
## + Fold2.Rep2: alpha=0.5556, lambda=0.2 
## - Fold2.Rep2: alpha=0.5556, lambda=0.2 
## + Fold2.Rep2: alpha=0.6667, lambda=0.2 
## - Fold2.Rep2: alpha=0.6667, lambda=0.2 
## + Fold2.Rep2: alpha=0.7778, lambda=0.2 
## - Fold2.Rep2: alpha=0.7778, lambda=0.2 
## + Fold2.Rep2: alpha=0.8889, lambda=0.2 
## - Fold2.Rep2: alpha=0.8889, lambda=0.2 
## + Fold2.Rep2: alpha=1.0000, lambda=0.2 
## - Fold2.Rep2: alpha=1.0000, lambda=0.2 
## + Fold3.Rep2: alpha=0.0000, lambda=0.2 
## - Fold3.Rep2: alpha=0.0000, lambda=0.2 
## + Fold3.Rep2: alpha=0.1111, lambda=0.2 
## - Fold3.Rep2: alpha=0.1111, lambda=0.2 
## + Fold3.Rep2: alpha=0.2222, lambda=0.2 
## - Fold3.Rep2: alpha=0.2222, lambda=0.2 
## + Fold3.Rep2: alpha=0.3333, lambda=0.2 
## - Fold3.Rep2: alpha=0.3333, lambda=0.2 
## + Fold3.Rep2: alpha=0.4444, lambda=0.2 
## - Fold3.Rep2: alpha=0.4444, lambda=0.2 
## + Fold3.Rep2: alpha=0.5556, lambda=0.2 
## - Fold3.Rep2: alpha=0.5556, lambda=0.2 
## + Fold3.Rep2: alpha=0.6667, lambda=0.2 
## - Fold3.Rep2: alpha=0.6667, lambda=0.2 
## + Fold3.Rep2: alpha=0.7778, lambda=0.2 
## - Fold3.Rep2: alpha=0.7778, lambda=0.2 
## + Fold3.Rep2: alpha=0.8889, lambda=0.2 
## - Fold3.Rep2: alpha=0.8889, lambda=0.2 
## + Fold3.Rep2: alpha=1.0000, lambda=0.2 
## - Fold3.Rep2: alpha=1.0000, lambda=0.2 
## + Fold4.Rep2: alpha=0.0000, lambda=0.2 
## - Fold4.Rep2: alpha=0.0000, lambda=0.2 
## + Fold4.Rep2: alpha=0.1111, lambda=0.2 
## - Fold4.Rep2: alpha=0.1111, lambda=0.2 
## + Fold4.Rep2: alpha=0.2222, lambda=0.2 
## - Fold4.Rep2: alpha=0.2222, lambda=0.2 
## + Fold4.Rep2: alpha=0.3333, lambda=0.2 
## - Fold4.Rep2: alpha=0.3333, lambda=0.2 
## + Fold4.Rep2: alpha=0.4444, lambda=0.2 
## - Fold4.Rep2: alpha=0.4444, lambda=0.2 
## + Fold4.Rep2: alpha=0.5556, lambda=0.2 
## - Fold4.Rep2: alpha=0.5556, lambda=0.2 
## + Fold4.Rep2: alpha=0.6667, lambda=0.2 
## - Fold4.Rep2: alpha=0.6667, lambda=0.2 
## + Fold4.Rep2: alpha=0.7778, lambda=0.2 
## - Fold4.Rep2: alpha=0.7778, lambda=0.2 
## + Fold4.Rep2: alpha=0.8889, lambda=0.2 
## - Fold4.Rep2: alpha=0.8889, lambda=0.2 
## + Fold4.Rep2: alpha=1.0000, lambda=0.2 
## - Fold4.Rep2: alpha=1.0000, lambda=0.2 
## + Fold5.Rep2: alpha=0.0000, lambda=0.2 
## - Fold5.Rep2: alpha=0.0000, lambda=0.2 
## + Fold5.Rep2: alpha=0.1111, lambda=0.2 
## - Fold5.Rep2: alpha=0.1111, lambda=0.2 
## + Fold5.Rep2: alpha=0.2222, lambda=0.2 
## - Fold5.Rep2: alpha=0.2222, lambda=0.2 
## + Fold5.Rep2: alpha=0.3333, lambda=0.2 
## - Fold5.Rep2: alpha=0.3333, lambda=0.2 
## + Fold5.Rep2: alpha=0.4444, lambda=0.2 
## - Fold5.Rep2: alpha=0.4444, lambda=0.2 
## + Fold5.Rep2: alpha=0.5556, lambda=0.2 
## - Fold5.Rep2: alpha=0.5556, lambda=0.2 
## + Fold5.Rep2: alpha=0.6667, lambda=0.2 
## - Fold5.Rep2: alpha=0.6667, lambda=0.2 
## + Fold5.Rep2: alpha=0.7778, lambda=0.2 
## - Fold5.Rep2: alpha=0.7778, lambda=0.2 
## + Fold5.Rep2: alpha=0.8889, lambda=0.2 
## - Fold5.Rep2: alpha=0.8889, lambda=0.2 
## + Fold5.Rep2: alpha=1.0000, lambda=0.2 
## - Fold5.Rep2: alpha=1.0000, lambda=0.2 
## + Fold1.Rep3: alpha=0.0000, lambda=0.2 
## - Fold1.Rep3: alpha=0.0000, lambda=0.2 
## + Fold1.Rep3: alpha=0.1111, lambda=0.2 
## - Fold1.Rep3: alpha=0.1111, lambda=0.2 
## + Fold1.Rep3: alpha=0.2222, lambda=0.2 
## - Fold1.Rep3: alpha=0.2222, lambda=0.2 
## + Fold1.Rep3: alpha=0.3333, lambda=0.2 
## - Fold1.Rep3: alpha=0.3333, lambda=0.2 
## + Fold1.Rep3: alpha=0.4444, lambda=0.2 
## - Fold1.Rep3: alpha=0.4444, lambda=0.2 
## + Fold1.Rep3: alpha=0.5556, lambda=0.2 
## - Fold1.Rep3: alpha=0.5556, lambda=0.2 
## + Fold1.Rep3: alpha=0.6667, lambda=0.2 
## - Fold1.Rep3: alpha=0.6667, lambda=0.2 
## + Fold1.Rep3: alpha=0.7778, lambda=0.2 
## - Fold1.Rep3: alpha=0.7778, lambda=0.2 
## + Fold1.Rep3: alpha=0.8889, lambda=0.2 
## - Fold1.Rep3: alpha=0.8889, lambda=0.2 
## + Fold1.Rep3: alpha=1.0000, lambda=0.2 
## - Fold1.Rep3: alpha=1.0000, lambda=0.2 
## + Fold2.Rep3: alpha=0.0000, lambda=0.2 
## - Fold2.Rep3: alpha=0.0000, lambda=0.2 
## + Fold2.Rep3: alpha=0.1111, lambda=0.2 
## - Fold2.Rep3: alpha=0.1111, lambda=0.2 
## + Fold2.Rep3: alpha=0.2222, lambda=0.2 
## - Fold2.Rep3: alpha=0.2222, lambda=0.2 
## + Fold2.Rep3: alpha=0.3333, lambda=0.2 
## - Fold2.Rep3: alpha=0.3333, lambda=0.2 
## + Fold2.Rep3: alpha=0.4444, lambda=0.2 
## - Fold2.Rep3: alpha=0.4444, lambda=0.2 
## + Fold2.Rep3: alpha=0.5556, lambda=0.2 
## - Fold2.Rep3: alpha=0.5556, lambda=0.2 
## + Fold2.Rep3: alpha=0.6667, lambda=0.2 
## - Fold2.Rep3: alpha=0.6667, lambda=0.2 
## + Fold2.Rep3: alpha=0.7778, lambda=0.2 
## - Fold2.Rep3: alpha=0.7778, lambda=0.2 
## + Fold2.Rep3: alpha=0.8889, lambda=0.2 
## - Fold2.Rep3: alpha=0.8889, lambda=0.2 
## + Fold2.Rep3: alpha=1.0000, lambda=0.2 
## - Fold2.Rep3: alpha=1.0000, lambda=0.2 
## + Fold3.Rep3: alpha=0.0000, lambda=0.2 
## - Fold3.Rep3: alpha=0.0000, lambda=0.2 
## + Fold3.Rep3: alpha=0.1111, lambda=0.2 
## - Fold3.Rep3: alpha=0.1111, lambda=0.2 
## + Fold3.Rep3: alpha=0.2222, lambda=0.2 
## - Fold3.Rep3: alpha=0.2222, lambda=0.2 
## + Fold3.Rep3: alpha=0.3333, lambda=0.2 
## - Fold3.Rep3: alpha=0.3333, lambda=0.2 
## + Fold3.Rep3: alpha=0.4444, lambda=0.2 
## - Fold3.Rep3: alpha=0.4444, lambda=0.2 
## + Fold3.Rep3: alpha=0.5556, lambda=0.2 
## - Fold3.Rep3: alpha=0.5556, lambda=0.2 
## + Fold3.Rep3: alpha=0.6667, lambda=0.2 
## - Fold3.Rep3: alpha=0.6667, lambda=0.2 
## + Fold3.Rep3: alpha=0.7778, lambda=0.2 
## - Fold3.Rep3: alpha=0.7778, lambda=0.2 
## + Fold3.Rep3: alpha=0.8889, lambda=0.2 
## - Fold3.Rep3: alpha=0.8889, lambda=0.2 
## + Fold3.Rep3: alpha=1.0000, lambda=0.2 
## - Fold3.Rep3: alpha=1.0000, lambda=0.2 
## + Fold4.Rep3: alpha=0.0000, lambda=0.2 
## - Fold4.Rep3: alpha=0.0000, lambda=0.2 
## + Fold4.Rep3: alpha=0.1111, lambda=0.2 
## - Fold4.Rep3: alpha=0.1111, lambda=0.2 
## + Fold4.Rep3: alpha=0.2222, lambda=0.2 
## - Fold4.Rep3: alpha=0.2222, lambda=0.2 
## + Fold4.Rep3: alpha=0.3333, lambda=0.2 
## - Fold4.Rep3: alpha=0.3333, lambda=0.2 
## + Fold4.Rep3: alpha=0.4444, lambda=0.2 
## - Fold4.Rep3: alpha=0.4444, lambda=0.2 
## + Fold4.Rep3: alpha=0.5556, lambda=0.2 
## - Fold4.Rep3: alpha=0.5556, lambda=0.2 
## + Fold4.Rep3: alpha=0.6667, lambda=0.2 
## - Fold4.Rep3: alpha=0.6667, lambda=0.2 
## + Fold4.Rep3: alpha=0.7778, lambda=0.2 
## - Fold4.Rep3: alpha=0.7778, lambda=0.2 
## + Fold4.Rep3: alpha=0.8889, lambda=0.2 
## - Fold4.Rep3: alpha=0.8889, lambda=0.2 
## + Fold4.Rep3: alpha=1.0000, lambda=0.2 
## - Fold4.Rep3: alpha=1.0000, lambda=0.2 
## + Fold5.Rep3: alpha=0.0000, lambda=0.2 
## - Fold5.Rep3: alpha=0.0000, lambda=0.2 
## + Fold5.Rep3: alpha=0.1111, lambda=0.2 
## - Fold5.Rep3: alpha=0.1111, lambda=0.2 
## + Fold5.Rep3: alpha=0.2222, lambda=0.2 
## - Fold5.Rep3: alpha=0.2222, lambda=0.2 
## + Fold5.Rep3: alpha=0.3333, lambda=0.2 
## - Fold5.Rep3: alpha=0.3333, lambda=0.2 
## + Fold5.Rep3: alpha=0.4444, lambda=0.2 
## - Fold5.Rep3: alpha=0.4444, lambda=0.2 
## + Fold5.Rep3: alpha=0.5556, lambda=0.2 
## - Fold5.Rep3: alpha=0.5556, lambda=0.2 
## + Fold5.Rep3: alpha=0.6667, lambda=0.2 
## - Fold5.Rep3: alpha=0.6667, lambda=0.2 
## + Fold5.Rep3: alpha=0.7778, lambda=0.2 
## - Fold5.Rep3: alpha=0.7778, lambda=0.2 
## + Fold5.Rep3: alpha=0.8889, lambda=0.2 
## - Fold5.Rep3: alpha=0.8889, lambda=0.2 
## + Fold5.Rep3: alpha=1.0000, lambda=0.2 
## - Fold5.Rep3: alpha=1.0000, lambda=0.2 
## + Fold1.Rep4: alpha=0.0000, lambda=0.2 
## - Fold1.Rep4: alpha=0.0000, lambda=0.2 
## + Fold1.Rep4: alpha=0.1111, lambda=0.2 
## - Fold1.Rep4: alpha=0.1111, lambda=0.2 
## + Fold1.Rep4: alpha=0.2222, lambda=0.2 
## - Fold1.Rep4: alpha=0.2222, lambda=0.2 
## + Fold1.Rep4: alpha=0.3333, lambda=0.2 
## - Fold1.Rep4: alpha=0.3333, lambda=0.2 
## + Fold1.Rep4: alpha=0.4444, lambda=0.2 
## - Fold1.Rep4: alpha=0.4444, lambda=0.2 
## + Fold1.Rep4: alpha=0.5556, lambda=0.2 
## - Fold1.Rep4: alpha=0.5556, lambda=0.2 
## + Fold1.Rep4: alpha=0.6667, lambda=0.2 
## - Fold1.Rep4: alpha=0.6667, lambda=0.2 
## + Fold1.Rep4: alpha=0.7778, lambda=0.2 
## - Fold1.Rep4: alpha=0.7778, lambda=0.2 
## + Fold1.Rep4: alpha=0.8889, lambda=0.2 
## - Fold1.Rep4: alpha=0.8889, lambda=0.2 
## + Fold1.Rep4: alpha=1.0000, lambda=0.2 
## - Fold1.Rep4: alpha=1.0000, lambda=0.2 
## + Fold2.Rep4: alpha=0.0000, lambda=0.2 
## - Fold2.Rep4: alpha=0.0000, lambda=0.2 
## + Fold2.Rep4: alpha=0.1111, lambda=0.2 
## - Fold2.Rep4: alpha=0.1111, lambda=0.2 
## + Fold2.Rep4: alpha=0.2222, lambda=0.2 
## - Fold2.Rep4: alpha=0.2222, lambda=0.2 
## + Fold2.Rep4: alpha=0.3333, lambda=0.2 
## - Fold2.Rep4: alpha=0.3333, lambda=0.2 
## + Fold2.Rep4: alpha=0.4444, lambda=0.2 
## - Fold2.Rep4: alpha=0.4444, lambda=0.2 
## + Fold2.Rep4: alpha=0.5556, lambda=0.2 
## - Fold2.Rep4: alpha=0.5556, lambda=0.2 
## + Fold2.Rep4: alpha=0.6667, lambda=0.2 
## - Fold2.Rep4: alpha=0.6667, lambda=0.2 
## + Fold2.Rep4: alpha=0.7778, lambda=0.2 
## - Fold2.Rep4: alpha=0.7778, lambda=0.2 
## + Fold2.Rep4: alpha=0.8889, lambda=0.2 
## - Fold2.Rep4: alpha=0.8889, lambda=0.2 
## + Fold2.Rep4: alpha=1.0000, lambda=0.2 
## - Fold2.Rep4: alpha=1.0000, lambda=0.2 
## + Fold3.Rep4: alpha=0.0000, lambda=0.2 
## - Fold3.Rep4: alpha=0.0000, lambda=0.2 
## + Fold3.Rep4: alpha=0.1111, lambda=0.2 
## - Fold3.Rep4: alpha=0.1111, lambda=0.2 
## + Fold3.Rep4: alpha=0.2222, lambda=0.2 
## - Fold3.Rep4: alpha=0.2222, lambda=0.2 
## + Fold3.Rep4: alpha=0.3333, lambda=0.2 
## - Fold3.Rep4: alpha=0.3333, lambda=0.2 
## + Fold3.Rep4: alpha=0.4444, lambda=0.2 
## - Fold3.Rep4: alpha=0.4444, lambda=0.2 
## + Fold3.Rep4: alpha=0.5556, lambda=0.2 
## - Fold3.Rep4: alpha=0.5556, lambda=0.2 
## + Fold3.Rep4: alpha=0.6667, lambda=0.2 
## - Fold3.Rep4: alpha=0.6667, lambda=0.2 
## + Fold3.Rep4: alpha=0.7778, lambda=0.2 
## - Fold3.Rep4: alpha=0.7778, lambda=0.2 
## + Fold3.Rep4: alpha=0.8889, lambda=0.2 
## - Fold3.Rep4: alpha=0.8889, lambda=0.2 
## + Fold3.Rep4: alpha=1.0000, lambda=0.2 
## - Fold3.Rep4: alpha=1.0000, lambda=0.2 
## + Fold4.Rep4: alpha=0.0000, lambda=0.2 
## - Fold4.Rep4: alpha=0.0000, lambda=0.2 
## + Fold4.Rep4: alpha=0.1111, lambda=0.2 
## - Fold4.Rep4: alpha=0.1111, lambda=0.2 
## + Fold4.Rep4: alpha=0.2222, lambda=0.2 
## - Fold4.Rep4: alpha=0.2222, lambda=0.2 
## + Fold4.Rep4: alpha=0.3333, lambda=0.2 
## - Fold4.Rep4: alpha=0.3333, lambda=0.2 
## + Fold4.Rep4: alpha=0.4444, lambda=0.2 
## - Fold4.Rep4: alpha=0.4444, lambda=0.2 
## + Fold4.Rep4: alpha=0.5556, lambda=0.2 
## - Fold4.Rep4: alpha=0.5556, lambda=0.2 
## + Fold4.Rep4: alpha=0.6667, lambda=0.2 
## - Fold4.Rep4: alpha=0.6667, lambda=0.2 
## + Fold4.Rep4: alpha=0.7778, lambda=0.2 
## - Fold4.Rep4: alpha=0.7778, lambda=0.2 
## + Fold4.Rep4: alpha=0.8889, lambda=0.2 
## - Fold4.Rep4: alpha=0.8889, lambda=0.2 
## + Fold4.Rep4: alpha=1.0000, lambda=0.2 
## - Fold4.Rep4: alpha=1.0000, lambda=0.2 
## + Fold5.Rep4: alpha=0.0000, lambda=0.2 
## - Fold5.Rep4: alpha=0.0000, lambda=0.2 
## + Fold5.Rep4: alpha=0.1111, lambda=0.2 
## - Fold5.Rep4: alpha=0.1111, lambda=0.2 
## + Fold5.Rep4: alpha=0.2222, lambda=0.2 
## - Fold5.Rep4: alpha=0.2222, lambda=0.2 
## + Fold5.Rep4: alpha=0.3333, lambda=0.2 
## - Fold5.Rep4: alpha=0.3333, lambda=0.2 
## + Fold5.Rep4: alpha=0.4444, lambda=0.2 
## - Fold5.Rep4: alpha=0.4444, lambda=0.2 
## + Fold5.Rep4: alpha=0.5556, lambda=0.2 
## - Fold5.Rep4: alpha=0.5556, lambda=0.2 
## + Fold5.Rep4: alpha=0.6667, lambda=0.2 
## - Fold5.Rep4: alpha=0.6667, lambda=0.2 
## + Fold5.Rep4: alpha=0.7778, lambda=0.2 
## - Fold5.Rep4: alpha=0.7778, lambda=0.2 
## + Fold5.Rep4: alpha=0.8889, lambda=0.2 
## - Fold5.Rep4: alpha=0.8889, lambda=0.2 
## + Fold5.Rep4: alpha=1.0000, lambda=0.2 
## - Fold5.Rep4: alpha=1.0000, lambda=0.2 
## + Fold1.Rep5: alpha=0.0000, lambda=0.2 
## - Fold1.Rep5: alpha=0.0000, lambda=0.2 
## + Fold1.Rep5: alpha=0.1111, lambda=0.2 
## - Fold1.Rep5: alpha=0.1111, lambda=0.2 
## + Fold1.Rep5: alpha=0.2222, lambda=0.2 
## - Fold1.Rep5: alpha=0.2222, lambda=0.2 
## + Fold1.Rep5: alpha=0.3333, lambda=0.2 
## - Fold1.Rep5: alpha=0.3333, lambda=0.2 
## + Fold1.Rep5: alpha=0.4444, lambda=0.2 
## - Fold1.Rep5: alpha=0.4444, lambda=0.2 
## + Fold1.Rep5: alpha=0.5556, lambda=0.2 
## - Fold1.Rep5: alpha=0.5556, lambda=0.2 
## + Fold1.Rep5: alpha=0.6667, lambda=0.2 
## - Fold1.Rep5: alpha=0.6667, lambda=0.2 
## + Fold1.Rep5: alpha=0.7778, lambda=0.2 
## - Fold1.Rep5: alpha=0.7778, lambda=0.2 
## + Fold1.Rep5: alpha=0.8889, lambda=0.2 
## - Fold1.Rep5: alpha=0.8889, lambda=0.2 
## + Fold1.Rep5: alpha=1.0000, lambda=0.2 
## - Fold1.Rep5: alpha=1.0000, lambda=0.2 
## + Fold2.Rep5: alpha=0.0000, lambda=0.2 
## - Fold2.Rep5: alpha=0.0000, lambda=0.2 
## + Fold2.Rep5: alpha=0.1111, lambda=0.2 
## - Fold2.Rep5: alpha=0.1111, lambda=0.2 
## + Fold2.Rep5: alpha=0.2222, lambda=0.2 
## - Fold2.Rep5: alpha=0.2222, lambda=0.2 
## + Fold2.Rep5: alpha=0.3333, lambda=0.2 
## - Fold2.Rep5: alpha=0.3333, lambda=0.2 
## + Fold2.Rep5: alpha=0.4444, lambda=0.2 
## - Fold2.Rep5: alpha=0.4444, lambda=0.2 
## + Fold2.Rep5: alpha=0.5556, lambda=0.2 
## - Fold2.Rep5: alpha=0.5556, lambda=0.2 
## + Fold2.Rep5: alpha=0.6667, lambda=0.2 
## - Fold2.Rep5: alpha=0.6667, lambda=0.2 
## + Fold2.Rep5: alpha=0.7778, lambda=0.2 
## - Fold2.Rep5: alpha=0.7778, lambda=0.2 
## + Fold2.Rep5: alpha=0.8889, lambda=0.2 
## - Fold2.Rep5: alpha=0.8889, lambda=0.2 
## + Fold2.Rep5: alpha=1.0000, lambda=0.2 
## - Fold2.Rep5: alpha=1.0000, lambda=0.2 
## + Fold3.Rep5: alpha=0.0000, lambda=0.2 
## - Fold3.Rep5: alpha=0.0000, lambda=0.2 
## + Fold3.Rep5: alpha=0.1111, lambda=0.2 
## - Fold3.Rep5: alpha=0.1111, lambda=0.2 
## + Fold3.Rep5: alpha=0.2222, lambda=0.2 
## - Fold3.Rep5: alpha=0.2222, lambda=0.2 
## + Fold3.Rep5: alpha=0.3333, lambda=0.2 
## - Fold3.Rep5: alpha=0.3333, lambda=0.2 
## + Fold3.Rep5: alpha=0.4444, lambda=0.2 
## - Fold3.Rep5: alpha=0.4444, lambda=0.2 
## + Fold3.Rep5: alpha=0.5556, lambda=0.2 
## - Fold3.Rep5: alpha=0.5556, lambda=0.2 
## + Fold3.Rep5: alpha=0.6667, lambda=0.2 
## - Fold3.Rep5: alpha=0.6667, lambda=0.2 
## + Fold3.Rep5: alpha=0.7778, lambda=0.2 
## - Fold3.Rep5: alpha=0.7778, lambda=0.2 
## + Fold3.Rep5: alpha=0.8889, lambda=0.2 
## - Fold3.Rep5: alpha=0.8889, lambda=0.2 
## + Fold3.Rep5: alpha=1.0000, lambda=0.2 
## - Fold3.Rep5: alpha=1.0000, lambda=0.2 
## + Fold4.Rep5: alpha=0.0000, lambda=0.2 
## - Fold4.Rep5: alpha=0.0000, lambda=0.2 
## + Fold4.Rep5: alpha=0.1111, lambda=0.2 
## - Fold4.Rep5: alpha=0.1111, lambda=0.2 
## + Fold4.Rep5: alpha=0.2222, lambda=0.2 
## - Fold4.Rep5: alpha=0.2222, lambda=0.2 
## + Fold4.Rep5: alpha=0.3333, lambda=0.2 
## - Fold4.Rep5: alpha=0.3333, lambda=0.2 
## + Fold4.Rep5: alpha=0.4444, lambda=0.2 
## - Fold4.Rep5: alpha=0.4444, lambda=0.2 
## + Fold4.Rep5: alpha=0.5556, lambda=0.2 
## - Fold4.Rep5: alpha=0.5556, lambda=0.2 
## + Fold4.Rep5: alpha=0.6667, lambda=0.2 
## - Fold4.Rep5: alpha=0.6667, lambda=0.2 
## + Fold4.Rep5: alpha=0.7778, lambda=0.2 
## - Fold4.Rep5: alpha=0.7778, lambda=0.2 
## + Fold4.Rep5: alpha=0.8889, lambda=0.2 
## - Fold4.Rep5: alpha=0.8889, lambda=0.2 
## + Fold4.Rep5: alpha=1.0000, lambda=0.2 
## - Fold4.Rep5: alpha=1.0000, lambda=0.2 
## + Fold5.Rep5: alpha=0.0000, lambda=0.2 
## - Fold5.Rep5: alpha=0.0000, lambda=0.2 
## + Fold5.Rep5: alpha=0.1111, lambda=0.2 
## - Fold5.Rep5: alpha=0.1111, lambda=0.2 
## + Fold5.Rep5: alpha=0.2222, lambda=0.2 
## - Fold5.Rep5: alpha=0.2222, lambda=0.2 
## + Fold5.Rep5: alpha=0.3333, lambda=0.2 
## - Fold5.Rep5: alpha=0.3333, lambda=0.2 
## + Fold5.Rep5: alpha=0.4444, lambda=0.2 
## - Fold5.Rep5: alpha=0.4444, lambda=0.2 
## + Fold5.Rep5: alpha=0.5556, lambda=0.2 
## - Fold5.Rep5: alpha=0.5556, lambda=0.2 
## + Fold5.Rep5: alpha=0.6667, lambda=0.2 
## - Fold5.Rep5: alpha=0.6667, lambda=0.2 
## + Fold5.Rep5: alpha=0.7778, lambda=0.2 
## - Fold5.Rep5: alpha=0.7778, lambda=0.2 
## + Fold5.Rep5: alpha=0.8889, lambda=0.2 
## - Fold5.Rep5: alpha=0.8889, lambda=0.2 
## + Fold5.Rep5: alpha=1.0000, lambda=0.2 
## - Fold5.Rep5: alpha=1.0000, lambda=0.2
## Warning in nominalTrainWorkflow(x = x, y = y, wts = weights, info = trainInfo,
## : There were missing values in resampled performance measures.
## Aggregating results
## Selecting tuning parameters
## Fitting alpha = 1, lambda = 0.15 on full training set
print(en_f)
## glmnet 
## 
##  9 samples
## 15 predictors
## 
## No pre-processing
## Resampling: Cross-Validated (5 fold, repeated 5 times) 
## Summary of sample sizes: 7, 8, 7, 7, 7, 7, ... 
## Resampling results across tuning parameters:
## 
##   alpha      lambda    RMSE       Rsquared  MAE      
##   0.0000000  0.000100  0.3080098  1         0.2823277
##   0.0000000  0.050075  0.3080098  1         0.2823277
##   0.0000000  0.100050  0.3080098  1         0.2823277
##   0.0000000  0.150025  0.3080098  1         0.2823277
##   0.0000000  0.200000  0.3080098  1         0.2823277
##   0.1111111  0.000100  0.3816175  1         0.3429264
##   0.1111111  0.050075  0.3561445  1         0.3211453
##   0.1111111  0.100050  0.3290367  1         0.2955102
##   0.1111111  0.150025  0.3187866  1         0.2886116
##   0.1111111  0.200000  0.3111702  1         0.2822383
##   0.2222222  0.000100  0.4135175  1         0.3692482
##   0.2222222  0.050075  0.3603217  1         0.3218363
##   0.2222222  0.100050  0.3328512  1         0.2993225
##   0.2222222  0.150025  0.3160757  1         0.2852699
##   0.2222222  0.200000  0.3077394  1         0.2773534
##   0.3333333  0.000100  0.4355176  1         0.3876573
##   0.3333333  0.050075  0.3664811  1         0.3259316
##   0.3333333  0.100050  0.3289844  1         0.2940809
##   0.3333333  0.150025  0.3149256  1         0.2837315
##   0.3333333  0.200000  0.3114864  1         0.2826361
##   0.4444444  0.000100  0.4550473  1         0.4044608
##   0.4444444  0.050075  0.3674237  1         0.3248749
##   0.4444444  0.100050  0.3273134  1         0.2939227
##   0.4444444  0.150025  0.3158526  1         0.2861151
##   0.4444444  0.200000  0.3118124  1         0.2844568
##   0.5555556  0.000100  0.4689209  1         0.4153287
##   0.5555556  0.050075  0.3669409  1         0.3246236
##   0.5555556  0.100050  0.3262598  1         0.2940403
##   0.5555556  0.150025  0.3135711  1         0.2853656
##   0.5555556  0.200000  0.3058679  1         0.2793134
##   0.6666667  0.000100  0.4833006  1         0.4265619
##   0.6666667  0.050075  0.3647250  1         0.3230683
##   0.6666667  0.100050  0.3223254  1         0.2922088
##   0.6666667  0.150025  0.3070589  1         0.2794447
##   0.6666667  0.200000  0.3033075  1         0.2776935
##   0.7777778  0.000100  0.4941193  1         0.4355600
##   0.7777778  0.050075  0.3601262  1         0.3213819
##   0.7777778  0.100050  0.3169979  1         0.2883673
##   0.7777778  0.150025  0.3006092  1         0.2732433
##   0.7777778  0.200000  0.3013959  1         0.2762056
##   0.8888889  0.000100  0.4962638  1         0.4391466
##   0.8888889  0.050075  0.3526819  1         0.3167135
##   0.8888889  0.100050  0.3108904  1         0.2829865
##   0.8888889  0.150025  0.2958961  1         0.2684388
##   0.8888889  0.200000  0.3023506  1         0.2778307
##   1.0000000  0.000100  0.5197809  1         0.4633824
##   1.0000000  0.050075  0.3443541  1         0.3100357
##   1.0000000  0.100050  0.3036718  1         0.2762264
##   1.0000000  0.150025  0.2919726  1         0.2653162
##   1.0000000  0.200000  0.3044357  1         0.2819776
## 
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were alpha = 1 and lambda = 0.150025.
plot(varImp(en_f, scale = TRUE))

# extract Coefficients and rank predictors
options(scipen = 99)
coefs <- coef(en_f$finalModel, en_f$bestTune$lambda)
coefs.zero <- coefs[which(coefs == 0),]
coef_order <- order(abs(coefs), decreasing = TRUE)
head(as.matrix(coefs[coef_order,]), 10)
##                    [,1]
## (Intercept)   1.2420957
## Dist_Pour     0.4075865
## Avg_Flow      0.0000000
## Flow_Variance 0.0000000
## Avg_Sand      0.0000000
## Avg_Gravel    0.0000000
## Avg_Cobble    0.0000000
## Avg_Boulder   0.0000000
## Avg_Bedrock   0.0000000
## Avg_Organic   0.0000000
### --- PERIPHYTON BIOMASS MODEL --- ###
perinet <- merged[,-c(1:4)]
train <- perinet[split_ind == 1,]
test <- perinet[split_ind == 2,]

set.seed(1234)
en_p <- train(Periphyton ~ .,
              train,
              method = 'glmnet',
              tuneGrid = expand.grid(alpha = seq(0, 1, length = 10),
                                     lambda = seq(0.0001, 0.2, length = 5)),
              trControl = custom)
## + Fold1.Rep1: alpha=0.0000, lambda=0.2 
## - Fold1.Rep1: alpha=0.0000, lambda=0.2 
## + Fold1.Rep1: alpha=0.1111, lambda=0.2 
## - Fold1.Rep1: alpha=0.1111, lambda=0.2 
## + Fold1.Rep1: alpha=0.2222, lambda=0.2 
## - Fold1.Rep1: alpha=0.2222, lambda=0.2 
## + Fold1.Rep1: alpha=0.3333, lambda=0.2 
## - Fold1.Rep1: alpha=0.3333, lambda=0.2 
## + Fold1.Rep1: alpha=0.4444, lambda=0.2 
## - Fold1.Rep1: alpha=0.4444, lambda=0.2 
## + Fold1.Rep1: alpha=0.5556, lambda=0.2 
## - Fold1.Rep1: alpha=0.5556, lambda=0.2 
## + Fold1.Rep1: alpha=0.6667, lambda=0.2 
## - Fold1.Rep1: alpha=0.6667, lambda=0.2 
## + Fold1.Rep1: alpha=0.7778, lambda=0.2 
## - Fold1.Rep1: alpha=0.7778, lambda=0.2 
## + Fold1.Rep1: alpha=0.8889, lambda=0.2 
## - Fold1.Rep1: alpha=0.8889, lambda=0.2 
## + Fold1.Rep1: alpha=1.0000, lambda=0.2 
## - Fold1.Rep1: alpha=1.0000, lambda=0.2 
## + Fold2.Rep1: alpha=0.0000, lambda=0.2 
## - Fold2.Rep1: alpha=0.0000, lambda=0.2 
## + Fold2.Rep1: alpha=0.1111, lambda=0.2 
## - Fold2.Rep1: alpha=0.1111, lambda=0.2 
## + Fold2.Rep1: alpha=0.2222, lambda=0.2 
## - Fold2.Rep1: alpha=0.2222, lambda=0.2 
## + Fold2.Rep1: alpha=0.3333, lambda=0.2 
## - Fold2.Rep1: alpha=0.3333, lambda=0.2 
## + Fold2.Rep1: alpha=0.4444, lambda=0.2 
## - Fold2.Rep1: alpha=0.4444, lambda=0.2 
## + Fold2.Rep1: alpha=0.5556, lambda=0.2 
## - Fold2.Rep1: alpha=0.5556, lambda=0.2 
## + Fold2.Rep1: alpha=0.6667, lambda=0.2 
## - Fold2.Rep1: alpha=0.6667, lambda=0.2 
## + Fold2.Rep1: alpha=0.7778, lambda=0.2 
## - Fold2.Rep1: alpha=0.7778, lambda=0.2 
## + Fold2.Rep1: alpha=0.8889, lambda=0.2 
## - Fold2.Rep1: alpha=0.8889, lambda=0.2 
## + Fold2.Rep1: alpha=1.0000, lambda=0.2 
## - Fold2.Rep1: alpha=1.0000, lambda=0.2 
## + Fold3.Rep1: alpha=0.0000, lambda=0.2 
## - Fold3.Rep1: alpha=0.0000, lambda=0.2 
## + Fold3.Rep1: alpha=0.1111, lambda=0.2 
## - Fold3.Rep1: alpha=0.1111, lambda=0.2 
## + Fold3.Rep1: alpha=0.2222, lambda=0.2 
## - Fold3.Rep1: alpha=0.2222, lambda=0.2 
## + Fold3.Rep1: alpha=0.3333, lambda=0.2 
## - Fold3.Rep1: alpha=0.3333, lambda=0.2 
## + Fold3.Rep1: alpha=0.4444, lambda=0.2 
## - Fold3.Rep1: alpha=0.4444, lambda=0.2 
## + Fold3.Rep1: alpha=0.5556, lambda=0.2 
## - Fold3.Rep1: alpha=0.5556, lambda=0.2 
## + Fold3.Rep1: alpha=0.6667, lambda=0.2 
## - Fold3.Rep1: alpha=0.6667, lambda=0.2 
## + Fold3.Rep1: alpha=0.7778, lambda=0.2 
## - Fold3.Rep1: alpha=0.7778, lambda=0.2 
## + Fold3.Rep1: alpha=0.8889, lambda=0.2 
## - Fold3.Rep1: alpha=0.8889, lambda=0.2 
## + Fold3.Rep1: alpha=1.0000, lambda=0.2 
## - Fold3.Rep1: alpha=1.0000, lambda=0.2 
## + Fold4.Rep1: alpha=0.0000, lambda=0.2 
## - Fold4.Rep1: alpha=0.0000, lambda=0.2 
## + Fold4.Rep1: alpha=0.1111, lambda=0.2 
## - Fold4.Rep1: alpha=0.1111, lambda=0.2 
## + Fold4.Rep1: alpha=0.2222, lambda=0.2 
## - Fold4.Rep1: alpha=0.2222, lambda=0.2 
## + Fold4.Rep1: alpha=0.3333, lambda=0.2 
## - Fold4.Rep1: alpha=0.3333, lambda=0.2 
## + Fold4.Rep1: alpha=0.4444, lambda=0.2 
## - Fold4.Rep1: alpha=0.4444, lambda=0.2 
## + Fold4.Rep1: alpha=0.5556, lambda=0.2 
## - Fold4.Rep1: alpha=0.5556, lambda=0.2 
## + Fold4.Rep1: alpha=0.6667, lambda=0.2 
## - Fold4.Rep1: alpha=0.6667, lambda=0.2 
## + Fold4.Rep1: alpha=0.7778, lambda=0.2 
## - Fold4.Rep1: alpha=0.7778, lambda=0.2 
## + Fold4.Rep1: alpha=0.8889, lambda=0.2 
## - Fold4.Rep1: alpha=0.8889, lambda=0.2 
## + Fold4.Rep1: alpha=1.0000, lambda=0.2 
## - Fold4.Rep1: alpha=1.0000, lambda=0.2 
## + Fold5.Rep1: alpha=0.0000, lambda=0.2 
## - Fold5.Rep1: alpha=0.0000, lambda=0.2 
## + Fold5.Rep1: alpha=0.1111, lambda=0.2 
## - Fold5.Rep1: alpha=0.1111, lambda=0.2 
## + Fold5.Rep1: alpha=0.2222, lambda=0.2 
## - Fold5.Rep1: alpha=0.2222, lambda=0.2 
## + Fold5.Rep1: alpha=0.3333, lambda=0.2 
## - Fold5.Rep1: alpha=0.3333, lambda=0.2 
## + Fold5.Rep1: alpha=0.4444, lambda=0.2 
## - Fold5.Rep1: alpha=0.4444, lambda=0.2 
## + Fold5.Rep1: alpha=0.5556, lambda=0.2 
## - Fold5.Rep1: alpha=0.5556, lambda=0.2 
## + Fold5.Rep1: alpha=0.6667, lambda=0.2 
## - Fold5.Rep1: alpha=0.6667, lambda=0.2 
## + Fold5.Rep1: alpha=0.7778, lambda=0.2 
## - Fold5.Rep1: alpha=0.7778, lambda=0.2 
## + Fold5.Rep1: alpha=0.8889, lambda=0.2 
## - Fold5.Rep1: alpha=0.8889, lambda=0.2 
## + Fold5.Rep1: alpha=1.0000, lambda=0.2 
## - Fold5.Rep1: alpha=1.0000, lambda=0.2 
## + Fold1.Rep2: alpha=0.0000, lambda=0.2 
## - Fold1.Rep2: alpha=0.0000, lambda=0.2 
## + Fold1.Rep2: alpha=0.1111, lambda=0.2 
## - Fold1.Rep2: alpha=0.1111, lambda=0.2 
## + Fold1.Rep2: alpha=0.2222, lambda=0.2 
## - Fold1.Rep2: alpha=0.2222, lambda=0.2 
## + Fold1.Rep2: alpha=0.3333, lambda=0.2 
## - Fold1.Rep2: alpha=0.3333, lambda=0.2 
## + Fold1.Rep2: alpha=0.4444, lambda=0.2 
## - Fold1.Rep2: alpha=0.4444, lambda=0.2 
## + Fold1.Rep2: alpha=0.5556, lambda=0.2 
## - Fold1.Rep2: alpha=0.5556, lambda=0.2 
## + Fold1.Rep2: alpha=0.6667, lambda=0.2 
## - Fold1.Rep2: alpha=0.6667, lambda=0.2 
## + Fold1.Rep2: alpha=0.7778, lambda=0.2 
## - Fold1.Rep2: alpha=0.7778, lambda=0.2 
## + Fold1.Rep2: alpha=0.8889, lambda=0.2 
## - Fold1.Rep2: alpha=0.8889, lambda=0.2 
## + Fold1.Rep2: alpha=1.0000, lambda=0.2 
## - Fold1.Rep2: alpha=1.0000, lambda=0.2 
## + Fold2.Rep2: alpha=0.0000, lambda=0.2 
## - Fold2.Rep2: alpha=0.0000, lambda=0.2 
## + Fold2.Rep2: alpha=0.1111, lambda=0.2 
## - Fold2.Rep2: alpha=0.1111, lambda=0.2 
## + Fold2.Rep2: alpha=0.2222, lambda=0.2 
## - Fold2.Rep2: alpha=0.2222, lambda=0.2 
## + Fold2.Rep2: alpha=0.3333, lambda=0.2 
## - Fold2.Rep2: alpha=0.3333, lambda=0.2 
## + Fold2.Rep2: alpha=0.4444, lambda=0.2 
## - Fold2.Rep2: alpha=0.4444, lambda=0.2 
## + Fold2.Rep2: alpha=0.5556, lambda=0.2 
## - Fold2.Rep2: alpha=0.5556, lambda=0.2 
## + Fold2.Rep2: alpha=0.6667, lambda=0.2 
## - Fold2.Rep2: alpha=0.6667, lambda=0.2 
## + Fold2.Rep2: alpha=0.7778, lambda=0.2 
## - Fold2.Rep2: alpha=0.7778, lambda=0.2 
## + Fold2.Rep2: alpha=0.8889, lambda=0.2 
## - Fold2.Rep2: alpha=0.8889, lambda=0.2 
## + Fold2.Rep2: alpha=1.0000, lambda=0.2 
## - Fold2.Rep2: alpha=1.0000, lambda=0.2 
## + Fold3.Rep2: alpha=0.0000, lambda=0.2 
## - Fold3.Rep2: alpha=0.0000, lambda=0.2 
## + Fold3.Rep2: alpha=0.1111, lambda=0.2 
## - Fold3.Rep2: alpha=0.1111, lambda=0.2 
## + Fold3.Rep2: alpha=0.2222, lambda=0.2 
## - Fold3.Rep2: alpha=0.2222, lambda=0.2 
## + Fold3.Rep2: alpha=0.3333, lambda=0.2 
## - Fold3.Rep2: alpha=0.3333, lambda=0.2 
## + Fold3.Rep2: alpha=0.4444, lambda=0.2 
## - Fold3.Rep2: alpha=0.4444, lambda=0.2 
## + Fold3.Rep2: alpha=0.5556, lambda=0.2 
## - Fold3.Rep2: alpha=0.5556, lambda=0.2 
## + Fold3.Rep2: alpha=0.6667, lambda=0.2 
## - Fold3.Rep2: alpha=0.6667, lambda=0.2 
## + Fold3.Rep2: alpha=0.7778, lambda=0.2 
## - Fold3.Rep2: alpha=0.7778, lambda=0.2 
## + Fold3.Rep2: alpha=0.8889, lambda=0.2 
## - Fold3.Rep2: alpha=0.8889, lambda=0.2 
## + Fold3.Rep2: alpha=1.0000, lambda=0.2 
## - Fold3.Rep2: alpha=1.0000, lambda=0.2 
## + Fold4.Rep2: alpha=0.0000, lambda=0.2 
## - Fold4.Rep2: alpha=0.0000, lambda=0.2 
## + Fold4.Rep2: alpha=0.1111, lambda=0.2 
## - Fold4.Rep2: alpha=0.1111, lambda=0.2 
## + Fold4.Rep2: alpha=0.2222, lambda=0.2 
## - Fold4.Rep2: alpha=0.2222, lambda=0.2 
## + Fold4.Rep2: alpha=0.3333, lambda=0.2 
## - Fold4.Rep2: alpha=0.3333, lambda=0.2 
## + Fold4.Rep2: alpha=0.4444, lambda=0.2 
## - Fold4.Rep2: alpha=0.4444, lambda=0.2 
## + Fold4.Rep2: alpha=0.5556, lambda=0.2 
## - Fold4.Rep2: alpha=0.5556, lambda=0.2 
## + Fold4.Rep2: alpha=0.6667, lambda=0.2 
## - Fold4.Rep2: alpha=0.6667, lambda=0.2 
## + Fold4.Rep2: alpha=0.7778, lambda=0.2 
## - Fold4.Rep2: alpha=0.7778, lambda=0.2 
## + Fold4.Rep2: alpha=0.8889, lambda=0.2 
## - Fold4.Rep2: alpha=0.8889, lambda=0.2 
## + Fold4.Rep2: alpha=1.0000, lambda=0.2 
## - Fold4.Rep2: alpha=1.0000, lambda=0.2 
## + Fold5.Rep2: alpha=0.0000, lambda=0.2 
## - Fold5.Rep2: alpha=0.0000, lambda=0.2 
## + Fold5.Rep2: alpha=0.1111, lambda=0.2 
## - Fold5.Rep2: alpha=0.1111, lambda=0.2 
## + Fold5.Rep2: alpha=0.2222, lambda=0.2 
## - Fold5.Rep2: alpha=0.2222, lambda=0.2 
## + Fold5.Rep2: alpha=0.3333, lambda=0.2 
## - Fold5.Rep2: alpha=0.3333, lambda=0.2 
## + Fold5.Rep2: alpha=0.4444, lambda=0.2 
## - Fold5.Rep2: alpha=0.4444, lambda=0.2 
## + Fold5.Rep2: alpha=0.5556, lambda=0.2 
## - Fold5.Rep2: alpha=0.5556, lambda=0.2 
## + Fold5.Rep2: alpha=0.6667, lambda=0.2 
## - Fold5.Rep2: alpha=0.6667, lambda=0.2 
## + Fold5.Rep2: alpha=0.7778, lambda=0.2 
## - Fold5.Rep2: alpha=0.7778, lambda=0.2 
## + Fold5.Rep2: alpha=0.8889, lambda=0.2 
## - Fold5.Rep2: alpha=0.8889, lambda=0.2 
## + Fold5.Rep2: alpha=1.0000, lambda=0.2 
## - Fold5.Rep2: alpha=1.0000, lambda=0.2 
## + Fold1.Rep3: alpha=0.0000, lambda=0.2 
## - Fold1.Rep3: alpha=0.0000, lambda=0.2 
## + Fold1.Rep3: alpha=0.1111, lambda=0.2 
## - Fold1.Rep3: alpha=0.1111, lambda=0.2 
## + Fold1.Rep3: alpha=0.2222, lambda=0.2 
## - Fold1.Rep3: alpha=0.2222, lambda=0.2 
## + Fold1.Rep3: alpha=0.3333, lambda=0.2 
## - Fold1.Rep3: alpha=0.3333, lambda=0.2 
## + Fold1.Rep3: alpha=0.4444, lambda=0.2 
## - Fold1.Rep3: alpha=0.4444, lambda=0.2 
## + Fold1.Rep3: alpha=0.5556, lambda=0.2 
## - Fold1.Rep3: alpha=0.5556, lambda=0.2 
## + Fold1.Rep3: alpha=0.6667, lambda=0.2 
## - Fold1.Rep3: alpha=0.6667, lambda=0.2 
## + Fold1.Rep3: alpha=0.7778, lambda=0.2 
## - Fold1.Rep3: alpha=0.7778, lambda=0.2 
## + Fold1.Rep3: alpha=0.8889, lambda=0.2 
## - Fold1.Rep3: alpha=0.8889, lambda=0.2 
## + Fold1.Rep3: alpha=1.0000, lambda=0.2 
## - Fold1.Rep3: alpha=1.0000, lambda=0.2 
## + Fold2.Rep3: alpha=0.0000, lambda=0.2 
## - Fold2.Rep3: alpha=0.0000, lambda=0.2 
## + Fold2.Rep3: alpha=0.1111, lambda=0.2 
## - Fold2.Rep3: alpha=0.1111, lambda=0.2 
## + Fold2.Rep3: alpha=0.2222, lambda=0.2 
## - Fold2.Rep3: alpha=0.2222, lambda=0.2 
## + Fold2.Rep3: alpha=0.3333, lambda=0.2 
## - Fold2.Rep3: alpha=0.3333, lambda=0.2 
## + Fold2.Rep3: alpha=0.4444, lambda=0.2 
## - Fold2.Rep3: alpha=0.4444, lambda=0.2 
## + Fold2.Rep3: alpha=0.5556, lambda=0.2 
## - Fold2.Rep3: alpha=0.5556, lambda=0.2 
## + Fold2.Rep3: alpha=0.6667, lambda=0.2 
## - Fold2.Rep3: alpha=0.6667, lambda=0.2 
## + Fold2.Rep3: alpha=0.7778, lambda=0.2 
## - Fold2.Rep3: alpha=0.7778, lambda=0.2 
## + Fold2.Rep3: alpha=0.8889, lambda=0.2 
## - Fold2.Rep3: alpha=0.8889, lambda=0.2 
## + Fold2.Rep3: alpha=1.0000, lambda=0.2 
## - Fold2.Rep3: alpha=1.0000, lambda=0.2 
## + Fold3.Rep3: alpha=0.0000, lambda=0.2 
## - Fold3.Rep3: alpha=0.0000, lambda=0.2 
## + Fold3.Rep3: alpha=0.1111, lambda=0.2 
## - Fold3.Rep3: alpha=0.1111, lambda=0.2 
## + Fold3.Rep3: alpha=0.2222, lambda=0.2 
## - Fold3.Rep3: alpha=0.2222, lambda=0.2 
## + Fold3.Rep3: alpha=0.3333, lambda=0.2 
## - Fold3.Rep3: alpha=0.3333, lambda=0.2 
## + Fold3.Rep3: alpha=0.4444, lambda=0.2 
## - Fold3.Rep3: alpha=0.4444, lambda=0.2 
## + Fold3.Rep3: alpha=0.5556, lambda=0.2 
## - Fold3.Rep3: alpha=0.5556, lambda=0.2 
## + Fold3.Rep3: alpha=0.6667, lambda=0.2 
## - Fold3.Rep3: alpha=0.6667, lambda=0.2 
## + Fold3.Rep3: alpha=0.7778, lambda=0.2 
## - Fold3.Rep3: alpha=0.7778, lambda=0.2 
## + Fold3.Rep3: alpha=0.8889, lambda=0.2 
## - Fold3.Rep3: alpha=0.8889, lambda=0.2 
## + Fold3.Rep3: alpha=1.0000, lambda=0.2 
## - Fold3.Rep3: alpha=1.0000, lambda=0.2 
## + Fold4.Rep3: alpha=0.0000, lambda=0.2 
## - Fold4.Rep3: alpha=0.0000, lambda=0.2 
## + Fold4.Rep3: alpha=0.1111, lambda=0.2 
## - Fold4.Rep3: alpha=0.1111, lambda=0.2 
## + Fold4.Rep3: alpha=0.2222, lambda=0.2 
## - Fold4.Rep3: alpha=0.2222, lambda=0.2 
## + Fold4.Rep3: alpha=0.3333, lambda=0.2 
## - Fold4.Rep3: alpha=0.3333, lambda=0.2 
## + Fold4.Rep3: alpha=0.4444, lambda=0.2 
## - Fold4.Rep3: alpha=0.4444, lambda=0.2 
## + Fold4.Rep3: alpha=0.5556, lambda=0.2 
## - Fold4.Rep3: alpha=0.5556, lambda=0.2 
## + Fold4.Rep3: alpha=0.6667, lambda=0.2 
## - Fold4.Rep3: alpha=0.6667, lambda=0.2 
## + Fold4.Rep3: alpha=0.7778, lambda=0.2 
## - Fold4.Rep3: alpha=0.7778, lambda=0.2 
## + Fold4.Rep3: alpha=0.8889, lambda=0.2 
## - Fold4.Rep3: alpha=0.8889, lambda=0.2 
## + Fold4.Rep3: alpha=1.0000, lambda=0.2 
## - Fold4.Rep3: alpha=1.0000, lambda=0.2 
## + Fold5.Rep3: alpha=0.0000, lambda=0.2 
## - Fold5.Rep3: alpha=0.0000, lambda=0.2 
## + Fold5.Rep3: alpha=0.1111, lambda=0.2 
## - Fold5.Rep3: alpha=0.1111, lambda=0.2 
## + Fold5.Rep3: alpha=0.2222, lambda=0.2 
## - Fold5.Rep3: alpha=0.2222, lambda=0.2 
## + Fold5.Rep3: alpha=0.3333, lambda=0.2 
## - Fold5.Rep3: alpha=0.3333, lambda=0.2 
## + Fold5.Rep3: alpha=0.4444, lambda=0.2 
## - Fold5.Rep3: alpha=0.4444, lambda=0.2 
## + Fold5.Rep3: alpha=0.5556, lambda=0.2 
## - Fold5.Rep3: alpha=0.5556, lambda=0.2 
## + Fold5.Rep3: alpha=0.6667, lambda=0.2 
## - Fold5.Rep3: alpha=0.6667, lambda=0.2 
## + Fold5.Rep3: alpha=0.7778, lambda=0.2 
## - Fold5.Rep3: alpha=0.7778, lambda=0.2 
## + Fold5.Rep3: alpha=0.8889, lambda=0.2 
## - Fold5.Rep3: alpha=0.8889, lambda=0.2 
## + Fold5.Rep3: alpha=1.0000, lambda=0.2 
## - Fold5.Rep3: alpha=1.0000, lambda=0.2 
## + Fold1.Rep4: alpha=0.0000, lambda=0.2 
## - Fold1.Rep4: alpha=0.0000, lambda=0.2 
## + Fold1.Rep4: alpha=0.1111, lambda=0.2 
## - Fold1.Rep4: alpha=0.1111, lambda=0.2 
## + Fold1.Rep4: alpha=0.2222, lambda=0.2 
## - Fold1.Rep4: alpha=0.2222, lambda=0.2 
## + Fold1.Rep4: alpha=0.3333, lambda=0.2 
## - Fold1.Rep4: alpha=0.3333, lambda=0.2 
## + Fold1.Rep4: alpha=0.4444, lambda=0.2 
## - Fold1.Rep4: alpha=0.4444, lambda=0.2 
## + Fold1.Rep4: alpha=0.5556, lambda=0.2 
## - Fold1.Rep4: alpha=0.5556, lambda=0.2 
## + Fold1.Rep4: alpha=0.6667, lambda=0.2 
## - Fold1.Rep4: alpha=0.6667, lambda=0.2 
## + Fold1.Rep4: alpha=0.7778, lambda=0.2 
## - Fold1.Rep4: alpha=0.7778, lambda=0.2 
## + Fold1.Rep4: alpha=0.8889, lambda=0.2 
## - Fold1.Rep4: alpha=0.8889, lambda=0.2 
## + Fold1.Rep4: alpha=1.0000, lambda=0.2 
## - Fold1.Rep4: alpha=1.0000, lambda=0.2 
## + Fold2.Rep4: alpha=0.0000, lambda=0.2 
## - Fold2.Rep4: alpha=0.0000, lambda=0.2 
## + Fold2.Rep4: alpha=0.1111, lambda=0.2 
## - Fold2.Rep4: alpha=0.1111, lambda=0.2 
## + Fold2.Rep4: alpha=0.2222, lambda=0.2 
## - Fold2.Rep4: alpha=0.2222, lambda=0.2 
## + Fold2.Rep4: alpha=0.3333, lambda=0.2 
## - Fold2.Rep4: alpha=0.3333, lambda=0.2 
## + Fold2.Rep4: alpha=0.4444, lambda=0.2 
## - Fold2.Rep4: alpha=0.4444, lambda=0.2 
## + Fold2.Rep4: alpha=0.5556, lambda=0.2 
## - Fold2.Rep4: alpha=0.5556, lambda=0.2 
## + Fold2.Rep4: alpha=0.6667, lambda=0.2 
## - Fold2.Rep4: alpha=0.6667, lambda=0.2 
## + Fold2.Rep4: alpha=0.7778, lambda=0.2 
## - Fold2.Rep4: alpha=0.7778, lambda=0.2 
## + Fold2.Rep4: alpha=0.8889, lambda=0.2 
## - Fold2.Rep4: alpha=0.8889, lambda=0.2 
## + Fold2.Rep4: alpha=1.0000, lambda=0.2 
## - Fold2.Rep4: alpha=1.0000, lambda=0.2 
## + Fold3.Rep4: alpha=0.0000, lambda=0.2 
## - Fold3.Rep4: alpha=0.0000, lambda=0.2 
## + Fold3.Rep4: alpha=0.1111, lambda=0.2 
## - Fold3.Rep4: alpha=0.1111, lambda=0.2 
## + Fold3.Rep4: alpha=0.2222, lambda=0.2 
## - Fold3.Rep4: alpha=0.2222, lambda=0.2 
## + Fold3.Rep4: alpha=0.3333, lambda=0.2 
## - Fold3.Rep4: alpha=0.3333, lambda=0.2 
## + Fold3.Rep4: alpha=0.4444, lambda=0.2 
## - Fold3.Rep4: alpha=0.4444, lambda=0.2 
## + Fold3.Rep4: alpha=0.5556, lambda=0.2 
## - Fold3.Rep4: alpha=0.5556, lambda=0.2 
## + Fold3.Rep4: alpha=0.6667, lambda=0.2 
## - Fold3.Rep4: alpha=0.6667, lambda=0.2 
## + Fold3.Rep4: alpha=0.7778, lambda=0.2 
## - Fold3.Rep4: alpha=0.7778, lambda=0.2 
## + Fold3.Rep4: alpha=0.8889, lambda=0.2 
## - Fold3.Rep4: alpha=0.8889, lambda=0.2 
## + Fold3.Rep4: alpha=1.0000, lambda=0.2 
## - Fold3.Rep4: alpha=1.0000, lambda=0.2 
## + Fold4.Rep4: alpha=0.0000, lambda=0.2 
## - Fold4.Rep4: alpha=0.0000, lambda=0.2 
## + Fold4.Rep4: alpha=0.1111, lambda=0.2 
## - Fold4.Rep4: alpha=0.1111, lambda=0.2 
## + Fold4.Rep4: alpha=0.2222, lambda=0.2 
## - Fold4.Rep4: alpha=0.2222, lambda=0.2 
## + Fold4.Rep4: alpha=0.3333, lambda=0.2 
## - Fold4.Rep4: alpha=0.3333, lambda=0.2 
## + Fold4.Rep4: alpha=0.4444, lambda=0.2 
## - Fold4.Rep4: alpha=0.4444, lambda=0.2 
## + Fold4.Rep4: alpha=0.5556, lambda=0.2 
## - Fold4.Rep4: alpha=0.5556, lambda=0.2 
## + Fold4.Rep4: alpha=0.6667, lambda=0.2 
## - Fold4.Rep4: alpha=0.6667, lambda=0.2 
## + Fold4.Rep4: alpha=0.7778, lambda=0.2 
## - Fold4.Rep4: alpha=0.7778, lambda=0.2 
## + Fold4.Rep4: alpha=0.8889, lambda=0.2 
## - Fold4.Rep4: alpha=0.8889, lambda=0.2 
## + Fold4.Rep4: alpha=1.0000, lambda=0.2 
## - Fold4.Rep4: alpha=1.0000, lambda=0.2 
## + Fold5.Rep4: alpha=0.0000, lambda=0.2 
## - Fold5.Rep4: alpha=0.0000, lambda=0.2 
## + Fold5.Rep4: alpha=0.1111, lambda=0.2 
## - Fold5.Rep4: alpha=0.1111, lambda=0.2 
## + Fold5.Rep4: alpha=0.2222, lambda=0.2 
## - Fold5.Rep4: alpha=0.2222, lambda=0.2 
## + Fold5.Rep4: alpha=0.3333, lambda=0.2 
## - Fold5.Rep4: alpha=0.3333, lambda=0.2 
## + Fold5.Rep4: alpha=0.4444, lambda=0.2 
## - Fold5.Rep4: alpha=0.4444, lambda=0.2 
## + Fold5.Rep4: alpha=0.5556, lambda=0.2 
## - Fold5.Rep4: alpha=0.5556, lambda=0.2 
## + Fold5.Rep4: alpha=0.6667, lambda=0.2 
## - Fold5.Rep4: alpha=0.6667, lambda=0.2 
## + Fold5.Rep4: alpha=0.7778, lambda=0.2 
## - Fold5.Rep4: alpha=0.7778, lambda=0.2 
## + Fold5.Rep4: alpha=0.8889, lambda=0.2 
## - Fold5.Rep4: alpha=0.8889, lambda=0.2 
## + Fold5.Rep4: alpha=1.0000, lambda=0.2 
## - Fold5.Rep4: alpha=1.0000, lambda=0.2 
## + Fold1.Rep5: alpha=0.0000, lambda=0.2 
## - Fold1.Rep5: alpha=0.0000, lambda=0.2 
## + Fold1.Rep5: alpha=0.1111, lambda=0.2 
## - Fold1.Rep5: alpha=0.1111, lambda=0.2 
## + Fold1.Rep5: alpha=0.2222, lambda=0.2 
## - Fold1.Rep5: alpha=0.2222, lambda=0.2 
## + Fold1.Rep5: alpha=0.3333, lambda=0.2 
## - Fold1.Rep5: alpha=0.3333, lambda=0.2 
## + Fold1.Rep5: alpha=0.4444, lambda=0.2 
## - Fold1.Rep5: alpha=0.4444, lambda=0.2 
## + Fold1.Rep5: alpha=0.5556, lambda=0.2 
## - Fold1.Rep5: alpha=0.5556, lambda=0.2 
## + Fold1.Rep5: alpha=0.6667, lambda=0.2 
## - Fold1.Rep5: alpha=0.6667, lambda=0.2 
## + Fold1.Rep5: alpha=0.7778, lambda=0.2 
## - Fold1.Rep5: alpha=0.7778, lambda=0.2 
## + Fold1.Rep5: alpha=0.8889, lambda=0.2 
## - Fold1.Rep5: alpha=0.8889, lambda=0.2 
## + Fold1.Rep5: alpha=1.0000, lambda=0.2 
## - Fold1.Rep5: alpha=1.0000, lambda=0.2 
## + Fold2.Rep5: alpha=0.0000, lambda=0.2 
## - Fold2.Rep5: alpha=0.0000, lambda=0.2 
## + Fold2.Rep5: alpha=0.1111, lambda=0.2 
## - Fold2.Rep5: alpha=0.1111, lambda=0.2 
## + Fold2.Rep5: alpha=0.2222, lambda=0.2 
## - Fold2.Rep5: alpha=0.2222, lambda=0.2 
## + Fold2.Rep5: alpha=0.3333, lambda=0.2 
## - Fold2.Rep5: alpha=0.3333, lambda=0.2 
## + Fold2.Rep5: alpha=0.4444, lambda=0.2 
## - Fold2.Rep5: alpha=0.4444, lambda=0.2 
## + Fold2.Rep5: alpha=0.5556, lambda=0.2 
## - Fold2.Rep5: alpha=0.5556, lambda=0.2 
## + Fold2.Rep5: alpha=0.6667, lambda=0.2 
## - Fold2.Rep5: alpha=0.6667, lambda=0.2 
## + Fold2.Rep5: alpha=0.7778, lambda=0.2 
## - Fold2.Rep5: alpha=0.7778, lambda=0.2 
## + Fold2.Rep5: alpha=0.8889, lambda=0.2 
## - Fold2.Rep5: alpha=0.8889, lambda=0.2 
## + Fold2.Rep5: alpha=1.0000, lambda=0.2 
## - Fold2.Rep5: alpha=1.0000, lambda=0.2 
## + Fold3.Rep5: alpha=0.0000, lambda=0.2 
## - Fold3.Rep5: alpha=0.0000, lambda=0.2 
## + Fold3.Rep5: alpha=0.1111, lambda=0.2 
## - Fold3.Rep5: alpha=0.1111, lambda=0.2 
## + Fold3.Rep5: alpha=0.2222, lambda=0.2 
## - Fold3.Rep5: alpha=0.2222, lambda=0.2 
## + Fold3.Rep5: alpha=0.3333, lambda=0.2 
## - Fold3.Rep5: alpha=0.3333, lambda=0.2 
## + Fold3.Rep5: alpha=0.4444, lambda=0.2 
## - Fold3.Rep5: alpha=0.4444, lambda=0.2 
## + Fold3.Rep5: alpha=0.5556, lambda=0.2 
## - Fold3.Rep5: alpha=0.5556, lambda=0.2 
## + Fold3.Rep5: alpha=0.6667, lambda=0.2 
## - Fold3.Rep5: alpha=0.6667, lambda=0.2 
## + Fold3.Rep5: alpha=0.7778, lambda=0.2 
## - Fold3.Rep5: alpha=0.7778, lambda=0.2 
## + Fold3.Rep5: alpha=0.8889, lambda=0.2 
## - Fold3.Rep5: alpha=0.8889, lambda=0.2 
## + Fold3.Rep5: alpha=1.0000, lambda=0.2 
## - Fold3.Rep5: alpha=1.0000, lambda=0.2 
## + Fold4.Rep5: alpha=0.0000, lambda=0.2 
## - Fold4.Rep5: alpha=0.0000, lambda=0.2 
## + Fold4.Rep5: alpha=0.1111, lambda=0.2 
## - Fold4.Rep5: alpha=0.1111, lambda=0.2 
## + Fold4.Rep5: alpha=0.2222, lambda=0.2 
## - Fold4.Rep5: alpha=0.2222, lambda=0.2 
## + Fold4.Rep5: alpha=0.3333, lambda=0.2 
## - Fold4.Rep5: alpha=0.3333, lambda=0.2 
## + Fold4.Rep5: alpha=0.4444, lambda=0.2 
## - Fold4.Rep5: alpha=0.4444, lambda=0.2 
## + Fold4.Rep5: alpha=0.5556, lambda=0.2 
## - Fold4.Rep5: alpha=0.5556, lambda=0.2 
## + Fold4.Rep5: alpha=0.6667, lambda=0.2 
## - Fold4.Rep5: alpha=0.6667, lambda=0.2 
## + Fold4.Rep5: alpha=0.7778, lambda=0.2 
## - Fold4.Rep5: alpha=0.7778, lambda=0.2 
## + Fold4.Rep5: alpha=0.8889, lambda=0.2 
## - Fold4.Rep5: alpha=0.8889, lambda=0.2 
## + Fold4.Rep5: alpha=1.0000, lambda=0.2 
## - Fold4.Rep5: alpha=1.0000, lambda=0.2 
## + Fold5.Rep5: alpha=0.0000, lambda=0.2 
## - Fold5.Rep5: alpha=0.0000, lambda=0.2 
## + Fold5.Rep5: alpha=0.1111, lambda=0.2 
## - Fold5.Rep5: alpha=0.1111, lambda=0.2 
## + Fold5.Rep5: alpha=0.2222, lambda=0.2 
## - Fold5.Rep5: alpha=0.2222, lambda=0.2 
## + Fold5.Rep5: alpha=0.3333, lambda=0.2 
## - Fold5.Rep5: alpha=0.3333, lambda=0.2 
## + Fold5.Rep5: alpha=0.4444, lambda=0.2 
## - Fold5.Rep5: alpha=0.4444, lambda=0.2 
## + Fold5.Rep5: alpha=0.5556, lambda=0.2 
## - Fold5.Rep5: alpha=0.5556, lambda=0.2 
## + Fold5.Rep5: alpha=0.6667, lambda=0.2 
## - Fold5.Rep5: alpha=0.6667, lambda=0.2 
## + Fold5.Rep5: alpha=0.7778, lambda=0.2 
## - Fold5.Rep5: alpha=0.7778, lambda=0.2 
## + Fold5.Rep5: alpha=0.8889, lambda=0.2 
## - Fold5.Rep5: alpha=0.8889, lambda=0.2 
## + Fold5.Rep5: alpha=1.0000, lambda=0.2 
## - Fold5.Rep5: alpha=1.0000, lambda=0.2
## Warning in nominalTrainWorkflow(x = x, y = y, wts = weights, info = trainInfo,
## : There were missing values in resampled performance measures.
## Aggregating results
## Selecting tuning parameters
## Fitting alpha = 1, lambda = 0.1 on full training set
print(en_p)
## glmnet 
## 
##  9 samples
## 15 predictors
## 
## No pre-processing
## Resampling: Cross-Validated (5 fold, repeated 5 times) 
## Summary of sample sizes: 7, 8, 7, 7, 7, 7, ... 
## Resampling results across tuning parameters:
## 
##   alpha      lambda    RMSE       Rsquared  MAE      
##   0.0000000  0.000100  0.2884883  1         0.2731310
##   0.0000000  0.050075  0.2884883  1         0.2731310
##   0.0000000  0.100050  0.2884883  1         0.2731310
##   0.0000000  0.150025  0.2884883  1         0.2731310
##   0.0000000  0.200000  0.2884883  1         0.2731310
##   0.1111111  0.000100  0.4161650  1         0.3712960
##   0.1111111  0.050075  0.3779783  1         0.3363616
##   0.1111111  0.100050  0.3407692  1         0.3063865
##   0.1111111  0.150025  0.3159271  1         0.2850946
##   0.1111111  0.200000  0.3018028  1         0.2742277
##   0.2222222  0.000100  0.4482313  1         0.3987135
##   0.2222222  0.050075  0.3728569  1         0.3312740
##   0.2222222  0.100050  0.3223021  1         0.2869565
##   0.2222222  0.150025  0.2974051  1         0.2663367
##   0.2222222  0.200000  0.2888337  1         0.2626941
##   0.3333333  0.000100  0.4729963  1         0.4244349
##   0.3333333  0.050075  0.3619451  1         0.3214525
##   0.3333333  0.100050  0.3050714  1         0.2682782
##   0.3333333  0.150025  0.2893753  1         0.2604168
##   0.3333333  0.200000  0.2849683  1         0.2597520
##   0.4444444  0.000100  0.4979959  1         0.4477444
##   0.4444444  0.050075  0.3472966  1         0.3074893
##   0.4444444  0.100050  0.2966345  1         0.2624511
##   0.4444444  0.150025  0.2836646  1         0.2571966
##   0.4444444  0.200000  0.2861085  1         0.2659180
##   0.5555556  0.000100  0.5184818  1         0.4675372
##   0.5555556  0.050075  0.3365184  1         0.2980955
##   0.5555556  0.100050  0.2882350  1         0.2577133
##   0.5555556  0.150025  0.2819720  1         0.2595075
##   0.5555556  0.200000  0.2860976  1         0.2670695
##   0.6666667  0.000100  0.5380984  1         0.4848576
##   0.6666667  0.050075  0.3278270  1         0.2906837
##   0.6666667  0.100050  0.2814685  1         0.2541339
##   0.6666667  0.150025  0.2803094  1         0.2594379
##   0.6666667  0.200000  0.2865466  1         0.2671460
##   0.7777778  0.000100  0.5579192  1         0.5039367
##   0.7777778  0.050075  0.3251949  1         0.2905071
##   0.7777778  0.100050  0.2808168  1         0.2556296
##   0.7777778  0.150025  0.2791882  1         0.2577627
##   0.7777778  0.200000  0.2949911  1         0.2761257
##   0.8888889  0.000100  0.5575253  1         0.5034880
##   0.8888889  0.050075  0.3238583  1         0.2929500
##   0.8888889  0.100050  0.2759525  1         0.2519883
##   0.8888889  0.150025  0.2845154  1         0.2632881
##   0.8888889  0.200000  0.3021457  1         0.2840843
##   1.0000000  0.000100  0.5756223  1         0.5241637
##   1.0000000  0.050075  0.3194978  1         0.2902943
##   1.0000000  0.100050  0.2730480  1         0.2477272
##   1.0000000  0.150025  0.2902694  1         0.2701464
##   1.0000000  0.200000  0.3097939  1         0.2922539
## 
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were alpha = 1 and lambda = 0.10005.
plot(varImp(en_p, scale = TRUE))

# Coefficients
coefs <- coef(en_p$finalModel, en_p$bestTune$lambda)
coefs.zero <- coefs[which(coefs == 0),]
coef_order <- order(abs(coefs), decreasing = TRUE)
head(as.matrix(coefs[coef_order,]), 10)
##                    [,1]
## (Intercept)   1.3228105
## Dist_Pour     0.4047863
## Avg_Flow      0.0000000
## Flow_Variance 0.0000000
## Avg_Sand      0.0000000
## Avg_Gravel    0.0000000
## Avg_Cobble    0.0000000
## Avg_Boulder   0.0000000
## Avg_Bedrock   0.0000000
## Avg_Organic   0.0000000
### --- BENTHIC BIOMASS MODEL --- ###
bennet <- merged[,-c(1,3:5)]
train <- bennet[split_ind == 1,]
test <- bennet[split_ind == 2,]

set.seed(1234)
en_b <- train(Benthic ~ .,
              train,
              method = 'glmnet',
              tuneGrid = expand.grid(alpha = seq(0, 1, length = 10),
                                     lambda = seq(0.0001, 0.2, length = 5)),
              trControl = custom)
## + Fold1.Rep1: alpha=0.0000, lambda=0.2 
## - Fold1.Rep1: alpha=0.0000, lambda=0.2 
## + Fold1.Rep1: alpha=0.1111, lambda=0.2 
## - Fold1.Rep1: alpha=0.1111, lambda=0.2 
## + Fold1.Rep1: alpha=0.2222, lambda=0.2 
## - Fold1.Rep1: alpha=0.2222, lambda=0.2 
## + Fold1.Rep1: alpha=0.3333, lambda=0.2 
## - Fold1.Rep1: alpha=0.3333, lambda=0.2 
## + Fold1.Rep1: alpha=0.4444, lambda=0.2 
## - Fold1.Rep1: alpha=0.4444, lambda=0.2 
## + Fold1.Rep1: alpha=0.5556, lambda=0.2 
## - Fold1.Rep1: alpha=0.5556, lambda=0.2 
## + Fold1.Rep1: alpha=0.6667, lambda=0.2 
## - Fold1.Rep1: alpha=0.6667, lambda=0.2 
## + Fold1.Rep1: alpha=0.7778, lambda=0.2 
## - Fold1.Rep1: alpha=0.7778, lambda=0.2 
## + Fold1.Rep1: alpha=0.8889, lambda=0.2 
## - Fold1.Rep1: alpha=0.8889, lambda=0.2 
## + Fold1.Rep1: alpha=1.0000, lambda=0.2 
## - Fold1.Rep1: alpha=1.0000, lambda=0.2 
## + Fold2.Rep1: alpha=0.0000, lambda=0.2 
## - Fold2.Rep1: alpha=0.0000, lambda=0.2 
## + Fold2.Rep1: alpha=0.1111, lambda=0.2 
## - Fold2.Rep1: alpha=0.1111, lambda=0.2 
## + Fold2.Rep1: alpha=0.2222, lambda=0.2 
## - Fold2.Rep1: alpha=0.2222, lambda=0.2 
## + Fold2.Rep1: alpha=0.3333, lambda=0.2 
## - Fold2.Rep1: alpha=0.3333, lambda=0.2 
## + Fold2.Rep1: alpha=0.4444, lambda=0.2 
## - Fold2.Rep1: alpha=0.4444, lambda=0.2 
## + Fold2.Rep1: alpha=0.5556, lambda=0.2 
## - Fold2.Rep1: alpha=0.5556, lambda=0.2 
## + Fold2.Rep1: alpha=0.6667, lambda=0.2 
## - Fold2.Rep1: alpha=0.6667, lambda=0.2 
## + Fold2.Rep1: alpha=0.7778, lambda=0.2 
## - Fold2.Rep1: alpha=0.7778, lambda=0.2 
## + Fold2.Rep1: alpha=0.8889, lambda=0.2 
## - Fold2.Rep1: alpha=0.8889, lambda=0.2 
## + Fold2.Rep1: alpha=1.0000, lambda=0.2 
## - Fold2.Rep1: alpha=1.0000, lambda=0.2 
## + Fold3.Rep1: alpha=0.0000, lambda=0.2 
## - Fold3.Rep1: alpha=0.0000, lambda=0.2 
## + Fold3.Rep1: alpha=0.1111, lambda=0.2 
## - Fold3.Rep1: alpha=0.1111, lambda=0.2 
## + Fold3.Rep1: alpha=0.2222, lambda=0.2 
## - Fold3.Rep1: alpha=0.2222, lambda=0.2 
## + Fold3.Rep1: alpha=0.3333, lambda=0.2 
## - Fold3.Rep1: alpha=0.3333, lambda=0.2 
## + Fold3.Rep1: alpha=0.4444, lambda=0.2 
## - Fold3.Rep1: alpha=0.4444, lambda=0.2 
## + Fold3.Rep1: alpha=0.5556, lambda=0.2 
## - Fold3.Rep1: alpha=0.5556, lambda=0.2 
## + Fold3.Rep1: alpha=0.6667, lambda=0.2 
## - Fold3.Rep1: alpha=0.6667, lambda=0.2 
## + Fold3.Rep1: alpha=0.7778, lambda=0.2 
## - Fold3.Rep1: alpha=0.7778, lambda=0.2 
## + Fold3.Rep1: alpha=0.8889, lambda=0.2 
## - Fold3.Rep1: alpha=0.8889, lambda=0.2 
## + Fold3.Rep1: alpha=1.0000, lambda=0.2 
## - Fold3.Rep1: alpha=1.0000, lambda=0.2 
## + Fold4.Rep1: alpha=0.0000, lambda=0.2 
## - Fold4.Rep1: alpha=0.0000, lambda=0.2 
## + Fold4.Rep1: alpha=0.1111, lambda=0.2 
## - Fold4.Rep1: alpha=0.1111, lambda=0.2 
## + Fold4.Rep1: alpha=0.2222, lambda=0.2 
## - Fold4.Rep1: alpha=0.2222, lambda=0.2 
## + Fold4.Rep1: alpha=0.3333, lambda=0.2 
## - Fold4.Rep1: alpha=0.3333, lambda=0.2 
## + Fold4.Rep1: alpha=0.4444, lambda=0.2 
## - Fold4.Rep1: alpha=0.4444, lambda=0.2 
## + Fold4.Rep1: alpha=0.5556, lambda=0.2 
## - Fold4.Rep1: alpha=0.5556, lambda=0.2 
## + Fold4.Rep1: alpha=0.6667, lambda=0.2 
## - Fold4.Rep1: alpha=0.6667, lambda=0.2 
## + Fold4.Rep1: alpha=0.7778, lambda=0.2 
## - Fold4.Rep1: alpha=0.7778, lambda=0.2 
## + Fold4.Rep1: alpha=0.8889, lambda=0.2 
## - Fold4.Rep1: alpha=0.8889, lambda=0.2 
## + Fold4.Rep1: alpha=1.0000, lambda=0.2 
## - Fold4.Rep1: alpha=1.0000, lambda=0.2 
## + Fold5.Rep1: alpha=0.0000, lambda=0.2 
## - Fold5.Rep1: alpha=0.0000, lambda=0.2 
## + Fold5.Rep1: alpha=0.1111, lambda=0.2 
## - Fold5.Rep1: alpha=0.1111, lambda=0.2 
## + Fold5.Rep1: alpha=0.2222, lambda=0.2 
## - Fold5.Rep1: alpha=0.2222, lambda=0.2 
## + Fold5.Rep1: alpha=0.3333, lambda=0.2 
## - Fold5.Rep1: alpha=0.3333, lambda=0.2 
## + Fold5.Rep1: alpha=0.4444, lambda=0.2 
## - Fold5.Rep1: alpha=0.4444, lambda=0.2 
## + Fold5.Rep1: alpha=0.5556, lambda=0.2 
## - Fold5.Rep1: alpha=0.5556, lambda=0.2 
## + Fold5.Rep1: alpha=0.6667, lambda=0.2 
## - Fold5.Rep1: alpha=0.6667, lambda=0.2 
## + Fold5.Rep1: alpha=0.7778, lambda=0.2 
## - Fold5.Rep1: alpha=0.7778, lambda=0.2 
## + Fold5.Rep1: alpha=0.8889, lambda=0.2 
## - Fold5.Rep1: alpha=0.8889, lambda=0.2 
## + Fold5.Rep1: alpha=1.0000, lambda=0.2 
## - Fold5.Rep1: alpha=1.0000, lambda=0.2 
## + Fold1.Rep2: alpha=0.0000, lambda=0.2 
## - Fold1.Rep2: alpha=0.0000, lambda=0.2 
## + Fold1.Rep2: alpha=0.1111, lambda=0.2 
## - Fold1.Rep2: alpha=0.1111, lambda=0.2 
## + Fold1.Rep2: alpha=0.2222, lambda=0.2 
## - Fold1.Rep2: alpha=0.2222, lambda=0.2 
## + Fold1.Rep2: alpha=0.3333, lambda=0.2 
## - Fold1.Rep2: alpha=0.3333, lambda=0.2 
## + Fold1.Rep2: alpha=0.4444, lambda=0.2 
## - Fold1.Rep2: alpha=0.4444, lambda=0.2 
## + Fold1.Rep2: alpha=0.5556, lambda=0.2 
## - Fold1.Rep2: alpha=0.5556, lambda=0.2 
## + Fold1.Rep2: alpha=0.6667, lambda=0.2 
## - Fold1.Rep2: alpha=0.6667, lambda=0.2 
## + Fold1.Rep2: alpha=0.7778, lambda=0.2 
## - Fold1.Rep2: alpha=0.7778, lambda=0.2 
## + Fold1.Rep2: alpha=0.8889, lambda=0.2 
## - Fold1.Rep2: alpha=0.8889, lambda=0.2 
## + Fold1.Rep2: alpha=1.0000, lambda=0.2 
## - Fold1.Rep2: alpha=1.0000, lambda=0.2 
## + Fold2.Rep2: alpha=0.0000, lambda=0.2 
## - Fold2.Rep2: alpha=0.0000, lambda=0.2 
## + Fold2.Rep2: alpha=0.1111, lambda=0.2 
## - Fold2.Rep2: alpha=0.1111, lambda=0.2 
## + Fold2.Rep2: alpha=0.2222, lambda=0.2 
## - Fold2.Rep2: alpha=0.2222, lambda=0.2 
## + Fold2.Rep2: alpha=0.3333, lambda=0.2 
## - Fold2.Rep2: alpha=0.3333, lambda=0.2 
## + Fold2.Rep2: alpha=0.4444, lambda=0.2 
## - Fold2.Rep2: alpha=0.4444, lambda=0.2 
## + Fold2.Rep2: alpha=0.5556, lambda=0.2 
## - Fold2.Rep2: alpha=0.5556, lambda=0.2 
## + Fold2.Rep2: alpha=0.6667, lambda=0.2 
## - Fold2.Rep2: alpha=0.6667, lambda=0.2 
## + Fold2.Rep2: alpha=0.7778, lambda=0.2 
## - Fold2.Rep2: alpha=0.7778, lambda=0.2 
## + Fold2.Rep2: alpha=0.8889, lambda=0.2 
## - Fold2.Rep2: alpha=0.8889, lambda=0.2 
## + Fold2.Rep2: alpha=1.0000, lambda=0.2 
## - Fold2.Rep2: alpha=1.0000, lambda=0.2 
## + Fold3.Rep2: alpha=0.0000, lambda=0.2 
## - Fold3.Rep2: alpha=0.0000, lambda=0.2 
## + Fold3.Rep2: alpha=0.1111, lambda=0.2 
## - Fold3.Rep2: alpha=0.1111, lambda=0.2 
## + Fold3.Rep2: alpha=0.2222, lambda=0.2 
## - Fold3.Rep2: alpha=0.2222, lambda=0.2 
## + Fold3.Rep2: alpha=0.3333, lambda=0.2 
## - Fold3.Rep2: alpha=0.3333, lambda=0.2 
## + Fold3.Rep2: alpha=0.4444, lambda=0.2 
## - Fold3.Rep2: alpha=0.4444, lambda=0.2 
## + Fold3.Rep2: alpha=0.5556, lambda=0.2 
## - Fold3.Rep2: alpha=0.5556, lambda=0.2 
## + Fold3.Rep2: alpha=0.6667, lambda=0.2 
## - Fold3.Rep2: alpha=0.6667, lambda=0.2 
## + Fold3.Rep2: alpha=0.7778, lambda=0.2 
## - Fold3.Rep2: alpha=0.7778, lambda=0.2 
## + Fold3.Rep2: alpha=0.8889, lambda=0.2 
## - Fold3.Rep2: alpha=0.8889, lambda=0.2 
## + Fold3.Rep2: alpha=1.0000, lambda=0.2 
## - Fold3.Rep2: alpha=1.0000, lambda=0.2 
## + Fold4.Rep2: alpha=0.0000, lambda=0.2 
## - Fold4.Rep2: alpha=0.0000, lambda=0.2 
## + Fold4.Rep2: alpha=0.1111, lambda=0.2 
## - Fold4.Rep2: alpha=0.1111, lambda=0.2 
## + Fold4.Rep2: alpha=0.2222, lambda=0.2 
## - Fold4.Rep2: alpha=0.2222, lambda=0.2 
## + Fold4.Rep2: alpha=0.3333, lambda=0.2 
## - Fold4.Rep2: alpha=0.3333, lambda=0.2 
## + Fold4.Rep2: alpha=0.4444, lambda=0.2 
## - Fold4.Rep2: alpha=0.4444, lambda=0.2 
## + Fold4.Rep2: alpha=0.5556, lambda=0.2 
## - Fold4.Rep2: alpha=0.5556, lambda=0.2 
## + Fold4.Rep2: alpha=0.6667, lambda=0.2 
## - Fold4.Rep2: alpha=0.6667, lambda=0.2 
## + Fold4.Rep2: alpha=0.7778, lambda=0.2 
## - Fold4.Rep2: alpha=0.7778, lambda=0.2 
## + Fold4.Rep2: alpha=0.8889, lambda=0.2 
## - Fold4.Rep2: alpha=0.8889, lambda=0.2 
## + Fold4.Rep2: alpha=1.0000, lambda=0.2 
## - Fold4.Rep2: alpha=1.0000, lambda=0.2 
## + Fold5.Rep2: alpha=0.0000, lambda=0.2 
## - Fold5.Rep2: alpha=0.0000, lambda=0.2 
## + Fold5.Rep2: alpha=0.1111, lambda=0.2 
## - Fold5.Rep2: alpha=0.1111, lambda=0.2 
## + Fold5.Rep2: alpha=0.2222, lambda=0.2 
## - Fold5.Rep2: alpha=0.2222, lambda=0.2 
## + Fold5.Rep2: alpha=0.3333, lambda=0.2 
## - Fold5.Rep2: alpha=0.3333, lambda=0.2 
## + Fold5.Rep2: alpha=0.4444, lambda=0.2 
## - Fold5.Rep2: alpha=0.4444, lambda=0.2 
## + Fold5.Rep2: alpha=0.5556, lambda=0.2 
## - Fold5.Rep2: alpha=0.5556, lambda=0.2 
## + Fold5.Rep2: alpha=0.6667, lambda=0.2 
## - Fold5.Rep2: alpha=0.6667, lambda=0.2 
## + Fold5.Rep2: alpha=0.7778, lambda=0.2 
## - Fold5.Rep2: alpha=0.7778, lambda=0.2 
## + Fold5.Rep2: alpha=0.8889, lambda=0.2 
## - Fold5.Rep2: alpha=0.8889, lambda=0.2 
## + Fold5.Rep2: alpha=1.0000, lambda=0.2 
## - Fold5.Rep2: alpha=1.0000, lambda=0.2 
## + Fold1.Rep3: alpha=0.0000, lambda=0.2 
## - Fold1.Rep3: alpha=0.0000, lambda=0.2 
## + Fold1.Rep3: alpha=0.1111, lambda=0.2 
## - Fold1.Rep3: alpha=0.1111, lambda=0.2 
## + Fold1.Rep3: alpha=0.2222, lambda=0.2 
## - Fold1.Rep3: alpha=0.2222, lambda=0.2 
## + Fold1.Rep3: alpha=0.3333, lambda=0.2 
## - Fold1.Rep3: alpha=0.3333, lambda=0.2 
## + Fold1.Rep3: alpha=0.4444, lambda=0.2 
## - Fold1.Rep3: alpha=0.4444, lambda=0.2 
## + Fold1.Rep3: alpha=0.5556, lambda=0.2 
## - Fold1.Rep3: alpha=0.5556, lambda=0.2 
## + Fold1.Rep3: alpha=0.6667, lambda=0.2 
## - Fold1.Rep3: alpha=0.6667, lambda=0.2 
## + Fold1.Rep3: alpha=0.7778, lambda=0.2 
## - Fold1.Rep3: alpha=0.7778, lambda=0.2 
## + Fold1.Rep3: alpha=0.8889, lambda=0.2 
## - Fold1.Rep3: alpha=0.8889, lambda=0.2 
## + Fold1.Rep3: alpha=1.0000, lambda=0.2 
## - Fold1.Rep3: alpha=1.0000, lambda=0.2 
## + Fold2.Rep3: alpha=0.0000, lambda=0.2 
## - Fold2.Rep3: alpha=0.0000, lambda=0.2 
## + Fold2.Rep3: alpha=0.1111, lambda=0.2 
## - Fold2.Rep3: alpha=0.1111, lambda=0.2 
## + Fold2.Rep3: alpha=0.2222, lambda=0.2 
## - Fold2.Rep3: alpha=0.2222, lambda=0.2 
## + Fold2.Rep3: alpha=0.3333, lambda=0.2 
## - Fold2.Rep3: alpha=0.3333, lambda=0.2 
## + Fold2.Rep3: alpha=0.4444, lambda=0.2 
## - Fold2.Rep3: alpha=0.4444, lambda=0.2 
## + Fold2.Rep3: alpha=0.5556, lambda=0.2 
## - Fold2.Rep3: alpha=0.5556, lambda=0.2 
## + Fold2.Rep3: alpha=0.6667, lambda=0.2 
## - Fold2.Rep3: alpha=0.6667, lambda=0.2 
## + Fold2.Rep3: alpha=0.7778, lambda=0.2 
## - Fold2.Rep3: alpha=0.7778, lambda=0.2 
## + Fold2.Rep3: alpha=0.8889, lambda=0.2 
## - Fold2.Rep3: alpha=0.8889, lambda=0.2 
## + Fold2.Rep3: alpha=1.0000, lambda=0.2 
## - Fold2.Rep3: alpha=1.0000, lambda=0.2 
## + Fold3.Rep3: alpha=0.0000, lambda=0.2 
## - Fold3.Rep3: alpha=0.0000, lambda=0.2 
## + Fold3.Rep3: alpha=0.1111, lambda=0.2 
## - Fold3.Rep3: alpha=0.1111, lambda=0.2 
## + Fold3.Rep3: alpha=0.2222, lambda=0.2 
## - Fold3.Rep3: alpha=0.2222, lambda=0.2 
## + Fold3.Rep3: alpha=0.3333, lambda=0.2 
## - Fold3.Rep3: alpha=0.3333, lambda=0.2 
## + Fold3.Rep3: alpha=0.4444, lambda=0.2 
## - Fold3.Rep3: alpha=0.4444, lambda=0.2 
## + Fold3.Rep3: alpha=0.5556, lambda=0.2 
## - Fold3.Rep3: alpha=0.5556, lambda=0.2 
## + Fold3.Rep3: alpha=0.6667, lambda=0.2 
## - Fold3.Rep3: alpha=0.6667, lambda=0.2 
## + Fold3.Rep3: alpha=0.7778, lambda=0.2 
## - Fold3.Rep3: alpha=0.7778, lambda=0.2 
## + Fold3.Rep3: alpha=0.8889, lambda=0.2 
## - Fold3.Rep3: alpha=0.8889, lambda=0.2 
## + Fold3.Rep3: alpha=1.0000, lambda=0.2 
## - Fold3.Rep3: alpha=1.0000, lambda=0.2 
## + Fold4.Rep3: alpha=0.0000, lambda=0.2 
## - Fold4.Rep3: alpha=0.0000, lambda=0.2 
## + Fold4.Rep3: alpha=0.1111, lambda=0.2 
## - Fold4.Rep3: alpha=0.1111, lambda=0.2 
## + Fold4.Rep3: alpha=0.2222, lambda=0.2 
## - Fold4.Rep3: alpha=0.2222, lambda=0.2 
## + Fold4.Rep3: alpha=0.3333, lambda=0.2 
## - Fold4.Rep3: alpha=0.3333, lambda=0.2 
## + Fold4.Rep3: alpha=0.4444, lambda=0.2 
## - Fold4.Rep3: alpha=0.4444, lambda=0.2 
## + Fold4.Rep3: alpha=0.5556, lambda=0.2 
## - Fold4.Rep3: alpha=0.5556, lambda=0.2 
## + Fold4.Rep3: alpha=0.6667, lambda=0.2 
## - Fold4.Rep3: alpha=0.6667, lambda=0.2 
## + Fold4.Rep3: alpha=0.7778, lambda=0.2 
## - Fold4.Rep3: alpha=0.7778, lambda=0.2 
## + Fold4.Rep3: alpha=0.8889, lambda=0.2 
## - Fold4.Rep3: alpha=0.8889, lambda=0.2 
## + Fold4.Rep3: alpha=1.0000, lambda=0.2 
## - Fold4.Rep3: alpha=1.0000, lambda=0.2 
## + Fold5.Rep3: alpha=0.0000, lambda=0.2 
## - Fold5.Rep3: alpha=0.0000, lambda=0.2 
## + Fold5.Rep3: alpha=0.1111, lambda=0.2 
## - Fold5.Rep3: alpha=0.1111, lambda=0.2 
## + Fold5.Rep3: alpha=0.2222, lambda=0.2 
## - Fold5.Rep3: alpha=0.2222, lambda=0.2 
## + Fold5.Rep3: alpha=0.3333, lambda=0.2 
## - Fold5.Rep3: alpha=0.3333, lambda=0.2 
## + Fold5.Rep3: alpha=0.4444, lambda=0.2 
## - Fold5.Rep3: alpha=0.4444, lambda=0.2 
## + Fold5.Rep3: alpha=0.5556, lambda=0.2 
## - Fold5.Rep3: alpha=0.5556, lambda=0.2 
## + Fold5.Rep3: alpha=0.6667, lambda=0.2 
## - Fold5.Rep3: alpha=0.6667, lambda=0.2 
## + Fold5.Rep3: alpha=0.7778, lambda=0.2 
## - Fold5.Rep3: alpha=0.7778, lambda=0.2 
## + Fold5.Rep3: alpha=0.8889, lambda=0.2 
## - Fold5.Rep3: alpha=0.8889, lambda=0.2 
## + Fold5.Rep3: alpha=1.0000, lambda=0.2 
## - Fold5.Rep3: alpha=1.0000, lambda=0.2 
## + Fold1.Rep4: alpha=0.0000, lambda=0.2 
## - Fold1.Rep4: alpha=0.0000, lambda=0.2 
## + Fold1.Rep4: alpha=0.1111, lambda=0.2 
## - Fold1.Rep4: alpha=0.1111, lambda=0.2 
## + Fold1.Rep4: alpha=0.2222, lambda=0.2 
## - Fold1.Rep4: alpha=0.2222, lambda=0.2 
## + Fold1.Rep4: alpha=0.3333, lambda=0.2 
## - Fold1.Rep4: alpha=0.3333, lambda=0.2 
## + Fold1.Rep4: alpha=0.4444, lambda=0.2 
## - Fold1.Rep4: alpha=0.4444, lambda=0.2 
## + Fold1.Rep4: alpha=0.5556, lambda=0.2 
## - Fold1.Rep4: alpha=0.5556, lambda=0.2 
## + Fold1.Rep4: alpha=0.6667, lambda=0.2 
## - Fold1.Rep4: alpha=0.6667, lambda=0.2 
## + Fold1.Rep4: alpha=0.7778, lambda=0.2 
## - Fold1.Rep4: alpha=0.7778, lambda=0.2 
## + Fold1.Rep4: alpha=0.8889, lambda=0.2 
## - Fold1.Rep4: alpha=0.8889, lambda=0.2 
## + Fold1.Rep4: alpha=1.0000, lambda=0.2 
## - Fold1.Rep4: alpha=1.0000, lambda=0.2 
## + Fold2.Rep4: alpha=0.0000, lambda=0.2 
## - Fold2.Rep4: alpha=0.0000, lambda=0.2 
## + Fold2.Rep4: alpha=0.1111, lambda=0.2 
## - Fold2.Rep4: alpha=0.1111, lambda=0.2 
## + Fold2.Rep4: alpha=0.2222, lambda=0.2 
## - Fold2.Rep4: alpha=0.2222, lambda=0.2 
## + Fold2.Rep4: alpha=0.3333, lambda=0.2 
## - Fold2.Rep4: alpha=0.3333, lambda=0.2 
## + Fold2.Rep4: alpha=0.4444, lambda=0.2 
## - Fold2.Rep4: alpha=0.4444, lambda=0.2 
## + Fold2.Rep4: alpha=0.5556, lambda=0.2 
## - Fold2.Rep4: alpha=0.5556, lambda=0.2 
## + Fold2.Rep4: alpha=0.6667, lambda=0.2 
## - Fold2.Rep4: alpha=0.6667, lambda=0.2 
## + Fold2.Rep4: alpha=0.7778, lambda=0.2 
## - Fold2.Rep4: alpha=0.7778, lambda=0.2 
## + Fold2.Rep4: alpha=0.8889, lambda=0.2 
## - Fold2.Rep4: alpha=0.8889, lambda=0.2 
## + Fold2.Rep4: alpha=1.0000, lambda=0.2 
## - Fold2.Rep4: alpha=1.0000, lambda=0.2 
## + Fold3.Rep4: alpha=0.0000, lambda=0.2 
## - Fold3.Rep4: alpha=0.0000, lambda=0.2 
## + Fold3.Rep4: alpha=0.1111, lambda=0.2 
## - Fold3.Rep4: alpha=0.1111, lambda=0.2 
## + Fold3.Rep4: alpha=0.2222, lambda=0.2 
## - Fold3.Rep4: alpha=0.2222, lambda=0.2 
## + Fold3.Rep4: alpha=0.3333, lambda=0.2 
## - Fold3.Rep4: alpha=0.3333, lambda=0.2 
## + Fold3.Rep4: alpha=0.4444, lambda=0.2 
## - Fold3.Rep4: alpha=0.4444, lambda=0.2 
## + Fold3.Rep4: alpha=0.5556, lambda=0.2 
## - Fold3.Rep4: alpha=0.5556, lambda=0.2 
## + Fold3.Rep4: alpha=0.6667, lambda=0.2 
## - Fold3.Rep4: alpha=0.6667, lambda=0.2 
## + Fold3.Rep4: alpha=0.7778, lambda=0.2 
## - Fold3.Rep4: alpha=0.7778, lambda=0.2 
## + Fold3.Rep4: alpha=0.8889, lambda=0.2 
## - Fold3.Rep4: alpha=0.8889, lambda=0.2 
## + Fold3.Rep4: alpha=1.0000, lambda=0.2 
## - Fold3.Rep4: alpha=1.0000, lambda=0.2 
## + Fold4.Rep4: alpha=0.0000, lambda=0.2 
## - Fold4.Rep4: alpha=0.0000, lambda=0.2 
## + Fold4.Rep4: alpha=0.1111, lambda=0.2 
## - Fold4.Rep4: alpha=0.1111, lambda=0.2 
## + Fold4.Rep4: alpha=0.2222, lambda=0.2 
## - Fold4.Rep4: alpha=0.2222, lambda=0.2 
## + Fold4.Rep4: alpha=0.3333, lambda=0.2 
## - Fold4.Rep4: alpha=0.3333, lambda=0.2 
## + Fold4.Rep4: alpha=0.4444, lambda=0.2 
## - Fold4.Rep4: alpha=0.4444, lambda=0.2 
## + Fold4.Rep4: alpha=0.5556, lambda=0.2 
## - Fold4.Rep4: alpha=0.5556, lambda=0.2 
## + Fold4.Rep4: alpha=0.6667, lambda=0.2 
## - Fold4.Rep4: alpha=0.6667, lambda=0.2 
## + Fold4.Rep4: alpha=0.7778, lambda=0.2 
## - Fold4.Rep4: alpha=0.7778, lambda=0.2 
## + Fold4.Rep4: alpha=0.8889, lambda=0.2 
## - Fold4.Rep4: alpha=0.8889, lambda=0.2 
## + Fold4.Rep4: alpha=1.0000, lambda=0.2 
## - Fold4.Rep4: alpha=1.0000, lambda=0.2 
## + Fold5.Rep4: alpha=0.0000, lambda=0.2 
## - Fold5.Rep4: alpha=0.0000, lambda=0.2 
## + Fold5.Rep4: alpha=0.1111, lambda=0.2 
## - Fold5.Rep4: alpha=0.1111, lambda=0.2 
## + Fold5.Rep4: alpha=0.2222, lambda=0.2 
## - Fold5.Rep4: alpha=0.2222, lambda=0.2 
## + Fold5.Rep4: alpha=0.3333, lambda=0.2 
## - Fold5.Rep4: alpha=0.3333, lambda=0.2 
## + Fold5.Rep4: alpha=0.4444, lambda=0.2 
## - Fold5.Rep4: alpha=0.4444, lambda=0.2 
## + Fold5.Rep4: alpha=0.5556, lambda=0.2 
## - Fold5.Rep4: alpha=0.5556, lambda=0.2 
## + Fold5.Rep4: alpha=0.6667, lambda=0.2 
## - Fold5.Rep4: alpha=0.6667, lambda=0.2 
## + Fold5.Rep4: alpha=0.7778, lambda=0.2 
## - Fold5.Rep4: alpha=0.7778, lambda=0.2 
## + Fold5.Rep4: alpha=0.8889, lambda=0.2 
## - Fold5.Rep4: alpha=0.8889, lambda=0.2 
## + Fold5.Rep4: alpha=1.0000, lambda=0.2 
## - Fold5.Rep4: alpha=1.0000, lambda=0.2 
## + Fold1.Rep5: alpha=0.0000, lambda=0.2 
## - Fold1.Rep5: alpha=0.0000, lambda=0.2 
## + Fold1.Rep5: alpha=0.1111, lambda=0.2 
## - Fold1.Rep5: alpha=0.1111, lambda=0.2 
## + Fold1.Rep5: alpha=0.2222, lambda=0.2 
## - Fold1.Rep5: alpha=0.2222, lambda=0.2 
## + Fold1.Rep5: alpha=0.3333, lambda=0.2 
## - Fold1.Rep5: alpha=0.3333, lambda=0.2 
## + Fold1.Rep5: alpha=0.4444, lambda=0.2 
## - Fold1.Rep5: alpha=0.4444, lambda=0.2 
## + Fold1.Rep5: alpha=0.5556, lambda=0.2 
## - Fold1.Rep5: alpha=0.5556, lambda=0.2 
## + Fold1.Rep5: alpha=0.6667, lambda=0.2 
## - Fold1.Rep5: alpha=0.6667, lambda=0.2 
## + Fold1.Rep5: alpha=0.7778, lambda=0.2 
## - Fold1.Rep5: alpha=0.7778, lambda=0.2 
## + Fold1.Rep5: alpha=0.8889, lambda=0.2 
## - Fold1.Rep5: alpha=0.8889, lambda=0.2 
## + Fold1.Rep5: alpha=1.0000, lambda=0.2 
## - Fold1.Rep5: alpha=1.0000, lambda=0.2 
## + Fold2.Rep5: alpha=0.0000, lambda=0.2 
## - Fold2.Rep5: alpha=0.0000, lambda=0.2 
## + Fold2.Rep5: alpha=0.1111, lambda=0.2 
## - Fold2.Rep5: alpha=0.1111, lambda=0.2 
## + Fold2.Rep5: alpha=0.2222, lambda=0.2 
## - Fold2.Rep5: alpha=0.2222, lambda=0.2 
## + Fold2.Rep5: alpha=0.3333, lambda=0.2 
## - Fold2.Rep5: alpha=0.3333, lambda=0.2 
## + Fold2.Rep5: alpha=0.4444, lambda=0.2 
## - Fold2.Rep5: alpha=0.4444, lambda=0.2 
## + Fold2.Rep5: alpha=0.5556, lambda=0.2 
## - Fold2.Rep5: alpha=0.5556, lambda=0.2 
## + Fold2.Rep5: alpha=0.6667, lambda=0.2 
## - Fold2.Rep5: alpha=0.6667, lambda=0.2 
## + Fold2.Rep5: alpha=0.7778, lambda=0.2 
## - Fold2.Rep5: alpha=0.7778, lambda=0.2 
## + Fold2.Rep5: alpha=0.8889, lambda=0.2 
## - Fold2.Rep5: alpha=0.8889, lambda=0.2 
## + Fold2.Rep5: alpha=1.0000, lambda=0.2 
## - Fold2.Rep5: alpha=1.0000, lambda=0.2 
## + Fold3.Rep5: alpha=0.0000, lambda=0.2 
## - Fold3.Rep5: alpha=0.0000, lambda=0.2 
## + Fold3.Rep5: alpha=0.1111, lambda=0.2 
## - Fold3.Rep5: alpha=0.1111, lambda=0.2 
## + Fold3.Rep5: alpha=0.2222, lambda=0.2 
## - Fold3.Rep5: alpha=0.2222, lambda=0.2 
## + Fold3.Rep5: alpha=0.3333, lambda=0.2 
## - Fold3.Rep5: alpha=0.3333, lambda=0.2 
## + Fold3.Rep5: alpha=0.4444, lambda=0.2 
## - Fold3.Rep5: alpha=0.4444, lambda=0.2 
## + Fold3.Rep5: alpha=0.5556, lambda=0.2 
## - Fold3.Rep5: alpha=0.5556, lambda=0.2 
## + Fold3.Rep5: alpha=0.6667, lambda=0.2 
## - Fold3.Rep5: alpha=0.6667, lambda=0.2 
## + Fold3.Rep5: alpha=0.7778, lambda=0.2 
## - Fold3.Rep5: alpha=0.7778, lambda=0.2 
## + Fold3.Rep5: alpha=0.8889, lambda=0.2 
## - Fold3.Rep5: alpha=0.8889, lambda=0.2 
## + Fold3.Rep5: alpha=1.0000, lambda=0.2 
## - Fold3.Rep5: alpha=1.0000, lambda=0.2 
## + Fold4.Rep5: alpha=0.0000, lambda=0.2 
## - Fold4.Rep5: alpha=0.0000, lambda=0.2 
## + Fold4.Rep5: alpha=0.1111, lambda=0.2 
## - Fold4.Rep5: alpha=0.1111, lambda=0.2 
## + Fold4.Rep5: alpha=0.2222, lambda=0.2 
## - Fold4.Rep5: alpha=0.2222, lambda=0.2 
## + Fold4.Rep5: alpha=0.3333, lambda=0.2 
## - Fold4.Rep5: alpha=0.3333, lambda=0.2 
## + Fold4.Rep5: alpha=0.4444, lambda=0.2 
## - Fold4.Rep5: alpha=0.4444, lambda=0.2 
## + Fold4.Rep5: alpha=0.5556, lambda=0.2 
## - Fold4.Rep5: alpha=0.5556, lambda=0.2 
## + Fold4.Rep5: alpha=0.6667, lambda=0.2 
## - Fold4.Rep5: alpha=0.6667, lambda=0.2 
## + Fold4.Rep5: alpha=0.7778, lambda=0.2 
## - Fold4.Rep5: alpha=0.7778, lambda=0.2 
## + Fold4.Rep5: alpha=0.8889, lambda=0.2 
## - Fold4.Rep5: alpha=0.8889, lambda=0.2 
## + Fold4.Rep5: alpha=1.0000, lambda=0.2 
## - Fold4.Rep5: alpha=1.0000, lambda=0.2 
## + Fold5.Rep5: alpha=0.0000, lambda=0.2 
## - Fold5.Rep5: alpha=0.0000, lambda=0.2 
## + Fold5.Rep5: alpha=0.1111, lambda=0.2 
## - Fold5.Rep5: alpha=0.1111, lambda=0.2 
## + Fold5.Rep5: alpha=0.2222, lambda=0.2 
## - Fold5.Rep5: alpha=0.2222, lambda=0.2 
## + Fold5.Rep5: alpha=0.3333, lambda=0.2 
## - Fold5.Rep5: alpha=0.3333, lambda=0.2 
## + Fold5.Rep5: alpha=0.4444, lambda=0.2 
## - Fold5.Rep5: alpha=0.4444, lambda=0.2 
## + Fold5.Rep5: alpha=0.5556, lambda=0.2 
## - Fold5.Rep5: alpha=0.5556, lambda=0.2 
## + Fold5.Rep5: alpha=0.6667, lambda=0.2 
## - Fold5.Rep5: alpha=0.6667, lambda=0.2 
## + Fold5.Rep5: alpha=0.7778, lambda=0.2 
## - Fold5.Rep5: alpha=0.7778, lambda=0.2 
## + Fold5.Rep5: alpha=0.8889, lambda=0.2 
## - Fold5.Rep5: alpha=0.8889, lambda=0.2 
## + Fold5.Rep5: alpha=1.0000, lambda=0.2 
## - Fold5.Rep5: alpha=1.0000, lambda=0.2
## Warning in nominalTrainWorkflow(x = x, y = y, wts = weights, info = trainInfo,
## : There were missing values in resampled performance measures.
## Aggregating results
## Selecting tuning parameters
## Fitting alpha = 0.778, lambda = 0.1 on full training set
print(en_b)
## glmnet 
## 
##  9 samples
## 15 predictors
## 
## No pre-processing
## Resampling: Cross-Validated (5 fold, repeated 5 times) 
## Summary of sample sizes: 7, 8, 7, 7, 7, 7, ... 
## Resampling results across tuning parameters:
## 
##   alpha      lambda    RMSE       Rsquared  MAE      
##   0.0000000  0.000100  0.3240204  1         0.2853706
##   0.0000000  0.050075  0.3240204  1         0.2853706
##   0.0000000  0.100050  0.3240204  1         0.2853706
##   0.0000000  0.150025  0.3240204  1         0.2853706
##   0.0000000  0.200000  0.3240204  1         0.2853706
##   0.1111111  0.000100  0.2819756  1         0.2672011
##   0.1111111  0.050075  0.2811649  1         0.2640328
##   0.1111111  0.100050  0.2805939  1         0.2592482
##   0.1111111  0.150025  0.2780674  1         0.2558157
##   0.1111111  0.200000  0.2752600  1         0.2522081
##   0.2222222  0.000100  0.2809041  1         0.2657937
##   0.2222222  0.050075  0.2771808  1         0.2572174
##   0.2222222  0.100050  0.2664525  1         0.2448157
##   0.2222222  0.150025  0.2586974  1         0.2378401
##   0.2222222  0.200000  0.2544444  1         0.2329466
##   0.3333333  0.000100  0.2782128  1         0.2624318
##   0.3333333  0.050075  0.2696229  1         0.2468498
##   0.3333333  0.100050  0.2542299  1         0.2328748
##   0.3333333  0.150025  0.2479350  1         0.2248910
##   0.3333333  0.200000  0.2485499  1         0.2240723
##   0.4444444  0.000100  0.2774664  1         0.2573549
##   0.4444444  0.050075  0.2624569  1         0.2372101
##   0.4444444  0.100050  0.2479621  1         0.2260924
##   0.4444444  0.150025  0.2402050  1         0.2168968
##   0.4444444  0.200000  0.2486434  1         0.2198841
##   0.5555556  0.000100  0.2816538  1         0.2617566
##   0.5555556  0.050075  0.2659894  1         0.2406232
##   0.5555556  0.100050  0.2415642  1         0.2192756
##   0.5555556  0.150025  0.2389819  1         0.2133765
##   0.5555556  0.200000  0.2578007  1         0.2264976
##   0.6666667  0.000100  0.2890458  1         0.2703562
##   0.6666667  0.050075  0.2747661  1         0.2468887
##   0.6666667  0.100050  0.2361213  1         0.2125531
##   0.6666667  0.150025  0.2489030  1         0.2208648
##   0.6666667  0.200000  0.2735498  1         0.2400684
##   0.7777778  0.000100  0.2966698  1         0.2758776
##   0.7777778  0.050075  0.2681299  1         0.2416270
##   0.7777778  0.100050  0.2338884  1         0.2096062
##   0.7777778  0.150025  0.2634257  1         0.2329960
##   0.7777778  0.200000  0.2903918  1         0.2557196
##   0.8888889  0.000100  0.2978482  1         0.2764814
##   0.8888889  0.050075  0.2641560  1         0.2385991
##   0.8888889  0.100050  0.2449850  1         0.2194059
##   0.8888889  0.150025  0.2795829  1         0.2466691
##   0.8888889  0.200000  0.2986662  1         0.2635765
##   1.0000000  0.000100  0.2977368  1         0.2746219
##   1.0000000  0.050075  0.2634028  1         0.2380975
##   1.0000000  0.100050  0.2584236  1         0.2307093
##   1.0000000  0.150025  0.2883897  1         0.2549802
##   1.0000000  0.200000  0.3034788  1         0.2681929
## 
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were alpha = 0.7777778 and lambda = 0.10005.
plot(varImp(en_b, scale = TRUE))

# Coefficients
coefs <- coef(en_b$finalModel, en_b$bestTune$lambda)
coefs.zero <- coefs[which(coefs == 0),]
coef_order <- order(abs(coefs), decreasing = TRUE)
head(as.matrix(coefs[coef_order,]), 10)
##                          [,1]
## (Intercept)         1.3215079
## Avg_Cobble         -0.5761352
## Avg_Gravel          0.2752391
## Avg_Sand            0.2086779
## Avg_Flow            0.0000000
## Flow_Variance       0.0000000
## Avg_Boulder         0.0000000
## Avg_Bedrock         0.0000000
## Avg_Organic         0.0000000
## Mean_Slope_Degrees  0.0000000
### --- DRIFT BIOMASS MODEL --- ###
drinet <- merged[,-c(1,2,4,5)]
train <- drinet[split_ind == 1,]
test <- drinet[split_ind == 2,]

set.seed(1234)
en_d <- train(Drift ~ .,
              train,
              method = 'glmnet',
              tuneGrid = expand.grid(alpha = seq(0, 1, length = 10),
                                     lambda = seq(0.0001, 0.2, length = 5)),
              trControl = custom)
## + Fold1.Rep1: alpha=0.0000, lambda=0.2 
## - Fold1.Rep1: alpha=0.0000, lambda=0.2 
## + Fold1.Rep1: alpha=0.1111, lambda=0.2 
## - Fold1.Rep1: alpha=0.1111, lambda=0.2 
## + Fold1.Rep1: alpha=0.2222, lambda=0.2 
## - Fold1.Rep1: alpha=0.2222, lambda=0.2 
## + Fold1.Rep1: alpha=0.3333, lambda=0.2 
## - Fold1.Rep1: alpha=0.3333, lambda=0.2 
## + Fold1.Rep1: alpha=0.4444, lambda=0.2 
## - Fold1.Rep1: alpha=0.4444, lambda=0.2 
## + Fold1.Rep1: alpha=0.5556, lambda=0.2 
## - Fold1.Rep1: alpha=0.5556, lambda=0.2 
## + Fold1.Rep1: alpha=0.6667, lambda=0.2 
## - Fold1.Rep1: alpha=0.6667, lambda=0.2 
## + Fold1.Rep1: alpha=0.7778, lambda=0.2 
## - Fold1.Rep1: alpha=0.7778, lambda=0.2 
## + Fold1.Rep1: alpha=0.8889, lambda=0.2 
## - Fold1.Rep1: alpha=0.8889, lambda=0.2 
## + Fold1.Rep1: alpha=1.0000, lambda=0.2 
## - Fold1.Rep1: alpha=1.0000, lambda=0.2 
## + Fold2.Rep1: alpha=0.0000, lambda=0.2 
## - Fold2.Rep1: alpha=0.0000, lambda=0.2 
## + Fold2.Rep1: alpha=0.1111, lambda=0.2 
## - Fold2.Rep1: alpha=0.1111, lambda=0.2 
## + Fold2.Rep1: alpha=0.2222, lambda=0.2 
## - Fold2.Rep1: alpha=0.2222, lambda=0.2 
## + Fold2.Rep1: alpha=0.3333, lambda=0.2 
## - Fold2.Rep1: alpha=0.3333, lambda=0.2 
## + Fold2.Rep1: alpha=0.4444, lambda=0.2 
## - Fold2.Rep1: alpha=0.4444, lambda=0.2 
## + Fold2.Rep1: alpha=0.5556, lambda=0.2 
## - Fold2.Rep1: alpha=0.5556, lambda=0.2 
## + Fold2.Rep1: alpha=0.6667, lambda=0.2 
## - Fold2.Rep1: alpha=0.6667, lambda=0.2 
## + Fold2.Rep1: alpha=0.7778, lambda=0.2 
## - Fold2.Rep1: alpha=0.7778, lambda=0.2 
## + Fold2.Rep1: alpha=0.8889, lambda=0.2 
## - Fold2.Rep1: alpha=0.8889, lambda=0.2 
## + Fold2.Rep1: alpha=1.0000, lambda=0.2 
## - Fold2.Rep1: alpha=1.0000, lambda=0.2 
## + Fold3.Rep1: alpha=0.0000, lambda=0.2 
## - Fold3.Rep1: alpha=0.0000, lambda=0.2 
## + Fold3.Rep1: alpha=0.1111, lambda=0.2 
## - Fold3.Rep1: alpha=0.1111, lambda=0.2 
## + Fold3.Rep1: alpha=0.2222, lambda=0.2 
## - Fold3.Rep1: alpha=0.2222, lambda=0.2 
## + Fold3.Rep1: alpha=0.3333, lambda=0.2 
## - Fold3.Rep1: alpha=0.3333, lambda=0.2 
## + Fold3.Rep1: alpha=0.4444, lambda=0.2 
## - Fold3.Rep1: alpha=0.4444, lambda=0.2 
## + Fold3.Rep1: alpha=0.5556, lambda=0.2 
## - Fold3.Rep1: alpha=0.5556, lambda=0.2 
## + Fold3.Rep1: alpha=0.6667, lambda=0.2 
## - Fold3.Rep1: alpha=0.6667, lambda=0.2 
## + Fold3.Rep1: alpha=0.7778, lambda=0.2 
## - Fold3.Rep1: alpha=0.7778, lambda=0.2 
## + Fold3.Rep1: alpha=0.8889, lambda=0.2 
## - Fold3.Rep1: alpha=0.8889, lambda=0.2 
## + Fold3.Rep1: alpha=1.0000, lambda=0.2 
## - Fold3.Rep1: alpha=1.0000, lambda=0.2 
## + Fold4.Rep1: alpha=0.0000, lambda=0.2 
## - Fold4.Rep1: alpha=0.0000, lambda=0.2 
## + Fold4.Rep1: alpha=0.1111, lambda=0.2 
## - Fold4.Rep1: alpha=0.1111, lambda=0.2 
## + Fold4.Rep1: alpha=0.2222, lambda=0.2 
## - Fold4.Rep1: alpha=0.2222, lambda=0.2 
## + Fold4.Rep1: alpha=0.3333, lambda=0.2 
## - Fold4.Rep1: alpha=0.3333, lambda=0.2 
## + Fold4.Rep1: alpha=0.4444, lambda=0.2 
## - Fold4.Rep1: alpha=0.4444, lambda=0.2 
## + Fold4.Rep1: alpha=0.5556, lambda=0.2 
## - Fold4.Rep1: alpha=0.5556, lambda=0.2 
## + Fold4.Rep1: alpha=0.6667, lambda=0.2 
## - Fold4.Rep1: alpha=0.6667, lambda=0.2 
## + Fold4.Rep1: alpha=0.7778, lambda=0.2 
## - Fold4.Rep1: alpha=0.7778, lambda=0.2 
## + Fold4.Rep1: alpha=0.8889, lambda=0.2 
## - Fold4.Rep1: alpha=0.8889, lambda=0.2 
## + Fold4.Rep1: alpha=1.0000, lambda=0.2 
## - Fold4.Rep1: alpha=1.0000, lambda=0.2 
## + Fold5.Rep1: alpha=0.0000, lambda=0.2 
## - Fold5.Rep1: alpha=0.0000, lambda=0.2 
## + Fold5.Rep1: alpha=0.1111, lambda=0.2 
## - Fold5.Rep1: alpha=0.1111, lambda=0.2 
## + Fold5.Rep1: alpha=0.2222, lambda=0.2 
## - Fold5.Rep1: alpha=0.2222, lambda=0.2 
## + Fold5.Rep1: alpha=0.3333, lambda=0.2 
## - Fold5.Rep1: alpha=0.3333, lambda=0.2 
## + Fold5.Rep1: alpha=0.4444, lambda=0.2 
## - Fold5.Rep1: alpha=0.4444, lambda=0.2 
## + Fold5.Rep1: alpha=0.5556, lambda=0.2 
## - Fold5.Rep1: alpha=0.5556, lambda=0.2 
## + Fold5.Rep1: alpha=0.6667, lambda=0.2 
## - Fold5.Rep1: alpha=0.6667, lambda=0.2 
## + Fold5.Rep1: alpha=0.7778, lambda=0.2 
## - Fold5.Rep1: alpha=0.7778, lambda=0.2 
## + Fold5.Rep1: alpha=0.8889, lambda=0.2 
## - Fold5.Rep1: alpha=0.8889, lambda=0.2 
## + Fold5.Rep1: alpha=1.0000, lambda=0.2 
## - Fold5.Rep1: alpha=1.0000, lambda=0.2 
## + Fold1.Rep2: alpha=0.0000, lambda=0.2 
## - Fold1.Rep2: alpha=0.0000, lambda=0.2 
## + Fold1.Rep2: alpha=0.1111, lambda=0.2 
## - Fold1.Rep2: alpha=0.1111, lambda=0.2 
## + Fold1.Rep2: alpha=0.2222, lambda=0.2 
## - Fold1.Rep2: alpha=0.2222, lambda=0.2 
## + Fold1.Rep2: alpha=0.3333, lambda=0.2 
## - Fold1.Rep2: alpha=0.3333, lambda=0.2 
## + Fold1.Rep2: alpha=0.4444, lambda=0.2 
## - Fold1.Rep2: alpha=0.4444, lambda=0.2 
## + Fold1.Rep2: alpha=0.5556, lambda=0.2 
## - Fold1.Rep2: alpha=0.5556, lambda=0.2 
## + Fold1.Rep2: alpha=0.6667, lambda=0.2 
## - Fold1.Rep2: alpha=0.6667, lambda=0.2 
## + Fold1.Rep2: alpha=0.7778, lambda=0.2 
## - Fold1.Rep2: alpha=0.7778, lambda=0.2 
## + Fold1.Rep2: alpha=0.8889, lambda=0.2 
## - Fold1.Rep2: alpha=0.8889, lambda=0.2 
## + Fold1.Rep2: alpha=1.0000, lambda=0.2 
## - Fold1.Rep2: alpha=1.0000, lambda=0.2 
## + Fold2.Rep2: alpha=0.0000, lambda=0.2 
## - Fold2.Rep2: alpha=0.0000, lambda=0.2 
## + Fold2.Rep2: alpha=0.1111, lambda=0.2 
## - Fold2.Rep2: alpha=0.1111, lambda=0.2 
## + Fold2.Rep2: alpha=0.2222, lambda=0.2 
## - Fold2.Rep2: alpha=0.2222, lambda=0.2 
## + Fold2.Rep2: alpha=0.3333, lambda=0.2 
## - Fold2.Rep2: alpha=0.3333, lambda=0.2 
## + Fold2.Rep2: alpha=0.4444, lambda=0.2 
## - Fold2.Rep2: alpha=0.4444, lambda=0.2 
## + Fold2.Rep2: alpha=0.5556, lambda=0.2 
## - Fold2.Rep2: alpha=0.5556, lambda=0.2 
## + Fold2.Rep2: alpha=0.6667, lambda=0.2 
## - Fold2.Rep2: alpha=0.6667, lambda=0.2 
## + Fold2.Rep2: alpha=0.7778, lambda=0.2 
## - Fold2.Rep2: alpha=0.7778, lambda=0.2 
## + Fold2.Rep2: alpha=0.8889, lambda=0.2 
## - Fold2.Rep2: alpha=0.8889, lambda=0.2 
## + Fold2.Rep2: alpha=1.0000, lambda=0.2 
## - Fold2.Rep2: alpha=1.0000, lambda=0.2 
## + Fold3.Rep2: alpha=0.0000, lambda=0.2 
## - Fold3.Rep2: alpha=0.0000, lambda=0.2 
## + Fold3.Rep2: alpha=0.1111, lambda=0.2 
## - Fold3.Rep2: alpha=0.1111, lambda=0.2 
## + Fold3.Rep2: alpha=0.2222, lambda=0.2 
## - Fold3.Rep2: alpha=0.2222, lambda=0.2 
## + Fold3.Rep2: alpha=0.3333, lambda=0.2 
## - Fold3.Rep2: alpha=0.3333, lambda=0.2 
## + Fold3.Rep2: alpha=0.4444, lambda=0.2 
## - Fold3.Rep2: alpha=0.4444, lambda=0.2 
## + Fold3.Rep2: alpha=0.5556, lambda=0.2 
## - Fold3.Rep2: alpha=0.5556, lambda=0.2 
## + Fold3.Rep2: alpha=0.6667, lambda=0.2 
## - Fold3.Rep2: alpha=0.6667, lambda=0.2 
## + Fold3.Rep2: alpha=0.7778, lambda=0.2 
## - Fold3.Rep2: alpha=0.7778, lambda=0.2 
## + Fold3.Rep2: alpha=0.8889, lambda=0.2 
## - Fold3.Rep2: alpha=0.8889, lambda=0.2 
## + Fold3.Rep2: alpha=1.0000, lambda=0.2 
## - Fold3.Rep2: alpha=1.0000, lambda=0.2 
## + Fold4.Rep2: alpha=0.0000, lambda=0.2 
## - Fold4.Rep2: alpha=0.0000, lambda=0.2 
## + Fold4.Rep2: alpha=0.1111, lambda=0.2 
## - Fold4.Rep2: alpha=0.1111, lambda=0.2 
## + Fold4.Rep2: alpha=0.2222, lambda=0.2 
## - Fold4.Rep2: alpha=0.2222, lambda=0.2 
## + Fold4.Rep2: alpha=0.3333, lambda=0.2 
## - Fold4.Rep2: alpha=0.3333, lambda=0.2 
## + Fold4.Rep2: alpha=0.4444, lambda=0.2 
## - Fold4.Rep2: alpha=0.4444, lambda=0.2 
## + Fold4.Rep2: alpha=0.5556, lambda=0.2 
## - Fold4.Rep2: alpha=0.5556, lambda=0.2 
## + Fold4.Rep2: alpha=0.6667, lambda=0.2 
## - Fold4.Rep2: alpha=0.6667, lambda=0.2 
## + Fold4.Rep2: alpha=0.7778, lambda=0.2 
## - Fold4.Rep2: alpha=0.7778, lambda=0.2 
## + Fold4.Rep2: alpha=0.8889, lambda=0.2 
## - Fold4.Rep2: alpha=0.8889, lambda=0.2 
## + Fold4.Rep2: alpha=1.0000, lambda=0.2 
## - Fold4.Rep2: alpha=1.0000, lambda=0.2 
## + Fold5.Rep2: alpha=0.0000, lambda=0.2 
## - Fold5.Rep2: alpha=0.0000, lambda=0.2 
## + Fold5.Rep2: alpha=0.1111, lambda=0.2 
## - Fold5.Rep2: alpha=0.1111, lambda=0.2 
## + Fold5.Rep2: alpha=0.2222, lambda=0.2 
## - Fold5.Rep2: alpha=0.2222, lambda=0.2 
## + Fold5.Rep2: alpha=0.3333, lambda=0.2 
## - Fold5.Rep2: alpha=0.3333, lambda=0.2 
## + Fold5.Rep2: alpha=0.4444, lambda=0.2 
## - Fold5.Rep2: alpha=0.4444, lambda=0.2 
## + Fold5.Rep2: alpha=0.5556, lambda=0.2 
## - Fold5.Rep2: alpha=0.5556, lambda=0.2 
## + Fold5.Rep2: alpha=0.6667, lambda=0.2 
## - Fold5.Rep2: alpha=0.6667, lambda=0.2 
## + Fold5.Rep2: alpha=0.7778, lambda=0.2 
## - Fold5.Rep2: alpha=0.7778, lambda=0.2 
## + Fold5.Rep2: alpha=0.8889, lambda=0.2 
## - Fold5.Rep2: alpha=0.8889, lambda=0.2 
## + Fold5.Rep2: alpha=1.0000, lambda=0.2 
## - Fold5.Rep2: alpha=1.0000, lambda=0.2 
## + Fold1.Rep3: alpha=0.0000, lambda=0.2 
## - Fold1.Rep3: alpha=0.0000, lambda=0.2 
## + Fold1.Rep3: alpha=0.1111, lambda=0.2 
## - Fold1.Rep3: alpha=0.1111, lambda=0.2 
## + Fold1.Rep3: alpha=0.2222, lambda=0.2 
## - Fold1.Rep3: alpha=0.2222, lambda=0.2 
## + Fold1.Rep3: alpha=0.3333, lambda=0.2 
## - Fold1.Rep3: alpha=0.3333, lambda=0.2 
## + Fold1.Rep3: alpha=0.4444, lambda=0.2 
## - Fold1.Rep3: alpha=0.4444, lambda=0.2 
## + Fold1.Rep3: alpha=0.5556, lambda=0.2 
## - Fold1.Rep3: alpha=0.5556, lambda=0.2 
## + Fold1.Rep3: alpha=0.6667, lambda=0.2 
## - Fold1.Rep3: alpha=0.6667, lambda=0.2 
## + Fold1.Rep3: alpha=0.7778, lambda=0.2 
## - Fold1.Rep3: alpha=0.7778, lambda=0.2 
## + Fold1.Rep3: alpha=0.8889, lambda=0.2 
## - Fold1.Rep3: alpha=0.8889, lambda=0.2 
## + Fold1.Rep3: alpha=1.0000, lambda=0.2 
## - Fold1.Rep3: alpha=1.0000, lambda=0.2 
## + Fold2.Rep3: alpha=0.0000, lambda=0.2 
## - Fold2.Rep3: alpha=0.0000, lambda=0.2 
## + Fold2.Rep3: alpha=0.1111, lambda=0.2 
## - Fold2.Rep3: alpha=0.1111, lambda=0.2 
## + Fold2.Rep3: alpha=0.2222, lambda=0.2 
## - Fold2.Rep3: alpha=0.2222, lambda=0.2 
## + Fold2.Rep3: alpha=0.3333, lambda=0.2 
## - Fold2.Rep3: alpha=0.3333, lambda=0.2 
## + Fold2.Rep3: alpha=0.4444, lambda=0.2 
## - Fold2.Rep3: alpha=0.4444, lambda=0.2 
## + Fold2.Rep3: alpha=0.5556, lambda=0.2 
## - Fold2.Rep3: alpha=0.5556, lambda=0.2 
## + Fold2.Rep3: alpha=0.6667, lambda=0.2 
## - Fold2.Rep3: alpha=0.6667, lambda=0.2 
## + Fold2.Rep3: alpha=0.7778, lambda=0.2 
## - Fold2.Rep3: alpha=0.7778, lambda=0.2 
## + Fold2.Rep3: alpha=0.8889, lambda=0.2 
## - Fold2.Rep3: alpha=0.8889, lambda=0.2 
## + Fold2.Rep3: alpha=1.0000, lambda=0.2 
## - Fold2.Rep3: alpha=1.0000, lambda=0.2 
## + Fold3.Rep3: alpha=0.0000, lambda=0.2 
## - Fold3.Rep3: alpha=0.0000, lambda=0.2 
## + Fold3.Rep3: alpha=0.1111, lambda=0.2 
## - Fold3.Rep3: alpha=0.1111, lambda=0.2 
## + Fold3.Rep3: alpha=0.2222, lambda=0.2 
## - Fold3.Rep3: alpha=0.2222, lambda=0.2 
## + Fold3.Rep3: alpha=0.3333, lambda=0.2 
## - Fold3.Rep3: alpha=0.3333, lambda=0.2 
## + Fold3.Rep3: alpha=0.4444, lambda=0.2 
## - Fold3.Rep3: alpha=0.4444, lambda=0.2 
## + Fold3.Rep3: alpha=0.5556, lambda=0.2 
## - Fold3.Rep3: alpha=0.5556, lambda=0.2 
## + Fold3.Rep3: alpha=0.6667, lambda=0.2 
## - Fold3.Rep3: alpha=0.6667, lambda=0.2 
## + Fold3.Rep3: alpha=0.7778, lambda=0.2 
## - Fold3.Rep3: alpha=0.7778, lambda=0.2 
## + Fold3.Rep3: alpha=0.8889, lambda=0.2 
## - Fold3.Rep3: alpha=0.8889, lambda=0.2 
## + Fold3.Rep3: alpha=1.0000, lambda=0.2 
## - Fold3.Rep3: alpha=1.0000, lambda=0.2 
## + Fold4.Rep3: alpha=0.0000, lambda=0.2 
## - Fold4.Rep3: alpha=0.0000, lambda=0.2 
## + Fold4.Rep3: alpha=0.1111, lambda=0.2 
## - Fold4.Rep3: alpha=0.1111, lambda=0.2 
## + Fold4.Rep3: alpha=0.2222, lambda=0.2 
## - Fold4.Rep3: alpha=0.2222, lambda=0.2 
## + Fold4.Rep3: alpha=0.3333, lambda=0.2 
## - Fold4.Rep3: alpha=0.3333, lambda=0.2 
## + Fold4.Rep3: alpha=0.4444, lambda=0.2 
## - Fold4.Rep3: alpha=0.4444, lambda=0.2 
## + Fold4.Rep3: alpha=0.5556, lambda=0.2 
## - Fold4.Rep3: alpha=0.5556, lambda=0.2 
## + Fold4.Rep3: alpha=0.6667, lambda=0.2 
## - Fold4.Rep3: alpha=0.6667, lambda=0.2 
## + Fold4.Rep3: alpha=0.7778, lambda=0.2 
## - Fold4.Rep3: alpha=0.7778, lambda=0.2 
## + Fold4.Rep3: alpha=0.8889, lambda=0.2 
## - Fold4.Rep3: alpha=0.8889, lambda=0.2 
## + Fold4.Rep3: alpha=1.0000, lambda=0.2 
## - Fold4.Rep3: alpha=1.0000, lambda=0.2 
## + Fold5.Rep3: alpha=0.0000, lambda=0.2 
## - Fold5.Rep3: alpha=0.0000, lambda=0.2 
## + Fold5.Rep3: alpha=0.1111, lambda=0.2 
## - Fold5.Rep3: alpha=0.1111, lambda=0.2 
## + Fold5.Rep3: alpha=0.2222, lambda=0.2 
## - Fold5.Rep3: alpha=0.2222, lambda=0.2 
## + Fold5.Rep3: alpha=0.3333, lambda=0.2 
## - Fold5.Rep3: alpha=0.3333, lambda=0.2 
## + Fold5.Rep3: alpha=0.4444, lambda=0.2 
## - Fold5.Rep3: alpha=0.4444, lambda=0.2 
## + Fold5.Rep3: alpha=0.5556, lambda=0.2 
## - Fold5.Rep3: alpha=0.5556, lambda=0.2 
## + Fold5.Rep3: alpha=0.6667, lambda=0.2 
## - Fold5.Rep3: alpha=0.6667, lambda=0.2 
## + Fold5.Rep3: alpha=0.7778, lambda=0.2 
## - Fold5.Rep3: alpha=0.7778, lambda=0.2 
## + Fold5.Rep3: alpha=0.8889, lambda=0.2 
## - Fold5.Rep3: alpha=0.8889, lambda=0.2 
## + Fold5.Rep3: alpha=1.0000, lambda=0.2 
## - Fold5.Rep3: alpha=1.0000, lambda=0.2 
## + Fold1.Rep4: alpha=0.0000, lambda=0.2 
## - Fold1.Rep4: alpha=0.0000, lambda=0.2 
## + Fold1.Rep4: alpha=0.1111, lambda=0.2 
## - Fold1.Rep4: alpha=0.1111, lambda=0.2 
## + Fold1.Rep4: alpha=0.2222, lambda=0.2 
## - Fold1.Rep4: alpha=0.2222, lambda=0.2 
## + Fold1.Rep4: alpha=0.3333, lambda=0.2 
## - Fold1.Rep4: alpha=0.3333, lambda=0.2 
## + Fold1.Rep4: alpha=0.4444, lambda=0.2 
## - Fold1.Rep4: alpha=0.4444, lambda=0.2 
## + Fold1.Rep4: alpha=0.5556, lambda=0.2 
## - Fold1.Rep4: alpha=0.5556, lambda=0.2 
## + Fold1.Rep4: alpha=0.6667, lambda=0.2 
## - Fold1.Rep4: alpha=0.6667, lambda=0.2 
## + Fold1.Rep4: alpha=0.7778, lambda=0.2 
## - Fold1.Rep4: alpha=0.7778, lambda=0.2 
## + Fold1.Rep4: alpha=0.8889, lambda=0.2 
## - Fold1.Rep4: alpha=0.8889, lambda=0.2 
## + Fold1.Rep4: alpha=1.0000, lambda=0.2 
## - Fold1.Rep4: alpha=1.0000, lambda=0.2 
## + Fold2.Rep4: alpha=0.0000, lambda=0.2 
## - Fold2.Rep4: alpha=0.0000, lambda=0.2 
## + Fold2.Rep4: alpha=0.1111, lambda=0.2 
## - Fold2.Rep4: alpha=0.1111, lambda=0.2 
## + Fold2.Rep4: alpha=0.2222, lambda=0.2 
## - Fold2.Rep4: alpha=0.2222, lambda=0.2 
## + Fold2.Rep4: alpha=0.3333, lambda=0.2 
## - Fold2.Rep4: alpha=0.3333, lambda=0.2 
## + Fold2.Rep4: alpha=0.4444, lambda=0.2 
## - Fold2.Rep4: alpha=0.4444, lambda=0.2 
## + Fold2.Rep4: alpha=0.5556, lambda=0.2 
## - Fold2.Rep4: alpha=0.5556, lambda=0.2 
## + Fold2.Rep4: alpha=0.6667, lambda=0.2 
## - Fold2.Rep4: alpha=0.6667, lambda=0.2 
## + Fold2.Rep4: alpha=0.7778, lambda=0.2 
## - Fold2.Rep4: alpha=0.7778, lambda=0.2 
## + Fold2.Rep4: alpha=0.8889, lambda=0.2 
## - Fold2.Rep4: alpha=0.8889, lambda=0.2 
## + Fold2.Rep4: alpha=1.0000, lambda=0.2 
## - Fold2.Rep4: alpha=1.0000, lambda=0.2 
## + Fold3.Rep4: alpha=0.0000, lambda=0.2 
## - Fold3.Rep4: alpha=0.0000, lambda=0.2 
## + Fold3.Rep4: alpha=0.1111, lambda=0.2 
## - Fold3.Rep4: alpha=0.1111, lambda=0.2 
## + Fold3.Rep4: alpha=0.2222, lambda=0.2 
## - Fold3.Rep4: alpha=0.2222, lambda=0.2 
## + Fold3.Rep4: alpha=0.3333, lambda=0.2 
## - Fold3.Rep4: alpha=0.3333, lambda=0.2 
## + Fold3.Rep4: alpha=0.4444, lambda=0.2 
## - Fold3.Rep4: alpha=0.4444, lambda=0.2 
## + Fold3.Rep4: alpha=0.5556, lambda=0.2 
## - Fold3.Rep4: alpha=0.5556, lambda=0.2 
## + Fold3.Rep4: alpha=0.6667, lambda=0.2 
## - Fold3.Rep4: alpha=0.6667, lambda=0.2 
## + Fold3.Rep4: alpha=0.7778, lambda=0.2 
## - Fold3.Rep4: alpha=0.7778, lambda=0.2 
## + Fold3.Rep4: alpha=0.8889, lambda=0.2 
## - Fold3.Rep4: alpha=0.8889, lambda=0.2 
## + Fold3.Rep4: alpha=1.0000, lambda=0.2 
## - Fold3.Rep4: alpha=1.0000, lambda=0.2 
## + Fold4.Rep4: alpha=0.0000, lambda=0.2 
## - Fold4.Rep4: alpha=0.0000, lambda=0.2 
## + Fold4.Rep4: alpha=0.1111, lambda=0.2 
## - Fold4.Rep4: alpha=0.1111, lambda=0.2 
## + Fold4.Rep4: alpha=0.2222, lambda=0.2 
## - Fold4.Rep4: alpha=0.2222, lambda=0.2 
## + Fold4.Rep4: alpha=0.3333, lambda=0.2 
## - Fold4.Rep4: alpha=0.3333, lambda=0.2 
## + Fold4.Rep4: alpha=0.4444, lambda=0.2 
## - Fold4.Rep4: alpha=0.4444, lambda=0.2 
## + Fold4.Rep4: alpha=0.5556, lambda=0.2 
## - Fold4.Rep4: alpha=0.5556, lambda=0.2 
## + Fold4.Rep4: alpha=0.6667, lambda=0.2 
## - Fold4.Rep4: alpha=0.6667, lambda=0.2 
## + Fold4.Rep4: alpha=0.7778, lambda=0.2 
## - Fold4.Rep4: alpha=0.7778, lambda=0.2 
## + Fold4.Rep4: alpha=0.8889, lambda=0.2 
## - Fold4.Rep4: alpha=0.8889, lambda=0.2 
## + Fold4.Rep4: alpha=1.0000, lambda=0.2 
## - Fold4.Rep4: alpha=1.0000, lambda=0.2 
## + Fold5.Rep4: alpha=0.0000, lambda=0.2 
## - Fold5.Rep4: alpha=0.0000, lambda=0.2 
## + Fold5.Rep4: alpha=0.1111, lambda=0.2 
## - Fold5.Rep4: alpha=0.1111, lambda=0.2 
## + Fold5.Rep4: alpha=0.2222, lambda=0.2 
## - Fold5.Rep4: alpha=0.2222, lambda=0.2 
## + Fold5.Rep4: alpha=0.3333, lambda=0.2 
## - Fold5.Rep4: alpha=0.3333, lambda=0.2 
## + Fold5.Rep4: alpha=0.4444, lambda=0.2 
## - Fold5.Rep4: alpha=0.4444, lambda=0.2 
## + Fold5.Rep4: alpha=0.5556, lambda=0.2 
## - Fold5.Rep4: alpha=0.5556, lambda=0.2 
## + Fold5.Rep4: alpha=0.6667, lambda=0.2 
## - Fold5.Rep4: alpha=0.6667, lambda=0.2 
## + Fold5.Rep4: alpha=0.7778, lambda=0.2 
## - Fold5.Rep4: alpha=0.7778, lambda=0.2 
## + Fold5.Rep4: alpha=0.8889, lambda=0.2 
## - Fold5.Rep4: alpha=0.8889, lambda=0.2 
## + Fold5.Rep4: alpha=1.0000, lambda=0.2 
## - Fold5.Rep4: alpha=1.0000, lambda=0.2 
## + Fold1.Rep5: alpha=0.0000, lambda=0.2 
## - Fold1.Rep5: alpha=0.0000, lambda=0.2 
## + Fold1.Rep5: alpha=0.1111, lambda=0.2 
## - Fold1.Rep5: alpha=0.1111, lambda=0.2 
## + Fold1.Rep5: alpha=0.2222, lambda=0.2 
## - Fold1.Rep5: alpha=0.2222, lambda=0.2 
## + Fold1.Rep5: alpha=0.3333, lambda=0.2 
## - Fold1.Rep5: alpha=0.3333, lambda=0.2 
## + Fold1.Rep5: alpha=0.4444, lambda=0.2 
## - Fold1.Rep5: alpha=0.4444, lambda=0.2 
## + Fold1.Rep5: alpha=0.5556, lambda=0.2 
## - Fold1.Rep5: alpha=0.5556, lambda=0.2 
## + Fold1.Rep5: alpha=0.6667, lambda=0.2 
## - Fold1.Rep5: alpha=0.6667, lambda=0.2 
## + Fold1.Rep5: alpha=0.7778, lambda=0.2 
## - Fold1.Rep5: alpha=0.7778, lambda=0.2 
## + Fold1.Rep5: alpha=0.8889, lambda=0.2 
## - Fold1.Rep5: alpha=0.8889, lambda=0.2 
## + Fold1.Rep5: alpha=1.0000, lambda=0.2 
## - Fold1.Rep5: alpha=1.0000, lambda=0.2 
## + Fold2.Rep5: alpha=0.0000, lambda=0.2 
## - Fold2.Rep5: alpha=0.0000, lambda=0.2 
## + Fold2.Rep5: alpha=0.1111, lambda=0.2 
## - Fold2.Rep5: alpha=0.1111, lambda=0.2 
## + Fold2.Rep5: alpha=0.2222, lambda=0.2 
## - Fold2.Rep5: alpha=0.2222, lambda=0.2 
## + Fold2.Rep5: alpha=0.3333, lambda=0.2 
## - Fold2.Rep5: alpha=0.3333, lambda=0.2 
## + Fold2.Rep5: alpha=0.4444, lambda=0.2 
## - Fold2.Rep5: alpha=0.4444, lambda=0.2 
## + Fold2.Rep5: alpha=0.5556, lambda=0.2 
## - Fold2.Rep5: alpha=0.5556, lambda=0.2 
## + Fold2.Rep5: alpha=0.6667, lambda=0.2 
## - Fold2.Rep5: alpha=0.6667, lambda=0.2 
## + Fold2.Rep5: alpha=0.7778, lambda=0.2 
## - Fold2.Rep5: alpha=0.7778, lambda=0.2 
## + Fold2.Rep5: alpha=0.8889, lambda=0.2 
## - Fold2.Rep5: alpha=0.8889, lambda=0.2 
## + Fold2.Rep5: alpha=1.0000, lambda=0.2 
## - Fold2.Rep5: alpha=1.0000, lambda=0.2 
## + Fold3.Rep5: alpha=0.0000, lambda=0.2 
## - Fold3.Rep5: alpha=0.0000, lambda=0.2 
## + Fold3.Rep5: alpha=0.1111, lambda=0.2 
## - Fold3.Rep5: alpha=0.1111, lambda=0.2 
## + Fold3.Rep5: alpha=0.2222, lambda=0.2 
## - Fold3.Rep5: alpha=0.2222, lambda=0.2 
## + Fold3.Rep5: alpha=0.3333, lambda=0.2 
## - Fold3.Rep5: alpha=0.3333, lambda=0.2 
## + Fold3.Rep5: alpha=0.4444, lambda=0.2 
## - Fold3.Rep5: alpha=0.4444, lambda=0.2 
## + Fold3.Rep5: alpha=0.5556, lambda=0.2 
## - Fold3.Rep5: alpha=0.5556, lambda=0.2 
## + Fold3.Rep5: alpha=0.6667, lambda=0.2 
## - Fold3.Rep5: alpha=0.6667, lambda=0.2 
## + Fold3.Rep5: alpha=0.7778, lambda=0.2 
## - Fold3.Rep5: alpha=0.7778, lambda=0.2 
## + Fold3.Rep5: alpha=0.8889, lambda=0.2 
## - Fold3.Rep5: alpha=0.8889, lambda=0.2 
## + Fold3.Rep5: alpha=1.0000, lambda=0.2 
## - Fold3.Rep5: alpha=1.0000, lambda=0.2 
## + Fold4.Rep5: alpha=0.0000, lambda=0.2 
## - Fold4.Rep5: alpha=0.0000, lambda=0.2 
## + Fold4.Rep5: alpha=0.1111, lambda=0.2 
## - Fold4.Rep5: alpha=0.1111, lambda=0.2 
## + Fold4.Rep5: alpha=0.2222, lambda=0.2 
## - Fold4.Rep5: alpha=0.2222, lambda=0.2 
## + Fold4.Rep5: alpha=0.3333, lambda=0.2 
## - Fold4.Rep5: alpha=0.3333, lambda=0.2 
## + Fold4.Rep5: alpha=0.4444, lambda=0.2 
## - Fold4.Rep5: alpha=0.4444, lambda=0.2 
## + Fold4.Rep5: alpha=0.5556, lambda=0.2 
## - Fold4.Rep5: alpha=0.5556, lambda=0.2 
## + Fold4.Rep5: alpha=0.6667, lambda=0.2 
## - Fold4.Rep5: alpha=0.6667, lambda=0.2 
## + Fold4.Rep5: alpha=0.7778, lambda=0.2 
## - Fold4.Rep5: alpha=0.7778, lambda=0.2 
## + Fold4.Rep5: alpha=0.8889, lambda=0.2 
## - Fold4.Rep5: alpha=0.8889, lambda=0.2 
## + Fold4.Rep5: alpha=1.0000, lambda=0.2 
## - Fold4.Rep5: alpha=1.0000, lambda=0.2 
## + Fold5.Rep5: alpha=0.0000, lambda=0.2 
## - Fold5.Rep5: alpha=0.0000, lambda=0.2 
## + Fold5.Rep5: alpha=0.1111, lambda=0.2 
## - Fold5.Rep5: alpha=0.1111, lambda=0.2 
## + Fold5.Rep5: alpha=0.2222, lambda=0.2 
## - Fold5.Rep5: alpha=0.2222, lambda=0.2 
## + Fold5.Rep5: alpha=0.3333, lambda=0.2 
## - Fold5.Rep5: alpha=0.3333, lambda=0.2 
## + Fold5.Rep5: alpha=0.4444, lambda=0.2 
## - Fold5.Rep5: alpha=0.4444, lambda=0.2 
## + Fold5.Rep5: alpha=0.5556, lambda=0.2 
## - Fold5.Rep5: alpha=0.5556, lambda=0.2 
## + Fold5.Rep5: alpha=0.6667, lambda=0.2 
## - Fold5.Rep5: alpha=0.6667, lambda=0.2 
## + Fold5.Rep5: alpha=0.7778, lambda=0.2 
## - Fold5.Rep5: alpha=0.7778, lambda=0.2 
## + Fold5.Rep5: alpha=0.8889, lambda=0.2 
## - Fold5.Rep5: alpha=0.8889, lambda=0.2 
## + Fold5.Rep5: alpha=1.0000, lambda=0.2 
## - Fold5.Rep5: alpha=1.0000, lambda=0.2
## Warning in nominalTrainWorkflow(x = x, y = y, wts = weights, info = trainInfo,
## : There were missing values in resampled performance measures.
## Aggregating results
## Selecting tuning parameters
## Fitting alpha = 1, lambda = 0.1 on full training set
print(en_d)
## glmnet 
## 
##  9 samples
## 15 predictors
## 
## No pre-processing
## Resampling: Cross-Validated (5 fold, repeated 5 times) 
## Summary of sample sizes: 7, 8, 7, 7, 7, 7, ... 
## Resampling results across tuning parameters:
## 
##   alpha      lambda    RMSE       Rsquared  MAE      
##   0.0000000  0.000100  0.3018466  1         0.2621042
##   0.0000000  0.050075  0.3018466  1         0.2621042
##   0.0000000  0.100050  0.3018466  1         0.2621042
##   0.0000000  0.150025  0.3018466  1         0.2621042
##   0.0000000  0.200000  0.3018466  1         0.2621042
##   0.1111111  0.000100  0.4079107  1         0.3693623
##   0.1111111  0.050075  0.3853183  1         0.3467703
##   0.1111111  0.100050  0.3580800  1         0.3188674
##   0.1111111  0.150025  0.3396780  1         0.3000203
##   0.1111111  0.200000  0.3279803  1         0.2898342
##   0.2222222  0.000100  0.4158578  1         0.3781358
##   0.2222222  0.050075  0.3683127  1         0.3318370
##   0.2222222  0.100050  0.3340651  1         0.2976492
##   0.2222222  0.150025  0.3165231  1         0.2811197
##   0.2222222  0.200000  0.3066727  1         0.2695501
##   0.3333333  0.000100  0.4210263  1         0.3847318
##   0.3333333  0.050075  0.3470938  1         0.3118891
##   0.3333333  0.100050  0.3114716  1         0.2770359
##   0.3333333  0.150025  0.3023396  1         0.2674801
##   0.3333333  0.200000  0.2967204  1         0.2615725
##   0.4444444  0.000100  0.4273850  1         0.3924805
##   0.4444444  0.050075  0.3261126  1         0.2936150
##   0.4444444  0.100050  0.2962810  1         0.2635316
##   0.4444444  0.150025  0.2892884  1         0.2570612
##   0.4444444  0.200000  0.2899346  1         0.2566599
##   0.5555556  0.000100  0.4308227  1         0.3979816
##   0.5555556  0.050075  0.3053717  1         0.2750192
##   0.5555556  0.100050  0.2834180  1         0.2529481
##   0.5555556  0.150025  0.2826094  1         0.2512320
##   0.5555556  0.200000  0.2838240  1         0.2505237
##   0.6666667  0.000100  0.4317863  1         0.3986584
##   0.6666667  0.050075  0.2909196  1         0.2582465
##   0.6666667  0.100050  0.2728236  1         0.2434354
##   0.6666667  0.150025  0.2745284  1         0.2429465
##   0.6666667  0.200000  0.2768853  1         0.2469416
##   0.7777778  0.000100  0.4385506  1         0.4042271
##   0.7777778  0.050075  0.2759798  1         0.2402731
##   0.7777778  0.100050  0.2596748  1         0.2320422
##   0.7777778  0.150025  0.2645247  1         0.2360092
##   0.7777778  0.200000  0.2667466  1         0.2381834
##   0.8888889  0.000100  0.4469232  1         0.4104420
##   0.8888889  0.050075  0.2761240  1         0.2449359
##   0.8888889  0.100050  0.2480228  1         0.2211811
##   0.8888889  0.150025  0.2568940  1         0.2310975
##   0.8888889  0.200000  0.2653948  1         0.2367666
##   1.0000000  0.000100  0.4602056  1         0.4220002
##   1.0000000  0.050075  0.2663598  1         0.2354491
##   1.0000000  0.100050  0.2405888  1         0.2152169
##   1.0000000  0.150025  0.2522479  1         0.2272323
##   1.0000000  0.200000  0.2673058  1         0.2381457
## 
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were alpha = 1 and lambda = 0.10005.
plot(varImp(en_d, scale = TRUE))

# Coefficients
coefs <- coef(en_d$finalModel, en_d$bestTune$lambda)
coefs.zero <- coefs[which(coefs == 0),]
coef_order <- order(abs(coefs), decreasing = TRUE)
head(as.matrix(coefs[coef_order,]), 10)
##                         [,1]
## (Intercept)        0.9962072
## Avg_Sand           0.6980398
## Avg_Flow           0.0000000
## Flow_Variance      0.0000000
## Avg_Gravel         0.0000000
## Avg_Cobble         0.0000000
## Avg_Boulder        0.0000000
## Avg_Bedrock        0.0000000
## Avg_Organic        0.0000000
## Mean_Slope_Degrees 0.0000000
#RDA analysis

rda_env <- as.data.frame(env_scaled[,-c(1,3)])
rda_env1 <- lapply(rda_env, as.numeric)
rda_env <- na.omit(rda_env)
rda_biomass <- as.data.frame(for_clust[, -c(1)])

cor_matrix_env <- cor(rda_env)
corrplot(cor_matrix_env,
         method = "color",  # Color-based visualization
         type = "upper",  # Display upper triangle to avoid redundancy
         tl.col = "black",  # Text label color
         tl.srt = 45,  # Rotate text labels for better readability
         col = colorRampPalette(c("blue", "white", "red"))(200),  # Color scale (blue for negative, red for positive)
         addCoef.col = "black",  # Add correlation coefficients on the plot
         diag = FALSE)  # Remove the diagonal as it's always 1 (perfect correlation)

#Removing, total length, reach length, distance to headwaters, and boulder to improve model due to collinearity

rda_result1 <- rda(rda_biomass ~ Avg_Flow + Avg_Sand + Avg_Cobble + Avg_Gravel + Avg_Bedrock + Avg_Organic + Mean_Slope_Degrees +  Dist_Pour + Dist_Head + Relative_position + Percent_occupied, data = rda_env1, grouping = rda_env$Basin)


summary(rda_result1)
## 
## Call:
## rda(formula = rda_biomass ~ Avg_Flow + Avg_Sand + Avg_Cobble +      Avg_Gravel + Avg_Bedrock + Avg_Organic + Mean_Slope_Degrees +      Dist_Pour + Dist_Head + Relative_position + Percent_occupied,      data = rda_env1, grouping = rda_env$Basin) 
## 
## Partitioning of variance:
##               Inertia Proportion
## Total          0.3573     1.0000
## Constrained    0.2097     0.5869
## Unconstrained  0.1476     0.4131
## 
## Eigenvalues, and their contribution to the variance 
## 
## Importance of components:
##                         RDA1    RDA2    RDA3     RDA4     PC1     PC2      PC3
## Eigenvalue            0.1228 0.06855 0.01180 0.006549 0.07579 0.06204 0.008995
## Proportion Explained  0.3437 0.19185 0.03303 0.018328 0.21209 0.17361 0.025173
## Cumulative Proportion 0.3437 0.53558 0.56861 0.586938 0.79903 0.97264 0.997812
##                             PC4
## Eigenvalue            0.0007818
## Proportion Explained  0.0021880
## Cumulative Proportion 1.0000000
## 
## Accumulated constrained eigenvalues
## Importance of components:
##                         RDA1    RDA2    RDA3     RDA4
## Eigenvalue            0.1228 0.06855 0.01180 0.006549
## Proportion Explained  0.5856 0.32686 0.05627 0.031227
## Cumulative Proportion 0.5856 0.91250 0.96877 1.000000
# Get the summary of the RDA results
rda_summary1 <- summary(rda_result1)

# Access the scores for each variable (explained by RDA1 and RDA2)
explanatory_scores1 <- rda_summary1$species
biplot_scores1 <- scores(rda_result1, display = "bp")
# View the RDA1 and RDA2 scores for each variable
print(explanatory_scores1)
##                  RDA1      RDA2         RDA3          RDA4        PC1
## Benthic    -0.4483168 0.3695983 -0.196402365 -0.0009572963 -0.0145337
## Drift      -0.5373464 0.2376426  0.195353557 -0.0566125905 -0.3072169
## Fish        0.5352608 0.3514003 -0.002373459 -0.1345227378 -0.3818513
## Periphyton  0.2663705 0.3953242  0.068297590  0.1545028399 -0.5313528
##                    PC2
## Benthic    -0.39791675
## Drift      -0.42622368
## Fish       -0.04881877
## Periphyton  0.29240054
rda_result1$CCA$biplot
##                           RDA1       RDA2        RDA3         RDA4
## Avg_Flow           -0.03170125  0.6536434 -0.02107042 -0.301932192
## Avg_Sand           -0.40469023  0.1696484 -0.30244201 -0.229385145
## Avg_Cobble          0.53051217 -0.1861885  0.54395708 -0.175714888
## Avg_Gravel         -0.35884343  0.4993419 -0.21560312 -0.058080944
## Avg_Bedrock        -0.17224580 -0.5107016 -0.12999230  0.026879231
## Avg_Organic        -0.23401655 -0.1583259 -0.07028935  0.075607200
## Mean_Slope_Degrees  0.06155451 -0.5004119 -0.31513246  0.004100659
## Dist_Pour           0.06234654  0.3056049  0.56239915 -0.560803136
## Dist_Head           0.31421881  0.6199239 -0.05376866 -0.413030874
## Relative_position   0.20381983  0.1236047 -0.47819552  0.329960873
## Percent_occupied   -0.40788546 -0.4472868 -0.11181006  0.518950245
rda_result1$CCA$v
##                  RDA1      RDA2         RDA3         RDA4
## Benthic    -0.4870747 0.5374929 -0.688359933 -0.004504069
## Drift      -0.5838010 0.3455946  0.684684024 -0.266361625
## Fish        0.5815351 0.5110283 -0.008318606 -0.632928024
## Periphyton  0.2893987 0.5749051  0.239372495  0.726934188
rda_result1$CCA$u
##              RDA1         RDA2       RDA3        RDA4
## row1  -0.21138899 -0.344484529 -0.2472420 -0.07658550
## row2   0.18459751 -0.077433964  0.1011064  0.06256028
## row3   0.37064035 -0.077292946 -0.1982373 -0.22955477
## row4   0.13380192 -0.183502520  0.3797046 -0.13651876
## row5  -0.14775774  0.422597736  0.2234676 -0.21407699
## row6   0.12918815  0.140144711  0.2297294 -0.15864812
## row7   0.23410422 -0.045563572 -0.3042305 -0.39776854
## row8   0.28502672  0.179747948 -0.2566591  0.40737299
## row9   0.07388433  0.149794017 -0.1388560  0.25698915
## row10 -0.03490215 -0.395146190  0.1589141  0.47016520
## row11  0.27354363 -0.002961474 -0.1513152  0.26696058
## row12 -0.08851214  0.185897238  0.3538624  0.18855209
## row13 -0.05272083 -0.070477938  0.3268264  0.02380564
## row14 -0.64361898 -0.089689993 -0.1825884  0.10298529
## row15 -0.07546313  0.108310282  0.1206975 -0.17643623
## row16 -0.04822695 -0.040638259  0.0707420 -0.18444929
## row17 -0.14651964 -0.350146864 -0.1605113 -0.22993585
## row18 -0.23567627  0.490846317 -0.3254105  0.02458285
#reduce model further/explore if reduction is possible

sqrt(vif.cca(rda_result1)) 
##           Avg_Flow           Avg_Sand         Avg_Cobble         Avg_Gravel 
##           3.752504           1.883210           3.865884           3.119525 
##        Avg_Bedrock        Avg_Organic Mean_Slope_Degrees          Dist_Pour 
##           2.237205           2.201307           1.710499           4.828406 
##          Dist_Head  Relative_position   Percent_occupied 
##           6.228138           4.965958           2.818134
anova.cca(rda_result1)#global RDA significance
## Permutation test for rda under reduced model
## Permutation: free
## Number of permutations: 999
## 
## Model: rda(formula = rda_biomass ~ Avg_Flow + Avg_Sand + Avg_Cobble + Avg_Gravel + Avg_Bedrock + Avg_Organic + Mean_Slope_Degrees + Dist_Pour + Dist_Head + Relative_position + Percent_occupied, data = rda_env1, grouping = rda_env$Basin)
##          Df Variance      F Pr(>F)
## Model    11  0.20973 0.7751  0.742
## Residual  6  0.14760
anova.cca(rda_result1, by = "axis") # significance of axes
## Permutation test for rda under reduced model
## Forward tests for axes
## Permutation: free
## Number of permutations: 999
## 
## Model: rda(formula = rda_biomass ~ Avg_Flow + Avg_Sand + Avg_Cobble + Avg_Gravel + Avg_Bedrock + Avg_Organic + Mean_Slope_Degrees + Dist_Pour + Dist_Head + Relative_position + Percent_occupied, data = rda_env1, grouping = rda_env$Basin)
##          Df Variance       F Pr(>F)
## RDA1      1 0.122826 10.8180  0.687
## RDA2      1 0.068553  6.0379  0.942
## RDA3      1 0.011802  1.0395  1.000
## RDA4      1 0.006549  0.5768       
## Residual 13 0.147599
anova.cca(rda_result1, by = "terms") #significance of terms
## Permutation test for rda under reduced model
## Terms added sequentially (first to last)
## Permutation: free
## Number of permutations: 999
## 
## Model: rda(formula = rda_biomass ~ Avg_Flow + Avg_Sand + Avg_Cobble + Avg_Gravel + Avg_Bedrock + Avg_Organic + Mean_Slope_Degrees + Dist_Pour + Dist_Head + Relative_position + Percent_occupied, data = rda_env1, grouping = rda_env$Basin)
##                    Df Variance      F Pr(>F)
## Avg_Flow            1 0.030015 1.2201  0.348
## Avg_Sand            1 0.027126 1.1027  0.368
## Avg_Cobble          1 0.018410 0.7484  0.527
## Avg_Gravel          1 0.013705 0.5571  0.641
## Avg_Bedrock         1 0.013967 0.5678  0.639
## Avg_Organic         1 0.021678 0.8812  0.460
## Mean_Slope_Degrees  1 0.010148 0.4125  0.774
## Dist_Pour           1 0.005825 0.2368  0.888
## Dist_Head           1 0.040541 1.6480  0.213
## Relative_position   1 0.009013 0.3664  0.787
## Percent_occupied    1 0.019302 0.7846  0.518
## Residual            6 0.147599
#Trying ordistep() which uses p-value as a stopping criterion
step.forward <- ordistep(rda(rda_biomass ~ 1, data=rda_env1), scope=formula(rda_result1), direction="forward", pstep=1000)
## 
## Start: rda_biomass ~ 1 
## 
##                      Df     AIC      F Pr(>F)
## + Avg_Cobble          1 -17.726 2.0532  0.130
## + Dist_Head           1 -17.668 1.9955  0.145
## + Percent_occupied    1 -17.468 1.7959  0.150
## + Avg_Gravel          1 -17.323 1.6541  0.185
## + Avg_Flow            1 -17.132 1.4672  0.230
## + Avg_Sand            1 -16.778 1.1270  0.350
## + Avg_Bedrock         1 -16.682 1.0359  0.390
## + Mean_Slope_Degrees  1 -16.526 0.8887  0.395
## + Dist_Pour           1 -16.203 0.5883  0.630
## + Relative_position   1 -16.041 0.4399  0.750
## + Avg_Organic         1 -15.988 0.3918  0.755
# Define basin colors (you can adjust this list based on your actual basin names)
site_colors <- c(
  "AA1_REACH" = "darkorange3", "AA2_REACH" = "darkorange4", 
  "AC_REACH" = "slateblue3", "AP_REACH" = "darkcyan", 
  "AZ_REACH" = "gold3", "BA1_REACH" = "firebrick3", 
  "BA2_REACH" = "green4", "BC_REACH" = "purple", 
  "BZ_REACH" = "darkgreen", "CA1_REACH" = "darkolivegreen4", 
  "CA2_REACH" = "mediumblue", "CP_REACH" = "yellowgreen", 
  "CZ2_REACH" = "darkred", "DA1_REACH" = "seagreen", 
  "DA2_REACH" = "indianred", "DC_REACH" = "orangered", 
  "DP_REACH" = "chocolate", "DZ_REACH" = "violet"
)

# Vector of colors for each site based on  bAsin
colors <- sapply(env_scaled$Basin, function(Basin) site_colors[Basin])

# Extract site coordinates (coordinates of site scores)
site_scores1 <- rda_result1$CCA$u

par(mar = c(5, 5, 2, 7))

# Plot RDA result, scaling 2 (displaying effects of explanatory variables)
plot(rda_result1, xlim = c(-1, 1), ylim = c(-1,1), scaling = 2, main = "RDA - Scaling = 2", type = "n") 

# Plot species labels
text(rda_result1, display = "species", col = "firebrick3", cex = 1)

# Plot sites with colors corresponding to basins
points(site_scores1, col = colors, pch = 20, cex = 1.)

# Plot biplot arrows (explanatory variables)
text(rda_result1, scaling = 2, display = "bp", col = "darkblue", cex = 1)

# Add the legend to the plot, positioned outside
legend("right", legend = names(site_colors), col = site_colors, pch = 19, title = "Basin", 
       inset = -0.2, xpd = TRUE, cex = 0.7)

rda_result2 <- rda(rda_biomass ~ Avg_Flow + Mean_Slope_Degrees +  Dist_Pour + Dist_Head + Relative_position + Percent_occupied, data = rda_env1, grouping = rda_env$Basin)

summary(rda_result2)
## 
## Call:
## rda(formula = rda_biomass ~ Avg_Flow + Mean_Slope_Degrees + Dist_Pour +      Dist_Head + Relative_position + Percent_occupied, data = rda_env1,      grouping = rda_env$Basin) 
## 
## Partitioning of variance:
##               Inertia Proportion
## Total          0.3573     1.0000
## Constrained    0.1271     0.3557
## Unconstrained  0.2302     0.6443
## 
## Eigenvalues, and their contribution to the variance 
## 
## Importance of components:
##                          RDA1    RDA2     RDA3     RDA4    PC1     PC2     PC3
## Eigenvalue            0.07456 0.04206 0.006889 0.003577 0.1082 0.09487 0.01840
## Proportion Explained  0.20866 0.11772 0.019280 0.010010 0.3027 0.26551 0.05149
## Cumulative Proportion 0.20866 0.32638 0.345660 0.355670 0.6583 0.92384 0.97533
##                            PC4
## Eigenvalue            0.008814
## Proportion Explained  0.024665
## Cumulative Proportion 1.000000
## 
## Accumulated constrained eigenvalues
## Importance of components:
##                          RDA1    RDA2     RDA3     RDA4
## Eigenvalue            0.07456 0.04206 0.006889 0.003577
## Proportion Explained  0.58667 0.33098 0.054209 0.028145
## Cumulative Proportion 0.58667 0.91765 0.971855 1.000000
# Get the summary of the RDA results
rda_summary2 <- summary(rda_result2)

# Access the scores for each variable (explained by RDA1 and RDA2)
explanatory_scores2 <- rda_summary2$species
biplot_scores2 <- scores(rda_result2, display = "bp")
# View the RDA1 and RDA2 scores for each variable
print(explanatory_scores2)
##                  RDA1       RDA2        RDA3         RDA4        PC1       PC2
## Benthic     0.2860379 -0.2653565 -0.16828029  0.007521157 -0.4006270 0.4179902
## Drift       0.4401563 -0.2415147  0.13429969  0.032453225 -0.2465405 0.5466802
## Fish       -0.4523481 -0.2887791  0.01108955  0.087751864  0.3887168 0.2930666
## Periphyton -0.1846746 -0.2792890  0.03228372 -0.125943466  0.6112154 0.3081027
rda_result2$CCA$biplot
##                            RDA1       RDA2        RDA3       RDA4
## Avg_Flow            0.008001701 -0.8388008 -0.20142998  0.1938876
## Mean_Slope_Degrees -0.068603785  0.6525343 -0.24143783  0.1980816
## Dist_Pour          -0.080189772 -0.4273673  0.70977831  0.5391080
## Dist_Head          -0.434572225 -0.7746998 -0.09516631  0.2561819
## Relative_position  -0.277630207 -0.1094919 -0.60495568 -0.4696942
## Percent_occupied    0.542384037  0.5609244 -0.21239479 -0.4029779
rda_result2$CCA$v
##                  RDA1       RDA2        RDA3       RDA4
## Benthic     0.3988635 -0.4926365 -0.77196139  0.0478832
## Drift       0.6137728 -0.4483740  0.61608034  0.2066124
## Fish       -0.6307735 -0.5361207  0.05087168  0.5586693
## Periphyton -0.2575182 -0.5185023  0.14809689 -0.8018149
rda_result2$CCA$u
##               RDA1        RDA2        RDA3         RDA4
## row1  -0.194362619  0.29471347 -0.01829024 -0.285462452
## row2  -0.125291289  0.20671338  0.03528298  0.234165395
## row3  -0.221996348  0.15826910  0.06081694  0.248893946
## row4  -0.186136729 -0.09304450  0.44740051 -0.075706044
## row5   0.150712346 -0.48837164  0.18766781  0.140916994
## row6   0.053073880  0.05654218  0.01997357 -0.038398959
## row7  -0.435438370 -0.12111428 -0.40048615  0.245345080
## row8  -0.373076868  0.23822194 -0.08452013 -0.212036968
## row9  -0.073447035 -0.08346520  0.10484241 -0.293917641
## row10  0.282077401  0.33524618  0.03829178 -0.383200287
## row11 -0.017363776 -0.23730407 -0.46473935 -0.272604529
## row12  0.047659926 -0.23030157  0.22056040 -0.215222743
## row13  0.009060125  0.07272707  0.20260956  0.185595112
## row14  0.591781168  0.17743083 -0.25255673 -0.003898403
## row15  0.088546291 -0.12132158  0.33247063  0.120749864
## row16  0.158488290 -0.05951718 -0.05081201  0.061520118
## row17  0.183285800  0.28955038 -0.07806656  0.511688624
## row18  0.062427809 -0.39497452 -0.30044542  0.031572892
#reduce model further/explore if reduction is possible

sqrt(vif.cca(rda_result2)) 
##           Avg_Flow Mean_Slope_Degrees          Dist_Pour          Dist_Head 
##           2.676554           1.664104           3.811689           4.984668 
##  Relative_position   Percent_occupied 
##           3.925248           2.423043
anova.cca(rda_result2)#global RDA significance
## Permutation test for rda under reduced model
## Permutation: free
## Number of permutations: 999
## 
## Model: rda(formula = rda_biomass ~ Avg_Flow + Mean_Slope_Degrees + Dist_Pour + Dist_Head + Relative_position + Percent_occupied, data = rda_env1, grouping = rda_env$Basin)
##          Df Variance     F Pr(>F)
## Model     6  0.12709 1.012   0.47
## Residual 11  0.23024
anova.cca(rda_result2, by = "axis") # significance of axes
## Permutation test for rda under reduced model
## Forward tests for axes
## Permutation: free
## Number of permutations: 999
## 
## Model: rda(formula = rda_biomass ~ Avg_Flow + Mean_Slope_Degrees + Dist_Pour + Dist_Head + Relative_position + Percent_occupied, data = rda_env1, grouping = rda_env$Basin)
##          Df Variance      F Pr(>F)
## RDA1      1 0.074560 4.2099  0.498
## RDA2      1 0.042065 2.3751  0.828
## RDA3      1 0.006889 0.3890  1.000
## RDA4      1 0.003577 0.2020       
## Residual 13 0.230238
anova.cca(rda_result2, by = "terms") #significance of terms
## Permutation test for rda under reduced model
## Terms added sequentially (first to last)
## Permutation: free
## Number of permutations: 999
## 
## Model: rda(formula = rda_biomass ~ Avg_Flow + Mean_Slope_Degrees + Dist_Pour + Dist_Head + Relative_position + Percent_occupied, data = rda_env1, grouping = rda_env$Basin)
##                    Df Variance      F Pr(>F)  
## Avg_Flow            1 0.030015 1.4340  0.236  
## Mean_Slope_Degrees  1 0.006484 0.3098  0.830  
## Dist_Pour           1 0.006464 0.3088  0.837  
## Dist_Head           1 0.065564 3.1324  0.040 *
## Relative_position   1 0.003510 0.1677  0.937  
## Percent_occupied    1 0.015055 0.7193  0.531  
## Residual           11 0.230238                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Trying ordistep() which uses p-value as a stopping criterion
step.forward <- ordistep(rda(rda_biomass ~ 1, data=rda_env1), scope=formula(rda_result2), direction="forward", pstep=1000)
## 
## Start: rda_biomass ~ 1 
## 
##                      Df     AIC      F Pr(>F)
## + Dist_Head           1 -17.668 1.9955  0.140
## + Percent_occupied    1 -17.468 1.7959  0.140
## + Avg_Flow            1 -17.132 1.4672  0.240
## + Mean_Slope_Degrees  1 -16.526 0.8887  0.430
## + Dist_Pour           1 -16.203 0.5883  0.635
## + Relative_position   1 -16.041 0.4399  0.730
# Extract site coordinates (coordinates of site scores)
site_scores2 <- rda_result2$CCA$u

par(mar = c(5, 5, 2, 7))

# Plot RDA result, scaling 2 (displaying effects of explanatory variables)
plot(rda_result2, xlim = c(-1, 1), ylim = c(-1,1), scaling = 2, main = "RDA - Scaling = 2", type = "n") 

# Plot species labels
text(rda_result2, display = "species", col = "firebrick3", cex = 1)

# Plot sites with colors corresponding to basins
points(site_scores2, col = colors, pch = 20, cex = 1.)

# Plot biplot arrows (explanatory variables)
text(rda_result2, scaling = 2, display = "bp", col = "darkblue", cex = 1)

# Add the legend to the plot, positioned outside
legend("right", legend = names(site_colors), col = site_colors, pch = 19, title = "Basin", 
       inset = -0.2, xpd = TRUE, cex = 0.7)

# Define response and predictor variables
response_var <- colnames(merged)[2:5]  # Columns 2, 3, 4, and 5 are response variables
env_var <- colnames(merged)[6:20]      # Columns 6 to 20 are environmental variables

# Empty data frame to store results
results_table <- data.frame(Response_Variable = character(),
                            Predictor_Variable = character(),
                            Coefficient = numeric(),
                            Std_Error = numeric(),
                            t_value = numeric(),
                            p_value = numeric(),
                            RSquared = numeric(),  # Add a column for R^2
                            stringsAsFactors = FALSE)

# Perform regression analysis for each response variable and predictor variable
for (response in response_var) {
  for (env in env_var) {
    model <- lm(merged[[response]] ~ merged[[env]])
    
    # Results stored in the table
    results_table <- rbind(results_table, data.frame(Response_Variable = response,
                                                     Predictor_Variable = env,
                                                     Coefficient = coef(model)[2],
                                                     Std_Error = summary(model)$coefficients[2, 2],
                                                     t_value = summary(model)$coefficients[2, 3],
                                                     p_value = summary(model)$coefficients[2, 4],
                                                     RSquared = summary(model)$r.squared))
  }
}

# Filter significant results (p-value < 0.05)
significant_results <- results_table[results_table$p_value < 0.05, ]

# Highlighting the table based on conditions
library(DT)
## Warning: package 'DT' was built under R version 4.4.2
# Hide column names
rownames(results_table) <- NULL

# Highlight R^2 > 0.3 and p-values < 0.05
datatable(results_table, options = list(
  columnDefs = list(
    list(
      targets = 7,  # R_squared column index
      render = JS("function(data, type, row, meta) {",
                  "if (parseFloat(data) > 0.3) {",
                  "return '<span style=\"color: red;\">' + data + '</span>';",
                  "} else {",
                  "return data;}",
                  "}")
    ),
    list(
      targets = 6,  # p_value column index
      render = JS("function(data, type, row, meta) {",
                  "if (parseFloat(data) < 0.05) {",
                  "return '<span style=\"color: blue;\">' + data + '</span>';",
                  "} else {",
                  "return data;}",
                  "}")
    )
  ),
  dom = 'tip',
  pageLength = 10,
  lengthMenu = c(10, 20, 50),
  scrollX = TRUE,
  width = "80%"  
)) 
FFG_df <- benthic_absummary %>% filter(Feeding.Group != "PA")

# Remove the '%' sign and convert the column to numeric
FFG_df$Relative_Abundance <- as.numeric(gsub("%", "", FFG_df$Relative_Abundance)) / 100

FFG_df <- FFG_df %>%
  group_by(Basin, Feeding.Group) %>%
  summarise(Relative_Abundance = sum(Relative_Abundance), .groups = "drop")


df_wide <- FFG_df %>%
  pivot_wider(names_from = Feeding.Group, values_from = Relative_Abundance, values_fill = 0)

expected_cols <- c("DSH", "HSH", "CF", "CG", "PR", "MH", "OM", "SC")
df_wide[setdiff(expected_cols, names(df_wide))] <- 0

df_wide <- df_wide %>%
  mutate(
    PR = ifelse((DSH + CG + CF) == 0, NA, (SC + HSH) / (DSH + CG + CF)),
    Plants_Algae = ifelse(SC == 0, NA, HSH / SC),
    CPOM_FPOM = ifelse(CG + CF == 0, NA, DSH / (CG + CF)),
    FPOM_CPOM = ifelse(CG == 0, NA, CF / CG),
    Channel_Stability = ifelse((DSH + CG) == 0, NA, (SC + HSH + CF) / (DSH + CG)),
    Predator_Control = ifelse((SC + DSH + HSH + CF + CG) == 0, NA, PR / (SC + DSH + HSH + CF + CG))
  )

df_wide <- df_wide %>%
  mutate(
    PR_Flag = case_when(PR > 0.75 ~ "Autotrophic (Above 0.75)", TRUE ~ "Heterotrophic (Below 0.75)"),
    Plants_Algae_Flag = case_when(Plants_Algae > 0.50 ~ "Vascular Plants Dominant (Above 0.50)", TRUE ~ "Algae Dominant (Below 0.50)"),
    CPOM_FPOM_Flag = case_when(CPOM_FPOM > 0.50 ~ "High Shredder Dependency (Above 0.25)", TRUE ~ "Low Shredder Dependency (Below 0.25)"),
    FPOM_CPOM_Flag = case_when(FPOM_CPOM > 0.50 ~ "High FPOM Transport (Above 0.50)", TRUE ~ "Normal FPOM (Below 0.50)"),
    Channel_Stability_Flag = case_when(Channel_Stability > 0.50 ~ "Stable Channel (Above 0.50)", TRUE ~ "Unstable Channel (Below 0.50)"),
    Predator_Control_Flag = case_when(Predator_Control > 0.50 ~ "Predator Dominated (Above 0.50)", TRUE ~ "Prey Dominated (Below 0.50)")
  )

# Create a concise table with flagging results
df_flags <- df_wide %>%
  select(Basin, PR_Flag, Plants_Algae_Flag, CPOM_FPOM_Flag, FPOM_CPOM_Flag, Channel_Stability_Flag, Predator_Control_Flag)

# View the table
print(df_flags)
## # A tibble: 20 × 7
##    Basin     PR_Flag             Plants_Algae_Flag CPOM_FPOM_Flag FPOM_CPOM_Flag
##    <chr>     <chr>               <chr>             <chr>          <chr>         
##  1 AA1_REACH Heterotrophic (Bel… Algae Dominant (… High Shredder… Normal FPOM (…
##  2 AA2_REACH Heterotrophic (Bel… Algae Dominant (… Low Shredder … Normal FPOM (…
##  3 AC_REACH  Heterotrophic (Bel… Algae Dominant (… Low Shredder … Normal FPOM (…
##  4 AP_REACH  Autotrophic (Above… Algae Dominant (… Low Shredder … Normal FPOM (…
##  5 AZ_REACH  Autotrophic (Above… Algae Dominant (… Low Shredder … Normal FPOM (…
##  6 BA1_REACH Heterotrophic (Bel… Algae Dominant (… Low Shredder … Normal FPOM (…
##  7 BA2_REACH Heterotrophic (Bel… Algae Dominant (… Low Shredder … High FPOM Tra…
##  8 BC_REACH  Autotrophic (Above… Algae Dominant (… Low Shredder … Normal FPOM (…
##  9 BP_REACH  Autotrophic (Above… Algae Dominant (… Low Shredder … Normal FPOM (…
## 10 BZ_REACH  Heterotrophic (Bel… Algae Dominant (… Low Shredder … Normal FPOM (…
## 11 CA1_REACH Heterotrophic (Bel… Algae Dominant (… Low Shredder … Normal FPOM (…
## 12 CA2_REACH Autotrophic (Above… Algae Dominant (… Low Shredder … Normal FPOM (…
## 13 CC_REACH  Heterotrophic (Bel… Algae Dominant (… Low Shredder … Normal FPOM (…
## 14 CP_REACH  Heterotrophic (Bel… Algae Dominant (… Low Shredder … Normal FPOM (…
## 15 CZ2_REACH Heterotrophic (Bel… Algae Dominant (… Low Shredder … Normal FPOM (…
## 16 DA1_REACH Heterotrophic (Bel… Algae Dominant (… High Shredder… Normal FPOM (…
## 17 DA2_REACH Heterotrophic (Bel… Algae Dominant (… Low Shredder … Normal FPOM (…
## 18 DC_REACH  Heterotrophic (Bel… Algae Dominant (… Low Shredder … Normal FPOM (…
## 19 DP_REACH  Heterotrophic (Bel… Algae Dominant (… Low Shredder … Normal FPOM (…
## 20 DZ_REACH  Heterotrophic (Bel… Algae Dominant (… Low Shredder … Normal FPOM (…
## # ℹ 2 more variables: Channel_Stability_Flag <chr>, Predator_Control_Flag <chr>
condition_biomass1 <- read.csv("reach_condition.csv")

# Shapiro-Wilk test for normality (for each response variable)
shapiro.test(condition_biomass1$Fish)
## 
##  Shapiro-Wilk normality test
## 
## data:  condition_biomass1$Fish
## W = 0.95456, p-value = 0.501
shapiro.test(condition_biomass1$Periphyton)
## 
##  Shapiro-Wilk normality test
## 
## data:  condition_biomass1$Periphyton
## W = 0.94668, p-value = 0.3753
shapiro.test(condition_biomass1$Benthic)
## 
##  Shapiro-Wilk normality test
## 
## data:  condition_biomass1$Benthic
## W = 0.8098, p-value = 0.002096
shapiro.test(condition_biomass1$Drift)
## 
##  Shapiro-Wilk normality test
## 
## data:  condition_biomass1$Drift
## W = 0.85601, p-value = 0.01055
condition_biomass <- condition_biomass1[, -c(1, 3, 8, 9)]

condition_biomass$PR_Flag <- as.factor(condition_biomass$PR_Flag)
condition_biomass$CPOM_FPOM_Flag <- as.factor(condition_biomass$CPOM_FPOM_Flag)
condition_biomass$FPOM_CPOM_Flag <- as.factor(condition_biomass$FPOM_CPOM_Flag)
condition_biomass$Channel_Stability_Flag <- as.factor(condition_biomass$Channel_Stability_Flag)
condition_biomass$Predator_Control_Flag <- as.factor(condition_biomass$Predator_Control_Flag)

manova1 <- manova(cbind(Fish, Periphyton, Benthic, Drift) ~ PR_Flag + CPOM_FPOM_Flag + FPOM_CPOM_Flag + Channel_Stability_Flag + Predator_Control_Flag, data = condition_biomass)

summary(manova1, test = "Pillai")
##                        Df  Pillai approx F num Df den Df Pr(>F)
## PR_Flag                 1 0.28096  0.97683      4     10 0.4622
## CPOM_FPOM_Flag          1 0.40604  1.70905      4     10 0.2240
## FPOM_CPOM_Flag          1 0.25102  0.83786      4     10 0.5315
## Channel_Stability_Flag  1 0.36633  1.44526      4     10 0.2893
## Residuals              13
#ANOVAS
summary(aov(Fish ~ PR_Flag, data = condition_biomass))
##             Df Sum Sq Mean Sq F value Pr(>F)
## PR_Flag      1 0.0978 0.09780   1.141  0.301
## Residuals   16 1.3711 0.08569               
## 2 observations deleted due to missingness
summary(aov(Periphyton ~ PR_Flag, data = condition_biomass))
##             Df Sum Sq Mean Sq F value Pr(>F)
## PR_Flag      1 0.0014 0.00139   0.014  0.907
## Residuals   16 1.5724 0.09827               
## 2 observations deleted due to missingness
summary(aov(Benthic ~ PR_Flag, data = condition_biomass))
##             Df Sum Sq Mean Sq F value Pr(>F)
## PR_Flag      1 0.1064 0.10641    1.33  0.266
## Residuals   16 1.2804 0.08003               
## 2 observations deleted due to missingness
summary(aov(Drift ~ PR_Flag, data = condition_biomass))
##             Df Sum Sq Mean Sq F value Pr(>F)
## PR_Flag      1 0.0532 0.05316   0.534  0.475
## Residuals   16 1.5920 0.09950               
## 2 observations deleted due to missingness
summary(aov(Fish ~ CPOM_FPOM_Flag, data = condition_biomass))
##                Df Sum Sq Mean Sq F value Pr(>F)  
## CPOM_FPOM_Flag  1 0.3246  0.3246   4.538  0.049 *
## Residuals      16 1.1443  0.0715                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 2 observations deleted due to missingness
summary(aov(Fish ~ FPOM_CPOM_Flag, data = condition_biomass))
##                Df Sum Sq Mean Sq F value Pr(>F)
## FPOM_CPOM_Flag  1 0.0884 0.08835   1.024  0.327
## Residuals      16 1.3805 0.08628               
## 2 observations deleted due to missingness
summary(aov(Fish ~ Channel_Stability_Flag, data = condition_biomass))
##                        Df Sum Sq Mean Sq F value  Pr(>F)   
## Channel_Stability_Flag  1 0.6298  0.6298   12.01 0.00319 **
## Residuals              16 0.8391  0.0524                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 2 observations deleted due to missingness
summary(aov(Fish ~ Predator_Control_Flag, data = condition_biomass))
##                       Df Sum Sq Mean Sq F value Pr(>F)  
## Predator_Control_Flag  1 0.4376  0.4376    6.79 0.0191 *
## Residuals             16 1.0313  0.0645                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 2 observations deleted due to missingness
summary(aov(Fish ~ Mean_Slope_Degrees, data = condition_biomass))
##                    Df Sum Sq Mean Sq F value Pr(>F)
## Mean_Slope_Degrees  1 0.0502 0.05019   0.566  0.463
## Residuals          16 1.4187 0.08867               
## 2 observations deleted due to missingness
summary(aov(Fish ~ Dist_Pour, data = condition_biomass))
##             Df Sum Sq Mean Sq F value Pr(>F)
## Dist_Pour    1 0.1138 0.11379   1.344  0.263
## Residuals   16 1.3551 0.08469               
## 2 observations deleted due to missingness
summary(aov(Fish ~ Dist_Head, data = condition_biomass))
##             Df Sum Sq Mean Sq F value Pr(>F)  
## Dist_Head    1 0.4809  0.4809   7.788 0.0131 *
## Residuals   16 0.9880  0.0617                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 2 observations deleted due to missingness
summary(aov(Fish ~ Relative_position, data = condition_biomass))
##                   Df Sum Sq Mean Sq F value Pr(>F)
## Relative_position  1 0.0294 0.02943   0.327  0.575
## Residuals         16 1.4395 0.08997               
## 2 observations deleted due to missingness
summary(aov(Fish ~ Percent_occupied, data = condition_biomass))
##                  Df Sum Sq Mean Sq F value Pr(>F)  
## Percent_occupied  1 0.4882  0.4882   7.964 0.0123 *
## Residuals        16 0.9807  0.0613                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 2 observations deleted due to missingness
summary(aov(Periphyton ~ CPOM_FPOM_Flag, data = condition_biomass))
##                Df Sum Sq Mean Sq F value Pr(>F)
## CPOM_FPOM_Flag  1 0.1421 0.14211   1.588  0.226
## Residuals      16 1.4317 0.08948               
## 2 observations deleted due to missingness
summary(aov(Periphyton ~ FPOM_CPOM_Flag, data = condition_biomass))
##                Df Sum Sq Mean Sq F value Pr(>F)
## FPOM_CPOM_Flag  1  0.000 0.00001       0  0.994
## Residuals      16  1.574 0.09836               
## 2 observations deleted due to missingness
summary(aov(Periphyton ~ Channel_Stability_Flag, data = condition_biomass))
##                        Df Sum Sq Mean Sq F value Pr(>F)
## Channel_Stability_Flag  1 0.1858 0.18585   2.142  0.163
## Residuals              16 1.3879 0.08675               
## 2 observations deleted due to missingness
summary(aov(Periphyton ~ Predator_Control_Flag, data = condition_biomass))
##                       Df Sum Sq Mean Sq F value Pr(>F)
## Predator_Control_Flag  1 0.1872 0.18718    2.16  0.161
## Residuals             16 1.3866 0.08666               
## 2 observations deleted due to missingness
summary(aov(Periphyton ~ Mean_Slope_Degrees, data = condition_biomass))
##                    Df Sum Sq Mean Sq F value Pr(>F)
## Mean_Slope_Degrees  1 0.1009 0.10088   1.096  0.311
## Residuals          16 1.4729 0.09206               
## 2 observations deleted due to missingness
summary(aov(Periphyton ~ Dist_Pour, data = condition_biomass))
##             Df Sum Sq Mean Sq F value Pr(>F)
## Dist_Pour    1 0.0196 0.01960   0.202  0.659
## Residuals   16 1.5542 0.09714               
## 2 observations deleted due to missingness
summary(aov(Periphyton ~ Dist_Head, data = condition_biomass))
##             Df Sum Sq Mean Sq F value Pr(>F)
## Dist_Head    1 0.1683 0.16826   1.915  0.185
## Residuals   16 1.4055 0.08784               
## 2 observations deleted due to missingness
summary(aov(Periphyton ~ Relative_position, data = condition_biomass))
##                   Df Sum Sq Mean Sq F value Pr(>F)
## Relative_position  1 0.0364 0.03637   0.378  0.547
## Residuals         16 1.5374 0.09609               
## 2 observations deleted due to missingness
summary(aov(Periphyton ~ Percent_occupied, data = condition_biomass))
##                  Df Sum Sq Mean Sq F value Pr(>F)
## Percent_occupied  1 0.1117 0.11174   1.223  0.285
## Residuals        16 1.4620 0.09138               
## 2 observations deleted due to missingness
summary(aov(Benthic ~ CPOM_FPOM_Flag, data = condition_biomass))
##                Df Sum Sq Mean Sq F value Pr(>F)
## CPOM_FPOM_Flag  1 0.1618 0.16185   2.114  0.165
## Residuals      16 1.2250 0.07656               
## 2 observations deleted due to missingness
summary(aov(Benthic ~ FPOM_CPOM_Flag, data = condition_biomass))
##                Df Sum Sq Mean Sq F value Pr(>F)
## FPOM_CPOM_Flag  1 0.0062 0.00621   0.072  0.792
## Residuals      16 1.3806 0.08629               
## 2 observations deleted due to missingness
summary(aov(Benthic ~ Channel_Stability_Flag, data = condition_biomass))
##                        Df Sum Sq Mean Sq F value Pr(>F)
## Channel_Stability_Flag  1 0.1929 0.19287   2.585  0.127
## Residuals              16 1.1939 0.07462               
## 2 observations deleted due to missingness
summary(aov(Benthic ~ Predator_Control_Flag, data = condition_biomass))
##                       Df Sum Sq Mean Sq F value Pr(>F)
## Predator_Control_Flag  1 0.1645  0.1645   2.153  0.162
## Residuals             16 1.2223  0.0764               
## 2 observations deleted due to missingness
summary(aov(Benthic ~ Mean_Slope_Degrees, data = condition_biomass))
##                    Df Sum Sq Mean Sq F value Pr(>F)
## Mean_Slope_Degrees  1 0.0559 0.05594   0.673  0.424
## Residuals          16 1.3309 0.08318               
## 2 observations deleted due to missingness
summary(aov(Benthic ~ Dist_Pour, data = condition_biomass))
##             Df Sum Sq Mean Sq F value Pr(>F)
## Dist_Pour    1 0.0015 0.00153   0.018  0.896
## Residuals   16 1.3853 0.08658               
## 2 observations deleted due to missingness
summary(aov(Benthic ~ Dist_Head, data = condition_biomass))
##             Df Sum Sq Mean Sq F value Pr(>F)
## Dist_Head    1 0.0243 0.02426   0.285  0.601
## Residuals   16 1.3626 0.08516               
## 2 observations deleted due to missingness
summary(aov(Benthic ~ Relative_position, data = condition_biomass))
##                   Df Sum Sq Mean Sq F value Pr(>F)
## Relative_position  1 0.0057 0.00566   0.066  0.801
## Residuals         16 1.3812 0.08632               
## 2 observations deleted due to missingness
summary(aov(Benthic ~ Percent_occupied, data = condition_biomass))
##                  Df Sum Sq Mean Sq F value Pr(>F)
## Percent_occupied  1 0.0038 0.00375   0.043  0.838
## Residuals        16 1.3831 0.08644               
## 2 observations deleted due to missingness
summary(aov(Drift ~ CPOM_FPOM_Flag, data = condition_biomass))
##                Df Sum Sq Mean Sq F value Pr(>F)
## CPOM_FPOM_Flag  1 0.2371  0.2371   2.694   0.12
## Residuals      16 1.4081  0.0880               
## 2 observations deleted due to missingness
summary(aov(Drift ~ FPOM_CPOM_Flag, data = condition_biomass))
##                Df Sum Sq Mean Sq F value Pr(>F)
## FPOM_CPOM_Flag  1 0.0712 0.07121   0.724  0.407
## Residuals      16 1.5739 0.09837               
## 2 observations deleted due to missingness
summary(aov(Drift ~ Channel_Stability_Flag, data = condition_biomass))
##                        Df Sum Sq Mean Sq F value Pr(>F)
## Channel_Stability_Flag  1 0.0605 0.06048   0.611  0.446
## Residuals              16 1.5846 0.09904               
## 2 observations deleted due to missingness
summary(aov(Drift ~ Predator_Control_Flag, data = condition_biomass))
##                       Df Sum Sq Mean Sq F value Pr(>F)
## Predator_Control_Flag  1 0.0155 0.01548   0.152  0.702
## Residuals             16 1.6296 0.10185               
## 2 observations deleted due to missingness
summary(aov(Drift ~ Mean_Slope_Degrees, data = condition_biomass))
##                    Df Sum Sq Mean Sq F value Pr(>F)
## Mean_Slope_Degrees  1 0.1127 0.11265   1.176  0.294
## Residuals          16 1.5325 0.09578               
## 2 observations deleted due to missingness
summary(aov(Drift ~ Dist_Pour, data = condition_biomass))
##             Df Sum Sq Mean Sq F value Pr(>F)
## Dist_Pour    1 0.0805 0.08051   0.823  0.378
## Residuals   16 1.5646 0.09779               
## 2 observations deleted due to missingness
summary(aov(Drift ~ Dist_Head, data = condition_biomass))
##             Df Sum Sq Mean Sq F value Pr(>F)
## Dist_Head    1 0.0002 0.00018   0.002  0.967
## Residuals   16 1.6449 0.10281               
## 2 observations deleted due to missingness
summary(aov(Drift ~ Relative_position, data = condition_biomass))
##                   Df Sum Sq Mean Sq F value Pr(>F)
## Relative_position  1 0.0911 0.09109   0.938  0.347
## Residuals         16 1.5540 0.09713               
## 2 observations deleted due to missingness
summary(aov(Drift ~ Percent_occupied, data = condition_biomass))
##                  Df Sum Sq Mean Sq F value Pr(>F)
## Percent_occupied  1 0.0094 0.00937   0.092  0.766
## Residuals        16 1.6357 0.10223               
## 2 observations deleted due to missingness
mancova <- manova(cbind(Fish, Periphyton, Benthic, Drift) ~ PR_Flag + CPOM_FPOM_Flag + FPOM_CPOM_Flag + Channel_Stability_Flag + Predator_Control_Flag + Mean_Slope_Degrees + Dist_Pour + Dist_Head + Relative_position + Percent_occupied, data = condition_biomass)

summary(mancova, test = "Pillai")
##                        Df  Pillai approx F num Df den Df Pr(>F)
## PR_Flag                 1 0.48963  1.19922      4      5 0.4139
## CPOM_FPOM_Flag          1 0.58741  1.77963      4      5 0.2699
## FPOM_CPOM_Flag          1 0.43938  0.97969      4      5 0.4938
## Channel_Stability_Flag  1 0.45069  1.02559      4      5 0.4756
## Mean_Slope_Degrees      1 0.35813  0.69745      4      5 0.6256
## Dist_Pour               1 0.33791  0.63797      4      5 0.6580
## Dist_Head               1 0.50847  1.29310      4      5 0.3847
## Relative_position       1 0.18576  0.28518      4      5 0.8761
## Percent_occupied        1 0.61406  1.98886      4      5 0.2346
## Residuals               8
#NO SIGNIFICANT RELATIONSHIPS

# Model for Fish biomass
gam_fish <- gam(Fish ~ s(Mean_Slope_Degrees, k = 10) + s(Dist_Pour, k = 10) + s(Dist_Head, k = 10) +  s(Relative_position, k= 10) + s(Percent_occupied, k=5) + PR_Flag + CPOM_FPOM_Flag +  FPOM_CPOM_Flag + Channel_Stability_Flag + Predator_Control_Flag,  data = condition_biomass, family = gaussian)

# Model for Periphyton biomass
gam_periphyton <- gam(Periphyton ~ s(Mean_Slope_Degrees, k = 5) + s(Dist_Pour, k = 5) + s(Dist_Head, k = 5) +  s(Relative_position, k= 5) + s(Percent_occupied, k=5) + PR_Flag + CPOM_FPOM_Flag + FPOM_CPOM_Flag + Channel_Stability_Flag + Predator_Control_Flag, data = condition_biomass, family = gaussian)

# Model for Benthic biomass
gam_benthic <- gam(Benthic ~ s(Mean_Slope_Degrees, k = 5) + s(Dist_Pour, k = 5) + s(Dist_Head, k = 5) +  s(Relative_position, k= 5) + s(Percent_occupied, k=5) + PR_Flag + CPOM_FPOM_Flag +  FPOM_CPOM_Flag + Channel_Stability_Flag + Predator_Control_Flag,  data = condition_biomass, family = gaussian)

# Model for Drift biomass
gam_drift <- gam(Drift ~ s(Mean_Slope_Degrees, k = 5) + s(Dist_Pour, k = 5) + s(Dist_Head, k = 5) +  s(Relative_position, k= 5) + s(Percent_occupied, k=5) + PR_Flag + CPOM_FPOM_Flag +  FPOM_CPOM_Flag + Channel_Stability_Flag + Predator_Control_Flag,  data = condition_biomass, family = gaussian)

# Checking the summaries and assumptions for each model

# Summary of the Fish biomass model
summary(gam_fish)
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## Fish ~ s(Mean_Slope_Degrees, k = 10) + s(Dist_Pour, k = 10) + 
##     s(Dist_Head, k = 10) + s(Relative_position, k = 10) + s(Percent_occupied, 
##     k = 5) + PR_Flag + CPOM_FPOM_Flag + FPOM_CPOM_Flag + Channel_Stability_Flag + 
##     Predator_Control_Flag
## 
## Parametric coefficients:
##                                                     Estimate Std. Error t value
## (Intercept)                                          1.17909    0.33168   3.555
## PR_FlagHeterotrophic (Below 0.75)                    0.01717    0.31314   0.055
## CPOM_FPOM_FlagLow Shredder Dependency (Below 0.25)   0.20747    0.24815   0.836
## FPOM_CPOM_FlagNormal FPOM (Below 0.50)               0.14992    0.14064   1.066
## Channel_Stability_FlagUnstable Channel (Below 0.50) -0.56652    0.16073  -3.525
## Predator_Control_FlagPrey Dominated (Below 0.50)     0.46265    0.20188   2.292
##                                                     Pr(>|t|)  
## (Intercept)                                           0.0258 *
## PR_FlagHeterotrophic (Below 0.75)                     0.9590  
## CPOM_FPOM_FlagLow Shredder Dependency (Below 0.25)    0.4525  
## FPOM_CPOM_FlagNormal FPOM (Below 0.50)                0.3494  
## Channel_Stability_FlagUnstable Channel (Below 0.50)   0.0265 *
## Predator_Control_FlagPrey Dominated (Below 0.50)      0.0872 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                         edf Ref.df     F p-value  
## s(Mean_Slope_Degrees) 1.000  1.000 4.819  0.0932 .
## s(Dist_Pour)          4.041  4.645 2.484  0.1865  
## s(Dist_Head)          1.000  1.000 5.280  0.0831 .
## s(Relative_position)  1.000  1.000 0.293  0.6168  
## s(Percent_occupied)   2.159  2.447 5.061  0.0805 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Rank: 45/46
## R-sq.(adj) =  0.769   Deviance explained = 94.8%
## GCV = 0.094479  Scale est. = 0.019946  n = 18
gam.check(gam_fish)

## 
## Method: GCV   Optimizer: magic
## Smoothing parameter selection converged after 31 iterations.
## The RMS GCV score gradient at convergence was 0.00000009283181 .
## The Hessian was positive definite.
## Model rank =  45 / 46 
## 
## Basis dimension (k) checking results. Low p-value (k-index<1) may
## indicate that k is too low, especially if edf is close to k'.
## 
##                         k'  edf k-index p-value
## s(Mean_Slope_Degrees) 9.00 1.00    1.46    0.96
## s(Dist_Pour)          9.00 4.04    1.69    0.99
## s(Dist_Head)          9.00 1.00    1.69    1.00
## s(Relative_position)  9.00 1.00    1.36    0.90
## s(Percent_occupied)   4.00 2.16    1.16    0.72
shapiro.test(gam_fish$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  gam_fish$residuals
## W = 0.92947, p-value = 0.1901
# Summary of the Periphyton biomass model
summary(gam_periphyton)
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## Periphyton ~ s(Mean_Slope_Degrees, k = 5) + s(Dist_Pour, k = 5) + 
##     s(Dist_Head, k = 5) + s(Relative_position, k = 5) + s(Percent_occupied, 
##     k = 5) + PR_Flag + CPOM_FPOM_Flag + FPOM_CPOM_Flag + Channel_Stability_Flag + 
##     Predator_Control_Flag
## 
## Parametric coefficients:
##                                                     Estimate Std. Error t value
## (Intercept)                                          1.52561    0.30737   4.963
## PR_FlagHeterotrophic (Below 0.75)                   -0.58878    0.27430  -2.147
## CPOM_FPOM_FlagLow Shredder Dependency (Below 0.25)  -0.01111    0.22032  -0.050
## FPOM_CPOM_FlagNormal FPOM (Below 0.50)               0.30882    0.13368   2.310
## Channel_Stability_FlagUnstable Channel (Below 0.50) -0.54442    0.14860  -3.664
## Predator_Control_FlagPrey Dominated (Below 0.50)     0.67237    0.22077   3.046
##                                                     Pr(>|t|)  
## (Intercept)                                           0.0147 *
## PR_FlagHeterotrophic (Below 0.75)                     0.1185  
## CPOM_FPOM_FlagLow Shredder Dependency (Below 0.25)    0.9629  
## FPOM_CPOM_FlagNormal FPOM (Below 0.50)                0.1015  
## Channel_Stability_FlagUnstable Channel (Below 0.50)   0.0335 *
## Predator_Control_FlagPrey Dominated (Below 0.50)      0.0536 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                         edf Ref.df     F p-value  
## s(Mean_Slope_Degrees) 1.502  1.732 3.074   0.236  
## s(Dist_Pour)          3.250  3.614 2.918   0.170  
## s(Dist_Head)          1.640  1.834 0.703   0.475  
## s(Relative_position)  2.520  2.808 5.912   0.078 .
## s(Percent_occupied)   1.000  1.000 0.495   0.532  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Rank: 25/26
## R-sq.(adj) =  0.859   Deviance explained = 97.4%
## GCV = 0.075843  Scale est. = 0.01301   n = 18
gam.check(gam_periphyton)

## 
## Method: GCV   Optimizer: magic
## Smoothing parameter selection converged after 19 iterations.
## The RMS GCV score gradient at convergence was 0.00000007960886 .
## The Hessian was positive definite.
## Model rank =  25 / 26 
## 
## Basis dimension (k) checking results. Low p-value (k-index<1) may
## indicate that k is too low, especially if edf is close to k'.
## 
##                         k'  edf k-index p-value
## s(Mean_Slope_Degrees) 4.00 1.50    1.42    0.92
## s(Dist_Pour)          4.00 3.25    1.45    0.95
## s(Dist_Head)          4.00 1.64    1.42    0.92
## s(Relative_position)  4.00 2.52    1.59    1.00
## s(Percent_occupied)   4.00 1.00    1.56    0.98
shapiro.test(gam_periphyton$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  gam_periphyton$residuals
## W = 0.933, p-value = 0.2192
# Summary of the Benthic biomass model
summary(gam_benthic)
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## Benthic ~ s(Mean_Slope_Degrees, k = 5) + s(Dist_Pour, k = 5) + 
##     s(Dist_Head, k = 5) + s(Relative_position, k = 5) + s(Percent_occupied, 
##     k = 5) + PR_Flag + CPOM_FPOM_Flag + FPOM_CPOM_Flag + Channel_Stability_Flag + 
##     Predator_Control_Flag
## 
## Parametric coefficients:
##                                                     Estimate Std. Error t value
## (Intercept)                                          0.86426    0.24308   3.556
## PR_FlagHeterotrophic (Below 0.75)                    0.14067    0.16280   0.864
## CPOM_FPOM_FlagLow Shredder Dependency (Below 0.25)  -0.36598    0.19656  -1.862
## FPOM_CPOM_FlagNormal FPOM (Below 0.50)               0.57120    0.17012   3.358
## Channel_Stability_FlagUnstable Channel (Below 0.50) -0.01361    0.20488  -0.066
## Predator_Control_FlagPrey Dominated (Below 0.50)     0.27946    0.18732   1.492
##                                                     Pr(>|t|)   
## (Intercept)                                          0.00886 **
## PR_FlagHeterotrophic (Below 0.75)                    0.41542   
## CPOM_FPOM_FlagLow Shredder Dependency (Below 0.25)   0.10374   
## FPOM_CPOM_FlagNormal FPOM (Below 0.50)               0.01163 * 
## Channel_Stability_FlagUnstable Channel (Below 0.50)  0.94884   
## Predator_Control_FlagPrey Dominated (Below 0.50)     0.17820   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                         edf Ref.df     F p-value  
## s(Mean_Slope_Degrees) 1.000  1.000 0.835  0.3912  
## s(Dist_Pour)          1.000  1.000 3.045  0.1245  
## s(Dist_Head)          1.802  2.195 3.770  0.0823 .
## s(Relative_position)  1.000  1.000 3.749  0.0940 .
## s(Percent_occupied)   1.000  1.000 4.221  0.0790 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Rank: 25/26
## R-sq.(adj) =  0.444   Deviance explained = 76.5%
## GCV = 0.11332  Scale est. = 0.045317  n = 18
gam.check(gam_benthic)

## 
## Method: GCV   Optimizer: magic
## Smoothing parameter selection converged after 14 iterations.
## The RMS GCV score gradient at convergence was 0.00000003721144 .
## The Hessian was positive definite.
## Model rank =  25 / 26 
## 
## Basis dimension (k) checking results. Low p-value (k-index<1) may
## indicate that k is too low, especially if edf is close to k'.
## 
##                        k' edf k-index p-value
## s(Mean_Slope_Degrees) 4.0 1.0    1.28    0.79
## s(Dist_Pour)          4.0 1.0    0.94    0.36
## s(Dist_Head)          4.0 1.8    1.40    0.93
## s(Relative_position)  4.0 1.0    1.16    0.61
## s(Percent_occupied)   4.0 1.0    1.38    0.94
shapiro.test(gam_benthic$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  gam_benthic$residuals
## W = 0.86414, p-value = 0.01427
# Summary of the Drift biomass model
summary(gam_drift)
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## Drift ~ s(Mean_Slope_Degrees, k = 5) + s(Dist_Pour, k = 5) + 
##     s(Dist_Head, k = 5) + s(Relative_position, k = 5) + s(Percent_occupied, 
##     k = 5) + PR_Flag + CPOM_FPOM_Flag + FPOM_CPOM_Flag + Channel_Stability_Flag + 
##     Predator_Control_Flag
## 
## Parametric coefficients:
##                                                     Estimate Std. Error t value
## (Intercept)                                           1.1845     0.3024   3.917
## PR_FlagHeterotrophic (Below 0.75)                     0.2107     0.2014   1.046
## CPOM_FPOM_FlagLow Shredder Dependency (Below 0.25)   -0.5431     0.2462  -2.206
## FPOM_CPOM_FlagNormal FPOM (Below 0.50)                0.5050     0.2123   2.379
## Channel_Stability_FlagUnstable Channel (Below 0.50)  -0.3679     0.2553  -1.441
## Predator_Control_FlagPrey Dominated (Below 0.50)      0.3116     0.2297   1.357
##                                                     Pr(>|t|)   
## (Intercept)                                          0.00493 **
## PR_FlagHeterotrophic (Below 0.75)                    0.32768   
## CPOM_FPOM_FlagLow Shredder Dependency (Below 0.25)   0.06028 . 
## FPOM_CPOM_FlagNormal FPOM (Below 0.50)               0.04629 * 
## Channel_Stability_FlagUnstable Channel (Below 0.50)  0.18954   
## Predator_Control_FlagPrey Dominated (Below 0.50)     0.21379   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                         edf Ref.df     F p-value  
## s(Mean_Slope_Degrees) 1.000   1.00 2.667  0.1411  
## s(Dist_Pour)          1.000   1.00 0.019  0.8934  
## s(Dist_Head)          1.419   1.71 0.990  0.4005  
## s(Relative_position)  1.000   1.00 0.681  0.4331  
## s(Percent_occupied)   1.000   1.00 3.920  0.0831 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Rank: 25/26
## R-sq.(adj) =  0.258   Deviance explained = 66.9%
## GCV = 0.17055  Scale est. = 0.07183   n = 18
gam.check(gam_drift)

## 
## Method: GCV   Optimizer: magic
## Smoothing parameter selection converged after 14 iterations.
## The RMS GCV score gradient at convergence was 0.000000037265 .
## The Hessian was positive definite.
## Model rank =  25 / 26 
## 
## Basis dimension (k) checking results. Low p-value (k-index<1) may
## indicate that k is too low, especially if edf is close to k'.
## 
##                         k'  edf k-index p-value  
## s(Mean_Slope_Degrees) 4.00 1.00    1.17   0.650  
## s(Dist_Pour)          4.00 1.00    0.74   0.095 .
## s(Dist_Head)          4.00 1.42    1.00   0.430  
## s(Relative_position)  4.00 1.00    1.03   0.460  
## s(Percent_occupied)   4.00 1.00    0.84   0.200  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
shapiro.test(gam_drift$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  gam_drift$residuals
## W = 0.93783, p-value = 0.2659
condition_biomass_clean <- na.omit(condition_biomass)

response <- condition_biomass_clean[, c("Fish", "Periphyton", "Benthic", "Drift")]
explanatory <- condition_biomass_clean[, c(1:5)]

exp_binary <- model.matrix(~ PR_Flag + CPOM_FPOM_Flag + FPOM_CPOM_Flag + Channel_Stability_Flag + Predator_Control_Flag - 1, data = explanatory)

exp_binary <- as.data.frame(exp_binary)
response <- as.data.frame(response)

condition_rda <- rda(response ~ ., data = exp_binary, grouping = rda_env$Basin)
## 
## Some constraints or conditions were aliased because they were redundant. This
## can happen if terms are linearly dependent (collinear):
## '`PR_FlagHeterotrophic (Below 0.75)`',
## '`Predator_Control_FlagPrey Dominated (Below 0.50)`'
anova(condition_rda, by = "terms", permutations = 999)
## Permutation test for rda under reduced model
## Terms added sequentially (first to last)
## Permutation: free
## Number of permutations: 999
## 
## Model: rda(formula = response ~ `PR_FlagAutotrophic (Above 0.75)` + `PR_FlagHeterotrophic (Below 0.75)` + `CPOM_FPOM_FlagLow Shredder Dependency (Below 0.25)` + `FPOM_CPOM_FlagNormal FPOM (Below 0.50)` + `Channel_Stability_FlagUnstable Channel (Below 0.50)` + `Predator_Control_FlagPrey Dominated (Below 0.50)`, data = exp_binary, grouping = rda_env$Basin)
##                                                       Df Variance      F Pr(>F)
## `PR_FlagAutotrophic (Above 0.75)`                      1 0.015221 0.7654  0.538
## `CPOM_FPOM_FlagLow Shredder Dependency (Below 0.25)`   1 0.043321 2.1784  0.102
## `FPOM_CPOM_FlagNormal FPOM (Below 0.50)`               1 0.008884 0.4467  0.606
## `Channel_Stability_FlagUnstable Channel (Below 0.50)`  1 0.031373 1.5776  0.188
## Residual                                              13 0.258530
summary(condition_rda)
## 
## Call:
## rda(formula = response ~ `PR_FlagAutotrophic (Above 0.75)` +      `PR_FlagHeterotrophic (Below 0.75)` + `CPOM_FPOM_FlagLow Shredder Dependency (Below 0.25)` +      `FPOM_CPOM_FlagNormal FPOM (Below 0.50)` + `Channel_Stability_FlagUnstable Channel (Below 0.50)` +      `Predator_Control_FlagPrey Dominated (Below 0.50)`, data = exp_binary,      grouping = rda_env$Basin) 
## 
## Partitioning of variance:
##               Inertia Proportion
## Total          0.3573     1.0000
## Constrained    0.0988     0.2765
## Unconstrained  0.2585     0.7235
## 
## Eigenvalues, and their contribution to the variance 
## 
## Importance of components:
##                          RDA1    RDA2     RDA3     RDA4    PC1     PC2     PC3
## Eigenvalue            0.08305 0.01240 0.002063 0.001282 0.1301 0.09262 0.01920
## Proportion Explained  0.23243 0.03471 0.005775 0.003587 0.3641 0.25920 0.05374
## Cumulative Proportion 0.23243 0.26713 0.272906 0.276493 0.6406 0.89981 0.95355
##                           PC4
## Eigenvalue            0.01660
## Proportion Explained  0.04645
## Cumulative Proportion 1.00000
## 
## Accumulated constrained eigenvalues
## Importance of components:
##                          RDA1   RDA2     RDA3     RDA4
## Eigenvalue            0.08305 0.0124 0.002063 0.001282
## Proportion Explained  0.84062 0.1255 0.020885 0.012975
## Cumulative Proportion 0.84062 0.9661 0.987025 1.000000
# Get the summary of the RDA results
rda_summary2 <- summary(condition_rda)

# Access the scores for each variable (explained by RDA1 and RDA2)
explan_condition <- rda_summary2$species
biplot_scores2 <- scores(condition_rda, display = "bp")
# View the RDA1 and RDA2 scores for each variable
print(explan_condition)
##                  RDA1        RDA2        RDA3         RDA4        PC1
## Fish        0.5433552 -0.05269302 -0.05930159  0.042582055 -0.2597892
## Periphyton  0.3181973 -0.15975378  0.08567874 -0.008977313 -0.2627197
## Benthic    -0.3188273 -0.04835442  0.02805880  0.080882754 -0.5747066
## Drift      -0.2733430 -0.23431198 -0.05087016 -0.020146884 -0.6562257
##                   PC2
## Fish        0.3799758
## Periphyton  0.6335570
## Benthic    -0.2197752
## Drift      -0.2115968
condition_rda$CCA$biplot
##                                                             RDA1       RDA2
## `PR_FlagAutotrophic (Above 0.75)`                      0.3878409  0.3527564
## `CPOM_FPOM_FlagLow Shredder Dependency (Below 0.25)`   0.7681939  0.3223728
## `FPOM_CPOM_FlagNormal FPOM (Below 0.50)`              -0.2895138 -0.3744074
## `Channel_Stability_FlagUnstable Channel (Below 0.50)` -0.8624438  0.2370271
##                                                             RDA3        RDA4
## `PR_FlagAutotrophic (Above 0.75)`                     -0.5719269 -0.63090567
## `CPOM_FPOM_FlagLow Shredder Dependency (Below 0.25)`   0.5367472 -0.13362747
## `FPOM_CPOM_FlagNormal FPOM (Below 0.50)`               0.2707745 -0.83826134
## `Channel_Stability_FlagUnstable Channel (Below 0.50)`  0.4448151  0.04635023
condition_rda$CCA$v
##                  RDA1       RDA2       RDA3        RDA4
## Fish        0.7178980 -0.1801662 -0.4970778  0.45285351
## Periphyton  0.4204123 -0.5462248  0.7181762 -0.09547232
## Benthic    -0.4212447 -0.1653318  0.2351945  0.86017546
## Drift      -0.3611494 -0.8011517 -0.4264038 -0.21425897
condition_rda$CCA$u
##          RDA1       RDA2       RDA3        RDA4
## 1  -0.5121293 -0.2149152 -0.3578315  0.08908498
## 2   0.2160189 -0.3722182  0.0820836  0.04996143
## 3   0.2160189 -0.3722182  0.0820836  0.04996143
## 4   0.1710218  0.1555510 -0.2521961 -0.27820325
## 5   0.1710218  0.1555510 -0.2521961 -0.27820325
## 6  -0.1150373  0.1332344  0.2370369 -0.01140661
## 7   0.2813568  0.3638586 -0.2631455  0.81464359
## 8   0.1710218  0.1555510 -0.2521961 -0.27820325
## 10 -0.1150373  0.1332344  0.2370369 -0.01140661
## 11 -0.1150373  0.1332344  0.2370369 -0.01140661
## 12  0.1710218  0.1555510 -0.2521961 -0.27820325
## 14  0.2160189 -0.3722182  0.0820836  0.04996143
## 15 -0.1150373  0.1332344  0.2370369 -0.01140661
## 16 -0.5121293 -0.2149152 -0.3578315  0.08908498
## 17 -0.1150373  0.1332344  0.2370369 -0.01140661
## 18  0.2160189 -0.3722182  0.0820836  0.04996143
## 19 -0.1150373  0.1332344  0.2370369 -0.01140661
## 20 -0.1150373  0.1332344  0.2370369 -0.01140661
#reduce model further/explore if reduction is possible

sqrt(vif.cca(condition_rda)) 
##                     `PR_FlagAutotrophic (Above 0.75)` 
##                                              1.247219 
##                   `PR_FlagHeterotrophic (Below 0.75)` 
##                                                    NA 
##  `CPOM_FPOM_FlagLow Shredder Dependency (Below 0.25)` 
##                                              1.069045 
##              `FPOM_CPOM_FlagNormal FPOM (Below 0.50)` 
##                                              1.086534 
## `Channel_Stability_FlagUnstable Channel (Below 0.50)` 
##                                              1.329608 
##    `Predator_Control_FlagPrey Dominated (Below 0.50)` 
##                                                    NA
anova.cca(condition_rda)#global RDA significance
## Permutation test for rda under reduced model
## Permutation: free
## Number of permutations: 999
## 
## Model: rda(formula = response ~ `PR_FlagAutotrophic (Above 0.75)` + `PR_FlagHeterotrophic (Below 0.75)` + `CPOM_FPOM_FlagLow Shredder Dependency (Below 0.25)` + `FPOM_CPOM_FlagNormal FPOM (Below 0.50)` + `Channel_Stability_FlagUnstable Channel (Below 0.50)` + `Predator_Control_FlagPrey Dominated (Below 0.50)`, data = exp_binary, grouping = rda_env$Basin)
##          Df Variance     F Pr(>F)
## Model     4 0.098799 1.242  0.275
## Residual 13 0.258530
anova.cca(condition_rda, by = "axis") # significance of axes
## Permutation test for rda under reduced model
## Forward tests for axes
## Permutation: free
## Number of permutations: 999
## 
## Model: rda(formula = response ~ `PR_FlagAutotrophic (Above 0.75)` + `PR_FlagHeterotrophic (Below 0.75)` + `CPOM_FPOM_FlagLow Shredder Dependency (Below 0.25)` + `FPOM_CPOM_FlagNormal FPOM (Below 0.50)` + `Channel_Stability_FlagUnstable Channel (Below 0.50)` + `Predator_Control_FlagPrey Dominated (Below 0.50)`, data = exp_binary, grouping = rda_env$Basin)
##          Df Variance      F Pr(>F)
## RDA1      1 0.083052 4.1762  0.189
## RDA2      1 0.012401 0.6236  0.971
## RDA3      1 0.002063 0.1038  1.000
## RDA4      1 0.001282 0.0645       
## Residual 13 0.258530
anova.cca(condition_rda)
## Permutation test for rda under reduced model
## Permutation: free
## Number of permutations: 999
## 
## Model: rda(formula = response ~ `PR_FlagAutotrophic (Above 0.75)` + `PR_FlagHeterotrophic (Below 0.75)` + `CPOM_FPOM_FlagLow Shredder Dependency (Below 0.25)` + `FPOM_CPOM_FlagNormal FPOM (Below 0.50)` + `Channel_Stability_FlagUnstable Channel (Below 0.50)` + `Predator_Control_FlagPrey Dominated (Below 0.50)`, data = exp_binary, grouping = rda_env$Basin)
##          Df Variance     F Pr(>F)
## Model     4 0.098799 1.242  0.292
## Residual 13 0.258530
#Define basin colors (you can adjust this list based on your actual basin names)

site_colors <- c(
  "AA1_REACH" = "darkorange3", "AA2_REACH" = "darkorange4", 
  "AC_REACH" = "slateblue3", "AP_REACH" = "darkcyan", 
  "AZ_REACH" = "gold3", "BA1_REACH" = "firebrick3", 
  "BA2_REACH" = "green4", "BC_REACH" = "purple", 
  "BZ_REACH" = "darkgreen", "CA1_REACH" = "darkolivegreen4", 
  "CA2_REACH" = "mediumblue", "CP_REACH" = "yellowgreen", 
  "CZ2_REACH" = "darkred", "DA1_REACH" = "seagreen", 
  "DA2_REACH" = "indianred", "DC_REACH" = "orangered", 
  "DP_REACH" = "chocolate", "DZ_REACH" = "violet"
)

# Vector of colors for each site based on  bAsin
colors <- sapply(env_scaled$Basin, function(Basin) site_colors[Basin])

# Extract site coordinates (coordinates of site scores)
site_scores <- condition_rda$CCA$u

par(mar = c(5, 5, 2, 7))

# Plot RDA result, scaling 2 (displaying effects of explanatory variables)
plot(condition_rda, xlim = c(-1, 1), ylim = c(-1,1), scaling = 2, main = "RDA - Scaling = 2", type = "n") 

# Plot species labels
text(condition_rda, display = "species", col = "firebrick3", cex = 1)

# Plot sites with colors corresponding to basins
points(site_scores, col = colors, pch = 20, cex = 1.)

# Plot biplot arrows (explanatory variables)
text(condition_rda, scaling = 2, display = "bp", col = "darkblue", cex = 1)

# Add the legend to the plot, positioned outside
legend("right", legend = names(site_colors), col = site_colors, pch = 19, title = "Basin", 
       inset = -0.4, xpd = TRUE, cex = 0.7)

condition_biomass1 <- read.csv("reach_condition.csv")

condition_biomass1 <- condition_biomass1[-c(9,13),-c(8,9)]

# Select numerical variables
numerical_data <- condition_biomass1 %>%
  select(Mean_Slope_Degrees, Dist_Pour, Dist_Head, Relative_position, Percent_occupied)

# Convert categorical variables into binary format (dummy variables)
categorical_data <- condition_biomass1 %>%
  select(PR_Flag, CPOM_FPOM_Flag, FPOM_CPOM_Flag, Channel_Stability_Flag, Predator_Control_Flag) %>%
  mutate(across(everything(), as.factor))  # Convert all to factor if not already

# Create dummy variables for categorical factors
categorical_binary <- model.matrix(~ . - 1, data = categorical_data)

# Combine numerical and binary categorical data into one dataset
combined_data <- cbind(numerical_data, categorical_binary)

# Remove rows with NA values
combined_data <- drop_na(combined_data)

#shorten names
colnames(combined_data)[6] <- "Auto"
colnames(combined_data)[7] <- "Hetero"
colnames(combined_data)[8] <- "Shred_Dep"
colnames(combined_data)[9] <- "Normal_FPOM"
colnames(combined_data)[10] <- "Stable"
colnames(combined_data)[11] <- "Prey_Dom"

# Compute Gower's distance (excluding Basin)
dist_matrix_condition <- daisy(combined_data, metric = "gower")
## Warning in daisy(combined_data, metric = "gower"): binary variable(s) 6, 7, 8,
## 9, 10, 11 treated as interval scaled
# Perform hierarchical clustering (Ward's method)
condition_clust <- hclust(dist_matrix_condition, method = "ward.D2")

# Plot the dendrogram
plot(condition_clust, main = "Dendrogram of Clustering", cex = 0.7)

final_condition <- cbind(combined_data, Basin = condition_biomass1$Basin)

# Add cluster assignments to the original data (including Basin names)
final_condition <- final_condition %>%
  mutate(cluster = cutree(condition_clust, k = 5))

# View the table of cluster assignments
table(final_condition$cluster)
## 
## 1 2 3 4 5 
## 3 4 4 6 1
# Print the final data with Basin names and cluster assignments
print(final_condition)
##    Mean_Slope_Degrees  Dist_Pour  Dist_Head Relative_position Percent_occupied
## 1           0.5627990 0.09405779 0.30081412         0.8652373       0.30231646
## 2           0.8278380 0.56497136 0.51868785         0.5699650       0.18226414
## 3           0.8374997 0.65261370 0.58848006         0.5408931       0.12180508
## 4           0.3305135 0.99707250 0.48775562         0.2085846       0.06105181
## 5           0.3409368 1.00000000 1.00000000         0.5699650       0.00000000
## 6           0.3123774 0.51169256 0.24743058         0.3322185       0.24243322
## 7           0.9065602 0.43921008 0.93335283         0.8515649       0.06105181
## 8           0.7491686 0.17680785 0.41551067         0.8378557       0.30231646
## 9           0.1185268 0.51506735 0.32368682         0.4227938       0.18226414
## 10          1.0000000 0.00000000 0.07878532         0.8378557       1.00000000
## 11          0.1826329 0.01487185 0.62956884         1.0000000       0.18226414
## 12          0.0000000 0.71330529 0.48569967         0.4078205       0.06105181
## 13          0.4479005 0.77502576 0.45266012         0.3322185       0.06105181
## 14          0.8701152 0.14326609 0.00000000         0.4377192       1.00000000
## 15          0.3757263 0.94002705 0.60314221         0.3474385       0.06105181
## 16          0.3698918 0.45304981 0.59677976         0.7127662       0.12180508
## 17          0.4735465 0.66698235 0.11603491         0.0000000       0.12180508
## 18          0.2227579 0.44129387 0.88232499         0.8378557       0.00000000
##    Auto Hetero Shred_Dep Normal_FPOM Stable Prey_Dom     Basin cluster
## 1     0      1         0           1      1        1 AA1_REACH       1
## 2     0      1         1           1      0        0 AA2_REACH       2
## 3     0      1         1           1      0        0  AC_REACH       2
## 4     1      0         1           1      0        0  AP_REACH       3
## 5     1      0         1           1      0        0  AZ_REACH       3
## 6     0      1         1           1      1        1 BA1_REACH       4
## 7     0      1         1           0      0        1 BA2_REACH       5
## 8     1      0         1           1      0        0  BC_REACH       3
## 9     0      1         1           1      1        1  BZ_REACH       4
## 10    0      1         1           1      1        1 CA1_REACH       1
## 11    1      0         1           1      0        0 CA2_REACH       3
## 12    0      1         1           1      0        0  CP_REACH       2
## 13    0      1         1           1      1        1 CZ2_REACH       4
## 14    0      1         0           1      1        1 DA1_REACH       1
## 15    0      1         1           1      1        1 DA2_REACH       4
## 16    0      1         1           1      0        0  DC_REACH       2
## 17    0      1         1           1      1        1  DP_REACH       4
## 18    0      1         1           1      1        1  DZ_REACH       4
# Create the clusters using cutree
clusters <- cutree(condition_clust, k = 5)

# Plot the dendrogram and add rectangles around clusters
plot(condition_clust, labels = final_condition$Basin, main = "Clustering Dendrogram by Basin", cex = 0.7)
rect.hclust(condition_clust, k = 5, border = 2:5)  # Use the same 'k' value

# Prepare the data for the heatmap (remove non-numeric columns like Basin and cluster)
cluster_matrix_condition <- final_condition %>%
  select(-Basin, -cluster) %>%
  as.data.frame()

# Convert character columns to factors
cluster_matrix_condition[] <- lapply(cluster_matrix_condition, function(x) {
  if (is.character(x)) {
    return(factor(x))
  } else {
    return(x)
  }
})

# Calculate the distance matrix for the heatmap
condition_matrix_heatmap <- dist(cluster_matrix_condition, method = "euclidean")
final_condition_heatmap <- hclust(condition_matrix_heatmap, method = "ward.D2")

# Create the heatmap
pheatmap(cluster_matrix_condition, 
         clustering_method = "ward.D2", 
         cluster_rows = final_condition_heatmap, 
         cluster_cols = TRUE, 
         show_rownames = TRUE, 
         show_colnames = TRUE, 
         main = "Heatmap of Clustering Results",
         labels_row = final_condition$Basin)  # Custom labels based on Basin names