Instructions:

In this assignment you will:

Download ACS Data

#Download ACS data for the year 2015 for Los Angeles County, California census tracts in R using the tidycensus library
#In this extract, request both the proportion of the population that is Non-Hispanic Black and the proportion of the population that is Hispanic

#Use this to search for variables
acs2015 <- load_variables(2015 , "acs5", cache = TRUE) 

#demographic profile tables

LAtracts <- get_acs(geography = "tract",
                    year = 2015,
                    state = "CA", 
                    county = "Los Angeles", 
                    variables = c("B01001_001E","DP05_0071PE","DP05_0078PE"),
                    output ="wide", 
                    geometry = TRUE)%>%
  rename(pctHispLat =DP05_0071PE, pctBlackNH = DP05_0078PE, totpop =B01001_001E)
## Getting data from the 2011-2015 5-year ACS
## Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
## Fetching data by table type ("B/C", "S", "DP") and combining the result.
#Remove NAs
LAtracts <- na.omit(LAtracts)

Mapping the ACS Data

The following two maps show, in quantiles, the percent Black, NonHispanic and the percentage Hispanic/Latino for LA Census Tracts. Using the quantile breaks is not as effective for the Black population, as it seems to be less highly clustered (the highest percentage for a tract is 25% Black NH, compared to 100% Hispanic/Latino).

#Scatterplot of PHispanic by PBlackNH
library(ggplot2)
qplot(y = LAtracts$pctBlackNH,
      x=LAtracts$pctHispLat )

library(tmap)
  tm_shape(LAtracts)+
    tm_polygons("pctHispLat",
              title="Percent Hispanic/Latino, 2015", 
              palette="Blues", 
              border.alpha = 0, 
              style="quantile",
              legend.hist=T )  +
    tm_layout(legend.outside = TRUE)
## Linking to GEOS 3.8.1, GDAL 3.1.4, PROJ 6.3.1

    tm_shape(LAtracts)+
    tm_polygons("pctBlackNH",
              title="Percent Black, NonHispanic, 2015", 
              palette="Blues", 
              border.alpha = 0, 
              style="quantile",
              legend.hist=T )  +
    tm_layout(legend.outside = TRUE)

Making Queen and KNN Matrices

This tripped me up a bit, because the KNN option refused to create a “listw” object. I think I found the solution with the “nb2listw” function.

#Queen Style Matrix
library(spdep)
## 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')`
Qmatrix<-poly2nb(LAtracts, queen=T)
summary(Qmatrix)
## Neighbour list object:
## Number of regions: 2326 
## Number of nonzero links: 14794 
## Percentage nonzero weights: 0.2734426 
## Average number of links: 6.360275 
## Link number distribution:
## 
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  19  20  25 
##   4   9  49 200 464 609 494 273 134  51  23   5   2   2   2   2   1   1   1 
## 4 least connected regions:
## 1880 1938 1939 2027 with 1 link
## 1 most connected region:
## 1166 with 25 links
QmatrixW<-nb2listw(Qmatrix, style="W")
#the style = W row-standardizes the matrix

#make a k-nearest neighbor weight set, with k=4
library(spdep)
knn<-knearneigh(x = coordinates(as(LAtracts, "Spatial")), k = 4)
knn4<-knn2nb(knn = knn)
knn4lw <- nb2listw(knn4)
#Kept getting error below while weighting that it needs to be a listw object, so I think this ^ changed it into one

Plotting the Relationships

The following two maps are showing the spatial relationships for the Queen-based contiguity and the K-nearest neighbors. They’re not as effective as they could be if I had taken the time to find the correct lat and long for a zoom map but that’s for another day. ;)

plot(as(LAtracts, "Spatial"),
     main="Queen Neighbors")
plot(QmatrixW,
     coords=coordinates(as(LAtracts, "Spatial")),
     add=T,
     col=2)

plot(as(LAtracts, "Spatial"),
     main="k=4 Neighbors")
plot(knn4,
     coords=coordinates(as(LAtracts, "Spatial")),
     add=T,
     col=2)

Weighting Data by Queen and K=4

This section is creating the spatial lag/weight for the Queen and K-nearest neighbor (k=4) methods for both the Hispanic and Black, NH populations.

