# Evolution of sexual conflict - Variable female mating rates #### DATA AND VARIABLES #### # line. Unique code 1 to 16. # id: Unique code for female ID # gen: Generation # opp#: Each of the days that females had the opportunity to mate. 1= copulation 0= no copulation. # Females were given an opportunity to mate every day from d1 to d12 (gen 12) or from d1 to d10 (gen 30). # total: Total number of copulations carried out by the female. # totopp: Total number of opportunities given to each female. 12 for generation 12, and 10 for # generation 30 (No females died during the mating assays). # fails: Number of times that the female didn't mate. # age: female age at the start of the assay (mean ± SE = 1.48 ± 0.04, range 1-3, n = 262) #longev: female Longevity (from emergence as adults). #FACTORS: str (structure: meta vs. nometa), ss (mating system: poly vs. mono) #OTHER FACTORS: gen #COVARIATES (continuous): fel (female elytron length, proxy for body size).This was measured by same person (Miguel Lozano, who has #measured thousands and thousands of beetles thus far). #RANDOM FACTORS: selection line ID #Load libraries library(lme4) library(car) library(MASS) # Input data data = read.table("Matingfrequency.txt",header=TRUE,sep="\t") dim(data) # Longevity data: there are 4 NAs. Get rid of them here data <- subset(data, longev != "na") dim(data) names(data) head(data) str(data) # Define factors data$treat <- as.factor(data$treat) data$ss <- as.factor(data$ss) data$structure <- as.factor(data$structure) data$str <- as.factor(data$str) data$line <- as.factor(data$line) data$id <- as.factor(data$id) data$gen <- as.factor(data$gen) str(data) #Balanced dataset: table(data$str, data$ss) # Centering covariates data$cent.fel<-(data$fel-mean(data$fel,na.rm=TRUE)) hist(data$cent.fel) mean(data$cent.fel, na.rm=T) shapiro.test(data$cent.fel) data$cent.total<-(data$total-mean(data$total,na.rm=TRUE)) hist(data$cent.total) mean(data$cent.total, na.rm=T) shapiro.test(data$cent.total) data$cent.age<-(data$age-mean(data$age,na.rm=TRUE)) hist(data$cent.age) mean(data$cent.age, na.rm=T) shapiro.test(data$cent.age) #### LONGEVITY LMM with Random intercept and Random slope#### # Model estimating random intercept, random slopes, and the covariance between slope and intercept. M1<-lmer(longev~ss+str+gen+cent.age+cent.total+cent.fel+ ss:str+ss:cent.fel+str:cent.fel+ (1+cent.fel|line), REML=FALSE, data = data) summary(M1) Anova(M1, type=3) #Chi square. Wald test. Type III #Final model with REML M1<-lmer(longev~ss+str+gen+cent.age+cent.total+cent.fel+ ss:str+ss:cent.fel+str:cent.fel+ (1+cent.fel|line), REML=TRUE, data = data) summary(M1) # Diagnostic plots: par(mfrow = c(2, 2)) plot(M1) diagnostics.plot(M1) #Checking basic assumptions