I'm trying to simulate binary trait evolution along phylogenies in R. I'm simulating it with a matrix with species (final tree tips) as rows, and timesteps as columns. In each cell is a vector of traits- the position indicates the trait, and the value indicates presence/absence of the trait.
At every timestep, on every current branch, traits can independently appear with a certain probability, and each currently existing trait can disappear with a different probability. A trait that disappears on one branch doesn't necessarily disappear from another branch on which it exists. Every new trait that appears is unique.
So, as time goes along, whenever a branch gets a new trait at a timestep, a value "1" must be added to all the cells that belong to species descended from that branch, and a value "0" must be added to all the cells that belong to species not descended from that branch (so that position in the vector indicates the trait). Whenever a trait is lost in a branch, the "1" at the appropriate position must be turned into a "0" for all descendents of that branch.
To keep each trait as a position in each vector, the vectors of other branches have to get a new position (with "0" indicating trait absence) when any branch gets a new trait. The issue is I don't know how to identify which tips (rows in my matrix) originate from which current branches (at a specific timestep).
Is there a function for identifying, on a phylogeny, at a specific time, tips that share a current ancestor?
Or, do you have a better way of organizing the trait data through time?
I've tried using other software (TraitLab in Matlab) but not sure that it does exactly what I want, which is why I'd rather code it in R.