#Weigthing 1: Queen
##Hisp/Latino
wmatQ<-nb2mat(Qmatrix, style="W")
LAtracts$hisp.wq<-wmatQ%*%(LAtracts$pctHispLat)
LAtracts$hispwq<-lag.listw(x= QmatrixW, 
                     var = LAtracts$pctHispLat,)
head(LAtracts[, c("pctHispLat", "hispwq")])
pctHispLathispwqgeometry
66.373.6list(list(c(-118.300753, -118.300758, -118.297919, -118.291309, -118.288467, -118.286486, -118.284913, -118.28498, -118.285924, -118.287333, -118.291044, -118.299451, -118.302291, -118.300753, 34.259609, 34.263214, 34.263222, 34.262854, 34.26282, 34.262797, 34.262463, 34.255889, 34.255895, 34.255904, 34.255927, 34.255978, 34.258697, 34.259609)))
87.671.3list(list(c(-118.303195, -118.299696, -118.299749, -118.29697, -118.289899, -118.290225, -118.285616, -118.285271, -118.281666, -118.278953, -118.279483, -118.277285, -118.277422, -118.278125, -118.278167, -118.28498, -118.284913, -118.286486, -118.288467, -118.291309, -118.297919, -118.297917, -118.302753, -118.303195, 34.273334, 34.275262, 34.276706, 34.278807, 34.278946, 34.271, 34.270541, 34.266434, 34.266011, 34.266541, 34.26363, 34.26363, 34.259903, 34.257602, 34.255768, 34.255889, 34.262463,
34.262797, 34.26282, 34.262854, 34.263222, 34.266849, 34.272001, 34.273334)))
51.273.6list(list(c(-118.299451, -118.291044, -118.287333, -118.285924, -118.285925, -118.285924, -118.287591, -118.28862, -118.290104, -118.291215, -118.294696, -118.297919, -118.299451, 34.255978, 34.255927, 34.255904, 34.255895, 34.252274, 34.248018, 34.248173, 34.248614, 34.249364, 34.250225, 34.252336, 34.254511, 34.255978)))
74.174.1list(list(c(-118.285925, -118.285924, -118.28498, -118.278167, -118.278224, -118.276916, -118.276915, -118.278224, -118.277611, -118.276103, -118.27975, -118.285924, -118.285925, 34.252274, 34.255895, 34.255889, 34.255768, 34.253291, 34.253084, 34.251582, 34.251657, 34.249589, 34.246484, 34.247663, 34.248018, 34.252274)))
89.676.8list(list(c(-118.278224, -118.276915, -118.276916, -118.278224, -118.278167, -118.278125, -118.277422, -118.26529, -118.265238, -118.265264, -118.265295, -118.266672, -118.265999, -118.266016, -118.266024, -118.266733, -118.266803, -118.272473, -118.270685, -118.270474, -118.273615, -118.273907, -118.276295, -118.276103, -118.277611, -118.278224, 34.251657, 34.251582, 34.253084, 34.253291, 34.255768, 34.257602, 34.259903, 34.259954, 34.254617, 34.25238, 34.250402, 34.250779, 34.243892, 34.241535,
34.240356, 34.240796, 34.231235, 34.232527, 34.232995, 34.236937, 34.241304, 34.242918, 34.245152, 34.246484, 34.249589, 34.251657)))
75.772.3list(list(c(-118.322382, -118.316711, -118.312384, -118.307074, -118.306236, -118.30796, -118.307037, -118.305772, -118.302632, -118.300253, -118.300254, -118.299451, -118.297919, -118.294696, -118.291215, -118.290104, -118.28862, -118.287591, -118.285924, -118.27975, -118.276103, -118.276295, -118.273907, -118.273615, -118.270474, -118.270685, -118.272473, -118.280036, -118.28606, -118.290771, -118.298148, -118.300392, -118.302303, -118.308004, -118.31116, -118.316559, -118.319387, -118.321123,
-118.322382, 34.249631, 34.248602, 34.248867, 34.248124, 34.251114, 34.252408, 34.2524, 34.252082, 34.252388, 34.252869, 34.255983, 34.255978, 34.254511, 34.252336, 34.250225, 34.249364, 34.248614, 34.248173, 34.248018, 34.247663, 34.246484, 34.245152, 34.242918, 34.241304, 34.236937, 34.232995, 34.232527, 34.23421, 34.234236, 34.234821, 34.23462, 34.235064, 34.235841, 34.239841, 34.241379, 34.242724, 34.244418, 34.246415, 34.249631)))
##Black, NH
LAtracts$black.wq<-wmatQ%*%(LAtracts$pctBlackNH)
LAtracts$blackwq<-lag.listw(x= QmatrixW, 
                     var = LAtracts$pctBlackNH,)
