library(dplyr)
library(lavaan)
library(DiagrammeR)
library(tidyr)
library(ggplot2)
source("functions/table_funcs.R")

# For saving SEM diagrams:
library(purrr)
library(DiagrammeRsvg)
library(rsvg)
library(png)
library(grid)
library(ggpubr)

Import data

combined=read.csv("data/annual_averages/annual_data_compiled_regions.csv",stringsAsFactors = F)
cnames=read.csv("analysis/column_names_region.csv", stringsAsFactors = F)
dsub=filter(combined, Year>=1975) %>% arrange(Region,Year)
focaldata=dsub[,cnames$Datacolumn]
fvars=cnames$Shortname
colnames(focaldata)=fvars
regions=unique(focaldata$region)
regionorder=c("West","North","South")
regionorder_pub=c("Whole estuary", "Suisun","Sacramento","San Joaquin")
years=1975:2021

focaldata = focaldata %>% 
  mutate(tzoop=hcope+clad+mysid+pcope+rotif_m,
         tzoop_e=hcope_e+clad_e+mysid_e+pcope_e+rotif_e,
         hzoop=hcope+clad+rotif_m,
         hzoop_e=hcope_e+clad_e+rotif_e,
         pzoop=mysid+pcope,
         pzoop_e=mysid_e+pcope_e,
         turbid=-secchi) 
fvars=c(fvars,"tzoop","tzoop_e",
        "hzoop","hzoop_e",
        "pzoop","pzoop_e","turbid")
cnames=rbind(cnames,data.frame(Longname=NA,Shortname=c("tzoop","tzoop_e",
                                                       "hzoop","hzoop_e",
                                                       "pzoop","pzoop_e","turbid"),
                               Diagramname=c("total zooplankton",
                                             "total zooplankton\nenergy",
                                             "herbivorous\nzooplankton",
                                             "herbivorous\nzooplankton\nenergy",
                                             "predatory\nzooplankton",
                                             "predatory\nzooplankton\nenergy",
                                             "turbidity"),
                               Datacolumn=NA,Log=c(rep("yes",6),"no"),
                               Color=c("black","black","#ED7D31","#ED7D31","#7030A0",
                                       "#7030A0","#4472C4")))

#focal variables
varnames=c("temp","flow","turbid","chla","hzoop","pzoop","potam","corbic","estfish","estfish_bsmt","estfish_stn")

source("analysis/semDiagramFunctions.r")

Data prep

Log transform, scale

#log transform
logvars=fvars[cnames$Log=="yes"]
logtrans=function(x) {
  x2=x[which(!is.na(x))]
  if(any(x2==0)) {log(x+min(x2[which(x2>0)],na.rm=T))}
  else {log(x)}
}
focaldatalog = focaldata %>% 
  mutate(flow=flow-min(flow,na.rm=T)) %>%  #get rid of negative flow values
  mutate_at(logvars,logtrans)

#scale data
fdr0=focaldatalog
tvars=fvars[-(1:2)]

fdr=fdr0 %>% group_by(region) %>% 
  #lag
  #mutate_at(tvars,list("1"=lag)) %>% 
  #scale
  mutate_at(-(1:2),scale) %>% 
  ungroup() %>% 
  as.data.frame() 

#detrended data
fdr_dtr=fdr0 %>% group_by(region) %>% 
  #detrend
  mutate_at(tvars,function(x) { 
    x<<-x
    if(!all(is.na(x))) {
      if((length(which(x==0))/length(x))<0.5) {
        x2<<-x
        x2[x2==0]=NA
        res<<-residuals(lm(x2~years))
        out=x
        out[which(!is.na(x2))]=res
        return(out)
      } else {return(x)}
    } else {return(x)}
  }) %>%
  #lag
  #mutate_at(tvars,list("1"=lag)) %>% 
  #scale
  mutate_at(-(1:2),scale) %>% 
  ungroup() %>% 
  as.data.frame() 

Time series plots

Other useful plots

Breakdown of total zooplankton biomass.

## Warning: Removed 15 rows containing non-finite values (`stat_align()`).

