# Name: Rui Miguel Carvalho
# Date of creation: 7/2/2020



# Libraries -----------
library(BAT)
## 
## Attaching package: 'BAT'
## The following object is masked from 'package:base':
## 
##     beta
library(readr)
library(FD) 
## Loading required package: ade4
## 
## Attaching package: 'ade4'
## The following object is masked from 'package:BAT':
## 
##     originality
## Loading required package: ape
## Loading required package: geometry
## Loading required package: vegan
## Loading required package: permute
## Loading required package: lattice
## This is vegan 2.5-5
library(alphahull)
## Registered S3 method overwritten by 'R.oo':
##   method        from       
##   throw.default R.methodsS3
## 
## Attaching package: 'alphahull'
## The following object is masked from 'package:ape':
## 
##     complement
library(hypervolume)
## Loading required package: Rcpp
## Loading required package: rgl
library(car)
## Loading required package: carData
library(MASS)
library(lme4)
## Loading required package: Matrix
## Registered S3 methods overwritten by 'lme4':
##   method                          from
##   cooks.distance.influence.merMod car 
##   influence.merMod                car 
##   dfbeta.influence.merMod         car 
##   dfbetas.influence.merMod        car
#source(file = "HighstatLibV10.R") #cool tools to support
#library(factoextra) # Useful for PCA analysis
library(here)
## here() starts at /Users/santorui/Documents/GitHub/FinlandAnalysis
library(data.table) # to work with data
library(dplyr)      # to manage data
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:data.table':
## 
##     between, first, last
## The following object is masked from 'package:MASS':
## 
##     select
## The following object is masked from 'package:car':
## 
##     recode
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(magrittr)   # to use the pipe operator %>% 
library(MuMIn)
## Registered S3 method overwritten by 'MuMIn':
##   method         from
##   predict.merMod lme4
library(glmmTMB)
## Warning in checkMatrixPackageVersion(): Package version inconsistency detected.
## TMB was built with Matrix version 1.2.17
## Current Matrix version is 1.2.18
## Please re-install 'TMB' from source using install.packages('TMB', type = 'source') or ask CRAN for a binary version of 'TMB' matching CRAN's 'Matrix' package
library(bbmle)
## Loading required package: stats4
## 
## Attaching package: 'bbmle'
## The following object is masked from 'package:MuMIn':
## 
##     AICc
## The following object is masked from 'package:dplyr':
## 
##     slice
library(performance)
library(see)
library(pscl)
## Classes and Methods for R developed in the
## Political Science Computational Laboratory
## Department of Political Science
## Stanford University
## Simon Jackman
## hurdle and zeroinfl functions by Achim Zeileis
library(DHARMa)
## Registered S3 method overwritten by 'DHARMa':
##   method        from   
##   refit.glmmTMB glmmTMB
library(rlang)
## 
## Attaching package: 'rlang'
## The following object is masked from 'package:magrittr':
## 
##     set_names
## The following object is masked from 'package:data.table':
## 
##     :=
library(sjPlot)

# Load files -----------

plot.all <- read.csv2(here("data.veg","plots.alpha.csv"), row.names=1, header=TRUE,  stringsAsFactors = T, dec = ".", sep = ",")
plot.inv <- read.csv2(here("data.veg","plots.alphainv.csv"), row.names=1, header=TRUE,  stringsAsFactors = T, dec = ".", sep = ",")


###########################################################################
#                           Generating models                           ###
###########################################################################

## Fake database

dummy1 <- read.csv2(here("data.veg","dummy3.csv"), header=TRUE, row.names = 1,  stringsAsFactors = T,sep = ",", dec = ".")

