[R-sig-Geo] "txt" to "listw"
Roger Bivand
Roger.Bivand at nhh.no
Fri Mar 23 13:24:58 CET 2012
On Wed, 21 Mar 2012, anny peng wrote:
> Dear R users,
>
> Thank Dr. Divand for creating spdep, and thank everyone for your attention.
>
> I attach a 30*30 matrix, which consists of 0 and 1. The matrix is a
> "txt" file. I would like to change this matrix into a ".listw" file
> so that I can estimate spatial auto-correlation.
>
> As a new user, I have difficulties in reading "txt" file and use the
> function "mat2listw".
>
> Below are my errors.
>
>
> data1= read.fwf("30_30.txt", widths=1)
The problem was resolved off-list. The main issue was about reading the
tab-separated file into a matrix for use in mat2listw():
mat0 <- read.table("30_30.txt", header=FALSE, sep="\t", fill=TRUE)
and converted into a matrix with:
mat <- as.matrix(mat0)
dim(mat)
However, it has NAs, not 0, where there are no neighbours, so this must be
repaired:
summary(c(mat))
mat[is.na(mat)] <- 0
summary(c(mat))
Reading the R Data Import/Export manual would have helped, together with
the use of CSV as the spreadsheet output format - the fields here are not
of fixed width, although they look like that; tabs are used as field
separators, but fields with 1s in have a width of 2, while empty fields
have a width of 1.
Finally:
lw <- mat2listw(mat, style="B")
summary(lw)
Roger
>
>
> coords <- coordinates(data1)
> col005 <- dnearneigh(points, 0, 1, attr(col.gal.nb, row.names = IDs))
> summary(col005)
> col005.w.mat <- nb2mat(col005, zero.policy=TRUE)
> col005.w.b <- mat2listw(col005.w.mat)
>
> ......
>
>
>
> Best regard
> Zz
>
--
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