Similarity of fish indices.

## Warning: Removed 7 rows containing missing values (`geom_line()`).

## Warning: Removed 7 rows containing missing values (`geom_line()`).

Correlation between biomass and energy.

for(i in 1:length(regions)) {
  dtemp=filter(fdr,region==regions[i])
  print(regions[i])
  print(cor(dtemp$tzoop,dtemp$tzoop_e,use = "p"))
  print(cor(dtemp$hzoop,dtemp$hzoop_e,use = "p"))
  print(cor(dtemp$pzoop,dtemp$pzoop_e,use = "p"))
}
## [1] "North"
##           [,1]
## [1,] 0.9959517
##           [,1]
## [1,] 0.9870616
##           [,1]
## [1,] 0.9990414
## [1] "South"
##           [,1]
## [1,] 0.9941208
##           [,1]
## [1,] 0.9860441
##           [,1]
## [1,] 0.9993711
## [1] "West"
##          [,1]
## [1,] 0.995754
##           [,1]
## [1,] 0.9851301
##           [,1]
## [1,] 0.9994171

Cross-correlation matrices

(only sig correlations shown… no correction for multiple comparisons)

## Warning: `expand_scale()` was deprecated in ggplot2 3.3.0.
## ℹ Please use `expansion()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

Note correlation of fish indices, or lack thereof.

SEM model

With and without detrending.

West

#1
# modwest='zoop=~hcope+mysid
#         fish=~estfish_bsmt+estfish_bsot
#         zoop~chla+potam+flow
#         chla~potam+flow
#         fish~zoop+flow
# '
#2
# modwest='chla~potam+flow
#         tzoop~chla+potam+flow
#         estfish_bsmt~tzoop+flow
#         estfish_bsot~tzoop+flow
# '
#3
# modwest='chla~potam+flow+temp+turbid
#         tzoop~chla+potam+flow+temp+turbid
#         amphi~chla+potam+flow+temp+turbid
#         estfish_bsmt~tzoop+amphi+flow
#         #estfish_bsot~tzoop+amphi+flow+temp+turbid
#         amphi~~tzoop
# '
#4
# modwest='chla~potam+flow+temp+turbid
#         hzoop~chla+potam+flow+temp+turbid
#         pzoop~chla+potam+flow+temp+turbid+hzoop
#         amphi~chla+potam+flow+temp+turbid
#         estfish_bsmt~hzoop+pzoop+amphi+flow+temp+turbid
#         amphi~~hzoop+pzoop
# '
#5
modwest='chla~potam+flow+temp+turbid
        hzoop~chla+potam+flow+temp+turbid
        pzoop~chla+potam+flow+temp+turbid+hzoop
        fish~hzoop+pzoop+potam+flow+temp+turbid
        fish=~estfish+estfish_stn+estfish_bsmt
'