alpha.all <- dredge(glmmTMB(resp ~ dist_begin + dist_trail + (1 | ID), data= dummy1 , family = "poisson"))
## Fixed terms are "cond((Int))" and "disp((Int))"
alpha.all
## Global model call: glmmTMB(formula = resp ~ dist_begin + dist_trail + (1 | ID), 
##     data = dummy1, family = "poisson", ziformula = ~0, dispformula = ~1)
## ---
## Model selection table 
##   cnd((Int)) dsp((Int)) cnd(dst_bgn) cnd(dst_trl) df   logLik   AICc
## 4      2.888          +       0.3883       0.3574  4  -55.641  125.0
## 3      3.563          +                    0.5148  3 -285.708  580.4
## 2      2.922          +       0.5288               3 -324.027  657.1
## 1      4.033          +                            2 -915.760 1836.9
##     delta weight
## 4    0.00      1
## 3  455.42      0
## 2  532.06      0
## 1 1711.86      0
## Models ranked by AICc(x) 
## Random terms (all models): 
## 'cond(1 | ID)'
alpha.all1 <- glmmTMB(resp ~ dist_begin + dist_trail + (1 | ID), data= dummy1 , family = "poisson")
summary(alpha.all1)
##  Family: poisson  ( log )
## Formula:          resp ~ dist_begin + dist_trail + (1 | ID)
## Data: dummy1
## 
##      AIC      BIC   logLik deviance df.resid 
##    119.3    121.2    -55.6    111.3        8 
## 
## Random effects:
## 
## Conditional model:
##  Groups Name        Variance Std.Dev.
##  ID     (Intercept) 7.051    2.655   
## Number of obs: 12, groups:  ID, 2
## 
## Conditional model:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  2.88818    1.88083   1.536    0.125    
## dist_begin   0.38831    0.01858  20.899   <2e-16 ***
## dist_trail   0.35744    0.01573  22.721   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
performance::r2(alpha.all1)
## # R2 for Mixed Models
## 
##   Conditional R2: 0.998
##      Marginal R2: 0.059
alpha.all2 <- glm(resp ~ dist_begin , data= dummy1 , family = "poisson")
summary(alpha.all2)
## 
## Call:
## glm(formula = resp ~ dist_begin, family = "poisson", data = dummy1)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -35.277  -26.587   -5.217   10.390   33.398  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  4.88514    0.04168  117.19   <2e-16 ***
## dist_begin   0.52885    0.01677   31.54   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 8130.7  on 11  degrees of freedom
## Residual deviance: 6947.2  on 10  degrees of freedom
## AIC: 7019.5
## 
## Number of Fisher Scoring iterations: 6
performance::r2(alpha.all2)
## $R2_Nagelkerke
## Nagelkerke's R2 
##               1
alpha.all3 <- glmmTMB(resp ~  dist_trail + (1 | ID), data= dummy , family = "poisson")
## Error in eval(substitute(offset), data, enclos = environment(formula)): invalid 'envir' argument of type 'closure'
summary(alpha.all3)
## Error in summary(alpha.all3): object 'alpha.all3' not found
performance::r2(alpha.all3)
## Error in performance::r2(alpha.all3): object 'alpha.all3' not found
alpha.all4 <- glmmTMB(resp ~  dist_begin + (1 | ID), data= dummy , family = "poisson")
## Error in eval(substitute(offset), data, enclos = environment(formula)): invalid 'envir' argument of type 'closure'
summary(alpha.all4)
## Error in summary(alpha.all4): object 'alpha.all4' not found
performance::r2(alpha.all4)
## Error in performance::r2(alpha.all4): object 'alpha.all4' not found
## Uploading results for vegetation

test1veg <- read.csv2(here("data.veg","test1veg.CSV"), header=TRUE, row.names = 1,  stringsAsFactors = T,sep = ",", dec = ".")
test1veg$percent.alfa[test1veg$percent.alfa == 0] <- 0.001
test1veg$percent.alfa[test1veg$percent.alfa == 1] <- 0.999
test1veg$percent.abund[test1veg$percent.abund == 0] <- 0.001
test1veg$percent.abund[test1veg$percent.abund == 1] <- 0.999


