###Code for Kendall et al. (2018): Pollinator size and its consequences: Robust estimates of body size in pollinating insects #Data preparation #libraries library(ape) library(bayesplot) library(broom) library(brms) library(cvTools) library(caret) library(grid) library(gridExtra) library(lattice) library(lme4) library(MCMCglmm) library(ModelMetrics) library(MuMIn) library(rstan) library(parallel) library(phytools) library(plyr) library(dplyr) library(stargazer) #dataframes set.seed(123) poll_all <- read.csv(file="data/pollimetry_10172018.csv",header=T) #split to bees and hoverflies poll_all_split=split(poll_all,poll_all$Superfamily) bee_all=poll_all_split[[1]] hov_all=poll_all_split[[2]] #JUST AUSTRALIA bee_country=split(bee_all,bee_all$Country) bee_australia=bee_country$Australia bee_all$Family=relevel(bee_all$Family,ref="Apidae") bee_all$Region=relevel(bee_all$Region,ref="Europe") bee_all$Species=droplevels(bee_all$Species) bee_all=droplevels(bee_all) hov_all=droplevels(hov_all) #remove species not in phylogeny bee_all=bee_all[!bee_all$Species=="Flavipanurgus_venustus",] #Genus is not in phylogeny bee_all=bee_all[!bee_all$Species=="Protomeliturga_turnerea",] #Genus not in phylogeny bee_all=bee_all[!bee_all$Species=="Tetrapedia_diversipes",] #Genus not in phylogeny #for complete nestedness - remove species from introduced range bee_all=bee_all[!(bee_all$Species=="Andrena_wilkella" & bee_all$Region=="NorthAmerica"),] #Hurd (1979) presumes it is an #introduction in North America) bee_all=bee_all[!(bee_all$Species=="Halictus_rubicundus" & bee_all$Region=="NorthAmerica"),] bee_all=bee_all[!(bee_all$Species=="Lasioglossum_leucozonium" & bee_all$Region=="NorthAmerica"),] bee_all=bee_all[!(bee_all$Species=="Anthidium_manicatum" & bee_all$Region=="NorthAmerica"),] bee_all=bee_all[!(bee_all$Species=="Apis_mellifera" & bee_all$Region=="Australasia"),] ##PHYLO Dataframe for calculating phylogenetic signal bee_phylo=aggregate(Latitude~Family+Subfamily+ Genus+Species,bee_all,median) bee_phylo$Longitude=as.numeric(unlist(aggregate(Longitude~Family+Subfamily+Genus+Species,bee_all,median)[5])) bee_phylo$Spec.wgt=as.numeric(unlist(aggregate(Spec.wgt~Family+Subfamily+ Genus+Species,bee_all,mean)[5])) bee_phylo$Wgt.SD=as.numeric(unlist(aggregate(log(Spec.wgt)~Family+Subfamily+ Genus+Species,bee_all,sd)[5])) bee_phylo$IT=as.numeric(unlist(aggregate(IT~Family+Subfamily+ Genus+Species,bee_all,mean)[5])) bee_phylo$IT.SD=as.numeric(unlist(aggregate(log(IT)~Family+Subfamily+ Genus+Species,bee_all,sd)[5])) rownames(bee_phylo)=droplevels(bee_phylo$Species) ##LOAD TREE bee.trees=read.tree(file="data/hedtke_genera_tree.txt") ##Use tree 1 (376 genera) #Genera-level phylogney bee.mcmc=bee.trees[[1]] range(bee.mcmc$edge.length) ###root with apoid wasp outgroup bee.mcmc=root(bee.mcmc,outgroup="Tachysphex") range(bee.mcmc$edge.length) bee.mcmc=as.phylo(bee.mcmc) ##Make ultrametric bee.mcmc=chronos(bee.mcmc) bee.tree=drop.tip(bee.mcmc, setdiff(bee.mcmc$tip.label,unique(bee_all$Genus))) is.ultrametric(bee.tree) ## Will's suggestion bee.tree$tip.label<-paste(bee.tree$tip.label,"_dum",sep="") for(i in 1:length(species$Genus_species)){ bee.tree<-add.species.to.genus(bee.tree,species$Genus_species[i], where="root") } ## prune out these same taxa ii<-grep("dum",bee.tree$tip.label) bee.tree<-drop.tip(bee.tree,bee.tree$tip.label[ii]) plot(bee.tree) ##ADD missing species setdiff(species$Genus_species,bee.tree$tip.label) bee.tree<-add.species.to.genus(bee.tree,"Lasioglossum_paradmirandum", where="root") bee.tree<-add.species.to.genus(bee.tree,"Lasioglossum_admirandum", where="root") #Remove node labels bee.tree$node.label=NULL ##Intraspecific variance data frames bee_species=split(bee_all,bee_all$Species) hov_species=split(hov_all,hov_all$Species) ##Cane function Cane=function(IT){exp(0.6453 + 2.4691*log(IT))} ##FOR intraspecific species analyses bee_species=split(bee_all,bee_all$Species) #BRMS Hamiltonian monte carlo diagnostic checks - FROM MICHAEL BETANCOURT # Check transitions that ended with a divergence check_div <- function(fit) { sampler_params <- get_sampler_params(fit, inc_warmup=FALSE) divergent <- do.call(rbind, sampler_params)[,'divergent__'] n = sum(divergent) N = length(divergent) print(sprintf('%s of %s iterations ended with a divergence (%s%%)', n, N, 100 * n / N)) if (n > 0) print(' Try running with larger adapt_delta to remove the divergences') } # Check transitions that ended prematurely due to maximum tree depth limit check_treedepth <- function(fit, max_depth = 10) { sampler_params <- get_sampler_params(fit, inc_warmup=FALSE) treedepths <- do.call(rbind, sampler_params)[,'treedepth__'] n = length(treedepths[sapply(treedepths, function(x) x == max_depth)]) N = length(treedepths) print(sprintf('%s of %s iterations saturated the maximum tree depth of %s (%s%%)', n, N, max_depth, 100 * n / N)) if (n > 0) print(' Run again with max_depth set to a larger value to avoid saturation') } # Checks the energy Bayesian fraction of missing information (E-BFMI) check_energy <- function(fit) { sampler_params <- get_sampler_params(fit, inc_warmup=FALSE) no_warning <- TRUE for (n in 1:length(sampler_params)) { energies = sampler_params[n][[1]][,'energy__'] numer = sum(diff(energies)**2) / length(energies) denom = var(energies) if (numer / denom < 0.2) { print(sprintf('Chain %s: E-BFMI = %s', n, numer / denom)) no_warning <- FALSE } } if (no_warning) print('E-BFMI indicated no pathological behavior') else print(' E-BFMI below 0.2 indicates you may need to reparameterize your model') } # Checks the effective sample size per iteration check_n_eff <- function(fit) { fit_summary <- summary(fit, probs = c(0.5))$summary N <- dim(fit_summary)[[1]] iter <- dim(extract(fit)[[1]])[[1]] no_warning <- TRUE for (n in 1:N) { ratio <- fit_summary[,5][n] / iter if (ratio < 0.001) { print(sprintf('n_eff / iter for parameter %s is %s!', rownames(fit_summary)[n], ratio)) no_warning <- FALSE } } if (no_warning) print('n_eff / iter looks reasonable for all parameters') else print(' n_eff / iter below 0.001 indicates that the effective sample size has likely been overestimated') } # Checks the potential scale reduction factors check_rhat <- function(fit) { fit_summary <- summary(fit, probs = c(0.5))$summary N <- dim(fit_summary)[[1]] no_warning <- TRUE for (n in 1:N) { rhat <- fit_summary[,6][n] if (rhat > 1.1 || is.infinite(rhat) || is.nan(rhat)) { print(sprintf('Rhat for parameter %s is %s!', rownames(fit_summary)[n], rhat)) no_warning <- FALSE } } if (no_warning) print('Rhat looks reasonable for all parameters') else print(' Rhat above 1.1 indicates that the chains very likely have not mixed') } check_all_diagnostics <- function(fit) { check_n_eff(fit) check_rhat(fit) check_div(fit) check_treedepth(fit) check_energy(fit) } # Returns parameter arrays separated into divergent and non-divergent transitions partition_div <- function(fit) { nom_params <- extract(fit, permuted=FALSE) n_chains <- dim(nom_params)[2] params <- as.data.frame(do.call(rbind, lapply(1:n_chains, function(n) nom_params[,n,]))) sampler_params <- get_sampler_params(fit, inc_warmup=FALSE) divergent <- do.call(rbind, sampler_params)[,'divergent__'] params$divergent <- divergent div_params <- params[params$divergent == 1,] nondiv_params <- params[params$divergent == 0,] return(list(div_params, nondiv_params)) } #Bee priors #without random effects bprior <- prior(normal(0,0.4), class = b) + prior(normal(1,1),class=b,coef="logIT") + prior(normal(0,1), class = Intercept) + prior(normal(0,0.4), class = sigma) #with random effects bprior1 <- prior(normal(0,0.4), class = b) + prior(normal(1,1),class=b,coef="logIT") + prior(normal(0,1), class = Intercept) + prior(normal(0,0.4), class = sd) + prior(normal(0,0.4), class = sigma) #Hoverfly priors #without random effects hprior <- prior(normal(0,0.4), class = b) + prior(normal(1,1),class=b,coef="logIT") + prior(normal(0,1), class = Intercept) + prior(normal(0,0.4), class = sigma) #With random effects hprior1 <- prior(normal(0,0.4), class = b) + prior(normal(1,1),class=b,coef="logIT") + prior(normal(0,1), class = Intercept) + prior(normal(0,0.4), class = sd) + prior(normal(0,0.4), class = sigma) ##BRMS bee models and manual selection #Test OLS models w. biogeography #ITD ONLY MODEL bee<- brm(log(Spec.wgt)~log(IT), data = bee_all, cores=4, family = gaussian(),prior=bprior,control=list(adapt_delta=0.99,max_treedepth=15)) #ITD + RE MODEL bee1<- brm(log(Spec.wgt)~log(IT)+(1|Region/Species), data = bee_all, cores=4, family = gaussian(),prior=bprior1,control=list(adapt_delta=0.99,max_treedepth=15)) ##FULL MODEL bee2<- brm(log(Spec.wgt) ~ Sex + Family + log(IT) + (1 | Region/Species) + Family:log(IT) + Sex:log(IT), data = bee_all, cores=4, family = gaussian(),prior=bprior1,control=list(adapt_delta=0.99,max_treedepth=15)) #Reduced models bee3<- brm(log(Spec.wgt) ~ Sex + Family + log(IT) + (1 | Region/Species) + Family:log(IT), data = bee_all, cores=4, family = gaussian(),prior=bprior1,control=list(adapt_delta=0.99,max_treedepth=15)) bee4<- brm(log(Spec.wgt) ~ Sex + Family + log(IT) + (1 | Region/Species) + Sex:log(IT), data = bee_all, cores=4, family = gaussian(),prior=bprior1,control=list(adapt_delta=0.99,max_treedepth=15)) bee5<- brm(log(Spec.wgt) ~ Sex + Family + log(IT) + (1 | Region/Species), data = bee_all, cores=4, family = gaussian(),prior=bprior1,control=list(adapt_delta=0.99,max_treedepth=15)) bee6<- brm(log(Spec.wgt) ~ Family + log(IT) + (1 | Region/Species) + Family:log(IT), data = bee_all, cores=4, family = gaussian(),prior=bprior1,control=list(adapt_delta=0.99,max_treedepth=15)) #good bee7<- brm(log(Spec.wgt) ~ Family + log(IT) + (1 | Region/Species), data = bee_all, cores=4, family = gaussian(),prior=bprior1,control=list(adapt_delta=0.99,max_treedepth=15)) bee8<- brm(log(Spec.wgt) ~ Sex * log(IT) + (1 | Region/Species), data = bee_all, cores=4, family = gaussian(),prior=bprior1,control=list(adapt_delta=0.99,max_treedepth=15)) bee9<- brm(log(Spec.wgt) ~ Sex + log(IT) + (1 | Region/Species), data = bee_all, cores=4, family = gaussian(),prior=bprior1,control=list(adapt_delta=0.99,max_treedepth=15)) #BEE PHYLO models ##Nested phylogenetic co-variance matrix inv.phylo <- MCMCglmm::inverseA(bee.tree, nodes = "TIPS", scale = TRUE) A <- solve(inv.phylo$Ainv) rownames(A) <- rownames(inv.phylo$Ainv) isSymmetric(A, check.attributes = FALSE) B=A bee_all$Rspecies=paste(bee_all$Region,bee_all$Species,sep="_") region_species=unique(bee_all[order(match(bee_all$Species, bee.tree$tip.label)), ]$Rspecies) rownames(B)=unique(region_species) isSymmetric(B,check.attributes=FALSE) ##Phylogenetic bee models bee_phy1<- brm(log(Spec.wgt) ~ Sex + log(IT) + (1 | Region/Species) + Sex:log(IT), data = bee_all, cores=4, family = gaussian(),cov_ranef = list("Region:Species" = B), prior=bprior1,control = list(adapt_delta = 0.99,max_treedepth=15)) #good bee_phy2<- brm(log(Spec.wgt) ~ Sex + log(IT) + (1 | Region/Species), data = bee_all,prior=bprior1, cores=4, family = gaussian(),cov_ranef = list("Region:Species" = B), control = list(adapt_delta = 0.99,max_treedepth=15)) #good bee_phy3<- brm(log(Spec.wgt) ~ log(IT) + (1 | Region/Species), data = bee_all,prior=bprior1, cores=4, family = gaussian(),cov_ranef = list("Region:Species" = B), control = list(adapt_delta = 0.99,max_treedepth=15)) ##MODEL CHECKS check_all_diagnostics(bee$fit) plot(bee) np_bee <- nuts_params(bee) mcmc_nuts_energy(np_bee,binwidth=1) bee1 check_all_diagnostics(bee1$fit) plot(bee1) np_bee1 <- nuts_params(bee1) mcmc_nuts_energy(np_bee1,binwidth=10) bee2 check_all_diagnostics(bee2$fit) plot(bee2) np_bee2 <- nuts_params(bee2) mcmc_nuts_energy(np_bee2,binwidth=10) bee3 check_all_diagnostics(bee3$fit) plot(bee3) np_bee3 <- nuts_params(bee3) mcmc_nuts_energy(np_bee3,binwidth=10) bee4 check_all_diagnostics(bee4$fit) plot(bee4) np_bee4 <- nuts_params(bee4) mcmc_nuts_energy(np_bee4,binwidth=10) bee5 check_all_diagnostics(bee5$fit) plot(bee5) np_bee5 <- nuts_params(bee5) mcmc_nuts_energy(np_bee5,binwidth=10) bee6 check_all_diagnostics(bee6$fit) plot(bee6) np_bee6 <- nuts_params(bee6) mcmc_nuts_energy(np_bee6,binwidth=10) bee7 check_all_diagnostics(bee7$fit) plot(bee7) np_bee7 <- nuts_params(bee7) mcmc_nuts_energy(np_bee7,binwidth=10) bee8 check_all_diagnostics(bee8$fit) plot(bee8) np_bee8 <- nuts_params(bee8) mcmc_nuts_energy(np_bee8,binwidth=10) bee9 check_all_diagnostics(bee9$fit) plot(bee9) np_bee9 <- nuts_params(bee9) mcmc_nuts_energy(np_bee9,binwidth=10) bee_phy1 check_all_diagnostics(bee_phy1$fit) plot(bee_phy1) np_bee_phy1<- nuts_params(bee_phy1) mcmc_nuts_energy(np_bee_phy1,binwidth=10) bee_phy2 check_all_diagnostics(bee_phy2$fit) plot(bee_phy2) np_bee_phy2<- nuts_params(bee_phy2) mcmc_nuts_energy(np_bee_phy2,binwidth=10) bee_phy3 check_all_diagnostics(bee_phy3$fit) plot(bee_phy3) np_bee_p3<- nuts_params(bee_phy3) mcmc_nuts_energy(np_bee_phy3,binwidth=10) ``` ```{r bee WAIC loo-ic} options(loo.cores = 4) bee_list=list(bee,bee1,bee2,bee3,bee4,bee5,bee6,bee7,bee8,bee9,bee_phy1,bee_phy2,bee_phy3) #Bayesian R2 bee_R2=lapply(bee_list,function (x) bayes_R2(x)) bee_R2 <- do.call("rbind", bee_R2) ##WAIC bee_waic=lapply(bee_list,function (x) waic(x)) #poor diagnostics ##LOO-IC bee_loo=lapply(bee_list,function (x) loo(x)) #poor diagnostics ##K-FOLD bee_kfold=lapply(bee_list,function (x) kfold(x,cores=4)) ##RMSE bee_RMSE=lapply(bee_list,function (x) rmse(exp(predict(x)[,1]),bee_all$Spec.wgt)) ##Hoverfly model selection #basic IT hov<- brm(log(Spec.wgt)~log(IT), data = hov_all, cores=4, family = gaussian(),prior=hprior, control = list(adapt_delta = 0.99)) #IT + Sex + RE hov1<- brm(log(Spec.wgt) ~ Sex + log(IT) + (1 | Region/Species), data = hov_all, cores=4, family = gaussian(),prior=hprior1, control = list(adapt_delta = 0.99)) #good # Sex * IT hov2<- brm(log(Spec.wgt) ~ Sex + log(IT) + (1 | Region/Species) + Sex:log(IT), data = hov_all, cores=4, family = gaussian(),prior=hprior1, control = list(adapt_delta = 0.99,max_treedepth=15)) #3 divergent - gone with increased treedepth #IT + Sex + Subfamily hov3<- brm(log(Spec.wgt) ~ Sex + Subfamily + log(IT) + (1 | Region/Species), data = hov_all, cores=4, family = gaussian(),prior=hprior1, control = list(adapt_delta = 0.999)) #good #IT * Subf + Sex hov4<- brm(log(Spec.wgt) ~ Sex + Subfamily + log(IT) + (1 | Region/Species) + Subfamily:log(IT), data = hov_all, cores=4, family = gaussian(),prior=hprior1, control = list(adapt_delta = 0.99,max_treedepth=15)) #good #IT * Sex + Subf hov5<- brm(log(Spec.wgt) ~ Sex + Subfamily + log(IT) + (1 | Region/Species) + Sex:log(IT), data = hov_all, cores=4, family = gaussian(),prior=hprior1, control = list(adapt_delta = 0.99,max_treedepth=15)) #4 divergent transitions - gone with increased treedepth ##FULL MODEL IT* SEX + IT * SUBF hov6<- brm(log(Spec.wgt) ~ Sex + Subfamily + log(IT) + (1 | Region/Species) + Sex:log(IT) + Subfamily:log(IT), data = hov_all, cores=4, family = gaussian(),prior=hprior1, control = list(adapt_delta = 0.99)) #good hov7<- brm(log(Spec.wgt) ~ log(IT) + (1 | Region/Species), data = hov_all, cores=4, family = gaussian(),prior=hprior1, control = list(adapt_delta = 0.999,max_treedepth=20)) #5 divergent transitions, 1 with treedepth 15 and 20, #reduced stepsize (0.1) made it worse, gone with delta 0.999 #SUBF * IT hov8<- brm(log(Spec.wgt) ~ Subfamily + log(IT) + (1 | Region/Species) + Subfamily:log(IT), data = hov_all, cores=4, family = gaussian(),prior=hprior1, control = list(adapt_delta = 0.99,max_treedepth=15)) #1 divergent transition - gone with increased treedepth 15 #SUBF + IT hov9<- brm(log(Spec.wgt) ~ Subfamily + log(IT) + (1 | Region/Species), data = hov_all, cores=4, family = gaussian(),prior=hprior1, control = list(adapt_delta = 0.999,max_treedepth=20)) #4 divergent transitions, increaed delta 0.999, treedepth 20 - fixed #hoverfly model checks hov check_all_diagnostics(hov$fit) plot(hov) np_hov <- nuts_params(hov) mcmc_nuts_energy(np_hov,binwidth=1) hov1 check_all_diagnostics(hov1$fit) plot(hov1) np_hov1 <- nuts_params(hov1) mcmc_nuts_energy(np_hov1,binwidth=1) hov2 check_all_diagnostics(hov2$fit) plot(hov2) np_hov2 <- nuts_params(hov2) mcmc_nuts_energy(np_hov2,binwidth=1) hov3 check_all_diagnostics(hov3$fit) plot(hov3) np_hov3 <- nuts_params(hov3) mcmc_nuts_energy(np_hov3,binwidth=1) hov4 check_all_diagnostics(hov4$fit) plot(hov4) np_hov4 <- nuts_params(hov4) mcmc_nuts_energy(np_hov4,binwidth=1) hov5 check_all_diagnostics(hov5$fit) plot(hov5) np_hov5 <- nuts_params(hov5) mcmc_nuts_energy(np_hov5,binwidth=1) hov6 check_all_diagnostics(hov6$fit) plot(hov6) np_hov6 <- nuts_params(hov6) mcmc_nuts_energy(np_hov6,binwidth=1) hov7 check_all_diagnostics(hov7$fit) plot(hov7) np_hov7 <- nuts_params(hov7) mcmc_nuts_energy(np_hov7,binwidth=1) hov8 check_all_diagnostics(hov8$fit) plot(hov8) np_hov8 <- nuts_params(hov8) mcmc_nuts_energy(np_hov8,binwidth=1) hov9 check_all_diagnostics(hov9$fit) plot(hov9) np_hov9 <- nuts_params(hov9) mcmc_nuts_energy(np_hov9,binwidth=1) hov check_all_diagnostics(hov$fit) plot(hov) np_hov <- nuts_params(hov) mcmc_nuts_energy(np_hov,binwidth=1) ##ADD ICs to hoverfly models hov_list=list(hov,hov1,hov2,hov3,hov4,hov5,hov6,hov7,hov8,hov9) #Bayesian R2 hov_R2=lapply(hov_list,function (x) bayes_R2(x)) #WAIC hov_waic=lapply(hov_list,function(x) waic(x)) #LOO-IC hov_loo=lapply(hov_list,function(x) loo(x)) #K-FOLD hov_kfold=lapply(hov_list,function(x) kfold(x,cores=4)) #RMSE hov_RMSE=lapply(hov_list,function (x) rmse(exp(predict(x)[,1]),hov_all$Spec.wgt)) ##Bee phylogenetic signal #Weight WGT=as.data.frame(bee_phylo[,c("Spec.wgt")]) WGTSE=as.data.frame(bee_phylo[,c("Spec.wgt")]) WGT=log(WGT) rownames(WGT)=rownames(bee_phylo) WGT<-as.matrix((WGT))[,1] phylosig(tree=bee.tree,x=WGT,method="lambda",test=TRUE) ##Intraspecific predictions and variation ##Body size predictions - just females ```{r} bee_top10=rbind(bee_species$Homalictus_urbanus[bee_species$Homalictus_urbanus$Sex == "Female",], bee_species$Lasioglossum_pauxillum[bee_species$Lasioglossum_pauxillum$Sex == "Female",], bee_species$Bombus_terrestris[bee_species$Bombus_terrestris$Sex == "Female",], bee_species$Andrena_flavipes[bee_species$Andrena_flavipes$Sex == "Female",], bee_species$Lasioglossum_lanarium[bee_species$Lasioglossum_lanarium$Sex == "Female",], bee_species$Trigona_spinipes[bee_species$Trigona_spinipes$Sex == "Female",], bee_species$Lasioglossum_glabriusculum[bee_species$Lasioglossum_glabriusculum$Sex == "Female",], bee_species$Bombus_lapidarius[bee_species$Bombus_lapidarius$Sex == "Female",], bee_species$Andrena_nigroaenea[bee_species$Andrena_nigroaenea$Sex == "Female",], bee_species$Bombus_impatiens[bee_species$Bombus_impatiens$Sex == "Male",]) HU_lm=lm(log(Spec.wgt)~log(IT),data=bee_species$Homalictus_urbanus[bee_species$Homalictus_urbanus$Sex == "Female",]) LP_lm=lm(log(Spec.wgt)~log(IT),data=bee_species$Lasioglossum_pauxillum[bee_species$Lasioglossum_pauxillum$Sex == "Female",]) BT_lm=lm(log(Spec.wgt)~log(IT),bee_species$Bombus_terrestris[bee_species$Bombus_terrestris$Sex == "Female",]) AF_lm=lm(log(Spec.wgt)~log(IT),bee_species$Andrena_flavipes[bee_species$Andrena_flavipes$Sex == "Female",]) LL_lm=lm(log(Spec.wgt)~log(IT),bee_species$Lasioglossum_lanarium[bee_species$Lasioglossum_lanarium$Sex == "Female",]) TS_lm=lm(log(Spec.wgt)~log(IT),bee_species$Trigona_spinipes[bee_species$Trigona_spinipes$Sex == "Female",]) LG_lm=lm(log(Spec.wgt)~log(IT),bee_species$Lasioglossum_glabriusculum[bee_species$Lasioglossum_glabriusculum$Sex == "Female",]) BL_lm=lm(log(Spec.wgt)~log(IT),bee_species$Bombus_lapidarius[bee_species$Bombus_lapidarius$Sex == "Female",]) AN_lm=lm(log(Spec.wgt)~log(IT),bee_species$Andrena_nigroaenea[bee_species$Andrena_nigroaenea$Sex == "Female",]) BI_lm=lm(log(Spec.wgt)~log(IT),bee_species$Bombus_impatiens[bee_species$Bombus_impatiens$Sex == "Male",]) summary(HU_lm) summary(LP_lm) summary(BL_lm) summary(AF_lm) summary(LL_lm) summary(TS_lm) summary(LG_lm) summary(BL_lm) summary(AN_lm) summary(BI_lm) #HOVERFLIES hov_top5=rbind(hov_species$Helophilus_parallelus[hov_species$Helophilus_parallelus$Sex == "Female",], hov_species$Sphaerophoria_macrogaster[hov_species$Sphaerophoria_macrogaster$Sex == "Female",], hov_species$Episyrphus_balteatus[hov_species$Episyrphus_balteatus$Sex == "Female",], hov_species$Melanostoma_scalare[hov_species$Melanostoma_scalare$Sex == "Female",], hov_species$Austrosyphus_aussp1[hov_species$Austrosyphus_aussp1$Sex == "Female",]) HP_lm=lm(log(Spec.wgt)~log(IT),data=hov_species$Helophilus_parallelus[hov_species$Helophilus_parallelus$Sex == "Female",]) SM_lm=lm(log(Spec.wgt)~log(IT),data=hov_species$Sphaerophoria_macrogaster[hov_species$Sphaerophoria_macrogaster$Sex == "Female",]) EB_lm=lm(log(Spec.wgt)~log(IT),hov_species$Episyrphus_balteatus[hov_species$Episyrphus_balteatus$Sex == "Female",]) MS_lm=lm(log(Spec.wgt)~log(IT),hov_species$Melanostoma_scalare[hov_species$Melanostoma_scalare$Sex == "Female",]) AA_lm=lm(log(Spec.wgt)~log(IT),hov_species$Austrosyphus_aussp1[hov_species$Austrosyphus_aussp1$Sex == "Female",]) summary(HP_lm) summary(SM_lm) summary(EB_lm) summary(MS_lm) summary(AA_lm) ##Bee intraspecific variation with sample size set.seed(123) ##ONE## #Homalictus urbanus #IT HU_IT = c() for(i in 1:length(bee_species$Homalictus_urbanus[bee_species$Homalictus_urbanus$Sex == "Female",]$IT)){ subset1 = sample(bee_species$Homalictus_urbanus[bee_species$Homalictus_urbanus$Sex == "Female",]$IT, i) HU_IT[i] = mean(subset1) } #Specimen weight HU_WT = c() for(i in 1:length(bee_species$Homalictus_urbanus[bee_species$Homalictus_urbanus$Sex == "Female",]$Spec.wgt)){ subset1 = sample(bee_species$Homalictus_urbanus[bee_species$Homalictus_urbanus$Sex == "Female",]$Spec.wgt, i) HU_WT[i] = mean(subset1) } ##TWO## #Lasioglossum_pauxillum #IT LP_IT = c() for(i in 1:length(bee_species$Lasioglossum_pauxillum[bee_species$Lasioglossum_pauxillum$Sex == "Female",]$IT)){ subset1 = sample(bee_species$Lasioglossum_pauxillum[bee_species$Lasioglossum_pauxillum$Sex == "Female",]$IT, i) LP_IT[i] = mean(subset1) } #Specimen weight LP_WT = c() for(i in 1:length(bee_species$Lasioglossum_pauxillum[bee_species$Lasioglossum_pauxillum$Sex == "Female",]$Spec.wgt)){ subset1 = sample(bee_species$Lasioglossum_pauxillum[bee_species$Lasioglossum_pauxillum$Sex == "Female",]$Spec.wgt, i) LP_WT[i] = mean(subset1) } ##THREE #Bombus_lucorum BT_IT = c() for(i in 1:length(bee_species$Bombus_terrestris[bee_species$Bombus_terrestris$Sex == "Female",]$IT)){ subset1 = sample(bee_species$Bombus_terrestris[bee_species$Bombus_terrestris$Sex == "Female",]$IT, i) BT_IT[i] = mean(subset1) } #Specimen weight BT_WT = c() for(i in 1:length(bee_species$Bombus_terrestris[bee_species$Bombus_terrestris$Sex == "Female",]$Spec.wgt)){ subset1 = sample(bee_species$Bombus_terrestris[bee_species$Bombus_terrestris$Sex == "Female",]$Spec.wgt, i) BT_WT[i] = mean(subset1) } ##FOUR## #Andrena_flavipes #IT AF_IT = c() for(i in 1:length(bee_species$Andrena_flavipes[bee_species$Andrena_flavipes$Sex == "Female",]$IT)){ subset1 = sample(bee_species$Andrena_flavipes[bee_species$Andrena_flavipes$Sex == "Female",]$IT, i) AF_IT[i] = mean(subset1) } #Specimen weight AF_WT = c() for(i in 1:length(bee_species$Andrena_flavipes[bee_species$Andrena_flavipes$Sex == "Female",]$Spec.wgt)){ subset1 = sample(bee_species$Andrena_flavipes[bee_species$Andrena_flavipes$Sex == "Female",]$Spec.wgt, i) AF_WT[i] = mean(subset1) } ##FIVE## #Lasioglossum_lanarium LL_IT = c() for(i in 1:length(bee_species$Lasioglossum_lanarium[bee_species$Lasioglossum_lanarium$Sex == "Female",]$IT)){ subset1 = sample(bee_species$Lasioglossum_lanarium[bee_species$Lasioglossum_lanarium$Sex == "Female",]$IT, i) LL_IT[i] = mean(subset1) } #Specimen weight LL_WT = c() for(i in 1:length(bee_species$Lasioglossum_lanarium[bee_species$Lasioglossum_lanarium$Sex == "Female",]$Spec.wgt)){ subset1 = sample(bee_species$Lasioglossum_lanarium[bee_species$Lasioglossum_lanarium$Sex == "Female",]$Spec.wgt, i) LL_WT[i] = mean(subset1) } ##Six## #Andrena_nigroaenea AN_IT = c() for(i in 1:length(bee_species$Andrena_nigroaenea[bee_species$Andrena_nigroaenea$Sex == "Female",]$IT)){ subset1 = sample(bee_species$Andrena_nigroaenea[bee_species$Andrena_nigroaenea$Sex == "Female",]$IT, i) AN_IT[i] = mean(subset1) } #Specimen weight AN_WT = c() for(i in 1:length(bee_species$Andrena_nigroaenea[bee_species$Andrena_nigroaenea$Sex == "Female",]$Spec.wgt)){ subset1 = sample(bee_species$Andrena_nigroaenea[bee_species$Andrena_nigroaenea$Sex == "Female",]$Spec.wgt, i) AN_WT[i] = mean(subset1) } ##Seven## #Bombus_impatiens BI_IT = c() for(i in 1:length(bee_species$Bombus_impatiens[bee_species$Bombus_impatiens$Sex == "Male",]$IT)){ subset1 = sample(bee_species$Bombus_impatiens[bee_species$Bombus_impatiens$Sex == "Male",]$IT, i) BI_IT[i] = mean(subset1) } #Specimen weight BI_WT = c() for(i in 1:length(bee_species$Bombus_impatiens[bee_species$Bombus_impatiens$Sex == "Male",]$Spec.wgt)){ subset1 = sample(bee_species$Bombus_impatiens[bee_species$Bombus_impatiens$Sex == "Male",]$Spec.wgt, i) BI_WT[i] = mean(subset1) } ##Eight## #Bombus_lapidarius BL_IT = c() for(i in 1:length(bee_species$Bombus_lapidarius[bee_species$Bombus_lapidarius$Sex == "Female",]$IT)){ subset1 = sample(bee_species$Bombus_lapidarius[bee_species$Bombus_lapidarius$Sex == "Female",]$IT, i) BL_IT[i] = mean(subset1) } #Specimen weight BL_WT = c() for(i in 1:length(bee_species$Bombus_lapidarius[bee_species$Bombus_lapidarius$Sex == "Female",]$Spec.wgt)){ subset1 = sample(bee_species$Bombus_lapidarius[bee_species$Bombus_lapidarius$Sex == "Female",]$Spec.wgt, i) BL_WT[i] = mean(subset1) } ##Nine## #Lasioglossum_glabriusculum LG_IT = c() for(i in 1:length(bee_species$Lasioglossum_glabriusculum[bee_species$Lasioglossum_glabriusculum$Sex == "Female",]$IT)){ subset1 = sample(bee_species$Lasioglossum_glabriusculum[bee_species$Lasioglossum_glabriusculum$Sex == "Female",]$IT, i) LG_IT[i] = mean(subset1) } #Specimen weight LG_WT = c() for(i in 1:length(bee_species$Lasioglossum_glabriusculum[bee_species$Lasioglossum_glabriusculum$Sex == "Female",]$Spec.wgt)){ subset1 = sample(bee_species$Lasioglossum_glabriusculum[bee_species$Lasioglossum_glabriusculum$Sex == "Female",]$Spec.wgt, i) LG_WT[i] = mean(subset1) } ##Ten## #Trigona_spinipes TS_IT = c() for(i in 1:length(bee_species$Trigona_spinipes[bee_species$Trigona_spinipes$Sex == "Female",]$IT)){ subset1 = sample(bee_species$Trigona_spinipes[bee_species$Trigona_spinipes$Sex == "Female",]$IT, i) TS_IT[i] = mean(subset1) } #Specimen weight TS_WT = c() for(i in 1:length(bee_species$Trigona_spinipes[bee_species$Trigona_spinipes$Sex == "Female",]$Spec.wgt)){ subset1 = sample(bee_species$Trigona_spinipes[bee_species$Trigona_spinipes$Sex == "Female",]$Spec.wgt, i) TS_WT[i] = mean(subset1) } ## Body length comparison ```{r} ##Relationship between BL AND IT #bees bee_bl=bee_all[!is.na(bee_all$BL),] bee_blength=aggregate(bee_bl$BL~bee_bl$Species,FUN="mean") bee_blength$IT=as.numeric(unlist(aggregate(bee_bl$IT~bee_bl$Species,FUN="mean")[2])) bee_blength$Spec.wgt=as.numeric(unlist(aggregate(bee_bl$Spec.wgt~bee_bl$Species, FUN="mean")[2])) colnames(bee_blength)= c("Species","BL","IT","Spec.wgt") #hoverflies hov_bl=hov_all[!is.na(hov_all$BL),] hov_blength=aggregate(hov_bl$BL~hov_bl$Species,FUN="mean") hov_blength$IT=as.numeric(unlist(aggregate(hov_bl$IT~hov_bl$Species,FUN="mean")[2])) hov_blength$Spec.wgt=as.numeric(unlist(aggregate(hov_bl$Spec.wgt~hov_bl$Species,FUN="mean")[2])) colnames(hov_blength)= c("Species","BL","IT","Spec.wgt") ###BEES cor.test(~log(IT)+log(BL),data=bee_blength) str(bee_blength) summary(lm( log(Spec.wgt)~ log(BL),bee_blength)) summary(lm( log(Spec.wgt)~ log(IT),bee_blength)) ###HOV cor.test(~log(IT)+log(BL),hov_blength) summary(lm(log(Spec.wgt)~log(BL),hov_blength)) summary(lm(log(Spec.wgt)~log(IT),hov_blength)) lengthsize=function(BL, Eq = "DIP"){ if(!Eq %in% c("DIP","HYM","LEP", "S93DB","Sabo02DB", "Sabo02DA", "Sabo02DBB", "R77D", "S93DC", "BN06D", "G97D", "GR84D", "JS00DA", "S80DCF", "S80DCR", "S80DMF", "S93DA", "W13D", "JS00DN", "S93DN", "Sabo02DN","DIP","Brachycera","Nematocera", "Asilidae","Bombyliidae")){ stop("Equation should be one of 'DIP','HYM','LEP','R77D', 'S93DB', 'Sabo02DB', 'Sabo02DA', 'Sabo02DBB', 'S93DC', 'BN06D', 'G97D', 'GR84D', 'JS00DA', 'S80DCF', 'S80DCR', 'S80DMF', 'S93DA', 'W13D', 'JS00DN', 'S93DN', 'Sabo02DN','All','Brachycera','Nematocera','Asilidae','Bombyliidae'") } else { S93DB <- exp(-3.374+2.158*log(BL)) Sabo02DB <- 0.006*(BL)^3.05 Sabo02DA <- 0.38*(BL)^1.5 Sabo02DBB <- 0.007*(BL)^3.337 S93DC <- exp(-3.619+2.632*log(BL)) BN06D <- exp(-0.041+0.010*(BL)) G97D <- exp(-3.4294+2.5943*log(BL)) GR84D <- exp(-3.653+2.546*log(BL)) JS00DA <- exp(-2.462+1.881*log(BL)) R77D <- exp(-3.293+2.366*log(BL)) S80DCF <- exp(log(0.074)+1.64*log(BL)) S80DCR <- exp(log(0.068)+1.59*log(BL)) S80DMF <- exp(log(0.022)+2.42*log(BL)) S93DA <- exp(-3.184+2.23*log(BL)) W13D <- exp(-3.29+2.65*log(BL)) JS00DN <- exp(-2.462+1.881*log(BL)) S93DN <- exp(-3.675+2.212*log(BL)) Sabo02DN <- 0.1*(BL)^1.57 Sabo02HA <- 0.006*(BL)^3.407 S93HB <- exp(-3.854+2.441*log(BL)) BN06HF <- exp(log(0.001)+2.33*log(BL)) GR84F <- exp(-3.997+log(BL)*2.489) JS00HF <- exp(-3.730+2.103*log(BL)) R77A <- exp(-4.029+2.572*log(BL)) S80FCF <- exp(log(0.012)+2.72*log(BL)) S80FCR <- exp(log(0.021)+2.31*log(BL)) S80FMF <- exp(log(0.034)+2.19*log(BL)) S93HF <- exp(-4.727+2.919*log(BL)) S93HH <- exp(-2.891+2.302*log(BL)) S93HI <- exp(-4.149+2.464*log(BL)) BN06H1 <- exp(-6.783+2.544*log(BL)) G97H <- exp(-3.5917+2.6429*log(BL)) G97F <- exp(-3.1415+2.3447*log(BL)) GR84H <- exp(-2.86+(BL)*0.478) JS00HA <- exp(-3.556+2.193*log(BL)) R77H <- exp(-3.871+2.407*log(BL)) S80HCF <- exp(log(0.043)+2.07*log(BL)) S80HCR <- exp(log(0.022)+2.29*log(BL)) S80HMF <- exp(log(0.016)+2.55*log(BL)) S93HA <- exp(-4.284+2.696*log(BL)) Sabo02H <- 0.56*(BL)^1.56 W13H <- exp(-4.3+3*log(BL)) S93HP <- exp(-2.341+2.006*log(BL)) S93HV <- exp(-3.54+2.782*log(BL)) Sabo02HV <- 0.001*(BL)^3.723 S93LG <- exp(-4.172+2.628*log(BL)) S93LM <- exp(-4.913+2.918*log(BL)) BN06L <- exp(log(0.001)+2.313*log(BL)) G97L <- exp(-4.7915+2.8585*log(BL)) JS00L <- exp(-3.268+2.243*log(BL)) R77L <- exp(-4.037+2.903*log(BL)) S80LCF <- exp(log(0.026)+2.5*log(BL)) S80LCR <- exp(log(0.078)+1.32*log(BL)) S80LMF <- exp(log(0.014)+2.55*log(BL)) S93LA <- exp(-5.036+3.122*log(BL)) W13L <- exp(-3.83+2.77*log(BL)) S93LC <- exp(-3.755+2.658*log(BL)) S93LN <- exp(-3.337+2.499*log(BL)) if (Eq == "S93DB") out <- S93DB if (Eq == "Sabo02DB") out <- Sabo02DB if (Eq == "Sabo02DA") out <- Sabo02DA if (Eq == "Sabo02DBB") out <- Sabo02DBB if (Eq == "S93DC") out <- S93DC if (Eq == "BN06D") out <- BN06D if (Eq == "G97D") out <- G97D if (Eq == "GR84D") out <- GR84D if (Eq == "JS00DA") out <- JS00DA if (Eq == "R77D") out <- R77D if (Eq == "S80DCF") out <- S80DCF if (Eq == "S80DCR") out <- S80DCR if (Eq == "S80DMF") out <- S80DMF if (Eq == "S93DA") out <- S93DA if (Eq == "W13D") out <- W13D if (Eq == "JS00DN") out <- JS00DN if (Eq == "S93DN") out <- S93DN if (Eq == "Sabo02DN") out <- Sabo02DN if (Eq == "Brachycera") out <- cbind(S93DB, Sabo02DB) if (Eq == "Nematocera") out <- cbind(JS00DN, S93DN, Sabo02DN) if (Eq == "Asilidae") out <- cbind(Sabo02DA, S93DB) if (Eq == "Bombyliidae") out <-cbind(Sabo02DBB,S93DB) if (Eq == "DIP") out <- cbind(BN06D, G97D, GR84D, JS00DA, R77D, S80DCF, S80DCR, S80DMF, S93DA, W13D) if (Eq == "Sabo02HA") out <- Sabo02HA if (Eq == "S93HB") out <- S93HB if (Eq == "BN06HF") out <- BN06HF if (Eq == "GR84F") out <- GR84F if (Eq == "JS00HF") out <- JS00HF if (Eq == "R77A") out <- R77A if (Eq == "S80FCF") out <- S80FCF if (Eq == "S80FCR") out <- S80FCR if (Eq == "S80FMF") out <- S80FMF if (Eq == "S93HF") out <- S93HF if (Eq == "S93HH") out <- S93HH if (Eq == "S93HI") out <- S93HI if (Eq == "BN06H1") out <- BN06H1 if (Eq == "G97H") out <- G97H if (Eq == "G97F") out <- G97F if (Eq == "GR84H") out <- GR84H if (Eq == "JS00HA") out <- JS00HA if (Eq == "R77H") out <- R77H if (Eq == "S80HCF") out <- S80HCF if (Eq == "S80HCR") out <- S80HCR if (Eq == "S80HMF") out <- S80HMF if (Eq == "S93HA") out <- S93HA if (Eq == "Sabo02H") out <- Sabo02H if (Eq == "W13H") out <- W13H if (Eq == "S93HP") out <- S93HP if (Eq == "S93HV") out <- S93HV if (Eq == "Sabo02HV") out <- Sabo02HV if (Eq == "Vespidae") out <- cbind(S93HV,Sabo02HV) if (Eq == "Formicidae") out <- cbind(BN06HF,GR84F,JS00HF,R77A,S80FCF,S80FCR,S80FMF,S93HF,S93HH,S93HI,BN06H1,G97H,G97F) if (Eq == "HYM") out = cbind(BN06H1,G97H, G97F,GR84H,JS00HA,R77H,S80HCF,S80HCR,S80HMF,S93HA,Sabo02H,W13H) if (Eq == "S93LG") out <- S93LG if (Eq == "S93LM") out <- S93LM if (Eq == "BN06L") out <- BN06L if (Eq == "G97L") out <- G97L if (Eq == "JS00L") out <- JS00L if (Eq == "R77L") out <- R77L if (Eq == "S80LCF") out <- S80LCF if (Eq == "S80LCR") out <- S80LCR if (Eq == "S80LMF") out <- S80LMF if (Eq == "S93LA") out <- S93LA if (Eq == "W13L") out <- W13L if (Eq == "S93LC") out <- S93LC if (Eq == "S93LN") out <- S93LN if (Eq == "LEP") out <- cbind(BN06L,G97L,JS00L,R77L,S80LCF, S80LCR,S80LMF,S93LA,W13L) if (Eq == "Noctuidea") out <- cbind(S93LC, S93LN) out } } #BL analyses ##Pre-existing equations hov_bl_rmse=cbind(rmse((hov_bl$Spec.wgt),(lengthsize(hov_bl$BL,"DIP")[,1])), rmse((hov_bl$Spec.wgt),(lengthsize(hov_bl$BL,"DIP")[,2])), rmse((hov_bl$Spec.wgt),(lengthsize(hov_bl$BL,"DIP")[,3])), rmse((hov_bl$Spec.wgt),(lengthsize(hov_bl$BL,"DIP")[,4])), rmse((hov_bl$Spec.wgt),(lengthsize(hov_bl$BL,"DIP")[,5])), rmse((hov_bl$Spec.wgt),(lengthsize(hov_bl$BL,"DIP")[,6])), rmse((hov_bl$Spec.wgt),(lengthsize(hov_bl$BL,"DIP")[,7])), rmse((hov_bl$Spec.wgt),(lengthsize(hov_bl$BL,"DIP")[,8])), rmse((hov_bl$Spec.wgt),(lengthsize(hov_bl$BL,"DIP")[,9])), rmse((hov_bl$Spec.wgt),(lengthsize(hov_bl$BL,"DIP")[,10]))) #mean mean(hov_bl_rmse) #standard error mean(hov_bl_rmse)/sqrt(10) bee_bl_rmse=cbind(rmse((bee_bl$Spec.wgt), (lengthsize(bee_bl$BL,"HYM")[,1])), rmse((bee_bl$Spec.wgt), (lengthsize(bee_bl$BL,"HYM")[,2])), rmse((bee_bl$Spec.wgt), (lengthsize(bee_bl$BL,"HYM")[,3])), rmse((bee_bl$Spec.wgt), (lengthsize(bee_bl$BL,"HYM")[,4])), rmse((bee_bl$Spec.wgt), (lengthsize(bee_bl$BL,"HYM")[,5])), rmse((bee_bl$Spec.wgt), (lengthsize(bee_bl$BL,"HYM")[,6])), rmse((bee_bl$Spec.wgt), (lengthsize(bee_bl$BL,"HYM")[,7])), rmse((bee_bl$Spec.wgt), (lengthsize(bee_bl$BL,"HYM")[,8])), rmse((bee_bl$Spec.wgt), (lengthsize(bee_bl$BL,"HYM")[,9])), rmse((bee_bl$Spec.wgt), (lengthsize(bee_bl$BL,"HYM")[,10])), rmse((bee_bl$Spec.wgt), (lengthsize(bee_bl$BL,"HYM")[,11])), rmse((bee_bl$Spec.wgt), (lengthsize(bee_bl$BL,"HYM")[,12]))) #mean mean(bee_bl_rmse) #standard error mean(bee_bl_rmse)/sqrt(12) #Comparison with Cane 1989 rmse((bee_all$Spec.wgt),(Cane(IT=bee_all$IT)))