incubating models

library (tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.4.0      ✔ purrr   1.0.1 
✔ tibble  3.1.8      ✔ dplyr   1.0.10
✔ tidyr   1.3.0      ✔ stringr 1.5.0 
✔ readr   2.1.3      ✔ forcats 0.5.2 
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
library(ggplot2)
library(lme4)
Loading required package: Matrix

Attaching package: 'Matrix'

The following objects are masked from 'package:tidyr':

    expand, pack, unpack
library(glmmTMB)
Warning in checkDepPackageVersion(dep_pkg = "TMB"): Package version inconsistency detected.
glmmTMB was built with TMB version 1.9.1
Current TMB version is 1.9.2
Please re-install glmmTMB from source or restore original 'TMB' package (see '?reinstalling' for more information)
library(effects)
Loading required package: carData
lattice theme set by effectsTheme()
See ?effectsTheme for details.
library(car)

Attaching package: 'car'

The following object is masked from 'package:dplyr':

    recode

The following object is masked from 'package:purrr':

    some
library(bbmle)
Loading required package: stats4

Attaching package: 'bbmle'

The following object is masked from 'package:dplyr':

    slice
library(performance)

behavior_23Feb23 <- read.csv("behavior_23Feb23.csv")

behavior <- behavior_23Feb23

behaviorprop<- behavior_23Feb23

Incubating Model

m2 <- glmer(formula = incubating~temp+day_of_experiment+infected+total_alive+(1|parent), family = "poisson", data = behavior)
Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : Model is nearly unidentifiable: very large eigenvalue
 - Rescale variables?
# Warning: Model is nearly unidentifiable: very large eigenvalue
plot(allEffects(m2))

vif(m2)
             temp day_of_experiment          infected       total_alive 
         1.008934          1.663081          1.072261          1.578918 

Model 2.1: everything except random effect

m2.1 <- glm(formula = incubating~temp+day_of_experiment+infected+total_alive, family = "poisson", data = behavior)
plot(allEffects(m2.1))

vif(m2.1)
             temp day_of_experiment          infected       total_alive 
         1.016913          1.554293          1.113577          1.429696 

Assess whether or not to keep random effect with dAIC

ICtab(m2,m2.1)
     dAIC df
m2    0.0 6 
m2.1 13.9 5 
# Keep random effect of parent colony, dAIC is lower 

Model 2.2: everything except total_alive

m2.2 <- glmer(formula = incubating~temp+day_of_experiment+infected+(1|parent), family = "poisson", data = behavior)
Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : Model is nearly unidentifiable: very large eigenvalue
 - Rescale variables?
# Warning: Model is nearly unidentifiable: very large eigenvalue
plot(allEffects(m2.2))

Model 2.3: everything except infected

m2.3 <- glmer(formula = incubating~temp+day_of_experiment+total_alive+(1|parent), family = "poisson", data = behavior)
Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : Model is nearly unidentifiable: very large eigenvalue
 - Rescale variables?
# Warning: Model is nearly unidentifiable: very large eigenvalue
plot(allEffects(m2.3))

Model 2.4: everything except day_of_experiment

m2.4 <- glmer(formula = incubating~temp+infected+total_alive+(1|parent), family = "poisson", data = behavior)
Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, :
Model failed to converge with max|grad| = 0.00256368 (tol = 0.002, component 1)
#Warning: Model failed to converge with max|grad| = 0.00256368 (tol = 0.002, component 1)
plot(allEffects(m2.4))

Check these with performance

compare_performance(m2,m2.1,m2.2,m2.3,m2.4, rank=TRUE)
Warning: Following indices with missing values are not used for ranking:
  R2_conditional, R2_marginal, ICC, R2_Nagelkerke
# Comparison of Model Performance Indices

Name |    Model |  RMSE | Sigma | Score_log | Score_spherical | AIC weights | AICc weights | BIC weights | Performance-Score
----------------------------------------------------------------------------------------------------------------------------
m2   | glmerMod | 1.632 | 1.000 |    -1.572 |           0.023 |       0.999 |        0.999 |       0.988 |            91.03%
m2.2 | glmerMod | 1.674 | 1.000 |    -1.602 |           0.023 |    7.05e-17 |     7.12e-17 |    9.18e-16 |            39.33%
m2.1 |      glm | 1.645 | 1.198 |    -1.586 |           0.023 |    9.44e-04 |     9.53e-04 |       0.012 |            30.05%
m2.4 | glmerMod | 1.691 | 1.000 |    -1.614 |           0.023 |    8.63e-24 |     8.71e-24 |    1.12e-22 |            24.78%
m2.3 | glmerMod | 1.707 | 1.000 |    -1.616 |           0.022 |    1.32e-25 |     1.33e-25 |    1.72e-24 |            14.29%
#Model 2 (the one with everything) appears to be best 

Make incubating counts into proportions, redo best model (Model 2)

incubatingprop = (behaviorprop$incubating/behaviorprop$total_alive)
behaviorprop<- cbind(behaviorprop,incubatingprop)

m2.22 <- glmer(formula = incubatingprop~temp+day_of_experiment+infected+total_alive+(1|parent), family = "binomial", data = behaviorprop)
Warning in eval(family$initialize, rho): non-integer #successes in a binomial
glm!
boundary (singular) fit: see help('isSingular')
m2.22
Generalized linear mixed model fit by maximum likelihood (Laplace
  Approximation) [glmerMod]
 Family: binomial  ( logit )
Formula: incubatingprop ~ temp + day_of_experiment + infected + total_alive +  
    (1 | parent)
   Data: behaviorprop
      AIC       BIC    logLik  deviance  df.resid 
 566.6928  597.6205 -277.3464  554.6928      1274 
Random effects:
 Groups Name        Std.Dev.
 parent (Intercept) 0       
Number of obs: 1280, groups:  parent, 6
Fixed Effects:
      (Intercept)               temp  day_of_experiment           infected  
          7.93764           -0.32277            0.06016           -1.30031  
      total_alive  
         -0.14376  
optimizer (Nelder_Mead) convergence code: 0 (OK) ; 0 optimizer warnings; 1 lme4 warnings 
plot(allEffects(m2.22))
Warning in eval(family$initialize): non-integer #successes in a binomial glm!
Warning in eval(family$initialize): non-integer #successes in a binomial glm!

Warning in eval(family$initialize): non-integer #successes in a binomial glm!

Warning in eval(family$initialize): non-integer #successes in a binomial glm!

Vector of incubating yes/no

behaviorprop$notincubating <- behaviorprop$total_alive-behaviorprop$incubating

m2.5 <- glmer(formula = cbind(incubating,notincubating)~temp+day_of_experiment+infected+total_alive+(1|parent), family = "binomial", data = behaviorprop)
summary(m2.5)
Generalized linear mixed model fit by maximum likelihood (Laplace
  Approximation) [glmerMod]
 Family: binomial  ( logit )
Formula: cbind(incubating, notincubating) ~ temp + day_of_experiment +  
    infected + total_alive + (1 | parent)
   Data: behaviorprop

     AIC      BIC   logLik deviance df.resid 
  4247.3   4278.2  -2117.6   4235.3     1274 

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-5.5903 -0.8304 -0.3939  0.7511  6.6508 

Random effects:
 Groups Name        Variance Std.Dev.
 parent (Intercept) 0.04905  0.2215  
Number of obs: 1280, groups:  parent, 6

Fixed effects:
                   Estimate Std. Error z value Pr(>|z|)    
(Intercept)        2.235389   0.388888   5.748 9.02e-09 ***
temp              -0.149077   0.004639 -32.138  < 2e-16 ***
day_of_experiment  0.030602   0.002309  13.253  < 2e-16 ***
infected          -0.769842   0.060602 -12.703  < 2e-16 ***
total_alive        0.088870   0.033422   2.659  0.00784 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Correlation of Fixed Effects:
            (Intr) temp   dy_f_x infctd
temp        -0.465                     
dy_f_xprmnt -0.477  0.009              
infected    -0.168  0.120  0.230       
total_alive -0.900  0.131  0.490  0.059
plot(allEffects(m2.5))