#THIS FILE CONTAINS CODE FOR MODELING FREQUENCY OF OCCURRENCE DATA IN A GENERAL MIXED EFFECTS LINEAR REGRESSION MODEL
# STEP 0: LOAD PACKAGES
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.3
## Warning: package 'ggplot2' was built under R version 4.3.3
## Warning: package 'tibble' was built under R version 4.3.3
## Warning: package 'tidyr' was built under R version 4.3.3
## Warning: package 'readr' was built under R version 4.3.3
## Warning: package 'purrr' was built under R version 4.3.3
## Warning: package 'dplyr' was built under R version 4.3.3
## Warning: package 'stringr' was built under R version 4.3.3
## Warning: package 'forcats' was built under R version 4.3.3
## Warning: package 'lubridate' was built under R version 4.3.3
## ── 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.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ 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(readxl)
## Warning: package 'readxl' was built under R version 4.3.3
library(lme4)
## Warning: package 'lme4' was built under R version 4.3.3
## Loading required package: Matrix
## Warning: package 'Matrix' was built under R version 4.3.3
## 
## Attaching package: 'Matrix'
## 
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
library(lmerTest)
## Warning: package 'lmerTest' was built under R version 4.3.3
## 
## Attaching package: 'lmerTest'
## 
## The following object is masked from 'package:lme4':
## 
##     lmer
## 
## The following object is masked from 'package:stats':
## 
##     step
library(performance)
## Warning: package 'performance' was built under R version 4.3.3
library(emmeans)
## Warning: package 'emmeans' was built under R version 4.2.3
library(multcomp)
## Warning: package 'multcomp' was built under R version 4.2.3
## Loading required package: mvtnorm
## Warning: package 'mvtnorm' was built under R version 4.3.3
## Loading required package: survival
## Loading required package: TH.data
## Warning: package 'TH.data' was built under R version 4.2.3
## Loading required package: MASS
## 
## Attaching package: 'MASS'
## 
## The following object is masked from 'package:dplyr':
## 
##     select
## 
## 
## Attaching package: 'TH.data'
## 
## The following object is masked from 'package:MASS':
## 
##     geyser
library(boot)
## Warning: package 'boot' was built under R version 4.2.3
## 
## Attaching package: 'boot'
## 
## The following object is masked from 'package:survival':
## 
##     aml
library(broom.mixed)
## Warning: package 'broom.mixed' was built under R version 4.2.3

COMMON ABBREVIATIONS THROUGHOUT WORKFLOW: FoC_Pcri = Frequency of Occurrence of Curly Leaf Pondweed (aka Pcri / CLP) FoC_Mspi = Frequency of Occurrence of Eurasian Watermilfoil (aka Mspi / EWM) FoC_Nat = Frequency of Occurrence of Native Macrophytes (aka Nat) FoC_tot = Frequency of Occurrence of All Aquatic Macrophytes (aka tot) SECCHI = SECCHI DEPTH IN METERS TP = TOTAL EPILIMNETIC PHOSPHORUS Sig Letters = Significant Letters ALUM = ALUMINUM SULFATE TREATMENT PERIOD = GROUPING OF DATA BASED ON WHEN ALUM TREATMENT OCCURRED PRE = PRE-ALUM TREATMENT, BEFORE ANY ALUM HAS BEEN APPLIED DURING = DURING-ALUM TREATMENT, BETWEEN THE FIRST AND LAST ALUM TREATMENT APPLICATION POST = POST-ALUM TREATMENT, AFTER ALUM HAS BEEN APPLIED SEASON = TIME OF YEAR SAMPLING TOOK PLACE EARLY = SAMPLING PERFORMED BETWEEN APRIL AND JUNE LATE = SAMPLING PERFORMED BETWEEN JULY AND SEPTEMBER D_S = DEPTH CLASSIFICATION OF LAKE BASED ON MAX DEPTH DEEP (D)= MAX DEPTH GREATER THAN 15 FEET SHALLOW (S) = MAX DEPTH LESS THAN 15 FEET

DISCLAIMER: IF JUST CLP, EWM, OR NAT IS IN THE COMMENTS I’M REFERRING TO THE FREQUENCY OF OCCURRENCE

# STEP 1: LOAD IN PVALUES FOR EACH MODEL
models <- read.csv("C:/Users/k_hem/OneDrive/Desktop/UMN AV Data/PvaluesModelsUSGS.csv")
# STEP 2: RUN P ADJUSTMENT CODE
models$fdr <- p.adjust(models$p_value, method = "fdr") # USE P ADJUST TO ACCOUNT FOR FALSE POSITIVES AND STORE RESULTS IN NEW COLUMN 'FDR', SPECIFY MODEL TO TEST AND COLUMN TO CALCULATE WITH