head(LAtracts[, c("pctBlackNH", "blackwq")])
pctBlackNHblackwqgeometry
1.83.28list(list(c(-118.300753, -118.300758, -118.297919, -118.291309, -118.288467, -118.286486, -118.284913, -118.28498, -118.285924, -118.287333, -118.291044, -118.299451, -118.302291, -118.300753, 34.259609, 34.263214, 34.263222, 34.262854, 34.26282, 34.262797, 34.262463, 34.255889, 34.255895, 34.255904, 34.255927, 34.255978, 34.258697, 34.259609)))
1.72.98list(list(c(-118.303195, -118.299696, -118.299749, -118.29697, -118.289899, -118.290225, -118.285616, -118.285271, -118.281666, -118.278953, -118.279483, -118.277285, -118.277422, -118.278125, -118.278167, -118.28498, -118.284913, -118.286486, -118.288467, -118.291309, -118.297919, -118.297917, -118.302753, -118.303195, 34.273334, 34.275262, 34.276706, 34.278807, 34.278946, 34.271, 34.270541, 34.266434, 34.266011, 34.266541, 34.26363, 34.26363, 34.259903, 34.257602, 34.255768, 34.255889, 34.262463,
34.262797, 34.26282, 34.262854, 34.263222, 34.266849, 34.272001, 34.273334)))
4.73.27list(list(c(-118.299451, -118.291044, -118.287333, -118.285924, -118.285925, -118.285924, -118.287591, -118.28862, -118.290104, -118.291215, -118.294696, -118.297919, -118.299451, 34.255978, 34.255927, 34.255904, 34.255895, 34.252274, 34.248018, 34.248173, 34.248614, 34.249364, 34.250225, 34.252336, 34.254511, 34.255978)))
3.62.76list(list(c(-118.285925, -118.285924, -118.28498, -118.278167, -118.278224, -118.276916, -118.276915, -118.278224, -118.277611, -118.276103, -118.27975, -118.285924, -118.285925, 34.252274, 34.255895, 34.255889, 34.255768, 34.253291, 34.253084, 34.251582, 34.251657, 34.249589, 34.246484, 34.247663, 34.248018, 34.252274)))
0.94.5 list(list(c(-118.278224, -118.276915, -118.276916, -118.278224, -118.278167, -118.278125, -118.277422, -118.26529, -118.265238, -118.265264, -118.265295, -118.266672, -118.265999, -118.266016, -118.266024, -118.266733, -118.266803, -118.272473, -118.270685, -118.270474, -118.273615, -118.273907, -118.276295, -118.276103, -118.277611, -118.278224, 34.251657, 34.251582, 34.253084, 34.253291, 34.255768, 34.257602, 34.259903, 34.259954, 34.254617, 34.25238, 34.250402, 34.250779, 34.243892, 34.241535,
34.240356, 34.240796, 34.231235, 34.232527, 34.232995, 34.236937, 34.241304, 34.242918, 34.245152, 34.246484, 34.249589, 34.251657)))
4.72.47list(list(c(-118.322382, -118.316711, -118.312384, -118.307074, -118.306236, -118.30796, -118.307037, -118.305772, -118.302632, -118.300253, -118.300254, -118.299451, -118.297919, -118.294696, -118.291215, -118.290104, -118.28862, -118.287591, -118.285924, -118.27975, -118.276103, -118.276295, -118.273907, -118.273615, -118.270474, -118.270685, -118.272473, -118.280036, -118.28606, -118.290771, -118.298148, -118.300392, -118.302303, -118.308004, -118.31116, -118.316559, -118.319387, -118.321123,
-118.322382, 34.249631, 34.248602, 34.248867, 34.248124, 34.251114, 34.252408, 34.2524, 34.252082, 34.252388, 34.252869, 34.255983, 34.255978, 34.254511, 34.252336, 34.250225, 34.249364, 34.248614, 34.248173, 34.248018, 34.247663, 34.246484, 34.245152, 34.242918, 34.241304, 34.236937, 34.232995, 34.232527, 34.23421, 34.234236, 34.234821, 34.23462, 34.235064, 34.235841, 34.239841, 34.241379, 34.242724, 34.244418, 34.246415, 34.249631)))
#Weigthing 2: KNN K=4
##Hisp/Latino
wmatK4<-nb2mat(knn4, style="W")
LAtracts$hisp.wk<-wmatK4%*%(LAtracts$pctHispLat)
LAtracts$hispwk<-lag.listw(x= knn4lw, 
                     var = LAtracts$pctHispLat,)
