In this example, I use census tract information for Webb County, TX to make 2 map layouts. In the first stage, I estimate of the percentage of the poverty rate and the coefficient of variation for this estimate for each year (2011, 2105).Then, describe the reliability of the estimates. In the final stage,the change in the overall poverty rate (difference in the rate between the two years) and the classification of whether the increase was significant, and the direction of the significance (higher or lower) will be discussed
library(tidycensus); library(tidyverse); library(sf)
## -- Attaching packages ------------------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 2.2.1.9000 v purrr 0.2.4
## 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
v15_Profile <- load_variables(2015, "acs5/profile", cache = TRUE) #demographic profile tables
v15_tables <- load_variables(2015 , "acs5", cache = TRUE) #all tables
#v10_sf1_tables <- load_variables(2010 , "sf1", cache = TRUE) #all tables for 2010 SF1
#v10_sf1_tables <- load_variables(2000 , "sf3", cache = TRUE) #all tables for 2000 SF#
View(v15_Profile)
#Search for variables by
v15_Profile[grep(x = v15_Profile$label, "HighSchool"), c("name", "label")]
## # A tibble: 0 x 2
## # ... with 2 variables: name <chr>, label <chr>
v15_Profile[grep(x = v15_Profile$label, "Built 2000 to 2009"), c("name", "label")]
## # A tibble: 2 x 2
## name label
## <chr> <chr>
## 1 DP04_0019E Estimate!!YEAR STRUCTURE BUILT!!Total housing units!!Built ~
## 2 DP04_0019PE Percent!!YEAR STRUCTURE BUILT!!Total housing units!!Built 2~
v11_Profile <- load_variables(2011 ,dataset = "acs5", cache = TRUE) #demographic profile
options(tigris_use_cache = TRUE)
#2010 ACS
acs_11<-get_acs(geography = "tract", state="TX", county = "479", year = 2010, variables="B17001_002",summary_var = "B17001_001", geometry = TRUE, output = "wide")
## Getting data from the 2006-2010 5-year ACS
## Getting data from the 2006-2010 5-year ACS
## Getting data from the 2007-2011 5-year ACS
#2015 ACS
acs_15<-get_acs(geography = "tract", state="TX", county = "Webb", year = 2015, variables="B17001_002",summary_var = "B17001_001",geometry = TRUE, output = "wide")
## Getting data from the 2011-2015 5-year ACS
webb.asc1<-get_acs(geography = "tract", state="TX", county = c("Webb County"), year = 2015,
variables=c( "B17001_001E","B17001_002E","B17001_002M") ,
summary_var = "B01001_001",
geometry = T, output = "wide")
## Getting data from the 2011-2015 5-year ACS
#create a county FIPS code - 5 digit
webb.asc1$county<-substr(webb.asc1$GEOID, 1, 5)
#rename variables and filter missing cases
webb2<-webb.asc1%>%
mutate(pov.cv= (B17001_002M/1.645)/B17001_001E) %>%
# st_transform(crs = 102740)%>%
filter(complete.cases(pov.cv))
class(webb2)
acs_11<-acs_11%>%
mutate(pov11=B17001_002E/summary_est, poverr11=moe_prop(B17001_002E, denom = summary_est, moe_num = B17001_002M, moe_denom = summary_moe))%>%
select(GEOID, NAME, pov11, poverr11)
#create variables with nice names
acs_15<-acs_15%>%
mutate(pov15=B17001_002E/summary_est, poverr15=moe_prop(B17001_002E, denom = summary_est, moe_num = B17001_002M, moe_denom = summary_moe))%>%
select(GEOID, NAME, pov15, poverr15)
#merge the two years worth of data
mdat<-left_join(acs_11, as.data.frame(acs_15), by="GEOID", st_join=FALSE)
head(mdat)
## Simple feature collection with 6 features and 7 fields
## Active geometry column: geometry.x
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -99.48566 ymin: 27.46338 xmax: -99.44769 ymax: 27.50658
## epsg (SRID): 4269
## proj4string: +proj=longlat +datum=NAD83 +no_defs
## GEOID NAME.x pov11 poverr11
## 1 48479000101 Census Tract 1.01, Webb County, Texas 0.3972872 0.1016614
## 2 48479000105 Census Tract 1.05, Webb County, Texas 0.4584250 0.2390404
## 3 48479000106 Census Tract 1.06, Webb County, Texas 0.3328264 0.1250222
## 4 48479000107 Census Tract 1.07, Webb County, Texas 0.6368375 0.1591927
## 5 48479000108 Census Tract 1.08, Webb County, Texas 0.3614423 0.1344986
## 6 48479000109 Census Tract 1.09, Webb County, Texas 0.4628211 0.1823501
## NAME.y pov15 poverr15
## 1 Census Tract 1.01, Webb County, Texas 0.5355420 0.09750194
## 2 Census Tract 1.05, Webb County, Texas 0.3780776 0.10244021
## 3 Census Tract 1.06, Webb County, Texas 0.5017866 0.12427429
## 4 Census Tract 1.07, Webb County, Texas 0.4048738 0.08696312
## 5 Census Tract 1.08, Webb County, Texas 0.3763865 0.09373043
## 6 Census Tract 1.09, Webb County, Texas 0.5350477 0.08634313
## geometry.x geometry.y
## 1 MULTIPOLYGON (((-99.46392 2... MULTIPOLYGON (((-99.48036 2...
## 2 MULTIPOLYGON (((-99.48102 2... MULTIPOLYGON (((-99.48022 2...
## 3 MULTIPOLYGON (((-99.47155 2... MULTIPOLYGON (((-99.4717 27...
## 4 MULTIPOLYGON (((-99.46845 2... MULTIPOLYGON (((-99.47458 2...
## 5 MULTIPOLYGON (((-99.46227 2... MULTIPOLYGON (((-99.47047 2...
## 6 MULTIPOLYGON (((-99.48102 2... MULTIPOLYGON (((-99.48526 2...
## geometry
## 1 GEOMETRYCOLLECTION EMPTY
## 2 GEOMETRYCOLLECTION EMPTY
## 3 GEOMETRYCOLLECTION EMPTY
## 4 GEOMETRYCOLLECTION EMPTY
## 5 GEOMETRYCOLLECTION EMPTY
## 6 GEOMETRYCOLLECTION EMPTY
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)
}
#Making comparisons
diff0915<-acstest(names = mdat$NAME.x, geoid = mdat$GEOID, est1 = mdat$pov11, est2 = mdat$pov15, err1 = mdat$poverr11, err2=mdat$poverr15, alpha = .1, yr1 = 2011, yr2=2015, span = 5)
head(diff0915, n=50)
## name geoid est1
## 1 Census Tract 1.01, Webb County, Texas 48479000101 0.39728725
## 2 Census Tract 1.05, Webb County, Texas 48479000105 0.45842499
## 3 Census Tract 1.06, Webb County, Texas 48479000106 0.33282636
## 4 Census Tract 1.07, Webb County, Texas 48479000107 0.63683753
## 5 Census Tract 1.08, Webb County, Texas 48479000108 0.36144228
## 6 Census Tract 1.09, Webb County, Texas 48479000109 0.46282109
## 7 Census Tract 2, Webb County, Texas 48479000200 0.35359273
## 8 Census Tract 3, Webb County, Texas 48479000300 0.36183960
## 9 Census Tract 6.01, Webb County, Texas 48479000601 0.38389310
## 10 Census Tract 6.02, Webb County, Texas 48479000602 0.38500209
## 11 Census Tract 7, Webb County, Texas 48479000700 0.41283784
## 12 Census Tract 8, Webb County, Texas 48479000800 0.44291188
## 13 Census Tract 9.01, Webb County, Texas 48479000901 0.30432812
## 14 Census Tract 9.03, Webb County, Texas 48479000903 0.56369748
## 15 Census Tract 9.04, Webb County, Texas 48479000904 0.53812500
## 16 Census Tract 10.01, Webb County, Texas 48479001001 0.29556752
## 17 Census Tract 10.03, Webb County, Texas 48479001003 0.34068220
## 18 Census Tract 10.04, Webb County, Texas 48479001004 0.38950820
## 19 Census Tract 11.01, Webb County, Texas 48479001101 0.30063532
## 20 Census Tract 11.03, Webb County, Texas 48479001103 0.39658120
## 21 Census Tract 11.04, Webb County, Texas 48479001104 0.12713582
## 22 Census Tract 11.05, Webb County, Texas 48479001105 0.37447136
## 23 Census Tract 12.01, Webb County, Texas 48479001201 0.43980683
## 24 Census Tract 12.02, Webb County, Texas 48479001202 0.49383055
## 25 Census Tract 13, Webb County, Texas 48479001300 0.51097454
## 26 Census Tract 14.01, Webb County, Texas 48479001401 0.62473246
## 27 Census Tract 14.02, Webb County, Texas 48479001402 0.45028320
## 28 Census Tract 15.01, Webb County, Texas 48479001501 0.32770270
## 29 Census Tract 15.02, Webb County, Texas 48479001502 0.35415439
## 30 Census Tract 16.01, Webb County, Texas 48479001601 0.17771616
## 31 Census Tract 16.02, Webb County, Texas 48479001602 0.10146976
## 32 Census Tract 17.06, Webb County, Texas 48479001706 0.30607652
## 33 Census Tract 17.09, Webb County, Texas 48479001709 0.14624315
## 34 Census Tract 17.10, Webb County, Texas 48479001710 0.17635659
## 35 Census Tract 17.11, Webb County, Texas 48479001711 0.11732163
## 36 Census Tract 17.12, Webb County, Texas 48479001712 0.06620503
## 37 Census Tract 17.13, Webb County, Texas 48479001713 0.08170149
## 38 Census Tract 17.14, Webb County, Texas 48479001714 0.07153338
## 39 Census Tract 17.15, Webb County, Texas 48479001715 0.14353479
## 40 Census Tract 17.16, Webb County, Texas 48479001716 0.14066580
## 41 Census Tract 17.17, Webb County, Texas 48479001717 0.33364226
## 42 Census Tract 17.18, Webb County, Texas 48479001718 0.27272727
## 43 Census Tract 17.19, Webb County, Texas 48479001719 0.15361363
## 44 Census Tract 17.20, Webb County, Texas 48479001720 0.14872695
## 45 Census Tract 17.21, Webb County, Texas 48479001721 0.07513954
## 46 Census Tract 17.22, Webb County, Texas 48479001722 0.02953586
## 47 Census Tract 18.06, Webb County, Texas 48479001806 0.26565465
## 48 Census Tract 18.07, Webb County, Texas 48479001807 0.41547213
## 49 Census Tract 18.08, Webb County, Texas 48479001808 0.43001829
## 50 Census Tract 18.09, Webb County, Texas 48479001809 0.31889383
## est2 se1 se2 difference test
## 1 0.53554204 0.07932681 0.07608116 -0.138254797 -1.54053953
## 2 0.37807757 0.18652418 0.07993452 0.080347417 0.48492009
## 3 0.50178663 0.09755536 0.09697174 -0.168960263 -1.50439935
## 4 0.40487375 0.12421875 0.06785768 0.231963780 2.00711162
## 5 0.37638649 0.10494980 0.07313824 -0.014944213 -0.14307983
## 6 0.53504770 0.14228857 0.06737390 -0.072226607 -0.56188306
## 7 0.49843680 0.09689496 0.11039421 -0.144844073 -1.20771865
## 8 0.70016978 0.09204934 0.10322007 -0.338330175 -2.99610945
## 9 0.54333333 0.17191879 0.12949042 -0.159440231 -0.90727985
## 10 0.35854153 0.07521163 0.09921383 0.026460569 0.26030140
## 11 0.51472868 0.11445348 0.11971866 -0.101890844 -0.75344372
## 12 0.57432682 0.13791510 0.08406523 -0.131414939 -0.99649199
## 13 0.38644728 0.09326955 0.08394421 -0.082119162 -0.80150679
## 14 0.52379182 0.09017200 0.11571894 0.039905657 0.33315026
## 15 0.45180523 0.07450135 0.10269337 0.086319766 0.83328159
## 16 0.28065054 0.06586860 0.08149022 0.014916981 0.17435671
## 17 0.36638717 0.15028275 0.12229858 -0.025704971 -0.16248187
## 18 0.47639752 0.12348793 0.12544242 -0.086889319 -0.60455494
## 19 0.30451415 0.10025174 0.05643220 -0.003878831 -0.04129375
## 20 0.30209084 0.12968880 0.11232150 0.094490353 0.67452622
## 21 0.37096774 0.05793395 0.08038815 -0.243831918 -3.01377934
## 22 0.38471761 0.13418318 0.06643605 -0.010246251 -0.08381152
## 23 0.45627981 0.11735051 0.07770573 -0.016472979 -0.14334514
## 24 0.64133845 0.10320669 0.06386450 -0.147507900 -1.48852174
## 25 0.44736842 0.06588231 0.07951488 0.063606118 0.75440208
## 26 0.54428086 0.15392659 0.08644161 0.080451601 0.55813972
## 27 0.51024310 0.09394910 0.09771596 -0.059959906 -0.54174481
## 28 0.49150743 0.08616235 0.09675991 -0.163804728 -1.54843471
## 29 0.39865012 0.08573169 0.07941189 -0.044495730 -0.46633658
## 30 0.23267577 0.05858240 0.07881239 -0.054959613 -0.68545140
## 31 0.09844454 0.07521555 0.03411986 0.003025222 0.04486023
## 32 0.39651187 0.06933808 0.08328240 -0.090435353 -1.02207056
## 33 0.16330402 0.05718952 0.05602384 -0.017060871 -0.26100036
## 34 0.38513514 0.14132877 0.14543570 -0.208778546 -1.26088830
## 35 0.09790439 0.04675802 0.05731869 0.019417245 0.32149212
## 36 0.07188564 0.03872613 0.03968285 -0.005680604 -0.12547492
## 37 0.05801940 0.03112965 0.02738165 0.023682087 0.69960299
## 38 0.12834569 0.04883139 0.06672148 -0.056812313 -0.84154786
## 39 0.14103653 0.05884998 0.04745233 0.002498252 0.04047362
## 40 0.19461502 0.05013357 0.05315765 -0.053949225 -0.90426632
## 41 0.42870999 0.20516758 0.08782280 -0.095067729 -0.52171747
## 42 0.11278195 0.10222737 0.06048898 0.159945318 1.64916313
## 43 0.22409565 0.05662867 0.06742252 -0.070482017 -0.98039303
## 44 0.09423587 0.07083360 0.03329262 0.054491079 0.85268712
## 45 0.18616087 0.04138094 0.13110847 -0.111021326 -0.98900916
## 46 0.05986728 0.01456782 0.02747135 -0.030331417 -1.19467123
## 47 0.42778570 0.11859127 0.08363678 -0.162131051 -1.36833661
## 48 0.32301587 0.09665206 0.06972292 0.092456254 0.95015252
## 49 0.40369240 0.09413951 0.10452583 0.026325890 0.22920752
## 50 0.35774115 0.09345302 0.08432242 -0.038847320 -0.37798788
## result pval
## 1 insignificant change 0.123428895
## 2 insignificant change 0.627733034
## 3 insignificant change 0.132478571
## 4 significant decrease 0.044737779
## 5 insignificant change 0.886227137
## 6 insignificant change 0.574195699
## 7 insignificant change 0.227155502
## 8 significant increase 0.002734483
## 9 insignificant change 0.364258826
## 10 insignificant change 0.794631292
## 11 insignificant change 0.451183321
## 12 insignificant change 0.319011156
## 13 insignificant change 0.422838312
## 14 insignificant change 0.739020859
## 15 insignificant change 0.404685937
## 16 insignificant change 0.861585133
## 17 insignificant change 0.870926402
## 18 insignificant change 0.545474754
## 19 insignificant change 0.967061712
## 20 insignificant change 0.499976825
## 21 significant increase 0.002580154
## 22 insignificant change 0.933206289
## 23 insignificant change 0.886017609
## 24 insignificant change 0.136613354
## 25 insignificant change 0.450607825
## 26 insignificant change 0.576748978
## 27 insignificant change 0.587994319
## 28 insignificant change 0.121517669
## 29 insignificant change 0.640974599
## 30 insignificant change 0.493059117
## 31 insignificant change 0.964218719
## 32 insignificant change 0.306747508
## 33 insignificant change 0.794092227
## 34 insignificant change 0.207349096
## 35 insignificant change 0.747837479
## 36 insignificant change 0.900147577
## 37 insignificant change 0.484175274
## 38 insignificant change 0.400041085
## 39 insignificant change 0.967715534
## 40 insignificant change 0.365854199
## 41 insignificant change 0.601867059
## 42 significant decrease 0.099114219
## 43 insignificant change 0.326892150
## 44 insignificant change 0.393832836
## 45 insignificant change 0.322658659
## 46 insignificant change 0.232215506
## 47 insignificant change 0.171206737
## 48 insignificant change 0.342034760
## 49 insignificant change 0.818707629
## 50 insignificant change 0.705439600
As we cansee in the most of cases, there was no significant change. ##compare poverty rate between two points of time
options(scipen=999)
acs_merge<-left_join(webb.asc1, diff0915, by=c("GEOID"="geoid"))
## Warning: Column `GEOID`/`geoid` joining character vector and factor,
## coercing into character vector
devtools::install_github("jannes-m/RQGIS",dep=T)
## Skipping install of 'RQGIS' from a github remote, the SHA1 (e6d6eccd) has not changed since last install.
## Use `force = TRUE` to force installation
library(dplyr)
library(ggplot2)
library(RQGIS)
## Loading required package: reticulate
## Loading required package: reticulate
library(ggsn)
## Loading required package: grid
options(scipen=999)
acs_merge<-left_join(webb.asc1, diff0915, by=c("GEOID"="geoid"))
## Warning: Column `GEOID`/`geoid` joining character vector and factor,
## coercing into character vector
brks<-quantile(c(acs_11$pov11, acs_15$pov15), probs = seq(0,1,.2), na.rm=T)
acs_11map<-acs_11%>%
mutate(pov_q = cut(pov11,breaks = brks, include.lowest = T))
p1<-ggplot(acs_11map, aes( fill=pov_q, color=pov_q))+
geom_sf() +
ggtitle("Poverty Rate 2011 ",
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(acs_11map)+
scalebar(acs_11map, dist = 5, dd2km =T, model="GRS80", st.size = 2)
acs_15map<-acs_15%>%
mutate(pov_q = cut(pov15,breaks = brks, include.lowest = T))
p2<-ggplot(acs_15map, 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")+
theme(axis.text.x = element_blank(), axis.text.y = element_blank())+ north(acs_15map)+
scalebar(acs_15map, dist = 5, dd2km =T, model="GRS80", st.size = 2)
myfil<-c("grey", "red", "blue")
p3<-ggplot(acs_merge, aes( fill=result))+
geom_sf() +
ggtitle("Comparison of Poverty Rate 2011 to 2015",
subtitle = "Webb County Texas")+
scale_fill_manual(values=myfil)+
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)
p3
library(gridExtra)
##
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
##
## combine
combine
## function (..., along = 1L, join = "outer")
## {
## .Deprecated("gtable_combine")
## gtable_combine(..., along = along, join = join)
## }
## <bytecode: 0x000000003fcafb50>
## <environment: namespace:gridExtra>
out<-grid.arrange(p1, p2, p3, nrow = 2)
out
## TableGrob (2 x 2) "arrange": 3 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]
In comparison, most of the cases are insignificant, but just one fairly big part and two samll parts of the county have changed during the time signifacntly.When compare two points of times, peverty rate has increased in some pats of county a little bit, but these changes are not significant.