What does this error mean "order(vertex_attr(g, measure), decreasing = TRUE) : argument 1 is not a vector" in R? - r

I am trying to calculate robustness, a graph theory measure using R (braingraph package).
Robustness = robustness(my_networkgraph, type = c("vertex"), measure = ("btwn.cent"))
I get the following error, when I use the above robustness function:
Error in order(vertex_attr(g, measure), decreasing = TRUE) : argument 1 is not a vector
Any idea, what I am doing wrong here?
My network, which is a matrix has been converted to igraph object and robustness was calculated.
My network as a matrix:
mynetwork <- matrix(c(0, 1, 0, 1, 0, 0, 0, 0,
1, 0, 1, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 1, 1, 0, 1, 1,
0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0), nrow = 8)
This matrix was converted as igraph using the following code:
my_networkgraph <-graph_from_adjacency_matrix(mynetwork, mode = c("undirected"),weighted = NULL, diag = TRUE, add.colnames = NULL, add.rownames = NA)
Please help me to understand the above error
Thanks
Priya

There was a bug in the above function. To run the robustness code, you will need to supply a vertex attribute to your network: V(network)$degree <- degree(network) V(network)$btwn.cent <- centr_betw(network)$res

Related

ICC unused argument

I need to calculate the 95% CI of iCC.
I'm using this code:
icc(mydata[,c(1,2)], model = "twoway",type = "agreement", unit = "average")
I obtain this error message.
Error in icc(mydata[, c(1, 1)], model = "twoway", type = "agreement", :
unused arguments (model = "twoway", type = "agreement", unit = "average")
My data:
mydata= data.frame(A=c(0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 3), B=c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 2))
How can I fix it?
Thank you!
You might have competing packages loaded that both use icc(). The psych package and the irr package both have this as a function name. If both are loaded, the psych package is likely masking the irr command. Those are not valid arguments for psych::icc() but they are for irr::icc()
Try:
irr:: icc(mydata[,c(1,2)], model = "twoway",type = "agreement", unit = "average")

R -- Error when selecting subset of predictors for an imputation model in mice package

