Fits the Ornstein-Uhlenbeck-based Hansen model to data.
The fitting is done using `optim`

or `subplex`

.

1 2 3 4 |

`data` |
Phenotypic data for extant species, i.e., species at the terminal twigs of the phylogenetic tree.
This can either be a single named numeric vector, a list of |

`tree` |
A phylogenetic tree, specified as an |

`regimes` |
A vector of codes, one for each node in the tree, specifying the selective regimes hypothesized to have been operative.
Corresponding to each node, enter the code of the regime hypothesized for the branch segment terminating in that node.
For the root node, because it has no branch segment terminating on it, the regime specification is irrelevant.
If there are |

`sqrt.alpha, sigma` |
These are used to initialize the optimization algorithm.
The selection strength matrix |

`fit` |
If |

`method` |
The method to be used by the optimization algorithm, |

`hessian` |
If |

`...` |
Additional arguments will be passed as |

The Hansen model for the evolution of a multivariate trait *X* along a lineage can be written as a stochastic differential equation (Ito diffusion)

*dX = alpha (theta(t)-X(t)) dt + sigma dB(t),*

where *t* is time along the lineage, *theta(t)* is the optimum trait value, *B(t)* is a standard Wiener process (Brownian motion), and *alpha* and *sigma* are matrices quantifying, respectively, the strength of selection and random drift.
Without loss of generality, one can assume *sigma* is lower-triangular.
This is because only the infinitesimal variance-covariance matrix *sigma^2 = sigma%*%transpose(sigma)* is identifiable, and for any admissible variance-covariance matrix, we can choose *sigma* to be lower-triangular.
Moreover, if we view the basic model as describing evolution on a fitness landscape, then *alpha* will be symmetric and if we further restrict ourselves to the case of stabilizing selection, *alpha* will be positive definite as well.
We make these assumptions and therefore can assume that the matrix *alpha* has a lower-triangular square root.

The `hansen`

code uses unconstrained numerical optimization to maximize the likelihood.
To do this, it parameterizes the *alpha* and *sigma^2* matrices in a special way:
each matrix is parameterized by `nchar*(nchar+1)/2`

parameters, where `nchar`

is the number of quantitative characters.
Specifically, the parameters initialized by the `sqrt.alpha`

argument of `hansen`

are used to fill the nonzero entries of a lower-triangular matrix (in column-major order), which is then multiplied by its transpose to give the selection-strength matrix.
The parameters specified in `sigma`

fill the nonzero entries in the lower triangular *sigma* matrix.
When `hansen`

is executed, the numerical optimizer maximizes the likelihood over these parameters.
The `print`

, `show`

, and `summary`

methods for the resulting `hansentree`

object display (among other things) the estimated *alpha* and *sigma^2* matrices.
The `coef`

method extracts a named list containing not only these matrices (given as the `alpha.matrix`

and `sigma.sq.matrix`

elements) but also the MLEs returned by the optimizer (as `sqrt.alpha`

and `sigma`

, respectively).
**The latter elements should not be interpreted, but can be used to restart the algorithm, etc.**

`hansen`

returns an object of class `hansentree`

.
For details on the methods of that class, see `hansentree`

.

Aaron A. King <kingaa at umich dot edu>

Butler, M.A. and A.A. King (2004) Phylogenetic comparative analysis: a modeling approach for adaptive evolution. American Naturalist 164:683-695.

`ouchtree`

, `hansentree`

, `optim`

, `bimac`

, `anolis.ssd`

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | ```
## Not run:
if (require(geiger)) {
### an example data set (Darwin's finches)
data(geospiza)
str(geospiza)
sapply(geospiza,class)
### check the correspondence between data and tree tips:
print(nc <- with(geospiza,name.check(geospiza.tree,geospiza.data)))
### looks like one of the terminal twigs has no data associated
### drop that tip:
tree <- with(geospiza,drop.tip(geospiza.tree,nc$tree_not_data))
dat <- as.data.frame(geospiza$dat)
### make an ouchtree out of the phy-format tree
ot <- ape2ouch(tree)
### merge data with tree info
otd <- as(ot,"data.frame")
### in these data, it so happens that the rownames correspond to node names
### we will exploit this correspondence in the 'merge' operation:
dat$labels <- rownames(dat)
otd <- merge(otd,dat,by="labels",all=TRUE)
rownames(otd) <- otd$nodes
print(otd)
### this data-frame now contains the data as well as the tree geometry
### now remake the ouch tree
ot <- with(otd,ouchtree(nodes=nodes,ancestors=ancestors,times=times,labels=labels))
b1 <- brown(tree=ot,data=otd[c("tarsusL","beakD")])
summary(b1)
### evaluate an OU model with a single, global selective regime
otd$regimes <- as.factor("global")
h1 <- hansen(
tree=ot,
data=otd[c("tarsusL","beakD")],
regimes=otd["regimes"],
sqrt.alpha=c(1,0,1),
sigma=c(1,0,1),
maxit=10000
)
summary(h1)
}
## End(Not run)
``` |

Questions? Problems? Suggestions? Tweet to @rdrrHQ or email at ian@mutexlabs.com.

Please suggest features or report bugs with the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.