Commits

Casey Dunn committed 0574b8e

cleaned up contrasts text

Comments (0)

Files changed (1)

analyses/independent_contrasts/knitr/independent_contrasts.Rnw

 column gives the ending node. The edge number is just the row 
 number. 
 
-The edge.length is the lengths of each of the edges. These are organized in 
-the same order as in the edge rows.
+The edge.length vector contains the lengths of each of the edges.
 
 Nnode simply holds the number of internal nodes. tip.label gives the names 
 of the external nodes (tips).
 
 The primary take-home message is that each node has a number that refers to it. 
 The observed data correspond to external nodes. Ancestral reconstructions 
-and independent correspond to internal nodes. In addition, each edge has a 
-number that refers to it.
+and independent contrasts will correspond to internal nodes. In addition, each 
+edge has a number that refers to it. We can use this number to look up the 
+edge length.
 
 You can plot the node numbers right onto the tree. ape handles tips and 
 internal nodes a bit differently, so we will label them on the same tree with 
 edgelabels( round(tree.primates$edge.length,2) )
 @
 
-
 \subsection{Adjusting the branch lengths}
 
 The lengths of internal branches must be adjusted to reflect the increased 
-variance associated with the inference of ancestral character states. There are 
-only three internal branch lengths, so we'll take care of those by hand.
+variance associated with the inference of ancestral character states. Below, 
+we create a copy of all the branch lengths and then correct the lengths of the 
+three internal branches.
 
 <<>>=
 v <- tree.primates$edge.length
 
 The internal character states are calculated as weighted averages of the 
 states at the descendant nodes. This calculation is described by Equation 3 of 
-\cite{Felsenstein:1985ua}.
+\cite{Felsenstein:1985ua}. Since the ancestral character state of the root node 
+isn't used in any contrast calculations, we won't worry about it.
 
 
 <<>>=
 
 A slightly different but equivalent formulation of these calculations is 
 presented for the calculations of $X_i$ in Table 1 of \cite{Felsenstein:1985ua}. 
-According to this alternative formulation, the ancestral character state for 
+According to this alternative formulation, the ancestral character states for 
 nodes 9 and 8 would be calculated as:
 
 <<>>=
 @
 
 
-
-Now take a look at the X data. Calculate the ancestral character states:
+We can also calculate the ancestral character states with the ape function 
+ace:
 
 <<>>=
 aX <- ace(X, tree.primates, method = "pic", scaled = FALSE)$ace
 aX
 @
 
-This creates a vector of the reconstructed values of tarsusL at the internal 
-nodes. These values have names 14-25, correspoding to the numbers of the 
-internal nodes. Plot these on the tree, along with the values at the tips:
+Note that the values for the states at nodes 7-9 are the same as those we 
+calculated above. Now, plot the observed values and inferred ancestral 
+character states:
 
 <<>>=
 plot(tree.primates, label.offset = 0.04)
 nodelabels( round(aX, 2) )
 @
 
-For later convenience, combine all these node values into a single vector:
+For later convenience, combine all these state values into a single vector:
 
 <<>>=
 nX <- c( X, aX )
 nX
 @
 
-This provides an overview of the evolution of tarsusL, including both the 
-observed values at the tips and the inferred values at the ancestral nodes.
-
-We will use these variances when we scale the contrasts below.
-
 \subsection{Calculating the contrasts}
 
 From \cite{Felsenstein:1985ua}, the contrast at an internal node is the 
-difference in values at the descendent nodes.
-
+difference in values at the descendent nodes:
 
 <<>>=
 contrasts <- c( nX[7]-nX[5], nX[8]-nX[4],  nX[9]-nX[3],  nX[1]-nX[2] )
 contrasts
 @
 
+That's it. The independent contrasts are just a series of subtractions.
+
 
-Now, compare this to what we get with the pic function:
+Now, compare the values we calculated to what we get with the pic function:
 
 <<>>=
 pic.X.ns <- pic(X, tree.primates, scaled = FALSE, var.contrasts = TRUE)
 pic.X.ns
-pic.X.ns[,1]/sqrt( pic.X.ns[,2] )
-pic(X, tree.primates, scaled = TRUE, var.contrasts = TRUE)
 @
 
+The values are the same as what we got above.
 
+For most downstream calculations, we want to scale the contrasts to normalize 
+the expected differences according to branch lengths. I turned off this 
+scaling above so that we could directly compare the pic() results to our own 
+results.
 
-Now you have everything you need to calculate the scaled contrast for node 22:
+The branch length is proportional to the variance. To normalize, we'll divide 
+by the standard deviation. The square root of the variance of each contrast 
+is the standard deviation for that contrast:
 
 <<>>=
-
+pic.X.ns[,1]/sqrt( pic.X.ns[,2] )
 @
 
+These scaled vales are the same as what we get when we have pic() do the 
+scaling for us:
 
-
+<<>>=
+pic(X, tree.primates, scaled = TRUE, var.contrasts = TRUE)
+@