-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannual-aggregates.Rmd
396 lines (333 loc) · 13.1 KB
/
annual-aggregates.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
---
title: "Cohabitation Trends: 2012-2022"
author: "Lorae Stojanovic"
date: "2024-10-21"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r stacked-bar-function, echo = FALSE}
# Initialize a graphing function
stacked_bar <- function(
data,
title = "Default title"
) {
ggplot(data, aes(x = AGE_bucket, y = percent, fill = cohabit_bin)) +
geom_bar(stat = "identity", position = "fill") +
scale_y_continuous(labels = scales::percent_format()) +
labs(
title = title,
x = "Age Group",
y = "Percentage",
fill = "Cohabit Bin"
) +
scale_fill_manual(values = c(
"0" = "white",
"1" = "#075792",
"2" = "#1e81b0",
"3" = "#46b1d5",
"9" = "lightcoral"
), labels = c(
"0" = "Not living with parents",
"1" = "Child provides for parent",
"2" = "Both child and parent are dependent",
"3" = "Child depends on parent",
"9" = "Living in institution"
)) +
geom_text(aes(
label = scales::percent(percent / 100, accuracy = 0.1)),
position = position_fill(vjust = 0.5), size = 3) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1),
legend.position = "bottom")
}
```
```{r summarize-stacked-bar, echo = FALSE}
summarize_stacked_bar <- function(
data,
title = "Default title"
) {
# Summarize the input data set using the crosstab_percent function
graph_input <- crosstab_percent(
data = data,
weight = "PERWT",
group_by = c("AGE_bucket", "cohabit_bin"),
percent_group_by = c("AGE_bucket"),
every_combo = TRUE,
repwts = paste0("REPWTP", sprintf("%d", 1:80))
) |>
arrange(AGE_bucket, cohabit_bin) |>
mutate(cohabit_bin = factor(cohabit_bin, levels = c(0, 1, 2, 3, 9)),
AGE_bucket = factor(AGE_bucket, levels = c("Under 16", "16-17", "17-18", "18-19", "20-21", "22-23", "24-25", "26-27", "28-29", "30+")))
# Produce the stacked bar graph
graph <- stacked_bar(data = graph_input, title = title)
return(graph)
}
```
## Introduction
This report tracks the rates of young adults living with parents from 2012 to 2022. It presents a comparison of percentages for different age groups and cohabitation types between these two years, and evaluates whether there were significant changes in these percentages.
## Cohabitation Trends in 2012 and 2022
We begin by loading the data and calculating the percentages of individuals living with their parents across different age groups.
```{r, echo = FALSE}
# ----- Step 0: Load packages ----- #
library("dplyr")
library("duckdb")
library("ipumsr")
library("readr")
library("tidyr")
library("ggplot2")
library("knitr")
library("purrr")
# ----- Step 1: Source helper functions ----- #
devtools::load_all("../dataduck")
# ----- Step 2: Import and wrangle data ----- #
con <- dbConnect(duckdb::duckdb(), "data/db/ipums.duckdb")
ipums_relate <- tbl(con, "ipums_relationships")
# Calculate percentages for 2022 and 2012
cohabit_2022_with_percents <- estimate_with_bootstrap_se(
data = ipums_relate |> filter(YEAR == 2022),
f = crosstab_percent,
wt_col = "PERWT",
repwt_cols = paste0("REPWTP", sprintf("%d", 1:80)),
constant = 4/80,
se_cols = c("percent"),
id_cols = c("AGE_bucket", "cohabit_bin"),
group_by = c("AGE_bucket", "cohabit_bin"),
percent_group_by = c("AGE_bucket"),
every_combo = TRUE
) |>
mutate(
cohabit_bin = factor(
cohabit_bin,
levels = c(0, 1, 2, 3, 9)
),
AGE_bucket = factor(
AGE_bucket,
levels =
c("Under 16",
"16-17",
"17-18",
"18-19",
"20-21",
"22-23",
"24-25",
"26-27",
"28-29",
"30+")
)) |>
arrange(AGE_bucket, cohabit_bin)
cohabit_2012_with_percents <- estimate_with_bootstrap_se(
data = ipums_relate |> filter(YEAR == 2022),
f = crosstab_percent,
wt_col = "PERWT",
repwt_cols = paste0("REPWTP", sprintf("%d", 1:80)),
constant = 4/80,
se_cols = c("percent"),
id_cols = c("AGE_bucket", "cohabit_bin"),
group_by = c("AGE_bucket", "cohabit_bin"),
percent_group_by = c("AGE_bucket"),
every_combo = TRUE
) |>
mutate(
cohabit_bin = factor(
cohabit_bin,
levels = c(0, 1, 2, 3, 9)
),
AGE_bucket = factor(
AGE_bucket,
levels =
c("Under 16",
"16-17",
"17-18",
"18-19",
"20-21",
"22-23",
"24-25",
"26-27",
"28-29",
"30+")
)) |>
arrange(AGE_bucket, cohabit_bin)
```
Statistical Comparison
In this section, we compare the percentages between 2012 and 2022 using a z-test to determine if the differences are statistically significant.
```{r, echo = FALSE}
# Function to compare percent changes between years and calculate p-value
compare_percent_changes <- function(df1, df2) {
comparison <- df1 %>%
inner_join(df2, by = c("AGE_bucket", "cohabit_bin"), suffix = c("_2012", "_2022")) %>%
rowwise() %>%
mutate(
percent_difference = percent_2022 - percent_2012,
combined_standard_error = sqrt(se_percent_2022^2 + se_percent_2012^2),
z_score = percent_difference / combined_standard_error,
p_value = 2 * pnorm(-abs(z_score)), # Two-tailed p-value calculation
significant_change = ifelse(abs(z_score) > 1.96, "Significant", "Not Significant")
) %>%
ungroup() %>%
select(AGE_bucket, cohabit_bin, percent_2012, percent_2022, percent_difference, combined_standard_error, z_score, p_value, significant_change)
return(comparison)
}
# Calculate the comparison between 2012 and 2022
result <- compare_percent_changes(cohabit_2012_with_percents, cohabit_2022_with_percents)
# Display the results
kable(result, caption = "Comparison of Cohabitation Trends (2012 vs 2022)")
```
## Visualizing Cohabitation Trends
In this section, we display bar charts showing the percentage of young adults cohabiting with their parents for the years 2012 and 2022. These charts are broken down by age group.
```{r, echo = FALSE, fig.width = 10, fig.height = 10}
stacked_bar(data = cohabit_2022_with_percents, title = "Cohabitation by Age Group, 2022")
```
```{r, echo = FALSE, fig.width = 10, fig.height = 10}
stacked_bar(data = cohabit_2012_with_percents, title = "Cohabitation by Age Group, 2012")
```
# By Gender
```{r, echo = FALSE}
women_2022 <- crosstab_percent(
data = ipums_relate |> filter(YEAR == 2022 & SEX == 2),
weight = "PERWT",
group_by = c("AGE_bucket", "cohabit_bin"),
percent_group_by = c("AGE_bucket"),
every_combo = TRUE,
repwts = paste0("REPWTP", sprintf("%d", 1:80))
) |>
arrange(AGE_bucket, cohabit_bin) |>
mutate(cohabit_bin = factor(cohabit_bin, levels = c(0, 1, 2, 3, 9)),
AGE_bucket = factor(AGE_bucket, levels = c("Under 16", "16-17", "17-18", "18-19", "20-21", "22-23", "24-25", "26-27", "28-29", "30+")))
men_2022 <- crosstab_percent(
data = ipums_relate |> filter(YEAR == 2022 & SEX == 1),
weight = "PERWT",
group_by = c("AGE_bucket", "cohabit_bin"),
percent_group_by = c("AGE_bucket"),
every_combo = TRUE,
repwts = paste0("REPWTP", sprintf("%d", 1:80))
) |>
arrange(AGE_bucket, cohabit_bin) |>
mutate(cohabit_bin = factor(cohabit_bin, levels = c(0, 1, 2, 3, 9)),
AGE_bucket = factor(AGE_bucket, levels = c("Under 16", "16-17", "17-18", "18-19", "20-21", "22-23", "24-25", "26-27", "28-29", "30+")))
```
```{r, echo = FALSE, fig.width = 10, fig.height = 10}
stacked_bar(data = women_2022, title = "Cohabitation by Age Group, 2022: Females")
```
```{r, echo = FALSE, fig.width = 10, fig.height = 10}
stacked_bar(data = men_2022, title = "Cohabitation by Age Group, 2022: Males")
```
# By Race/Ethnicity
Here's a breakdown of the percentage of the population in 2012 and 2022 falling into our nonoverlapping and comprehensive race/ethnicity groups. The percentages differ slightly from what is published by the Census due to our differing definitions. For more information on our race/ethnicity aggregation methodology, please see XXX [link to another Rmd here].
```{r, echo = FALSE}
# Calculate percentages for 2022 and 2012
race_ethnicity_2022 <- crosstab_percent(
data = ipums_relate |> filter(YEAR == 2022),
weight = "PERWT",
group_by = c("RACE_ETH_bucket"),
percent_group_by = c(),
every_combo = TRUE,
repwts = paste0("REPWTP", sprintf("%d", 1:80))
) |> arrange(RACE_ETH_bucket)
race_ethnicity_2012 <- crosstab_percent(
data = ipums_relate |> filter(YEAR == 2012),
weight = "PERWT",
group_by = c("RACE_ETH_bucket"),
percent_group_by = c(),
every_combo = TRUE,
repwts = paste0("REPWTP", sprintf("%d", 1:80))
) |> arrange(RACE_ETH_bucket)
# Function to compare percent changes for race/ethnicity and calculate p-values
compare_race_eth_changes <- function(df1, df2) {
comparison <- df1 %>%
inner_join(df2, by = c("RACE_ETH_bucket"), suffix = c("_2012", "_2022")) %>%
rowwise() %>%
mutate(
percent_difference = percent_2022 - percent_2012,
combined_standard_error = sqrt(percent_standard_error_2022^2 + percent_standard_error_2012^2),
z_score = percent_difference / combined_standard_error,
p_value = 2 * pnorm(-abs(z_score)), # Two-tailed p-value calculation
significant_change = ifelse(abs(z_score) > 1.96, "Significant", "Not Significant")
) %>%
ungroup() %>%
select(RACE_ETH_bucket, percent_2012, percent_2022, percent_difference, combined_standard_error, z_score, p_value, significant_change)
return(comparison)
}
# Perform comparison for race/ethnicity between 2012 and 2022
race_eth_comparison <- compare_race_eth_changes(race_ethnicity_2012, race_ethnicity_2022)
# Display the results
kable(race_eth_comparison, caption = "Comparison of Race/Ethnicity Cohabitation Trends (2012 vs 2022)")
```
# Asian American/Pacific Islanders
```{r, echo = FALSE, fig.width = 10, fig.height = 10}
summarize_stacked_bar(
data = ipums_relate |> filter(YEAR == 2022, RACE_ETH_bucket == "AAPI", SEX == 2),
title = "Cohabitation by Age Group, 2022: AAPI Females")
```
```{r, echo = FALSE, fig.width = 10, fig.height = 10}
summarize_stacked_bar(
data = ipums_relate |> filter(YEAR == 2022, RACE_ETH_bucket == "AAPI", SEX == 1),
title = "Cohabitation by Age Group, 2022: AAPI Males")
```
# American Indians / Alaska Natives
```{r, echo = FALSE, fig.width = 10, fig.height = 10}
summarize_stacked_bar(
data = ipums_relate |> filter(YEAR == 2022, RACE_ETH_bucket == "AIAN", SEX == 2),
title = "Cohabitation by Age Group, 2022: AIAN Females")
```
```{r, echo = FALSE, fig.width = 10, fig.height = 10}
summarize_stacked_bar(
data = ipums_relate |> filter(YEAR == 2022, RACE_ETH_bucket == "AIAN", SEX == 1),
title = "Cohabitation by Age Group, 2022: AIAN Males")
```
# Black Americans
```{r, echo = FALSE, fig.width = 10, fig.height = 10}
summarize_stacked_bar(
data = ipums_relate |> filter(YEAR == 2022, RACE_ETH_bucket == "Black", SEX == 2),
title = "Cohabitation by Age Group, 2022: Black American Females")
```
```{r, echo = FALSE, fig.width = 10, fig.height = 10}
summarize_stacked_bar(
data = ipums_relate |> filter(YEAR == 2022, RACE_ETH_bucket == "Black", SEX == 1),
title = "Cohabitation by Age Group, 2022: Black American Males")
```
# Hispanic Americans
```{r, echo = FALSE, fig.width = 10, fig.height = 10}
summarize_stacked_bar(
data = ipums_relate |> filter(YEAR == 2022, RACE_ETH_bucket == "Hispanic", SEX == 2),
title = "Cohabitation by Age Group, 2022: Hispanic American Females")
```
```{r, echo = FALSE, fig.width = 10, fig.height = 10}
summarize_stacked_bar(
data = ipums_relate |> filter(YEAR == 2022, RACE_ETH_bucket == "Hispanic", SEX == 1),
title = "Cohabitation by Age Group, 2022: Hispanic American Males")
```
# Multiracial Americans
```{r, echo = FALSE, fig.width = 10, fig.height = 10}
summarize_stacked_bar(
data = ipums_relate |> filter(YEAR == 2022, RACE_ETH_bucket == "Multiracial", SEX == 2),
title = "Cohabitation by Age Group, 2022: Multiracial American Females")
```
```{r, echo = FALSE, fig.width = 10, fig.height = 10}
summarize_stacked_bar(
data = ipums_relate |> filter(YEAR == 2022, RACE_ETH_bucket == "Multiracial", SEX == 1),
title = "Cohabitation by Age Group, 2022: Multiracial American Males")
```
# White Americans
```{r, echo = FALSE, fig.width = 10, fig.height = 10}
summarize_stacked_bar(
data = ipums_relate |> filter(YEAR == 2022, RACE_ETH_bucket == "White", SEX == 2),
title = "Cohabitation by Age Group, 2022: White American Females")
```
```{r, echo = FALSE, fig.width = 10, fig.height = 10}
summarize_stacked_bar(
data = ipums_relate |> filter(YEAR == 2022, RACE_ETH_bucket == "White", SEX == 1),
title = "Cohabitation by Age Group, 2022: White American Males")
```
# Other Americans
```{r, echo = FALSE, fig.width = 10, fig.height = 10}
summarize_stacked_bar(
data = ipums_relate |> filter(YEAR == 2022, RACE_ETH_bucket == "Other", SEX == 2),
title = "Cohabitation by Age Group, 2022: Not Otherwise Classified American Females")
```
```{r, echo = FALSE, fig.width = 10, fig.height = 10}
summarize_stacked_bar(
data = ipums_relate |> filter(YEAR == 2022, RACE_ETH_bucket == "Other", SEX == 1),
title = "Cohabitation by Age Group, 2022: Not Otherwise Classified American Males")
```