Dominitz and Manski, “More Data or Better Data? A Statistical Decision Problem”(Dominitz and Manski 2017)
Connect APIs to secondary data collection
Retrieve four types of data in R
Check what each downloaded observation represents
Document a reproducible retrieval
Share the current data plan for each research project
Readings and course handouts are available on Canvas
APIs deliver secondary data
The organization responsible for the source determines
What is measured
Which people, places, or activities are covered
When observations are collected and released
How uncertainty, missing values, and revisions are recorded
An application programming interface provides a documented way for R to request selected records from that source
What happens when R calls an API
Endpoint is the web address for a data service
Parameters specify the place, time, variables, and other selections
Response is the information returned by the service
R package translates the response into a familiar object
Many services transmit a structured text format called JSON. The packages in today’s examples read that format and return a data frame, map, or raster
Four applications
Application
Source
Returned to R
Land use
USDA Cropland Data Layer
Raster map or summary table
Water
USGS Water Data
Daily time series
Weather
NASA POWER
Daily weather table
Population
Census ACS
Estimates margins of error and map geometry
Each example begins with a small request that can be inspected before the request is expanded
Run R in the HTML slides
Wait for the R ready message after opening the HTML lecture
Select Run Code to execute a live cell
Change a named value such as threshold or selected
Select Start Over to restore the original code
The API retrieval slides show the complete request code for RStudio. The live cells use compact classroom extracts shaped like the returned records, so every student can run them from the course site
The returned columns include date, value, unit, approval status, qualifiers, station geometry, and a time-series identifier
Try it: define a low-flow threshold
Change threshold, then run the cell. The compact series represents the date and value columns returned by the water request
Assess the streamflow series
Confirm the monitoring location and parameter
Read the unit attached to each series
Examine provisional and approved status
Identify missing dates and changes in the operating record
Learn whether reservoirs, diversions, or return flows affect the gauge
Define the geographic relationship between the gauge and the study area
The gauge measures conditions at one point on a river network. A study of irrigation access may also require diversion service areas, water rights, or delivery records
Example 3: weather
Question
What temperature and precipitation conditions occurred near Fort Collins during the 2024 growing season?
Source
NASA Prediction Of Worldwide Energy Resources or POWER
Observation
Each row reports modeled weather conditions for a grid cell and day
The result contains a geographic identifier, name, estimate, margin of error, and tract geometry
Map the ACS estimates
library(ggplot2)ggplot(larimer_income) +geom_sf(aes(fill = estimate), color =NA) +labs(fill ="Median income",title ="Median household income by census tract" )
The default margin of error describes a 90 percent confidence interval
Try it: use the ACS margin of error
Change relative_moe_limit, then run the cell. The classroom extract contains one estimate and margin of error for each tract
Assess the ACS estimates
Treat the five-year estimate as a pooled period ending in the named year
Retain the margin of error
Identify estimates with large uncertainty
Use geographic identifiers as character values
Record the tract boundary vintage
Adjust monetary values when comparing periods
ACS data can describe the population and socioeconomic setting for a land or water study. Exposure measures and program participation may come from separate sources
The unit changes across sources
Source
Unit returned by the request
Time
Geography
Cropland Data Layer
Raster cell
Annual
Grid
USGS Water Data
Gauge and day
Daily
Monitoring point
NASA POWER
Grid cell and day
Daily
Weather grid
Census ACS
Estimate and area
Five-year period
Census tract
A combined dataset requires a written rule for assigning pixels, gauges, weather cells, and census areas to the study units
Combining the downloads
For every join record
Identifier used by each source
Geographic crosswalk or spatial rule
Time aggregation rule
Number of observations before and after the join
Unmatched locations and periods
Treatment of revised, suppressed, and missing values
The combined analytic sample includes the observations that satisfy all source coverage and matching rules
The reading: allocating the collection budget
Dominitz and Manski ask how to allocate a fixed budget across collection processes that differ in cost and information (Dominitz and Manski 2017)
API access reduces the time required to retrieve additional observations
Research time is also required to
Verify definitions and units
Examine coverage, missing values, flags, and uncertainty
Select spatial and temporal aggregation rules
Validate joins across sources
Archive metadata and retrieval records
Question: How should a researcher allocate one additional week across these tasks?
Select the access method
Project need
Useful access method
A defined subset repeated across places or periods
API request
Most records in a fixed release
Bulk download
A few values needed once
Web table or manual download
Repeated data cleaning and analysis
Scripted retrieval with archived raw files
Choose the method after reviewing the source documentation update schedule and request limits
Other APIs: agriculture and food
USDA NASS Quick Stats
USDA AMS Market News
USDA ERS ARMS
USDA FoodData Central
USDA NRCS Soil Data Access
Other APIs: natural resources and energy
NOAA Climate Data
Water Quality Portal
EPA ECHO
US Forest Service FIA
US Energy Information Administration
Other APIs: economies and populations
Population, firms, and regional economies
Census County Business Patterns and ZIP Business Patterns
Bureau of Labor Statistics
Bureau of Economic Analysis
Federal Reserve Economic Data
International applications
World Bank Indicators
FAOSTAT
OECD Data Explorer
Start with the documentation for the specific dataset and release used in the research
Document the retrieval
Keep these items with the project
Source and dataset name
Data documentation
Retrieval script
Parameters submitted through the script
Retrieval date
Raw files returned by the service
Processing script and data dictionary
Package and R versions
Validation results
Store API keys in the user environment file and exclude that file from version control
Class share: where are you in the process?
We will go around the room. For your project describe
The outcome, exposure, or behavior you need to observe
The primary, secondary, or combined collection approach you expect to use
A source, package, website, or other tool you have found helpful
The part of the collection process you are still working through
The next concrete step in your data search or collection
Summary
APIs provide scripted access to secondary data
R packages translate requests and responses into familiar R objects
Source documentation defines the population, measures, timing, geography, and uncertainty
Small requests make the returned structure easier to inspect
Retrieval records support reproducibility
Data joins help define the final analytic sample
References
Dominitz, Jeff, and Charles F. Manski. 2017. “More Data or Better Data? A Statistical Decision Problem.”The Review of Economic Studies 84 (4): 1583–605. https://doi.org/10.1093/restud/rdx005.