###Load libraries#####
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.3.3
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Warning: package 'ggplot2' was built under R version 3.3.3
## Warning: package 'tibble' was built under R version 3.3.3
## Warning: package 'tidyr' was built under R version 3.3.3
## Warning: package 'readr' was built under R version 3.3.3
## Warning: package 'purrr' was built under R version 3.3.3
## Warning: package 'dplyr' was built under R version 3.3.3
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag():    dplyr, stats
library(lme4)
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## The following object is masked from 'package:tidyr':
## 
##     expand
library(knitr)
library(broom)
library(forcats)
library(lmerTest)
## Warning: package 'lmerTest' was built under R version 3.3.3
## 
## Attaching package: 'lmerTest'
## The following object is masked from 'package:lme4':
## 
##     lmer
## The following object is masked from 'package:stats':
## 
##     step
library(MuMIn)
## Warning: package 'MuMIn' was built under R version 3.3.3
library(knitr)
library(sjPlot)
## Warning: package 'sjPlot' was built under R version 3.3.3
setwd ("P:/MTM2 Oranga Taiao/EMP Data/EMP Data/Biotic Index calculator/Index calculation/MTM new Data")
source('theme_javier.R')
theme_set(theme_javier(8))
options(digits = 3)

###read data#####
all_ind <-
  read_csv('all_indices_truncated.csv', col_types =  cols(Date = 'D')) %>%
  mutate(Type = factor(Type))
## Warning: package 'bindrcpp' was built under R version 3.3.3
dat_mult <-
  all_ind %>%
  dplyr::select(
    Region,
    n,
    council,
    estuary,
    site,
    cesy,
    ces,
    year,
    AMBI:MEDOCC,
    BQI:TBI,
    logN,
    S,
    Type,
    metals,
    sqrtTP,
    sqrtmud
  ) %>%
  drop_na() %>%
  mutate(Region = fct_collapse(
    Region,
    `North Eastern`  = c('North Eastern', 'Western North Island')
  )) %>%
  dplyr::select(-MEDOCC) %>%
  mutate(
    TBI = sqrt(TBI),
    S = sqrt(S),
    BENTIX = sqrt(max(BENTIX) - BENTIX) * -1,
    AMBI = sqrt(max(AMBI) - AMBI),
    AMBI_S = AMBI_S * -1,
    Mud = scale(sqrtmud),
    TP = scale(sqrtTP),
    Metals = scale(metals)
  ) 

dat_mult_centered <-
  dat_mult %>% 
  gather(index, value, AMBI:S) %>%
  group_by(index) %>%
  mutate(value = scale(value))

ggplot(dat_mult_centered, aes(x = value)) +
  geom_histogram() +
  facet_wrap( ~ index)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

##Data structure####
kable(with(dat_mult_centered, table(council, Region)))
Cook Strait Eastern North Island North Eastern Southern
bayofplentyregionalcouncil 0 0 396 0
environmentcanterbury 0 0 0 27
environmentcanterburyccc 0 0 0 18
environmentsouthland 0 0 0 522
greaterwellington 72 0 0 0
hawkesbayregionalcouncil 0 432 0 0
marlboroughdistrictcouncil 72 0 0 0
nelsoncitycouncil 27 0 0 0
northlandregionalcouncil 0 0 432 0
tasmandistrictcouncil 135 0 0 0
kable(with(dat_mult_centered, table(estuary, Region)))
Cook Strait Eastern North Island North Eastern Southern
ahuriri 0 351 0 0
avonheathcote 0 0 0 45
awarua 0 0 0 18
bluff 0 0 0 18
delaware 27 0 0 0
fortrose 0 0 0 45
haldane 0 0 0 45
havelock 72 0 0 0
jacobsriver 0 0 0 162
kaipara 0 0 27 0
mangonui 0 0 117 0
moutere 36 0 0 0
newriver 0 0 0 162
ngunguru 0 0 180 0
pauatahanui 36 0 0 0
porangahau 0 81 0 0
porirua 36 0 0 0
ruakaka 0 0 18 0
ruataniwha 27 0 0 0
tauranga 0 0 396 0
waikawa 0 0 0 72
waimea 72 0 0 0
whangarei 0 0 36 0
whangaroa 0 0 54 0
kable(with(dat_mult_centered, table(estuary, year)))
2001 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016
ahuriri 0 0 0 0 27 36 36 36 36 36 36 36 36 36 0
avonheathcote 27 0 0 0 0 0 0 0 0 18 0 0 0 0 0
awarua 0 0 0 18 0 0 0 0 0 0 0 0 0 0 0
bluff 0 0 0 18 0 0 0 0 0 0 0 0 0 0 0
delaware 0 0 0 0 0 0 0 27 0 0 0 0 0 0 0
fortrose 0 0 18 9 9 0 0 9 0 0 0 0 0 0 0
haldane 0 0 0 0 9 0 0 9 9 9 0 9 0 0 0
havelock 18 0 0 0 0 0 0 0 0 0 0 0 18 36 0
jacobsriver 0 27 27 27 27 0 0 0 0 27 18 9 0 0 0
kaipara 27 0 0 0 0 0 0 0 0 0 0 0 0 0 0
mangonui 0 0 0 0 0 0 0 0 0 0 0 0 0 0 117
moutere 0 0 0 0 18 0 0 0 0 0 0 18 0 0 0
newriver 36 27 27 27 0 0 0 0 27 0 0 18 0 0 0
ngunguru 0 0 0 0 0 0 0 0 0 0 0 0 0 0 180
pauatahanui 0 0 0 0 0 0 0 18 18 0 0 0 0 0 0
porangahau 0 0 0 0 0 9 9 9 9 9 9 9 9 9 0
porirua 0 0 0 0 0 0 0 18 18 0 0 0 0 0 0
ruakaka 0 0 0 0 0 0 18 0 0 0 0 0 0 0 0
ruataniwha 27 0 0 0 0 0 0 0 0 0 0 0 0 0 0
tauranga 0 0 0 0 0 0 0 0 0 396 0 0 0 0 0
waikawa 0 0 0 18 18 18 18 0 0 0 0 0 0 0 0
waimea 36 0 0 0 0 0 0 0 0 0 0 0 36 0 0
whangarei 0 0 0 0 0 0 36 0 0 0 0 0 0 0 0
whangaroa 0 0 0 0 0 0 0 18 18 18 0 0 0 0 0
###Linear mixed models using lmer#####
EG_models8 <-
  dat_mult_centered %>%
  do(
    fit_index8 = lmer(
      value ~ 
        Mud +
        Metals +
        TP +
        (1|Type) + 
        (0+Mud|Type) + 
        (0+TP|Type)+ 
        (0+Metals|Type) +
        (1|year) +
        (1|Region/estuary),
      data = .,
      REML = FALSE,
      weights = sqrt(n),
      na.action = "na.fail"
    )
  )