I am attempting to create a multiple imputation model using the mice package in R. Here are some details about what I am specifically trying to do, and below is a subset of the data I am working with, the code I have tried, and the error that I am getting.
In the dataset, labelled 'mturk.all', there are ~300 variables and around 1300 cases. For the multiple imputation model, I am trying to use only 33 of the ~300 variables; these 33 variables are dichotomous (coded as 0 and 1 in the dataset).
Following the mice code provided at https://stefvanbuuren.name/fimd/sec-toomany.html (see section 9.1.6 on the linked website), I have tried the following code, which resulted in an error (also shown below):
>library(mice)
>pred <- quickpred(mturk.all, mincor = .1, minpuc = 0, inc=c("TSHS_1R", "TSHS_2R", "TSHS_3R", "TSHS_4R", "TSHS_5R", "TSHS_6R", "TSHS_7R", "TSHS_8R", "TSHS_9R", "TSHS_10R", "TSHS_11R", "TSHS_12R", "TSHS_13R", "TSHS_14R", "TSHS_15R", "TSHS_16R", "TSHS_17R", "TSHS_18R", "TSHS_19R", "TSHS_20R", "TSHS_21R", "TSHS_22R", "TSHS_23R", "TSHS_24R", "TSHS_25R", "TSHS_26R", "TSHS_27R", "TSHS_28R", "TSHS_29R", "TSHS_30R", "TSHS_31R", "TSHS_32R", "TSHS_33R"))
There were 14 warnings (use warnings() to see them)
> warnings()
Warning messages:
1: In data.matrix(data) : NAs introduced by coercion
2: In data.matrix(data) : NAs introduced by coercion
3: In data.matrix(data) : NAs introduced by coercion
4: In data.matrix(data) : NAs introduced by coercion
5: In data.matrix(data) : NAs introduced by coercion
6: In data.matrix(data) : NAs introduced by coercion
7: In data.matrix(data) : NAs introduced by coercion
8: In data.matrix(data) : NAs introduced by coercion
9: In data.matrix(data) : NAs introduced by coercion
10: In data.matrix(data) : NAs introduced by coercion
11: In data.matrix(data) : NAs introduced by coercion
12: In data.matrix(data) : NAs introduced by coercion
13: In data.matrix(data) : NAs introduced by coercion
14: In data.matrix(data) : NAs introduced by coercion
>mturk.all.imp <- mice(mturk.all, m = 40, method = 'logreg', pred = pred)
Error in parse(text = x, keep.source = FALSE) :
<text>:1:1: unexpected '<'
1: <
^
Alternatively, I tried the following:
>inlist <- mturk.all[c("TSHS_1R", "TSHS_2R", "TSHS_3R", "TSHS_4R", "TSHS_5R", "TSHS_6R", "TSHS_7R", "TSHS_8R", "TSHS_9R", "TSHS_10R", "TSHS_11R", "TSHS_12R", "TSHS_13R", "TSHS_14R", "TSHS_15R", "TSHS_16R", "TSHS_17R", "TSHS_18R", "TSHS_19R", "TSHS_20R", "TSHS_21R", "TSHS_22R", "TSHS_23R", "TSHS_24R", "TSHS_25R", "TSHS_26R", "TSHS_27R", "TSHS_28R", "TSHS_29R", "TSHS_30R", "TSHS_31R", "TSHS_32R", "TSHS_33R")]
>pred <- quickpred(mturk.all, mincor = .1, minpuc = 0, inc=inlist)
>mturk.all.imp <- mice(mturk.all, m = 40, method = 'logreg', pred = pred)
Error in parse(text = x, keep.source = FALSE) :
<text>:1:1: unexpected '<'
1: <
^
I have also switched out the 'logreg' imputation method with 'pmm' and received the same error message.
Here is a subset of the dataset, mturk.all, and the version of R Studio I am using, plus the version of mice I am using.
> dput(mturk.all[425:434, 1:33])
structure(list(TSHS_1R = c(0, 1, 1, 0, 0, 0, 1, 1, 0, 1), TSHS_2R = c(0,
0, 0, 1, 0, 0, 0, 0, 0, 0), TSHS_3R = c(0, 1, 0, 0, 0, 0, 0,
0, 1, 0), TSHS_4R = c(0, 1, 0, 0, 0, 0, 1, 0, 1, 0), TSHS_5R = c(0,
0, 0, 1, 0, 0, 0, 0, 1, 0), TSHS_6R = c(0, 0, NA, NA, 0, 0, 1,
0, 0, 0), TSHS_7R = c(0, 0, 0, 1, 0, 1, 1, 0, 0, 0), TSHS_8R = c(1,
1, 0, 0, 1, 1, 1, 1, 0, 1), TSHS_9R = c(0, 0, 0, 0, 0, 0, 0,
1, 1, 0), TSHS_10R = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), TSHS_11R = c(0,
0, NA, 0, 0, 0, 0, 1, 0, 0), TSHS_12R = c(1, 0, 1, 0, 1, 0, 0,
0, 0, 0), TSHS_13R = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 1), TSHS_14R = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), TSHS_15R = c(0, 0, 1, 0, 0, 0, 0,
0, 0, 0), TSHS_16R = c(0, 0, 1, 0, 0, 0, 0, 0, 0, 0), TSHS_17R = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), TSHS_18R = c(0, 0, 0, 0, 1, 0, 0,
0, 0, 0), TSHS_19R = c(0, 0, 0, NA, 0, 0, 0, 0, 0, 0), TSHS_20R = c(0,
0, 0, 1, 0, 0, 0, 0, 0, 0), TSHS_21R = c(0, 0, 0, 0, 1, 0, 0,
0, 0, 0), TSHS_22R = c(0, 1, 0, 0, NA, 1, 0, 0, 0, 0), TSHS_23R = c(0,
0, 0, 0, NA, 1, 0, 0, 0, 1), TSHS_24R = c(0, 0, 0, 1, NA, 1,
0, 1, 1, 0), TSHS_25R = c(1, 1, 1, 0, 1, 1, 1, 1, 1, 1), TSHS_26R = c(1,
0, 1, 0, 0, 0, 0, 1, 0, 1), TSHS_27R = c(1, 0, 0, 1, 0, 1, 1,
0, 0, 1), TSHS_28R = c(0, 0, 0, 0, 0, 0, 0, 0, 1, 0), TSHS_29R = c(1,
0, 0, 0, 1, 0, 0, 0, 0, 0), TSHS_30R = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0), TSHS_31R = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), TSHS_32R = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), TSHS_33R = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0)), row.names = c(NA, -10L), class = c("tbl_df", "tbl",
"data.frame"))
> rstudioapi::versionInfo()
$`citation`
To cite RStudio in publications use:
RStudio Team (2018). RStudio: Integrated Development for R. RStudio, Inc., Boston, MA URL http://www.rstudio.com/.
A BibTeX entry for LaTeX users is
#Manual{,
title = {RStudio: Integrated Development Environment for R},
author = {{RStudio Team}},
organization = {RStudio, Inc.},
address = {Boston, MA},
year = {2018},
url = {http://www.rstudio.com/},
}
$`mode`
[1] "desktop"
$version
[1] ‘1.2.1335’
> packageVersion("mice")
[1] ‘3.8.0’
Any help identifying what I am doing wrong in the code would be appreciated!
Ian
There are some you may want to try:
Try traceback() to see more recent calls for further troubleshoot.
Try updating your R version and the mice package.
Try with different inputs.
I think you can try as.data.frame(mturk.all) since the quickpredand mice may only takes in dataframe and matrix.
Sorry for the late reply, I cannot reproduce the problem. A few more suggestion I have;
Check your encoding of the data file. You should save with UTF-8 encoding (with notpad++,...).
Also check the data for any error.
Open issue in the package repository, you may get an answer there.