modfitwest=sem(modwest, data=filter(fdr,region=="West"))
modfitwest_dtr=sem(modwest, data=filter(fdr_dtr,region=="West"))
summary(modfitwest, standardized=T, rsq=T)
## lavaan 0.6.17 ended normally after 37 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        30
## 
##                                                   Used       Total
##   Number of observations                            40          47
## 
## Model Test User Model:
##                                                       
##   Test statistic                                20.568
##   Degrees of freedom                                15
##   P-value (Chi-square)                           0.151
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   fish =~                                                               
##     estfish           1.000                               0.871    0.875
##     estfish_stn       0.925    0.116    7.972    0.000    0.806    0.885
##     estfish_bsmt      0.856    0.144    5.952    0.000    0.745    0.755
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   chla ~                                                                
##     potam            -0.285    0.169   -1.688    0.091   -0.285   -0.285
##     flow              0.281    0.182    1.540    0.124    0.281    0.280
##     temp             -0.281    0.150   -1.870    0.062   -0.281   -0.278
##     turbid           -0.221    0.199   -1.112    0.266   -0.221   -0.213
##   hzoop ~                                                               
##     chla              0.542    0.100    5.404    0.000    0.542    0.625
##     potam            -0.306    0.111   -2.757    0.006   -0.306   -0.353
##     flow              0.093    0.119    0.777    0.437    0.093    0.106
##     temp              0.042    0.099    0.421    0.674    0.042    0.048
##     turbid           -0.255    0.128   -1.993    0.046   -0.255   -0.284
##   pzoop ~                                                               
##     chla              0.375    0.086    4.367    0.000    0.375    0.430
##     potam            -0.012    0.079   -0.151    0.880   -0.012   -0.014
##     flow             -0.319    0.078   -4.082    0.000   -0.319   -0.364
##     temp             -0.016    0.065   -0.240    0.810   -0.016   -0.018
##     turbid            0.134    0.087    1.532    0.126    0.134    0.148
##     hzoop             0.610    0.103    5.926    0.000    0.610    0.606
##   fish ~                                                                
##     hzoop             0.248    0.159    1.565    0.118    0.285    0.247
##     pzoop             0.267    0.147    1.821    0.069    0.307    0.267
##     potam            -0.285    0.090   -3.165    0.002   -0.328   -0.327
##     flow              0.194    0.097    1.995    0.046    0.223    0.222
##     temp              0.005    0.071    0.076    0.939    0.006    0.006
##     turbid            0.237    0.102    2.327    0.020    0.272    0.262
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .estfish           0.232    0.065    3.577    0.000    0.232    0.234
##    .estfish_stn       0.180    0.052    3.465    0.001    0.180    0.217
##    .estfish_bsmt      0.419    0.101    4.149    0.000    0.419    0.430
##    .chla              0.718    0.161    4.472    0.000    0.718    0.719
##    .hzoop             0.289    0.065    4.472    0.000    0.289    0.385
##    .pzoop             0.122    0.027    4.472    0.000    0.122    0.161
##    .fish              0.061    0.039    1.557    0.119    0.080    0.080
## 
## R-Square:
##                    Estimate
##     estfish           0.766
##     estfish_stn       0.783
##     estfish_bsmt      0.570
##     chla              0.281
##     hzoop             0.615
##     pzoop             0.839
##     fish              0.920
summary(modfitwest_dtr, standardized=T, rsq=T)
## lavaan 0.6.17 ended normally after 35 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        30
## 
##                                                   Used       Total
##   Number of observations                            40          47
## 
## Model Test User Model:
##                                                       
##   Test statistic                                16.070
##   Degrees of freedom                                15
##   P-value (Chi-square)                           0.377
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   fish =~                                                               
##     estfish           1.000                               0.644    0.698
##     estfish_stn       1.202    0.252    4.767    0.000    0.774    0.786
##     estfish_bsmt      0.996    0.251    3.964    0.000    0.641    0.650
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   chla ~                                                                
##     potam             0.175    0.157    1.119    0.263    0.175    0.178
##     flow              0.632    0.196    3.228    0.001    0.632    0.629
##     temp             -0.330    0.144   -2.295    0.022   -0.330   -0.325
##     turbid           -0.422    0.183   -2.307    0.021   -0.422   -0.418
##   hzoop ~                                                               
##     chla              0.493    0.130    3.802    0.000    0.493    0.493
##     potam            -0.035    0.130   -0.269    0.788   -0.035   -0.036
##     flow              0.421    0.180    2.338    0.019    0.421    0.420
##     temp             -0.006    0.125   -0.045    0.964   -0.006   -0.006
##     turbid           -0.517    0.160   -3.232    0.001   -0.517   -0.511
##   pzoop ~                                                               
##     chla              0.414    0.097    4.263    0.000    0.414    0.491
##     potam             0.029    0.084    0.343    0.732    0.029    0.035
##     flow             -0.255    0.123   -2.065    0.039   -0.255   -0.301
##     temp             -0.041    0.080   -0.509    0.611   -0.041   -0.048
##     turbid            0.025    0.115    0.216    0.829    0.025    0.029
##     hzoop             0.428    0.101    4.216    0.000    0.428    0.508
##   fish ~                                                                
##     hzoop             0.055    0.107    0.518    0.605    0.086    0.086
##     pzoop             0.213    0.118    1.801    0.072    0.331    0.278
##     potam            -0.083    0.074   -1.126    0.260   -0.129   -0.130
##     flow              0.514    0.129    3.974    0.000    0.798    0.792
##     temp             -0.042    0.069   -0.600    0.548   -0.065   -0.064
##     turbid           -0.008    0.101   -0.081    0.936   -0.013   -0.012
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .estfish           0.436    0.107    4.089    0.000    0.436    0.513
##    .estfish_stn       0.371    0.102    3.645    0.000    0.371    0.382
##    .estfish_bsmt      0.564    0.134    4.209    0.000    0.564    0.578
##    .chla              0.698    0.156    4.472    0.000    0.698    0.703
##    .hzoop             0.470    0.105    4.472    0.000    0.470    0.473
##    .pzoop             0.193    0.043    4.472    0.000    0.193    0.274
##    .fish              0.022    0.040    0.545    0.586    0.053    0.053
## 
## R-Square:
##                    Estimate
##     estfish           0.487
##     estfish_stn       0.618
##     estfish_bsmt      0.422
##     chla              0.297
##     hzoop             0.527
##     pzoop             0.726
##     fish              0.947
labelswest <- createLabels(modfitwest, cnames)

