In this assignment, I obtain the percent of vacant housing units for 2011 and 2019 ACS survey demographic profiles for Galveston County where I was born and raised.

library(tidycensus)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.3     v purrr   0.3.4
## v tibble  3.0.5     v dplyr   1.0.3
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.0
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(tmap)

My Census developer API Key

census_api_key(key =  "8c0b6ab768e3e309934cd84592d09c2ee27ce193", install = T,overwrite=TRUE )
## Your original .Renviron will be backed up and stored in your R HOME directory if needed.
## Your API key has been stored in your .Renviron and can be accessed by Sys.getenv("CENSUS_API_KEY"). 
## To use now, restart R or run `readRenviron("~/.Renviron")`
## [1] "8c0b6ab768e3e309934cd84592d09c2ee27ce193"
v11_Profile <- load_variables(2011, "acs5/profile", cache = TRUE) #demographic profile tables
v19_Profile <- load_variables(2019, "acs5/profile", cache = TRUE) #demographic 

#Search for variables by using grep()

v11_Profile[grep(x = v11_Profile$label, "PERCENT VACANT HOUSING UNITS", ignore.case = TRUE), c("name", "label")]
## # A tibble: 0 x 2
## # ... with 2 variables: name <chr>, label <chr>
v19_Profile[grep(x = v19_Profile$label, "PERCENT VACANT HOUSING UNITS", ignore.case = TRUE), c("name", "label")]
## # A tibble: 0 x 2
## # ... with 2 variables: name <chr>, label <chr>

Extract from ACS summary file data profile variables from 2011 and 2019 for Galveston County, TX Census Tracts

VHU11<-get_acs(geography = "tract",
                state="TX",
                county = "Galveston",
                year = 2011,
                variables="DP04_0003P",
                geometry = T,
                output = "wide")
## Getting data from the 2007-2011 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
VHU19<-get_acs(geography = "tract",
                state="TX",
                county = "Galveston",
                year = 2019,
                variables="DP04_0003P",
                geometry = T,
                output = "wide")
## Getting data from the 2015-2019 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
#rename variables and filter missing cases


VHU11 <- VHU11%>%
  mutate(VHU11 = DP04_0003PE,
         VHU11_er = DP04_0003PM/1.645,
         VHU11_cv =100* (VHU11_er/VHU11)) %>%
  filter(complete.cases(VHU11), is.finite(VHU11_cv)==T)

head(VHU11)
## Simple feature collection with 6 features and 7 fields
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: -95.23308 ymin: 29.29611 xmax: -94.92506 ymax: 29.52631
## geographic CRS: NAD83
##         GEOID                                          NAME DP04_0003PE
## 1 48167720302 Census Tract 7203.02, Galveston County, Texas         0.8
## 2 48167720502 Census Tract 7205.02, Galveston County, Texas         6.0
## 3 48167720501 Census Tract 7205.01, Galveston County, Texas         3.7
## 4 48167721202 Census Tract 7212.02, Galveston County, Texas         2.5
## 5 48167722002 Census Tract 7220.02, Galveston County, Texas         6.7
## 6 48167723502 Census Tract 7235.02, Galveston County, Texas         3.9
##   DP04_0003PM                       geometry VHU11  VHU11_er  VHU11_cv
## 1         1.4 POLYGON ((-95.23098 29.4754...   0.8 0.8510638 106.38298
## 2         4.6 POLYGON ((-95.12573 29.4685...   6.0 2.7963526  46.60588
## 3         4.1 POLYGON ((-95.13526 29.4973...   3.7 2.4924012  67.36220
## 4         4.2 POLYGON ((-95.06361 29.4849...   2.5 2.5531915 102.12766
## 5         5.0 POLYGON ((-94.93173 29.3952...   6.7 3.0395137  45.36588
## 6         3.0 POLYGON ((-95.08098 29.3434...   3.9 1.8237082  46.76175
VHU19 <- VHU19%>%
  mutate(VHU19 = DP04_0003PE,
         VHU19_er = DP04_0003PM/1.645,
         VHU19_cv =100* (VHU19_er/VHU19)) %>%
  filter(complete.cases(VHU19), is.finite(VHU19_cv)==T)

head(VHU19)
## Simple feature collection with 6 features and 7 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -95.09076 ymin: 29.2863 xmax: -94.8109 ymax: 29.48285
## geographic CRS: NAD83
##         GEOID                                       NAME DP04_0003PE
## 1 48167720800 Census Tract 7208, Galveston County, Texas        15.2
## 2 48167720900 Census Tract 7209, Galveston County, Texas        23.1
## 3 48167725200 Census Tract 7252, Galveston County, Texas        11.0
## 4 48167722200 Census Tract 7222, Galveston County, Texas        14.7
## 5 48167722700 Census Tract 7227, Galveston County, Texas        16.7
## 6 48167722800 Census Tract 7228, Galveston County, Texas        16.8
##   DP04_0003PM                       geometry VHU19 VHU19_er VHU19_cv
## 1         7.3 MULTIPOLYGON (((-95.09076 2...  15.2 4.437690 29.19533
## 2         5.8 MULTIPOLYGON (((-95.0804 29...  23.1 3.525836 15.26336
## 3         5.5 MULTIPOLYGON (((-94.83092 2...  11.0 3.343465 30.39514
## 4         5.7 MULTIPOLYGON (((-94.90975 2...  14.7 3.465046 23.57174
## 5         5.8 MULTIPOLYGON (((-95.00338 2...  16.7 3.525836 21.11279
## 6         6.0 MULTIPOLYGON (((-94.97188 2...  16.8 3.647416 21.71081