head(LAtracts[, c("pctHispLat", "hispwk")])
pctHispLathispwkgeometry
66.372  list(list(c(-118.300753, -118.300758, -118.297919, -118.291309, -118.288467, -118.286486, -118.284913, -118.28498, -118.285924, -118.287333, -118.291044, -118.299451, -118.302291, -118.300753, 34.259609, 34.263214, 34.263222, 34.262854, 34.26282, 34.262797, 34.262463, 34.255889, 34.255895, 34.255904, 34.255927, 34.255978, 34.258697, 34.259609)))
87.660.6list(list(c(-118.303195, -118.299696, -118.299749, -118.29697, -118.289899, -118.290225, -118.285616, -118.285271, -118.281666, -118.278953, -118.279483, -118.277285, -118.277422, -118.278125, -118.278167, -118.28498, -118.284913, -118.286486, -118.288467, -118.291309, -118.297919, -118.297917, -118.302753, -118.303195, 34.273334, 34.275262, 34.276706, 34.278807, 34.278946, 34.271, 34.270541, 34.266434, 34.266011, 34.266541, 34.26363, 34.26363, 34.259903, 34.257602, 34.255768, 34.255889, 34.262463,
34.262797, 34.26282, 34.262854, 34.263222, 34.266849, 34.272001, 34.273334)))
51.275.9list(list(c(-118.299451, -118.291044, -118.287333, -118.285924, -118.285925, -118.285924, -118.287591, -118.28862, -118.290104, -118.291215, -118.294696, -118.297919, -118.299451, 34.255978, 34.255927, 34.255904, 34.255895, 34.252274, 34.248018, 34.248173, 34.248614, 34.249364, 34.250225, 34.252336, 34.254511, 34.255978)))
74.170.7list(list(c(-118.285925, -118.285924, -118.28498, -118.278167, -118.278224, -118.276916, -118.276915, -118.278224, -118.277611, -118.276103, -118.27975, -118.285924, -118.285925, 34.252274, 34.255895, 34.255889, 34.255768, 34.253291, 34.253084, 34.251582, 34.251657, 34.249589, 34.246484, 34.247663, 34.248018, 34.252274)))
89.674.7list(list(c(-118.278224, -118.276915, -118.276916, -118.278224, -118.278167, -118.278125, -118.277422, -118.26529, -118.265238, -118.265264, -118.265295, -118.266672, -118.265999, -118.266016, -118.266024, -118.266733, -118.266803, -118.272473, -118.270685, -118.270474, -118.273615, -118.273907, -118.276295, -118.276103, -118.277611, -118.278224, 34.251657, 34.251582, 34.253084, 34.253291, 34.255768, 34.257602, 34.259903, 34.259954, 34.254617, 34.25238, 34.250402, 34.250779, 34.243892, 34.241535,
34.240356, 34.240796, 34.231235, 34.232527, 34.232995, 34.236937, 34.241304, 34.242918, 34.245152, 34.246484, 34.249589, 34.251657)))
75.767.5list(list(c(-118.322382, -118.316711, -118.312384, -118.307074, -118.306236, -118.30796, -118.307037, -118.305772, -118.302632, -118.300253, -118.300254, -118.299451, -118.297919, -118.294696, -118.291215, -118.290104, -118.28862, -118.287591, -118.285924, -118.27975, -118.276103, -118.276295, -118.273907, -118.273615, -118.270474, -118.270685, -118.272473, -118.280036, -118.28606, -118.290771, -118.298148, -118.300392, -118.302303, -118.308004, -118.31116, -118.316559, -118.319387, -118.321123,
-118.322382, 34.249631, 34.248602, 34.248867, 34.248124, 34.251114, 34.252408, 34.2524, 34.252082, 34.252388, 34.252869, 34.255983, 34.255978, 34.254511, 34.252336, 34.250225, 34.249364, 34.248614, 34.248173, 34.248018, 34.247663, 34.246484, 34.245152, 34.242918, 34.241304, 34.236937, 34.232995, 34.232527, 34.23421, 34.234236, 34.234821, 34.23462, 34.235064, 34.235841, 34.239841, 34.241379, 34.242724, 34.244418, 34.246415, 34.249631)))
##Black, NH
LAtracts$black.wk<-wmatK4%*%(LAtracts$pctBlackNH)
LAtracts$blackwk<-lag.listw(x= knn4lw, 
                     var = LAtracts$pctBlackNH,)
