library(canpumf)
#> canpumf.cache_path is not set.
#> Downloaded data is stored in tempdir() and discarded when this R session ends, so it will be re-downloaded next time.
#> To persist data across sessions, set a cache directory:
#> options(canpumf.cache_path = "~/canpumf_cache")
#> Add that line to your .Rprofile to make it permanent.
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(ggplot2)
options(canpumf.cache_path = Sys.getenv("COMPILE_VIG_CANPUMF"))The package supports Census PUMF from 1971 through 2021, covering
individuals, hierarchical, households, and families variants depending
on the year. The 2021 and 2016 files are available via direct download;
older years must be ordered through Statistics Canada’s EFT portal and
placed in the directory pointed to by the
canpumf.cache_path option.
Statistics Canada codes non-responses in the dollar variables as
sentinel values: an income of 99999999 means “not
applicable” and 88888888 “not available”. canpumf converts
these to NA, so sum() and mean()
are right without further recoding. The reason is kept in a sidecar
table, one labelled column per affected variable, that
pumf_sidecar(tbl, "sentinels") returns on its own or joined
onto the data.
pumf_sidecar(census_2021, "sentinels") |>
count(TOTINC) |>
collect_pumf()
#> # A tibble: 3 × 2
#> TOTINC n
#> <fct> <dbl>
#> 1 <NA> 786283
#> 2 Not available 3118
#> 3 Not applicable 191467
census_2021 |>
pumf_sidecar("sentinels", join = TRUE) |>
filter(is.na(TOTINC)) |>
count(AGEGRP, TOTINC_sentinel) |>
collect_pumf()
#> # A tibble: 39 × 3
#> AGEGRP TOTINC_sentinel n
#> <fct> <fct> <dbl>
#> 1 0 to 4 years Not applicable 49068
#> 2 5 to 6 years Not applicable 21891
#> 3 7 to 9 years Not applicable 33507
#> 4 10 to 11 years Not applicable 22921
#> 5 12 to 14 years Not applicable 34484
#> 6 15 to 17 years Not available 78
#> 7 15 to 17 years Not applicable 17111
#> 8 18 to 19 years Not available 82
#> 9 18 to 19 years Not applicable 2151
#> 10 20 to 24 years Not available 283
#> # ℹ 29 more rowsAs a simple application we look at household maintainer rates by age group for select metro areas, using standard weights.
census_2021 |>
filter(CMA %in% c("Vancouver","Toronto","Montréal","Québec")) |>
filter(PRIHM!="Not applicable") |>
filter(AGEGRP!="Not available") |>
summarise(across(matches("WEIGHT|WT\\d+"),sum),
.by=c(CMA,AGEGRP,PRIHM)) |>
mutate(Share=WEIGHT/sum(WEIGHT),.by=c(CMA,AGEGRP)) |>
filter(PRIHM=="Person is primary maintainer") |>
ggplot(aes(y=AGEGRP,x=Share,fill=CMA)) +
geom_bar(stat="identity",position="dodge") +
scale_x_continuous(labels=scales::percent) +
labs(title="Age-specifc household maintainer rates",
y="Age group",
x="Household maintainer rate",
caption="StatCan Census 2021 PUMF")
#> Warning: Missing values are always removed in SQL aggregation functions.
#> Use `na.rm = TRUE` to silence this warning
#> This warning is displayed once every 8 hours.Census PUMF data is quite rich and fairly accurate when slicing it coarsely like this, but it’s always good to check for variability in the data. Census PUMF (for the recent years) comes with 16 replication weights, and we can look at the range they provide for the estimates.
census_2021 |>
filter(CMA %in% c("Vancouver","Toronto","Montréal","Québec")) |>
filter(PRIHM!="Not applicable") |>
filter(AGEGRP!="Not available") |>
summarise(across(matches("WEIGHT|WT\\d+"),sum),
.by=c(CMA,AGEGRP,PRIHM)) |>
collect_pumf() |>
tidyr::pivot_longer(matches("WT\\d+"),names_to="Weights") |>
mutate(Share=WEIGHT/sum(WEIGHT),
Share_bsw=value/sum(value),
.by=c(CMA,AGEGRP,Weights)) |>
filter(PRIHM=="Person is primary maintainer") |>
ggplot(aes(y=AGEGRP,fill=CMA)) +
geom_bar(aes(x=Share),stat="identity",position="dodge") +
geom_boxplot(aes(x=Share_bsw, group=interaction(CMA,AGEGRP)), fill=NA,shape=1, position="dodge") +
scale_x_continuous(labels=scales::percent) +
labs(title="Age-specifc household maintainer rates",
y="Age group",
x="Household maintainer rate (and replication weight ranges)",
caption="StatCan Census 2021 PUMF") However, if we want a deeper understanding of the robustness of the
results we can add bootstrap weights, by default
add_bootstrap_weights will add 500 bootstrap weights. They
cover the whole table, which for the Census is a lot of data to keep in
a temporary table, so it pays to store them in the database once, from a
table opened with
get_pumf("Census", "2021", read_only = FALSE). Every later
call, like the one below, then finds the stored weights and only joins
them.
census_2021 |>
filter(CMA %in% c("Vancouver","Toronto","Montréal","Québec")) |>
filter(PRIHM!="Not applicable") |>
filter(AGEGRP!="Not available") |>
add_bootstrap_weights("WEIGHT") |>
summarise(across(matches("WEIGHT|WT\\d+|CPBSW\\d+"),sum),
.by=c(CMA,AGEGRP,PRIHM)) |>
collect_pumf() |>
tidyr::pivot_longer(matches("CPBSW\\d+"),names_to="Weights") |>
mutate(Share=WEIGHT/sum(WEIGHT),
Share_bsw=value/sum(value),
.by=c(CMA,AGEGRP,Weights)) |>
filter(PRIHM=="Person is primary maintainer") |>
ggplot(aes(y=AGEGRP,fill=CMA)) +
geom_bar(aes(x=Share),stat="identity",position="dodge") +
geom_boxplot(aes(x=Share_bsw, group=interaction(CMA,AGEGRP)), fill=NA,shape=1, position="dodge") +
scale_x_continuous(labels=scales::percent) +
labs(title="Age-specifc household maintainer rates",
y="Age group",
x="Household maintainer rate (and bootstrap weight ranges)",
caption="StatCan Census 2021 PUMF")