[R-sig-Geo] Error in moran.test()

Roger Bivand Roger.Bivand at nhh.no
Tue Dec 28 19:49:39 CET 2010


On Tue, 28 Dec 2010, Wei Tu wrote:

> Hello,
>
> I got an error message after running the following R code:
>
> Error in moran.test(resid(mod.lm), ga159_mat) :  objects of different length
>
> # load data
> srm <- read.csv("srm.csv",sep=",",header=T)
> ga159.shp <- readShapeSpatial("srm.shp")
> data <- srm
>
> # create a weight matrix
> ga159w <- poly2nb(ga159.shp, queen=T)
> ga159w_mat <- nb2listw(ga159, style="B", zero.policy=TRUE)
>
> # Linear Model
> mod.lm <- lm(Rate_A ~ BlackZ + PopDenZ + MFIncomeZ + LanZ + Less9Z, data =
> data)
> summary(mod.lm)
> moran.test(resid(mod.lm),ga159w_mat)
>
> srm.shp is a shapefile including 159 counties in Georgia.

Had it occurred to you to check length(resid(mod.lm)) and length(ga159w)? 
They are not equal. In addition, unless you check carefully that srm and 
ga159.shp are in the same order - you almost certainly have some counties 
represented by multiple polygons in the shapefile - your results would be 
at severe risk of being meaningless. Use the FIPS in the shapefile and the 
data file to match correctly.

Hope this helps,

Roger

>
> Thanks!
>
> Wei
>
> 	[[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
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, 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