head(LAtracts[, c("pctBlackNH", "blackwk")])
pctBlackNHblackwkgeometry
1.83   list(list(c(-118.300753, -118.300758, -118.297919, -118.291309, -118.288467, -118.286486, -118.284913, -118.28498, -118.285924, -118.287333, -118.291044, -118.299451, -118.302291, -118.300753, 34.259609, 34.263214, 34.263222, 34.262854, 34.26282, 34.262797, 34.262463, 34.255889, 34.255895, 34.255904, 34.255927, 34.255978, 34.258697, 34.259609)))
1.74.4 list(list(c(-118.303195, -118.299696, -118.299749, -118.29697, -118.289899, -118.290225, -118.285616, -118.285271, -118.281666, -118.278953, -118.279483, -118.277285, -118.277422, -118.278125, -118.278167, -118.28498, -118.284913, -118.286486, -118.288467, -118.291309, -118.297919, -118.297917, -118.302753, -118.303195, 34.273334, 34.275262, 34.276706, 34.278807, 34.278946, 34.271, 34.270541, 34.266434, 34.266011, 34.266541, 34.26363, 34.26363, 34.259903, 34.257602, 34.255768, 34.255889, 34.262463,
34.262797, 34.26282, 34.262854, 34.263222, 34.266849, 34.272001, 34.273334)))
4.72.95list(list(c(-118.299451, -118.291044, -118.287333, -118.285924, -118.285925, -118.285924, -118.287591, -118.28862, -118.290104, -118.291215, -118.294696, -118.297919, -118.299451, 34.255978, 34.255927, 34.255904, 34.255895, 34.252274, 34.248018, 34.248173, 34.248614, 34.249364, 34.250225, 34.252336, 34.254511, 34.255978)))
3.63.02list(list(c(-118.285925, -118.285924, -118.28498, -118.278167, -118.278224, -118.276916, -118.276915, -118.278224, -118.277611, -118.276103, -118.27975, -118.285924, -118.285925, 34.252274, 34.255895, 34.255889, 34.255768, 34.253291, 34.253084, 34.251582, 34.251657, 34.249589, 34.246484, 34.247663, 34.248018, 34.252274)))
0.94.05list(list(c(-118.278224, -118.276915, -118.276916, -118.278224, -118.278167, -118.278125, -118.277422, -118.26529, -118.265238, -118.265264, -118.265295, -118.266672, -118.265999, -118.266016, -118.266024, -118.266733, -118.266803, -118.272473, -118.270685, -118.270474, -118.273615, -118.273907, -118.276295, -118.276103, -118.277611, -118.278224, 34.251657, 34.251582, 34.253084, 34.253291, 34.255768, 34.257602, 34.259903, 34.259954, 34.254617, 34.25238, 34.250402, 34.250779, 34.243892, 34.241535,
34.240356, 34.240796, 34.231235, 34.232527, 34.232995, 34.236937, 34.241304, 34.242918, 34.245152, 34.246484, 34.249589, 34.251657)))
4.73.27list(list(c(-118.322382, -118.316711, -118.312384, -118.307074, -118.306236, -118.30796, -118.307037, -118.305772, -118.302632, -118.300253, -118.300254, -118.299451, -118.297919, -118.294696, -118.291215, -118.290104, -118.28862, -118.287591, -118.285924, -118.27975, -118.276103, -118.276295, -118.273907, -118.273615, -118.270474, -118.270685, -118.272473, -118.280036, -118.28606, -118.290771, -118.298148, -118.300392, -118.302303, -118.308004, -118.31116, -118.316559, -118.319387, -118.321123,
-118.322382, 34.249631, 34.248602, 34.248867, 34.248124, 34.251114, 34.252408, 34.2524, 34.252082, 34.252388, 34.252869, 34.255983, 34.255978, 34.254511, 34.252336, 34.250225, 34.249364, 34.248614, 34.248173, 34.248018, 34.247663, 34.246484, 34.245152, 34.242918, 34.241304, 34.236937, 34.232995, 34.232527, 34.23421, 34.234236, 34.234821, 34.23462, 34.235064, 34.235841, 34.239841, 34.241379, 34.242724, 34.244418, 34.246415, 34.249631)))

