#Title: #Authors: Mads F. Schou #Date: 23/09/2020 #------- 1. Random regression models # 1a. Egg laying # 1b. Egg mass # 1c. Chick hathcing # 1d. Number of offspring # 1e. Number of sperm # 1f. Sperm viability #------- 2. Random regression models with year-specific slopes # 2a. Egg laying # 2b. Egg mass # 2c. Number of sperm # 2d. Sperm viability # 2e. Repeatabilitites # 2f. PSlopeVar #------- 3. Character-state models # 3a. Egg laying # 3b. Egg mass # 3c. Chick hathcing # 3d. Number of offspring # 3e. Number of sperm # 3f. Sperm viability #------- 4. Bivariate (two-trait) models # 4a. Egg laying vs Egg mass using character-state model # 4b. Egg laying vs Egg mass using random regression model # 4c. Number of sperm vs Sperm viability using character-state model # 4d. Number of sperm vs Sperm viability using random regression model ############################################################# # 1. Random regression models ############################################################# Myprior <- list( R=list(V = diag(1), nu=0.002, fix = FALSE), G=list(G1=list(V = diag(3), nu = 2.002), G2=list(V = diag(1), nu = 0.002), G3=list(V = diag(1), nu = 0.002))) #------------- 1a. Egg laying ----------------# RR.laying <- MCMCglmm(cbind(Neggs, NoEggsHalf) ~ TSIcon:Stress*dam_subpop + sire_subpop + dam_age_z, random = ~ us(1 + TSIcon:Stress):damid + enclosure + year, data = temp, family = "multinomial2", prior = Myprior, thin = 10000,burnin = 100000,nitt = 10000000, verbose = TRUE) #------------- 1b. Egg mass ----------------# RR.mass <- MCMCglmm(mass ~ TSI:Stress*dam_subpop + sire_subpop + poly(days.since.prev.eggLOG_z,2) + dam_age_z, random = ~ us(1 + TSI:Stress):damid + enclosure + year, data = temp, prior = Myprior, thin = 10000,burnin = 100000,nitt = 10000000, verbose = TRUE) #------------- 1c. Chick hatching ----------------# RR.hatching <- MCMCglmm(cbind(Nhatched, NotHatched) ~ TSIcon:Stress*dam_subpop + sire_subpop + dam_age_z, random = ~ us(1 + TSIcon:Stress):damid + enclosure + year, data = eggC, family = "multinomial2", prior = Myprior, thin = 10000,burnin = 100000,nitt = 10000000, verbose = TRUE) #------------- 1d. Number of offspring ----------------# RR.offspring <- MCMCglmm(cbind(Nchicks, NoChicksHalf) ~ TSIcon:Stress*dam_subpop + sire_subpop + dam_age_z, random = ~ us(1 + TSIcon:Stress):damid + enclosure + year, data = temp, family = "multinomial2", prior = prior.m2, thin = 10000,burnin = 100000,nitt = 10000000, verbose = TRUE) #------------- 1e. Number of sperm ----------------# Spermprior<-list(R=list(V = diag(1),nu=0.002), G=list( G1=list(V = diag(3),nu = 2.002), G2=list(V= diag(1),nu = 0.002) )) RR.spermnum <- MCMCglmm(sperm_num5 ~ TSI:Stress + poly(age,2), random= ~ us(1+TSI:Stress):name + year ,data=sperm_num, nitt=3000000, thin=30000, burnin=3000, prior=Spermprior, family = "poisson",pr = TRUE) #------------- 1f. Sperm viability ----------------# RR.spermlive <- MCMCglmm(cbind(sperm_live,sperm_notlive) ~ TSI:Stress + poly(age,2), random= ~ us(1+TSI:Stress):name + year ,data=sperm_liv, nitt=3000000, thin=30000, burnin=3000, prior=Spermprior, family = "multinomial2") ############################################################# # 2. Random regression models with year-specific slopes ############################################################# prior.m2 <- list( R=list(V = diag(1), nu=0.002, fix = FALSE), #Not fixed at one as binomial model!, inverse-Wishart prior with low beleif. 2.002 because 3 in diag! G=list(G1=list(V = diag(3), nu = 2.002), G2=list(V = diag(3), nu = 2.002), G3=list(V = diag(1), nu = 0.002), G4=list(V = diag(1), nu = 0.002))) #------------- 2a. Egg laying ----------------# RRyear.laying <- MCMCglmm(cbind(Neggs, NoEggsHalf) ~ TSIcon:Stress*dam_subpop + sire_subpop + dam_age_z, random = ~ us(1 + TSIcon:Stress):damid + us(1 + TSIcon:Stress):year_damid + enclosure + year, data = temp, family = "multinomial2", prior = prior.m2, thin = 10000,burnin = 100000,nitt = 10000000, verbose = TRUE) #------------- 2b. Egg mass ----------------# RRyear.mass <- MCMCglmm(mass ~ TSI:Stress*dam_subpop + sire_subpop + poly(days.since.prev.eggLOG_z,2) + dam_age_z, random = ~ us(1 + TSI:Stress):damid + us(1 + TSI:Stress):year_damid + enclosure + year, data = temp, prior = prior.m2, thin = 10000,burnin = 100000,nitt = 10000000, verbose = TRUE) #------------- 2c. Number of sperm ----------------# Spermprior2 <-list(R=list(V = diag(1),nu=0.002), G=list( G1=list(V = diag(3),nu = 2.002), G2=list(V = diag(3),nu = 2.002), G3=list(V= diag(1),nu = 0.002) )) RRyear.spermnum <- MCMCglmm(sperm_num5 ~ TSI:Stress + poly(age,2), random= ~ us(1+TSI:Stress):name + us(1+TSI:Stress):year_name + year ,data=sperm_num, nitt=3000000, thin=30000, burnin=3000, prior=Spermprior2, family = "poisson",pr = TRUE) #------------- 2d. Sperm viability ----------------# RRyear.spermvia <- MCMCglmm(cbind(sperm_live,sperm_notlive) ~ TSI:Stress + poly(age,2), random= ~ us(1+TSI:Stress):name + us(1+TSI:Stress):year_name + year ,data=sperm_liv, nitt=3000000, thin=30000, burnin=3000, prior=Spermprior2, family = "multinomial2") #------------- 2e. Repeatabilitites ----------------# #Example egg-laying #-- Intercept rep_int<-(RRyear.laying$VCV[,'(Intercept):(Intercept).damid'])/rowSums(RRyear.laying$VCV[,c('(Intercept):(Intercept).damid', '(Intercept):(Intercept).year_damid', 'year', 'enclosure', 'units')]) rep_int <- data.frame(t(c(posterior.mode(rep_int),HPDinterval(rep_int)))) #-- slope heat stress rep_slope<- RRyear.laying$VCV[,'TSIcon:StressHeat:TSIcon:StressHeat.damid']/rowSums(RRyear.laying$VCV[,c('TSIcon:StressHeat:TSIcon:StressHeat.damid','TSIcon:StressHeat:TSIcon:StressHeat.year_damid')]) rep_slope <- data.frame(t(c(posterior.mode(rep_slope),HPDinterval(rep_slope)))) #-- slope cold stress rep_slope_cold<- RRyear.laying$VCV[,'TSIcon:StressCold:TSIcon:StressCold.damid']/rowSums(RRyear.laying$VCV[,c('TSIcon:StressCold:TSIcon:StressCold.damid','TSIcon:StressCold:TSIcon:StressCold.year_damid')]) rep_slope_cold <- data.frame(t(c(posterior.mode(rep_slope_cold),HPDinterval(rep_slope_cold)))) #------------- 2f. PSlopeVar ----------------# #Example egg-laying PSlopeVar <- (RRyear.laying$VCV[,'TSIcon:StressHeat:TSIcon:StressHeat.damid'] * var(temp$TSIcon[temp$Stress %in% "Heat"])+ RRyear.laying$VCV[,'TSIcon:StressHeat:TSIcon:StressHeat.year_damid'] * var(temp$TSIcon[temp$Stress %in% "Heat"])+ RRyear.laying$VCV[,'TSIcon:StressCold:TSIcon:StressCold.damid'] * var(temp$TSIcon[temp$Stress %in% "Cold"])+ RRyear.laying$VCV[,'TSIcon:StressCold:TSIcon:StressCold.year_damid'] * var(temp$TSIcon[temp$Stress %in% "Cold"]))/ ( RRyear.laying$VCV[,'TSIcon:StressHeat:TSIcon:StressHeat.damid'] * var(temp$TSIcon[temp$Stress %in% "Heat"])+ RRyear.laying$VCV[,'TSIcon:StressHeat:TSIcon:StressHeat.year_damid'] * var(temp$TSIcon[temp$Stress %in% "Heat"])+ RRyear.laying$VCV[,'TSIcon:StressCold:TSIcon:StressCold.damid'] * var(temp$TSIcon[temp$Stress %in% "Cold"])+ RRyear.laying$VCV[,'TSIcon:StressCold:TSIcon:StressCold.year_damid'] * var(temp$TSIcon[temp$Stress %in% "Cold"])+ RRyear.laying$VCV[,'(Intercept):(Intercept).damid']+ RRyear.laying$VCV[,'(Intercept):(Intercept).year_damid'] + RRyear.laying$VCV[,'enclosure']+ RRyear.laying$VCV[,'year']+ RRyear.laying$VCV[,'units'] ) PSlopeVarCI <- data.frame(t(c(posterior.mode(PSlopeVar),HPDinterval(PSlopeVar)))) ############################################################# # 3. Character-state models ############################################################# prior.m2.char <- list( R=list(V = diag(3), nu=2.002, fix = FALSE), #Not fixed at one as binomial model!, inverse-Wishart prior with low beleif. 3.002 because 3 in diag! G=list(G1=list(V = diag(3), n = 2.002), G2=list(V = diag(1), n = 0.002), G3=list(V = diag(1), n = 0.002))) #------------- 3a. Egg laying ----------------# CS.laying <- MCMCglmm(cbind(Neggs, NoEggsHalf) ~ 1 + TSIcat*dam_subpop + sire_subpop + dam_age_z, random = ~ us(TSIcat):damid + enclosure + year, rcov = ~idh(TSIcat):units, data = temp, family = "multinomial2", prior = prior.m2.char, thin = 10000,burnin = 100000,nitt = 10000000, verbose = TRUE) #------------- 3b. Egg mass ----------------# CS.mass <- MCMCglmm(mass ~ 1 + TSIcat*dam_subpop + sire_subpop + poly(days.since.prev.eggLOG_z,2) + dam_age_z, random = ~ us(TSIcat):damid + enclosure + year, rcov = ~idh(TSIcat):units, data = temp, prior = prior.m2.char, thin = 10000,burnin = 100000,nitt = 10000000, verbose = TRUE) #------------- 3c. Chick hathcing ----------------# CS.hatching <- MCMCglmm(cbind(Nhatched, NotHatched) ~ 1 + TSIcat*dam_subpop + sire_subpop + dam_age_z, random = ~ us(TSIcat):damid + enclosure + year, rcov = ~idh(TSIcat):units, data = eggC, family = "multinomial2", prior = prior.m2.char, thin = 10000,burnin = 100000,nitt = 10000000, verbose = TRUE) #------------- 3d. Number of offspring ----------------# CS.offspring <- MCMCglmm(cbind(Nchicks, NoChicksHalf) ~ 1 + TSIcat*dam_subpop + sire_subpop + dam_age_z, random = ~ us(TSIcat):damid + enclosure + year, rcov = ~idh(TSIcat):units, data = temp, family = "multinomial2", prior = prior.m2.char, thin = 10000,burnin = 100000,nitt = 10000000, verbose = TRUE) #------------- 3e. Number of sperm ----------------# priorspermCS<-list(R=list(V = diag(3), nu=2.002), G=list( G1=list(V = diag(3),nu = 2.002), G2=list(V= diag(1),nu = 0.002) )) CS.spermnum <- MCMCglmm(sperm_num5 ~ TSIcat + poly(age,2), random= ~ us(TSIcat):name + year ,rcov = ~idh(TSIcat):units ,data=sperm_num, nitt=3000000, thin=30000, burnin=3000, prior=priorspermCS, family = "poisson") #------------- 3f. Sperm viability ----------------# CS.spermvia <- MCMCglmm(cbind(sperm_live,sperm_notlive) ~ TSIcat + poly(age,2), random= ~ us(TSIcat):name + year ,rcov = ~idh(TSIcat):units ,data=sperm_liv, nitt=3000000, thin=30000, burnin=3000, prior=priorspermCS, family = "multinomial2") ############################################################# # 4. Bivariate (two-trait) models ############################################################# #------------- 4a. Egg laying vs Egg mass using character-state model ----------------# prior.cross.egg.cs <- list( R=list( R1 = list(V = diag(2), nu=1.002, fix = FALSE), R2 = list(V = diag(2), nu=1.002, fix = FALSE), R3 = list(V = diag(2), nu=1.002, fix = FALSE)), G=list( G1=list(V=diag(2),nu= 1.002), G2=list(V=diag(2),nu= 1.002), G3=list(V=diag(2),nu= 1.002), G4=list(V=diag(2),nu= 1.002), G5=list(V=diag(2),nu= 1.002))) twotrait.egg.CS <- MCMCglmm(cbind(mass,cbind(Neggs, NoEggsHalf)) ~ trait-1 + dam_subpop*TSIcat*trait + trait*sire_subpop + dam_age_z*trait, random = ~ us(trait:at.level(TSIcat,"Cold")):damid + us(trait:at.level(TSIcat,"Benign")):damid + us(trait:at.level(TSIcat,"Hot")):damid + idh(trait):enclosure + idh(trait):year, rcov = ~ us(trait:at.level(TSIcat,"Cold")):units + us(trait:at.level(TSIcat,"Benign")):units + us(trait:at.level(TSIcat,"Hot")):units , data = dat, family = c("gaussian","multinomial2"), prior = prior.cross.egg.cs, thin = 2000,burnin = 100000,nitt = 5000000, verbose = TRUE, pr = TRUE) #------------- 4b. Egg laying vs Egg mass using random regression model ----------------# prior.cross.egg.RR <- list( R=list(V = diag(2), nu=1.002, fix = FALSE), G=list(G1=list(V = diag(4), nu = 3.002), G2=list(V = diag(4), nu = 3.002), G3=list(V = diag(4), nu = 3.002), G4=list(V = diag(4), nu = 3.002), G5=list(V = diag(2), nu = 1.002), G6=list(V = diag(2), nu = 1.002))) twotrait.egg.RR <- MCMCglmm(cbind(mass,cbind(Neggs, NoEggsHalf)) ~ trait-1 + dam_subpop*trait:TSIcon:Stress + sire_subpop*trait + dam_age_z*trait, random = ~ us(trait-1+trait:TSIcon:at.level(Stress, "Heat")):damid + us(trait-1+trait:TSIcon:at.level(Stress, "Cold")):damid + us(trait-1+trait:TSIcon:at.level(Stress, "Heat")):year_damid + us(trait-1+trait:TSIcon:at.level(Stress, "Cold")):year_damid + idh(trait):enclosure + idh(trait):year, rcov = ~us(trait):units, data = dat, family = c("gaussian","multinomial2"), prior = prior.cross.egg.RR, thin = 2000,burnin = 100000,nitt = 5000000, verbose = TRUE, pr = FALSE) #------------- 4c. Number of sperm vs Sperm viability using character-state model ----------------# prior.cross.sperm.cs <- list( R=list( R1 = list(V = diag(2), nu=1.002, fix = FALSE), R2 = list(V = diag(2), nu=1.002, fix = FALSE), R3 = list(V = diag(2), nu=1.002, fix = FALSE)), G=list( G1=list(V=diag(2),nu= 1.002), G2=list(V=diag(2),nu= 1.002), G3=list(V=diag(2),nu= 1.002), G4=list(V=diag(2),nu= 1.002))) twotrait.sperm.CS <- MCMCglmm(cbind(sperm_num5,cbind(sperm_live,sperm_notlive)) ~ trait-1 + trait:TSIcat + trait*poly(age,2), random = ~ us(trait:at.level(TSIcat,"Cold")):name + us(trait:at.level(TSIcat,"Benign")):name + us(trait:at.level(TSIcat,"Hot")):name + idh(trait):year, rcov = ~us(trait:at.level(TSIcat,"Cold")):units + us(trait:at.level(TSIcat,"Benign")):units + us(trait:at.level(TSIcat,"Hot")):units, data = prior.cross.sperm.cs, family = c("poisson","multinomial2"), prior = prior.cross.sperm.cs, nitt=3000000, thin=30000, burnin=3000, verbose = T, pr = F) #------------- 4d. Number of sperm vs Sperm viability using random regression model ----------------# prior.cross.sperm.RR <- list( R=list(V = diag(2), nu=1.002, fix = FALSE), G=list(G1=list(V = diag(4), nu = 3.002), G2=list(V = diag(4), nu = 3.002), G3=list(V = diag(4), nu = 3.002), G4=list(V = diag(4), nu = 3.002), G5=list(V = diag(2), nu = 1.002))) twotrait.sperm.RR <- MCMCglmm(cbind(sperm_num5,cbind(sperm_live,sperm_notlive)) ~ trait-1 + trait:TSI:Stress + trait*poly(age,2), random = ~ us(trait-1+trait:TSI:at.level(Stress, "Heat")):name + us(trait-1+trait:TSI:at.level(Stress, "Cold")):year_name +us(trait-1+trait:TSI:at.level(Stress, "Heat")):year_name + us(trait-1+trait:TSI:at.level(Stress, "Cold")):name + idh(trait):year, rcov = ~us(trait):units, data = prior.cross.sperm.RR, family = c("poisson","multinomial2"), prior = prior.cross.sperm.RR, nitt=3000000, thin=30000, burnin=3000, verbose = T, pr = F)