[R-sig-Geo] Linking lm residuls to specific polygon in shape files

Roger Bivand Roger.Bivand at nhh.no
Mon Aug 14 22:12:55 CEST 2006


On Mon, 14 Aug 2006, David Martell wrote:

> 
> I am using the lm.morantest procedure in spdep to test for spatial
> autocorrelation in my lm residuals.
> 
> I am not sure where this procedure gets the residuals.
> 
> I have prepared a GIS coverage that has a .shp file.  I ran the lm model,
> produced the residuals, then put them them in the GIS coverage.
> 
> 
> When I run lm in my R program, it produces an lm object that also contains
> the residuals.
> 
> 
> Does lm.morantest get the residuals from the .shp file or the lm object.
> 
> If it gets them from the latter, how does it determine which residual is
> assigned to which polygon in the .shp file.

lm.morantest() uses the whole "lm" object returned by lm(), so the 
residuals are by definition in the same order as the variables included in 
the model formula. If you need tight control, do something like this:

xx <- readShapePoly(system.file("shapes/columbus.shp", 
  package="maptools")[1], IDvar="NEIGNO")
row.names(as(xx, "data.frame"))
xx_nb <- poly2nb(as(xx, "SpatialPolygons"))
attr(xx_nb, "region.id")
lm_obj <- lm(CRIME ~ INC + HOVAL, data=xx)
names(residuals(lm_obj))
lm.morantest(lm_obj, nb2listw(xx_nb), spChk=TRUE)

where spChk=TRUE checks the equality of the names of the residuals of the 
"lm" object (taken from the data= object of the call to lm()) with the 
"region.id" attribute of the neighbour list. From the time the sapefile is 
read, it has nothing more to do with the SpatialPolygonsDataFrame object 
(here xx). Doing something like:

xx$lm_obj_resids <- residuals(lm_obj)

and then

writePolyShape(xx, "newshape")

includes them in a new shapefile.

Does this clarify things?

Roger

> 
> Thanks,
> 
> Dave Martell
> 
> _____________________________________________________________
> 
> David L. Martell, Professor
> Faculty of Forestry, University of Toronto
> 33 Willcocks Street, Toronto, Ontario, Canada, M5S 3B3
> 
> E-mail:  martell at smokey.forestry.utoronto.ca
> URL:     http://www.firelab.utoronto.ca
> Phone:   (416) 978-6960
> Fax:     (416) 978-3834
> Map:     ES on campus map at http://oracle.osm.utoronto.ca/map/
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> 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