Mapping the Weighted Results

This section provides a series of three maps for both the Hispanic and Black, NH populations, showing the differences between the Queen, K=4, and Raw data.

library(RColorBrewer)
#lwd = 0 removes borders around tracts
h1<-LAtracts%>%
  ggplot()+
  geom_sf(aes(fill = hisp.wq), lwd = 0)+
  ggtitle("Queen Spatial Lag-Pct Hispanic")

h2<-LAtracts%>%
  ggplot()+
  geom_sf(aes(fill = hisp.wk), lwd = 0)+
  ggtitle("K=4 Spatial Lag-Pct Hispanic")

h3<-LAtracts%>%
  ggplot()+
  geom_sf(aes(fill = pctHispLat), lwd = 0)+
  ggtitle("Raw-Pct Hispanic")

b1<-LAtracts%>%
  ggplot()+
  geom_sf(aes(fill = black.wq), lwd = 0)+
  ggtitle("Queen Spatial Lag-Pct Black NH")

b2<-LAtracts%>%
  ggplot()+
  geom_sf(aes(fill = black.wk), lwd = 0)+
  ggtitle("K=4 Spatial Lag-Pct Black NH")

b3<-LAtracts%>%
  ggplot()+
  geom_sf(aes(fill = pctBlackNH), lwd = 0)+
  ggtitle("Raw-Pct Black NH")

library(patchwork)
## 
## Attaching package: 'patchwork'
## The following object is masked from 'package:MASS':
## 
##     area
h1+h2+h3

b1+b2+b3

## Calculating Global Moran’s I The Global Moran’s I tells us whether there is spatial autocorrelation within our data, with a value of -1 representing complete clustering of unlike values (dispersion), 0 representing complete randomness, and 1 representing complete clustering with like values. Based on the results of the below, we see that all of the p-values are significant for the four calculated Global Moran’s I statistics, each with values above 0. This means we can reject the null hypothesis that there is not spatial autocorrelation within the variables, for both the Black and Hispanic population using both weighting methods. The Queen and K=4 results are similar, but not identical. The Percent Hispanic population has higher Moran’s I values (Queen=0.817 and KNN=0.832) than those for the Percent Black population (Queen=0.395 and KNN=0.416) suggesting that the Hispanic population is more spatially clustered than the Black population on the county-wide scale. However, the Black population does have significant I values, meaning there is clustering occuring.

#Listw Queen = QmatrixW
#Listw KNN = knn4lw

#Queen - Pct Hispanic - Moran I = 0.817
moran.test(LAtracts$pctHispLat, 
           listw=QmatrixW)
## 
##  Moran I test under randomisation
## 
## data:  LAtracts$pctHispLat  
## weights: QmatrixW    
## 
## Moran I statistic standard deviate = 68.81, p-value < 2.2e-16
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic       Expectation          Variance 
##      0.8165160858     -0.0004301075      0.0001409572
moran.mc(LAtracts$pctHispLat,
         listw=QmatrixW,
         nsim=999)
## 
##  Monte-Carlo simulation of Moran I
## 
## data:  LAtracts$pctHispLat 
## weights: QmatrixW  
## number of simulations + 1: 1000 
## 
## statistic = 0.81652, observed rank = 1000, p-value = 0.001
## alternative hypothesis: greater
moran.plot(LAtracts$pctHispLat,
           listw=QmatrixW)

#Queen - Pct Black - Moran I = 0.395
moran.test(LAtracts$pctBlackNH, 
           listw=QmatrixW)
## 
##  Moran I test under randomisation
## 
## data:  LAtracts$pctBlackNH  
## weights: QmatrixW    
## 
## Moran I statistic standard deviate = 33.351, p-value < 2.2e-16
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic       Expectation          Variance 
##      0.3947038302     -0.0004301075      0.0001403648
moran.mc(LAtracts$pctBlackNH,
         listw=QmatrixW,
         nsim=999)
## 
##  Monte-Carlo simulation of Moran I
## 
## data:  LAtracts$pctBlackNH 
## weights: QmatrixW  
## number of simulations + 1: 1000 
## 
## statistic = 0.3947, observed rank = 1000, p-value = 0.001
## alternative hypothesis: greater
moran.plot(LAtracts$pctBlackNH,
           listw=QmatrixW)

