Vacancy Rates in Dallas County, Tx from 2011 to 2019 (Rates of Change Homework - 4)

In this homework, I’ll be creating a comparison of housing vacancy rates in Dallas County from 2011 to 2019, using ACS data.

library(tidycensus)
library(tidyverse)
## -- Attaching packages --------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.3     v dplyr   1.0.2
## v tidyr   1.1.1     v stringr 1.4.0
## v readr   1.3.1     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)

Here, I load the 2011 and 2019 variables:

v10_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()

v10_Profile[grep(x = v10_Profile$label, "VACANT", ignore.case = TRUE), c("name", "label")]
## # A tibble: 2 x 2
##   name       label                                            
##   <chr>      <chr>                                            
## 1 DP04_0003  Estimate!!HOUSING OCCUPANCY!!Vacant housing units
## 2 DP04_0003P Percent!!HOUSING OCCUPANCY!!Vacant housing units
v19_Profile[grep(x = v19_Profile$label, "VACANT", ignore.case = TRUE), c("name", "label")]
## # A tibble: 2 x 2
##   name       label                                                              
##   <chr>      <chr>                                                              
## 1 DP04_0003  Estimate!!HOUSING OCCUPANCY!!Total housing units!!Vacant housing u~
## 2 DP04_0003P Percent!!HOUSING OCCUPANCY!!Total housing units!!Vacant housing un~

It seems the variable we are interested in is “DP04_0003P”.

Here, I load the data for Dallas County in 2011:

