Hi Kate,
You can compute PICs for a categorical variable in the same way than
you enter it in a linear model, that is by first computing its
"contrasts" (this is different from the "P-I-Contrasts", though both
have some conceptual similarities). The easiest way to do it is to use
the function model.matrix. For instance, we generate a tree (tr) and two
variables simulated on that tree, one continuous (x) and one discrete
(z) with three levels:
tr <- rphylo(50, .1, 0)
x <- rTraitCont(tr)
z <- rTraitDisc(tr, k = 3)
We compute the PICs for x the usual way:
pic.x <- pic(x, tr)
And for z:
Z <- model.matrix(~ z)[, -1]
rownames(Z) <- names(z)
pic.z <- apply(Z, 2, pic, phy = tr)
Note that we drop the column with 1's, so Z is a matrix with 2 columns
(number of levels - 1). Thus, pic.z is a matrix with 49 rows (n - 1) and
2 columns. We can now perform a linear regression with the two sets of PICs:
lm(pic.x ~ pic.z - 1)
This should give you the same coefficients than a PGLS like this:
library(nlme)
gls(x ~ z, correlation = corBrownian(phy = tr))
A very nice explanation of this can be found in Blomberg et al. (2012,
Syst. Biol.).
The two columns in pic.z should not be considered separately, in the
same way than in an ANOVA where there are 2 df associated with a 3-level
factor.
HTH
Best,
Emmanuel
Le 04/03/2016 01:07, Kate Boyce-Miles a écrit :
Hello
I have been using numeric vectors to perform phylogenetic independent contrasts
of several ecological variables for the cat phylogeny, using the ape package in
r, and I was wondering how this can be done using categorical values?
Example of how I have been using numeric values:
tree <- read.newick("phylogeny.txt")
tree <- as.phylo(tree)
tree <- root(tree,1)
x<-c() # x would vectors, for instance rainfall. One value for each operational
taxonomic unit on the tree, and the same order as they appear in the newick file.
pic.rain <- pic(x, tree, scaled = T, var.contrasts = F, rescaled.tree = F) # To
generate the phylogenetic independent contrasts
cor.test(pic.rain,pic.rain) # To test for a correlation, obviously for rain and
rain it would be 1.
How can this be done using data such as habitat, i.e grassland/forest/desert,
or activity, i.e. nocturnal, diurnal etc?
I would be very grateful for any suggestions.
Kate.
[[alternative HTML version deleted]]
_______________________________________________
R-sig-phylo mailing list - R-sig-phylo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-sig-phylo@r-project.org/
Pour nous remonter une erreur de filtrage, veuillez vous rendre ici :
http://f.security-mail.net/301tdFND1Ht
_______________________________________________
R-sig-phylo mailing list - R-sig-phylo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-sig-phylo@r-project.org/