Phylo correlogram in R bootstrapping error? - r

I am trying to create a phylo correlogram based on my data using phyloCorrelogram from the phylosignal package in order to test for presence of a phylogenetic signal. My data is in the so-called phylo4d format and is called tree.
Now, when I run phyloCorrelogram(tree), I am returned the following error:
library("phylobase")
library("ape")
library("phylosignal") # contains phyloCorrelogram()
> phyloCorrelogram(tree, ci.bs = 10, n.points = 10)
Error in boot::boot(X, function(x, z) moranTest(xr = x[z], Wr = prop.table(Wi[z, :
no data in call to 'boot'
I have already done an elaborate search on the internet to look for ways how to solve this issue, but without success.
Since the dimension of the data that I am using is too large to post it here, I have posted my data in .rda format on dropbox.
Does anyone know the flaw?

You have no data associated to your tree. Your tree is a phylo4d object which has the "tree" information but no data attached to it. You need something like that
library(phylobase)
g1 <- as(geospiza_raw$tree, "phylo4")
geodata <- geospiza_raw$data
g2 <- phylo4d(g1, geodata)
pc <- phyloCorrelogram(g2)

Related

Extract data from signac RegionMatrix?

I'm trying to extract the data matrix computed by Signac r package when running RegionMatrix(). I know I can plot the data with RegionHeatmap(), but I want to plot the data with another package.
So, if I created a region matrix like this:
my_object <- RegionMatrix(my_object
, key = "my_region_matrix"
, regions = StringToGRanges(top_cluster_genes$gene)
, upstream = 2500, downstream = 2500)
How do I go about extracting the data stored within the object under the key "my_region_matrix"? I know it's a pretty basic question, but I've been browsing the returned object and can't find any assay/matrix with that key name. (I'm putting this under Seurat because apparently there is no tag for Signac and the object is a Seurat object anyway)

R Convert egor object to igraph/ use the visualization app (error: Duplicate vertex names)

I want to analyse egocentric networks using R's egor package. This package includes the egor's Network Visualization App - short egor_vis_app - (which uses igraph). I managed to create an egor object but I can't use this app to visualize the networks (error Duplicate vertex names) OR create an igraph objext (as_igraph(), same error). What am I doing wrong?
What I tried so far:
I used the pre-existing egor object (data("egor32")) and the visualization app worked.
Then, I used this pre-existing data to create an egor object:
data("alters32")
data("egos32")
data("edges32")
e <- egor(alters.df = alters32,
egos.df = egos32,
aaties = edges32,
ID.vars = list(
ego = "egoID",
alter = "alterID",
source = "Source",
target = "Target"))
and the app as well as the as_igraph(e) function
don't work (I followed this tutorial when creating the egor object with this data).
And this is my sample code (based on this):
df_new <- read.csv(textConnection('"id","numgiven","sex",
"sex1","sex2","sex3","sex4","sex5","close12",
"close13","close14","close15","close23","close24",
"close25","close34","close35","close45"
10,6,1,2,2,1,2,2,0,0,0,0,0,0,0,1,1,1
36,6,2,2,2,2,1,1,0,0,0,1,0,0,0,1,0,0'
), as.is=TRUE)
e1 <- with(df_new, onefile_to_egor(egos = df_new, pmin(numgiven,5),
ID.vars=list(ego="id"),
attr.start.col="sex1",
attr.end.col="sex5",
max.alters=5,
aa.first.var="close12",
aa.regex="^(?<attr>[[:alpha:]]+)(?<src>[[:digit:]])(?<tgt>[[:digit:]])$"))
Doesn't work either.
I am one of the developers of egor, and while I am aware of this problem and there are plans to fix it in upcoming versions, there's a way to fix this yourself, by reordering the alter data columns. The problem is that igraph expects the alter IDs or vertex names to be the first column in the dataframe listing the alters/vertices.
Here is how you can fix your egor object:
library(dplyr)
e1$.alts <- lapply(e1$.alts, function(x) select(x, .altID, everything()))
With this fix in place the as_igraph() function and the visualization app should work.

Creating a compartive object in R from two dataframes for comparitive phylogenetics

I'm trying to read in two dataframes into a comparitive object so I can plot them using pgls.
I'm not sure what the error being returned means, and how to go about getting rid of it.
My code:
library(ape)
library(geiger)
library(caper)
taxatree <- read.nexus("taxonomyforzeldospecies.nex")
LWEVIYRcombodata <- read.csv("LWEVIYR.csv")
LWEVIYRcombodataPGLS <-data.frame(LWEVIYRcombodata$Sum.of.percentage,OGT=LWEVIYRcombodata$OGT, Species=LWEVIYRcombodata$Species)
comp.dat <- comparative.data(taxatree, LWEVIYRcombodataPGLS, "Species")
Returns error:
> comp.dat <- comparative.data(taxatree, LWEVIYRcombodataPGLS, 'Species')
Error in if (tabulate(phy$edge[, 1])[ntips + 1] > 2) FALSE else TRUE :
missing value where TRUE/FALSE needed
This might come from your data set and your phylogeny having some discrepancies that comparative.data struggles to handle (by the look of the error message).
You can try cleaning both the data set and the tree using dispRity::clean.data:
library(dispRity)
## Reading the data
taxatree <- read.nexus("taxonomyforzeldospecies.nex")
LWEVIYRcombodata <- read.csv("LWEVIYR.csv")
LWEVIYRcombodataPGLS <- data.frame(LWEVIYRcombodata$Sum.of.percentage,OGT=LWEVIYRcombodata$OGT, Species=LWEVIYRcombodata$Species)
## Cleaning the data
cleaned_data <- clean.data(LWEVIYRcombodataPGLS, taxatree)
## Preparing the comparative data object
comp.dat <- comparative.data(cleaned_data$tree, cleaned_data$data, "Species")
However, as #MrFlick suggests, it's hard to know if that solves the problem without a reproducible example.
The error here is that I was using a nexus file, although ?comparitive.data does not specify which phylo objects it should use, newick trees seem to work fine, whereas nexus files do not.

R not recognizing Excel cells populated with Bloomberg API code

I have built a dynamically updating spreadsheet in Excel with BBG addin that pulls price data using BBG API. I am trying to pull a table from that sheet into R and create a simple scatterplot using the below code:
wb <- loadWorkbook("Fx Vol Framework.xlsx")
data <- readWorksheet(wb,sheet = "Carry", region = "AL40:AN68",header=TRUE, rownames = 1)
plot(data,ylim = c(-2,12))
with(data,text(data, labels = row.names(data), pos = 1))
reg1 <- lm(data[,2]~data[,1])
abline(reg1)
The region I am calling (AL40:AN68) is populated with results from an HLOOKUP formula that calls from cells with BBG API. When I run the code, I get the below error (repeats the same error text for each cell):
There were 50 or more warnings (use warnings() to see the first 50)
> warnings()
Warning messages:
1: Error when trying to evaluate cell AM41 - Name '_xll.BDP' is completely unknown in the current workbook
If I go back to the excel sheet and populate that same region AL40:AN68 with numeric values (copy -> paste values), save the workbook, and run the same code, I get the scatterplot I was expecting with the original code. Is there any way for me to get the scatterplot using the cells with Bloomberg API or do I need to run it with simple numeric values? Do I need the Bbg package for this to work? Thank you.
A simpler approach for other users now might be to use the Rblpapi package from CRAN to connect to the Bloomberg API directly.
I'm not familiar with BBG add in, but it seems like after calling readWorksheet you would want to call a function that actually opens the workbook. I think that would in a sense "complete the binding". At any rate I sometimes need to pass data between R and excel. Here is how I'd tackle the problem using the package RDCOMClient.
R Code:
library(RDCOMClient)
exB <- COMCreate("Excel.Application")
book <- exB$Workbooks()$Open("C:/'the right directory/exp.xlsx'")
dNames <- book$Worksheets("Sheet1")$Range("AL40:AN40")
dValues <- book$Worksheets("Sheet1")$Range("AL41:AN68")
dNames <- unlist(dNames[["Value"]])
dValues <- unlist(dValues[["Value"]])
data1 <- matrix(dValues,ncol=3)
colnames(data1) <- dNames
data1 <- as.data.frame(data1)
plot(data1$v1,data1$v2)
Obviously you can plot things, model things or whatever in a number of ways, but this gets things into R which is probably the best place for it. At any rate there is also a good introduction to using the RDCOMClient package to connect R and Excel for quick data tasks at http://www.omegahat.org/RDCOMClient/Docs/introduction.html

Convert igraph object to a data frame in R

I'm working with the iGraph library and I need to run some statistical analysis on the network. I'm computing several variables using iGraph and then want to use those indicators as the dependent variable in a few regressions and the vertex attributes as the independent variables in the model.
So, I'm able to load the data, run the igraph analysis, but I'm having trouble turning the igraph object back into a data frame. I don't really need the edges to be preserved, just each vertex to be turned into an observation with the attributes serving as a column in each row.
I tried the following:
fg <- fastgreedy.community(uncompg, merges=TRUE)
z<-which.max(fg$modularity)
fgc<- community.to.membership(uncompg, fg$merges,z)
names<-array(V(uncompg)$name)
fccommunity<-array(fgc$membership)
fcresult<-as.matrix(cbind(names,fccommunity))
compg <- set.vertex.attribute(compg, "community", value=fccommunity)
uncompg<-simplify(as.undirected(compg))
hubscore<-hub.score(compg)$vector
authscore<-authority.score(compg)$vector
netdata<-as.data.frame(compg)
But it throws the following error:
cannot coerce class '"igraph"' into a data.frame
Any help or pointers would be greatly appreciated.
I am not quite sure what you are trying to do. Do you want the relationships as a data frame, or the node attribute as a data frame?
To do the former:
> compg.edges <- as.data.frame(get.edgelist(compg))
To do the latter:
> compg.df <- as.data.frame(list(Vertex=V(compg), Community=fccommunity, Hubscore=hubscore, Authscore=authscore), stringsAsFactors=FALSE)

Resources