# residuals(modfitwest)
# modificationindices(modfitwest)

North

#1
# modnorth='zoop=~hcope+mysid
#         #fish=~estfish_bsmt+estfish_bsot
#         zoop~chla+potam+flow
#         chla~potam+flow
#         estfish_bsmt~zoop+flow
#         estfish_bsot~zoop+flow
# '
# modnorth='zoop=~clad
#         zoop~chla+corbic+potam+flow
#         chla~corbic+potam+flow
#         estfish_bsmt~zoop+flow+sside+chla
#         estfish_bsot~zoop+flow+sside+chla
# '
#2
# modnorth='chla~corbic+potam+flow
#         tzoop~chla+corbic+potam+flow
#         estfish_bsmt~tzoop+flow+chla
#         estfish_bsot~tzoop+flow
# '
#3
# modnorth='chla~corbic+potam+flow+temp+turbid
#         tzoop~chla+corbic+potam+flow+temp+turbid
#         amphi~chla+corbic+potam+flow+temp+turbid
#         estfish_bsmt~tzoop+amphi+flow+temp+turbid+chla+sside
#         #estfish_bsot~tzoop+amphi+flow+temp+turbid+sside
#         amphi~~tzoop
# '
#4
# modnorth='chla~corbic+flow+temp+turbid
#         hzoop~chla+corbic+flow+temp+turbid
#         pzoop~chla+corbic+flow+temp+turbid+hzoop
#         amphi~chla+corbic+flow+temp+turbid
#         estfish_bsmt~hzoop+pzoop+amphi+flow+temp+turbid+chla+sside
#         amphi~~hzoop+pzoop
# '
#5
modnorth='chla~corbic+flow+temp+turbid
        hzoop~chla+corbic+flow+temp+turbid
        pzoop~chla+corbic+flow+temp+turbid+hzoop
        fish~chla+hzoop+pzoop+corbic+flow+temp+turbid
        fish=~estfish+estfish_stn #+estfish_bsmt
        estfish_stn~~chla
'

