how can I create response curves using biomod2? - r

I am using R version 3.6.0. I have made some biomod models. There is one factor among my environmental variables. When I am trying to create response plots this error comes up:
Error in subinfo == "MinMax" :
comparison (1) is possible only for atomic and list types
The code I use is:
SumGBMs <- BIOMOD_LoadModels(WgBiomodModelOut, models='GBM')
SumGBM_repcurves <- response.plot2(model= SumGBMs ,
Data = WgBiomodModelOut,
show.variables= get_formal_data(WgBiomodModelOut, dem),
fixed.var.metric = 'median',
col = c("blue", "red"),
legend = TRUE,
plot=T)
although I don't think that's the problem, but I have tried to use github biomod2:
devtools::install_github("biomodhub/biomod2", dependencies = TRUE)
but it did not work and the following erro0r showed up:
Error: Failed to install 'biomod2' from GitHub:
(converted from warning) installation of package ‘C:/Users/NP/AppData/Local/Temp/RtmpAtIiba/file70c6f00293b/biomod2_3.4-03.tar.gz’ had non-zero exit status
Can you tell me what goes wrong?
Thanks

I think you are not giving to response.plot2 function what it expects.
Data should be a data.frame with the same explanatory variables than the one used in model fitting process
show.variables should be the name of the explanatory viarable you want to display in the response plot
Maybe something like that will work:
SumGBMs <- BIOMOD_LoadModels(WgBiomodModelOut, models='GBM')
SumGBM_repcurves <- response.plot2(model= SumGBMs ,
Data = get_formal_data(WgBiomodModelOut, 'expl.var'),
show.variables= get_formal_data(WgBiomodModelOut, 'expl.var.names'),
fixed.var.metric = 'median',
col = c("blue", "red"),
legend = TRUE,
plot=T)

Related

Using pcaCoda from the package "robCompositions" with ggplot2

