[R-sig-Geo] inverse distance weights matrix
Griffin, Terry W
twgriffi at purdue.edu
Fri Mar 17 22:39:06 CET 2006
The MATLAB routines are the Spatial Econometrics Toolbox format. I
tried the read.dat2listw() in R; the code and error message for a small
matrix example is below; this uses a full matrix from MATLAB (realizing
that "read.dat2listw" is intended for sparse matrices).
> mattry<-read.dat2listw("mat4rf")
Error in "[.default"(sn, , 1) : incorrect number of dimensions
> traceback()
5: NextMethod("[")
4: "[.factor"(sn, , 1)
3: sn[, 1]
2: unique(sn[, 1])
1: read.dat2listw("mat4rf")
I'm not able to use dlmwrite in MATLAB to export the sparse matrix so I
tried to import the full matrix into R (code is above). The MATLAB code
I'm using came from this listserve archive. The MATLAB code and error
message are below. I'm able to use dlmwrite to create the MATLAB full
matrix (used above in the R code).
load testmat.GWT; A = (spconvert(testmat)); %have to change name in two
locations on this line
n=size(A); nobs=n(1);
% Normalize matrix (adapted form LeSage 1999)
wt = sparse(A);
[i1,j1,s1]=find(wt);
rsum=sum(wt);
for i=1:nobs;
ind=find(i1==i);
s1(ind,1)=s1(ind,1)/rsum(1,i);
end;
[m,n] = size(wt);
W = sparse(i1,j1,s1,m,n);
S = sparse(W);
dlmwrite('mat4rst', S); %give the new sparse weight matrix a permanent
name for R
??? Error using ==> sprintf
Function is not defined for sparse inputs.
Error in ==> dlmwrite at 172
str = sprintf(format,m(i,:));
If anyone wants to give it a try, I've put the small example matrices
and the "real" matrix up on my website at:
http://web.ics.purdue.edu/~twgriffi/R_matrix.htm
My objective is to use R for my spatial data analysis using inverse
distance weights matrices. I'm struggling with the inverse distance
matrices. I hope to either create these inverse distance matrices in
SpaceStat then import into R or create the matrices in R.
Thank you,
Terry
-----Original Message-----
From: Roger Bivand [mailto:Roger.Bivand at nhh.no]
Sent: Friday, March 17, 2006 2:43 PM
To: Griffin, Terry W
Cc: r-sig-geo at stat.math.ethz.ch
Subject: Re: [R-sig-Geo] inverse distance weights matrix
On Thu, 16 Mar 2006, Griffin, Terry W wrote:
> Greetings,
>
>
>
> I'm migrating from MATLAB to R and am having trouble converting
inverse
> distance weights matrix into a form usable by R. I've created the
> matrix in SpaceStat (in *.FMT format) and converted to *.GWT format
and
> also have it in MATLAB *.m format.
>
I wonder whether your Matlab data are in the Spatial Econometrics
Toolbox
format - if they are, you could try the read.dat2listw() function
mentioned on the read.gwt2nb function help page.
>
>
> I've tried using the read.gwt2nb in R, but without success. There are
> nearly 2,451 observations with an average of 482 linkages. About 20 %
> have nonzero weights. Any suggestions are appreciated.
Without details of what you mean by "without success", it's hard to
offer
any advice. Could you create a small example of the problems you are
seeing, and repost with copies of the commands you give and any error
messages?
If you are seeing errors ("Error in ..." messages from R), please also
type traceback() at the prompt and report the output. You could also put
the uncooperative GWT file on a website for others to try to see what is
going on.
>
>
>
> Thank you,
>
> Terry
>
>
>
>
>
> Terry W. Griffin
>
> Graduate Research Assistant
>
> Agricultural Economics
>
> Purdue University
>
> 403 W State St
>
> West Lafayette, IN 47907
>
> 765-494-4257
>
> http://web.ics.purdue.edu/~twgriffi/
> <http://web.ics.purdue.edu/~twgriffi/>
>
>
>
>
>
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> 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