index_coef_all8 <-
  tidy(EG_models8, fit_index8, conf.int = T) %>%
  data.frame(.) %>% 
  mutate(
  term = factor(term),
  term = fct_recode (
    term,
    Intercept = '(Intercept)',
    Type = 'sd_(Intercept).Type.3',
    `Mud x Type` = 'sd_Mud.Type.2',
    `Metals x Type` = 'sd_Metals.Type',
    `TP x Type` = 'sd_TP.Type.1',
    `Estuary (Region)` = 'sd_(Intercept).estuary.Region',
    Year = 'sd_(Intercept).year',
    Region = 'sd_(Intercept).Region',
    Residual = 'sd_Observation.Residual'
  )
)

kable(index_coef_all8)
index term estimate std.error statistic conf.low conf.high group
AMBI Intercept -0.127 0.143 -0.883 -0.408 0.154 fixed
AMBI Mud -0.420 0.102 -4.136 -0.619 -0.221 fixed
AMBI Metals -0.170 0.105 -1.611 -0.376 0.037 fixed
AMBI TP 0.025 0.108 0.230 -0.187 0.237 fixed
AMBI Estuary (Region) 0.358 NA NA NA NA estuary.Region
AMBI Year 0.096 NA NA NA NA year
AMBI Region 0.000 NA NA NA NA Region
AMBI Metals x Type 0.000 NA NA NA NA Type
AMBI TP x Type 0.000 NA NA NA NA Type.1
AMBI Mud x Type 0.072 NA NA NA NA Type.2
AMBI Type 0.138 NA NA NA NA Type.3
AMBI Residual 1.419 NA NA NA NA Residual
AMBI_S Intercept -0.104 0.110 -0.940 -0.320 0.112 fixed
AMBI_S Mud -0.570 0.134 -4.247 -0.833 -0.307 fixed
AMBI_S Metals -0.158 0.094 -1.680 -0.341 0.026 fixed
AMBI_S TP -0.009 0.097 -0.095 -0.198 0.180 fixed
AMBI_S Estuary (Region) 0.343 NA NA NA NA estuary.Region
AMBI_S Year 0.085 NA NA NA NA year
AMBI_S Region 0.000 NA NA NA NA Region
AMBI_S Metals x Type 0.000 NA NA NA NA Type
AMBI_S TP x Type 0.000 NA NA NA NA Type.1
AMBI_S Mud x Type 0.154 NA NA NA NA Type.2
AMBI_S Type 0.074 NA NA NA NA Type.3
AMBI_S Residual 1.251 NA NA NA NA Residual
BENTIX Intercept -0.163 0.176 -0.928 -0.508 0.181 fixed
BENTIX Mud -0.407 0.086 -4.703 -0.576 -0.237 fixed
BENTIX Metals -0.243 0.101 -2.406 -0.441 -0.045 fixed
BENTIX TP 0.213 0.105 2.036 0.008 0.419 fixed
BENTIX Estuary (Region) 0.567 NA NA NA NA estuary.Region
BENTIX Year 0.000 NA NA NA NA year
BENTIX Region 0.000 NA NA NA NA Region
BENTIX Metals x Type 0.000 NA NA NA NA Type
BENTIX TP x Type 0.000 NA NA NA NA Type.1
BENTIX Mud x Type 0.000 NA NA NA NA Type.2
BENTIX Type 0.161 NA NA NA NA Type.3
BENTIX Residual 1.283 NA NA NA NA Residual
BQI Intercept -0.039 0.285 -0.136 -0.598 0.520 fixed
BQI Mud -0.271 0.063 -4.286 -0.395 -0.147 fixed
BQI Metals -0.223 0.074 -3.004 -0.369 -0.078 fixed
BQI TP 0.125 0.094 1.324 -0.060 0.309 fixed
BQI Estuary (Region) 0.525 NA NA NA NA estuary.Region
BQI Year 0.098 NA NA NA NA year
BQI Region 0.506 NA NA NA NA Region
BQI Metals x Type 0.000 NA NA NA NA Type
BQI TP x Type 0.079 NA NA NA NA Type.1
BQI Mud x Type 0.000 NA NA NA NA Type.2
BQI Type 0.000 NA NA NA NA Type.3
BQI Residual 0.877 NA NA NA NA Residual
ITI Intercept -0.169 0.182 -0.930 -0.526 0.187 fixed
ITI Mud -0.329 0.085 -3.867 -0.496 -0.162 fixed
ITI Metals -0.095 0.101 -0.943 -0.294 0.103 fixed
ITI TP 0.318 0.102 3.113 0.118 0.518 fixed
ITI Estuary (Region) 0.550 NA NA NA NA estuary.Region
ITI Year 0.217 NA NA NA NA year
ITI Region 0.000 NA NA NA NA Region
ITI Metals x Type 0.000 NA NA NA NA Type
ITI TP x Type 0.000 NA NA NA NA Type.1
ITI Mud x Type 0.000 NA NA NA NA Type.2
ITI Type 0.159 NA NA NA NA Type.3
ITI Residual 1.209 NA NA NA NA Residual
logN Intercept -0.313 0.574 -0.545 -1.438 0.812 fixed
logN Mud 0.121 0.131 0.928 -0.135 0.378 fixed
logN Metals -0.103 0.080 -1.288 -0.259 0.054 fixed
logN TP 0.090 0.157 0.571 -0.218 0.397 fixed
logN Estuary (Region) 0.406 NA NA NA NA estuary.Region
logN Year 0.175 NA NA NA NA year
logN Region 0.916 NA NA NA NA Region
logN Metals x Type 0.000 NA NA NA NA Type
logN TP x Type 0.191 NA NA NA NA Type.1
logN Mud x Type 0.159 NA NA NA NA Type.2
logN Type 0.459 NA NA NA NA Type.3
logN Residual 0.930 NA NA NA NA Residual
M_AMBI Intercept -0.018 0.197 -0.089 -0.405 0.369 fixed
M_AMBI Mud -0.450 0.071 -6.302 -0.590 -0.310 fixed
M_AMBI Metals -0.154 0.084 -1.831 -0.318 0.011 fixed
M_AMBI TP 0.080 0.109 0.732 -0.133 0.293 fixed
M_AMBI Estuary (Region) 0.505 NA NA NA NA estuary.Region
M_AMBI Year 0.102 NA NA NA NA year
M_AMBI Region 0.303 NA NA NA NA Region
M_AMBI Metals x Type 0.000 NA NA NA NA Type
M_AMBI TP x Type 0.094 NA NA NA NA Type.1
M_AMBI Mud x Type 0.000 NA NA NA NA Type.2
M_AMBI Type 0.000 NA NA NA NA Type.3
M_AMBI Residual 1.015 NA NA NA NA Residual
S Intercept -0.048 0.256 -0.189 -0.549 0.453 fixed
S Mud -0.254 0.064 -3.980 -0.379 -0.129 fixed
S Metals -0.185 0.076 -2.446 -0.333 -0.037 fixed
S TP 0.112 0.099 1.132 -0.082 0.307 fixed
S Estuary (Region) 0.505 NA NA NA NA estuary.Region
S Year 0.144 NA NA NA NA year
S Region 0.441 NA NA NA NA Region
S Metals x Type 0.000 NA NA NA NA Type
S TP x Type 0.089 NA NA NA NA Type.1
S Mud x Type 0.000 NA NA NA NA Type.2
S Type 0.000 NA NA NA NA Type.3
S Residual 0.878 NA NA NA NA Residual
TBI Intercept -0.070 0.259 -0.269 -0.578 0.439 fixed
TBI Mud -0.279 0.060 -4.627 -0.397 -0.161 fixed
TBI Metals -0.248 0.072 -3.453 -0.388 -0.107 fixed
TBI TP 0.197 0.084 2.328 0.031 0.362 fixed
TBI Estuary (Region) 0.413 NA NA NA NA estuary.Region
TBI Year 0.191 NA NA NA NA year
TBI Region 0.464 NA NA NA NA Region
TBI Metals x Type 0.000 NA NA NA NA Type
TBI TP x Type 0.063 NA NA NA NA Type.1
TBI Mud x Type 0.000 NA NA NA NA Type.2
TBI Type 0.000 NA NA NA NA Type.3
TBI Residual 0.825 NA NA NA NA Residual
##Model summary####
index_Summ8  <-
  glance(EG_models8, fit_index8)
