Load data, remove NAs

## Libraries
library(vegan)
## Loading required package: permute
## Loading required package: lattice
## This is vegan 2.6-6.1
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.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(MuMIn)
library(car) 
## Loading required package: carData
## 
## Attaching package: 'car'
## 
## The following object is masked from 'package:dplyr':
## 
##     recode
## 
## The following object is masked from 'package:purrr':
## 
##     some
library(lme4)
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## 
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
library(lmerTest)  
## 
## Attaching package: 'lmerTest'
## 
## The following object is masked from 'package:lme4':
## 
##     lmer
## 
## The following object is masked from 'package:stats':
## 
##     step
library(performance) 
library(glmmTMB) 
library(fitdistrplus) 
## Loading required package: MASS
## 
## Attaching package: 'MASS'
## 
## The following object is masked from 'package:dplyr':
## 
##     select
## 
## Loading required package: survival
library(logspline)
library(pwr)
library(Hmisc)
## 
## Attaching package: 'Hmisc'
## 
## The following objects are masked from 'package:dplyr':
## 
##     src, summarize
## 
## The following objects are masked from 'package:base':
## 
##     format.pval, units
## Load data
data <- read.csv("NewData8.csv", stringsAsFactors = FALSE)
data[data == "#VALUE!"] <- NA ## convert Excel errors to NA
data[,15:22] <- apply(data[,15:22], 2, as.numeric)
data$AgeClass <- as.factor(data$AgeClass) ## convert ageclass to factor


## Select response variables
responseVars <- c("BlueLUM","BlueBY","BlueRG","RedLUM","RedBY", "RedRG")


## Select predictor variables
predictorVars<- c( "Age", "Prevalence", "Richness", "GCMEAN", "ARMEAN", "Ordinal")


#Drop AgeClass
data2 <- data[ -c(4) ]

## Drop NA

data6 <- na.omit(data2)

##Load Sociosexual Data

data_socio <- read.csv("Data_counts_9Aug.csv", stringsAsFactors = FALSE)

Intermale Analyses

Blue Luminance

#model1A_group_maleID <- lmer(BlueLUM ~ Ordinal + Diversity + GCMEAN + log(ARMEAN) + PropInfected + Season + (1|Group) + (1|MaleID), data=data6, na.action = "na.fail")
#model convergence issues 

#model1A_group <- lmer(BlueLUM ~ Ordinal + Diversity + GCMEAN + log(ARMEAN) + PropInfected + Season + (1|Group), data=data6, na.action = "na.fail")
#model convergence issues 

#model1A_maleID <- lmer(BlueLUM ~ Ordinal + Diversity + GCMEAN + log(ARMEAN) + PropInfected + Season + (1|MaleID), data=data6, na.action = "na.fail")
#check_collinearity(model1A_maleID) # low VIF <4
#options(na.action = na.fail)
#dd1A_maleID <- dredge(model1A_maleID) # Warning: comparing models fitted by REML
#options(na.action = na.omit)
## model convergence issues when dredging, cannot use maleID as random variable

#Change column headings

data6 <- data6 %>%
  rename(Diversity = Richness,PropInfected = Prevalence, Season = Session)


## Final model with no random variables: 
model1A <- lm(BlueLUM ~ Ordinal + Diversity + GCMEAN + log(ARMEAN) + PropInfected + Season, data=data6, na.action = "na.fail")
check_collinearity(model1A) # low VIF <4
## # Check for Multicollinearity
## 
## Low Correlation
## 
##          Term  VIF   VIF 95% CI Increased SE Tolerance Tolerance 95% CI
##       Ordinal 1.27 [1.06, 2.11]         1.13      0.79     [0.47, 0.94]
##     Diversity 1.53 [1.20, 2.40]         1.24      0.65     [0.42, 0.83]
##        GCMEAN 3.07 [2.12, 4.84]         1.75      0.33     [0.21, 0.47]
##   log(ARMEAN) 3.29 [2.25, 5.20]         1.81      0.30     [0.19, 0.44]
##  PropInfected 1.59 [1.24, 2.48]         1.26      0.63     [0.40, 0.81]
##        Season 1.10 [1.01, 2.96]         1.05      0.91     [0.34, 0.99]
#dredge model selection
options(na.action = na.fail) 
dredge1A <- dredge(model1A)
## Fixed term is "(Intercept)"
options(na.action = na.omit)

#model averaging
dredge1A_modelaverage <- model.avg(dredge1A, subset = delta <= 7)
summary(dredge1A_modelaverage) 
## 
## Call:
## model.avg(object = dredge1A, subset = delta <= 7)
## 
## Component model call: 
## lm(formula = BlueLUM ~ <28 unique rhs>, data = data6, na.action = 
##      na.fail)
## 
## Component models: 
##        df logLik   AICc delta weight
## 4       3  45.25 -83.65  0.00   0.15
## 234     5  47.65 -82.99  0.66   0.11
## 34      4  46.21 -82.94  0.71   0.10
## 1234    6  48.93 -82.49  1.16   0.08
## 46      4  45.73 -81.97  1.68   0.06
## 14      4  45.64 -81.81  1.85   0.06
## 134     5  46.84 -81.38  2.27   0.05
## 45      4  45.28 -81.07  2.58   0.04
## 24      4  45.26 -81.05  2.60   0.04
## 2346    6  48.12 -80.87  2.78   0.04
## 346     5  46.50 -80.69  2.96   0.03
## 345     5  46.23 -80.16  3.49   0.03
## 2345    6  47.67 -79.97  3.68   0.02
## 12345   7  49.23 -79.79  3.86   0.02
## 12346   7  49.20 -79.73  3.92   0.02
## 146     5  46.01 -79.71  3.94   0.02
## 246     5  45.73 -79.15  4.50   0.02
## 456     5  45.73 -79.15  4.50   0.02
## 145     5  45.68 -79.05  4.60   0.01
## 124     5  45.65 -79.00  4.65   0.01
## 1346    6  47.00 -78.65  5.00   0.01
## 1345    6  46.93 -78.51  5.14   0.01
## 245     5  45.29 -78.27  5.38   0.01
## 3456    6  46.50 -77.64  6.01   0.01
## 23456   7  48.12 -77.57  6.08   0.01
## 123456  8  49.59 -76.91  6.74   0.01
## 1456    6  46.08 -76.80  6.85   0.00
## 1246    6  46.01 -76.66  6.99   0.00
## 
## Term codes: 
##    Diversity       GCMEAN  log(ARMEAN)      Ordinal PropInfected       Season 
##            1            2            3            4            5            6 
## 
## Model-averaged coefficients:  
## (full average) 
##                Estimate Std. Error Adjusted SE z value Pr(>|z|)   
## (Intercept)   0.5081531  0.2188447   0.2221661   2.287  0.02218 * 
## Ordinal      -0.0145630  0.0043648   0.0045388   3.209  0.00133 **
## GCMEAN        0.0004693  0.0008848   0.0008987   0.522  0.60153   
## log(ARMEAN)  -0.0452184  0.0590345   0.0598490   0.756  0.44992   
## Diversity    -0.0037211  0.0081399   0.0083311   0.447  0.65513   
## Season        0.0044532  0.0134147   0.0138210   0.322  0.74729   
## PropInfected  0.0007494  0.0280168   0.0291698   0.026  0.97950   
##  
## (conditional average) 
##               Estimate Std. Error Adjusted SE z value Pr(>|z|)   
## (Intercept)   0.508153   0.218845    0.222166   2.287  0.02218 * 
## Ordinal      -0.014563   0.004365    0.004539   3.209  0.00133 **
## GCMEAN        0.001204   0.001060    0.001090   1.105  0.26924   
## log(ARMEAN)  -0.082663   0.057234    0.058760   1.407  0.15949   
## Diversity    -0.011631   0.010729    0.011178   1.041  0.29810   
## Season        0.017920   0.021973    0.022964   0.780  0.43519   
## PropInfected  0.003988   0.064526    0.067190   0.059  0.95267   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Ordinal Rank is the only significant variable 

#Getting 95% confidence intervals 
confint(dredge1A_modelaverage, full=TRUE) 
##                     2.5 %       97.5 %
## (Intercept)   0.072715627  0.943590547
## Ordinal      -0.023458837 -0.005667116
## GCMEAN       -0.001292137  0.002230749
## log(ARMEAN)  -0.162520216  0.072083511
## Diversity    -0.020049822  0.012607600
## Season       -0.022635407  0.031541900
## PropInfected -0.056422335  0.057921231
#confirms ordinal rank is the only significant variable 

Blue B/Y Channel

#model1B_group_maleID <- lmer(BlueBY ~ Ordinal + Diversity + GCMEAN + log(ARMEAN) + PropInfected + Season + (1|Group) + (1|MaleID), data=data6, na.action = "na.fail")
#check_collinearity(model1B_group_maleID) # low VIF < 3
#options(na.action = na.fail)
#dd1B_group_maleID <- dredge(model1B_group_maleID) ## boundary (singular) fit: see help('isSingular')
#options(na.action = na.omit)
## model convergence issues when dredging, cannot use group and maleID as random variable 

#model1B_group <- lmer(BlueBY ~ Ordinal + Diversity + GCMEAN + log(ARMEAN) + PropInfected + Season + (1|Group), data=data6, na.action = "na.fail")
## model convergence issues

#model1B_maleID <- lmer(BlueBY ~ Ordinal + Diversity + GCMEAN + log(ARMEAN) + PropInfected + Season + (1|MaleID), data=data6, na.action = "na.fail")
#check_collinearity(model1B_maleID) # low VIF < 4
#options(na.action = na.fail)
#dd1B_maleID <- dredge(model1B_maleID) ## boundary (singular) fit: see help('isSingular')
#options(na.action = na.omit)
## model convergence issues when dredging, cannot use maleID as random variable


## Final model with no random variables: 
model1B <- lm(BlueBY ~ Ordinal + Diversity + GCMEAN + log(ARMEAN) + PropInfected + Season, data=data6, na.action = "na.fail")
check_collinearity(model1B) # low VIF < 4
## # Check for Multicollinearity
## 
## Low Correlation
## 
##          Term  VIF   VIF 95% CI Increased SE Tolerance Tolerance 95% CI
##       Ordinal 1.27 [1.06, 2.11]         1.13      0.79     [0.47, 0.94]
##     Diversity 1.53 [1.20, 2.40]         1.24      0.65     [0.42, 0.83]
##        GCMEAN 3.07 [2.12, 4.84]         1.75      0.33     [0.21, 0.47]
##   log(ARMEAN) 3.29 [2.25, 5.20]         1.81      0.30     [0.19, 0.44]
##  PropInfected 1.59 [1.24, 2.48]         1.26      0.63     [0.40, 0.81]
##        Season 1.10 [1.01, 2.96]         1.05      0.91     [0.34, 0.99]
#dredge model selection
options(na.action = na.fail) 
dredge1B <- dredge(model1B)
## Fixed term is "(Intercept)"
options(na.action = na.omit)