modfitnorth=sem(modnorth, data=filter(fdr,region=="North"))
modfitnorth_dtr=sem(modnorth, data=filter(fdr_dtr,region=="North"))
summary(modfitnorth, standardized=T, rsq=T)
## lavaan 0.6.17 ended normally after 37 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        30
## 
##                                                   Used       Total
##   Number of observations                            41          47
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 4.572
##   Degrees of freedom                                 5
##   P-value (Chi-square)                           0.470
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   fish =~                                                               
##     estfish           1.000                               0.828    0.808
##     estfish_stn       0.901    0.169    5.324    0.000    0.747    0.843
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   chla ~                                                                
##     corbic            0.449    0.126    3.547    0.000    0.449    0.450
##     flow              0.210    0.146    1.437    0.151    0.210    0.201
##     temp              0.182    0.131    1.392    0.164    0.182    0.178
##     turbid            0.108    0.133    0.813    0.416    0.108    0.106
##   hzoop ~                                                               
##     chla              0.138    0.116    1.182    0.237    0.138    0.168
##     corbic            0.314    0.117    2.697    0.007    0.314    0.384
##     flow             -0.136    0.123   -1.110    0.267   -0.136   -0.159
##     temp              0.073    0.110    0.663    0.508    0.073    0.086
##     turbid            0.377    0.105    3.595    0.000    0.377    0.448
##   pzoop ~                                                               
##     chla              0.580    0.090    6.471    0.000    0.580    0.602
##     corbic            0.347    0.096    3.625    0.000    0.347    0.361
##     flow             -0.483    0.094   -5.119    0.000   -0.483   -0.479
##     temp             -0.004    0.084   -0.044    0.965   -0.004   -0.004
##     turbid            0.127    0.091    1.397    0.162    0.127    0.129
##     hzoop             0.157    0.118    1.328    0.184    0.157    0.134
##   fish ~                                                                
##     chla             -0.561    0.183   -3.058    0.002   -0.677   -0.686
##     hzoop             0.521    0.159    3.281    0.001    0.629    0.524
##     pzoop             0.517    0.200    2.587    0.010    0.624    0.610
##     corbic           -0.282    0.150   -1.885    0.059   -0.340   -0.346
##     flow              0.274    0.156    1.748    0.080    0.330    0.320
##     temp              0.077    0.109    0.708    0.479    0.093    0.092
##     turbid            0.273    0.121    2.266    0.023    0.330    0.326
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .estfish_stn ~~                                                        
##    .chla              0.281    0.114    2.474    0.013    0.281    0.595
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .estfish           0.364    0.113    3.219    0.001    0.364    0.346
##    .estfish_stn       0.317    0.108    2.940    0.003    0.317    0.404
##    .chla              0.705    0.156    4.528    0.000    0.705    0.686
##    .hzoop             0.392    0.087    4.528    0.000    0.392    0.565
##    .pzoop             0.225    0.050    4.528    0.000    0.225    0.235
##    .fish              0.188    0.090    2.100    0.036    0.274    0.274
## 
## R-Square:
##                    Estimate
##     estfish           0.654
##     estfish_stn       0.596
##     chla              0.314
##     hzoop             0.435
##     pzoop             0.765
##     fish              0.726
summary(modfitnorth_dtr, standardized=T, rsq=T)
## lavaan 0.6.17 ended normally after 32 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        30
## 
##                                                   Used       Total
##   Number of observations                            41          47
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 5.442
##   Degrees of freedom                                 5
##   P-value (Chi-square)                           0.364
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   fish =~                                                               
##     estfish           1.000                               0.611    0.616
##     estfish_stn       1.188    0.422    2.814    0.005    0.725    0.726
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   chla ~                                                                
##     corbic            0.517    0.158    3.271    0.001    0.517    0.486
##     flow              0.151    0.160    0.945    0.344    0.151    0.143
##     temp              0.173    0.134    1.291    0.197    0.173    0.167
##     turbid            0.200    0.141    1.412    0.158    0.200    0.193
##   hzoop ~                                                               
##     chla              0.365    0.157    2.324    0.020    0.365    0.393
##     corbic           -0.062    0.186   -0.336    0.737   -0.062   -0.063
##     flow              0.026    0.179    0.148    0.883    0.026    0.027
##     temp              0.160    0.151    1.062    0.288    0.160    0.166
##     turbid           -0.069    0.161   -0.431    0.667   -0.069   -0.072
##   pzoop ~                                                               
##     chla              0.751    0.109    6.910    0.000    0.751    0.730
##     corbic            0.292    0.121    2.413    0.016    0.292    0.267
##     flow             -0.539    0.117   -4.623    0.000   -0.539   -0.497
##     temp              0.020    0.099    0.197    0.843    0.020    0.018
##     turbid            0.007    0.105    0.066    0.947    0.007    0.007
##     hzoop             0.020    0.102    0.193    0.847    0.020    0.018
##   fish ~                                                                
##     chla             -0.425    0.217   -1.961    0.050   -0.696   -0.713
##     hzoop             0.098    0.108    0.911    0.362    0.161    0.153
##     pzoop             0.332    0.182    1.826    0.068    0.544    0.573
##     corbic           -0.411    0.189   -2.179    0.029   -0.673   -0.648
##     flow              0.339    0.177    1.911    0.056    0.555    0.538
##     temp              0.137    0.118    1.163    0.245    0.225    0.222
##     turbid            0.052    0.123    0.425    0.671    0.086    0.085
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .estfish_stn ~~                                                        
##    .chla              0.460    0.184    2.496    0.013    0.460    0.676
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .estfish           0.611    0.169    3.607    0.000    0.611    0.621
##    .estfish_stn       0.623    0.240    2.593    0.010    0.623    0.624
##    .chla              0.743    0.164    4.528    0.000    0.743    0.708
##    .hzoop             0.750    0.166    4.528    0.000    0.750    0.830
##    .pzoop             0.318    0.070    4.528    0.000    0.318    0.286
##    .fish              0.166    0.109    1.524    0.128    0.444    0.444
## 
## R-Square:
##                    Estimate
##     estfish           0.379
##     estfish_stn       0.376
##     chla              0.292
##     hzoop             0.170
##     pzoop             0.714
##     fish              0.556
labelsnorth <- createLabels(modfitnorth, cnames)