alpha.all <- dredge(glmmTMB(alpha.all ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= test1veg , family = "poisson"))
## Fixed terms are "cond((Int))" and "disp((Int))"
alpha.all
## Global model call: glmmTMB(formula = alpha.all ~ Dist_edge_std + Dist_trail_std + 
##     Dist_trail_beginning_std + (1 | ForestID), data = test1veg, 
##     family = "poisson", ziformula = ~0, dispformula = ~1)
## ---
## Model selection table 
##   cnd((Int)) dsp((Int)) cnd(Dst_edg_std) cnd(Dst_trl_bgn_std)
## 1      2.091          +                                      
## 5      2.075          +                                      
## 3      2.101          +                              -0.02198
## 2      2.086          +         0.013110                     
## 7      2.084          +                              -0.02074
## 6      2.076          +        -0.005063                     
## 4      2.096          +         0.020980             -0.03154
## 8      2.084          +         0.001905             -0.02164
##   cnd(Dst_trl_std) df   logLik  AICc delta weight
## 1                   2 -162.004 328.2  0.00  0.385
## 5          0.03729  3 -161.715 329.8  1.62  0.171
## 3                   3 -161.953 330.3  2.10  0.135
## 2                   3 -161.972 330.3  2.13  0.132
## 7          0.03691  4 -161.670 332.0  3.80  0.057
## 6          0.03946  4 -161.711 332.1  3.89  0.055
## 4                   4 -161.879 332.4  4.22  0.047
## 8          0.03608  5 -161.670 334.4  6.15  0.018
## Models ranked by AICc(x) 
## Random terms (all models): 
## 'cond(1 | ForestID)'
alpha.all1 <- glmer(alpha.all ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std  + (1 | ForestID), data= test1veg , family = "poisson")
summary (alpha.all1)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: poisson  ( log )
## Formula: 
## alpha.all ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std +  
##     (1 | ForestID)
##    Data: test1veg
## 
##      AIC      BIC   logLik deviance df.resid 
##    333.3    344.2   -161.7    323.3       60 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -2.4983 -0.5602 -0.1193  0.3897  2.3903 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  ForestID (Intercept) 0.1469   0.3833  
## Number of obs: 65, groups:  ForestID, 4
## 
## Fixed effects:
##                           Estimate Std. Error z value Pr(>|z|)    
## (Intercept)               2.083997   0.202375  10.298   <2e-16 ***
## Dist_edge_std             0.001908   0.061934   0.031    0.975    
## Dist_trail_std            0.036083   0.055326   0.652    0.514    
## Dist_trail_beginning_std -0.021639   0.075024  -0.288    0.773    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) Dst_d_ Dst_t_
## Dist_dg_std  0.000              
## Dst_trl_std -0.096 -0.489       
## Dst_trl_bg_ -0.142 -0.389  0.209
performance::r2(alpha.all1)
## # R2 for Mixed Models
## 
##   Conditional R2: 0.560
##      Marginal R2: 0.005
alpha.inv<- (glmmTMB(alpha.inv ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= test1veg , family = "poisson"))
alpha.inv
## Formula:          
## alpha.inv ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std +  
##     (1 | ForestID)
## Data: test1veg
##       AIC       BIC    logLik  df.resid 
## 140.27114 151.14308 -65.13557        60 
## Random-effects (co)variances:
## 
## Conditional model:
##  Groups   Name        Std.Dev.
##  ForestID (Intercept) 0.8867  
## 
## Number of obs: 65 / Conditional model: ForestID, 4
## 
## Fixed Effects:
## 
## Conditional model:
##              (Intercept)             Dist_edge_std  
##                 -0.30632                  -0.01404  
##           Dist_trail_std  Dist_trail_beginning_std  
##                 -0.10677                  -0.32615
alpha.inv1<- glmmTMB(alpha.inv ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= test1veg , family = "poisson")
summary(alpha.inv1)
##  Family: poisson  ( log )
## Formula:          
## alpha.inv ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std +  
##     (1 | ForestID)
## Data: test1veg
## 
##      AIC      BIC   logLik deviance df.resid 
##    140.3    151.1    -65.1    130.3       60 
## 
## Random effects:
## 
## Conditional model:
##  Groups   Name        Variance Std.Dev.
##  ForestID (Intercept) 0.7863   0.8867  
## Number of obs: 65, groups:  ForestID, 4
## 
## Conditional model:
##                          Estimate Std. Error z value Pr(>|z|)
## (Intercept)              -0.30632    0.50548  -0.606    0.545
## Dist_edge_std            -0.01404    0.25538  -0.055    0.956
## Dist_trail_std           -0.10677    0.21160  -0.505    0.614
## Dist_trail_beginning_std -0.32615    0.33575  -0.971    0.331
performance::r2(alpha.inv1)
## Warning: mu of 0.6 is too close to zero, estimate of random effect variances may be unreliable.
## # R2 for Mixed Models
## 
##   Conditional R2: 0.474
##      Marginal R2: 0.045
percent.alfa <- dredge(glmmTMB(percent.alfa ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= test1veg , family = "beta_family"))
## Fixed terms are "cond((Int))" and "disp((Int))"
percent.alfa1 <- glmmTMB(percent.alfa ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= test1veg , family = "beta_family")
summary(percent.alfa1)
##  Family: beta  ( logit )
## Formula:          
## percent.alfa ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std +  
##     (1 | ForestID)
## Data: test1veg
## 
##      AIC      BIC   logLik deviance df.resid 
##   -270.2   -257.2    141.1   -282.2       59 
## 
## Random effects:
## 
## Conditional model:
##  Groups   Name        Variance Std.Dev.
##  ForestID (Intercept) 0.2666   0.5163  
## Number of obs: 65, groups:  ForestID, 4
## 
## Overdispersion parameter for beta family (): 5.16 
## 
## Conditional model:
##                          Estimate Std. Error z value Pr(>|z|)    
## (Intercept)              -2.33574    0.33564  -6.959 3.43e-12 ***
## Dist_edge_std            -0.05027    0.18130  -0.277    0.782    
## Dist_trail_std           -0.02667    0.15343  -0.174    0.862    
## Dist_trail_beginning_std -0.12707    0.17424  -0.729    0.466    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
performance::r2(percent.alfa1)
## Warning: mu of 0.1 is too close to zero, estimate of random effect variances may be unreliable.
## # R2 for Mixed Models
## 
##   Conditional R2: 0.223
##      Marginal R2: 0.012
percent.alfa2 <- glmmTMB(percent.alfa ~ Dist_trail_beginning_std + (1 | ForestID), data= test1veg , family = "beta_family")
summary(percent.alfa2)
##  Family: beta  ( logit )
## Formula:          percent.alfa ~ Dist_trail_beginning_std + (1 | ForestID)
## Data: test1veg
## 
##      AIC      BIC   logLik deviance df.resid 
##   -274.1   -265.4    141.0   -282.1       61 
## 
## Random effects:
## 
## Conditional model:
##  Groups   Name        Variance Std.Dev.
##  ForestID (Intercept) 0.2589   0.5089  
## Number of obs: 65, groups:  ForestID, 4
## 
## Overdispersion parameter for beta family (): 5.12 
## 
## Conditional model:
##                          Estimate Std. Error z value Pr(>|z|)    
## (Intercept)               -2.3581     0.3278  -7.194 6.27e-13 ***
## Dist_trail_beginning_std  -0.1412     0.1667  -0.847    0.397    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
performance::r2_nakagawa(percent.alfa2)
## Warning: mu of 0.1 is too close to zero, estimate of random effect variances may be unreliable.
## # R2 for Mixed Models
## 
##   Conditional R2: 0.216
##      Marginal R2: 0.011
performance::r2(percent.alfa2)
## Warning: mu of 0.1 is too close to zero, estimate of random effect variances may be unreliable.
## # R2 for Mixed Models
## 
##   Conditional R2: 0.216
##      Marginal R2: 0.011
percent.alfa3 <- glmmTMB(percent.alfa ~ Dist_edge_std  + (1 | ForestID), data= test1veg , family = "beta_family")
summary(percent.alfa3)
##  Family: beta  ( logit )
## Formula:          percent.alfa ~ Dist_edge_std + (1 | ForestID)
## Data: test1veg
## 
##      AIC      BIC   logLik deviance df.resid 
##   -273.7   -265.0    140.8   -281.7       61 
## 
## Random effects:
## 
## Conditional model:
##  Groups   Name        Variance Std.Dev.
##  ForestID (Intercept) 0.2941   0.5423  
## Number of obs: 65, groups:  ForestID, 4
## 
## Overdispersion parameter for beta family (): 5.18 
## 
## Conditional model:
##               Estimate Std. Error z value Pr(>|z|)    
## (Intercept)    -2.3904     0.3367  -7.099 1.26e-12 ***
## Dist_edge_std  -0.0974     0.1626  -0.599    0.549    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
performance::r2_nakagawa(percent.alfa3)
## Warning: mu of 0.1 is too close to zero, estimate of random effect variances may be unreliable.
## # R2 for Mixed Models
## 
##   Conditional R2: 0.235
##      Marginal R2: 0.006
performance::r2(percent.alfa3)
## Warning: mu of 0.1 is too close to zero, estimate of random effect variances may be unreliable.
## # R2 for Mixed Models
## 
##   Conditional R2: 0.235
##      Marginal R2: 0.006
abund.all <- dredge(glmmTMB(abund.all ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= test1veg , family = "poisson"))
## Fixed terms are "cond((Int))" and "disp((Int))"
abund.all
## Global model call: glmmTMB(formula = abund.all ~ Dist_edge_std + Dist_trail_std + 
##     Dist_trail_beginning_std + (1 | ForestID), data = test1veg, 
##     family = "poisson", ziformula = ~0, dispformula = ~1)
## ---
## Model selection table 
##   cnd((Int)) dsp((Int)) cnd(Dst_edg_std) cnd(Dst_trl_bgn_std)
## 5      3.656          +                                      
## 6      3.662          +         -0.03118                     
## 7      3.653          +                             0.0066840
## 8      3.654          +         -0.03842            0.0226200
## 1      3.705          +                                      
## 2      3.693          +          0.03032                     
## 3      3.705          +                             0.0007069
## 4      3.698          +          0.03314           -0.0121600
##   cnd(Dst_trl_std) df   logLik   AICc delta weight
## 5           0.1086  3 -526.655 1059.7  0.00  0.431
## 6           0.1221  4 -525.870 1060.4  0.70  0.303
## 7           0.1088  4 -526.627 1061.9  2.22  0.142
## 8           0.1259  5 -525.597 1062.2  2.51  0.123
## 1                   2 -541.561 1087.3 27.61  0.000
## 2                   3 -540.608 1087.6 27.91  0.000
## 3                   3 -541.560 1089.5 29.81  0.000
## 4                   4 -540.524 1089.7 30.01  0.000
## Models ranked by AICc(x) 
## Random terms (all models): 
## 'cond(1 | ForestID)'
abund.all1 <- glmmTMB(abund.all ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= test1veg , family = "poisson")
summary(abund.all1 )
##  Family: poisson  ( log )
## Formula:          
## abund.all ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std +  
##     (1 | ForestID)
## Data: test1veg
## 
##      AIC      BIC   logLik deviance df.resid 
##   1061.2   1072.1   -525.6   1051.2       60 
## 
## Random effects:
## 
## Conditional model:
##  Groups   Name        Variance Std.Dev.
##  ForestID (Intercept) 0.1387   0.3724  
## Number of obs: 65, groups:  ForestID, 4
## 
## Conditional model:
##                          Estimate Std. Error z value Pr(>|z|)    
## (Intercept)               3.65379    0.18855  19.379  < 2e-16 ***
## Dist_edge_std            -0.03842    0.02696  -1.425    0.154    
## Dist_trail_std            0.12590    0.02260   5.571 2.54e-08 ***
## Dist_trail_beginning_std  0.02262    0.03046   0.743    0.458    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
performance::r2(alpha.all1)
## # R2 for Mixed Models
## 
##   Conditional R2: 0.560
##      Marginal R2: 0.005
abund.all3 <- glmmTMB(abund.all ~  Dist_trail_std + (1 | ForestID), data= test1veg , family = "poisson")
summary(abund.all3 )
##  Family: poisson  ( log )
## Formula:          abund.all ~ Dist_trail_std + (1 | ForestID)
## Data: test1veg
## 
##      AIC      BIC   logLik deviance df.resid 
##   1059.3   1065.8   -526.7   1053.3       62 
## 
## Random effects:
## 
## Conditional model:
##  Groups   Name        Variance Std.Dev.
##  ForestID (Intercept) 0.1343   0.3665  
## Number of obs: 65, groups:  ForestID, 4
## 
## Conditional model:
##                Estimate Std. Error z value Pr(>|z|)    
## (Intercept)     3.65623    0.18520  19.743  < 2e-16 ***
## Dist_trail_std  0.10863    0.01912   5.682 1.33e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
performance::r2(alpha.all3)
## Error in performance::r2(alpha.all3): object 'alpha.all3' not found
abund.inv <- dredge(glmmTMB(abund.inv ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= test1veg , family = "poisson"))
## Fixed terms are "cond((Int))" and "disp((Int))"
abund.inv
## Global model call: glmmTMB(formula = abund.inv ~ Dist_edge_std + Dist_trail_std + 
##     Dist_trail_beginning_std + (1 | ForestID), data = test1veg, 
##     family = "poisson", ziformula = ~0, dispformula = ~1)
## ---
## Model selection table 
##   cnd((Int)) dsp((Int)) cnd(Dst_edg_std) cnd(Dst_trl_bgn_std)
## 7      1.186          +                               -0.2456
## 8      1.186          +          0.01946              -0.2575
## 5      1.084          +                                      
## 6      1.103          +         -0.08698                     
## 3      1.109          +                               -0.2297
## 2      1.072          +         -0.15840                     
## 4      1.123          +         -0.10360              -0.1719
## 1      1.016          +                                      
##   cnd(Dst_trl_std) df   logLik  AICc delta weight
## 7          -0.1937  4 -214.596 437.9  0.00  0.475
## 8          -0.2010  5 -214.579 440.2  2.32  0.149
## 5          -0.1836  3 -216.992 440.4  2.52  0.135
## 6          -0.1526  4 -216.566 441.8  3.94  0.066
## 3                   3 -217.702 441.8  3.94  0.066
## 2                   3 -218.079 442.6  4.69  0.045
## 4                   4 -217.099 442.9  5.01  0.039
## 1                   2 -219.768 443.7  5.87  0.025
## Models ranked by AICc(x) 
## Random terms (all models): 
## 'cond(1 | ForestID)'
abund.inv1 <- glmmTMB(abund.inv ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= test1veg , family = "poisson")
summary(abund.inv1)
##  Family: poisson  ( log )
## Formula:          
## abund.inv ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std +  
##     (1 | ForestID)
## Data: test1veg
## 
##      AIC      BIC   logLik deviance df.resid 
##    439.2    450.0   -214.6    429.2       60 
## 
## Random effects:
## 
## Conditional model:
##  Groups   Name        Variance Std.Dev.
##  ForestID (Intercept) 1.34     1.158   
## Number of obs: 65, groups:  ForestID, 4
## 
## Conditional model:
##                          Estimate Std. Error z value Pr(>|z|)  
## (Intercept)               1.18640    0.59519   1.993   0.0462 *
## Dist_edge_std             0.01946    0.10759   0.181   0.8565  
## Dist_trail_std           -0.20099    0.09344  -2.151   0.0315 *
## Dist_trail_beginning_std -0.25749    0.13345  -1.930   0.0537 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
performance::r2(abund.inv1)
## Warning: mu of 2.8 is too close to zero, estimate of random effect variances may be unreliable.
## # R2 for Mixed Models
## 
##   Conditional R2: 0.821
##      Marginal R2: 0.043
abund.inv2 <- glmmTMB(abund.inv ~ Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= test1veg , family = "poisson")
summary(abund.inv2)
##  Family: poisson  ( log )
## Formula:          
## abund.inv ~ Dist_trail_std + Dist_trail_beginning_std + (1 |  
##     ForestID)
## Data: test1veg
## 
##      AIC      BIC   logLik deviance df.resid 
##    437.2    445.9   -214.6    429.2       61 
## 
## Random effects:
## 
## Conditional model:
##  Groups   Name        Variance Std.Dev.
##  ForestID (Intercept) 1.342    1.159   
## Number of obs: 65, groups:  ForestID, 4
## 
## Conditional model:
##                          Estimate Std. Error z value Pr(>|z|)  
## (Intercept)               1.18609    0.59567   1.991   0.0465 *
## Dist_trail_std           -0.19368    0.08436  -2.296   0.0217 *
## Dist_trail_beginning_std -0.24558    0.11549  -2.126   0.0335 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
performance::r2(abund.inv2)
## Warning: mu of 2.8 is too close to zero, estimate of random effect variances may be unreliable.
## # R2 for Mixed Models
## 
##   Conditional R2: 0.821
##      Marginal R2: 0.041
percent.abund <- dredge(glmmTMB(percent.abund ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= test1veg , family = "beta_family"))
## Fixed terms are "cond((Int))" and "disp((Int))"
percent.abund
## Global model call: glmmTMB(formula = percent.abund ~ Dist_edge_std + Dist_trail_std + 
##     Dist_trail_beginning_std + (1 | ForestID), data = test1veg, 
##     family = "beta_family", ziformula = ~0, dispformula = ~1)
## ---
## Model selection table 
##   cnd((Int)) dsp((Int)) cnd(Dst_edg_std) cnd(Dst_trl_bgn_std)
## 1     -2.089          +                                      
## 3     -1.978          +                               -0.2340
## 2     -2.047          +         -0.10920                     
## 5     -2.072          +                                      
## 7     -1.955          +                               -0.2362
## 4     -1.963          +         -0.05595              -0.2178
## 6     -2.045          +         -0.10550                     
## 8     -1.951          +         -0.03747              -0.2249
##   cnd(Dst_trl_std) df  logLik   AICc delta weight
## 1                   3 124.424 -242.5  0.00  0.304
## 3                   4 125.354 -242.0  0.41  0.247
## 2                   4 124.650 -240.6  1.82  0.122
## 5        -0.043160  4 124.470 -240.3  2.18  0.102
## 7        -0.053440  5 125.424 -239.8  2.62  0.082
## 4                   5 125.412 -239.8  2.65  0.081
## 6        -0.008489  5 124.652 -238.3  4.17  0.038
## 8        -0.041040  6 125.446 -237.4  5.01  0.025
## Models ranked by AICc(x) 
## Random terms (all models): 
## 'cond(1 | ForestID)'
percent.abund1 <- glmmTMB(percent.abund ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= test1veg , family = "beta_family")
summary(percent.abund1)
##  Family: beta  ( logit )
## Formula:          
## percent.abund ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std +  
##     (1 | ForestID)
## Data: test1veg
## 
##      AIC      BIC   logLik deviance df.resid 
##   -238.9   -225.8    125.4   -250.9       59 
## 
## Random effects:
## 
## Conditional model:
##  Groups   Name        Variance Std.Dev.
##  ForestID (Intercept) 0.1833   0.4282  
## Number of obs: 65, groups:  ForestID, 4
## 
## Overdispersion parameter for beta family (): 3.31 
## 
## Conditional model:
##                          Estimate Std. Error z value Pr(>|z|)    
## (Intercept)              -1.95057    0.30755  -6.342 2.26e-10 ***
## Dist_edge_std            -0.03747    0.17903  -0.209    0.834    
## Dist_trail_std           -0.04104    0.15666  -0.262    0.793    
## Dist_trail_beginning_std -0.22488    0.18361  -1.225    0.221    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
performance::r2(percent.abund1)
## Warning: mu of 0.1 is too close to zero, estimate of random effect variances may be unreliable.
## # R2 for Mixed Models
## 
##   Conditional R2: 0.187
##      Marginal R2: 0.033
percent.abund2 <- glmmTMB(percent.abund ~  Dist_trail_std  + (1 | ForestID), data= test1veg , family = "beta_family")
summary(percent.abund2)
##  Family: beta  ( logit )
## Formula:          percent.abund ~ Dist_trail_std + (1 | ForestID)
## Data: test1veg
## 
##      AIC      BIC   logLik deviance df.resid 
##   -240.9   -232.2    124.5   -248.9       61 
## 
## Random effects:
## 
## Conditional model:
##  Groups   Name        Variance Std.Dev.
##  ForestID (Intercept) 0.2129   0.4614  
## Number of obs: 65, groups:  ForestID, 4
## 
## Overdispersion parameter for beta family (): 3.27 
## 
## Conditional model:
##                Estimate Std. Error z value Pr(>|z|)    
## (Intercept)    -2.07157    0.30890  -6.706    2e-11 ***
## Dist_trail_std -0.04316    0.14445  -0.299    0.765    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
performance::r2(percent.abund2)
## Warning: mu of 0.1 is too close to zero, estimate of random effect variances may be unreliable.
## # R2 for Mixed Models
## 
##   Conditional R2: 0.180
##      Marginal R2: 0.001
percent.abund3 <- glmmTMB(percent.abund ~ Dist_edge_std +  (1 | ForestID), data= test1veg , family = "beta_family")
summary(percent.abund3)
##  Family: beta  ( logit )
## Formula:          percent.abund ~ Dist_edge_std + (1 | ForestID)
## Data: test1veg
## 
##      AIC      BIC   logLik deviance df.resid 
##   -241.3   -232.6    124.7   -249.3       61 
## 
## Random effects:
## 
## Conditional model:
##  Groups   Name        Variance Std.Dev.
##  ForestID (Intercept) 0.2198   0.4688  
## Number of obs: 65, groups:  ForestID, 4
## 
## Overdispersion parameter for beta family ():  3.3 
## 
## Conditional model:
##               Estimate Std. Error z value Pr(>|z|)    
## (Intercept)    -2.0473     0.3118  -6.566 5.16e-11 ***
## Dist_edge_std  -0.1092     0.1653  -0.660    0.509    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
performance::r2(percent.abund3)
## Warning: mu of 0.1 is too close to zero, estimate of random effect variances may be unreliable.
## # R2 for Mixed Models
## 
##   Conditional R2: 0.190
##      Marginal R2: 0.008
#R2
Ralpha.all <- performance::r2(glmmTMB(alpha.all ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= test1veg , family = "poisson"))
Rpercent.alfa <- performance::r2(glmmTMB(percent.alfa ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= test1veg , family = "beta_family"))
## Warning: mu of 0.1 is too close to zero, estimate of random effect variances may be unreliable.
Rabund.all <- performance::r2(glmmTMB(abund.all ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= test1veg , family = "poisson"))
Rabund.inv <- performance::r2(glmmTMB(abund.inv ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= test1veg , family = "poisson"))
## Warning: mu of 2.8 is too close to zero, estimate of random effect variances may be unreliable.
#Rpercent.abund <- performance::r2(glmmTMB(percent.abund ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= test1veg , family = "beta_family"))


test2veg <- read.csv2(here("data.veg","test2veg.CSV"), header=TRUE, row.names = 1,  stringsAsFactors = T,sep = ",", dec = ".")

test2veg$delta.percent.alpha[test2veg$delta.percent.alpha == 0] <- 0.001
test2veg$delta.percent.alpha[test2veg$delta.percent.alpha == 1] <- 0.999
test2veg$delta.percent.abund[test2veg$delta.percent.abund == 0] <- 0.001
test2veg$delta.percent.abund[test2veg$delta.percent.abund == 1] <- 0.999

delta.alpha <- dredge(glmmTMB(delta.alpha ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= test2veg , family = "gaussian"))
## Fixed terms are "cond((Int))" and "disp((Int))"
delta.abund <- dredge(glmmTMB(delta.abund ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= test2veg , family = "gaussian"))
## Fixed terms are "cond((Int))" and "disp((Int))"
delta.percent.alpha <- dredge(glmmTMB(delta.percent.alpha ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= test2veg , family = "gaussian"))
## Fixed terms are "cond((Int))" and "disp((Int))"
delta.percent.abund <- dredge(glmmTMB(delta.percent.abund ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= test2veg , family = "gaussian"))
## Fixed terms are "cond((Int))" and "disp((Int))"
#Rdelta.alpha <- performance::r2(glmmTMB(delta.alpha ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= test2veg , family = "gaussian"))
#Rdelta.abund <- performance::r2(glmmTMB(delta.abund ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= test2veg , family = "gaussian"))
Rdelta.percent.alpha <- performance::r2(glmmTMB(delta.percent.alpha ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= test2veg , family = "gaussian"))
#Rdelta.percent.abund <- performance::r2(glmmTMB(delta.percent.abund ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= test2veg , family = "gaussian"))

fam.veg <- c(NA, NA, NA, NA,NA, NA,NA, "poisson", "poisson","beta_family","beta_family", "beta_family", "beta_family","beta_family", "beta_family")

## GENERATING MODELS FOR PLOT 1

# Alpha

dredge.alpha.plot1 <- dredge(glmmTMB(alpha.plot1.all ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= Results.plot1.import , family = "poisson"))
## Error in eval(substitute(offset), data, enclos = environment(formula)): object 'Results.plot1.import' not found
dredge.alpha.plot2 <- dredge(glmmTMB(alpha.plot2.all ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= Results.plot2.import , family = "poisson"))
## Error in eval(substitute(offset), data, enclos = environment(formula)): object 'Results.plot2.import' not found
dredge.alpha.plot3 <- dredge(glmmTMB(alpha.plot3.all ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= Results.plot3.import , family = "poisson"))
## Error in eval(substitute(offset), data, enclos = environment(formula)): object 'Results.plot3.import' not found
dredge.alpha.plot1.inv <- dredge(glmmTMB(alpha.plot1.inv ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= Results.plot1.import , family = "poisson"))
## Error in eval(substitute(offset), data, enclos = environment(formula)): object 'Results.plot1.import' not found
dredge.alpha.plot2.inv <- dredge(glmmTMB(alpha.plot2.inv ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= Results.plot2.import , family = "poisson"))
## Error in eval(substitute(offset), data, enclos = environment(formula)): object 'Results.plot2.import' not found
dredge.alpha.plot3.inv <- dredge(glmmTMB(alpha.plot3.inv ~ Dist_edge_std + Dist_trail_std + Dist_trail_beginning_std + (1 | ForestID), data= Results.plot3.import , family = "poisson"))
## Error in eval(substitute(offset), data, enclos = environment(formula)): object 'Results.plot3.import' not found
#dredge.alpha.inv <- data.frame(dredge.alpha.inv)
#write.csv(dredge.alpha.inv, file = here("results","test.alpha.inv.csv"), row.names = TRUE)