#model averaging
dredge1B_modelaverage <- model.avg(dredge1B, subset = delta <= 7)
summary(dredge1B_modelaverage) 
## 
## Call:
## model.avg(object = dredge1B, subset = delta <= 7)
## 
## Component model call: 
## lm(formula = BlueBY ~ <48 unique rhs>, data = data6, na.action = 
##      na.fail)
## 
## Component models: 
##        df logLik    AICc delta weight
## 5       3  55.30 -103.74  0.00   0.13
## (Null)  2  53.52 -102.63  1.11   0.07
## 25      4  55.86 -102.24  1.50   0.06
## 1       3  54.46 -102.07  1.67   0.05
## 56      4  55.59 -101.70  2.04   0.05
## 6       3  54.12 -101.39  2.36   0.04
## 15      4  55.41 -101.35  2.39   0.04
## 45      4  55.41 -101.34  2.40   0.04
## 35      4  55.35 -101.23  2.51   0.04
## 2       3  54.02 -101.19  2.55   0.04
## 12      4  55.01 -100.55  3.20   0.03
## 256     5  56.39 -100.47  3.27   0.02
## 26      4  54.94 -100.39  3.35   0.02
## 16      4  54.87 -100.25  3.49   0.02
## 4       3  53.55 -100.24  3.50   0.02
## 3       3  53.54 -100.22  3.53   0.02
## 235     5  56.22 -100.13  3.62   0.02
## 125     5  55.99  -99.67  4.07   0.02
## 23      4  54.56  -99.64  4.10   0.02
## 245     5  55.97  -99.63  4.11   0.02
## 123     5  55.92  -99.53  4.21   0.02
## 13      4  54.46  -99.45  4.30   0.01
## 14      4  54.46  -99.45  4.30   0.01
## 456     5  55.71  -99.12  4.62   0.01
## 356     5  55.71  -99.11  4.63   0.01
## 126     5  55.69  -99.06  4.68   0.01
## 156     5  55.68  -99.06  4.68   0.01
## 36      4  54.20  -98.91  4.83   0.01
## 46      4  54.13  -98.78  4.96   0.01
## 145     5  55.53  -98.75  4.99   0.01
## 24      4  54.05  -98.63  5.12   0.01
## 135     5  55.45  -98.59  5.15   0.01
## 345     5  55.44  -98.58  5.17   0.01
## 236     5  55.43  -98.54  5.20   0.01
## 2356    6  56.73  -98.10  5.64   0.01
## 124     5  55.01  -97.72  6.02   0.01
## 1235    6  56.53  -97.70  6.05   0.01
## 34      4  53.57  -97.67  6.08   0.01
## 2456    6  56.51  -97.66  6.08   0.01
## 2345    6  56.51  -97.66  6.08   0.01
## 1236    6  56.49  -97.62  6.12   0.01
## 1256    6  56.49  -97.61  6.13   0.01
## 246     5  54.95  -97.58  6.16   0.01
## 136     5  54.89  -97.48  6.26   0.01
## 146     5  54.87  -97.43  6.31   0.01
## 1245    6  56.10  -96.84  6.90   0.00
## 234     5  54.57  -96.83  6.92   0.00
## 1234    6  56.08  -96.81  6.94   0.00
## 
## Term codes: 
##    Diversity       GCMEAN  log(ARMEAN)      Ordinal PropInfected       Season 
##            1            2            3            4            5            6 
## 
## Model-averaged coefficients:  
## (full average) 
##                Estimate Std. Error Adjusted SE z value Pr(>|z|)
## (Intercept)   0.0515562  0.0784137   0.0805892   0.640    0.522
## PropInfected  0.0375563  0.0472490   0.0481309   0.780    0.435
## GCMEAN       -0.0001987  0.0004209   0.0004298   0.462    0.644
## Diversity     0.0022371  0.0057031   0.0058424   0.383    0.702
## Season       -0.0043839  0.0113149   0.0116119   0.378    0.706
## Ordinal       0.0001286  0.0014663   0.0015240   0.084    0.933
## log(ARMEAN)   0.0028440  0.0185141   0.0190153   0.150    0.881
##  
## (conditional average) 
##                Estimate Std. Error Adjusted SE z value Pr(>|z|)
## (Intercept)   0.0515562  0.0784137   0.0805892   0.640    0.522
## PropInfected  0.0714883  0.0427054   0.0445407   1.605    0.108
## GCMEAN       -0.0005717  0.0005447   0.0005643   1.013    0.311
## Diversity     0.0077168  0.0083613   0.0086868   0.888    0.374
## Season       -0.0157611  0.0167620   0.0174772   0.902    0.367
## Ordinal       0.0006708  0.0032944   0.0034284   0.196    0.845
## log(ARMEAN)   0.0127701  0.0375811   0.0386884   0.330    0.741
#No significant variables 

#Getting 95% confidence intervals 
confint(dredge1B_modelaverage, full=TRUE) 
##                     2.5 %       97.5 %
## (Intercept)  -0.106395822 0.2095081721
## PropInfected -0.056778577 0.1318911174
## GCMEAN       -0.001041125 0.0006438195
## Diversity    -0.009213878 0.0136879904
## Season       -0.027142908 0.0183750838
## Ordinal      -0.002858474 0.0031156041
## log(ARMEAN)  -0.034425225 0.0401132688
#confirms NULL MODEL is the best model 

Blue R/G Channel

#model1C_group_maleID <- lmer(BlueRG ~ Ordinal + Diversity + GCMEAN + log(ARMEAN) + PropInfected + Season + (1|Group) + (1|MaleID), data=data6, na.action = "na.fail")
#check_collinearity(model1C_group_maleID) # low VIF >3
#options(na.action = na.fail)
#dd1C_group_maleID <- dredge(model1C_group_maleID) ## boundary (singular) fit: see help('isSingular')
#options(na.action = na.omit)
## model convergence issues when dredging, cannot use group and maleID as random variable 

#model1C_group <- lmer(BlueRG ~ Ordinal + Diversity + GCMEAN + log(ARMEAN) + PropInfected + Season + (1|Group), data=data6, na.action = "na.fail")
## model convergence issues 


#model1C_maleID <- lmer(BlueRG ~ Ordinal + Diversity + GCMEAN + log(ARMEAN) + PropInfected + Season + (1|MaleID), data=data6, na.action = "na.fail")
#check_collinearity(model1C_maleID) # low VIF >3
#options(na.action = na.fail)
#dd1C_maleID <- dredge(model1C_maleID) ## boundary (singular) fit: see help('isSingular')
#options(na.action = na.omit)
## model convergence issues when dredging, cannot use maleID as random variable


## Final model with no random variables: 
model1C <- lm(BlueRG ~ Ordinal + Diversity + GCMEAN + log(ARMEAN) + PropInfected + Season, data=data6, na.action = "na.fail")
check_collinearity(model1C) # low VIF < 4
## # Check for Multicollinearity
## 
## Low Correlation
## 
##          Term  VIF   VIF 95% CI Increased SE Tolerance Tolerance 95% CI
##       Ordinal 1.27 [1.06, 2.11]         1.13      0.79     [0.47, 0.94]
##     Diversity 1.53 [1.20, 2.40]         1.24      0.65     [0.42, 0.83]
##        GCMEAN 3.07 [2.12, 4.84]         1.75      0.33     [0.21, 0.47]
##   log(ARMEAN) 3.29 [2.25, 5.20]         1.81      0.30     [0.19, 0.44]
##  PropInfected 1.59 [1.24, 2.48]         1.26      0.63     [0.40, 0.81]
##        Season 1.10 [1.01, 2.96]         1.05      0.91     [0.34, 0.99]
#dredge model selection
options(na.action = na.fail) 
dredge1C <- dredge(model1C)
## Fixed term is "(Intercept)"
options(na.action = na.omit)

#model averaging
dredge1C_modelaverage <- model.avg(dredge1C, subset = delta <= 7)
summary(dredge1C_modelaverage) 
## 
## Call:
## model.avg(object = dredge1C, subset = delta <= 7)
## 
## Component model call: 
## lm(formula = BlueRG ~ <40 unique rhs>, data = data6, na.action = 
##      na.fail)
## 
## Component models: 
##       df logLik    AICc delta weight
## 45     4  99.08 -188.67  0.00   0.13
## 14     4  98.65 -187.83  0.84   0.08
## 345    5  99.90 -187.49  1.19   0.07
## 456    5  99.86 -187.40  1.27   0.07
## 4      3  97.10 -187.34  1.33   0.07
## 145    5  99.52 -186.73  1.95   0.05
## 245    5  99.34 -186.37  2.31   0.04
## 146    5  99.29 -186.26  2.41   0.04
## 34     4  97.80 -186.12  2.55   0.04
## 5      3  96.46 -186.07  2.61   0.03
## 134    5  99.15 -185.99  2.68   0.03
## 124    5  98.93 -185.55  3.12   0.03
## 3456   6 100.45 -185.54  3.13   0.03
## 1456   6 100.42 -185.47  3.20   0.03
## 46     4  97.40 -185.33  3.34   0.02
## 24     4  97.33 -185.18  3.49   0.02
## 1345   6 100.18 -185.00  3.67   0.02
## 2345   6 100.03 -184.71  3.96   0.02
## 56     4  97.06 -184.65  4.02   0.02
## 2456   6  99.97 -184.59  4.09   0.02
## 1245   6  99.80 -184.23  4.44   0.01
## 15     4  96.85 -184.23  4.45   0.01
## 35     4  96.73 -183.97  4.70   0.01
## 25     4  96.67 -183.85  4.82   0.01
## 1346   6  99.60 -183.84  4.83   0.01
## 346    5  97.97 -183.64  5.03   0.01
## 234    5  97.90 -183.50  5.17   0.01
## 1246   6  99.43 -183.49  5.18   0.01
## 1      3  95.00 -183.14  5.53   0.01
## 13456  7 100.85 -183.03  5.65   0.01
## 1234   6  99.16 -182.95  5.72   0.01
## 156    5  97.55 -182.80  5.87   0.01
## 246    5  97.55 -182.79  5.88   0.01
## 23456  7 100.66 -182.65  6.02   0.01
## 12456  7 100.54 -182.41  6.26   0.01
## 356    5  97.21 -182.12  6.56   0.00
## 256    5  97.16 -182.00  6.67   0.00
## 125    5  97.07 -181.84  6.84   0.00
## 12345  7 100.24 -181.81  6.86   0.00
## 135    5  97.03 -181.76  6.91   0.00
## 
## Term codes: 
##    Diversity       GCMEAN  log(ARMEAN)      Ordinal PropInfected       Season 
##            1            2            3            4            5            6 
## 
## Model-averaged coefficients:  
## (full average) 
##                Estimate Std. Error Adjusted SE z value Pr(>|z|)  
## (Intercept)  -5.341e-02  2.520e-02   2.576e-02   2.073   0.0382 *
## Ordinal       1.825e-03  1.039e-03   1.063e-03   1.716   0.0861 .
## PropInfected -1.365e-02  1.452e-02   1.477e-02   0.924   0.3556  
## Diversity    -1.018e-03  1.878e-03   1.916e-03   0.531   0.5952  
## log(ARMEAN)   1.903e-03  4.837e-03   4.964e-03   0.383   0.7014  
## Season       -1.253e-03  3.034e-03   3.113e-03   0.403   0.6872  
## GCMEAN        7.194e-06  6.385e-05   6.616e-05   0.109   0.9134  
##  
## (conditional average) 
##                Estimate Std. Error Adjusted SE z value Pr(>|z|)  
## (Intercept)  -5.341e-02  2.520e-02   2.576e-02   2.073   0.0382 *
## Ordinal       2.076e-03  8.401e-04   8.747e-04   2.374   0.0176 *
## PropInfected -2.240e-02  1.225e-02   1.274e-02   1.758   0.0787 .
## Diversity    -2.745e-03  2.184e-03   2.271e-03   1.209   0.2269  
## log(ARMEAN)   6.785e-03  7.091e-03   7.397e-03   0.917   0.3590  
## Season       -4.349e-03  4.299e-03   4.491e-03   0.968   0.3328  
## GCMEAN        3.496e-05  1.373e-04   1.425e-04   0.245   0.8062  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#No significant variables 

