## Solution to Challenge Problem 5: Fitting Pagel’s (1994) model

The challenge problem was as follows.

Use the phytools function `fitPagel` to fit Pagel's model for correlated binary-trait evolution on the tree. What do you find? Visualize the character data on the tree. What does this tell you about the basis for your result?

``````library(phytools)
``````
``````## Loading required package: ape
``````
``````## Warning: package 'ape' was built under R version 3.3.0
``````
``````## Loading required package: maps
##
##  # maps v3.1: updated 'world': all lakes moved to separate new #
##  # 'lakes' database. Type '?world' or 'news(package="maps")'.  #
``````

Now load data & tree from file:

``````tree<-read.tree("fitPagel-challenge.tre")
``````

Now, let's apply Pagel's (1994) method using `fitPagel`:

``````x<-setNames(X[,1],rownames(X))
y<-setNames(as.factor(X[,2]),rownames(X))
fitted.model<-fitPagel(tree,x,y)
fitted.model
``````
``````##
##   Pagel's binary character correlation test:
##
## Independent model rate matrix:
##            a|0        a|1        b|0        b|1
## a|0 -0.1021196  0.0000000  0.1021196  0.0000000
## a|1  3.5809628 -3.6830823  0.0000000  0.1021196
## b|0  0.1021198  0.0000000 -0.1021198  0.0000000
## b|1  0.0000000  0.1021198  3.5809628 -3.6830825
##
## Dependent model rate matrix:
##          a|0       a|1       b|0       b|1
## a|0  0.00000   0.00000   0.00000   0.00000
## a|1 18.34017 -36.69125   0.00000  18.35108
## b|0  0.00000   0.00000 -61.01172  61.01172
## b|1  0.00000   0.00000  60.99491 -60.99491
##
## Model fit:
##             log-likelihood
## independent      -47.46589
## dependent        -24.95332
##
## Hypothesis test result:
##   likelihood-ratio:  45.02514
##   p-value:  3.928399e-09
##
## Model fitting method used was fitMk
``````

Obviously, the more parameter rich “dependent” model fits better. But let's visualize our data on the tree to see if it tells us anything more:

``````dotTree(tree,X,fsize=0.6,ftype="i")
``````

Now, we see that there is only one change in character one - but that change (or, rather, trait difference) defines a shift in rate or process for character 2.

This is essentially the result identified by Maddisson & Fitzjohn (2015).

Written by Liam J. Revell. Last updated 12 Mar. 2016.