lapply Question: How to streamline further without generating errors

I'm looking to condense the steps in my script, but I'm having issues with lapply(). It looks to be an issue with my code as usual. Any help would be much appreciated!
library(iNEXT)
sa4 <- list(Bird = list(structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1,
0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0,
0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0), .Dim = c(26L,
6L), .Dimnames = list(Scientific_name = c(" Pycnonotus plumosus",
"Acridotheres javanicus", "Aegithina tiphia", "Aethopyga siparaja",
"Anthreptes malacensis", "Aplonis panayensis", "Cacatua goffiniana",
"Callosciurus notatus", "Cinnyris jugularis", "Copsychus malabaricus",
"Copsychus saularis", "Dicaeum cruentatum", "Dicrurus paradiseus",
"Gorsachius melanolophus", "Larvivora cyane", "Macronus gularis",
"Oriolus chinensis", "Orthotomus atrogularis", "Otus lempiji",
"Pitta moluccensis", "Pycnonotus goiavier", "Pycnonotus plumosus",
"Pycnonotus zeylanicus", "Spilopelia chinensis", "Todiramphus chloris",
"Zosterops simplex"), Sampling_Point = c("SA_01", "SA_02", "SA_03",
"SA_04", "SA_05", "SA_06")))), Butterfly = list(structure(c(0,
0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0,
0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0), .Dim = c(10L,
4L), .Dimnames = list(Scientific_name = c("Burara harisa consobrina",
"Catopsilia pyranthe pyranthe", "Catopsilia scylla cornelia",
"Delias hyparete metarete", "Eurema sp", "Idea leuconoe clara",
"Pachliopta aristolochiae asteris", "Phalanta phalantha phalantha",
"Troides helena cerberus", "Zizula hylax pygmaea"), Sampling_Point = c("SA_01",
"SA_02", "SA_04", "SA_06")))), Mammal = list(structure(c(0, 1,
1, 1, 1, 0), .Dim = 2:3, .Dimnames = list(Scientific_name = c("Callosciurus notatus",
"Unidentified Fruit Bat sp"), Sampling_Point = c("SA_03", "SA_04",
"SA_05")))), Reptile = list(structure(1, .Dim = c(1L, 1L), .Dimnames = list(
Scientific_name = "Hemidactylus frenatus", Sampling_Point = "SA_05"))))
I've been doing it the longer way:
estimateD(sa4$Butterfly, datatype="incidence_raw") #Sampling coverage for butterflies
estimateD(sa4$Mammal, datatype="incidence_raw") #Sampling coverage for mammals
estimateD(sa4$Bird, datatype="incidence_raw") #Sampling coverage for birds
estimateD(sa4$Reptile, datatype="incidence_raw") #Sampling coverage for reptiles
Note that estimateD(sa4$Reptile, datatype="incidence_raw" generates an error since it only has one species.
Is it possible to condense the following steps via lapply? In this situation I've only have 4 taxa, but for other projects, it might be a lot more. I tried the following and it gives me a warning message--which actually is the same warning message as the one above. I'm wondering if lapply stops working if one component gives an error?
> (lapply(sa4, function(x) estimateD(x, datatype="incidence_raw")) )
Error in `[.data.frame`(tmp, , c(1, 2, 3, 7, 4, 5, 6)) :
undefined columns selected
In addition: Warning messages:
1: In FUN(X[[i]], ...) :
Invalid data type, the element of species by sites presence-absence matrix should be 0 or 1. Set nonzero elements as 1.
2: In log(b/Ub) : NaNs produced
Please let me know if I need to provide more information? Thank you!
This is a simple error trapping issue. Wrap tryCatcharound your problem function call and have the error function return information on what happened.
results <- lapply(sa4, function(x) {
tryCatch(estimateD(x, datatype="incidence_raw"),
error = function(e) e)
})
Now determine which ran alright.
ok <- !sapply(results, inherits, "error")
ok
# Bird Butterfly Mammal Reptile
# TRUE TRUE TRUE FALSE
And check those that did.
results[ok]
It is the issue with the 'Reptiles', so if we select the first 3 elements of the list, it should work
lapply(sa4[1:3], function(x) estimateD(x, datatype="incidence_raw"))

Planned Contrasts on glmmTMB

Apologies if this is a repeat question. Many have posted looking looking for a way to do post-hoc analyses on the conditional model (fixed factors) in glmmTMB. I want to do plannned contrasts between certain groups, not test every pairwise comparison (e.g. Tukey).
The code below worked well on nlme:lme for a lmm. However, it returns an error on the code below.
Error in modelparm.default(model, ...) :
dimensions of coefficients and covariance matrix don't match
Is there a way to do planned contrasts on a glmmTMB?
#filtdens is a dataframe and TRT,DATE,BURN,VEG are factors
filtdens <- merged %>% filter(!BLOCK %in% c("JB2","JB4","JB5") & MEAS =="DENS" &
group == "TOT" & BURN == "N" & VEG == "C")
filtdens$TD <- interaction(filtdens$TRT, filtdens$DATE)
mod2 <- glmmTMB(count~(TD)+(1|BLOCK),
data=filtdens,
zi=~1,
family=nbinom1(link = "log"))
k1 <- matrix(c(0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, -1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, -1, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, -1, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, -1, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, -1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, -1, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, -1, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 1), byrow = T, ncol = 12)
summary(glht(mod2, linfct=k1),test=adjusted("bonferroni"))
A reproducible example would be helpful, but: this vignette in the development version offers code that ought to enable multcomp::linfct, i.e.:
glht_glmmTMB <- function (model, ..., component="cond") {
glht(model, ...,
coef. = function(x) fixef(x)[[component]],
vcov. = function(x) vcov(x)[[component]],
df = NULL)
}
modelparm.glmmTMB <- function (model,
coef. = function(x) fixef(x)[[component]],
vcov. = function(x) vcov(x)[[component]],
df = NULL, component="cond", ...) {
multcomp:::modelparm.default(model, coef. = coef., vcov. = vcov.,
df = df, ...)
}
Test (this example is with Tukey, but I don't see why it shouldn't work more generally ...)
library(glmmTMB)
data("cbpp",package="lme4")
cbpp_b1 <- glmmTMB(incidence/size~period+(1|herd),
weights=size,family=binomial,
data=cbpp)
g1 <- glht(cbpp_b1, linfct = mcp(period = "Tukey"))
summary(g1)
This works with the current CRAN version, but the current development version of glmmTMB offers more options (e.g. emmeans(); see the above-linked vignette). You'll need to install via devtools::install_github("glmmTMB/glmmTMB/glmmTMB") (you'll need compilation tools installed as well).

Using car::Anova package for a doubly-multivariate MANOVA in R

I'm trying to run a repeated-measures MANOVA in R, which also contains a number of dependent variables (key outcome variables of behavioural tasks). The repeated-measures are due to a cross-over design, in which individuals took a drug and placebo (in randomised order).
The code I'm running looks like this:
imatrix <- matrix(c(
1, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, -1,
0, 1, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, -1,
0, 0, 1, 0, 0, 0, 1,
0, 0, 1, 0, 0, 0, -1,
0, 0, 0, 1, 0, 0, 1,
0, 0, 0, 1, 0, 0, -1,
0, 0, 0, 0, 1, 0, 1,
0, 0, 0, 0, 1, 0, -1,
0, 0, 0, 0, 0, 1, 1,
0, 0, 0, 0, 0, 1, -1
), 12, 7, byrow=TRUE)
colnames(imatrix) <- c("BCST", "CGT", "AST", "AGN", "DDT", "FERT", "NAC")
(imatrix <- list(measure=imatrix[,1:6], condition=imatrix[,7]))
contrasts(condition_factor) <- matrix(c(-1,1,1, -1), ncol=2)
doubly.mod<-lm(cbind(bcst_nac$totPersErr,bcst_placebo$totPersErr,cantab_nac$CGT.Delay.aversion,cantab_placebo$CGT.Delay.aversion,cantab_nac$AST.Switching.cost..Mean..correct.,cantab_placebo$AST.Switching.cost..Mean..correct.,cantab_nac$AGN.Affective.response.bias..Mean.,cantab_placebo$AGN.Affective.response.bias..Mean.,aucs_NAC,aucs_placebo,fert_nac$FERTACCHA,fert_placebo$FERTACCHA)~1))
Manova(doubly.mod, imatrix=imatrix, type =3)
The result is this error: Error in Anova.III.mlm(mod, SSPE, error.df, idata, idesign, icontrasts, :
(list) object cannot be coerced to type 'double'
However, when I change imatrix back from a list to a matrix, I get this error response:
Error in do.call(cbind, imatrix) : second argument must be a list
I've based this off the example from the car::Anova package about doubly multivariate analyses. Please let me know if you can help, or if I can add anything to make this question clearer.

Resources