#Getting 95% confidence intervals 
confint(dredge1C_modelaverage, full=TRUE) 
##                      2.5 %        97.5 %
## (Intercept)  -0.1039055350 -0.0029168754
## Ordinal      -0.0002590787  0.0039095596
## PropInfected -0.0426056687  0.0153100066
## Diversity    -0.0047735998  0.0027373970
## log(ARMEAN)  -0.0078256888  0.0116326271
## Season       -0.0073549994  0.0048482751
## GCMEAN       -0.0001224717  0.0001368607
#confirms NULL MODEL is the best model 

Red Luminance

#model1D_group_maleID <- lmer(RedLUM ~ Ordinal + Diversity + GCMEAN + log(ARMEAN) + PropInfected + Season + (1|Group) + (1|MaleID), data=data6, na.action = "na.fail")
## model convergence issues

#model1D_group <- lmer(RedLUM ~ Ordinal + Diversity + GCMEAN + log(ARMEAN) + PropInfected + Season + (1|Group), data=data6, na.action = "na.fail")
## model convergence issues

#model1D_maleID <- lmer(RedLUM ~ Ordinal + Diversity + GCMEAN + log(ARMEAN) + PropInfected + Season + (1|MaleID), data=data6, na.action = "na.fail")
#check_collinearity(model1D_maleID) # low VIF < 4
#options(na.action = na.fail)
#dd1D_maleID <- dredge(model1D_maleID) ## Warning: comparing models fitted by REML
#options(na.action = na.omit)
## model convergence issues when dredging, cannot use maleID as random variable


## Final model with no random variables: 
model1D <- lm(RedLUM ~ Ordinal + Diversity + GCMEAN + log(ARMEAN) + PropInfected + Season, data=data6, na.action = "na.fail")
check_collinearity(model1D) # low VIF < 4
## # Check for Multicollinearity
## 
## Low Correlation
## 
##          Term  VIF   VIF 95% CI Increased SE Tolerance Tolerance 95% CI
##       Ordinal 1.27 [1.06, 2.11]         1.13      0.79     [0.47, 0.94]
##     Diversity 1.53 [1.20, 2.40]         1.24      0.65     [0.42, 0.83]
##        GCMEAN 3.07 [2.12, 4.84]         1.75      0.33     [0.21, 0.47]
##   log(ARMEAN) 3.29 [2.25, 5.20]         1.81      0.30     [0.19, 0.44]
##  PropInfected 1.59 [1.24, 2.48]         1.26      0.63     [0.40, 0.81]
##        Season 1.10 [1.01, 2.96]         1.05      0.91     [0.34, 0.99]
#dredge model selection
options(na.action = na.fail) 
dredge1D <- dredge(model1D)
## Fixed term is "(Intercept)"
options(na.action = na.omit)

#model averaging
dredge1D_modelaverage <- model.avg(dredge1D, subset = delta <= 7)
summary(dredge1D_modelaverage) 
## 
## Call:
## model.avg(object = dredge1D, subset = delta <= 7)
## 
## Component model call: 
## lm(formula = RedLUM ~ <57 unique rhs>, data = data6, na.action = 
##      na.fail)
## 
## Component models: 
##        df logLik    AICc delta weight
## 25      4  64.47 -119.46  0.00   0.08
## 24      4  64.15 -118.82  0.65   0.06
## 5       3  62.70 -118.54  0.93   0.05
## 245     5  65.39 -118.48  0.99   0.05
## 124     5  65.32 -118.33  1.13   0.05
## 12      4  63.89 -118.29  1.17   0.04
## 4       3  62.37 -117.89  1.58   0.04
## 2       3  62.27 -117.68  1.78   0.03
## 45      4  63.49 -117.50  1.97   0.03
## 1       3  62.13 -117.41  2.06   0.03
## 125     5  64.85 -117.39  2.07   0.03
## 14      4  63.38 -117.28  2.18   0.03
## 35      4  63.32 -117.16  2.31   0.03
## 34      4  63.30 -117.12  2.35   0.02
## (Null)  2  60.71 -117.02  2.45   0.02
## 235     5  64.66 -117.01  2.46   0.02
## 123     5  64.63 -116.95  2.52   0.02
## 345     5  64.51 -116.70  2.76   0.02
## 256     5  64.49 -116.68  2.79   0.02
## 15      4  63.01 -116.54  2.93   0.02
## 246     5  64.26 -116.21  3.26   0.02
## 1245    6  65.78 -116.20  3.26   0.02
## 234     5  64.18 -116.05  3.41   0.01
## 56      4  62.72 -115.95  3.51   0.01
## 134     5  64.11 -115.92  3.55   0.01
## 23      4  62.61 -115.75  3.72   0.01
## 1234    6  65.54 -115.73  3.74   0.01
## 126     5  63.94 -115.58  3.89   0.01
## 2345    6  65.42 -115.48  3.99   0.01
## 2456    6  65.41 -115.46  4.01   0.01
## 26      4  62.46 -115.43  4.03   0.01
## 13      4  62.42 -115.37  4.10   0.01
## 3       3  61.11 -115.36  4.11   0.01
## 1246    6  65.35 -115.34  4.13   0.01
## 145     5  63.81 -115.31  4.16   0.01
## 46      4  62.37 -115.27  4.20   0.01
## 1235    6  65.26 -115.16  4.31   0.01
## 16      4  62.13 -114.79  4.68   0.01
## 135     5  63.52 -114.73  4.73   0.01
## 456     5  63.52 -114.72  4.74   0.01
## 6       3  60.74 -114.62  4.85   0.01
## 146     5  63.40 -114.48  4.98   0.01
## 346     5  63.35 -114.39  5.07   0.01
## 1256    6  64.86 -114.36  5.10   0.01
## 356     5  63.32 -114.33  5.13   0.01
## 1345    6  64.68 -114.00  5.46   0.01
## 2356    6  64.67 -113.99  5.48   0.01
## 1236    6  64.65 -113.95  5.52   0.01
## 156     5  63.04 -113.78  5.69   0.00
## 3456    6  64.51 -113.65  5.81   0.00
## 236     5  62.78 -113.25  6.22   0.00
## 2346    6  64.28 -113.21  6.26   0.00
## 12345   7  65.92 -113.16  6.30   0.00
## 12456   7  65.79 -112.91  6.55   0.00
## 36      4  61.19 -112.89  6.57   0.00
## 1346    6  64.12 -112.87  6.59   0.00
## 136     5  62.43 -112.55  6.92   0.00
## 
## Term codes: 
##    Diversity       GCMEAN  log(ARMEAN)      Ordinal PropInfected       Season 
##            1            2            3            4            5            6 
## 
## Model-averaged coefficients:  
## (full average) 
##                Estimate Std. Error Adjusted SE z value Pr(>|z|)  
## (Intercept)   0.1446473  0.0701433   0.0719256   2.011   0.0443 *
## GCMEAN        0.0003555  0.0004219   0.0004299   0.827   0.4082  
## PropInfected -0.0253469  0.0357292   0.0363832   0.697   0.4860  
## Ordinal       0.0017048  0.0024773   0.0025243   0.675   0.4994  
## Diversity    -0.0028437  0.0053357   0.0054464   0.522   0.6016  
## log(ARMEAN)   0.0004665  0.0165799   0.0170099   0.027   0.9781  
## Season        0.0003831  0.0057946   0.0060391   0.063   0.9494  
##  
## (conditional average) 
##                Estimate Std. Error Adjusted SE z value Pr(>|z|)  
## (Intercept)   0.1446473  0.0701433   0.0719256   2.011   0.0443 *
## GCMEAN        0.0006207  0.0003823   0.0003976   1.561   0.1185  
## PropInfected -0.0536602  0.0343985   0.0358204   1.498   0.1341  
## Ordinal       0.0037011  0.0024361   0.0025386   1.458   0.1449  
## Diversity    -0.0077714  0.0062855   0.0065398   1.188   0.2347  
## log(ARMEAN)   0.0017212  0.0318121   0.0326390   0.053   0.9579  
## Season        0.0020236  0.0131923   0.0137595   0.147   0.8831  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#No significant variables 

#Getting 95% confidence intervals 
confint(dredge1D_modelaverage, full=TRUE) 
##                      2.5 %      97.5 %
## (Intercept)   0.0036758165 0.285618823
## GCMEAN       -0.0004870596 0.001198102
## PropInfected -0.0966567624 0.045962909
## Ordinal      -0.0032426147 0.006652265
## Diversity    -0.0135184422 0.007831006
## log(ARMEAN)  -0.0328723152 0.033805408
## Season       -0.0114533975 0.012219611
#confirms NULL MODEL is the best model 

