[R-sig-Geo] Moran's I Weight Matrix in R (error: objects of different length)
Roger Bivand
Roger.Bivand at nhh.no
Thu Jan 30 08:42:35 CET 2014
On Thu, 30 Jan 2014, Lareina La Flair wrote:
> Good evening,
>
> I'm new to spatial analysis R and therefore to this group but am thankful
> for any level of feedback on my question that follows. I've encountered an
> error in calculating Moran's I using the spdep package in R and am unable
> to isolate the problem on my own:
>
> *Error in moran.test(AODED$adcount/AODED$POP100, nb2listw(BC_nb, style =
> "B")) : objects of different length*
>
> How might I begin solving this problem?
>
> My code, adapted from prior posts on this subject:
>
> *# load data for 70 area units*
>
> *AODED<-read.csv("AODEDlags_R.csv",header=TRUE)
> *
>
>
> *BC <- readShapePoly("StudyareaZC11.shp") *
>
> *# create a weight matrix*
>
> *BC_nb <- poly2nb(as(BC, "SpatialPolygons"))
> X <- nb2mat(BC_nb,zero.policy=TRUE)
> n<-length(BC_nb)
> *
>
> *Nbr.matrix<-matrix(0,n,n) for(i in 1:n) *
>
> * Nbr.matrix[i,BC_nb[[i]]]<-1**> BC_nb*
>
> I have 70 area units, yet the weight matrix counts 71:
>
> *Neighbour list object:*
> *Number of regions: 71 *
> *Number of nonzero links: 320 *
> *Percentage nonzero weights: 6.347947 *
> *Average number of links: 4.507042 *
The very odd appearance of your posting is caused by rendering from
(forbidden) HTML. Only post plain text, please - HTML postings can contain
malicious payloads and are not permitted).
If you have 71 entities in BC, you'll get 71 regions in BC_nb for obvious
reasons. You should combine the attribute data object with the geometries
- see the vignette in the maptools package:
library(maptools)
vignette("combine_maptools")
In this way, you should be able to work out which of your 70 observations
in AODED match the 71 geometries in BC (hopefully you have unique IDs in
both the CSV and the shapefile that can be matched). You are the only
person who can do this, as a random deletion of a geometry by the software
(to make things easier) doesn't make any sense.
>
> Any insights on this problem and/or references to prior posts are welcome.
Prior posts may not be any guidance unless you understand what they are
doing. You references to nb2mat, and your very odd if() loop suggest that
you have to try to understand harder, as the steps involving X and
Nbr.matrix are neither clear (maybe the HTML problem) nor necessary. If
you refer to a prior post, do include its unique URL, preferably by thread
from:
https://stat.ethz.ch/pipermail/r-sig-geo/
so that we can see what you are referring to. Reading the help pages of
the functions carefully is also always sensible. Always also check the
sizes of input objects; doing:
dim(BC)
dim(AODED)
after reading them would have alerted you to the size problem. Usually
also look at the output of summary() of input objects (also maybe str() or
head() for non-Spatial objects), to be sure that they are what you think
they are.
Hope this helps,
Roger
> Thank you.
>
> Respectfully,
> Lareina
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
--
Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no
More information about the R-sig-Geo
mailing list