0

I'm trying to run a distance decay model on beta diversity data quantified using betapart::beta.pair (v1.5.6) and elevational distance calculated from a data frame of elevation data (in m) using vegan::dist (v2.5-7). I'm trying to use betapart::decay.model but I'm getting the following error message:

Error in x[1:6, , drop = FALSE] : incorrect number of dimensions

My data are both dist objects with the same dimensions so I'm a bit perplexed why this error message is popping up.

Is this an issue with the current betapart version?

Updated with reproducible example:

# dissimilarity matrix    
r1 = c(NA, 0.2558140, 0.5675676, 0.4594595, 0.3636364)
r2 = c(0.2558140, NA, 0.5789474, 0.4736842, 0.4222222)
r3 = c(0.5675676, 0.5789474, NA, 0.6875000, 0.6410256)
r4 = c(0.4594595, 0.4736842, 0.6875000, NA, 0.5384615)
r5 = c(0.3636364, 0.4222222, 0.6410256, 0.5384615, NA)
        
dissim_mat = rbind(r1,r2,r3,r4,r5)
rownames(dissim_mat) = c("s1","s2","s3","s4","s5")
colnames(dissim_mat) = c("s1","s2","s3","s4","s5")
dissim_mat = vegan::as.dist(dissim_mat)

# elevational distance
r1 = c(NA,10,50,100,90)
r2 = c(10,NA,40,90,80)
r3 = c(50,40,NA,50,40)
r4 = c(100,90,50,NA,10)
r5 = c(0,80,40,10,NA)

elev_mat = rbind(r1,r2,r3,r4,r5)
rownames(elev_mat) = c("s1","s2","s3","s4","s5")
colnames(elev_mat) = c("s1","s2","s3","s4","s5")

elev_mat = vegan::as.dist(elev_mat)

# run distance decay model
test <- betapart::decay.model(y = dissim_mat,
                    x = elev_mat,
                    model.type="power",
                    y.type="dissim",
                    perm=10
                    )

From that I get the above error message.

Crow
  • 11
  • 4

3 Answers3

0

I am having the same issue. It seems to be caused by loading certain packages (in my case elevatr and sf). If you load one of these packages first and run the example from the manual (https://rdrr.io/cran/betapart/man/decay.model.html) it recreates the error:

library(betapart)
library(elevatr) #Loading this package seems to cause the error

# presence/absence tables for longhorn beetles of South and North Europe
data(ceram.s)
data(ceram.n)

# spatial coordinates of territories in South and North Europe
data(coords.s)
data(coords.n)

# dissimilarity matrices
ceram.s.sim<-beta.pair(ceram.s)$beta.sim
ceram.n.sim<-beta.pair(ceram.n)$beta.sim

# spatial distances in km
distgeo.s<-dist(coords.s[,1:2])
distgeo.n<-dist(coords.n[,1:2])

# Negative exponential models for the decay of similarity with spatial distance
decay.south<-betapart::decay.model(y=as.dist(1-ceram.s.sim), x=as.dist(distgeo.s), y.type="sim", model.type="exp")
decay.north<-decay.model(y=1-ceram.n.sim, x=distgeo.n, y.type="sim", model.type="exp")

I am unaware of a solution to this problem.

0

Ran into the same trouble so posting here how I solved it. May be useful for someone in future.

Solved it by detaching all loaded packages, and then re-running the betapart::decay.model().

Used this excellent function to detach all packages:

detachAllPackages <- function() {

  basic.packages <- c("package:stats","package:graphics","package:grDevices","package:utils","package:datasets","package:methods","package:base")

  package.list <- search()[ifelse(unlist(gregexpr("package:",search()))==1,TRUE,FALSE)]

  package.list <- setdiff(package.list,basic.packages)

  if (length(package.list)>0)  for (package in package.list) detach(package, character.only=TRUE)

}

detachAllPackages()

Mansi
  • 133
  • 2
  • 11
0

Alternative explanation to the same error :

"Error in x[i, , drop = FALSE] : incorrect number of dimensions"

The function betapart::decay.model() depends on the package minpack.lm to compute the nonlinear distance-decay model (check the betapart package manual https://cran.r-project.org/web/packages/betapart/betapart.pdf)

In my case, the install.packages("betapart") did not also install the dependent package minpack.lm, and I got your error message.

If this is your case, the error should be solve by installing the required package with: install.packages("minpack.lm")

If beta.pair() still yields the same error you should consider the previous answers and remove any unnecessary packages before running the function decay.model().

Cheers !

Dodo
  • 1
  • 2