Red B/Y Channel

#model1E_group_maleID <- lmer(RedBY ~ Ordinal + Diversity + GCMEAN + log(ARMEAN) + PropInfected + Season + (1|Group) + (1|MaleID), data=data6, na.action = "na.fail")
## model convergence issues 

#model1E_group <- lmer(RedBY ~ Ordinal + Diversity + GCMEAN + log(ARMEAN) + PropInfected + Season + (1|Group), data=data6, na.action = "na.fail")
## model convergence issues 

#model1E_maleID <- lmer(RedBY ~ Ordinal + Diversity + GCMEAN + log(ARMEAN) + PropInfected + Season + (1|MaleID), data=data6, na.action = "na.fail")
## model convergence issues 

## Final model with no random variables: 
model1E <- lm(RedBY ~ Ordinal + Diversity + GCMEAN + log(ARMEAN) + PropInfected + Season, data=data6, na.action = "na.fail")
check_collinearity(model1E) # low VIF < 4
## # Check for Multicollinearity
## 
## Low Correlation
## 
##          Term  VIF   VIF 95% CI Increased SE Tolerance Tolerance 95% CI
##       Ordinal 1.27 [1.06, 2.11]         1.13      0.79     [0.47, 0.94]
##     Diversity 1.53 [1.20, 2.40]         1.24      0.65     [0.42, 0.83]
##        GCMEAN 3.07 [2.12, 4.84]         1.75      0.33     [0.21, 0.47]
##   log(ARMEAN) 3.29 [2.25, 5.20]         1.81      0.30     [0.19, 0.44]
##  PropInfected 1.59 [1.24, 2.48]         1.26      0.63     [0.40, 0.81]
##        Season 1.10 [1.01, 2.96]         1.05      0.91     [0.34, 0.99]
#dredge model selection
options(na.action = na.fail) 
dredge1E <- dredge(model1E)
## Fixed term is "(Intercept)"
options(na.action = na.omit)

#model averaging
dredge1E_modelaverage <- model.avg(dredge1E, subset = delta <= 7)
summary(dredge1E_modelaverage) 
## 
## Call:
## model.avg(object = dredge1E, subset = delta <= 7)
## 
## Component model call: 
## lm(formula = RedBY ~ <43 unique rhs>, data = data6, na.action = 
##      na.fail)
## 
## Component models: 
##        df logLik    AICc delta weight
## 236     5  60.82 -109.33  0.00   0.14
## 6       3  57.63 -108.41  0.93   0.09
## 23      4  58.61 -107.74  1.60   0.06
## 26      4  58.58 -107.67  1.66   0.06
## 2346    6  61.24 -107.12  2.22   0.05
## 2       3  56.84 -106.83  2.50   0.04
## 2356    6  61.06 -106.76  2.57   0.04
## 56      4  58.06 -106.64  2.70   0.04
## (Null)  2  55.42 -106.43  2.91   0.03
## 1236    6  60.85 -106.34  2.99   0.03
## 16      4  57.79 -106.09  3.25   0.03
## 234     5  59.12 -105.93  3.40   0.03
## 36      4  57.65 -105.81  3.52   0.02
## 46      4  57.64 -105.80  3.54   0.02
## 256     5  58.98 -105.64  3.69   0.02
## 156     5  58.80 -105.29  4.04   0.02
## 123     5  58.79 -105.27  4.07   0.02
## 126     5  58.76 -105.21  4.13   0.02
## 12      4  57.23 -104.97  4.36   0.02
## 235     5  58.63 -104.96  4.38   0.02
## 23456   7  61.80 -104.93  4.40   0.02
## 246     5  58.59 -104.87  4.47   0.02
## 1       3  55.79 -104.72  4.61   0.01
## 25      4  56.94 -104.40  4.93   0.01
## 24      4  56.89 -104.30  5.03   0.01
## 1256    6  59.76 -104.16  5.18   0.01
## 5       3  55.51 -104.16  5.18   0.01
## 4       3  55.47 -104.08  5.26   0.01
## 456     5  58.18 -104.04  5.29   0.01
## 3       3  55.45 -104.04  5.30   0.01
## 12356   7  61.34 -104.01  5.32   0.01
## 356     5  58.07 -103.83  5.50   0.01
## 12346   7  61.24 -103.81  5.53   0.01
## 125     5  57.81 -103.31  6.03   0.01
## 136     5  57.79 -103.27  6.06   0.01
## 146     5  57.79 -103.26  6.07   0.01
## 2345    6  59.29 -103.22  6.11   0.01
## 15      4  56.33 -103.18  6.15   0.01
## 346     5  57.66 -103.02  6.32   0.01
## 1234    6  59.18 -103.00  6.34   0.01
## 2456    6  59.09 -102.83  6.51   0.01
## 1235    6  59.01 -102.66  6.67   0.01
## 1456    6  58.92 -102.47  6.86   0.00
## 
## Term codes: 
##    Diversity       GCMEAN  log(ARMEAN)      Ordinal PropInfected       Season 
##            1            2            3            4            5            6 
## 
## Model-averaged coefficients:  
## (full average) 
##                Estimate Std. Error Adjusted SE z value Pr(>|z|)
## (Intercept)  -0.0311810  0.1354090   0.1377714   0.226    0.821
## GCMEAN        0.0007414  0.0007683   0.0007789   0.952    0.341
## log(ARMEAN)  -0.0284137  0.0394217   0.0400091   0.710    0.478
## Season       -0.0202318  0.0183037   0.0186679   1.084    0.278
## Ordinal      -0.0003431  0.0015124   0.0015615   0.220    0.826
## PropInfected -0.0080470  0.0250937   0.0257922   0.312    0.755
## Diversity     0.0010413  0.0040708   0.0041935   0.248    0.804
##  
## (conditional average) 
##                Estimate Std. Error Adjusted SE z value Pr(>|z|)  
## (Intercept)  -0.0311810  0.1354090   0.1377714   0.226   0.8209  
## GCMEAN        0.0011362  0.0006754   0.0006937   1.638   0.1014  
## log(ARMEAN)  -0.0580640  0.0381333   0.0393638   1.475   0.1402  
## Season       -0.0293945  0.0147452   0.0153943   1.909   0.0562 .
## Ordinal      -0.0016918  0.0029997   0.0031211   0.542   0.5878  
## PropInfected -0.0328408  0.0419000   0.0435965   0.753   0.4513  
## Diversity     0.0048159  0.0076462   0.0079471   0.606   0.5445  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#NULL model is best model 

#Getting 95% confidence intervals 
confint(dredge1E_modelaverage, full=TRUE) 
##                      2.5 %      97.5 %
## (Intercept)  -0.3012079708 0.238845953
## GCMEAN       -0.0007852258 0.002268094
## log(ARMEAN)  -0.1068301530 0.050002693
## Season       -0.0568202983 0.016356702
## Ordinal      -0.0034034967 0.002717335
## PropInfected -0.0585987849 0.042504718
## Diversity    -0.0071778898 0.009260425
#confirms NULL model 

Red R/G Channel

#model1F_group_maleID <- lmer(RedRG ~ Ordinal + Diversity + GCMEAN + log(ARMEAN) + PropInfected + Season + (1|Group) + (1|MaleID), data=data6, na.action 
## model convergence issues 

#model1F_group <- lmer(RedRG ~ Ordinal + Diversity + GCMEAN + log(ARMEAN) + PropInfected + Season + (1|Group), data=data6, na.action = "na.fail")
#check_collinearity(model1F_group) ## low VIF > 4
#options(na.action = na.fail)
#dd1F_group <- dredge(model1F_group) ## boundary (singular) fit: see help('isSingular')
##options(na.action = na.omit)
## model convergence issues when dredging, cannot use group as random variable 

#model1F_maleID <- lmer(RedRG ~ Ordinal + Diversity + GCMEAN + log(ARMEAN) + PropInfected + Season + (1|MaleID), data=data6, na.action = "na.fail")
## model convergence issues 


## Final model with no random variables: 
model1F <- lm(RedRG ~ Ordinal + Diversity + GCMEAN + log(ARMEAN) + PropInfected + Season, data=data6, na.action = "na.fail")
check_collinearity(model1F) # low VIF > 4
## # Check for Multicollinearity
## 
## Low Correlation
## 
##          Term  VIF   VIF 95% CI Increased SE Tolerance Tolerance 95% CI
##       Ordinal 1.27 [1.06, 2.11]         1.13      0.79     [0.47, 0.94]
##     Diversity 1.53 [1.20, 2.40]         1.24      0.65     [0.42, 0.83]
##        GCMEAN 3.07 [2.12, 4.84]         1.75      0.33     [0.21, 0.47]
##   log(ARMEAN) 3.29 [2.25, 5.20]         1.81      0.30     [0.19, 0.44]
##  PropInfected 1.59 [1.24, 2.48]         1.26      0.63     [0.40, 0.81]
##        Season 1.10 [1.01, 2.96]         1.05      0.91     [0.34, 0.99]
#dredge model selection
options(na.action = na.fail) 
dredge1F <- dredge(model1F)
## Fixed term is "(Intercept)"
options(na.action = na.omit)

