Our last couple of weeks’ discussions dealt with objects, features and attributes
Spatial modeling relies on the establishment of neighbors or connections
These neighbors serve to model spatial dependence AKA as spatial autocorrelation
Rook criterion defines neighbors by the existence of a common edge between two spatial units.
The queen criterion is somewhat more encompassing and defines neighbors as spatial units sharing a common edge or a common vertex.
Finally, the Bishop’s approach just relies on a common vertex (not available anymore, but conceptually works as follows)
Spatial weights matrix
Once more, we would need to access data from repositories hosted in the internet
Specifically, we will rely on the following packages
install.packages("tigris")
install.packages("acs")
install.packages("spdep")
install.pacakges("tmaptools")
library(spdep)
library(tigris)
options(tigris_use_cache = TRUE)
library(acs)
library(stringr)
library(tmaptools)
library(plyr)
Let’s work with a map of the USA
states(cb = TRUE, class="sp") states <-
## Warning in proj4string(obj): CRS object has comment, which is lost in output
"%ni%" <- Negate("%in%")
plot(states)
Geographic data obtained from Walker (2020)
Limit to the contiguous USA
$NAME %ni% c('Alaska','American Samoa','Commonwealth of the Northern Mariana Islands','Guam','Hawaii','United States Virgin Islands','Puerto Rico'),]
states<-states[statesplot(states)
Geographic data obtained from Walker (2020)
Now let’s create our first weight matrices
poly2nb(states, queen=TRUE)
m1f<- nb2listw(m1f,zero.policy=T)
m1f<-coordinates(states)
coords<-str(m1f)
## List of 3
## $ style : chr "W"
## $ neighbours:List of 49
## ..$ : int [1:2] 15 16
## ..$ : int [1:4] 12 23 26 32
## ..$ : int [1:4] 12 32 33 39
## ..$ : int [1:5] 7 18 36 44 45
## ..$ : int [1:2] 16 43
## ..$ : int 34
## ..$ : int [1:2] 4 44
## ..$ : int [1:2] 30 49
## ..$ : int [1:6] 12 19 23 25 33 42
## ..$ : int [1:2] 26 40
## ..$ : int [1:5] 13 20 21 35 42
## ..$ : int [1:6] 2 3 9 23 32 33
## ..$ : int [1:4] 11 17 21 46
## ..$ : int [1:3] 27 35 40
## ..$ : int [1:4] 1 16 22 41
## ..$ : int [1:5] 1 5 15 22 43
## ..$ : int [1:6] 13 19 21 22 41 46
## ..$ : int [1:6] 4 31 36 38 45 47
## ..$ : int [1:8] 9 17 21 22 25 28 33 37
## ..$ : int [1:6] 11 23 26 27 35 42
## ..$ : int [1:6] 11 13 17 19 25 42
## ..$ : int [1:8] 15 16 17 19 37 41 43 44
## ..$ : int [1:6] 2 9 12 20 26 42
## ..$ : int [1:4] 28 37 38 48
## ..$ : int [1:4] 9 19 21 42
## ..$ : int [1:6] 2 10 20 23 27 40
## ..$ : int [1:5] 14 20 26 35 40
## ..$ : int [1:5] 19 24 33 37 39
## ..$ : int [1:3] 34 47 49
## ..$ : int [1:3] 8 47 49
## ..$ : int [1:3] 18 36 47
## ..$ : int [1:3] 2 3 12
## ..$ : int [1:6] 3 9 12 19 28 39
## ..$ : int [1:3] 6 29 49
## ..$ : int [1:5] 11 14 20 27 42
## ..$ : int [1:3] 4 18 31
## ..$ : int [1:7] 19 22 24 28 38 44 45
## ..$ : int [1:5] 18 24 37 45 48
## ..$ : int [1:4] 3 28 33 48
## ..$ : int [1:4] 10 14 26 27
## ..$ : int [1:4] 15 17 22 46
## ..$ : int [1:7] 9 11 20 21 23 25 35
## ..$ : int [1:4] 5 16 22 44
## ..$ : int [1:6] 4 7 22 37 43 45
## ..$ : int [1:5] 4 18 37 38 44
## ..$ : int [1:3] 13 17 41
## ..$ : int [1:5] 18 29 30 31 49
## ..$ : int [1:3] 24 38 39
## ..$ : int [1:5] 8 29 30 34 47
## ..- attr(*, "class")= chr "nb"
## ..- attr(*, "region.id")= chr [1:49] "0" "2" "3" "4" ...
## ..- attr(*, "call")= language poly2nb(pl = states, queen = TRUE)
## ..- attr(*, "type")= chr "queen"
## ..- attr(*, "sym")= logi TRUE
## $ weights :List of 49
## ..$ : num [1:2] 0.5 0.5
## ..$ : num [1:4] 0.25 0.25 0.25 0.25
## ..$ : num [1:4] 0.25 0.25 0.25 0.25
## ..$ : num [1:5] 0.2 0.2 0.2 0.2 0.2
## ..$ : num [1:2] 0.5 0.5
## ..$ : num 1
## ..$ : num [1:2] 0.5 0.5
## ..$ : num [1:2] 0.5 0.5
## ..$ : num [1:6] 0.167 0.167 0.167 0.167 0.167 ...
## ..$ : num [1:2] 0.5 0.5
## ..$ : num [1:5] 0.2 0.2 0.2 0.2 0.2
## ..$ : num [1:6] 0.167 0.167 0.167 0.167 0.167 ...
## ..$ : num [1:4] 0.25 0.25 0.25 0.25
## ..$ : num [1:3] 0.333 0.333 0.333
## ..$ : num [1:4] 0.25 0.25 0.25 0.25
## ..$ : num [1:5] 0.2 0.2 0.2 0.2 0.2
## ..$ : num [1:6] 0.167 0.167 0.167 0.167 0.167 ...
## ..$ : num [1:6] 0.167 0.167 0.167 0.167 0.167 ...
## ..$ : num [1:8] 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125
## ..$ : num [1:6] 0.167 0.167 0.167 0.167 0.167 ...
## ..$ : num [1:6] 0.167 0.167 0.167 0.167 0.167 ...
## ..$ : num [1:8] 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125
## ..$ : num [1:6] 0.167 0.167 0.167 0.167 0.167 ...
## ..$ : num [1:4] 0.25 0.25 0.25 0.25
## ..$ : num [1:4] 0.25 0.25 0.25 0.25
## ..$ : num [1:6] 0.167 0.167 0.167 0.167 0.167 ...
## ..$ : num [1:5] 0.2 0.2 0.2 0.2 0.2
## ..$ : num [1:5] 0.2 0.2 0.2 0.2 0.2
## ..$ : num [1:3] 0.333 0.333 0.333
## ..$ : num [1:3] 0.333 0.333 0.333
## ..$ : num [1:3] 0.333 0.333 0.333
## ..$ : num [1:3] 0.333 0.333 0.333
## ..$ : num [1:6] 0.167 0.167 0.167 0.167 0.167 ...
## ..$ : num [1:3] 0.333 0.333 0.333
## ..$ : num [1:5] 0.2 0.2 0.2 0.2 0.2
## ..$ : num [1:3] 0.333 0.333 0.333
## ..$ : num [1:7] 0.143 0.143 0.143 0.143 0.143 ...
## ..$ : num [1:5] 0.2 0.2 0.2 0.2 0.2
## ..$ : num [1:4] 0.25 0.25 0.25 0.25
## ..$ : num [1:4] 0.25 0.25 0.25 0.25
## ..$ : num [1:4] 0.25 0.25 0.25 0.25
## ..$ : num [1:7] 0.143 0.143 0.143 0.143 0.143 ...
## ..$ : num [1:4] 0.25 0.25 0.25 0.25
## ..$ : num [1:6] 0.167 0.167 0.167 0.167 0.167 ...
## ..$ : num [1:5] 0.2 0.2 0.2 0.2 0.2
## ..$ : num [1:3] 0.333 0.333 0.333
## ..$ : num [1:5] 0.2 0.2 0.2 0.2 0.2
## ..$ : num [1:3] 0.333 0.333 0.333
## ..$ : num [1:5] 0.2 0.2 0.2 0.2 0.2
## ..- attr(*, "mode")= chr "binary"
## ..- attr(*, "W")= logi TRUE
## ..- attr(*, "comp")=List of 1
## .. ..$ d: num [1:49] 2 4 4 5 2 1 2 2 6 2 ...
## - attr(*, "class")= chr [1:2] "listw" "nb"
## - attr(*, "region.id")= chr [1:49] "0" "2" "3" "4" ...
## - attr(*, "call")= language nb2listw(neighbours = m1f, zero.policy = T)
Now we can plot:
plot(states, border="grey")
plot(m1f, coords, add=TRUE)
Geographic data obtained from Walker (2020)
To compare we need to repeat
poly2nb(states, queen=FALSE)
m2f<- nb2listw(m2f,zero.policy=T)
m2f<-coordinates(states)
coords<-str(m2f)
## List of 3
## $ style : chr "W"
## $ neighbours:List of 49
## ..$ : int [1:2] 15 16
## ..$ : int [1:4] 12 23 26 32
## ..$ : int [1:4] 12 32 33 39
## ..$ : int [1:5] 7 18 36 44 45
## ..$ : int [1:2] 16 43
## ..$ : int 34
## ..$ : int [1:2] 4 44
## ..$ : int [1:2] 30 49
## ..$ : int [1:6] 12 19 23 25 33 42
## ..$ : int [1:2] 26 40
## ..$ : int [1:4] 13 21 35 42
## ..$ : int [1:6] 2 3 9 23 32 33
## ..$ : int [1:4] 11 17 21 46
## ..$ : int [1:3] 27 35 40
## ..$ : int [1:4] 1 16 22 41
## ..$ : int [1:5] 1 5 15 22 43
## ..$ : int [1:6] 13 19 21 22 41 46
## ..$ : int [1:6] 4 31 36 38 45 47
## ..$ : int [1:8] 9 17 21 22 25 28 33 37
## ..$ : int [1:5] 23 26 27 35 42
## ..$ : int [1:6] 11 13 17 19 25 42
## ..$ : int [1:8] 15 16 17 19 37 41 43 44
## ..$ : int [1:6] 2 9 12 20 26 42
## ..$ : int [1:4] 28 37 38 48
## ..$ : int [1:4] 9 19 21 42
## ..$ : int [1:6] 2 10 20 23 27 40
## ..$ : int [1:5] 14 20 26 35 40
## ..$ : int [1:5] 19 24 33 37 39
## ..$ : int [1:3] 34 47 49
## ..$ : int [1:3] 8 47 49
## ..$ : int [1:3] 18 36 47
## ..$ : int [1:3] 2 3 12
## ..$ : int [1:6] 3 9 12 19 28 39
## ..$ : int [1:3] 6 29 49
## ..$ : int [1:4] 11 14 20 27
## ..$ : int [1:3] 4 18 31
## ..$ : int [1:7] 19 22 24 28 38 44 45
## ..$ : int [1:5] 18 24 37 45 48
## ..$ : int [1:4] 3 28 33 48
## ..$ : int [1:4] 10 14 26 27
## ..$ : int [1:4] 15 17 22 46
## ..$ : int [1:6] 9 11 20 21 23 25
## ..$ : int [1:4] 5 16 22 44
## ..$ : int [1:6] 4 7 22 37 43 45
## ..$ : int [1:5] 4 18 37 38 44
## ..$ : int [1:3] 13 17 41
## ..$ : int [1:5] 18 29 30 31 49
## ..$ : int [1:3] 24 38 39
## ..$ : int [1:5] 8 29 30 34 47
## ..- attr(*, "class")= chr "nb"
## ..- attr(*, "region.id")= chr [1:49] "0" "2" "3" "4" ...
## ..- attr(*, "call")= language poly2nb(pl = states, queen = FALSE)
## ..- attr(*, "type")= chr "rook"
## ..- attr(*, "sym")= logi TRUE
## $ weights :List of 49
## ..$ : num [1:2] 0.5 0.5
## ..$ : num [1:4] 0.25 0.25 0.25 0.25
## ..$ : num [1:4] 0.25 0.25 0.25 0.25
## ..$ : num [1:5] 0.2 0.2 0.2 0.2 0.2
## ..$ : num [1:2] 0.5 0.5
## ..$ : num 1
## ..$ : num [1:2] 0.5 0.5
## ..$ : num [1:2] 0.5 0.5
## ..$ : num [1:6] 0.167 0.167 0.167 0.167 0.167 ...
## ..$ : num [1:2] 0.5 0.5
## ..$ : num [1:4] 0.25 0.25 0.25 0.25
## ..$ : num [1:6] 0.167 0.167 0.167 0.167 0.167 ...
## ..$ : num [1:4] 0.25 0.25 0.25 0.25
## ..$ : num [1:3] 0.333 0.333 0.333
## ..$ : num [1:4] 0.25 0.25 0.25 0.25
## ..$ : num [1:5] 0.2 0.2 0.2 0.2 0.2
## ..$ : num [1:6] 0.167 0.167 0.167 0.167 0.167 ...
## ..$ : num [1:6] 0.167 0.167 0.167 0.167 0.167 ...
## ..$ : num [1:8] 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125
## ..$ : num [1:5] 0.2 0.2 0.2 0.2 0.2
## ..$ : num [1:6] 0.167 0.167 0.167 0.167 0.167 ...
## ..$ : num [1:8] 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125
## ..$ : num [1:6] 0.167 0.167 0.167 0.167 0.167 ...
## ..$ : num [1:4] 0.25 0.25 0.25 0.25
## ..$ : num [1:4] 0.25 0.25 0.25 0.25
## ..$ : num [1:6] 0.167 0.167 0.167 0.167 0.167 ...
## ..$ : num [1:5] 0.2 0.2 0.2 0.2 0.2
## ..$ : num [1:5] 0.2 0.2 0.2 0.2 0.2
## ..$ : num [1:3] 0.333 0.333 0.333
## ..$ : num [1:3] 0.333 0.333 0.333
## ..$ : num [1:3] 0.333 0.333 0.333
## ..$ : num [1:3] 0.333 0.333 0.333
## ..$ : num [1:6] 0.167 0.167 0.167 0.167 0.167 ...
## ..$ : num [1:3] 0.333 0.333 0.333
## ..$ : num [1:4] 0.25 0.25 0.25 0.25
## ..$ : num [1:3] 0.333 0.333 0.333
## ..$ : num [1:7] 0.143 0.143 0.143 0.143 0.143 ...
## ..$ : num [1:5] 0.2 0.2 0.2 0.2 0.2
## ..$ : num [1:4] 0.25 0.25 0.25 0.25
## ..$ : num [1:4] 0.25 0.25 0.25 0.25
## ..$ : num [1:4] 0.25 0.25 0.25 0.25
## ..$ : num [1:6] 0.167 0.167 0.167 0.167 0.167 ...
## ..$ : num [1:4] 0.25 0.25 0.25 0.25
## ..$ : num [1:6] 0.167 0.167 0.167 0.167 0.167 ...
## ..$ : num [1:5] 0.2 0.2 0.2 0.2 0.2
## ..$ : num [1:3] 0.333 0.333 0.333
## ..$ : num [1:5] 0.2 0.2 0.2 0.2 0.2
## ..$ : num [1:3] 0.333 0.333 0.333
## ..$ : num [1:5] 0.2 0.2 0.2 0.2 0.2
## ..- attr(*, "mode")= chr "binary"
## ..- attr(*, "W")= logi TRUE
## ..- attr(*, "comp")=List of 1
## .. ..$ d: num [1:49] 2 4 4 5 2 1 2 2 6 2 ...
## - attr(*, "class")= chr [1:2] "listw" "nb"
## - attr(*, "region.id")= chr [1:49] "0" "2" "3" "4" ...
## - attr(*, "call")= language nb2listw(neighbours = m2f, zero.policy = T)
plot(states, border="grey")
plot(m1f, coords, add=TRUE)
plot(m2f, coords, add=TRUE, col="red")
Geographic data obtained from Walker (2020)
For this section we will access all colleges and universities in Philly
Distances in Philly, see González Canché (2019)
Standardization with Radii, see González Canché (2019)
Standardization with kth, see González Canché (2019)
library(tmaptools)
getwd()
a<- paste('http://nces.ed.gov/ipeds/datacenter/data/HD2018.zip')
url18 <-download.file(url18, destfile = paste(a,"HD2018.zip",sep="/"))
read.csv(unz("HD2018.zip", "hd2018.csv"))
d18<-
#keeping only five first digits of a zip code
$ZIP<- as.numeric(substr(d18$ZIP, 1, 5))
d18#Subsetting the file with institutions located in Philadelphia, these zip codes were retrieved from Zillow
$ZIP %in% c(19126, 19125, 19128, 19127, 19130, 19129, 19132, 19131, 19134, 19133, 19136, 19135, 19138, 19137, 19140, 19139, 19142, 19141, 19144, 19143, 19146, 19145, 19148, 19147, 19150, 19149, 19152, 19151, 19154, 19153, 19155, 19170, 19173, 19019, 19176, 19187, 19192, 19092, 19101, 19099, 19103, 19102, 19105, 19104, 19107, 19106, 19109, 19111, 19110, 19112, 19115, 19114, 19118, 19116, 19120, 19119, 19122, 19121, 19124, 19123),]
d18 <-d18[d18#Looking at the dimensions of the subsetted dataset
dim(d18)
## [1] 37 73
table(d18$SECTOR)
##
## 1 2 3 4 5 6 8 9
## 1 17 4 1 3 5 1 5
$SECTOR<3,c("UNITID", "INSTNM", "SECTOR", "LONGITUD", "LATITUDE")]
d18<-d18[d18#Print Table d18
UNITID <int> | ||
---|---|---|
2958 | 210508 | |
2993 | 211583 | |
2999 | 211893 | |
3006 | 212054 | |
3039 | 212984 | |
3054 | 213367 | |
3087 | 214148 | |
3122 | 214883 | |
3125 | 214971 | |
3129 | 215062 |
#This will create the object coords as shown in equation (17)
cbind(d18$LONGITUD, d18$LATITUDE) coords =
Print distances:
spDists(coords, coords, longlat=T)
dist <-#Moving from KM to Miles
dist * 0.621371192237
dist <-#Prints distance table with actual data dist
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 0.0000000 10.012459 0.1712984 1.0006310 12.614185 6.306948 0.6350112
## [2,] 10.0124592 0.000000 10.0012119 9.3062647 12.969018 5.237701 9.4395681
## [3,] 0.1712984 10.001212 0.0000000 1.1159608 12.450388 6.219961 0.5718978
## [4,] 1.0006310 9.306265 1.1159608 0.0000000 13.020336 6.038045 0.9614580
## [5,] 12.6141852 12.969018 12.4503877 13.0203357 0.000000 8.970754 12.1660606
## [6,] 6.3069480 5.237701 6.2199605 6.0380451 8.970754 0.000000 5.6724469
## [7,] 0.6350112 9.439568 0.5718978 0.9614580 12.166061 5.672447 0.0000000
## [8,] 0.4286572 10.295565 0.3296307 1.4273361 12.415880 6.420435 0.8567019
## [9,] 0.7632002 9.684695 0.6094543 1.4124608 11.855834 5.704068 0.4690691
## [10,] 1.0907076 9.553368 1.2392903 0.3598743 13.356296 6.385156 1.2330556
## [11,] 5.2657047 4.806120 5.2380874 4.6705236 11.239352 2.343632 4.6691159
## [12,] 0.4377258 10.268306 0.3231432 1.4314644 12.374185 6.381259 0.8314790
## [13,] 4.5254724 5.690013 4.5542238 3.6950408 12.771047 4.048722 4.0410401
## [14,] 1.7697204 9.753059 1.9335857 1.0858638 14.106200 6.935817 1.9937067
## [15,] 4.6954162 6.391158 4.7722529 3.7275798 14.118122 5.430496 4.3597156
## [16,] 4.8623453 6.805905 4.9575557 3.8682887 14.718357 6.054051 4.5979672
## [17,] 2.4201742 8.261752 2.3069789 2.4816658 10.592531 3.967679 1.8101581
## [18,] 0.8217110 10.261436 0.6629135 1.7544007 11.964251 6.195292 0.9533728
## [,8] [,9] [,10] [,11] [,12] [,13]
## [1,] 0.42865719 0.7632002 1.0907076 5.265705 0.43772580 4.525472
## [2,] 10.29556481 9.6846953 9.5533676 4.806120 10.26830573 5.690013
## [3,] 0.32963066 0.6094543 1.2392903 5.238087 0.32314315 4.554224
## [4,] 1.42733611 1.4124608 0.3598743 4.670524 1.43146445 3.695041
## [5,] 12.41588049 11.8558344 13.3562957 11.239352 12.37418545 12.771047
## [6,] 6.42043470 5.7040683 6.3851561 2.343632 6.38125902 4.048722
## [7,] 0.85670191 0.4690691 1.2330556 4.669116 0.83147901 4.041040
## [8,] 0.00000000 0.7207223 1.5118626 5.519665 0.04374115 4.875763
## [9,] 0.72072225 0.0000000 1.6500805 4.887682 0.67989128 4.395942
## [10,] 1.51186259 1.6500805 0.0000000 4.964794 1.52610354 3.904395
## [11,] 5.51966471 4.8876819 4.9647938 0.000000 5.49007527 1.757560
## [12,] 0.04374115 0.6798913 1.5261035 5.490075 0.00000000 4.857718
## [13,] 4.87576271 4.3959421 3.9043952 1.757560 4.85771823 0.000000
## [14,] 2.16059955 2.3992091 0.7618053 5.330715 2.18338801 4.063047
## [15,] 5.10124880 4.7904692 3.8178285 3.119028 5.09468693 1.383153
## [16,] 5.28181268 5.0481222 3.9017276 3.741531 5.28025566 2.006011
## [17,] 2.47085672 1.7501719 2.8362542 3.483868 2.43003711 3.563742
## [18,] 0.46961740 0.5812735 1.9015484 5.461316 0.43366595 4.967533
## [,14] [,15] [,16] [,17] [,18]
## [1,] 1.7697204 4.6954162 4.8623453 2.420174 0.8217110
## [2,] 9.7530589 6.3911577 6.8059053 8.261752 10.2614357
## [3,] 1.9335857 4.7722529 4.9575557 2.306979 0.6629135
## [4,] 1.0858638 3.7275798 3.8682887 2.481666 1.7544007
## [5,] 14.1061995 14.1181218 14.7183572 10.592531 11.9642507
## [6,] 6.9358166 5.4304958 6.0540507 3.967679 6.1952919
## [7,] 1.9937067 4.3597156 4.5979672 1.810158 0.9533728
## [8,] 2.1605996 5.1012488 5.2818127 2.470857 0.4696174
## [9,] 2.3992091 4.7904692 5.0481222 1.750172 0.5812735
## [10,] 0.7618053 3.8178285 3.9017276 2.836254 1.9015484
## [11,] 5.3307151 3.1190284 3.7415311 3.483868 5.4613164
## [12,] 2.1833880 5.0946869 5.2802557 2.430037 0.4336660
## [13,] 4.0630467 1.3831528 2.0060112 3.563742 4.9675333
## [14,] 0.0000000 3.7079946 3.6692842 3.554220 2.5911120
## [15,] 3.7079946 0.0000000 0.6237331 4.429434 5.3117886
## [16,] 3.6692842 0.6237331 0.0000000 4.872350 5.5376318
## [17,] 3.5542196 4.4294339 4.8723505 0.000000 2.2278248
## [18,] 2.5911120 5.3117886 5.5376318 2.227825 0.0000000
knn2nb(knearneigh(coords,k=1))
test.nb_k<-nb2listw(test.nb_k)
test.listw1c<-# plot(states[states@data$NAME=="Pennsylvania",])
plot(test.nb_k, coords, lwd=.2, col="blue", cex = .5)
Four-year colleges in Philadelphia
max(unlist(nbdists(test.nb_k, coords, longlat = TRUE)))
critical.threshold <- critical.threshold
## [1] 14.43703
#First create a neighbors object reaching 1 mile (default is in KM)
dnearneigh(coords, 0, 1.60934, row.names = d18$UNITID, longlat = TRUE)
test.nb<-#Now create a list, similar to an edgelist in spatial form
nb2listw(test.nb, zero.policy=TRUE)
test.listw<-plot(test.nb, coords, lwd=.2, col="blue", cex = .5)
In order to conform to Tobler’s first law of geography, a distance decay effect must be respected.
The value of the function of distance needs to decrease with a growing distance
Since the weights are inversely related to distance, large values for the latter will yield small values for the former, and vice versa.
Recalculate back to 1-k neighbor test.nb_k
and the critical.threshold
along with all distances as follows
dnearneigh(coords, 0, critical.threshold, longlat = TRUE)
nb.dist.band <- nbdists(nb.dist.band,coords, longlat = TRUE)
distances <-1] distances[
## [[1]]
## [1] 0.2756780 1.6103595 10.1500490 1.0219514 0.6898569 1.2282517
## [7] 1.7553238 8.4743303 0.7044514 7.2830419 2.8480890 7.5565399
## [13] 7.8251862 3.8948929 1.3224157
Finally apply this function to obtain the inverse
lapply(distances, function(x) (1/x))
invd1 <-length(invd1)
## [1] 18
lapply(distances, function(x) (1/(x/1)))
invd1a <-1] invd1a[
## [[1]]
## [1] 3.62742056 0.62097934 0.09852169 0.97852013 1.44957603 0.81416538
## [7] 0.56969546 0.11800342 1.41954436 0.13730527 0.35111263 0.13233570
## [13] 0.12779249 0.25674647 0.75619185
nb2listw(nb.dist.band, glist = invd1a, style = "B") invd.weights <-
By specifying style = "B"
we apply these weights, if we do not include that, we get row standardization
plot(invd.weights, coords, lwd=.2, col="blue", cex = .5)
Four-year colleges in Philadelphia
To see the weights, we can do the following
listw2mat(invd.weights)
w3 = w3
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## 1 0.00000000 0.00000000 3.62742056 0.6209793 0.00000000 0.09852169 0.9785201
## 2 0.00000000 0.00000000 0.00000000 0.0000000 0.00000000 0.11863435 0.0000000
## 3 3.62742056 0.00000000 0.00000000 0.5568038 0.00000000 0.09989954 1.0865073
## 4 0.62097934 0.00000000 0.55680378 0.0000000 0.00000000 0.10290933 0.6462802
## 5 0.00000000 0.00000000 0.00000000 0.0000000 0.00000000 0.06926633 0.0000000
## 6 0.09852169 0.11863435 0.09989954 0.1029093 0.06926633 0.00000000 0.1095420
## 7 0.97852013 0.00000000 1.08650735 0.6462802 0.00000000 0.10954200 0.0000000
## 8 1.44957603 0.00000000 1.88505281 0.4353363 0.00000000 0.09678024 0.7253062
## 9 0.81416538 0.00000000 1.01955330 0.4399210 0.00000000 0.10893474 1.3246900
## 10 0.56969546 0.00000000 0.50139277 1.7266341 0.00000000 0.09731496 0.5039279
## 11 0.11800342 0.12928748 0.11862559 0.1330410 0.00000000 0.26513175 0.1330811
## 12 1.41954436 0.00000000 1.92289761 0.4340808 0.00000000 0.09737439 0.7473083
## 13 0.13730527 0.10920382 0.13643844 0.1681636 0.00000000 0.15347342 0.1537652
## 14 0.35111263 0.00000000 0.32135694 0.5722368 0.00000000 0.08958876 0.3116663
## 15 0.13233570 0.09722357 0.13020500 0.1666956 0.00000000 0.11442255 0.1425256
## 16 0.12779249 0.09129883 0.12533822 0.1606321 0.00000000 0.10263726 0.1351404
## 17 0.25674647 0.07521059 0.26934412 0.2503847 0.00000000 0.15660822 0.3432690
## 18 0.75619185 0.00000000 0.93733371 0.3541786 0.00000000 0.10029732 0.6517610
## [,8] [,9] [,10] [,11] [,12] [,13] [,14]
## 1 1.44957603 0.8141654 0.56969546 0.1180034 1.41954436 0.1373053 0.35111263
## 2 0.00000000 0.0000000 0.00000000 0.1292875 0.00000000 0.1092038 0.00000000
## 3 1.88505281 1.0195533 0.50139277 0.1186256 1.92289761 0.1364384 0.32135694
## 4 0.43533628 0.4399210 1.72663406 0.1330410 0.43408077 0.1681636 0.57223679
## 5 0.00000000 0.0000000 0.00000000 0.0000000 0.00000000 0.0000000 0.00000000
## 6 0.09678024 0.1089347 0.09731496 0.2651318 0.09737439 0.1534734 0.08958876
## 7 0.72530618 1.3246900 0.50392795 0.1330811 0.74730833 0.1537652 0.31166630
## 8 0.00000000 0.8621507 0.41099713 0.1125741 14.20564389 0.1274408 0.28759202
## 9 0.86215070 0.0000000 0.37657023 0.1271300 0.91392729 0.1413511 0.25899001
## 10 0.41099713 0.3765702 0.00000000 0.1251555 0.40716188 0.1591466 0.81565615
## 11 0.11257408 0.1271300 0.12515549 0.0000000 0.11318081 0.3535419 0.11656432
## 12 14.20564389 0.9139273 0.40716188 0.1131808 0.00000000 0.1279142 0.28459037
## 13 0.12744082 0.1413511 0.15914659 0.3535419 0.12791421 0.0000000 0.15293233
## 14 0.28759202 0.2589900 0.81565615 0.1165643 0.28459037 0.1529323 0.00000000
## 15 0.12180766 0.1297099 0.16275513 0.1992195 0.12196455 0.4492426 0.16757608
## 16 0.11764355 0.1230896 0.15925540 0.1660740 0.11767824 0.3097546 0.16934398
## 17 0.25148006 0.3550344 0.21908163 0.1783567 0.25570440 0.1743592 0.17482633
## 18 1.32314347 1.0689825 0.32677117 0.1137768 1.43283370 0.1250865 0.23980870
## [,15] [,16] [,17] [,18]
## 1 0.13233570 0.12779249 0.25674647 0.7561919
## 2 0.09722357 0.09129883 0.07521059 0.0000000
## 3 0.13020500 0.12533822 0.26934412 0.9373337
## 4 0.16669561 0.16063206 0.25038472 0.3541786
## 5 0.00000000 0.00000000 0.00000000 0.0000000
## 6 0.11442255 0.10263726 0.15660822 0.1002973
## 7 0.14252563 0.13514042 0.34326902 0.6517610
## 8 0.12180766 0.11764355 0.25148006 1.3231435
## 9 0.12970988 0.12308957 0.35503439 1.0689825
## 10 0.16275513 0.15925540 0.21908163 0.3267712
## 11 0.19921947 0.16607404 0.17835669 0.1137768
## 12 0.12196455 0.11767824 0.25570440 1.4328337
## 13 0.44924262 0.30975459 0.17435920 0.1250865
## 14 0.16757608 0.16934398 0.17482633 0.2398087
## 15 0.00000000 0.99621325 0.14028230 0.1169797
## 16 0.99621325 0.00000000 0.12753007 0.1122088
## 17 0.14028230 0.12753007 0.00000000 0.2789139
## 18 0.11697965 0.11220883 0.27891385 0.0000000
We do not need to get critical threshold
knn2nb(knearneigh(coords,k=3))
test.nb_k<- nbdists(test.nb_k, coords, longlat = TRUE)
k.distances <- lapply(k.distances, function(x) (1/(x/1)))
invd2a <-1] invd2a[
## [[1]]
## [1] 3.627421 1.449576 1.419544
nb2listw(test.nb_k, glist = invd2a,style = "B")
invd.weights.knn <-plot(invd.weights.knn, coords, lwd=.2, col="blue", cex = .5)
Four-year colleges in Philadelphia with weights
Once more, to see the weights, we can do the following
listw2mat(invd.weights.knn)
w4 = w4
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## 1 0.0000000 0 3.627421 0.0000000 0 0.00000000 0.0000000 1.449576
## 2 0.0000000 0 0.000000 0.0000000 0 0.11863435 0.0000000 0.000000
## 3 3.6274206 0 0.000000 0.0000000 0 0.00000000 0.0000000 1.885053
## 4 0.6209793 0 0.000000 0.0000000 0 0.00000000 0.6462802 0.000000
## 5 0.0000000 0 0.000000 0.0000000 0 0.06926633 0.0000000 0.000000
## 6 0.0000000 0 0.000000 0.0000000 0 0.00000000 0.0000000 0.000000
## 7 0.9785201 0 1.086507 0.0000000 0 0.00000000 0.0000000 0.000000
## 8 1.4495760 0 1.885053 0.0000000 0 0.00000000 0.0000000 0.000000
## 9 0.0000000 0 0.000000 0.0000000 0 0.00000000 1.3246900 0.000000
## 10 0.5696955 0 0.000000 1.7266341 0 0.00000000 0.0000000 0.000000
## 11 0.0000000 0 0.000000 0.0000000 0 0.26513175 0.0000000 0.000000
## 12 0.0000000 0 1.922898 0.0000000 0 0.00000000 0.0000000 14.205644
## 13 0.0000000 0 0.000000 0.0000000 0 0.00000000 0.0000000 0.000000
## 14 0.3511126 0 0.000000 0.5722368 0 0.00000000 0.0000000 0.000000
## 15 0.0000000 0 0.000000 0.0000000 0 0.00000000 0.0000000 0.000000
## 16 0.0000000 0 0.000000 0.0000000 0 0.00000000 0.0000000 0.000000
## 17 0.0000000 0 0.000000 0.0000000 0 0.00000000 0.3432690 0.000000
## 18 0.0000000 0 0.000000 0.0000000 0 0.00000000 0.0000000 1.323143
## [,9] [,10] [,11] [,12] [,13] [,14] [,15]
## 1 0.0000000 0.0000000 0.0000000 1.4195444 0.0000000 0.0000000 0.0000000
## 2 0.0000000 0.0000000 0.1292875 0.0000000 0.1092038 0.0000000 0.0000000
## 3 0.0000000 0.0000000 0.0000000 1.9228976 0.0000000 0.0000000 0.0000000
## 4 0.0000000 1.7266341 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## 5 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## 6 0.0000000 0.0000000 0.2651318 0.0000000 0.1534734 0.0000000 0.0000000
## 7 1.3246900 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## 8 0.0000000 0.0000000 0.0000000 14.2056439 0.0000000 0.0000000 0.0000000
## 9 0.0000000 0.0000000 0.0000000 0.9139273 0.0000000 0.0000000 0.0000000
## 10 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.8156561 0.0000000
## 11 0.0000000 0.0000000 0.0000000 0.0000000 0.3535419 0.0000000 0.1992195
## 12 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## 13 0.0000000 0.0000000 0.3535419 0.0000000 0.0000000 0.0000000 0.4492426
## 14 0.0000000 0.8156561 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## 15 0.0000000 0.0000000 0.1992195 0.0000000 0.4492426 0.0000000 0.0000000
## 16 0.0000000 0.0000000 0.0000000 0.0000000 0.3097546 0.1693440 0.9962132
## 17 0.3550344 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## 18 1.0689825 0.0000000 0.0000000 1.4328337 0.0000000 0.0000000 0.0000000
## [,16] [,17] [,18]
## 1 0.0000000 0.00000000 0.00000000
## 2 0.0000000 0.00000000 0.00000000
## 3 0.0000000 0.00000000 0.00000000
## 4 0.0000000 0.00000000 0.00000000
## 5 0.0000000 0.05866126 0.05193565
## 6 0.0000000 0.15660822 0.00000000
## 7 0.0000000 0.00000000 0.00000000
## 8 0.0000000 0.00000000 0.00000000
## 9 0.0000000 0.00000000 1.06898254
## 10 0.0000000 0.00000000 0.00000000
## 11 0.0000000 0.00000000 0.00000000
## 12 0.0000000 0.00000000 1.43283370
## 13 0.3097546 0.00000000 0.00000000
## 14 0.0000000 0.00000000 0.00000000
## 15 0.9962132 0.00000000 0.00000000
## 16 0.0000000 0.00000000 0.00000000
## 17 0.0000000 0.00000000 0.27891385
## 18 0.0000000 0.00000000 0.00000000
Anselin, Luc. 2020. Spatial Data Science. University of Chicago Center for Spatial Data Science. https://spatialanalysis.github.io/tutorials/.
González Canché, Manuel S. 2019. “Geographical, Statistical, and Qualitative Network Analysis: A Multifaceted Method-Bridging Tool to Reveal and Model Meaningful Structures in Education Research.” In Higher Education: Handbook of Theory and Research: Volume 34, edited by Michael B. Paulsen and Laura W. Perna, 535–634. Cham: Springer International Publishing. https://doi.org/10.1007/978-3-030-03457-3_12.
Walker, Kyle. 2020. Tigris: Load Census Tiger/Line Shapefiles. https://CRAN.R-project.org/package=tigris.