vacancy11<-get_acs(geography = "tract",
                state="TX",
                county = "Dallas",
                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
#rename variables and filter missing cases
vacancy11 <- vacancy11%>%
  mutate(pvacant_11 = DP04_0003PM,
         pvacant_er_11 = DP04_0003PM/1.645,
         pvacant_cv_11 =100* (pvacant_er_11/pvacant_11)) %>%
  filter(complete.cases(pvacant_11), is.finite(pvacant_cv_11)==T)

head(vacancy11)

And now the same thing for 2019:

vacancy19<-get_acs(geography = "tract",
                state="TX",
                county = "Dallas",
                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
vacancy19 <- vacancy19%>%
  mutate(pvacant_19 = DP04_0003PM,
         pvacant_er_19 = DP04_0003PM/1.645,
         pvacant_cv_19 =100* (pvacant_er_19/pvacant_19)) %>%
  filter(complete.cases(pvacant_19), is.finite(pvacant_cv_19)==T)

head(vacancy19)

Now I merge the two data sets:

#merge the two years worth of data
mdat<-tigris::geo_join(vacancy11, as.data.frame(vacancy19), 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: -96.80067 ymin: 32.83427 xmax: -96.57525 ymax: 32.94708
## geographic CRS: NAD83
##         GEOID                                    NAME.x DP04_0003PE.x
## 1 48113018120 Census Tract 181.20, Dallas County, Texas          10.2
## 2 48113012702 Census Tract 127.02, Dallas County, Texas           5.2
## 3 48113012800    Census Tract 128, Dallas County, Texas           2.9
## 4 48113012900    Census Tract 129, Dallas County, Texas           7.4
## 5 48113013004 Census Tract 130.04, Dallas County, Texas           5.7
## 6 48113019400    Census Tract 194, Dallas County, Texas           6.5
##   DP04_0003PM.x pvacant_11 pvacant_er_11 pvacant_cv_11
## 1           6.4        6.4      3.890578      60.79027
## 2           5.7        5.7      3.465046      60.79027
## 3           3.0        3.0      1.823708      60.79027
## 4           4.9        4.9      2.978723      60.79027
## 5           4.6        4.6      2.796353      60.79027
## 6           4.8        4.8      2.917933      60.79027
##                                      NAME.y DP04_0003PE.y DP04_0003PM.y
## 1 Census Tract 181.20, Dallas County, Texas           4.6           4.2
## 2 Census Tract 127.02, Dallas County, Texas           3.8           3.6
## 3    Census Tract 128, Dallas County, Texas           1.1           1.4
## 4    Census Tract 129, Dallas County, Texas           6.4           5.5
## 5 Census Tract 130.04, Dallas County, Texas           3.2           3.6
## 6    Census Tract 194, Dallas County, Texas           7.8           5.4
##   pvacant_19 pvacant_er_19 pvacant_cv_19 rank                     geometry.x
## 1        4.2     2.5531915      60.79027    1 POLYGON ((-96.57525 32.9365...
## 2        3.6     2.1884498      60.79027    1 POLYGON ((-96.68648 32.8400...
## 3        1.4     0.8510638      60.79027    1 POLYGON ((-96.68664 32.8402...
## 4        5.5     3.3434650      60.79027    1 POLYGON ((-96.71915 32.8612...
## 5        3.6     2.1884498      60.79027    1 POLYGON ((-96.73348 32.8677...
## 6        5.4     3.2826748      60.79027    1 POLYGON ((-96.78691 32.8507...
##                       geometry.y
## 1 MULTIPOLYGON (((-96.61445 3...
## 2 MULTIPOLYGON (((-96.68511 3...
## 3 MULTIPOLYGON (((-96.70107 3...
## 4 MULTIPOLYGON (((-96.7251 32...
## 5 MULTIPOLYGON (((-96.73332 3...
## 6 MULTIPOLYGON (((-96.80067 3...

Here, I use a function designed by Dr. Sparks to test differences in geographic areas between datasets.

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)
}

Now I can use this function to see if there have been any changes in vacancy rates in Dallas County between 2011 and 2019. This test maintains the alpha of 0.1 used in the example, as smaller alphas did not show any significant effects.

  diff1119<-acstest(names = mdat$NAME.x, geoid = mdat$GEOID, est1 = mdat$pvacant_11, est2 = mdat$pvacant_19, err1 = mdat$pvacant_er_11, err2=mdat$pvacant_er_19,alpha = .1, yr1 = 2011, yr2=2019, span = 5)

head(diff1119)
##                                        name       geoid est1 est2      se1
## 1 Census Tract 181.20, Dallas County, Texas 48113018120  6.4  4.2 3.035834
## 2 Census Tract 127.02, Dallas County, Texas 48113012702  5.7  3.6 2.703789
## 3    Census Tract 128, Dallas County, Texas 48113012800  3.0  1.4 1.423047
## 4    Census Tract 129, Dallas County, Texas 48113012900  4.9  5.5 2.324310
## 5 Census Tract 130.04, Dallas County, Texas 48113013004  4.6  3.6 2.182006
## 6    Census Tract 194, Dallas County, Texas 48113019400  4.8  5.4 2.276875
##         se2 difference       test               result      pval
## 1 1.9922659        2.2  0.6058650 insignificant change 0.5446044
## 2 1.7076565        2.1  0.6566808 insignificant change 0.5113862
## 3 0.6640886        1.6  1.0188651 insignificant change 0.3082670
## 4 2.6089196       -0.6 -0.1717170 insignificant change 0.8636600
## 5 1.7076565        1.0  0.3609088 insignificant change 0.7181676
## 6 2.5614847       -0.6 -0.1750725 insignificant change 0.8610226
table(diff1119$result)
## 
## insignificant change significant decrease significant increase 
##                  524                    2                    1

We see that there has been a significant decrease in only two census tracts, while only one census tract has seen a significant increase.

Here are the census tracts, mapped:

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("pvacant_11"), title=c("% Vacant in  2011"), palette="Blues", style="quantile", n=5)+
  #tm_format("World", legend.outside=T, title.size =4)+
  tm_scale_bar()+
  tm_layout(title="Dallas County Vacancy Rate Estimates 2011", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_compass()

p2<-tm_shape(acs_merge)+
  tm_polygons(c("pvacant_19"), title=c("% Vacant 2019"), palette="Blues", style="quantile", n=5)+
  #tm_format("World", title="Dallas County Vacancy CV", legend.outside=T)+
  tm_layout(title="Dallas County Vacancy Estimate 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="San Antonio Poverty Rate CV", legend.outside=T)+
  tm_layout(title="Dallas County Vacancy Estimate Changes", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_scale_bar()+
  tm_compass()
  

tmap_arrange(p1, p2, p3)

#Conclusion

Dallas County vacancy rates have not changed much from 2011 to 2019!