#model averaging
dredge1F_modelaverage <- model.avg(dredge1F, subset = delta <= 7)
summary(dredge1F_modelaverage) 
## 
## Call:
## model.avg(object = dredge1F, subset = delta <= 7)
## 
## Component model call: 
## lm(formula = RedRG ~ <38 unique rhs>, data = data6, na.action = 
##      na.fail)
## 
## Component models: 
##        df logLik    AICc delta weight
## 46      4  86.16 -162.84  0.00   0.13
## 146     5  87.33 -162.36  0.48   0.10
## 14      4  85.88 -162.27  0.56   0.10
## 4       3  84.38 -161.90  0.94   0.08
## 134     5  86.64 -160.97  1.87   0.05
## 1456    6  87.96 -160.56  2.28   0.04
## 346     5  86.36 -160.40  2.43   0.04
## 1346    6  87.78 -160.19  2.65   0.03
## 145     5  86.23 -160.16  2.68   0.03
## 34      4  84.80 -160.12  2.71   0.03
## 246     5  86.21 -160.12  2.71   0.03
## 456     5  86.18 -160.05  2.78   0.03
## 124     5  86.12 -159.93  2.90   0.03
## 24      4  84.61 -159.74  3.09   0.03
## 1246    6  87.41 -159.45  3.38   0.02
## 45      4  84.39 -159.30  3.54   0.02
## 1345    6  87.12 -158.88  3.95   0.02
## 13456   7  88.50 -158.33  4.51   0.01
## 1234    6  86.78 -158.20  4.63   0.01
## (Null)  2  81.28 -158.14  4.70   0.01
## 6       3  82.34 -157.83  5.00   0.01
## 1245    6  86.47 -157.59  5.25   0.01
## 2346    6  86.39 -157.43  5.41   0.01
## 3456    6  86.37 -157.39  5.45   0.01
## 12456   7  88.02 -157.37  5.47   0.01
## 345     5  84.81 -157.32  5.52   0.01
## 234     5  84.81 -157.30  5.53   0.01
## 12346   7  87.98 -157.30  5.53   0.01
## 2456    6  86.23 -157.10  5.73   0.01
## 245     5  84.62 -156.94  5.90   0.01
## 1       3  81.83 -156.81  6.03   0.01
## 15      4  83.01 -156.54  6.29   0.01
## 56      4  82.92 -156.36  6.48   0.01
## 156     5  84.32 -156.33  6.51   0.01
## 5       3  81.53 -156.20  6.64   0.00
## 12345   7  87.36 -156.06  6.77   0.00
## 2       3  81.45 -156.05  6.79   0.00
## 16      4  82.70 -155.93  6.91   0.00
## 
## Term codes: 
##    Diversity       GCMEAN  log(ARMEAN)      Ordinal PropInfected       Season 
##            1            2            3            4            5            6 
## 
## Model-averaged coefficients:  
## (full average) 
##                Estimate Std. Error Adjusted SE z value Pr(>|z|)    
## (Intercept)   1.166e-01  3.420e-02   3.503e-02   3.328 0.000876 ***
## Ordinal      -3.088e-03  1.397e-03   1.439e-03   2.146 0.031892 *  
## Season        5.433e-03  6.918e-03   7.051e-03   0.771 0.440978    
## Diversity    -2.560e-03  3.330e-03   3.393e-03   0.754 0.450694    
## log(ARMEAN)  -2.284e-03  6.728e-03   6.916e-03   0.330 0.741246    
## PropInfected  2.859e-03  1.126e-02   1.159e-02   0.247 0.805108    
## GCMEAN       -7.344e-06  9.154e-05   9.500e-05   0.077 0.938374    
##  
## (conditional average) 
##                Estimate Std. Error Adjusted SE z value Pr(>|z|)    
## (Intercept)   1.166e-01  3.420e-02   3.503e-02   3.328 0.000876 ***
## Ordinal      -3.280e-03  1.201e-03   1.253e-03   2.618 0.008857 ** 
## Season        1.048e-02  6.279e-03   6.558e-03   1.598 0.110111    
## Diversity    -4.976e-03  3.088e-03   3.219e-03   1.546 0.122144    
## log(ARMEAN)  -9.160e-03  1.089e-02   1.135e-02   0.807 0.419698    
## PropInfected  1.210e-02  2.061e-02   2.136e-02   0.566 0.571105    
## GCMEAN       -3.768e-05  2.046e-04   2.125e-04   0.177 0.859264    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Ordinal rank is significant 

#Getting 95% confidence intervals 
confint(dredge1F_modelaverage, full=TRUE) 
##                      2.5 %        97.5 %
## (Intercept)   0.0479134833  0.1852302931
## Ordinal      -0.0059094432 -0.0002674220
## Season       -0.0083862924  0.0192520623
## Diversity    -0.0092105272  0.0040914751
## log(ARMEAN)  -0.0158386117  0.0112712878
## PropInfected -0.0198477687  0.0255648013
## GCMEAN       -0.0001935337  0.0001788448
#confirms ordinal rank is significant 

Sociosexual Analysis (Model 2)

Copulations

## MaleID and group as random variables 
#model2A_group_maleID <- glmer(COPcount ~ offset(log(FollowTime)) + BlueR.G + BlueLUM + RedLUM + BlueB.Y + RedB.Y + RedR.G + Ordinal + Season + (1|Group) + (1|MaleID), family = poisson, data = data7, na.action = "na.fail") 
#model convergence issues 

## Male ID as random variable
#model2A_maleID <- glmer(COPcount ~ offset(log(FollowTime)) + BlueR.G + BlueLUM + RedLUM + BlueB.Y + RedB.Y + RedR.G + Ordinal + Season + (1|MaleID), family = poisson, data = data7, na.action = "na.fail") 
#check_collinearity(model2A_maleID) # low VIF (<4)
#check_overdispersion(model2A_maleID) 
#OVER DISPERSION DETECTED (dispersion ratio: 1.649)

# re-run as negative binomial to fix over-dispersion 
#model2A_maleID_NB <- glmer.nb(COPcount ~ offset(log(FollowTime)) + BlueR.G + BlueLUM + RedLUM + BlueB.Y + RedB.Y + RedR.G + Ordinal + Season + (1|MaleID), data = data7, na.action = "na.fail") 
#model convergence issues 


## group as random variable 
#model2A_group <- glmer(COPcount ~ offset(log(FollowTime)) + BlueR.G + BlueLUM + RedLUM + BlueB.Y + RedB.Y + RedR.G + Ordinal + Season + (1|Group), family = poisson, data = data7, na.action = "na.fail") 
#check_collinearity(model2A_group) # low VIF (<4)
#check_overdispersion(model2A_group) 
#OVER DISPERSION DETECTED (dispersion ratio: 1.785)

# re-run as negative binomial to fix over-dispersion 
#model2A_group_NB <- glmer.nb(COPcount ~ offset(log(FollowTime)) + BlueR.G + BlueLUM + RedLUM + BlueB.Y + RedB.Y + RedR.G + Ordinal + Season + (1|Group), data = data7, na.action = "na.fail") 
#model convergence issues (will try without randoms)


## No random variables 
#model2A <- glm(COPcount ~ offset(log(FollowTime)) + BlueR.G + BlueLUM + RedLUM + BlueB.Y + RedB.Y + RedR.G + Ordinal + Season, family = poisson, data = data7, na.action = "na.fail") 
#check_collinearity(model2A) # low VIF (<4)
#check_overdispersion(model2A) 
#OVER DISPERSION DETECTED (dispersion ratio: 1.717)

#Rename dataframe
data7 <- data_socio

#Change column headings
data7 <- data7 %>%
  rename(Diversity = Richness,PropInfected = Prevalence, Season = Session)

# re-run as negative binomial to fix over-dispersion 
model2A_NB <- glm.nb(COPcount ~ offset(log(FollowTime)) + BlueRG + BlueLUM + RedLUM + BlueBY + RedBY + RedRG + Ordinal + Season, data = data7, na.action = "na.fail", control=glm.control(maxit=50)) 
summary(model2A_NB)
## 
## Call:
## glm.nb(formula = COPcount ~ offset(log(FollowTime)) + BlueRG + 
##     BlueLUM + RedLUM + BlueBY + RedBY + RedRG + Ordinal + Season, 
##     data = data7, na.action = "na.fail", control = glm.control(maxit = 50), 
##     init.theta = 11.84322451, link = log)
## 
## Coefficients:
##              Estimate Std. Error z value Pr(>|z|)  
## (Intercept)  -2.25548    2.08525  -1.082   0.2794  
## BlueRG      -20.92224   20.27309  -1.032   0.3021  
## BlueLUM      -7.26862    3.23800  -2.245   0.0248 *
## RedLUM       -0.15913    6.23133  -0.026   0.9796  
## BlueBY       -4.38115    5.28090  -0.830   0.4068  
## RedBY         7.68089    4.48245   1.714   0.0866 .
## RedRG        -2.73486   10.12722  -0.270   0.7871  
## Ordinal      -0.19461    0.08256  -2.357   0.0184 *
## SeasonP3      0.87967    0.34930   2.518   0.0118 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for Negative Binomial(11.8432) family taken to be 1)
## 
##     Null deviance: 67.715  on 30  degrees of freedom
## Residual deviance: 37.219  on 22  degrees of freedom
## AIC: 126.35
## 
## Number of Fisher Scoring iterations: 1
## 
## 
##               Theta:  11.8 
##           Std. Err.:  16.5 
## 
##  2 x log-likelihood:  -106.353
check_collinearity(model2A_NB) # low VIF (<4)
## # Check for Multicollinearity
## 
## Low Correlation
## 
##     Term  VIF   VIF 95% CI Increased SE Tolerance Tolerance 95% CI
##   BlueRG 3.06 [2.17, 4.64]         1.75      0.33     [0.22, 0.46]
##  BlueLUM 2.00 [1.51, 2.99]         1.42      0.50     [0.33, 0.66]
##   RedLUM 3.01 [2.14, 4.55]         1.73      0.33     [0.22, 0.47]
##   BlueBY 2.84 [2.03, 4.28]         1.68      0.35     [0.23, 0.49]
##    RedBY 1.68 [1.31, 2.50]         1.30      0.59     [0.40, 0.76]
##    RedRG 1.92 [1.45, 2.85]         1.38      0.52     [0.35, 0.69]
##  Ordinal 2.41 [1.76, 3.62]         1.55      0.41     [0.28, 0.57]
##   Season 1.58 [1.25, 2.36]         1.26      0.63     [0.42, 0.80]
check_overdispersion(model2A_NB) # no overdispersion 
## # Overdispersion test
## 
##  dispersion ratio = 1.082
##           p-value = 0.632
## No overdispersion detected.
#dredge model selection 
options(na.action = na.fail)
dredge_model2A_NB <- dredge(model2A_NB)
## Fixed term is "(Intercept)"
options(na.action = "na.omit")

