[API] rnassqs: Application

Estimates related to U.S. agricultural production

Thierry Warin https://warin.ca/aboutme.html (HEC Montréal and CIRANO (Canada))https://www.hec.ca/en/profs/thierry.warin.html
02-20-2020

This course will teach you how to use the rnassqs package.

The U.S. DEPARTMENT OF AGRICULTURE provides a wide range of agricultural data that includes animal, crop, demographic, economic, and environmental measures across a number of geographies and time periods.

How to use the rnassqs package

Step One : Register your API key


# Load the rnassqs library
library(rnassqs)

To access the API’s functions, a key is necessary. To do so, request a key


#NASSQS_TOKEN="yourAPIkey"
#nassqs_auth(key = "yourAPIkey")

Step Two : Extract the data


# Available parameters
nassqs_params()

 [1] "agg_level_desc"        "asd_code"             
 [3] "asd_desc"              "begin_code"           
 [5] "class_desc"            "commodity_desc"       
 [7] "congr_district_code"   "country_code"         
 [9] "country_name"          "county_ansi"          
[11] "county_code"           "county_name"          
[13] "CV"                    "domaincat_desc"       
[15] "domain_desc"           "end_code"             
[17] "freq_desc"             "group_desc"           
[19] "load_time"             "location_desc"        
[21] "prodn_practice_desc"   "reference_period_desc"
[23] "region_desc"           "sector_desc"          
[25] "short_desc"            "state_alpha"          
[27] "state_ansi"            "state_name"           
[29] "state_fips_code"       "statisticcat_desc"    
[31] "source_desc"           "unit_desc"            
[33] "util_practice_desc"    "Value"                
[35] "watershed_code"        "watershed_desc"       
[37] "week_ending"           "year"                 
[39] "zip_5"                

# Download the data from the Quick Stats database.
dataframe <- nassqs(commodity_desc = "CORN",
             year__GE = 2019,
             agg_level_desc = "STATE",
             statisticcat_desc = "YIELD")

  |                                                                  
  |                                                            |   0%
  |                                                                  
  |==                                                          |   3%
  |                                                                  
  |====                                                        |   7%
  |                                                                  
  |======                                                      |  11%
  |                                                                  
  |===========                                                 |  18%
  |                                                                  
  |===============                                             |  25%
  |                                                                  
  |===================                                         |  32%
  |                                                                  
  |====================                                        |  33%
  |                                                                  
  |====================                                        |  34%
  |                                                                  
  |=========================                                   |  41%
  |                                                                  
  |=============================                               |  48%
  |                                                                  
  |=============================                               |  49%
  |                                                                  
  |==============================                              |  50%
  |                                                                  
  |==================================                          |  57%
  |                                                                  
  |===================================                         |  58%
  |                                                                  
  |=======================================                     |  66%
  |                                                                  
  |============================================                |  73%
  |                                                                  
  |============================================                |  74%
  |                                                                  
  |=============================================               |  74%
  |                                                                  
  |=================================================           |  82%
  |                                                                  
  |==================================================          |  83%
  |                                                                  
  |======================================================      |  90%
  |                                                                  
  |=========================================================== |  98%
  |                                                                  
  |=========================================================== |  99%
  |                                                                  
  |============================================================| 100%

# Select only the data you need
dataframefinal<- dplyr::select(dataframe, Value, location_desc, state_alpha)

# Data wrangling
dataframefinal$Value <- as.numeric(dataframefinal$Value) # numeric format

dataframeAgg <- aggregate(Value~ state_alpha, data = dataframefinal, mean) # aggregate function

names(dataframeAgg)[names(dataframeAgg) == "state_alpha"] <- "STUSPS" #rename column

Step 3 : Visualize your data


# Load the following libraries
library(dplyr)
library(spdep)
library(maptools)
library(leaflet)
library(maps)
library(rgdal)

# Open your shp file with the readOGR function

USA <- readOGR('cb_2018_us_state_500k.shp')

OGR data source with driver: ESRI Shapefile 
Source: "/home/thibaults/portfolio/warin/_posts/api-rnassqs-application/cb_2018_us_state_500k.shp", layer: "cb_2018_us_state_500k"
with 56 features
It has 9 fields
Integer64 fields read as strings:  ALAND AWATER 

Shapefile link United States Census Bureau


# Use "Left join" function to combine your two data frames

USA@data <- left_join(USA@data, dataframeAgg, by = "STUSPS")

USA@data$STUSPS <- as.character(USA@data$STUSPS)

# Create labels

USA@data$NAME <- as.character(USA@data$NAME)

labels <- sprintf("<strong>%s</strong><br/>%g", USA@data$NAME, USA@data$Value) %>% lapply(htmltools::HTML)

# Determinate the intervalls that will be shown in the map legend
bins <- c(0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200)

# Choose a color scheme for your map

colors <- colorBin("Greens", domain = USA@data$Value, bins = bins)

# Plot the data using leaflet

leaflet(USA) %>% setView(lng = -95.712891, lat = 37.090240, zoom = 4) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addLegend(pal = colors, values = USA@data$Value, opacity = 0.7, title = NULL, position = "bottomleft") %>%
  addPolygons(fillColor = ~colors(USA@data$Value),
              weight = 2,
              opacity = 1,
              color = "white",
              dashArray = 1,
              fillOpacity = 0.8,
              highlight = highlightOptions(weight = 2,
                                           color = "black",
                                           dashArray = 1,
                                           fillOpacity = 0.7,
                                           bringToFront = TRUE),
              label = labels
              )