Assess factors shaping the evolution of duettting in owls
2 Load packages
Code
# knitr is require for creating html/pdf/word reports# formatR is used for soft-wrapping codeif (!"sketchy"%in%installed.packages()[,1]) remotes::install_github("maRce10/sketchy")# unload R packages (specific packages required for this code)x <-c("ggplot2","kableExtra","knitr","formatR","rprojroot","xaringanExtra","viridis","brms","ape","brmsish","corrplot","phytools",github ="maRce10/brmsish" )# install/ load packagessketchy::load_packages(packages = x)
Code
## Open dataframes# read datatrees_owl <-read.nexus("./data/raw/BirdTree_Owl_Output.nex")corr_df <- df <-readRDS("./data/raw/Owl_Duet.RDS")# Set categorical response variable as ordered factors (ordinal data) and #USE FOR BRMSdf$tip.labels <-as.factor(df$tip.labels) # binary categorical response variabledf$Duetting <-as.factor(df$Duetting) # categorical explanatory variabledf$Social_Bond <-as.factor(df$Social_Bond) # categorical explanatory variabledf$Territoriality <-as.factor(df$Territoriality) # categorical explanatory variabledf$White_Plumage <-as.factor(df$White_Plumage) # categorical explanatory variabledf$Nocturnality <-as.factor(df$Nocturnality) # categorical explanatory variabledf$Habitat_Density <-as.factor(df$Habitat_Density) # categorical explanatory variabledf$Migration <-as.factor(df$Migration) # categorical explanatory variable# scale and center numerical datadf$Central_Latitude <-scale(df$Central_Latitude, center =TRUE, scale =TRUE)# use if you need to reset rownames as sequential numbersrownames(df) <-1:nrow(df)# look at structurestr(df)
####################### Response variables ######################## Make the correlation table### NOTE: DEFAULT CODE DOES NOT REMOVE MISSING DATA; use complete.obs to drop nas.corrtab <-cor(corr_df[,-1], method ="pearson", use ="complete.obs")round(corrtab, 2)
# Set categorical data to analyze as numbersdf$duet <-as.numeric(df$Duetting)### phylogenetic signal for response variabletrait_duet <- df$duetlength(trait_duet)
Phylogenetic signal lambda : 0.428795
logL(lambda) : -37.3961
LR(lambda=0) : 1.65596
P-value (based on LR test) : 0.19815
2.0.2 PHYLOGENETICALLY CONTROLLED COMPARATIVE ANALYSES USING JETZ TREE
2.0.3 USE TO COMPARE DUET PRESENCE/ABSENCE TO LIFE HISTORY TRAITS
2.1 BRMS V1 - original
Code
### See https://urldefense.proofpoint.com/v2/url?u=https-3A__psyarxiv.com_x8swp_&d=DwIGAg&c=t1KneP5CA95qj21Jnj2QJw&r=uXrXEa22P1XTtZOkZJzsx8pAQCDanQ2sMUk_AfMLQ0I&m=Th_rFO1G4mV3f039Te1VuvpmcjwF2KXjHAfcU0UhAb8vpbbU-nEL3HwCTlICHqS3&s=qolX0qBhJIjMDpanzBhiCR3QCeDrzGH4A9eyiqlhW2g&e= for explanation of types of ordinal models implemented in brms.# And https://urldefense.proofpoint.com/v2/url?u=https-3A__solomonkurz.netlify.app_post_2021-2D12-2D29-2Dnotes-2Don-2Dthe-2Dbayesian-2Dcumulative-2Dprobit_&d=DwIGAg&c=t1KneP5CA95qj21Jnj2QJw&r=uXrXEa22P1XTtZOkZJzsx8pAQCDanQ2sMUk_AfMLQ0I&m=Th_rFO1G4mV3f039Te1VuvpmcjwF2KXjHAfcU0UhAb8vpbbU-nEL3HwCTlICHqS3&s=PQ-Sgi8kE3P2Yamj_rtG-r2rZUbEB5iytYjn9NLil08&e= ## set function to prune treesprep_tree <-function(tree, spp) { spp <-unique(spp)if (is(tree, "multiPhylo")) trees <-lapply(tree, function(x)drop.tip(phy = x, tip =setdiff(x$tip.label, spp)))else trees <-drop.tip(phy = tree, tip =setdiff(tree$tip.label, spp))return(tree)}# set trees#A <- vcv.phylo(trees)# set parametersset.seed(123)parallel <-7# set this to the number of cores you want to use in the clusterthin <-100priors <-c(prior(normal(0, 2), class ="b"))chains <-4## run model### Owl duets: present/absent vs explanatory variablesmod <-phylogenetic_uncertainty(formula = Duetting ~ Social_Bond + Territoriality + Nocturnality + Habitat_Density + Migration + Central_Latitude + White_Plumage,data = df,phylos =prep_tree(trees_owl, spp = df$tip.labels),family =bernoulli(link ="logit"),chains = chains,cores = chains,iter =20000,prior = priors,control =list(adapt_delta =0.99, max_treedepth =15),init =0,thin = thin,sp.id.column ="tip.labels" )saveRDS(mod, "./data/processed/phylogenetic_uncertainty_model.RDS")
Code
# mod <- readRDS("./data/processed/phylogenetic_uncertainty_model.RDS")extended_summary(read.file ="./data/processed/phylogenetic_uncertainty_model.RDS", plot.area.prop =0.5)
Warning: Removed 165 rows containing missing values or values outside the scale range
(`stat_slabinterval()`).