kable(index_Summ8)
index sigma logLik AIC BIC deviance df.residual
AMBI 1.419 -321 666 708 642 225
AMBI_S 1.251 -293 609 651 585 225
BENTIX 1.283 -304 632 674 608 225
BQI 0.877 -226 475 517 451 225
ITI 1.209 -295 614 656 590 225
logN 0.930 -243 510 551 486 225
M_AMBI 1.015 -255 534 576 510 225
S 0.878 -226 477 519 453 225
TBI 0.825 -212 448 489 424 225
ggplot(index_coef_all8,
       aes(
         x = estimate,
         y = fct_rev(fct_inorder(term)),
         color = term
       )) +
  geom_point() +
  geom_errorbarh(aes(
    xmin = conf.low,
    xmax = conf.high,
    height = .2
  )) +
  facet_wrap( ~ index) +
  geom_vline(xintercept = 0,
             lty = 2,
             col = 'gray60') +
  ylab('') +
  xlab('Coefficients') +
  scale_color_discrete(guide = F) +
  theme_javier()
## Warning: Removed 72 rows containing missing values (geom_errorbarh).

###residuals
index_residuals8  <-
  augment(EG_models8, fit_index8)
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
## Warning: Deprecated: please use `purrr::possibly()` instead
ggplot(index_residuals8) +
  geom_point(aes(x = .fitted, y = .resid), alpha = .3) +
  facet_wrap(~ index, scale = 'free') +
  geom_hline(yintercept = 0,
             lty = 2,
             col = 2)

