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
columbus <- st_read(system.file("shapes/columbus.shp", package="spData")[1], quiet=TRUE)
col005 <- dnearneigh(st_coordinates(st_centroid(st_geometry(columbus),
 of_largest_polygon=TRUE)), 0, 0.5, as.character(columbus$NEIGNO))
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(Matrix::Diagonal(3), W)
col005.spM.3T <- mat2listw(IW, style="W")
summary(col005.spM.3T$neighbours)

Example output

sh: 1: cannot create /dev/null: Permission denied
Loading required package: sp
Loading required package: spData
To access larger datasets in this package, install the spDataLarge
package with: `install.packages('spDataLarge',
repos='https://nowosad.github.io/drat/', type='source')`
Loading required package: sf
sh: 1: cannot create /dev/null: Permission denied
Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
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:
1005 1006 1008 1043
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:
1001 1007 1018 1010 1045 1044 1046 1047 1049 1048 1015 with 1 link
2 most connected regions:
1038 1036 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:
1005 1006 1008 1043
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:
1001 1007 1018 1010 1045 1044 1046 1047 1049 1048 1015 with 1 link
2 most connected regions:
1038 1036 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:
1005 1001 1006 1002 1007 1008 1004 1003 1018 1010 1038 1037 1039 1040 1009 1036 1011 1042 1041 1017 1043 1019 1012 1035 1032 1020 1021 1031 1033 1034 1045 1013 1022 1044 1023 1046 1030 1024 1047 1016 1014 1049 1029 1025 1028 1048 1015 1027 1026
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
Registered S3 methods overwritten by 'spatialreg':
  method                   from 
  residuals.stsls          spdep
  deviance.stsls           spdep
  coef.stsls               spdep
  print.stsls              spdep
  summary.stsls            spdep
  print.summary.stsls      spdep
  residuals.gmsar          spdep
  deviance.gmsar           spdep
  coef.gmsar               spdep
  fitted.gmsar             spdep
  print.gmsar              spdep
  summary.gmsar            spdep
  print.summary.gmsar      spdep
  print.lagmess            spdep
  summary.lagmess          spdep
  print.summary.lagmess    spdep
  residuals.lagmess        spdep
  deviance.lagmess         spdep
  coef.lagmess             spdep
  fitted.lagmess           spdep
  logLik.lagmess           spdep
  fitted.SFResult          spdep
  print.SFResult           spdep
  fitted.ME_res            spdep
  print.ME_res             spdep
  print.lagImpact          spdep
  plot.lagImpact           spdep
  summary.lagImpact        spdep
  HPDinterval.lagImpact    spdep
  print.summary.lagImpact  spdep
  print.sarlm              spdep
  summary.sarlm            spdep
  residuals.sarlm          spdep
  deviance.sarlm           spdep
  coef.sarlm               spdep
  vcov.sarlm               spdep
  fitted.sarlm             spdep
  logLik.sarlm             spdep
  anova.sarlm              spdep
  predict.sarlm            spdep
  print.summary.sarlm      spdep
  print.sarlm.pred         spdep
  as.data.frame.sarlm.pred spdep
  residuals.spautolm       spdep
  deviance.spautolm        spdep
  coef.spautolm            spdep
  fitted.spautolm          spdep
  print.spautolm           spdep
  summary.spautolm         spdep
  logLik.spautolm          spdep
  print.summary.spautolm   spdep
  print.WXImpact           spdep
  summary.WXImpact         spdep
  print.summary.WXImpact   spdep
  predict.SLX              spdep
Warning message:
In sn2listw(df) : 1005, 1006, 1008, 1043 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:
1005 1006 1008 1043
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:
1001 1007 1018 1010 1045 1044 1046 1047 1049 1048 1015 with 1 link
2 most connected regions:
1038 1036 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:
1005 1001 1006 1002 1007 1008 1004 1003 1018 1010 1038 1037 1039 1040 1009 1036 1011 1042 1041 1017 1043 1019 1012 1035 1032 1020 1021 1031 1033 1034 1045 1013 1022 1044 1023 1046 1030 1024 1047 1016 1014 1049 1029 1025 1028 1048 1015 1027 1026
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. 7, 2021, 5:07 p.m.