#model averaging 
model2A_NB_modelaverage <- model.avg(dredge_model2A_NB, subset = delta <= 7)
summary(model2A_NB_modelaverage) #No significant variables 
## 
## Call:
## model.avg(object = dredge_model2A_NB, subset = delta <= 7)
## 
## Component model call: 
## glm.nb(formula = COPcount ~ <67 unique rhs>, data = data7, na.action = 
##      na.fail, control = glm.control(maxit = 50), init.theta = 11.84322451, 
##      link = log)
## 
## Component models: 
##         df logLik   AICc delta weight
## 24589    6 -53.72 122.94  0.00   0.15
## 2458     6 -54.32 124.14  1.21   0.08
## 2489     5 -56.69 125.78  2.85   0.04
## 234589   7 -53.54 125.94  3.01   0.03
## 4589     5 -56.87 126.14  3.21   0.03
## 489      4 -58.34 126.23  3.29   0.03
## 49       3 -59.70 126.29  3.35   0.03
## 245789   7 -53.71 126.29  3.35   0.03
## 124589   7 -53.72 126.30  3.37   0.03
## 245689   7 -53.72 126.31  3.37   0.03
## 23458    7 -53.95 126.77  3.83   0.02
## 458      5 -57.30 127.01  4.07   0.02
## 24578    7 -54.22 127.30  4.37   0.02
## 139      4 -58.93 127.41  4.47   0.02
## 48       4 -58.94 127.43  4.49   0.02
## 12458    7 -54.28 127.43  4.50   0.02
## 45689    6 -55.97 127.44  4.51   0.02
## 248      5 -57.54 127.48  4.54   0.02
## 24568    7 -54.32 127.51  4.57   0.02
## 249      4 -59.02 127.57  4.64   0.01
## 349      4 -59.09 127.72  4.79   0.01
## 1239     5 -57.67 127.73  4.80   0.01
## 3489     5 -57.70 127.81  4.87   0.01
## 23489    6 -56.17 127.85  4.91   0.01
## 39       3 -60.49 127.86  4.93   0.01
## 459      4 -59.19 127.92  4.98   0.01
## 24789    6 -56.23 127.96  5.02   0.01
## 34589    6 -56.41 128.32  5.38   0.01
## 4568     6 -56.42 128.33  5.39   0.01
## 24689    6 -56.47 128.44  5.50   0.01
## 348      5 -58.10 128.61  5.67   0.01
## 5689     5 -58.11 128.61  5.67   0.01
## 389      4 -59.57 128.68  5.74   0.01
## 3458     6 -56.60 128.71  5.77   0.01
## 4789     5 -58.21 128.81  5.87   0.01
## 12349    6 -56.66 128.82  5.89   0.01
## 12489    6 -56.66 128.83  5.89   0.01
## 38       4 -59.65 128.83  5.89   0.01
## 469      4 -59.66 128.86  5.92   0.01
## 14589    6 -56.70 128.91  5.97   0.01
## 4689     5 -58.25 128.91  5.97   0.01
## 2459     5 -58.26 128.91  5.97   0.01
## 479      4 -59.69 128.92  5.99   0.01
## 149      4 -59.70 128.93  6.00   0.01
## 1234589  8 -53.22 128.98  6.04   0.01
## 568      5 -58.30 129.01  6.07   0.01
## 1489     5 -58.30 129.01  6.07   0.01
## 123489   7 -55.07 129.01  6.07   0.01
## 2478     6 -56.79 129.08  6.15   0.01
## 1349     5 -58.36 129.12  6.19   0.01
## 45789    6 -56.83 129.16  6.22   0.01
## 1389     5 -58.39 129.17  6.24   0.01
## 2348     6 -56.84 129.18  6.25   0.01
## 12389    6 -56.86 129.21  6.28   0.01
## 2349     5 -58.44 129.27  6.34   0.01
## 1369     5 -58.46 129.33  6.39   0.01
## 358      5 -58.47 129.35  6.41   0.01
## 1458     6 -56.96 129.41  6.48   0.01
## 2345689  8 -53.51 129.57  6.63   0.01
## 2345789  8 -53.53 129.60  6.66   0.01
## 478      5 -58.60 129.61  6.67   0.01
## 3589     5 -58.60 129.61  6.67   0.01
## 138      5 -58.64 129.67  6.74   0.01
## 13       4 -60.11 129.76  6.82   0.00
## 3459     5 -58.69 129.78  6.84   0.00
## 359      4 -60.13 129.80  6.86   0.00
## 4        3 -61.46 129.80  6.86   0.00
## 
## Term codes: 
##                  BlueBY                 BlueLUM                  BlueRG 
##                       1                       2                       3 
##                 Ordinal                   RedBY                  RedLUM 
##                       4                       5                       6 
##                   RedRG                  Season offset(log(FollowTime)) 
##                       7                       8                       9 
## 
## Model-averaged coefficients:  
## (full average) 
##             Estimate Std. Error Adjusted SE z value Pr(>|z|)  
## (Intercept) -1.58570    3.55587     3.57087   0.444   0.6570  
## BlueLUM     -3.75123    3.76123     3.82040   0.982   0.3262  
## Ordinal     -0.17934    0.09588     0.09773   1.835   0.0665 .
## RedBY        5.51562    5.60404     5.69082   0.969   0.3324  
## SeasonP3     0.75801    0.50925     0.51774   1.464   0.1432  
## BlueRG      -7.60040   17.37696    17.57271   0.433   0.6654  
## RedRG       -0.36601    3.20412     3.32134   0.110   0.9123  
## BlueBY      -0.75342    3.23192     3.27874   0.230   0.8183  
## RedLUM      -0.36826    2.58278     2.64560   0.139   0.8893  
##  
## (conditional average) 
##              Estimate Std. Error Adjusted SE z value Pr(>|z|)   
## (Intercept)  -1.58570    3.55587     3.57087   0.444  0.65700   
## BlueLUM      -6.23439    2.83380     2.96243   2.104  0.03534 * 
## Ordinal      -0.20358    0.07417     0.07686   2.649  0.00808 **
## RedBY         9.16719    4.32691     4.51125   2.032  0.04215 * 
## SeasonP3      0.93145    0.39639     0.40968   2.274  0.02299 * 
## BlueRG      -27.00415   23.42806    23.94121   1.128  0.25935   
## RedRG        -3.82027    9.69328    10.09673   0.378  0.70516   
## BlueBY       -4.53780    6.76305     6.89745   0.658  0.51061   
## RedLUM       -3.06915    6.87797     7.07417   0.434  0.66439   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Getting 95% confidence intervals
confint(model2A_NB_modelaverage, full=TRUE) #confirms NULL model is best model
##                   2.5 %      97.5 %
## (Intercept)  -8.5844755  5.41307458
## BlueLUM     -11.2390702  3.73660312
## Ordinal      -0.3708779  0.01220255
## RedBY        -5.6381879 16.66943444
## SeasonP3     -0.2567524  1.77276682
## BlueRG      -42.0422793 26.84147261
## RedRG        -6.8757247  6.14370178
## BlueBY       -7.1796392  5.67280031
## RedLUM       -5.5535354  4.81702385

Mate Presentations

## MaleID and group as random variables 
#model2B_group_maleID <- glmer(PRcount ~ offset(log(FollowTime)) + BlueR.G + BlueLUM + RedLUM + BlueB.Y + RedB.Y + RedR.G + Ordinal + Season + (1|Group) + (1|MaleID), family = poisson, data = data7, na.action = "na.fail") 
#model convergence issues 

## group as random variable 
#model2B_group <- glmer(PRcount ~ offset(log(FollowTime)) + BlueR.G + BlueLUM + RedLUM + BlueB.Y + RedB.Y + RedR.G + Ordinal + Season + (1|Group), family = poisson, data = data7, na.action = "na.fail") 
#model convergence issues 

## Male ID as random variable
#model2B_maleID <- glmer(PRcount ~ offset(log(FollowTime)) + BlueR.G + BlueLUM + RedLUM + BlueB.Y + RedB.Y + RedR.G + Ordinal + Season + (1|MaleID), family = poisson, data = data7, na.action = "na.fail") 
#model convergence issues 

## No random variables
model2B <- glm(PRcount ~ offset(log(FollowTime)) + BlueRG + BlueLUM + RedLUM + BlueBY + RedBY + RedRG + Ordinal + Season, family = poisson, data = data7, na.action = "na.fail") 
check_collinearity(model2B) # low VIF (<6)
## # Check for Multicollinearity
## 
## Low Correlation
## 
##     Term  VIF   VIF 95% CI Increased SE Tolerance Tolerance 95% CI
##  BlueLUM 3.24 [2.28, 4.92]         1.80      0.31     [0.20, 0.44]
##   RedLUM 3.64 [2.54, 5.54]         1.91      0.27     [0.18, 0.39]
##   BlueBY 2.83 [2.02, 4.26]         1.68      0.35     [0.23, 0.49]
##    RedBY 2.10 [1.57, 3.14]         1.45      0.48     [0.32, 0.64]
##    RedRG 2.73 [1.96, 4.12]         1.65      0.37     [0.24, 0.51]
##  Ordinal 3.06 [2.17, 4.63]         1.75      0.33     [0.22, 0.46]
##   Season 1.90 [1.44, 2.82]         1.38      0.53     [0.35, 0.69]
## 
## Moderate Correlation
## 
##    Term  VIF   VIF 95% CI Increased SE Tolerance Tolerance 95% CI
##  BlueRG 5.78 [3.89, 8.90]         2.40      0.17     [0.11, 0.26]
check_overdispersion(model2B) # no over dispersion 
## # Overdispersion test
## 
##        dispersion ratio =  1.161
##   Pearson's Chi-Squared = 25.543
##                 p-value =  0.272
## No overdispersion detected.
#dredge model selection
options(na.action = na.fail)
dredge_model2B <- dredge(model2B)
## Fixed term is "(Intercept)"
options(na.action = "na.omit")

