-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhawaii_population.Rmd
129 lines (100 loc) · 3.8 KB
/
hawaii_population.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
---
title: "Population Decline"
output: html_notebook
---
## Notebook Purpose
To study Hawaii's population decline.
```{r setup}
library(tidycensus)
library(tidyverse)
library(ggplot2)
census_api_key(read_file("D:/keychain/census_key.txt"), install = TRUE, overwrite = TRUE)
# Reference for get_estimates() code
# https://walker-data.com/tidycensus/articles/other-datasets.html
# Reference for iteration code
# https://walker-data.com/census-r/wrangling-census-data-with-tidyverse-tools.html#preparing-time-series-acs-estimates
# Reference for Census popest API
# https://www.census.gov/data/developers/data-sets/popest-popproj/popest.Vintage_2021.html
# Reference for Census popest announcements
# https://www.census.gov/newsroom/press-releases/2021/2021-population-estimates.html
# Census glossary of popest terms
# https://www.census.gov/programs-surveys/popest/about/glossary.html
# Compare to DBEDT report on brain drain
# https://files.hawaii.gov/dbedt/economic/reports/Brain_Drain_Hawaii_Born_Population.pdf
# As featured in https://www.civilbeat.org/2022/01/hawaiis-population-drain-outpaces-most-states-again/
```
## Population Components
This section looks at births, deaths, and migration 2015-2021.
```{r}
# 2020 and 2021 components not available through API, only FTP
recent_years <- read.csv("https://www2.census.gov/programs-surveys/popest/datasets/2020-2021/state/totals/NST-EST2021-alldata.csv", header = TRUE)
hi_recent_years <- filter(recent_years, recent_years$STATE == 15)
# Get rest of years through API
years <- 2015:2019
names(years) <- years
comp_by_year <- map_dfr(years, ~{
get_estimates(
geography = "state",
product = "components",
state = "HI",
year = .x
)
}, .id = "year")
## Merge all years together from both sources
# remove extra geographic columns
hi_recent_years <- hi_recent_years[6:30]
# pivot long, 1 row per variable
hi_recent_years <- pivot_longer(hi_recent_years, everything(), names_to = c("variable", "year"), names_sep = -4, values_to = "value")
# keep raw pop estimates separately in case I need them later
popest <- hi_recent_years[0:5,]
hi_recent_years <- hi_recent_years[-(0:5),]
# remove RESIDUAL, which is minor and does not appear in comp_by_year
hi_recent_years <- hi_recent_years[-(13:14),]
# reorder & row bind with comp by year
hi_recent_years <- hi_recent_years[c(2,1,3)]
comp_by_year <- comp_by_year[-(2:3)]
comp_by_year <- rbind(comp_by_year, hi_recent_years)
# pivot wide, 1 row per variable. remove rates.
comp_by_year <- pivot_wider(comp_by_year, names_from = "year")
comp_rates <- comp_by_year[7:12,]
comp_by_year <- comp_by_year[0:6,]
# NOTE: 2020 data looks unreliable.
# Visualizations
# TODO line chart with B/D/M/Net over time
comp_by_year %>% ggplot(, aes(x = , y = BIRTHS) +
geom_line())
```
```{r}
pop_by_year <- map_dfr(years, ~{
get_estimates(
geography = "state",
product = "population",
state = "HI",
year = .x
)
}, .id = "year")
```
## Age Group Characteristics
This section breaks down the population by age group to understand
```{r}
# Age group characteristics only available to 2019
# Later datasets to be released here in 2022: https://www.census.gov/data/datasets/time-series/demo/popest/2020s-state-detail.html
char_by_year <- map_dfr(years, ~{
get_estimates(
geography = "state",
product = "characteristics",
state = "HI",
year = .x,
breakdown = "AGEGROUP",
breakdown_labels = TRUE
)
}, .id = "year")
# TODO Pivot wide, 1 row per year
# Visualizations
# group by summarize ages: https://walker-data.com/census-r/wrangling-census-data-with-tidyverse-tools.html#preparing-time-series-acs-estimates
# TODO animated population pyramid by year
```
```{r}
# TODO could experiment with migration flows as well:
# https://walker-data.com/tidycensus/articles/other-datasets.html
```