[R-sig-Geo] How to import distance based spatial weight matrix into R to produceMoran's I

Roger Bivand Roger.Bivand at nhh.no
Sat Jun 16 09:46:24 CEST 2012


On Sat, 16 Jun 2012, ya wrote:

> Now I have two data files in my hand.One is "province.csv" which contains
> provincial logGDP per person data.The other is "Wd1.csv" which contains
> distance based spatial weight matrix. This weight matrix which is the matlab
> type file(ie. "Wd1.m") comes directly from a Matlab program. And I have
> transfered "Wd1.m"into "Wd1.xls" ,and "Wd1.xls" into "Wd1.csv".The question
> is that how can I load(or import) spatial weight matrix into R to calculate
> Moran's I?
>
> I've run following code in R,but it send message "error in moran(lny1978,
> gdpdw) : objects of different length".

Do please read the posting guide, and the help pages of the functions 
involved, noting the classes of the objects passed as arguments. Your 
quoted code is unhelpfull, as foreign is superfluous, but you do not say 
which package moran() comes from. Please try to create a reprodicible 
example, once you have re-examined your workflow (I suggest writing out 
your sparse spatial weights from Matlab as a three-column text file, and 
reading that into R as a listw object using read.dat2listw() in spdep.

Roger

PS. Never use attach, it only makes problems more opaque.

>
>> library(foreign)
>
>> gdp<- read.csv("province.csv")
>
>> attach(gdp)
>
>> gdpdw<- read.csv("Wd1.csv")
>
>> str(moran(lny1978,gdpdw))
>
>
> 	[[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, NHH 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