Skip to contents
library(phyf)
#> 
#> Attaching package: 'phyf'
#> The following object is masked from 'package:stats':
#> 
#>     pf
library(ape)
test_tree <- rtree(100) 
tree_pf <- pf_as_pf(test_tree)
tree_pf
#> # A tibble: 198 × 3
#>    label is_tip phlo                                                            
#>    <chr> <lgl>  <pfc>                                                           
#>  1 t3    TRUE   ◎── 0.32──→ Node2 ── 0.18──→ No── 0.27──→ Node5 ── 0.85──→ t3 
#>  2 t20   TRUE   ◎── 0.32──→ Node2 ── 0.18──→ No── 0.27──→ Node5 ── 0.76──→ t20 
#>  3 t47   TRUE   ◎── 0.32──→ Node2 ── 0.18──→ No── 0.56──→ Node7 ── 0.49──→ t47 
#>  4 t23   TRUE   ◎── 0.32──→ Node2 ── 0.18──→ No── 0.56──→ Node7 ── 0.82──→ t23 
#>  5 t18   TRUE   ◎── 0.32──→ Node2 ── 0.18──→ No── 0.12──→ Node6 ── 0.90──→ t18 
#>  6 t83   TRUE   ◎── 0.32──→ Node2 ── 0.18──→ No── 0.58──→ Node8 ── 0.30──→ t83 
#>  7 t22   TRUE   ◎── 0.32──→ Node2 ── 0.18──→ No── 0.73──→ Node9 ── 0.29──→ t22 
#>  8 t54   TRUE   ◎── 0.32──→ Node2 ── 0.18──→ No── 0.73──→ Node9 ── 0.52──→ t54 
#>  9 t21   TRUE   ◎── 0.32──→ Node2 ── 0.02──→ No 0.32──→ Node12 ── 0.70──→ t21 
#> 10 t93   TRUE   ◎── 0.32──→ Node2 ── 0.02──→ No 0.32──→ Node12 ── 0.28──→ t93 
#> # ℹ 188 more rows

This tibble is easy to join data by using the node_names, which include the tip labels from the phylogeny as well as ‘NodeXX’ for internal nodes, where XX starts a 1 and goes to the total number of internal nodes. You can also use node_nums to join by the node number, where the node number uses the traditional ordering of the nodes used in the ape package in phylo objects. Usually you will only have data on the tips, and so when joining to the pf object (using e.g. dplyr::left_join()) the internal node rows will recieve NA values. This is the desired behaviour. The missing values are easy to drop for fitting a model, but are useful later when making prediction (that is, ancestral state estimates).

Here, we will use one of the built-in pf datasets in phyf to show off some of its feature.

data("avonet")
avonet
#> # A tibble: 13,338 × 39
#>    label is_tip phlo            Species3 Family3 Order3 Total.individuals Female
#>    <chr> <lgl>  <pfc>           <chr>    <chr>   <chr>              <dbl>  <dbl>
#>  1 Rhea… TRUE   ◎── 24.ricana  Rhea am… Rheidae Rheif…                 5      2
#>  2 Rhea… TRUE   ◎── 24.ennata  Rhea pe… Rheidae Rheif…                 6      3
#>  3 Apte… TRUE   ◎── 24.tralis  Apteryx… Aptery… Apter…                 6      2
#>  4 Apte… TRUE   ◎── 24.ntelli  Apteryx… Aptery… Apter…                 4      2
#>  5 Apte… TRUE   ◎── 24.owenii  Apteryx… Aptery… Apter…                 5      2
#>  6 Apte… TRUE   ◎── 24.aastii  Apteryx… Aptery… Apter…                 9      6
#>  7 Drom… TRUE   ◎── 24.andiae  Dromaiu… Dromai… Casua…                 5      2
#>  8 Casu… TRUE   ◎── 24.uarius  Casuari… Casuar… Casua…                 7      2
#>  9 Casu… TRUE   ◎── 24.nnetti  Casuari… Casuar… Casua…                 4      1
#> 10 Stru… TRUE   ◎──   2amelus  Struthi… Struth… Strut…                 8      1
#> # ℹ 13,328 more rows
#> # ℹ 31 more variables: Male <dbl>, Unknown <dbl>, Complete.measures <dbl>,
#> #   Beak.Length_Culmen <dbl>, Beak.Length_Nares <dbl>, Beak.Width <dbl>,
#> #   Beak.Depth <dbl>, Tarsus.Length <dbl>, Wing.Length <dbl>,
#> #   Kipps.Distance <dbl>, Secondary1 <dbl>, `Hand-Wing.Index` <dbl>,
#> #   Tail.Length <dbl>, Mass <dbl>, Mass.Source <chr>, Mass.Refs.Other <chr>,
#> #   Inference <chr>, Traits.inferred <chr>, Reference.species <chr>, …

First, let’s try plotting the object.