# STEP 2.1: DISPLAY RESULTS
print(models)
##                       comparison       p_value     model           fdr
## 1                      intercept  1.260000e-28        tp  2.126250e-27
## 2                   periodduring  1.964692e-02        tp  3.274487e-02
## 3                     periodpost  1.300000e-05        tp  3.510000e-05
## 4                           d_sS  6.790079e-02        tp  9.963703e-02
## 5                     seasonlate  3.480000e-23        tp  2.936250e-22
## 6              periodduring:d_sS  9.583838e-01        tp  9.583838e-01
## 7                periodpost:d_sS  1.031078e-01        tp  1.420363e-01
## 8        periodduring:seasonlate  1.439092e-02        tp  2.428468e-02
## 9          periodpost:seasonlate  4.574405e-01        tp  5.233429e-01
## 10               d_sS:seasonlate  1.965300e-03        tp  4.019932e-03
## 11                     Intercept  1.050000e-27    secchi  1.575000e-26
## 12                  periodduring  5.531339e-02    secchi  8.438258e-02
## 13                    periodpost  5.812920e-04    secchi  1.256027e-03
## 14                          d_sS  3.390000e-09    secchi  1.204342e-08
## 15                    seasonlate 1.120000e-103    secchi 5.040000e-102
## 16             periodduring:d_sS  8.281993e-01    secchi  8.600531e-01
## 17               periodpost:d_sS  1.423744e-02    secchi  2.428468e-02
## 18               d_sS:seasonlate  3.992158e-01    secchi  4.646046e-01
## 19       periodduring:seasonlate  1.570000e-18    secchi  9.634091e-18
## 20         periodpost:seasonlate  2.950000e-12    secchi  1.284677e-11
## 21                     Intercept  5.673769e-01       clp  6.382990e-01
## 22                   period2post  5.861460e-04       clp  1.256027e-03
## 23                          d_sS  1.680585e-01       clp  2.181529e-01
## 24                    seasonlate 4.200000e-125       clp 2.835000e-123
## 25              period2post:d_sS  1.690000e-08       clp  5.432143e-08
## 26        period2post:seasonlate  2.040000e-24       clp  2.520000e-23
## 27               d_sS:seasonlate  1.920000e-23       clp  1.993846e-22
## 28   period2post:d_sS:seasonlate  3.250000e-05       clp  8.602941e-05
## 29                     Intercept  8.675081e-01      clp2  8.931477e-01
## 30                   period2post  9.300000e-10      clp2  3.692647e-09
## 31                          d_sS  9.065482e-02      clp2  1.276759e-01
## 32                    seasonlate 1.860000e-143      clp2 2.511000e-141
## 33              period2post:d_sS  2.250000e-15      clp2  1.052069e-14
## 34        period2post:seasonlate  6.380000e-32      clp2  1.435500e-30
## 35               d_sS:seasonlate  3.340000e-30      clp2  6.441429e-29
## 36   period2post:d_sS:seasonlate  6.270000e-08      clp2  1.923750e-07
## 37                   (Intercept)  2.500197e-03       ewm  4.821809e-03
## 38                    periodpost  1.280000e-21       ewm  8.640000e-21
## 39                     periodpre  2.070000e-16       ewm  1.164375e-15
## 40                    seasonlate  5.200000e-22       ewm  3.900000e-21
## 41         periodpost:seasonlate  1.210000e-09       ewm  4.414865e-09
## 42          periodpre:seasonlate  2.720000e-07       ewm  7.982609e-07
## 43                   (Intercept)  2.500197e-03      ewm2  4.821809e-03
## 44                    periodpost  1.280000e-21      ewm2  8.640000e-21
## 45                     periodpre  2.070000e-16      ewm2  1.164375e-15
## 46                    seasonlate  5.200000e-22      ewm2  3.900000e-21
## 47         periodpost:seasonlate  1.210000e-09      ewm2  4.414865e-09
## 48          periodpre:seasonlate  2.720000e-07      ewm2  7.982609e-07
## 49                   (Intercept)  1.922824e-01   natives  2.472202e-01
## 50                  periodduring  1.510000e-19   natives  9.707143e-19
## 51                    periodpost  2.260000e-15   natives  1.052069e-14
## 52                          d_sS  8.917269e-01   natives  9.051363e-01
## 53                    seasonlate  1.276040e-04   natives  3.132098e-04
## 54             periodduring:d_sS  7.264868e-03   natives  1.290470e-02
## 55               periodpost:d_sS  1.570947e-03   natives  3.262736e-03
## 56       periodduring:seasonlate  3.501612e-02   natives  5.627591e-02
## 57         periodpost:seasonlate  5.707285e-02   natives  8.560927e-02
## 58               d_sS:seasonlate  2.618150e-03   natives  4.978173e-03
## 59  periodduring:d_sS:seasonlate  8.734585e-03   natives  1.531388e-02
## 60    periodpost:d_sS:seasonlate  7.331437e-01   natives  7.855111e-01
## 61                   (Intercept)  2.215493e-01  natives2  2.769366e-01
## 62                  periodduring  1.070000e-15  natives2  5.555769e-15
## 63                    periodpost  1.410000e-15  natives2  7.050000e-15
## 64                          d_sS  9.526043e-01  natives2  9.583838e-01
## 65                    seasonlate  5.675950e-04  natives2  1.256027e-03
## 66             periodduring:d_sS  4.189914e-03  natives2  7.748471e-03
## 67               periodpost:d_sS  4.411520e-04  natives2  9.925920e-04
## 68       periodduring:seasonlate  5.420592e-02  natives2  8.411263e-02
## 69         periodpost:seasonlate  3.621070e-01  natives2  4.288109e-01
## 70               d_sS:seasonlate  5.162593e-03  natives2  9.292667e-03
## 71  periodduring:d_sS:seasonlate  1.533660e-04  natives2  3.569726e-04
## 72    periodpost:d_sS:seasonlate  3.881070e-01  natives2  4.556039e-01
## 73                   (Intercept)  2.154421e-01       all  2.718195e-01
## 74                  periodduring  8.032806e-02       all  1.153648e-01
## 75                    periodpost  1.442950e-04       all  3.417513e-04
## 76                    seasonlate  1.880000e-68       all  5.076000e-67
## 77                          d_sS  7.778106e-01       all  8.139878e-01
## 78       periodduring:seasonlate  3.220000e-23       all  2.898000e-22
## 79         periodpost:seasonlate  2.240000e-24       all  2.520000e-23
## 80             periodduring:d_sS  4.810000e-06       all  1.352812e-05
## 81               periodpost:d_sS  9.173747e-02       all  1.276759e-01
## 82               seasonlate:d_sS  9.260000e-09       all  3.049024e-08
## 83  periodduring:seasonlate:d_sS  6.862000e-01       all  7.410960e-01
## 84    periodpost:seasonlate:d_sS  2.148012e-03       all  4.264436e-03
## 85                   (Intercept)  2.154421e-01      all2  2.718195e-01
## 86                  periodduring  8.032806e-02      all2  1.153648e-01
## 87                    periodpost  1.442950e-04      all2  3.417513e-04
## 88                    seasonlate  1.880000e-68      all2  5.076000e-67
## 89                          d_sS  7.778106e-01      all2  8.139878e-01
## 90       periodduring:seasonlate  3.220000e-23      all2  2.898000e-22
## 91         periodpost:seasonlate  2.240000e-24      all2  2.520000e-23
## 92             periodduring:d_sS  4.810000e-06      all2  1.352812e-05
## 93               periodpost:d_sS  9.173747e-02      all2  1.276759e-01
## 94               seasonlate:d_sS  9.260000e-09      all2  3.049024e-08
## 95  periodduring:seasonlate:d_sS  6.862000e-01      all2  7.410960e-01
## 96    periodpost:seasonlate:d_sS  2.148012e-03      all2  4.264436e-03
## 97                           tot  1.640000e-01     tbass  2.149515e-01
## 98                           tot  1.607000e-01 thalfmoon  2.126912e-01
## 99                           tot  8.733000e-01   thyland  8.931477e-01
## 100                          tot  2.431000e-01   tkeller  3.010872e-01
## 101                          tot  4.714000e-02     tlong  7.399884e-02
## 102                          tot  7.570000e-05    triley  1.892500e-04
## 103                          tot  7.670000e-01     twass  8.139878e-01
## 104                          nat  3.596000e-02     tbass  5.711294e-02
## 105                          nat  1.270000e-03 thalfmoon  2.678906e-03
## 106                          nat  4.205000e-01   thyland  4.851923e-01
## 107                          nat  5.210000e-05   tkeller  1.352596e-04
## 108                          nat  5.563000e-02     tlong  8.438258e-02
## 109                          nat  1.850000e-08    triley  5.808140e-08
## 110                          nat  5.604000e-01     twass  6.357479e-01
## 111                         pcri  5.850000e-01     tbass  6.473361e-01
## 112                         pcri  1.272000e-01 thalfmoon  1.717200e-01
## 113                         pcri  3.620000e-01   thyland  4.288109e-01
## 114                         pcri  5.939000e-02   tkeller  8.810604e-02
## 115                         pcri  3.140000e-02     tlong  5.107229e-02
## 116                         pcri  6.093000e-01    triley  6.687439e-01
## 117                         pcri  2.790000e-01     twass  3.424091e-01
## 118                         mspi  3.293000e-01   thyland  3.969241e-01
## 119                         mspi  1.260000e-01   tkeller  1.717200e-01
## 120                         mspi  1.138000e-02    triley  1.969615e-02
## 121                         mspi  1.350000e-01     twass  1.804455e-01
## 122                          tep  7.280000e-05     tbass  1.854340e-04
## 123                          tep  1.200000e-05     tbass  3.306122e-05
## 124                          tep  5.800000e-01 thalfmoon  6.471074e-01
## 125                          tep  3.180000e-01 thalfmoon  3.867568e-01
## 126                          tep  9.530000e-14   thyland  4.288500e-13
## 127                          tep  4.500000e-03   thyland  8.209459e-03
## 128                          tep  2.200000e-16   tkeller  1.188000e-15
## 129                          sec  1.200000e-09   tkeller  4.414865e-09
## 130                          sec  1.820000e-04     tlong  4.164407e-04
## 131                          sec  3.670000e-03     tlong  6.881250e-03
## 132                          sec  5.000000e-11    triley  2.109375e-10
## 133                          sec  3.910000e-09    triley  1.353462e-08
## 134                          sec  5.760000e-10     twass  2.356364e-09
## 135                          sec  2.790000e-02     twass  4.593293e-02
sessionInfo()
## R version 4.2.2 (2022-10-31 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 22621)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=English_United States.utf8 
## [2] LC_CTYPE=English_United States.utf8   
## [3] LC_MONETARY=English_United States.utf8
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.utf8    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] broom.mixed_0.2.9.5 boot_1.3-30         multcomp_1.4-25    
##  [4] TH.data_1.1-2       MASS_7.3-58.1       survival_3.4-0     
##  [7] mvtnorm_1.2-4       emmeans_1.10.0      performance_0.10.9 
## [10] lmerTest_3.1-3      lme4_1.1-35.1       Matrix_1.6-5       
## [13] readxl_1.4.3        lubridate_1.9.3     forcats_1.0.0      
## [16] stringr_1.5.1       dplyr_1.1.4         purrr_1.0.2        
## [19] readr_2.1.5         tidyr_1.3.1         tibble_3.2.1       
## [22] ggplot2_3.5.0       tidyverse_2.0.0    
## 
## loaded via a namespace (and not attached):
##  [1] sass_0.4.9          jsonlite_1.8.8      splines_4.2.2      
##  [4] bslib_0.6.1         cellranger_1.1.0    yaml_2.3.8         
##  [7] globals_0.16.3      numDeriv_2016.8-1.1 pillar_1.9.0       
## [10] backports_1.4.1     lattice_0.20-45     glue_1.7.0         
## [13] digest_0.6.35       minqa_1.2.6         colorspace_2.1-0   
## [16] sandwich_3.1-0      htmltools_0.5.7     pkgconfig_2.0.3    
## [19] broom_1.0.5         listenv_0.9.1       xtable_1.8-4       
## [22] scales_1.3.0        tzdb_0.4.0          timechange_0.3.0   
## [25] generics_0.1.3      cachem_1.0.8        withr_3.0.0        
## [28] furrr_0.3.1         cli_3.6.2           magrittr_2.0.3     
## [31] estimability_1.5    evaluate_0.23       parallelly_1.37.1  
## [34] fansi_1.0.6         future_1.33.1       nlme_3.1-160       
## [37] tools_4.2.2         hms_1.1.3           lifecycle_1.0.4    
## [40] munsell_0.5.0       compiler_4.2.2      jquerylib_0.1.4    
## [43] rlang_1.1.3         grid_4.2.2          nloptr_2.0.3       
## [46] rstudioapi_0.15.0   rmarkdown_2.26      gtable_0.3.4       
## [49] codetools_0.2-18    R6_2.5.1            zoo_1.8-12         
## [52] knitr_1.45          fastmap_1.1.1       utf8_1.2.4         
## [55] insight_0.19.9      stringi_1.8.3       parallel_4.2.2     
## [58] Rcpp_1.0.12         vctrs_0.6.5         tidyselect_1.2.1   
## [61] xfun_0.42           coda_0.19-4.1