Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mahalanobis Distance #4

Open
cdueben opened this issue Aug 18, 2020 · 4 comments
Open

Mahalanobis Distance #4

cdueben opened this issue Aug 18, 2020 · 4 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@cdueben
Copy link

cdueben commented Aug 18, 2020

transition() produces an error in the RasterBrick case.

x <- raster(matrix(data = c(15, 20, 7, 12, 13, 18, 6, 5, 1, 21, 3, 15, 22, 17, 12, 14), nrow = 4, ncol = 4, byrow = T))
y <- raster(matrix(data = c(690, 623, 673, 442, 750, 620, 680, 491, 467, 489, 624, 590, 552, 605, 727, 462), nrow = 4, ncol = 4, byrow = T))
x <- brick(x, y)
z <- transition(x, transitionFunction = "mahal", directions = 8)
Error in h(simpleError(msg, call)) :
  error in evaluating the argument 'x' in selecting a method for function 'nrow': error in evaluating the argument 'x' in selecting a method for function 'transitionMatrix': invalid class “TransitionData” object: FALSE

It is linked to line 324 in transition.R.

directions <- 8
x <- raster(matrix(data = c(15, 20, 7, 12, 13, 18, 6, 5, 1, 21, 3, 15, 22, 17, 12, 14), nrow = 4, ncol = 4, byrow = T))
y <- raster(matrix(data = c(690, 623, 673, 442, 750, 620, 680, 491, 467, 489, 624, 590, 552, 605, 727, 462), nrow = 4, ncol = 4, byrow = T))
x <- brick(x, y)
xy <- cbind(1:ncell(x),getValues(x))
xy <- na.omit(xy)
dataCells <- xy[,1]
adj <- adjacent(x, cells=dataCells, pairs=TRUE, target=dataCells, directions=directions)
x.minus.y <- raster::getValues(x)[adj[,1],] - raster::getValues(x)[adj[,2],]
cov.inv <- solve(cov(xy[,-1]))
mahaldistance <- apply(x.minus.y,1,function(x){sqrt((x%*%cov.inv)%*%x)})
mahaldistance <- mean(mahaldistance)/(mahaldistance+mean(mahaldistance))
transitiondsC <- new("dsCMatrix", p = as.integer(rep(0,ncell(x)+1)), Dim = as.integer(c(ncell(x),ncell(x))), Dimnames = list(as.character(1:ncell(x)),as.character(1:ncell(x))))
transitiondsC[adj] <- mahaldistance
tr <- new("TransitionLayer", nrows=as.integer(nrow(x)), ncols=as.integer(ncol(x)), extent = extent(x), crs=projection(x, asText=FALSE), matrixValues="conductance", transitionMatrix = transitiondsC)
Error in h(simpleError(msg, call)) :
  error in evaluating the argument 'x' in selecting a method for function 'nrow': error in evaluating the argument 'x' in selecting a method for function 'transitionMatrix': invalid class “TransitionData” object: FALSE

Do you know how to fix this?

@kauedesousa kauedesousa added bug Something isn't working help wanted Extra attention is needed labels Aug 20, 2020
@kauedesousa
Copy link
Member

Thanks @ChristianDueben I am trying to figure out how to solve this issue. It may be also related with the exports on the latest version when we moved the code to roxygen2. I will check the previous versions and get back to you.

@JamesWGriffiths
Copy link

Hi, I have run into the same issue as @ChristianDueben with transitionFunction = "mahal". Has a fix been identified?

@scbrown86
Copy link

This issue is still occurring. Are there any plans to fix this function?

@Rilquer
Copy link

Rilquer commented Mar 29, 2023

Hello, all.

Just wanted to say I am getting a similar error with transitionFunction = "mahal" on a RasterBrick of 19 bioclimatic variables. Was wondering if this error was fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants