#Libraries
library(tidycensus);  library(tidyverse); library(sf);library(classInt);library(dplyr);library(gridExtra);library(ggsn)
## -- Attaching packages ----------------------------------------------------------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 2.2.1.9000     v purrr   0.2.5     
## v tibble  1.4.2          v dplyr   0.7.5     
## v tidyr   0.8.1          v stringr 1.3.1     
## v readr   1.1.1          v forcats 0.3.0
## -- Conflicts -------------------------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## Linking to GEOS 3.6.1, GDAL 2.2.3, proj.4 4.9.3
## 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'))`
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
## Loading required package: grid
#setting directory
sf::st_write(webb_acs_2010,dsn="D:/Homework",layer="webb_2010_tract_dp", driver="ESRI Shapefile", delete_layer=T, update=T)
sf::st_write(webb_acs_2015,dsn="D:/Homework",layer="webb_2015_tract_dp", driver="ESRI Shapefile", delete_layer=T, update=T)

Here I create a function that implements the testing procedure used by the Census for comparing estimates across year

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)
}
  diff1015<-acstest(names = mdat$NAME.x, geoid = mdat$GEOID, est1 = mdat$pov10, est2 = mdat$pov15, err1 = mdat$poverr10, err2=mdat$poverr15, alpha = .1, yr1 = 2010, yr2=2015, span = 5)

head(diff1015)
##                                     name       geoid      est1      est2
## 1  Census Tract 1.06, Webb County, Texas 48479000106 0.3328264 0.5017866
## 2  Census Tract 1.07, Webb County, Texas 48479000107 0.6368375 0.4048738
## 3     Census Tract 3, Webb County, Texas 48479000300 0.3618396 0.7001698
## 4  Census Tract 9.03, Webb County, Texas 48479000903 0.5636975 0.5237918
## 5 Census Tract 10.03, Webb County, Texas 48479001003 0.3406822 0.3663872
## 6 Census Tract 11.03, Webb County, Texas 48479001103 0.3965812 0.3020908
##          se1        se2  difference       test               result
## 1 0.09755536 0.09697174 -0.16896026 -1.3455757 insignificant change
## 2 0.12421875 0.06785768  0.23196378  1.7952152 significant decrease
## 3 0.09204934 0.10322007 -0.33833017 -2.6798018 significant increase
## 4 0.09017200 0.11571894  0.03990566  0.2979787 insignificant change
## 5 0.15028275 0.12229858 -0.02570497 -0.1453282 insignificant change
## 6 0.12968880 0.11232150  0.09449035  0.6033146 insignificant change
##          pval
## 1 0.178439397
## 2 0.072619418
## 3 0.007366577
## 4 0.765719455
## 5 0.884451747
## 6 0.546299430
options(scipen=999)

brks<-quantile(c(webb_acs_2010$pov10, webb_acs_2015$pov15),  probs = seq(0,1,.2), na.rm=T)
webb_acs_2010map<-webb_acs_2010%>%
  mutate(pov_q = cut(pov10,breaks = brks, include.lowest = T))

p1<-ggplot(webb_acs_2010map, aes( fill=pov_q, color=pov_q))+
  geom_sf() + 
  ggtitle("Poverty Rate 2010 ", 
          subtitle = "Webb County Texas")+
  scale_fill_brewer(palette = "Blues") + 
  scale_color_brewer(palette = "Blues")+
  #scale_fill_manual(values=myfil)+
    theme(axis.text.x = element_blank(), axis.text.y = element_blank())+
  north(webb_acs_2010map)+
  scalebar(webb_acs_2010map, dist = 5,  dd2km =T, model="GRS80", st.size = 2)

webb_acs_2015map<-webb_acs_2015%>%
  mutate(pov_q = cut(pov15,breaks = brks, include.lowest = T))

p2<-ggplot(webb_acs_2015map, aes( fill=pov_q, color=pov_q))+
  geom_sf() + 
  ggtitle("Poverty Rate 2015 ", 
          subtitle = "Webb County Texas")+
  scale_fill_brewer(palette = "Blues") + 
  scale_color_brewer(palette = "Blues")+
  #scale_fill_manual(values=myfil)+
    theme(axis.text.x = element_blank(), axis.text.y = element_blank())+
  north(webb_acs_2015map)+
  scalebar(webb_acs_2015map, dist = 5,  dd2km =T, model="GRS80", st.size = 2)


cv_map10<-webb_acs_2010 %>%
  mutate(cv_cut=cut(poverr10,breaks = quantile(poverr10, na.rm=T, p=seq(0,1,length.out = 6)),include.lowest = T))
p3<-ggplot(cv_map10, aes(fill = cv_cut, color = cv_cut)) + 
  geom_sf() + 
  ggtitle("Coefficent of Variation in Population Estimate", 
          subtitle = "Webb County Texas, 2010 ACS")+
    scale_fill_brewer(palette = "Blues") + 
  scale_color_brewer(palette = "Blues")+
  theme(axis.text.x = element_blank(), axis.text.y = element_blank())+
  north(cv_map10)+
  scalebar(cv_map10, dist = 5,  dd2km =T, model="GRS80", st.size = 2)


cv_map15<-webb_acs_2015 %>%
  mutate(cv_cut=cut(poverr15,breaks = quantile(poverr15, na.rm=T, p=seq(0,1,length.out = 6)),include.lowest = T))

p4<-ggplot(cv_map15, aes(fill = cv_cut, color = cv_cut)) + 
  geom_sf() + 
  ggtitle("Coefficent of Variation in Estimate of % Greater than High School", 
          subtitle = "Webb County Texas, 2015 ACS")+
  scale_fill_brewer(palette = "Blues") + 
  scale_color_brewer(palette = "Blues")+
    theme(axis.text.x = element_blank(), axis.text.y = element_blank())+
  north(cv_map15)+
  scalebar(cv_map15, dist = 5,  dd2km =T, model="GRS80", st.size = 2)

out<-grid.arrange(p1, p2, p3, p4, nrow = 2)

out
## TableGrob (2 x 2) "arrange": 4 grobs
##   z     cells    name           grob
## 1 1 (1-1,1-1) arrange gtable[layout]
## 2 2 (1-1,2-2) arrange gtable[layout]
## 3 3 (2-2,1-1) arrange gtable[layout]
## 4 4 (2-2,2-2) arrange gtable[layout]

The error rates are very high except for a few areas in the West of the county with higher populations.

Change in poverty rate in Webb County, TX, between 2010 and 2015, 5-year estimates:

acs_merge<-left_join(webb_acs_2010, diff1015, by=c("GEOID"="geoid"))
## Warning: Column `GEOID`/`geoid` joining character vector and factor,
## coercing into character vector
myfil<-c("grey", "red", "blue")
p5<-ggplot(acs_merge, aes( fill=result))+
  geom_sf() + 
  ggtitle("Comparison of Poverty Rate 2011 to 2015", 
          subtitle = "Webb County Texas")+
    theme(axis.text.x = element_blank(), axis.text.y = element_blank())+
  north(acs_merge)+
  scalebar(acs_merge, dist = 5,  dd2km =T, model="GRS80", st.size = 2)

p5