# Matrix and array for Null Models

Hi, I'm following the example given for the nullmodel function: R: Generates null models for network analysis

Example:

``````data(Safariland)
nullmodel(Safariland, N=2, method=1)
nullmodel(Safariland>0, N=2, method=4)

# analysis example:
obs <- unlist(networklevel(Safariland, index="weighted nestedness"))
nulls <- nullmodel(Safariland, N=100, method=1)
null <- unlist(sapply(nulls, networklevel, index="weighted nestedness")) #takes a while ...

plot(density(null), xlim=c(min(obs, min(null)), max(obs, max(null))),
main="comparison of observed with null model Patefield")
abline(v=obs, col="red", lwd=2)

praw <- sum(null>obs) / length(null)
ifelse(praw > 0.5, 1-praw, praw)    # P-value

# comparison of null model 4 and 5 for binary:
nulls4 <- nullmodel(Safariland>0, N=100, method=4)
nulls5 <- nullmodel(Safariland>0, N=100, method=5)
null4 <- unlist(sapply(nulls4, networklevel, index="weighted nestedness"))
null5 <- unlist(sapply(nulls5, networklevel, index="weighted nestedness"))

plot(density(null4), xlim=range(c(null4, null5)), lwd=2,
main="comparison of null models")
lines(density(null5), col="red", lwd=2)
legend("topright", c("shuffle", "mgen"), col=c("black", "red"), lwd=c(2,2),
bty="n", cex=1.5)
abline(v=networklevel(Safariland>0, index="weighted nestedness"))
``````

The data Safariland has a class "matrix" and "array". I imported a matrix of presence and absence, and the class is "tbl_df", "tbl" and "data.frame". Is there a function I can use to convert the data to "matrix" and "array"?

class(MatrixCen)

class(MatrixCen)
[1] "tbl_df" "tbl" "data.frame"

nullmodel(MatrixCen, N=2, method=1)
Error in rowSums(web) : 'x' must be numeric

Thank you.

``````library(tibble)

(a_tib <- tibble(a=1:2,
b=3:4))
class(a_tib)

(a_mat <- as.matrix(a_tib))
class(a_mat)``````

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.