mat2listw: Convert a square spatial weights matrix to a weights list...

Description Usage Arguments Value Author(s) See Also Examples

View source: R/nb2mat.R

Description

The function converts a square spatial weights matrix, optionally a sparse matrix to a weights list object, optionally adding region IDs from the row names of the matrix, as a sequence of numbers 1:nrow(x), or as given as an argument. The style can be imposed by rebuilting the weights list object internally.

Usage

1
mat2listw(x, row.names = NULL, style="M")

Arguments

x

A square non-negative matrix with no NAs representing spatial weights; may be a matrix of class “sparseMatrix”

row.names

row names to use for region IDs

style

default "M", unknown style; if not "M", passed to nb2listw to re-build the object

Value

A listw object with the following members:

style

"M", meaning matrix style, underlying style unknown, or assigned style argument in rebuilt object

neighbours

the derived neighbours list

weights

the weights for the neighbours derived from the matrix

Author(s)

Roger Bivand Roger.Bivand@nhh.no

See Also

nb2listw, nb2mat

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
example(columbus)
coords <- coordinates(columbus)
col005 <- dnearneigh(coords, 0, 0.5, attr(col.gal.nb, "region.id"))
summary(col005)
col005.w.mat <- nb2mat(col005, zero.policy=TRUE)
col005.w.b <- mat2listw(col005.w.mat)
summary(col005.w.b$neighbours)
diffnb(col005, col005.w.b$neighbours)
col005.w.mat.3T <- kronecker(diag(3), col005.w.mat)
col005.w.b.3T <- mat2listw(col005.w.mat.3T, style="W")
summary(col005.w.b.3T$neighbours)
W <- as(nb2listw(col005, style="W", zero.policy=TRUE), "CsparseMatrix")
col005.spM <- mat2listw(W)
summary(col005.spM$neighbours)
diffnb(col005, col005.spM$neighbours)
IW <- kronecker(Diagonal(3), W)
col005.spM.3T <- mat2listw(IW, style="W")
summary(col005.spM.3T$neighbours)

Example output

Loading required package: sp
Loading required package: Matrix

colmbs> require(maptools)
Loading required package: maptools
Checking rgeos availability: TRUE

colmbs> columbus <- readShapePoly(system.file("etc/shapes/columbus.shp",
colmbs+  package="spdep")[1])

colmbs> col.gal.nb <- read.gal(system.file("etc/weights/columbus.gal",
colmbs+  package="spdep")[1])
Warning message:
use rgdal::readOGR or sf::st_read 
Neighbour list object:
Number of regions: 49 
Number of nonzero links: 170 
Percentage nonzero weights: 7.080383 
Average number of links: 3.469388 
4 regions with no links:
1 3 6 21
Link number distribution:

 0  1  2  3  4  5  6  7  8  9 
 4 11  5  8  3  9  2  2  3  2 
11 least connected regions:
2 5 9 10 31 34 36 39 42 46 47 with 1 link
2 most connected regions:
11 16 with 9 links
Neighbour list object:
Number of regions: 49 
Number of nonzero links: 170 
Percentage nonzero weights: 7.080383 
Average number of links: 3.469388 
4 regions with no links:
1 3 6 21
Link number distribution:

 0  1  2  3  4  5  6  7  8  9 
 4 11  5  8  3  9  2  2  3  2 
11 least connected regions:
2 5 9 10 31 34 36 39 42 46 47 with 1 link
2 most connected regions:
11 16 with 9 links
Neighbour list object:
Number of regions: 49 
Number of nonzero links: 0 
Percentage nonzero weights: 0 
Average number of links: 0 
49 regions with no links:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
Warning message:
In nb2listw(res$neighbours, glist = res$weights, style = style,  :
  zero sum general weights
Neighbour list object:
Number of regions: 147 
Number of nonzero links: 510 
Percentage nonzero weights: 2.360128 
Average number of links: 3.469388 
12 regions with no links:
1 3 6 21 50 52 55 70 99 101 104 119
Link number distribution:

 0  1  2  3  4  5  6  7  8  9 
12 33 15 24  9 27  6  6  9  6 
33 least connected regions:
2 5 9 10 31 34 36 39 42 46 47 51 54 58 59 80 83 85 88 91 95 96 100 103 107 108 129 132 134 137 140 144 145 with 1 link
6 most connected regions:
11 16 60 65 109 114 with 9 links
Warning message:
In sn2listw(df) : 1, 3, 6, 21 are not origins
Neighbour list object:
Number of regions: 49 
Number of nonzero links: 170 
Percentage nonzero weights: 7.080383 
Average number of links: 3.469388 
4 regions with no links:
1 3 6 21
Link number distribution:

 0  1  2  3  4  5  6  7  8  9 
 4 11  5  8  3  9  2  2  3  2 
11 least connected regions:
2 5 9 10 31 34 36 39 42 46 47 with 1 link
2 most connected regions:
11 16 with 9 links
Neighbour list object:
Number of regions: 49 
Number of nonzero links: 0 
Percentage nonzero weights: 0 
Average number of links: 0 
49 regions with no links:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
Note: method with signature 'diagonalMatrix#Matrix' chosen for function 'kronecker',
 target signature 'ddiMatrix#dgCMatrix'.
 "ANY#sparseMatrix" would also be valid
Note: method with signature 'dsparseMatrix#dsparseMatrix' chosen for function 'kronecker',
 target signature 'dtTMatrix#dgCMatrix'.
 "TsparseMatrix#sparseMatrix" would also be valid
Warning messages:
1: In sn2listw(df) :
  1, 3, 6, 21, 50, 52, 55, 70, 99, 101, 104, 119 are not origins
2: In nb2listw(res$neighbours, glist = res$weights, style = style,  :
  zero sum general weights
Neighbour list object:
Number of regions: 147 
Number of nonzero links: 510 
Percentage nonzero weights: 2.360128 
Average number of links: 3.469388 
12 regions with no links:
1 3 6 21 50 52 55 70 99 101 104 119
Link number distribution:

 0  1  2  3  4  5  6  7  8  9 
12 33 15 24  9 27  6  6  9  6 
33 least connected regions:
2 5 9 10 31 34 36 39 42 46 47 51 54 58 59 80 83 85 88 91 95 96 100 103 107 108 129 132 134 137 140 144 145 with 1 link
6 most connected regions:
11 16 60 65 109 114 with 9 links

spdep documentation built on Sept. 2, 2017, 1:11 a.m.