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.4
## 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)
v11_Profile <- load_variables(2011, "acs5/profile", cache = TRUE)
v19_Profile <- load_variables(2019 , "acs5/profile", cache = TRUE)

v11_Profile[grep(x = v11_Profile$label, "Vacant housing units", 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, "Percent Vacant housing units", ignore.case = TRUE), c("name", "label")]
## # A tibble: 0 x 2
## # ... with 2 variables: name <chr>, label <chr>
vac11sa<-get_acs(geography = "tract",
                state="TX",
                county = "Bexar",
                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
vac11sa <- vac11sa%>%
  mutate(pvac11 = DP04_0003PE,
         pvac_er11 = DP04_0003PM/1.645,
         pvac_cv11 =100* (pvac_er11/pvac11)) %>%
  filter(complete.cases(pvac11), is.finite(pvac_cv11)==T)

head(vac11sa)
summary(vac11sa)
vac11d<-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
vac11d <- vac11d%>%
  mutate(pvac11 = DP04_0003PE,
         pvac_er11 = DP04_0003PM/1.645,
         pvac_cv11 =100* (pvac_er11/pvac11)) %>%
  filter(complete.cases(pvac11), is.finite(pvac_cv11)==T)

head(vac11d)
summary(vac11d)
vac19sa<-get_acs(geography = "tract",
                state="TX",
                county = "Bexar",
                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
vac19sa <- vac19sa%>%
  mutate(pvac19 = DP04_0003PE,
         pvac_er19 = DP04_0003PM/1.645,
         pvac_cv19 =100* (pvac_er19/pvac19)) %>%
  filter(complete.cases(pvac19), is.finite(pvac_cv19)==T)

head(vac19sa)
summary(vac19sa)
vac19d<-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
vac19d <- vac19d%>%
  mutate(pvac19 = DP04_0003PE,
         pvac_er19 = DP04_0003PM/1.645,
         pvac_cv19 =100* (pvac_er19/pvac19)) %>%
  filter(complete.cases(pvac19), is.finite(pvac_cv19)==T)

head(vac19d)
summary(vac19d)
tm_shape(vac11sa)+
  tm_polygons(c("pvac11"), 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="Bexar County Vacant housing units Rate Estimates - Quantile Breaks", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_compass()

tm_shape(vac11d)+
  tm_polygons(c("pvac11"), 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="Dallas County Vacant housing units Rate Estimates - Quantile Breaks", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_compass()

tm_shape(vac19sa)+
  tm_polygons(c("pvac19"), 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="Bexar County Vacant housing units Rate Estimates - Quantile Breaks", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_compass()

tm_shape(vac19d)+
  tm_polygons(c("pvac19"), 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="Dallas County Vacant housing units Rate Estimates - Quantile Breaks", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_compass()

tm_shape(vac11sa)+
  tm_polygons(c("pvac11"), title=c("% Vacant housing units"), palette="Blues", style="pretty", n=5)+
  #tm_format("World", legend.outside=T, title.size =4)+
  tm_scale_bar()+
  tm_layout(title="Bexar County Vacant housing units Rate Estimates - Pretty Breaks", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_compass()

tm_shape(vac11d)+
  tm_polygons(c("pvac11"), title=c("% Vacant housing units"), palette="Blues", style="pretty", n=5)+
  #tm_format("World", legend.outside=T, title.size =4)+
  tm_scale_bar()+
  tm_layout(title="Dallas County Vacant housing units Rate Estimates - Pretty Breaks", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_compass()

tm_shape(vac19sa)+
  tm_polygons(c("pvac19"), title=c("% Vacant housing units"), palette="Blues", style="pretty", n=5)+
  #tm_format("World", legend.outside=T, title.size =4)+
  tm_scale_bar()+
  tm_layout(title="Bexar County Vacant housing units Rate Estimates - Pretty Breaks", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_compass()

tm_shape(vac19d)+
  tm_polygons(c("pvac19"), title=c("% Vacant housing units"), palette="Blues", style="pretty", n=5)+
  #tm_format("World", legend.outside=T, title.size =4)+
  tm_scale_bar()+
  tm_layout(title="Dallas County Vacant housing units Rate Estimates - Pretty Breaks", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_compass()

tm_shape(vac11sa)+
  tm_polygons(c("pvac11"), title=c("% Vacant housing units"), palette="Blues", style="jenks", n=5)+
  #tm_format("World", legend.outside=T, title.size =4)+
  tm_scale_bar()+
  tm_layout(title="Bexar County Vacant housing units Rate Estimates - Jenks Breaks", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_compass()

tm_shape(vac11d)+
  tm_polygons(c("pvac11"), title=c("% Vacant housing units"), palette="Blues", style="pretty", n=5)+
  #tm_format("World", legend.outside=T, title.size =4)+
  tm_scale_bar()+
  tm_layout(title="Dallas County Vacant housing units Rate Estimates - Pretty Breaks", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_compass()

tm_shape(vac19sa)+
  tm_polygons(c("pvac19"), title=c("% Vacant housing units"), palette="Blues", style="jenks", n=5)+
  #tm_format("World", legend.outside=T, title.size =4)+
  tm_scale_bar()+
  tm_layout(title="Bexar County Vacant housing units Rate Estimates - Jenks Breaks", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_compass()

tm_shape(vac19d)+
  tm_polygons(c("pvac19"), title=c("% Vacant housing units"), palette="Blues", style="jenks", n=5)+
  #tm_format("World", legend.outside=T, title.size =4)+
  tm_scale_bar()+
  tm_layout(title="Dallas County Vacant housing units Rate Estimates - Jenks Breaks", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_compass()

p1<-tm_shape(vac11sa)+
  tm_polygons(c("pvac11"), 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="Bexar County Vacant housing units Rate Estimates", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_compass()

p2<-tm_shape(vac11sa)+
  tm_polygons(c("pvac_cv11"), title=c("CV Vacant housing units"), palette="Blues", style="quantile", n=5)+
  #tm_format("World", title="San Antonio Poverty Rate CV", legend.outside=T)+
  tm_layout(title="Bexar County Vacant housing units Rate CV", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_scale_bar()+
  tm_compass()


tmap_arrange(p1, p2)

p3<-tm_shape(vac11d)+
  tm_polygons(c("pvac11"), 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="Dallas County Vacant housing units Rate Estimates", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_compass()

p4<-tm_shape(vac11d)+
  tm_polygons(c("pvac_cv11"), title=c("CV Vacant housing units"), palette="Blues", style="quantile", n=5)+
  #tm_format("World", title="San Antonio Poverty Rate CV", legend.outside=T)+
  tm_layout(title="Dallas County Vacant housing units Rate CV", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_scale_bar()+
  tm_compass()


tmap_arrange(p3, p4)

p5<-tm_shape(vac19sa)+
  tm_polygons(c("pvac19"), 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="Bexar County Vacant housing units Rate Estimates", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_compass()

p6<-tm_shape(vac19sa)+
  tm_polygons(c("pvac_cv19"), title=c("CV Vacant housing units"), palette="Blues", style="quantile", n=5)+
  #tm_format("World", title="San Antonio Poverty Rate CV", legend.outside=T)+
  tm_layout(title="Bexar County Vacant housing units Rate CV", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_scale_bar()+
  tm_compass()


tmap_arrange(p5, p6)

p7<-tm_shape(vac19d)+
  tm_polygons(c("pvac19"), 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="Dallas County Vacant housing units Rate Estimates", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_compass()

p8<-tm_shape(vac19d)+
  tm_polygons(c("pvac_cv19"), title=c("CV Vacant housing units"), palette="Blues", style="quantile", n=5)+
  #tm_format("World", title="San Antonio Poverty Rate CV", legend.outside=T)+
  tm_layout(title="Dallas County Vacant housing units Rate CV", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_scale_bar()+
  tm_compass()


tmap_arrange(p7, p8)

plot(vac11sa$pvac11, vac11sa$pvac_cv11)

plot(vac11d$pvac11, vac11d$pvac_cv11)

plot(vac19sa$pvac19, vac19sa$pvac_cv19)

plot(vac19d$pvac19, vac19d$pvac_cv19)

mdatsa<-tigris::geo_join(vac11sa, as.data.frame(vac19sa), 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(mdatsa)
## Simple feature collection with 6 features and 14 fields
## Active geometry column: geometry.x
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: -98.56778 ymin: 29.35415 xmax: -98.39488 ymax: 29.44661
## geographic CRS: NAD83
##         GEOID                                    NAME.x DP04_0003PE.x
## 1 48029170401 Census Tract 1704.01, Bexar County, Texas          17.3
## 2 48029170402 Census Tract 1704.02, Bexar County, Texas          16.0
## 3 48029160702 Census Tract 1607.02, Bexar County, Texas          11.1
## 4 48029160200    Census Tract 1602, Bexar County, Texas           8.3
## 5 48029141200    Census Tract 1412, Bexar County, Texas          10.8
## 6 48029141300    Census Tract 1413, Bexar County, Texas          15.5
##   DP04_0003PM.x pvac11 pvac_er11 pvac_cv11
## 1           5.6   17.3  3.404255  19.67778
## 2           6.7   16.0  4.072948  25.45593
## 3           4.8   11.1  2.917933  26.28769
## 4           7.4    8.3  4.498480  54.19856
## 5           6.3   10.8  3.829787  35.46099
## 6           6.6   15.5  4.012158  25.88489
##                                      NAME.y DP04_0003PE.y DP04_0003PM.y pvac19
## 1 Census Tract 1704.01, Bexar County, Texas          22.1           7.2   22.1
## 2 Census Tract 1704.02, Bexar County, Texas          10.3           4.5   10.3
## 3 Census Tract 1607.02, Bexar County, Texas          13.3           4.7   13.3
## 4    Census Tract 1602, Bexar County, Texas           4.9           3.1    4.9
## 5    Census Tract 1412, Bexar County, Texas           5.3           4.8    5.3
## 6    Census Tract 1413, Bexar County, Texas          11.5           4.9   11.5
##   pvac_er19 pvac_cv19 rank                     geometry.x
## 1  4.376900  19.80498    1 POLYGON ((-98.5251 29.44358...
## 2  2.735562  26.55886    1 POLYGON ((-98.53594 29.4405...
## 3  2.857143  21.48228    1 POLYGON ((-98.55955 29.3849...
## 4  1.884498  38.45915    1 POLYGON ((-98.51153 29.3992...
## 5  2.917933  55.05534    1 POLYGON ((-98.44832 29.3826...
## 6  2.978723  25.90194    1 POLYGON ((-98.41921 29.3684...
##                       geometry.y
## 1 MULTIPOLYGON (((-98.54106 2...
## 2 MULTIPOLYGON (((-98.54196 2...
## 3 MULTIPOLYGON (((-98.56773 2...
## 4 MULTIPOLYGON (((-98.52883 2...
## 5 MULTIPOLYGON (((-98.44833 2...
## 6 MULTIPOLYGON (((-98.43312 2...
mdatd<-tigris::geo_join(vac11d, as.data.frame(vac19d), by_sp="GEOID", by_df="GEOID")

head(mdatd)
## 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 pvac11 pvac_er11 pvac_cv11
## 1           6.4   10.2  3.890578  38.14292
## 2           5.7    5.2  3.465046  66.63549
## 3           3.0    2.9  1.823708  62.88649
## 4           4.9    7.4  2.978723  40.25302
## 5           4.6    5.7  2.796353  49.05882
## 6           4.8    6.5  2.917933  44.89128
##                                      NAME.y DP04_0003PE.y DP04_0003PM.y pvac19
## 1 Census Tract 181.20, Dallas County, Texas           4.6           4.2    4.6
## 2 Census Tract 127.02, Dallas County, Texas           3.8           3.6    3.8
## 3    Census Tract 128, Dallas County, Texas           1.1           1.4    1.1
## 4    Census Tract 129, Dallas County, Texas           6.4           5.5    6.4
## 5 Census Tract 130.04, Dallas County, Texas           3.2           3.6    3.2
## 6    Census Tract 194, Dallas County, Texas           7.8           5.4    7.8
##   pvac_er19 pvac_cv19 rank                     geometry.x
## 1 2.5531915  55.50416    1 POLYGON ((-96.57525 32.9365...
## 2 2.1884498  57.59079    1 POLYGON ((-96.68648 32.8400...
## 3 0.8510638  77.36944    1 POLYGON ((-96.68664 32.8402...
## 4 3.3434650  52.24164    1 POLYGON ((-96.71915 32.8612...
## 5 2.1884498  68.38906    1 POLYGON ((-96.73348 32.8677...
## 6 3.2826748  42.08557    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...
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)
}
diff1119sa<-acstest(names = mdatsa$NAME.x, geoid = mdatsa$GEOID, est1 = mdatsa$pvac11, est2 = mdatsa$pvac19, err1 = mdatsa$pvac_er11, err2=mdatsa$pvac_er19,alpha = .1, yr1 = 2011, yr2=2019, span = 5)

head(diff1119sa)
##                                        name       geoid est1 est2      se1
## 1 Census Tract 1704.01, Bexar County, Texas 48029170401 17.3 22.1 2.656355
## 2 Census Tract 1704.02, Bexar County, Texas 48029170402 16.0 10.3 3.178138
## 3 Census Tract 1607.02, Bexar County, Texas 48029160702 11.1 13.3 2.276875
## 4    Census Tract 1602, Bexar County, Texas 48029160200  8.3  4.9 3.510183
## 5    Census Tract 1412, Bexar County, Texas 48029141200 10.8  5.3 2.988399
## 6    Census Tract 1413, Bexar County, Texas 48029141300 15.5 11.5 3.130704
##        se2 difference      test               result      pval
## 1 3.415313       -4.8 -1.109383 insignificant change 0.2672649
## 2 2.134571        5.7  1.488857 insignificant change 0.1365250
## 3 2.229440       -2.2 -0.690386 insignificant change 0.4899515
## 4 1.470482        3.4  0.893386 insignificant change 0.3716505
## 5 2.276875        5.5  1.463953 insignificant change 0.1432069
## 6 2.324310        4.0  1.025853 insignificant change 0.3049610
table(diff1119sa$result)
## 
## insignificant change significant decrease significant increase 
##                  251                   65                   24
diff1119d<-acstest(names = mdatd$NAME.x, geoid = mdatd$GEOID, est1 = mdatd$pvac11, est2 = mdatd$pvac19, err1 = mdatd$pvac_er11, err2=mdatd$pvac_er19,alpha = .1, yr1 = 2011, yr2=2019, span = 5)

head(diff1119d)
##                                        name       geoid est1 est2      se1
## 1 Census Tract 181.20, Dallas County, Texas 48113018120 10.2  4.6 3.035834
## 2 Census Tract 127.02, Dallas County, Texas 48113012702  5.2  3.8 2.703789
## 3    Census Tract 128, Dallas County, Texas 48113012800  2.9  1.1 1.423047
## 4    Census Tract 129, Dallas County, Texas 48113012900  7.4  6.4 2.324310
## 5 Census Tract 130.04, Dallas County, Texas 48113013004  5.7  3.2 2.182006
## 6    Census Tract 194, Dallas County, Texas 48113019400  6.5  7.8 2.276875
##         se2 difference       test               result      pval
## 1 1.9922659        5.6  1.5422018 insignificant change 0.1230246
## 2 1.7076565        1.4  0.4377872 insignificant change 0.6615405
## 3 0.6640886        1.8  1.1462233 insignificant change 0.2517028
## 4 2.6089196        1.0  0.2861950 insignificant change 0.7747287
## 5 1.7076565        2.5  0.9022720 insignificant change 0.3669124
## 6 2.5614847       -1.3 -0.3793238 insignificant change 0.7044474
table(diff1119d$result)
## 
## insignificant change significant decrease significant increase 
##                  342                  117                   43
acs_mergesa<-left_join(mdatsa, diff1119sa, by=c("GEOID"="geoid"))

tmap_mode("plot")
## tmap mode set to plotting
p1sa<-tm_shape(acs_mergesa)+
  tm_polygons(c("pvac11"), title=c("% Vacant Housing Units  2011"), palette="Blues", style="quantile", n=6)+
  #tm_format("World", legend.outside=T, title.size =4)+
  tm_scale_bar()+
  tm_layout(title="Bexar County Vacant Housing Units Rate Estimates 2011", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_compass()

p2sa<-tm_shape(acs_mergesa)+
  tm_polygons(c("pvac19"), title=c("% Vacant Housing Units 2019"), palette="Blues", style="quantile", n=6)+
  #tm_format("World", title="San Antonio Poverty Rate CV", legend.outside=T)+
  tm_layout(title="Bexar County Vacant Housing Units  Rate Estimate 2019", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_scale_bar()+
  tm_compass()


p3sa  <- tm_shape(acs_mergesa)+
  tm_polygons(c("result"), title=c("Changes"), palette = "Dark2")+
  #tm_format("World", title="San Antonio Poverty Rate CV", legend.outside=T)+
  tm_layout(title="Bexar County Vacant Housing Units Rate Estimate Changes", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_scale_bar()+
  tm_compass()
  

tmap_arrange(p1sa, p2sa, p3sa)

diff1119d<-acstest(names = mdatd$NAME.x, geoid = mdatd$GEOID, est1 = mdatd$pvac11, est2 = mdatd$pvac19, err1 = mdatd$pvac_er11, err2=mdatd$pvac_er19,alpha = .1, yr1 = 2011, yr2=2019, span = 5)

head(diff1119d)
##                                        name       geoid est1 est2      se1
## 1 Census Tract 181.20, Dallas County, Texas 48113018120 10.2  4.6 3.035834
## 2 Census Tract 127.02, Dallas County, Texas 48113012702  5.2  3.8 2.703789
## 3    Census Tract 128, Dallas County, Texas 48113012800  2.9  1.1 1.423047
## 4    Census Tract 129, Dallas County, Texas 48113012900  7.4  6.4 2.324310
## 5 Census Tract 130.04, Dallas County, Texas 48113013004  5.7  3.2 2.182006
## 6    Census Tract 194, Dallas County, Texas 48113019400  6.5  7.8 2.276875
##         se2 difference       test               result      pval
## 1 1.9922659        5.6  1.5422018 insignificant change 0.1230246
## 2 1.7076565        1.4  0.4377872 insignificant change 0.6615405
## 3 0.6640886        1.8  1.1462233 insignificant change 0.2517028
## 4 2.6089196        1.0  0.2861950 insignificant change 0.7747287
## 5 1.7076565        2.5  0.9022720 insignificant change 0.3669124
## 6 2.5614847       -1.3 -0.3793238 insignificant change 0.7044474
table(diff1119d$result)
## 
## insignificant change significant decrease significant increase 
##                  342                  117                   43
acs_merged<-left_join(mdatd, diff1119d, by=c("GEOID"="geoid"))

tmap_mode("plot")
## tmap mode set to plotting
p1d<-tm_shape(acs_merged)+
  tm_polygons(c("pvac11"), title=c("% Vacant Housing Units  2011"), palette="Blues", style="quantile", n=6)+
  #tm_format("World", legend.outside=T, title.size =4)+
  tm_scale_bar()+
  tm_layout(title="Dallas County Vacant Housing Units Rate Estimates 2011", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_compass()

p2d<-tm_shape(acs_mergesa)+
  tm_polygons(c("pvac19"), title=c("% Vacant Housing Units 2019"), palette="Blues", style="quantile", n=6)+
  #tm_format("World", title="San Antonio Poverty Rate CV", legend.outside=T)+
  tm_layout(title="Dallas County Vacant Housing Units  Rate Estimate 2019", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_scale_bar()+
  tm_compass()


p3d  <- tm_shape(acs_mergesa)+
  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 Vacant Housing Units Rate Estimate Changes", title.size =1.5, legend.frame = TRUE, title.position = c('right', 'top'))+
  tm_scale_bar()+
  tm_compass()
  

tmap_arrange(p1d, p2d, p3d)