#KNN - Pct Hispanic - Moran I = 0.832
moran.test(LAtracts$pctHispLat, 
           listw=knn4lw)
## 
##  Moran I test under randomisation
## 
## data:  LAtracts$pctHispLat  
## weights: knn4lw    
## 
## Moran I statistic standard deviate = 60.208, p-value < 2.2e-16
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic       Expectation          Variance 
##      0.8315918392     -0.0004301075      0.0001909709
moran.mc(LAtracts$pctHispLat,
         listw=knn4lw,
         nsim=999)
## 
##  Monte-Carlo simulation of Moran I
## 
## data:  LAtracts$pctHispLat 
## weights: knn4lw  
## number of simulations + 1: 1000 
## 
## statistic = 0.83159, observed rank = 1000, p-value = 0.001
## alternative hypothesis: greater
moran.plot(LAtracts$pctHispLat,
           listw=knn4lw)

#KNN - Pct Black - Moran I = 0.416
moran.test(LAtracts$pctBlackNH, 
           listw=knn4lw)
## 
##  Moran I test under randomisation
## 
## data:  LAtracts$pctBlackNH  
## weights: knn4lw    
## 
## Moran I statistic standard deviate = 30.166, p-value < 2.2e-16
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic       Expectation          Variance 
##      0.4155597307     -0.0004301075      0.0001901684
moran.mc(LAtracts$pctBlackNH,
         listw=knn4lw,
         nsim=999)
## 
##  Monte-Carlo simulation of Moran I
## 
## data:  LAtracts$pctBlackNH 
## weights: knn4lw  
## number of simulations + 1: 1000 
## 
## statistic = 0.41556, observed rank = 1000, p-value = 0.001
## alternative hypothesis: greater
moran.plot(LAtracts$pctBlackNH,
           listw=knn4lw)

Calculating Local Moran I for K=4 Neighbor List

The local Moran’s I tells us whether there is significant localized clustering of the variables by comparing the value of the tract with the value of it’s neighbors (here based on knn). Based on the maps below, there is significant clustering for both the Black and Hispanic population in Los Angeles. The Hispanic population appears to be clustered at larger geographies throughout the county, while the Black population is cluster in smaller neighborhoods (and is a smaller population overall).

#Listw KNN = knn4lw

#K=4 - Pct Hispanic
Hlocali<-localmoran(LAtracts$pctHispLat, knn4lw, p.adjust.method="fdr")
LAtracts$Hlocali<-Hlocali[,1]
LAtracts$Hlocalp<-Hlocali[,5]

#We have to make our own cluster identifiers
LAtracts$Hcl<-as.factor(ifelse(LAtracts$Hlocalp<=.05,"Clustered","NotClustered"))

#Plots of the results
lm1<- LAtracts%>%
  ggplot()+
  geom_sf(aes(fill = Hlocali), lwd = 0)+
  scale_fill_viridis_c()+
  ggtitle(label = "Local Moran's I Value - Pct Hispanic - K=4" )

#K=4 - Pct Hispanic
Blocali<-localmoran(LAtracts$pctBlackNH, knn4lw, p.adjust.method="fdr")
LAtracts$Blocali<-Blocali[,1]
LAtracts$Blocalp<-Blocali[,5]

#We have to make our own cluster identifiers
LAtracts$Bcl<-as.factor(ifelse(LAtracts$Blocalp<=.05,"Clustered","NotClustered"))

#Plots of the results
lm2<- LAtracts%>%
  ggplot()+
  geom_sf(aes(fill = Blocali), lwd = 0)+
  scale_fill_viridis_c()+
  ggtitle(label = "Local Moran's I Value - Pct Black, NH - K=4" )

lm1+lm2

#Cluster maps
c1<- LAtracts%>%
  ggplot()+
  geom_sf(aes(fill = Bcl), lwd = 0)+
  ggtitle("Moran Cluster Map -\nPercent Black, NH",
          sub="Los Angeles County, CA")

c2<- LAtracts%>%
  ggplot()+
  geom_sf(aes(fill = Hcl), lwd = 0)+
  ggtitle("Moran Cluster Map -\nPercent Hispanic",
          sub="Los Angeles County, CA")

c1+c2