# residuals(modfitnorth)
# modificationindices(modfitnorth)

South

#1
# modsouth='zoop=~hcope+mysid
#         #fish=~estfish_bsmt+estfish_bsot
#         zoop~chla+corbic+flow
#         chla~corbic+flow
#         estfish_bsmt~zoop+flow
#         estfish_bsot~zoop+flow
# '
#2
# modsouth='chla~corbic+flow
#         tzoop~chla+corbic+flow
#         estfish_bsmt~tzoop+flow+corbic+sside
#         estfish_bsot~tzoop+flow+corbic+sside
# '
#3
# modsouth='chla~corbic+flow+temp+turbid
#         tzoop~chla+corbic+flow+temp+turbid
#         amphi~chla+corbic+flow+temp+turbid
#         estfish_bsmt~tzoop+amphi+flow+temp+turbid+corbic+sside
#         #estfish_bsot~tzoop+amphi+flow+temp+turbid+corbic+sside
#         amphi~~tzoop
# '
#4
# modsouth='chla~corbic+flow+temp+turbid
#         hzoop~chla+corbic+flow+temp+turbid
#         pzoop~chla+corbic+flow+temp+turbid+hzoop
#         amphi~chla+corbic+flow+temp+turbid
#         estfish_bsmt~hzoop+pzoop+amphi+flow+temp+turbid+corbic+sside
#         amphi~~hzoop+pzoop
# '
#5
modsouth='chla~corbic+flow+temp+turbid
        hzoop~chla+corbic+flow+temp+turbid
        pzoop~chla+corbic+flow+temp+turbid+hzoop
        fish~hzoop+pzoop+corbic+flow+temp+turbid
        fish=~estfish+estfish_stn+estfish_bsmt
'
modsouth_dtr='chla~corbic+flow+temp+turbid
        hzoop~chla+corbic+flow+temp+turbid
        pzoop~chla+corbic+flow+temp+turbid+hzoop
        fish~hzoop+pzoop+corbic+flow+temp+turbid
        fish=~estfish #+estfish_stn+estfish_bsmt
