Hello to the CTPP listserv. Thought I would share something I started several months ago.
I somehow got distracted - - baseball or beer or squirrels or something!
My ex-colleague mentioned a year or so ago the R-package “CTPPr” produced by Westat staff.
Thought I would give it a test drive.
It works great for its intended use: table specific downloads of Part 1, 2, 3 data for
entire states (or multiple states).
Ideally I would flesh out these examples, using the census mapping options in R, as well
as combining it with other census R-packages such as TIGRIS and TIDYCENSUS.
Here’s my “getting started” R script for extracting CTPP 2006/10 and 2012/16 data using
the CTPPr package.
Cheers, Chuck Purvis, Hayward, California (formerly with the Metropolitan Transportation
Commission, San Francisco, California).
############################################################
# CTPPr_GettingStarted_1.r
# Getting started with the Westat R Package "CTPPr"
# CTPPr is authored by: Anthony Fucci, Alexander Cates
# and Marcelo Simas of Westat
# Note that there isn't an option to select only certain
# tracts, places, TADs, TAZs, WITHIN a state... only the
# ENTIRE state! User may prefer to use the Beyond2020
# software for extracting only some parts within a state!
#
# Examples Developed by Chuck Purvis, Hayward, California
# -- March 21-25, 2021 --
###########################################################
# Install CTPPr onto local computer.. Just need to do once!
# I'm just not sure about etiquette for updating packages!
install.packages('devtools')
devtools::install_github('Westat-Transportation/CTPPr')
# Activate/load the CTPPr library for this script.
library("CTPPr")
# optional libraries to load, depending on how I expand the examples!
library('dplyr')
library("tidyverse")
library("magrittr")
# Produce a VIEWER table of ALL CTPP Tables
# It defaults to the CTPP 2012-16 data. Not sure about 2006-10....
ctpp_tables()
# Demonstrate various examples of the download_ctpp function....
# without geography= or state=, the function defaults to US States,
# 50 US States + District of Columbia + Puerto Rico!
# Package authors strongly recommend using BOTH geography= and
# state= or you will crash the AASHTO servers or worse.
# The default datset is the 2012-2016 CTPP.
# CTPPr can also be used to pull 2006-2010 CTPP data.
# But TABLE NUMBERING can be different between the two databases,
# so be really careful!
# Note that a simple one-cell table will retrieve three variables:
# 1. Geography Variable Name (RESIDENCE, WORKPLACE, or both!)
# 2. Estimate
# 3. Standard Error (the Authors have divided the MOE by 1.645 to yield SE)
# Table A101100 is Total Population! A one-cell table!
# "A" -- based on the entire ACS microdata
# "1" -- residence-based geographies
# "01" -- data universe is "total population"
# "100" -- This is Table #100.
# These three examples pull US States.
totalpop_1216 <- download_ctpp(A101100)
totalpop_1216a <- download_ctpp(A101100,dataset='2016')
totalpop_0610 <- download_ctpp(A101100,dataset='2010')
# This example, weirdly, provides County data for California!
Califpop_1216 <- download_ctpp(A101100,dataset='2016',
geography='State',
state="California")
# Example: Counties within California
Calpop_1216a <- download_ctpp(A101100,dataset='2016',
geography='County',
state="California")
# Example: Places within California
CalPlacePop_1216a <- download_ctpp(A101100,dataset='2016',
geography='Place',
state="California")
# Example: All Census Tracts within California with the Tract Name
CalTractPop_1216a <- download_ctpp(A101100,dataset='2016',
geography='Tract',
state="California",
output="Name")
# Example: Tracts within California, Using the FIPS Code instead of Name
CalTractPop_1216b <- download_ctpp(A101100,dataset='2016',
geography='Tract',
state="California",
output = 'FIPS Code')
# Example: Tracts within California, Split FIPS Code
# probably need some follow-on examples to split the FIPS code
# into 3 separate variables: state, county, tract.
CalTractPop_1216c <- download_ctpp(A101100,dataset='2016',
geography='Tract',
state="California",
output = 'Split FIPS Code')
# Example: PUMAs within California
# And then add a new variable, Coefficient of Variation (SE/Estimate)
CalPUMAPop_1216 <- download_ctpp(A101100,dataset='2016',
geography='PUMA',
state="California",
output = 'Name')
CalPUMAPop_1216$CV =
CalPUMAPop_1216$SE / CalPUMAPop_1216$Estimate
# Example: PUMAs within California, FIPS Code
CalPUMAPop_1216a <- download_ctpp(A101100,dataset='2016',
geography='PUMA',
state="California",
output = 'FIPS Code')
# Example: PUMAs within California, FIPS Code and Name!!
CalPUMAPop_1216b <- download_ctpp(A101100,dataset='2016',
geography='PUMA',
state="California",
output = 'FIPS and Name')
# Example: PUMAs within California, Split FIPS Code
CalPUMAPop_1216c <- download_ctpp(A101100,dataset='2016',
geography='PUMA',
state="California",
output = 'Split FIPS Code')
# Example: Metropolitan Statistical Areas within California
# Note: CTPPr documentation doesn't mention the State>MSA sum level.
# but it works... at least for California
MSA_Pop_1216a <- download_ctpp(A101100,dataset='2016',
geography='MSA',
state="California",
output = 'Name')
# Example: Principal Cities within Each MSA in California.
# Note that MSAs may have multiple Principal Cities!!!
MSACity_Pop_1216 <- download_ctpp(A101100,dataset='2016',
geography='City',
state="California",
output = 'Name')
# for whatever reason, pulling urbanized areas isn't working.
UA_Pop_1216 <- download_ctpp(A101100,dataset='2016',
geography='UA',
# state="California",
output = 'Name')
# Other residence and workplace geography include:
# OUSA (workplace tables only); TAD; and TAZ, and MCD(12 MCD states)
# Pull a two way table: Population by Hispanic (3) by Race (5)
# This yields 15 records (3*5) for each County
# Two Additional columns are included in this particular data frame:
# -- "Hispanic Origin 3"
# -- "Race of Person 5"
Calif_County_Race_Hisp_1216 <-
download_ctpp(A101204,dataset = '2016', output = "FIPS Code",
geography = 'County', state = "California")
# Pull a REALLY big two-way table: Workers by Occupation (25)
# by Industry (15)...
# This yields 21,750 observations for the 58 county California,
# Columns include: RESIDENCE, "Occupation 25", "Industry 15",
# Estimate, and SE.
Calif_County_Workers_Occ_Ind_1216 <-
download_ctpp(A102214,dataset = '2016', output = "FIPS Code",
geography = 'County', state = "California")
# The following doesn't work, even though it's listed in the view
# of available tables. Be careful!!
# I don't think "C" tables are really available ....
Calif_County_Workers_Occ_Ind_1216aaa <-
download_ctpp(A102214C,dataset = '2016', output = "FIPS Code",
geography = 'County', state = "California")
##################################################################
# Workplace Tables! (Part 2 Tables)
# Total Workers at Work by County of Work
# Output columns in the data frame are: WORKPLACE, Estimate, and SE
# "A" -- data is from the entire ACS microdata, not just the 5% in PUMS
# "2" -- these are tabulated at the work-end (or workplace)
# "02" -- the data universe is "total workers at work"
# "100" -- this is table #100
Calif_COW_TotWorkers <-
download_ctpp(A202100,dataset = '2016', output = "Name",
geography = 'County', state = "California")
##################################################################
# Worker Flow Tables (Part 3 Tables)
# Total Worker Flows, County-to-County, Intra-State California
# Intra State California is 58 by 58 counties, for 3,364 records!
# 58 * 58 = 3364.... lots of records with zero commuters!!
# Output columns include: RESIDENCE, WORKPLACE, Estimate, SE
Calif_Co2Co_TotWork <-
download_ctpp(id="A302100",
dataset = '2016',
output = "FIPS and Name",
geography = 'County->County',
state = "California")
# Pull County-to-County workers for four Western US States
# This yields 15,876 records, but a lot of the records have 0 workers!
Calif_Neighbors_Co2Co_TotWork <-
download_ctpp(id="A302100",
dataset = '2016',
output = "FIPS and Name",
geography = 'County->County',
state = "California,Arizona,Nevada,Oregon")
# Perhaps continue the above example to just retain
# Intra-state records, and inter-state into/out of California....
# End of Getting Started Part 1.