Brms phylogenetic model with repeated measurements (by species)

I am new to phylogenetic methods and am hoping to brms phylogenetic model with repeated measurements (by species) on the following dataset.

IshanaRawData.csv (17.2 KB)

We want to test the prevalence of various sleeping strategies (the colored dots below) across habitats (a categorical variable: oceanic, terrestrial, etc) or trophic levels (a categorical variable: top predator, mesopredator, omnivore, herbivore) while controlling for phylogenetic relationships.

IshanaTreeMarm_All_Common|363x500

Here are some visualizations of the data:

I have read over the wonderful brms_phylogenetics vignette and executed the code, but I want to be sure I am handling my categorical variables sufficiently!

setwd("~/Publications/Submitted/Ishana Optimal Sleeping Theory/IshanaRawDataNEW")
ishana=read.csv('IshanaRawData.csv')
ishana$Scientific.Name=gsub(" ","_",ishana$Scientific.Name)#replace "_" with " "

#change to long format and delete zeros
data_long=gather(data=ishana, key=SleepStrategy, value=PresenceAbsence, Grouping:Physiological, factor_key=TRUE)
data_long=data_long[data_long$PresenceAbsence==1,]
data_long$Latin.Name=data_long$Scientific.Name

#read in phylogeny data
setwd("C:/Users/roxan/Desktop/Ishana Full Phylogeny")
filename='output.nex'
Scientific.Name=read.nexus(filename)
Scientific.Name=Scientific.Name[[1]]

#construct a covariance matrix of species (Hadfield Nakagawa 2010) based on phylo
A=ape::vcv.phylo(Scientific.Name) #phylo

#this fixes #Error: Levels of the within-group covariance matrix for 'Scientific.Name' do not match names of the grouping levels. 
needed=which(data_long$Scientific.Name %in% Scientific.Name$tip.label)    
data_long=data_long[needed,]

###MODEL
model_repeat1 <- brm(
  SleepStrategy ~ Habitat + (1|gr(Scientific.Name, cov = A)) + (1|Latin.Name), 
  data = data_long, 
  family = "categorical", 
  data2 = list(A = A),
  prior = prior(normal(0,1), "Intercept"),
  sample_prior = TRUE, chains = 2, cores = 2, 
  iter = 4000, warmup = 1000 #set this high for final runs (see website)
)

Would anyone be willing to look this over? I’d be happy to offer you co-authorship on the resulting manuscript!

Many thanks :)

Hi Roxanne!

What is the difference between Scientific.Name and Latin.Name?

Do some species appear in different habitats? If so, you might want to include a varying slope.

Have a good day!
Lucas

Latin.Name and Scientific.Name are identical. I did that per the suggestion here:

https://cran.r-project.org/web/packages/brms/vignettes/brms_phylogenetics.html

"The variables phylo and species are identical as they are both identifiers of the species. However, we model the phylogenetic covariance only for phylo and thus the species variable accounts for any specific effect that would be independent of the phylogenetic relationship between species (e.g., environmental or niche effects).

And yes, some species appear in different habitats. Thank you @Ideaschamps!