'
modfitsouth=sem(modsouth, data=filter(fdr,region=="South"))
modfitsouth_dtr=sem(modsouth_dtr, data=filter(fdr_dtr,region=="South"))
summary(modfitsouth, standardized=T, rsq=T)
## lavaan 0.6.17 ended normally after 30 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        30
## 
##                                                   Used       Total
##   Number of observations                            40          47
## 
## Model Test User Model:
##                                                       
##   Test statistic                                17.817
##   Degrees of freedom                                15
##   P-value (Chi-square)                           0.272
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   fish =~                                                               
##     estfish           1.000                               0.913    0.921
##     estfish_stn       0.776    0.124    6.275    0.000    0.709    0.764
##     estfish_bsmt      0.630    0.152    4.139    0.000    0.575    0.583
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   chla ~                                                                
##     corbic            0.284    0.149    1.906    0.057    0.284    0.283
##     flow             -0.124    0.137   -0.906    0.365   -0.124   -0.128
##     temp              0.081    0.140    0.578    0.563    0.081    0.081
##     turbid            0.392    0.158    2.478    0.013    0.392    0.373
##   hzoop ~                                                               
##     chla              0.449    0.107    4.213    0.000    0.449    0.523
##     corbic            0.326    0.105    3.104    0.002    0.326    0.378
##     flow             -0.052    0.093   -0.562    0.574   -0.052   -0.063
##     temp             -0.022    0.095   -0.229    0.819   -0.022   -0.025
##     turbid           -0.021    0.115   -0.182    0.855   -0.021   -0.023
##   pzoop ~                                                               
##     chla              0.493    0.127    3.894    0.000    0.493    0.516
##     corbic           -0.037    0.116   -0.323    0.747   -0.037   -0.039
##     flow             -0.066    0.092   -0.711    0.477   -0.066   -0.071
##     temp              0.047    0.094    0.503    0.615    0.047    0.049
##     turbid            0.179    0.113    1.579    0.114    0.179    0.178
##     hzoop             0.311    0.156    1.989    0.047    0.311    0.279
##   fish ~                                                                
##     hzoop             0.271    0.131    2.065    0.039    0.297    0.255
##     pzoop            -0.116    0.113   -1.027    0.304   -0.127   -0.121
##     corbic            0.160    0.097    1.652    0.099    0.175    0.175
##     flow             -0.070    0.078   -0.904    0.366   -0.077   -0.080
##     temp              0.016    0.079    0.198    0.843    0.017    0.017
##     turbid            0.763    0.101    7.585    0.000    0.835    0.794
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .estfish           0.149    0.068    2.186    0.029    0.149    0.152
##    .estfish_stn       0.359    0.090    4.001    0.000    0.359    0.417
##    .estfish_bsmt      0.644    0.150    4.304    0.000    0.644    0.661
##    .chla              0.723    0.162    4.472    0.000    0.723    0.724
##    .hzoop             0.329    0.074    4.472    0.000    0.329    0.446
##    .pzoop             0.322    0.072    4.472    0.000    0.322    0.352
##    .fish              0.114    0.065    1.756    0.079    0.136    0.136
## 
## R-Square:
##                    Estimate
##     estfish           0.848
##     estfish_stn       0.583
##     estfish_bsmt      0.339
##     chla              0.276
##     hzoop             0.554
##     pzoop             0.648
##     fish              0.864
summary(modfitsouth_dtr, standardized=T, rsq=T)
## lavaan 0.6.17 ended normally after 28 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        25
## 
##                                                   Used       Total
##   Number of observations                            41          47
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 0.334
##   Degrees of freedom                                 1
##   P-value (Chi-square)                           0.564
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   fish =~                                                               
##     estfish           1.000                               0.909    1.000
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   chla ~                                                                
##     corbic            0.120    0.157    0.764    0.445    0.120    0.120
##     flow             -0.033    0.162   -0.202    0.840   -0.033   -0.034
##     temp              0.176    0.158    1.110    0.267    0.176    0.176
##     turbid           -0.048    0.163   -0.291    0.771   -0.048   -0.048
##   hzoop ~                                                               
##     chla              0.447    0.123    3.629    0.000    0.447    0.458
##     corbic            0.274    0.125    2.199    0.028    0.274    0.281
##     flow             -0.008    0.128   -0.062    0.951   -0.008   -0.008
##     temp              0.036    0.127    0.283    0.777    0.036    0.037
##     turbid           -0.179    0.129   -1.390    0.164   -0.179   -0.184
##   pzoop ~                                                               
##     chla              0.536    0.138    3.897    0.000    0.536    0.516
##     corbic           -0.123    0.128   -0.960    0.337   -0.123   -0.118
##     flow             -0.013    0.124   -0.101    0.920   -0.013   -0.012
##     temp              0.150    0.123    1.222    0.222    0.150    0.145
##     turbid           -0.094    0.128   -0.732    0.464   -0.094   -0.090
##     hzoop             0.232    0.152    1.528    0.126    0.232    0.218
##   fish ~                                                                
##     hzoop             0.241    0.159    1.518    0.129    0.265    0.255
##     pzoop            -0.267    0.142   -1.883    0.060   -0.293   -0.301
##     corbic            0.104    0.137    0.753    0.451    0.114    0.113
##     flow              0.005    0.132    0.034    0.973    0.005    0.005
##     temp              0.108    0.133    0.814    0.416    0.119    0.118
##     turbid            0.357    0.136    2.624    0.009    0.393    0.388
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .estfish           0.000                               0.000    0.000
##    .chla              0.927    0.205    4.528    0.000    0.927    0.950
##    .hzoop             0.575    0.127    4.528    0.000    0.575    0.621
##    .pzoop             0.544    0.120    4.528    0.000    0.544    0.517
##    .fish              0.613    0.135    4.528    0.000    0.743    0.743
## 
## R-Square:
##                    Estimate
##     estfish           1.000
##     chla              0.050
##     hzoop             0.379
##     pzoop             0.483
##     fish              0.257
labelssouth <- createLabels(modfitsouth, cnames)

