Skip to content

Mahalanobis Distance #4

@cdueben

Description

@cdueben

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions