Last updated: 2023-03-05
Checks: 7 0
Knit directory: USC-QCB-IPUMS/
This reproducible R Markdown analysis was created with workflowr (version 1.7.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.
Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.
Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.
The command set.seed(20230130)
was run prior to running
the code in the R Markdown file. Setting a seed ensures that any results
that rely on randomness, e.g. subsampling or permutations, are
reproducible.
Great job! Recording the operating system, R version, and package versions is critical for reproducibility.
Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.
Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.
Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.
The results in this page were generated with repository version ef5986b. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.
Note that you need to be careful to ensure that all relevant files for
the analysis have been committed to Git prior to generating the results
(you can use wflow_publish
or
wflow_git_commit
). workflowr only checks the R Markdown
file, but you know if there are other scripts or data files that it
depends on. Below is the status of the Git repository when the results
were generated:
Ignored files:
Ignored: .DS_Store
Ignored: .Rhistory
Ignored: .Rproj.user/
Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.
These are the previous versions of the repository in which changes were
made to the R Markdown (analysis/exploratory.Rmd
) and HTML
(docs/exploratory.html
) files. If you’ve configured a
remote Git repository (see ?wflow_git_remote
), click on the
hyperlinks in the table below to view the files as they were in that
past version.
File | Version | Author | Date | Message |
---|---|---|---|---|
Rmd | ef5986b | Tina Lasisi | 2023-03-05 | publish website |
Rmd | 3c70f8e | Tina Lasisi | 2023-02-06 | Publish first analyses |
# open data
library(ipumsr)
setwd("data")
ddi <- read_ipums_ddi("usa_00006.xml")
data <- read_ipums_micro(ddi)
Use of data from IPUMS USA is subject to conditions including that users should
cite the data appropriately. Use command `ipums_conditions()` for more details.
setwd("../")
head(data)
# A tibble: 6 × 26
YEAR SAMPLE SERIAL CBSER…¹ HHWT CLUSTER STRATA GQ NFAMS NMOTH…²
<int> <int+lbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int+l> <int+l> <int+l>
1 1850 185001 [185… 101 NA 97 1.85e12 1.10e8 1 [Hou… 1 [1 f… 1
2 1850 185001 [185… 101 NA 97 1.85e12 1.10e8 1 [Hou… 1 [1 f… 1
3 1850 185001 [185… 101 NA 97 1.85e12 1.10e8 1 [Hou… 1 [1 f… 1
4 1850 185001 [185… 101 NA 97 1.85e12 1.10e8 1 [Hou… 1 [1 f… 1
5 1850 185001 [185… 101 NA 97 1.85e12 1.10e8 1 [Hou… 1 [1 f… 1
6 1850 185001 [185… 101 NA 97 1.85e12 1.10e8 1 [Hou… 1 [1 f… 1
# … with 16 more variables: NFATHERS <int+lbl>, PERNUM <dbl>, PERWT <dbl>,
# FAMSIZE <int+lbl>, NCHILD <int+lbl>, NCHLT5 <int+lbl>, NSIBS <int+lbl>,
# SEX <int+lbl>, AGE <int+lbl>, BIRTHYR <dbl+lbl>, MARRNO <int+lbl>,
# CHBORN <int+lbl>, RACE <int+lbl>, RACED <int+lbl>, HISPAN <int+lbl>,
# HISPAND <int+lbl>, and abbreviated variable names ¹CBSERIAL, ²NMOTHERS
library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.4.0 ✔ purrr 0.3.5
✔ tibble 3.1.8 ✔ dplyr 1.0.10
✔ tidyr 1.2.1 ✔ stringr 1.5.0
✔ readr 2.1.3 ✔ forcats 0.5.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
library(data.table)
Attaching package: 'data.table'
The following objects are masked from 'package:dplyr':
between, first, last
The following object is masked from 'package:purrr':
transpose
# Fix labels
vars_to_factor <- c("RACE", "HISPAN", "SEX", "CHBORN", "MARRNO")
data[vars_to_factor] <- lapply(data[vars_to_factor], as_factor)
vars_to_zap <- c("YEAR", "SERIAL", "AGE", "BIRTHYR", "NCHILD", "NCHLT5")
data[vars_to_zap] <- lapply(data[vars_to_zap], zap_labels)
# Specify the years to filter
# years_to_filter <- c(1870, 1900, 1930, 1960, 1990)
years_to_filter <- seq(1850, 2020, by=10)
keepvars = c("YEAR", "SEX", "AGE", "BIRTHYR", "RACE", "CHBORN", "NCHILD", "NCHLT5")
data_filtered <- data %>%
# Filter data for only the specified years
# filter(YEAR %in% years_to_filter) %>%
# Filter data for non-Hispanic only White and Black
filter(HISPAN == "Not Hispanic",
RACE == "White" | RACE == "Black/African American") %>%
select(all_of(keepvars)) %>%
mutate(AGE_RANGE = case_when(
AGE < 15 ~ "<15",
AGE >= 15 & AGE < 20 ~ "15-20",
AGE >= 20 & AGE < 25 ~ "20-25",
AGE >= 25 & AGE < 30 ~ "25-30",
AGE >= 30 & AGE < 40 ~ "30-40",
AGE >= 40 & AGE < 50 ~ "40-50",
AGE >= 50 ~ "50+"
))
data_grouped <- data_filtered %>%
group_by(YEAR, RACE, SEX, AGE_RANGE) %>%
mutate(CHBORN_num = as.numeric(CHBORN))
data_grouped_subset <- data_grouped %>%
sample_n(150)
mean_cl_normal <- function(y) {
mean_y <- mean(y)
sd_y <- sd(y)
ymin <- max(mean_y - sd_y, 0)
ymax <- mean_y + sd_y
c(y = mean_y, ymin = ymin, ymax = ymax)
}
data_grouped_subset %>%
filter(SEX == "Female") %>%
ggplot(aes(x = YEAR, y = CHBORN_num, group = RACE, color = RACE, fill = RACE)) +
stat_summary(fun.data = mean_cl_normal, geom = "ribbon", alpha = 0.3) +
stat_summary(fun.y = mean, geom = "line") +
scale_color_discrete(name = "Race") +
labs(x = "Year", y = "Number of Children", title = "Distribution of children born to Women across the years") +
theme_classic() +
facet_grid(rows = vars(AGE_RANGE), cols = vars(RACE))
Warning: The `fun.y` argument of `stat_summary()` is deprecated as of ggplot2 3.3.0.
ℹ Please use the `fun` argument instead.
Warning: Removed 23100 rows containing non-finite values (`stat_summary()`).
Removed 23100 rows containing non-finite values (`stat_summary()`).
data_grouped_subset %>%
ggplot(aes(x = YEAR, y = NCHILD, group = RACE, color = RACE)) + stat_summary(fun = mean, geom = "line") +
facet_grid(rows = vars(AGE_RANGE), cols = vars(SEX))+
labs(x = "Year", y = "Number of Children", title = "Mean number of own children per household")+
theme(legend.position = "bottom") +
theme_classic()
data_grouped_subset %>%
ggplot(aes(x = YEAR, y = NCHILD, group = RACE, color = RACE, fill = RACE)) +
stat_summary(fun.data = mean_cl_normal, geom = "ribbon", alpha = 0.3) +
stat_summary(fun.y = mean, geom = "line") +
scale_color_discrete(name = "Race") +
labs(x = "Year", y = "Number of Children", title = "Number of own children per household") +
theme_classic() +
facet_grid(rows = vars(AGE_RANGE), cols = vars(SEX))+
theme(legend.position = "bottom")
data_grouped_subset %>%
ggplot(aes(x = factor(YEAR), y = NCHILD, color = RACE)) +
geom_boxplot(alpha = 0.3, outlier.size = 0.5) +
scale_color_discrete(name = "Race") +
labs(x = "Year", y = "Number of Children", title = "Number of own children per household") +
theme_classic() +
theme(legend.position = "bottom") +
facet_grid(rows = vars(AGE_RANGE), cols = vars(SEX))
ggplot(data_grouped_subset %>% filter(SEX == "Male"), aes(x = factor(YEAR), y = NCHILD, fill = RACE, color = RACE)) +
geom_boxplot(alpha = 0.3, outlier.size = 0.5) +
scale_color_discrete(name = "Race") +
labs(x = "Year", y = "Number of Children", title = "Number of own children per household (Males)") +
theme_classic() +
theme(legend.position = "bottom") +
facet_grid(rows = vars(AGE_RANGE), cols = vars(SEX))
ggplot(data_grouped_subset %>% filter(SEX == "Female"), aes(x = factor(YEAR), y = NCHILD, fill = RACE, color = RACE)) +
geom_boxplot(alpha = 0.3, outlier.size = 0.5) +
scale_color_discrete(name = "Race") +
labs(x = "Year", y = "Number of Children", title = "Number of own children per household (Females)") +
theme_classic() +
theme(legend.position = "bottom") +
facet_grid(rows = vars(AGE_RANGE), cols = vars(SEX))
data_grouped_subset %>%
ggplot(aes(x = YEAR, y = NCHLT5, group = RACE, color = RACE, fill = RACE)) +
stat_summary(fun.data = mean_cl_normal, geom = "ribbon", alpha = 0.3) +
stat_summary(fun.y = mean, geom = "line") +
scale_color_discrete(name = "Race") +
labs(x = "Year", y = "Number of Children", title = "Number of own children under 5 per household") +
theme_classic() +
facet_grid(rows = vars(AGE_RANGE), cols = vars(SEX))
data_grouped_subset %>%
ggplot(aes(x = factor(YEAR), y = NCHLT5, color = RACE)) +
geom_boxplot(alpha = 0.3, outlier.size = 0.5) +
scale_color_discrete(name = "Race") +
labs(x = "Year", y = "Number of Children", title = "Number of own children under 5 per household") +
theme_classic() +
theme(legend.position = "bottom") +
facet_grid(rows = vars(AGE_RANGE), cols = vars(SEX))
data_sum <- data_grouped %>%
summarize(mean_child = mean(CHBORN_num),
sd_child = sd(CHBORN_num)) %>%
drop_na() %>%
ungroup()
`summarise()` has grouped output by 'YEAR', 'RACE', 'SEX'. You can override
using the `.groups` argument.
sessionInfo()
R version 4.2.2 (2022-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.0.1
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] data.table_1.14.6 forcats_0.5.2 stringr_1.5.0 dplyr_1.0.10
[5] purrr_0.3.5 readr_2.1.3 tidyr_1.2.1 tibble_3.1.8
[9] ggplot2_3.4.0 tidyverse_1.3.2 ipumsr_0.5.2 workflowr_1.7.0
loaded via a namespace (and not attached):
[1] httr_1.4.4 sass_0.4.4 hipread_0.2.3
[4] jsonlite_1.8.3 modelr_0.1.10 bslib_0.4.1
[7] assertthat_0.2.1 getPass_0.2-2 highr_0.9
[10] googlesheets4_1.0.1 cellranger_1.1.0 yaml_2.3.6
[13] pillar_1.8.1 backports_1.4.1 glue_1.6.2
[16] digest_0.6.30 promises_1.2.0.1 rvest_1.0.3
[19] colorspace_2.0-3 htmltools_0.5.3 httpuv_1.6.6
[22] pkgconfig_2.0.3 broom_1.0.1 haven_2.5.1
[25] scales_1.2.1 processx_3.8.0 whisker_0.4
[28] later_1.3.0 tzdb_0.3.0 timechange_0.1.1
[31] git2r_0.30.1 googledrive_2.0.0 farver_2.1.1
[34] generics_0.1.3 ellipsis_0.3.2 cachem_1.0.6
[37] withr_2.5.0 cli_3.4.1 magrittr_2.0.3
[40] crayon_1.5.2 readxl_1.4.1 evaluate_0.18
[43] ps_1.7.2 fs_1.5.2 fansi_1.0.3
[46] xml2_1.3.3 tools_4.2.2 hms_1.1.2
[49] gargle_1.2.1 lifecycle_1.0.3 munsell_0.5.0
[52] reprex_2.0.2 callr_3.7.3 compiler_4.2.2
[55] jquerylib_0.1.4 rlang_1.0.6 grid_4.2.2
[58] rstudioapi_0.14 labeling_0.4.2 rmarkdown_2.18
[61] gtable_0.3.1 DBI_1.1.3 R6_2.5.1
[64] lubridate_1.9.0 knitr_1.41 fastmap_1.1.0
[67] utf8_1.2.2 zeallot_0.1.0 rprojroot_2.0.3
[70] stringi_1.7.8 Rcpp_1.0.9 vctrs_0.5.1
[73] dbplyr_2.2.1 tidyselect_1.2.0 xfun_0.35