# residuals(modfitsouth)
# modificationindices(modfitsouth)

Nice plots

Original units

West

North

South

Detrended

West

North

South

Save updated SEM diagrams

plot_grobs <- map(list(figW, figN, figS), ~convert_html_to_grob(.x, 2000))
combined_figure <- ggarrange(plotlist=plot_grobs, labels="auto", 
                             font.label=list(size=9)) %>%
  annotate_figure(top = text_grob("Annual SEM (regional)",
                                  color = "black",
                                  face = "bold",
                                  size = 9))# + 
#theme(plot.margin = unit(c(1,0,0,0), "lines"))

ggsave('./fig_output/sem_annual_regions.png', combined_figure, width=6, height=6,
       dpi=300, bg = "white")

Combine and save annual no region model and annual regional models

load('./fig_output/sem_annual_noregions.RData')

plot_grobs <- map(list(sem_annual_noregions, figN, figW, figS),
                  ~convert_html_to_grob(.x, 2000))
combined_figure <- ggarrange(plotlist=plot_grobs, labels=c("(a)", "(b)", "(c)", "(d)"), 
                             font.label=list(size=12)) %>%
  annotate_figure(top = text_grob("Annual SEMs",
                                  color = "black",
                                  face = "bold",
                                  size = 14))

ggsave('./fig_output/sem_annual.tiff', combined_figure, width=7, height=7, dpi=600, 
       bg = "white")

#detrended
plot_grobs <- map(list(sem_annual_noregions_dtr, figNdt, figWdt, figSdt),
                  ~convert_html_to_grob(.x, 2000))
combined_figure_dtr <- ggarrange(plotlist=plot_grobs, labels=c("(a)", "(b)", "(c)", "(d)"), 
                                 font.label=list(size=9)) %>%
  annotate_figure(top = text_grob("Annual SEMs, Detrended",
                                  color = "black",
                                  face = "bold",
                                  size = 11))

ggsave('./fig_output/sem_annual_detrend.png', combined_figure_dtr, width=8, height=7,
       dpi=300, bg = "white")

Save model coefficients for manuscript table

load("./fig_output/coeftable_we.Rdata")
bind_rows(
  coeftable_we,
  coef_tabler(modfitwest, modfitnorth, modfitsouth, name="Original"))%>%
  arrange(Model, Region)%>%
  write.csv("fig_output/annual coefficients.csv", row.names = FALSE)