Create a map using quantile breaks of this variable for each year

tm_shape(VHU11)+
  tm_polygons(c("VHU11"), title=c("% Vacant Housing Units"), palette="Blues", style="quantile", n=5)+
  #tm_format("World", legend.outside=T, title.size =4)+
  tm_scale_bar()+
  tm_layout(title="Galveston County Percent Vacant Housing Units 2011 - Quantile Breaks", title.size =1, legend.frame = TRUE, title.position = c('left', 'top'))+
  tm_compass()

tm_shape(VHU19)+
  tm_polygons(c("VHU19"), title=c("% Vacant Housing Units"), palette="Blues", style="quantile", n=5)+
  #tm_format("World", legend.outside=T, title.size =4)+
  tm_scale_bar()+
  tm_layout(title="Galveston County Percent Vacant Housing Units 2019 - Quantile Breaks", title.size =1, legend.frame = TRUE, title.position = c('left', 'top'))+
  tm_compass()

Mapping of errors in estimates

Most of the tracts seem to have low error in the estimates.

p1<-tm_shape(VHU11)+
  tm_polygons(c("VHU11"), title=c("% Vacant Housing Units"), palette="Reds", style="quantile", n=5)+
  #tm_format("World", legend.outside=T, title.size =4)+
  tm_scale_bar()+
  tm_layout(title="Galveston County Vacant Housing Units Estimates 2011", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_compass()

p2<-tm_shape(VHU19)+
  tm_polygons(c("VHU19_cv"), title=c("CV Vacant Housing Units"), palette="Reds", style="quantile", n=5)+
  #tm_format("World", title="San Antonio Poverty Rate CV", legend.outside=T)+
  tm_layout(title="Galveston County Vacant Housing Units CV", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_scale_bar()+
  tm_compass()


tmap_arrange(p1, p2)

plot(VHU11$VHU11, VHU11$VHU11_cv)

plot(VHU19$VHU19, VHU19$VHU19_cv)

Calculate the differences between the two years, including the test for significance of the differences

mdat<-tigris::geo_join(VHU11, as.data.frame(VHU19), by_sp="GEOID", by_df="GEOID")
## Warning: `group_by_()` is deprecated as of dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
head(mdat)
## Simple feature collection with 6 features and 14 fields
## Active geometry column: geometry.x
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: -95.23308 ymin: 29.29611 xmax: -94.92506 ymax: 29.52631
## geographic CRS: NAD83
##         GEOID                                        NAME.x DP04_0003PE.x
## 1 48167720302 Census Tract 7203.02, Galveston County, Texas           0.8
## 2 48167720502 Census Tract 7205.02, Galveston County, Texas           6.0
## 3 48167720501 Census Tract 7205.01, Galveston County, Texas           3.7
## 4 48167721202 Census Tract 7212.02, Galveston County, Texas           2.5
## 5 48167722002 Census Tract 7220.02, Galveston County, Texas           6.7
## 6 48167723502 Census Tract 7235.02, Galveston County, Texas           3.9
##   DP04_0003PM.x VHU11  VHU11_er  VHU11_cv
## 1           1.4   0.8 0.8510638 106.38298
## 2           4.6   6.0 2.7963526  46.60588
## 3           4.1   3.7 2.4924012  67.36220
## 4           4.2   2.5 2.5531915 102.12766
## 5           5.0   6.7 3.0395137  45.36588
## 6           3.0   3.9 1.8237082  46.76175
##                                          NAME.y DP04_0003PE.y DP04_0003PM.y
## 1 Census Tract 7203.02, Galveston County, Texas           7.1           4.0
## 2 Census Tract 7205.02, Galveston County, Texas           4.1           3.7
## 3 Census Tract 7205.01, Galveston County, Texas           5.5           4.4
## 4 Census Tract 7212.02, Galveston County, Texas           3.6           3.9
## 5 Census Tract 7220.02, Galveston County, Texas           6.7           4.9
## 6 Census Tract 7235.02, Galveston County, Texas          16.4           7.0
##   VHU19 VHU19_er VHU19_cv rank                     geometry.x
## 1   7.1 2.431611 34.24804    1 POLYGON ((-95.23098 29.4754...
## 2   4.1 2.249240 54.85952    1 POLYGON ((-95.12573 29.4685...
## 3   5.5 2.674772 48.63222    1 POLYGON ((-95.13526 29.4973...
## 4   3.6 2.370821 65.85613    1 POLYGON ((-95.06361 29.4849...
## 5   6.7 2.978723 44.45856    1 POLYGON ((-94.93173 29.3952...
## 6  16.4 4.255319 25.94707    1 POLYGON ((-95.08098 29.3434...
##                       geometry.y
## 1 MULTIPOLYGON (((-95.2318 29...
## 2 MULTIPOLYGON (((-95.12927 2...
## 3 MULTIPOLYGON (((-95.1662 29...
## 4 MULTIPOLYGON (((-95.07501 2...
## 5 MULTIPOLYGON (((-94.93722 2...
## 6 MULTIPOLYGON (((-95.19592 2...
acstest<-function(names,geoid, est1, err1, est2, err2, alpha, yr1, yr2, span){
  
  se1<-err1/qnorm(.90)
  se2<-err2/qnorm(.90)
  yrs1<-seq(yr1, to=yr1-span)
  yrs2<-seq(yr2, to=yr2-span)

  C<-mean(yrs2%in%yrs1)
  diff<- (est1-est2)
  test<-(est1-est2) / (sqrt(1-C)*sqrt((se1^2+se2^2)))
  crit<-qnorm(1-alpha/2)
  pval<-2*pnorm(abs(test),lower.tail=F)
  result<-NULL
  result[pval > alpha]<-"insignificant change"
  result[pval < alpha & test < 0]<- "significant increase"
  result[pval < alpha & test > 0]<-"significant decrease" 
  
  data.frame(name=names,geoid=geoid, est1=est1, est2=est2, se1=se1, se2=se2,difference=diff, test=test, result=result, pval=pval)
}
  diff1119<-acstest(names = mdat$NAME.x, geoid = mdat$GEOID, est1 = mdat$VHU11, est2 = mdat$VHU19, err1 = mdat$VHU11_er, err2=mdat$VHU19_er,alpha = .1, yr1 = 2011, yr2=2019, span = 5)

head(diff1119)
##                                            name       geoid est1 est2       se1
## 1 Census Tract 7203.02, Galveston County, Texas 48167720302  0.8  7.1 0.6640886
## 2 Census Tract 7205.02, Galveston County, Texas 48167720502  6.0  4.1 2.1820055
## 3 Census Tract 7205.01, Galveston County, Texas 48167720501  3.7  5.5 1.9448310
## 4 Census Tract 7212.02, Galveston County, Texas 48167721202  2.5  3.6 1.9922659
## 5 Census Tract 7220.02, Galveston County, Texas 48167722002  6.7  6.7 2.3717451
## 6 Census Tract 7235.02, Galveston County, Texas 48167723502  3.9 16.4 1.4230471
##        se2 difference       test               result         pval
## 1 1.897396       -6.3 -3.1339306 significant increase 0.0017248166
## 2 1.755091        1.9  0.6785068 insignificant change 0.4974504207
## 3 2.087136       -1.8 -0.6309577 insignificant change 0.5280681887
## 4 1.849961       -1.1 -0.4046010 insignificant change 0.6857708000
## 5 2.324310        0.0  0.0000000 insignificant change 1.0000000000
## 6 3.320443      -12.5 -3.4601745 significant increase 0.0005398254
table(diff1119$result)
## 
## insignificant change significant decrease significant increase 
##                   40                   11                   14
acs_merge<-left_join(mdat, diff1119, by=c("GEOID"="geoid"))

tmap_mode("plot")
## tmap mode set to plotting
p1<-tm_shape(acs_merge)+
  tm_polygons(c("VHU11"), title=c("% Vacant Housing Units 2011"), palette="Blues", style="quantile", n=5)+
  #tm_format("World", legend.outside=T, title.size =4)+
  tm_scale_bar()+
  tm_layout(title="Galveston County Percent Vacant Housing Units 2011", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_compass()

p2<-tm_shape(acs_merge)+
  tm_polygons(c("VHU19"), title=c("% Vacant Housing Units 2019"), palette="Blues", style="quantile", n=5)+
  #tm_format("World", title="Galveston County Percent Vacant Housing Units CV", legend.outside=T)+
  tm_layout(title="Galveston County Percent Vacant Housing Units 2019", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_scale_bar()+
  tm_compass()


p3  <- tm_shape(acs_merge)+
  tm_polygons(c("result"), title=c("Changes"), palette = "Dark2")+
  #tm_format("World", title="Galveston County Percent Vacant Housing Units CV", legend.outside=T)+
  tm_layout(title="Galveston County Percent Vacant Housing Units Estimate Changes", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_scale_bar()+
  tm_compass()
  

tmap_arrange(p1, p2, p3)

The purple areas of the map of Galveston County shows that the census tracts with a significant increase in percent of vacant housing units are mostly on the west side of Interstate 45 on the mainland and the west side of the island.