rspatialcorrelationspdep

Spatial correlogram


I am trying to run a spatial auto correlogram for a project looking at deforestation in the Atlantic forest, Brazil.

I am however confused as to why I am hitting this problem.

Problem

When I run the initial part of my code i receive an error of

Error: ncol(x) == 2 is not TRUE

My code is

r.nb <- dnearneigh(as.matrix(shapeS$POINT_X,shapeS$POINT_Y),
                   d1=200, d2=100000, latlong=FALSE)

and then I hope to move run this code

p.cor <- sp.correlogram(r.nb, deforestation, order=15,
                        method="I", randomisation=FALSE)

r.nb <- dnearneigh(as.matrix(shapeS$POINT_X,shapeS$POINT_Y), 
                   d1=200, d2=100000, latlong=FALSE)

My data is

A vector data set with the headings

POINTID GRID_CODE POINT_X POINT_Y

Solution

  • You need to use cbind, not as.matrix, or the approach that I show below. Always identify the R packages you are using. You claim that your data set a 'vector data set'. I doubt that. I am assuming it is a matrix.

    If it is a matrix, you can do

    m <- shapeS[, c('POINT_X', 'POINT_Y')]
    library(spdep)
    r.nb <- dnearneigh(m, d1=200, d2=100000, latlong=FALSE)
    

    It it is a data.frame, you can do

    m <- as.matrix(shapeS[, c('POINT_X', 'POINT_Y')])
    

    or

    m <- cbind(shapeS$POINT_X, shapeS$POINT_Y)