#model averaging No random variables
model2B_modelaverage <- model.avg(dredge_model2B, subset = delta <= 7)
summary(model2B_modelaverage) #RedLUM and Season are significant  
## 
## Call:
## model.avg(object = dredge_model2B, subset = delta <= 7)
## 
## Component model call: 
## glm(formula = PRcount ~ <97 unique rhs>, family = poisson, data = 
##      data7, na.action = na.fail)
## 
## Component models: 
##         df logLik  AICc delta weight
## 678      4 -27.74 65.01  0.00   0.07
## 6789     4 -27.84 65.21  0.19   0.07
## 689      3 -29.48 65.85  0.84   0.05
## 5678     5 -26.84 66.07  1.06   0.04
## 68       3 -29.90 66.68  1.67   0.03
## 56789    5 -27.22 66.84  1.82   0.03
## 16789    5 -27.29 66.98  1.97   0.03
## 1678     5 -27.41 67.22  2.20   0.02
## 5689     4 -28.87 67.27  2.26   0.02
## 568      4 -29.03 67.60  2.59   0.02
## 2678     5 -27.65 67.70  2.68   0.02
## 26789    5 -27.69 67.78  2.76   0.02
## 4678     5 -27.71 67.83  2.81   0.02
## 4689     4 -29.16 67.86  2.85   0.02
## 3678     5 -27.74 67.87  2.86   0.02
## 46789    5 -27.80 67.99  2.98   0.02
## 2689     4 -29.24 68.02  3.01   0.02
## 36789    5 -27.83 68.05  3.04   0.02
## 4568     5 -27.83 68.06  3.04   0.02
## 1689     4 -29.29 68.12  3.11   0.02
## 45678    6 -26.32 68.14  3.13   0.02
## 25678    6 -26.34 68.17  3.16   0.02
## 45689    5 -27.90 68.20  3.19   0.02
## 3689     4 -29.44 68.43  3.41   0.01
## 468      4 -29.51 68.57  3.55   0.01
## 25689    5 -28.14 68.69  3.67   0.01
## 15678    6 -26.60 68.69  3.68   0.01
## 256789   6 -26.65 68.79  3.78   0.01
## 35678    6 -26.66 68.82  3.80   0.01
## 268      4 -29.65 68.83  3.81   0.01
## 2568     5 -28.25 68.90  3.89   0.01
## 456789   6 -26.77 69.04  4.03   0.01
## 156789   6 -26.77 69.04  4.03   0.01
## 69       2 -32.33 69.08  4.07   0.01
## 168      4 -29.82 69.17  4.16   0.01
## 368      4 -29.88 69.31  4.29   0.01
## 356789   6 -26.97 69.45  4.43   0.01
## 35689    5 -28.63 69.67  4.65   0.01
## 126789   6 -27.14 69.77  4.76   0.01
## 136789   6 -27.14 69.78  4.77   0.01
## 13678    6 -27.20 69.90  4.88   0.01
## 15689    5 -28.77 69.94  4.92   0.01
## 146789   6 -27.27 70.03  5.02   0.01
## 3568     5 -28.87 70.15  5.13   0.01
## 14689    5 -28.87 70.15  5.14   0.01
## 12678    6 -27.33 70.16  5.15   0.01
## 169      3 -31.65 70.19  5.17   0.01
## 12689    5 -28.94 70.27  5.26   0.01
## 14678    6 -27.40 70.29  5.28   0.01
## 1568     5 -29.02 70.43  5.42   0.00
## 23678    6 -27.58 70.65  5.64   0.00
## 24689    5 -29.14 70.67  5.66   0.00
## 34689    5 -29.15 70.70  5.68   0.00
## 24678    6 -27.64 70.79  5.78   0.00
## 236789   6 -27.66 70.83  5.81   0.00
## 469      3 -31.98 70.85  5.84   0.00
## 246789   6 -27.68 70.86  5.85   0.00
## 34678    6 -27.68 70.87  5.85   0.00
## 23689    5 -29.24 70.88  5.87   0.00
## 679      3 -32.00 70.89  5.88   0.00
## 145689   6 -27.72 70.94  5.93   0.00
## 13689    5 -29.29 70.98  5.97   0.00
## 24568    6 -27.74 70.99  5.97   0.00
## 14568    6 -27.78 71.06  6.05   0.00
## 245689   6 -27.78 71.06  6.05   0.00
## 346789   6 -27.79 71.09  6.08   0.00
## 125678   7 -26.14 71.15  6.13   0.00
## 1468     5 -29.38 71.15  6.14   0.00
## 34568    6 -27.83 71.16  6.14   0.00
## 145678   7 -26.15 71.17  6.15   0.00
## 569      3 -32.15 71.18  6.17   0.00
## 1679     4 -30.85 71.24  6.22   0.00
## 245678   7 -26.20 71.27  6.26   0.00
## 345689   6 -27.89 71.27  6.26   0.00
## 269      3 -32.20 71.28  6.27   0.00
## 3468     5 -29.45 71.29  6.28   0.00
## 1256789  7 -26.24 71.34  6.33   0.00
## 2468     5 -29.50 71.40  6.39   0.00
## 1268     5 -29.50 71.40  6.39   0.00
## 125689   6 -27.95 71.40  6.39   0.00
## 345678   7 -26.31 71.50  6.48   0.00
## 8        2 -33.54 71.51  6.50   0.00
## 235678   7 -26.33 71.53  6.52   0.00
## 369      3 -32.33 71.54  6.53   0.00
## 89       2 -33.56 71.55  6.53   0.00
## 1236789  7 -26.35 71.57  6.56   0.00
## 1469     4 -31.02 71.58  6.56   0.00
## 2368     5 -29.63 71.65  6.64   0.00
## 1456789  7 -26.40 71.67  6.65   0.00
## 235689   6 -28.11 71.73  6.71   0.00
## 12568    6 -28.20 71.90  6.89   0.00
## 6        2 -33.74 71.91  6.89   0.00
## 123678   7 -26.53 71.94  6.93   0.00
## 13469    5 -29.79 71.98  6.96   0.00
## 23568    6 -28.25 71.99  6.98   0.00
## 12389    5 -29.80 72.00  6.99   0.00
## 2456789  7 -26.57 72.01  6.99   0.00
## 
## Term codes: 
##                  BlueBY                 BlueLUM                  BlueRG 
##                       1                       2                       3 
##                 Ordinal                   RedBY                  RedLUM 
##                       4                       5                       6 
##                   RedRG                  Season offset(log(FollowTime)) 
##                       7                       8                       9 
## 
## Model-averaged coefficients:  
## (full average) 
##              Estimate Std. Error Adjusted SE z value Pr(>|z|)  
## (Intercept)   1.28803    4.63513     4.68893   0.275   0.7835  
## RedLUM      -26.76313   10.98169    11.36219   2.355   0.0185 *
## RedRG       -17.03864   19.50665    19.89667   0.856   0.3918  
## SeasonP3      1.54441    0.74592     0.76929   2.008   0.0447 *
## RedBY         3.49350    6.64571     6.79705   0.514   0.6073  
## BlueBY       -1.27009    4.26956     4.39591   0.289   0.7726  
## BlueLUM      -0.71612    2.60165     2.68260   0.267   0.7895  
## Ordinal       0.01642    0.05762     0.05927   0.277   0.7817  
## BlueRG       -0.15035   10.25109    10.62599   0.014   0.9887  
##  
## (conditional average) 
##              Estimate Std. Error Adjusted SE z value Pr(>|z|)  
## (Intercept)   1.28803    4.63513     4.68893   0.275   0.7835  
## RedLUM      -26.97986   10.75764    11.14887   2.420   0.0155 *
## RedRG       -29.92589   16.81211    17.59656   1.701   0.0890 .
## SeasonP3      1.61515    0.68383     0.71040   2.274   0.0230 *
## RedBY         9.96994    7.84041     8.20232   1.216   0.2242  
## BlueBY       -5.84054    7.55867     7.88469   0.741   0.4588  
## BlueLUM      -3.39470    4.79509     5.00208   0.679   0.4974  
## Ordinal       0.07467    0.10366     0.10780   0.693   0.4885  
## BlueRG       -0.90233   25.09968    26.01857   0.035   0.9723  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Getting 95% confidence intervals No random variables
confint(model2B_modelaverage, full=TRUE) #confirms RedLUM and Season are significant  
##                    2.5 %     97.5 %
## (Intercept)  -7.90209543 10.4781628
## RedLUM      -49.03260883 -4.4936558
## RedRG       -56.03540725 21.9581240
## SeasonP3      0.03663412  3.0521924
## RedBY        -9.82847163 16.8154687
## BlueBY       -9.88590398  7.3457331
## BlueLUM      -5.97392670  4.5416909
## Ordinal      -0.09973570  0.1325824
## BlueRG      -20.97689786 20.6762000

Mate Refusals

## MaleID and group as random variables
#model2C_group_maleID <- glmer(MRcount ~ offset(log(FollowTime)) + BlueR.G + BlueLUM + RedLUM + BlueB.Y + RedB.Y + RedR.G + Ordinal + Season + (1|Group) + (1|MaleID), family = poisson, data = data7, na.action = "na.fail") 
#model convergence issues 

## group as random variable 
#model2C_group <- glmer(MRcount ~ offset(log(FollowTime)) + BlueR.G + BlueLUM + RedLUM + BlueB.Y + RedB.Y + RedR.G + Ordinal + Season + (1|Group), family = poisson, data = data7, na.action = "na.fail") 
#model convergence issues 

## Male ID as random variable 
#model2C_maleID <- glmer(MRcount ~ offset(log(FollowTime)) + BlueR.G + BlueLUM + RedLUM + BlueB.Y + RedB.Y + RedR.G + Ordinal + Season + (1|MaleID), family = poisson, data = data7, na.action = "na.fail") 
#model convergence issues 

## No random variables 
#model2C <- glm(MRcount ~ offset(log(FollowTime)) + BlueR.G + BlueLUM + RedLUM + BlueB.Y + RedB.Y + RedR.G + Ordinal + Season, family = poisson, data = data7, na.action = "na.fail") 
#check_collinearity(model2C) ## VIF > 8 
#check_overdispersion(model2C) 
# OVER DISPERSION DETECTED (RATIO = 2.025) 

# re-run as negative binomial to fix over-dispersion 
#model2C_NB <- glmer.nb(MRcount ~ offset(log(FollowTime)) + BlueR.G + BlueLUM + RedLUM + BlueB.Y + RedB.Y + RedR.G + Ordinal + Season + (1|Group), data = data7, control=glmerControl(optCtrl=list(maxfun=30000)), na.action= "na.fail") # Warning: NaNs produced
#check_collinearity(model2C_NB) ## VIF < 8 
#check_overdispersion(model2C_NB) #OVERDISPERSION DETECTED (RATIO = 2.025)

## re-run as quasi-poisson 
#model2C_QP <- glm(MRcount ~ offset(log(FollowTime)) + BlueR.G + BlueLUM + RedLUM + BlueB.Y + RedB.Y + RedR.G + Ordinal + Season, family = quasipoisson, data = data7, na.action = "na.fail") 
#check_collinearity(model2C_QP) ## VIF > 8 
#check_overdispersion(model2C_QP) #OVERDISPERSION DETECTED (RATIO = 2.025)

## zero inflated poisson
#model2C_group_maleID <-glmmTMB(MRcount ~ offset(log(FollowTime)) + BlueR.G + BlueLUM + RedLUM + BlueB.Y + RedB.Y + RedR.G + Ordinal + Season, data = data7, family = poisson() , ziformula=~1)
#check_collinearity(model2C_group_maleID) # VIF < 8
#check_zeroinflation(model2C_group_maleID) # model ok (no zero inflation)
#check_overdispersion(model2C_group_maleID) #OVERDISPERSION DETECTED (RATIO = 2.121)

## zero-inflated negative binomial 2 
#model2C_group_maleID_NB <- update(model2C_group_maleID,family=nbinom2)
#model failed to converge

## zero-inflated negative binomial 1 
#model2C_group_maleID_NB <- update(model2C_group_maleID,family=nbinom1)
#model failed to converge

## Mating refusals = extreme zero inflation, cannot analyze the data

Intramale Analyses

#Load data
STDATA <- read.csv("Intramale_ST.csv")
LTDATA <- read.csv("Intramale_LT(1).csv")

