#A. Download ACS data for the year 2015 for Los Angeles County, California census tracts in R using the tidycensus library ##a.In this extract, request both the proportion of the population that is Non-Hispanic Black and the proportion of the population that is Hispanic
library(tidycensus)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.2 v dplyr 1.0.6
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 1.4.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
la_acs<-get_acs(geography = "tract", state="CA", county = c("Los Angeles"), year = 2015,
variables=c("DP05_0033PE", "DP05_0066PE"),
geometry = T, output = "wide")
## 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)`.
## Using the ACS Data Profile
head(la_acs)
## Simple feature collection with 6 features and 6 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -118.5152 ymin: 34.20656 xmax: -118.343 ymax: 34.29499
## Geodetic CRS: NAD83
## GEOID NAME DP05_0033PE
## 1 06037102105 Census Tract 1021.05, Los Angeles County, California 2.7
## 2 06037104500 Census Tract 1045, Los Angeles County, California 0.0
## 3 06037104822 Census Tract 1048.22, Los Angeles County, California 1.7
## 4 06037106642 Census Tract 1066.42, Los Angeles County, California 2.7
## 5 06037109601 Census Tract 1096.01, Los Angeles County, California 2.6
## 6 06037111100 Census Tract 1111, Los Angeles County, California 6.0
## DP05_0033PM DP05_0066PE DP05_0066PM geometry
## 1 2.8 66.8 5.1 MULTIPOLYGON (((-118.3529 3...
## 2 1.1 99.0 0.8 MULTIPOLYGON (((-118.4321 3...
## 3 2.6 87.4 7.6 MULTIPOLYGON (((-118.4258 3...
## 4 1.5 24.6 6.3 MULTIPOLYGON (((-118.5152 3...
## 5 1.6 70.4 4.4 MULTIPOLYGON (((-118.4587 3...
## 6 4.8 38.1 4.4 MULTIPOLYGON (((-118.5022 3...
la_acs2 <- la_acs
la_acs2$blckprop <- la_acs$DP05_0033PE
la_acs2$hisprop <- la_acs$DP05_0066PE
#removing missing/ without these the poly2nd won't work
library(dplyr)
la_acs2 <- la_acs2 %>%
filter(complete.cases(blckprop)) %>%
filter(complete.cases(hisprop))
library(dplyr)
library(ggplot2)
la_acs2%>%
mutate(blckprop=cut(blckprop, breaks = quantile(la_acs2$blckprop, p=c(0, .25, .5, .75, 1)),include.lowest = T))%>%
ggplot()+geom_sf(aes(fill=blckprop, color=blckprop))+
scale_fill_brewer(palette = "Blues")+scale_color_brewer(palette = "Blues")+
ggtitle("Proportion of black population in Los Angels, California, 2015")
library(dplyr)
la_acs2%>%
mutate(hisprop=cut(hisprop, breaks = quantile(la_acs2$hisprop, p=c(0, .25, .5, .75, 1)),include.lowest = T))%>%
ggplot()+geom_sf(aes(fill=hisprop, color=hisprop))+
scale_fill_brewer(palette = "Blues")+scale_color_brewer(palette = "Blues")+
ggtitle("Propotion of black population in Los Angels, California, 2015")
Now we Make a rook style weight matrix and a k-nearest neighbor adjacency matrix, with k=4
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')`
## Loading required package: sf
## Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
lanb<-poly2nb(la_acs2, queen=T)
summary(lanb)
## 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:
## 145 264 1330 1331 with 1 link
## 1 most connected region:
## 1015 with 25 links
lalw<-nb2listw(lanb, style="W")
#the style = W row-standardizes the matrix
#make a k-nearest neighbor weight set, with k=4
knn<-knearneigh(x = coordinates(as(la_acs2, "Spatial")), k = 4)
knn4<-knn2nb(knn = knn)
knn4lw <- nb2listw(knn4)
plot(as(la_acs2, "Spatial"),
main="Queen Neighbors")
plot(lalw,
coords=coordinates(as(la_acs2, "Spatial")),
add=T,
col=2)
plot(as(la_acs2, "Spatial"), main="k=4 Neighbors")
plot(la_acs2, coords=coordinates(as(knn4lw, "Spatial")), add=T, col=2)
moran.test(la_acs2$blckprop,
listw=lalw)
##
## Moran I test under randomisation
##
## data: la_acs2$blckprop
## weights: lalw
##
## Moran I statistic standard deviate = 66.499, p-value < 2.2e-16
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic Expectation Variance
## 0.7869649712 -0.0004301075 0.0001402028
moran.test(la_acs2$hisprop,
listw=lalw)
##
## Moran I test under randomisation
##
## data: la_acs2$hisprop
## weights: lalw
##
## Moran I statistic standard deviate = 68.81, p-value < 2.2e-16
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic Expectation Variance
## 0.8165201722 -0.0004301075 0.0001409572
#Moran’s I calculation for k4 neighbors
moran.test(la_acs2$blckprop,
listw=knn4lw)
##
## Moran I test under randomisation
##
## data: la_acs2$blckprop
## weights: knn4lw
##
## Moran I statistic standard deviate = 56.372, p-value < 2.2e-16
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic Expectation Variance
## 0.7765037114 -0.0004301075 0.0001899490
moran.test(la_acs2$hisprop,
listw=knn4lw)
##
## Moran I test under randomisation
##
## data: la_acs2$hisprop
## 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.8315955900 -0.0004301075 0.0001909709
locali<-localmoran(la_acs2$blckprop, knn4lw, p.adjust.method="fdr")
la_acs2$locali<-locali[,1]
la_acs2$localp<-locali[,5]
la_acs2$cl<-as.factor(ifelse(la_acs2$localp<=.05,"Clustered","NotClustered"))
la_acs2%>%
ggplot()+
geom_sf(aes(fill = cl))+
ggtitle(label = "Local Moran's I Value-Los Angeles County, California proportion of NH Black" )
locali<-localmoran(la_acs2$hisprop, knn4lw, p.adjust.method="fdr")
la_acs2$locali<-locali[,1]
la_acs2$localp<-locali[,5]
la_acs2$cl<-as.factor(ifelse(la_acs2$localp<=.05,"Clustered","NotClustered"))
la_acs2%>%
ggplot()+
geom_sf(aes(fill = cl))+
ggtitle(label = "Local Moran's I Value-Los Angeles County, California proportion of Hispanic" )