Date rendered: Mon Sep 30 08:49:29 2019
-What does the DHARMA error mean??? -I’d like some feedback on how to deal with betweenness data. The QQ plot is ugly. -Why is the model singular for clustering coefficient? And is this treatment effect real? Seems like means are the same. -I can also run these analyses for male-male networks and female-female networks. -What is the story that I want to tell with this data? Simply that resource distribution does not affect social networks?
The goal of this project is to compare individual- and population-level social network metrics between the two resource dispersion treatments.
NOTE (1) This data is not fully error-checked. (2) The social network metrics are calcualted from social networks that include beetles that died and moved (3MZ) during the experiment. But the analyses do not include beetles that died or 3MZ. (3) The beetle size measurements are elytra measurements done by Ruth and Patrick (work study students). In the future, we will have horn measurements as well and can calculate PCA. (4) These analyses ignore the lack of indepdence in network metrics. In the future, I need to incorporate permutations.
#clear memory
rm(list=ls())
#libraries
library(lme4)
## Loading required package: Matrix
library(car)
## Loading required package: carData
## Registered S3 methods overwritten by 'car':
## method from
## influence.merMod lme4
## cooks.distance.influence.merMod lme4
## dfbeta.influence.merMod lme4
## dfbetas.influence.merMod lme4
library(glmmTMB)
library(DHARMa)
## Registered S3 method overwritten by 'DHARMa':
## method from
## refit.glmmTMB glmmTMB
library(effects)
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
library(tidyverse)
## ── Attaching packages ──────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.2.0 ✔ purrr 0.3.2
## ✔ tibble 2.1.3 ✔ dplyr 0.8.3
## ✔ tidyr 0.8.3 ✔ stringr 1.4.0
## ✔ readr 1.3.1 ✔ forcats 0.4.0
## ── Conflicts ─────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ tidyr::expand() masks Matrix::expand()
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ✖ dplyr::recode() masks car::recode()
## ✖ purrr::some() masks car::some()
#function that excludes
`%sans%` <- Negate(`%in%`)
#Change R's defult to contrasts
options(contrasts=c("contr.sum", "contr.poly"))
#FUNCTION: check.assumptions
check.assumptions <- function(mod){
# Multiple panels
par(mfrow=c(2,2))
# Normality
hist(resid(mod))
qqnorm(resid(mod))
qqline(resid(mod))
# Linearity
# --> Plot residual versus fitted values with loess line
plot(fitted(mod), resid(mod), main = "Residual vs fitted values")
fr.fit <- loess(resid(mod)~fitted(mod))
fr.vec <- seq(min(fitted(mod)), max(fitted(mod)), length.out=100)
lines(fr.vec,predict(fr.fit,fr.vec), col="red")
# Homoscedasticity
# --> Plot the absolute value of the residuals against the fitted values
plot(fitted(mod), sqrt(abs(resid(mod))), main="Scale-location")
sl.fit <- loess(sqrt(abs(resid(mod)))~fitted(mod))
sl.vec <- seq(min(fitted(mod)), max(fitted(mod)), length.out=100)
lines(sl.vec,predict(sl.fit,sl.vec), col="red")
}
#upload individual-level social network data
IndividSN<-read.csv("socialnetworks_20190513_.csv")
#upload population-level social network data
PopSN<-read.csv("PopSN_20190513_.csv")
#upload surveys dataset
surveys_2018<-read.csv("20190422_OffMountainBeetleBase2.0.csv")
#NOTE: This is not a fully cleaned dataset.
#upload beetle measurements
elytra<-read.csv("BeetleMeasurements_20190324.csv")
#NOTE: This elytra data is not completely cleaned.
#(1) Need to check with Hannah Donald about how to measure elytra 3FM and 2MX. These are dirty scans. Ruth and Patrick measured these scans very differently. I already emailed Hannah about this (20190221) and just need to follow-up by looking at these measurements. Right now, these are wrong measurements!
#(2) Missing dorsal scan for 3NH. For now, I am using Rachel's measurement of the elytra. I do not have a pronotum measurement for 3NH. Luckily, this beetle died during Period 1. Rachel's measurement: 7.281
#create objects for ease of coding
DeadBeetles <- c("2BO", "2BX", "2H2", "2M6", "2MS", "2N6", "2N7", "2OH", "2OW", "2PK", "2PW", "2T9", "2TS", "2VB", "2VO", "2WV", "3A6", "3AB", "3AW", "3HK", "3HX", "3KA", "3LV", "3LX", "3MN", "3NH", "3ON", "3VO", "3W9", "3XM", "3XN", "2FF", "2HF", "3F3", "3KL", "3YF")
Dispersed <- c("1A_Period1", "2A_Period1", "3A_Period1", "4A_Period1", "5A_Period1", "6A_Period1", "1B_Period2", "2B_Period2", "3B_Period2", "4B_Period2", "5B_Period2", "6B_Period2")
Clumped <- c("1B_Period1", "2B_Period1", "3B_Period1", "4B_Period1", "5B_Period1", "6B_Period1", "1A_Period2", "2A_Period2", "3A_Period2", "4A_Period2", "5A_Period2", "6A_Period2")