Short Term with Benjamini-Hochberg correction

STDATA <- read.csv("Intramale_ST.csv")
# Select the relevant columns
variables <- c('BLUMST', 'BBYST', 'BRGST', 'RLUMST', 'RBYST', 'RRGST', 'ORDST', 'PREVST', 'RCHST', 'GCST', 'ARST')
STDATA_selected <- STDATA[, variables]

# Calculate the Spearman correlation matrix
correlation_matrix <- rcorr(as.matrix(STDATA_selected), type = "spearman")

# Extract the p-values
p_values <- correlation_matrix$P

# Apply Benjamini-Hochberg correction
p_values_corrected_bh <- p.adjust(p_values, method = "BH")

# Reshape the corrected p-values back into a matrix
p_values_corrected_matrix_bh <- matrix(p_values_corrected_bh, nrow = length(variables), ncol = length(variables))
dimnames(p_values_corrected_matrix_bh) <- list(variables, variables)


print("Spearman Correlation Matrix:")
## [1] "Spearman Correlation Matrix:"
print(correlation_matrix$r)
##             BLUMST       BBYST        BRGST     RLUMST       RBYST      RRGST
## BLUMST  1.00000000 -0.45000000 -0.317857143  0.4428571  0.01785714 -0.0750000
## BBYST  -0.45000000  1.00000000  0.892857143 -0.1000000 -0.30000000  0.1750000
## BRGST  -0.31785714  0.89285714  1.000000000 -0.1321429 -0.18571429  0.2250000
## RLUMST  0.44285714 -0.10000000 -0.132142857  1.0000000 -0.60714286 -0.4392857
## RBYST   0.01785714 -0.30000000 -0.185714286 -0.6071429  1.00000000  0.5857143
## RRGST  -0.07500000  0.17500000  0.225000000 -0.4392857  0.58571429  1.0000000
## ORDST  -0.30186808  0.68374939  0.574640444  0.1854852 -0.73466690 -0.4982642
## PREVST -0.22183237 -0.11722849 -0.218225343 -0.3913628  0.41480850  0.3246327
## RCHST  -0.27847984  0.24550196 -0.007328417 -0.2968009  0.28214405  0.3993987
## GCST    0.31428571  0.11428571  0.042857143  0.2428571 -0.07500000  0.3357143
## ARST    0.07500000  0.01785714 -0.085714286  0.1607143  0.03928571  0.2321429
##              ORDST     PREVST        RCHST        GCST        ARST
## BLUMST -0.30186808 -0.2218324 -0.278479838  0.31428571  0.07500000
## BBYST   0.68374939 -0.1172285  0.245501963  0.11428571  0.01785714
## BRGST   0.57464044 -0.2182253 -0.007328417  0.04285714 -0.08571429
## RLUMST  0.18548521 -0.3913628 -0.296800880  0.24285714  0.16071429
## RBYST  -0.73466690  0.4148085  0.282144047 -0.07500000  0.03928571
## RRGST  -0.49826418  0.3246327  0.399398716  0.33571429  0.23214286
## ORDST   1.00000000 -0.3765051 -0.041045919 -0.15275252 -0.30550505
## PREVST -0.37650505  1.0000000  0.549559173  0.30299056  0.45087881
## RCHST  -0.04104592  0.5495592  1.000000000  0.32977876  0.30046509
## GCST   -0.15275252  0.3029906  0.329778756  1.00000000  0.82857143
## ARST   -0.30550505  0.4508788  0.300465089  0.82857143  1.00000000
print("Corrected P-Values Matrix (BH):")
## [1] "Corrected P-Values Matrix (BH):"
print(p_values_corrected_matrix_bh)
##           BLUMST        BBYST        BRGST    RLUMST      RBYST     RRGST
## BLUMST        NA 0.4288331858 0.5553473663 0.4288332 0.96722114 0.8873080
## BBYST  0.4288332           NA 0.0004122105 0.8835412 0.55534737 0.7513115
## BRGST  0.5553474 0.0004122105           NA 0.8364508 0.73537038 0.6639688
## RLUMST 0.4288332 0.8835411753 0.8364507628        NA 0.18019437 0.4288332
## RBYST  0.9672211 0.5553473663 0.7353703768 0.1801944         NA 0.1968056
## RRGST  0.8873080 0.7513114910 0.6639688443 0.4288332 0.19680555        NA
## ORDST  0.5553474 0.0679673578 0.1968055543 0.7353704 0.03320002 0.3587955
## PREVST 0.6639688 0.8563322033 0.6639688443 0.5127043 0.48789891 0.5553474
## RCHST  0.5772713 0.6585018375 0.9793207158 0.5553474 0.57727130 0.5127043
## GCST   0.5553474 0.8563322033 0.9407601179 0.6585018 0.88730803 0.5553474
## ARST   0.8873080 0.9672211431 0.8873080343 0.7798955 0.94076012 0.6639688
##             ORDST    PREVST     RCHST        GCST        ARST
## BLUMST 0.55534737 0.6639688 0.5772713 0.555347366 0.887308034
## BBYST  0.06796736 0.8563322 0.6585018 0.856332203 0.967221143
## BRGST  0.19680555 0.6639688 0.9793207 0.940760118 0.887308034
## RLUMST 0.73537038 0.5127043 0.5553474 0.658501838 0.779895469
## RBYST  0.03320002 0.4878989 0.5772713 0.887308034 0.940760118
## RRGST  0.35879546 0.5553474 0.5127043 0.555347366 0.663968844
## ORDST          NA 0.5389802 0.9407601 0.787160331 0.555347366
## PREVST 0.53898016        NA 0.2325621 0.555347366 0.428833186
## RCHST  0.94076012 0.2325621        NA 0.555347366 0.555347366
## GCST   0.78716033 0.5553474 0.5553474          NA 0.003719231
## ARST   0.55534737 0.4288332 0.5553474 0.003719231          NA
##CSV to Desktop:

combined_matrix <- cbind(as.data.frame(correlation_matrix$r), as.data.frame(p_values_corrected_matrix_bh))
colnames(combined_matrix) <- paste0(rep(variables, 2), "_", rep(c("Corr", "PValue"), each = length(variables)))

Long term with Benjamini-Hochberg correction

LTDATA <- read.csv("Intramale_LT(1).csv")
# List of expected columns for LTDATA
variables_lt <- c('BLUMLT', 'BBYLT', 'BRGLT', 'RLUMLT', 'RBYRT', 'RRGLT', 'ORDLT', 'GCLT', 'ARLT')
LTDATA_selected <- LTDATA[, variables_lt]

# Calculate the Spearman correlation matrix
correlation_matrix_lt <- rcorr(as.matrix(LTDATA_selected), type = "spearman")

# Extract the p-values
p_values_lt <- correlation_matrix_lt$P

# Apply Benjamini-Hochberg correction
p_values_corrected_bh_lt <- p.adjust(p_values_lt, method = "BH")

# Reshape the corrected p-values back into a matrix
p_values_corrected_matrix_bh_lt <- matrix(p_values_corrected_bh_lt, nrow = length(variables_lt), ncol = length(variables_lt))
dimnames(p_values_corrected_matrix_bh_lt) <- list(variables_lt, variables_lt)

# Print the correlation matrix and the corrected p-values matrix
print("Spearman Correlation Matrix:")
## [1] "Spearman Correlation Matrix:"
print(correlation_matrix_lt$r, digits=2)
##        BLUMLT BBYLT BRGLT RLUMLT RBYRT RRGLT ORDLT GCLT ARLT
## BLUMLT    1.0   0.6   0.7   -0.8   0.4   0.8  -0.8 -0.5  0.6
## BBYLT     0.6   1.0   0.9   -0.4   0.8   0.4  -0.6 -0.7  0.8
## BRGLT     0.7   0.9   1.0   -0.2   0.4   0.2  -0.8 -0.9  0.9
## RLUMLT   -0.8  -0.4  -0.2    1.0  -0.8  -1.0   0.4  0.2 -0.2
## RBYRT     0.4   0.8   0.4   -0.8   1.0   0.8  -0.2 -0.4  0.4
## RRGLT     0.8   0.4   0.2   -1.0   0.8   1.0  -0.4 -0.2  0.2
## ORDLT    -0.8  -0.6  -0.8    0.4  -0.2  -0.4   1.0  0.6 -0.9
## GCLT     -0.5  -0.7  -0.9    0.2  -0.4  -0.2   0.6  1.0 -0.7
## ARLT      0.6   0.8   0.9   -0.2   0.4   0.2  -0.9 -0.7  1.0
print("Corrected P-Values Matrix (BH):")
## [1] "Corrected P-Values Matrix (BH):"
print(p_values_corrected_matrix_bh_lt)
##           BLUMLT     BBYLT     BRGLT    RLUMLT     RBYRT     RRGLT     ORDLT
## BLUMLT        NA 0.5125626 0.4500000 0.4500000 0.7448276 0.4500000 0.4500000
## BBYLT  0.5125626        NA 0.2691797 0.7448276 0.4500000 0.7448276 0.5125626
## BRGLT  0.4500000 0.2691797        NA 0.8000000 0.7448276 0.8000000 0.4500000
## RLUMLT 0.4500000 0.7448276 0.8000000        NA 0.4500000 0.0000000 0.7448276
## RBYRT  0.7448276 0.4500000 0.7448276 0.4500000        NA 0.4500000 0.8000000
## RRGLT  0.4500000 0.7448276 0.8000000 0.0000000 0.4500000        NA 0.7448276
## ORDLT  0.4500000 0.5125626 0.4500000 0.7448276 0.8000000 0.7448276        NA
## GCLT   0.6702895 0.4500000 0.2691797 0.8000000 0.7448276 0.8000000 0.5125626
## ARLT   0.5125626 0.4500000 0.2691797 0.8000000 0.7448276 0.8000000 0.2691797
##             GCLT      ARLT
## BLUMLT 0.6702895 0.5125626
## BBYLT  0.4500000 0.4500000
## BRGLT  0.2691797 0.2691797
## RLUMLT 0.8000000 0.8000000
## RBYRT  0.7448276 0.7448276
## RRGLT  0.8000000 0.8000000
## ORDLT  0.5125626 0.2691797
## GCLT          NA 0.4500000
## ARLT   0.4500000        NA
##CSV to Desktop:


combined_matrix_lt <- cbind(as.data.frame(correlation_matrix_lt$r), as.data.frame(p_values_corrected_matrix_bh_lt))
colnames(combined_matrix_lt) <- paste0(rep(variables_lt, 2), "_", rep(c("Corr", "PValue"), each = length(variables_lt)))