----- Le 17 Mai 21, à 13:41, Marguerite Butler <mbutler...@gmail.com> a écrit :
> Thank you very much for the reply Emmanuel! > OK, yes I just tried and Iʻm surprised that this works: >> dat <- data.frame(Species = c("Homo", "Pongo", "Macaca", "Ateles", >> "Galago"), X > > = X, Y = Y) > > cbm <- corBrownian(1, tree, form = ~Species) > > gls(Y ~ X, dat, correlation = cbm) > How does corBrownian know about Species, when it is only defined as an element > of dat, and not known in the global environment? corBrownian() doesn't know where all these variables are located. Indeed, if you print 'cbm': R> cbm Uninitialized correlation structure of class corBrownian It's gls() that looks for them. When you think about it, using "data = ..." is really a good way to be sure that all variables are ordered correctly whether they are involved as predictors (in 'model = '), in the correlation structure, or in the variance function ('weights = '). Best, Emmanuem > Anyway, thank you! > Marguerite > On Sun, May 16, 2021 at 7:03 PM Emmanuel Paradis < [ > mailto:emmanuel.para...@ird.fr | emmanuel.para...@ird.fr ] > wrote: >> Hi Marguerite, >> The issue is about nlme::gls. The help page ?gls says: >> data: an optional data frame containing the variables named in >> ‘model’, ‘correlation’, ‘weights’, and ‘subset’. By default >> the variables are taken from the environment from which ‘gls’ >> is called. >> So you make it work by removing "dat$" in the call to corBrownian(): >> cbm <- corBrownian(1, tree, form = ~Species) >> Best, >> Emmanuel >> ----- Le 17 Mai 21, à 6:22, Marguerite Butler [ mailto:mbutler...@gmail.com | >> mbutler...@gmail.com ] a écrit : >> > Aloha all, >> > Does anyone know the answer to this question - how does ape pass the >> > species name information into gls? Seems to be a global environment issue >> > when using apeʻs corBrownian and nlmeʻs gls? and a very specific form is >> > required, but why? >> > Modified from ape help page for CorClasses: >> > --- >> > library(nlme) >> > library(ape) >> > tree <- read.tree(text = >> > "((((Homo:0.21,Pongo:0.21):0.28,Macaca:0.49):0.13,Ateles:0.62):0.38,Galago:1.00);") >> > X <- c(4.09434, 3.61092, 2.37024, 2.02815, -1.46968) >> > Y <- c(4.74493, 3.33220, 3.36730, 2.89037, 2.30259) >> > dat <- data.frame(Species = c("Homo", "Pongo", "Macaca", "Ateles", >> > "Galago"), X = X, Y = Y) >> > gls(Y ~ X, dat, correlation=corBrownian(1, tree.primates, form = ~Species)) >> > --- >> > The above works just fine. However, if we try to calculate corBrownian >> > separately, gls cannot understand the corBrownian object: >> > --- >> >> cbm <- corBrownian(1, tree, form = ~dat$Species) # calculate correlation >> >> separately >> >> gls(Y ~ X, dat, correlation = cbm) ## does not work, invalid type >> >> (list) for dat >> > Error in model.frame.default(formula = ~dat + Species + Y + X, data = >> > list( : >> > invalid type (list) for variable 'dat' >> > --- >> > If we put the species information into a vector in the global >> > environment it works. Why canʻt we use dat$Species? >> > --- >> > spp <- dat$Species # save species as vector in global environment >> > cbm <- corBrownian(1, tree, form = ~spp) # calculate correlation separately >> > gls(Y ~ X, dat, correlation = cbm) ## works! >> > Thanks, >> > Marguerite >>> On Fri, May 14, 2021 at 10:21 PM Marguerite Butler < [ >> > mailto:mbutler...@gmail.com | mbutler...@gmail.com ] > >> > wrote: >> >> Aloha Oliver, >> >> From the cor.Brownian help page, the explanation for the form argument is >> >> this: >> >> a one sided formula of the form ~ t, or ~ t | g, specifying the taxa >> >> covariate t and, optionally, a grouping factor g. A covariate for this >> >> correlation structure must be character valued, with entries matching the >> >> tip labels in the phylogenetic tree. When a grouping factor is present in >> >> form, the correlation structure is assumed to apply only to observations >> >> within the same grouping level; observations with different grouping >> >> levels >> >> are assumed to be uncorrelated. Defaults to ~ 1, which corresponds to >> >> using >> >> the order of the observations in the data as a covariate, and no groups. >> >> This means that "t" should contain the species names or tip labels, >> >> spelled exactly as they are in your phylogeny. So for example if your data >> >> SteninaeData >> >> has a column for species names called "species" then you would specify: >> >> cor.BM <- corBrownian(phy=SteninaeTree, form = ~dat$species) >> >> and then proceed with your regression model as above. >> >> You can also follow the example on the corClasses page: >>>> [ http://127.0.0.1:27386/library/ape/html/corClasses.html | >> >> http://127.0.0.1:27386/library/ape/html/corClasses.html ] >> >> Slightly modified here: >> >> --- >> >> library(nlme) >> >> library(ape) >> >> tree <- >> >> read.tree(text="((((Homo:0.21,Pongo:0.21):0.28,Macaca:0.49):0.13,Ateles:0.62):0.38,Galago:1.00);" >> >> X <- c(4.09434, 3.61092, 2.37024, 2.02815, -1.46968) # a vector >> >> Y <- c(4.74493, 3.33220, 3.36730, 2.89037, 2.30259) # a vector >> >> Species <- c("Homo", "Pongo", "Macaca", "Ateles", "Galago") # a vector >> >> dat <- data.frame(Species = Species, X = X, Y = Y) # make a dataframe >> >> with Species, X, Y, can also read in .csv >> >> cbm <- corBrownian(1, tree, form = ~dat$Species) >> >> m1 <- gls(Y ~ X, dat, correlation=cbm) >> >> m1 >> >> --- >> >> hth, >> >> Marguerite >>>> On Fri, May 14, 2021 at 9:46 PM Oliver Betz < [ >> >> mailto:oliver.b...@uni-tuebingen.de | oliver.b...@uni-tuebingen.de ] > >> >> wrote: >> >>> Dear all: >> >>> I am currently facing the following problem: >> >>> Unsing ape, nlme por phylolm for PGLS I was wondering how to sort my >> >>> trait data (the ones in my csv file) according to the tree, so that >> >>> both are in the same order. I learned how to check whether the data & >> >>> the tree coincide in both number of species and names, but I do not >> >>> nderstand how to perform the mentioned sorting task. >> >>> Once I am running the PGLS commands such as >> >>> cor.BM <- corBrownian(phy=SteninaeTree) >> >>> pgls <- gls(LOG_rel_Attachment_force_smooth ~ >> >>> LOG_rel_Anzahl_Hafthaare_4.Vordertarsus, correlation = cor.BM, data = >> >>> SteninaeData, method = "ML")" >> >>> I regularly receive the follwing warning note: >> >>> "In Initialize.corPhyl(X[[i]], ...) : >> >>> No covariate specified, species will be taken as ordered in the data >> >>> frame. To avoid this message, specify a covariate containing the >> >>> species names with the 'form' argument." >> >>> Since this is just a warning message, I usually was going on with the >> >>> "summary(pgls)" command, where I was getting my results. >> >>> Anyhow, I was wondering whether it is OK to just ignore this warning >> >>> or whether it means that I will have to sort the sequence of the >> >>> sepcies trait data in my data file (csv) exactly according to the >> >>> sequence of the species as they occur at the tips of my tree file. >> >>> Since the spelling of the species names is exactly the same in both >> >>> the tree file and the data file, I thought that such sorting is >> >>> automatically done by the algorithm? >> >>> I will be glad if you have any hint how ro resove this. >> >>> My best wishes, >> >>> Oliver Betz >> >>> (University of Tübingen, Gernmany) >> >>> _______________________________________________ >>>>> R-sig-phylo mailing list - [ mailto:R-sig-phylo@r-project.org | >> >>> R-sig-phylo@r-project.org ] >>>>> [ https://stat.ethz.ch/mailman/listinfo/r-sig-phylo | >> >>> https://stat.ethz.ch/mailman/listinfo/r-sig-phylo ] >> >>> Searchable archive at >>>>> [ http://www.mail-archive.com/r-sig-phylo@r-project.org/ | >> >>> http://www.mail-archive.com/r-sig-phylo@r-project.org/ ] >> >> -- >> >> ____________________________________________ >> >> Marguerite A. Butler >> >> Professor >> >> Department of Biology >> >> 2538 McCarthy Mall, Edmondson Hall 216 >> >> Honolulu, HI 96822 >> >> Office: 808-956-4713 >> >> Dept: 808-956-8617 >> >> Lab: 808-956-5867 >> >> FAX: 808-956-4745 >> >> [ http://butlerlab.org/ | http://butlerlab.org ] >>>> [ http://manoa.hawaii.edu/biology/people/marguerite-butler | >> >> http://manoa.hawaii.edu/biology/people/marguerite-butler ] >> >> [ http://www2.hawaii.edu/~mbutler | http://www2.hawaii.edu/~mbutler ] >> > -- >> > ____________________________________________ >> > Marguerite A. Butler >> > Professor >> > Department of Biology >> > 2538 McCarthy Mall, Edmondson Hall 216 >> > Honolulu, HI 96822 >> > Office: 808-956-4713 >> > Dept: 808-956-8617 >> > Lab: 808-956-5867 >> > FAX: 808-956-4745 >> > [ http://butlerlab.org/ | http://butlerlab.org ] >>> [ http://manoa.hawaii.edu/biology/people/marguerite-butler | >> > http://manoa.hawaii.edu/biology/people/marguerite-butler ] >> > [ http://www2.hawaii.edu/~mbutler | http://www2.hawaii.edu/~mbutler ] >> > [[alternative HTML version deleted]] >> > _______________________________________________ >>> R-sig-phylo mailing list - [ mailto:R-sig-phylo@r-project.org | >> > R-sig-phylo@r-project.org ] >>> [ https://stat.ethz.ch/mailman/listinfo/r-sig-phylo | >> > https://stat.ethz.ch/mailman/listinfo/r-sig-phylo ] >>> Searchable archive at [ >>> http://www.mail-archive.com/r-sig-phylo@r-project.org/ | >> > http://www.mail-archive.com/r-sig-phylo@r-project.org/ ] > -- > ____________________________________________ > Marguerite A. Butler > Professor > Department of Biology > 2538 McCarthy Mall, Edmondson Hall 216 > Honolulu, HI 96822 > Office: 808-956-4713 > Dept: 808-956-8617 > Lab: 808-956-5867 > FAX: 808-956-4745 > [ http://butlerlab.org/ | http://butlerlab.org ] > [ http://manoa.hawaii.edu/biology/people/marguerite-butler | > http://manoa.hawaii.edu/biology/people/marguerite-butler ] > [ http://www2.hawaii.edu/~mbutler | http://www2.hawaii.edu/~mbutler ] [[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/