I would like to plot the results of the robust PCA (pcaCoDa) from the robCompositions package using ggplot2.
Previously, it worked with ggbiplot (https://github.com/vqv/ggbiplot) however, I can no longer get it to work with my current R version (3.6.0).
Is there a way to do a biplot with the pcaCoda results with ggplot2 using CRAN packages?
Here is a working example without using ggplot:
library(robCompositions)
df <- arcticLake
a <- pcaCoDa(df)
biplot(a)
And another example without using the robust PCA, but using the autoplot function:
library(ggplot2)
autoplot(princomp(df))
However, I would like to use the robust PCA with ggplot/autoplot. When I try to plot it, i get the following error:
autoplot(a)
Error: Objects of type pcaCoDa not supported by autoplot.
I also tried the following and also get an error:
autoplot(a$princompOutputClr)
Error in scale.default(data, center = FALSE, scale = 1/scale) :
length of 'scale' must equal the number of columns of 'x'
Any advice? Thanks!
For some reasons that I ignore pcaCoda returns one value less for scale and center compared to the output of other pca methods such as prcomp or princomp. I think that's the reason why autoplot does not want to plot this object.
Alternatively, if you want to apply the robust algortithm, you can use the package pcaMethods available on bioconductor, here i provided an example using the iris dataset that you can found on the documentation of pcaMethods (https://bioconductor.org/packages/release/bioc/html/pcaMethods.html):
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("pcaMethods")
library(pcaMethods)
library(ggplot2)
robust = pca(iris[c(1, 2, 3, 4)], method = "robustPca", scale = "uv", center = TRUE)
iris = merge(iris, scores(robust), by =0)
ggplot(iris, aes( x= PC1, y = PC2, colour = Species))+
geom_point()+
stat_ellipse()
Does it look what you are trying to get ?

Error message when ploting with "R"

I am attempting to plot my dataset using PCA in R in Windows 10. But while plotting I got the message
zero-length 'labels' specified
Below are the data set and code:
Taxon,L/D1 ,L/D4,L/DL,Di.Pro.,ST,H.Fo/H.Ch
1,3,2.9,2.28,200,2,1
1,1.33,2.3,2.44,225,2,1
1,2,2.52,2.4,150,2,1
1,3.5,2.75,2.32,125,2,1
1,3.18,2.81,2,125,2,1
1,2.35,3,2.82,125,2,1
1,2.72,2.28,2.09,250,2,1
2,3.6,2.82,2.65,125,4,1
2,2.5,2.7,3.02,150,4,1
2,3.2,2.6,2.7,150,4,1
2,2.4,3.3,2.7,200,4,1
2,3,2.87,3.08,175,4,1
3,2,2.83,2,100,1,0.67
3,4,2.66,2.26,100,1,0.67
3,2.33,2.78,2.28,150,1,0.67
3,2,2.83,2.3,100,1,0.67
4,2.83,2.83,2.66,200,1,0.66
4,3.66,2.57,2.27,100,1,0.66
4,3.33,2.9,2.25,150,1,0.66
4,4.33,3.09,2.15,125,1,0.66
4,1.85,2.44,2.43,225,1,0.66
4,2.85,2.57,2.65,175,1,0.66
4,2.8,2.55,2.2,200,1,0.66
4,1.85,2.57,2.04,175,1,0.66
5,2.83,2.5,3,125,3,0.66
5,2.8,4,3.14,200,3,0.66
5,2,3.5,2.4,200,3,0.66
5,3,3.42,2.9,150,3,0.66
5,2.4,2.6,2.71,175,3,0.66
5,4,3.37,2.52,125,3,0.66
5,1.75,2.69,2.9,250,3,0.66
5,2,2.54,2.76,200,3,0.66
5,2.4,3.25,2.46,175,3,0.66
6,3.5,3.14,2.48,75,5,1
6,2.25,2.16,2.1,75,5,1
6,3,3.23,2.77,125,5,1
6,2.5,2.3,1.85,100,5,1
6,2.95,2.71,2.14,100,5,1
6,2.5,3.4,3.09,125,5,1
6,2,2.57,2.3,100,5,1
6,3,2.55,2.46,125,5,1
7,3,4.76,2.94,100,1,1
7,1.66,2.77,2.2,200,1,1
7,2.16,2.8,2.4,250,1,1
8,2.25,3.09,2.83,150,3,1
8,3.5,3.5,2.88,125,3,1
8,3.33,2.87,3.04,150,3,1
library(vegan)
daz= read.csv (file.choose())
cor(daz)
daz.pca<-rda(daz)
summary(daz.pca)
PCA1,2
daz.taxon.pca <- rda(daz[,c(2:7)],scale=T)
par(pty="s")
plot(scores(daz.taxon.pca,display="sites",choices=1),scores(daz.taxon.pca,display="sites",choices=2),type="n",xlab="PCA1 (99.69%)",ylab="PCA2 (0.19%)",xlim=c (-8.00,8.00),ylim=c(-2.00,1.50))
abline(v=0,lty=3)
abline(h=0,lty=3)
arrows(0,0,scores(daz.taxon.pca,display="species",choices=1),scores(daz.taxon.pca,display="species",choices=2),length=0.10,col="dark gray")
text(scores(daz.taxon.pca,display="sites",choices=1),scores(daz.taxon.pca,display="sites",choices=2),as.character(daz$taxon),cex=0.7)
Error in text.default(scores(daz.taxon.pca, display = "sites", choices
= 1), : zero-length 'labels' specified
Any help? I am rather new to R, and failed to find any explanation of this error. The code in general seems fine, except when I add
text(scores(daz.taxon.pca,display="sites",choices=1),scores(daz.taxon.pca,display="sites",choices=2),as.character(daz$taxon),cex=0.7)
for the plot, the error comes up. Any hint would be greatly appreciated.
You use daz$taxon instead of daz$Taxon to access the data in daz variable which return back NULL.
Use the following code at the end of your analysis and it should work fine:
text(scores(daz.taxon.pca, display="sites", choices=1),
scores(daz.taxon.pca, display="sites", choices=2),
as.character(daz$Taxon),
cex=0.7)

ggnet2 : Error: Each variable must be a 1d atomic vector or list

I am trying to use ggnet2 for visualizing a network analysis, but have run into an error with the vignette.
I can generate a random network,
library(ggnet2)
library(network)
library(sna)
library(ggplot2)
net = rgraph(10, mode = "graph", tprob = 0.5)
net = network(net, directed = FALSE)
# vertex names
network.vertex.names(net) = letters[1:10]
With an output that looks reasonable
>net
Network attributes:
vertices = 10
directed = FALSE
hyper = FALSE
loops = FALSE
multiple = FALSE
bipartite = FALSE
total edges= 28
missing edges= 0
non-missing edges= 28
Vertex attribute names:
vertex.names
No edge attributes
However, when I try to run..
ggnet2(net)
I get an error
Error: Each variable must be a 1d atomic vector or list. Problem variables: 'x', 'y', 'xend', 'yend'
I am not clear on how this error is arising in the vignette as net is a list, and all variables within it are lists. I have checked to ensure that I have all the necessary packages and they are up-to-date as well as the most recent R version.
I just tried ggnetwork and seem to get a similar error.
Any thoughts on why this errors is arising?
The error message says
Error: Each variable must be a 1d atomic vector or list. Problem
variables: 'x', 'y', 'xend', 'yend'
If you do the following
library(ggnetwork)
net <- ggnetwork(net)
class(net$x)
you will find that x is a one column matrix. The same is true for your other components. So doing
net$x <- net$x[,1]
net$y <- net$y[,1]
net$xend <- net$xend[,1]
net$yend <- net$yend[,1]
will change these all to 1d atomic vectors and
ggplot(net, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(aes(linetype = "directed"), color = "grey50")
should work. You can read more about how to make ggnetwork work for prettier graphs.
I had the same issue following the tutorial at briatte.github.io/ggnet.
The issue went away after I updated the 'igraph', 'ggplot', 'intergraph', 'GGally' packages and installed the 'ggnetwork' from source:
install.packages("ggnetwork", type="source").
This was suggested here to fix another issue, but somehow it worked for me on this one. Perhaps you could give it a try.
I think I've found a solution for the issue, at least on my machine. I'm running R 3.4.3 on Win 10, but this is probably not platform-related.
Until now I've had the CRAN version of GGally installed, along with network and sna, just as mentioned in the example, and this setup threw the exact same error mentioned above. I was confused as the original network object doesn't even have members like x, xend, etc. It turned out that these are in fact members of the ggnetwork object, which is created invisibly during the command ggnet() (ggnetwork vignette).
So I've tried installing ggnetwork: install.packages("ggnetwork") along with the source version of ggnet
(devtools::install_github("briatte/ggnet")).
ggnetwork was installed succesfully, but the package couldn't be loaded because it said that ggplot2 is not installed (which I've found strange because it's a package that I use on a daily basis as part of the tidyverse library).
ggnet installation failed for some reason related to packages being already loaded.
I've restarted the R session, tried loading ggnetwork again, which again failed for lack of ggplot2, so I installed ggplot2. I tried once again installing ggnet from source, (this time before any packages were loaded) and it worked.
So, to sum it up:
After restarting the R session:
install.packages("ggnetwork")
install.packages("ggplot2")
devtools::install_github("briatte/ggnet")
I don't know exactly which step did it, but afterwards the example script produced a graph plot, just as it was supposed to:
library(ggnetwork)
library(network)
library(sna)
library(ggnet)
net = rgraph(10, mode = "graph", tprob = 0.5)
net = network(net, directed = FALSE)
network.vertex.names(net) = letters[1:10]
ggnet2(net)

r - Add text to each lattice histogram with panel.text but has error "object x is missing"

In the following R code, I try to create 30 histograms for the variable allowed.clean by the factor zip_cpt(which has 30 levels).
For each of these histograms, I also want to add mean and sample size--they need to be calculated for each level of the factor zip_cpt. So I used panel.text to do this.
After I run this code, I had error message inside each histogram which reads "Error using packet 21..."x" is missing, with..." (I am not able to read the whole error message because they don't show up in whole). I guess there's something wrong with the object x. Is it because mean(x) and length(x) don't actually apply to the data at each level of the factor zip_cpt?
I appreciate any help!
histogram(~allowed.clean|zip_cpt,data=cpt.IC_CAB1,
type='density',
nint=100,
breaks=NULL,
layout=c(10,3),
scales= list(y=list(relation="free"),
x=list(relation="free")),
panel=function(x,...) {
mean.values <-mean(x)
sample.n <- length(x)
panel.text(lab=paste("Sample size = ",sample.n))
panel.text(lab=paste("Mean = ",mean.values))
panel.histogram(x,col="pink", ...)
panel.mathdensity(dmath=dnorm, col="black",args=list(mean=mean(x, na.rm = TRUE),sd=sd(x, na.rm = TRUE)), ...)})
A discussion I found online is helpful for adding customized text (e.g., basic statistics) on each of the histograms:
https://stat.ethz.ch/pipermail/r-help/2007-March/126842.html

Creating graphs where the nodes are images gives error cex + offset : non-numeric argument to binary operator

While Running the Below Code
# Sample an adjacency matrix:
set.seed(1)
adj <- matrix(sample(0:1, 10^2, TRUE, prob = c(0.8, 0.2)), 10, 10)
# Run qgraph:
qgraph(adj, images = file, labels = FALSE, borders = FALSE)
## "**Error in cex + offset : non-numeric argument to binary operator**"
While running the above sample code, i get above quoted error i m using R 3.0.0 and qgraph version is latest one i.e qgraph_1.2.2
I need help to work further
Ah it seems I created this bug with the latest update. Will fix it soon. These kind of questions regarding specifically a package are best sent to the package maintainers directly instead of asking them here.

Resources