Commits

Casey Dunn committed dd38102

plot simulated data

Comments (0)

Files changed (2)

analyses/comparative_r/knitr/comparative_r.Rnw

 There is detailed information on phylogenetic analysis with R at both 
 \href{http://www.r-phylo.org/wiki/Main_Page}{the R phylo wiki} and the 
 \href{http://cran.r-project.org/web/views/Phylogenetics.html}{CRAN task page for phylogenetics}.
+Also consult the excellent book by \cite{Paradis:2012wn}.
 
 Make sure that you have the most recent version of R installed. For the 
 analyses below, you will need to install the following libraries in R:
-* ape
-* geiger
+
+\begin{itemize}
+\item ape
+\item geiger
+\item phylobase
+\end{itemize}
 
 Update them if they are not the latest version.
 
 library(ape)
 @
 
-The simplest way to generate a random tree is with the rtree() function:
+The simplest way to generate a random tree is with the rcoal() function:
+
+<<>>=
+t <- rcoal( 8 )
+plot( t )
+@
+
+The only required argument is the number of taxa. rcoal() generates ultrametric 
+trees, the related function rtree() can be used to generate trees whose tips 
+are not contemporaneous.
+
+
+\section{Simulating phenotypic data on trees}
+
+The sim.char() function from the geiger library is a convenient tool for 
+simulating character evolution. 
+
+We'll simulate two characters, each with a variance of 1 and a covariance with 
+each other of 0.3. First, make a variance-covariance matrix describes this 
+relationship:
+
+<<>>=
+vcv <- c( 1, 0.3, 0.3, 1 )
+dim( vcv ) <- c( 2, 2 )
+vcv
+@
+
+Now, generate 500 simulated datasets on tree t according to vcv:
+
 
 <<>>=
-plot( rtree( 8 ) )
+library(geiger)
+xy <- sim.char( t, vcv, nsims = 500, model = "brownian", root.state = 1 )
+xy[,,1]
 @
 
-The only required argument is the number of taxa. Unless otherwise specified, 
-the branch lengths are drawn from a uniform distribution between 0 and 1. 
+This shows just the first simulation.
+
+You can plot the data right onto the tree. The phylobase library has some nice 
+tools for plotting miltivariate data:
+
+
+<<>>=
+library( adephylo )
+xy4d <- phylo4d( t, xy[,,1] )
+table.phylo4d( xy4d, box=FALSE )
+
+@
 
 
 

analyses/comparative_r/knitr/references.bib

 number = {1},
 pages = {1--15}
 }
+
+
+@book{Paradis:2012wn,
+author = {Paradis, Emmanuel},
+title = {{Analysis of Phylogenetics and Evolution With R}},
+publisher = {Springer Science+Business Media},
+year = {2012},
+month = jan,
+isbn = {9781461417439},
+language = {English},
+rating = {0},
+date-added = {2013-04-08T14:34:13GMT},
+date-modified = {2013-04-08T14:34:24GMT},
+abstract = {This updated second edition includes material on interfacing R with other computer languages such as C. It reflects the increasing availability of molecular and genetic databases which allow biologists to pursue new issues such as molecular evolution patterns.},
+url = {http://books.google.com/books?id=FsScWggkW_cC&printsec=frontcover&dq=analysis+of+phylogenetics+and+evolution+with+R&hl=&cd=1&source=gbs_api},
+uri = {\url{papers2://publication/uuid/934641A3-4472-485B-9A84-91CF9AF79F5A}}
+}
+