ggplot(index_residuals8) +
  stat_qq(aes(sample = .wtres), alpha = .3) +
  facet_wrap(~ index, scale = 'free') +
  geom_abline(
    intercept =  0,
    slope = 1,
    lty = 2,
    col = 2
  )

library(MuMIn)
pseudo_rsq <-
  EG_models8 %>%
  ungroup() %>%
  mutate(r_sq = map(fit_index8, r.squaredGLMM),
         R_sq_dat = map(r_sq, data.frame)) %>%
  unnest(R_sq_dat) %>%
  rename(R_sq = .x..i..) %>%
  mutate(Type = rep(c('Marginal', 'Conditional'), 9),
         index = factor(index)) %>%
  print()
## # A tibble: 18 x 3
##     index    R_sq        Type
##    <fctr>   <dbl>       <chr>
##  1   AMBI 0.10479    Marginal
##  2   AMBI 0.17120 Conditional
##  3 AMBI_S 0.20408    Marginal
##  4 AMBI_S 0.27534 Conditional
##  5 BENTIX 0.08920    Marginal
##  6 BENTIX 0.24812 Conditional
##  7    BQI 0.08258    Marginal
##  8    BQI 0.46386 Conditional
##  9    ITI 0.04902    Marginal
## 10    ITI 0.24316 Conditional
## 11   logN 0.00886    Marginal
## 12   logN 0.60496 Conditional
## 13 M_AMBI 0.14730    Marginal
## 14 M_AMBI 0.37065 Conditional
## 15      S 0.07050    Marginal
## 16      S 0.42625 Conditional
## 17    TBI 0.09270    Marginal
## 18    TBI 0.44175 Conditional
ggplot(pseudo_rsq) +
  geom_col(aes(
    x = fct_reorder(index, R_sq),
    y = R_sq,
    fill = Type
  ), position = 'stack') +
  labs(y = 'Pseudo R - square', x = '') +
  coord_flip() +
  theme(legend.position = c(.9, .1))

####Random effects###
ran_ef <- 
  EG_models8 %>%
  ungroup() %>%
  mutate(ran_ef = map(fit_index8,~sjp.lmer(.x, type = "re", show.ci = F, sort.est = "(Intercept)")),
         ran_ef1 = map(fit_index8,~sjp.lmer(.x, type = "ri.slope")))
## Plotting random effects...
## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...

## Plotting random effects...