Data Visualization Using R in Undergraduate Macroeconomics

James M. Murray, Ph.D.

Department of Economics
University of Wisconsin-La Crosse
Email: jmurray@uwlax.edu


St. Louis Fed Professors’ Conference

November 8, 2024

QR Code
QR Code

Data Visualization Using R in Undergraduate Macroeconomics

Want to start using the ECODATA R package right now? Live on Posit Cloud!

  1. Login / Create Posit Cloud account at https://posit.cloud

  2. Create your own copy of the ECODATA Exercises project by following:

    https://posit.cloud/content/9084731

  3. Click + Save a Permanent Copy (top-right corner) to save your copy

  4. Login / Create FRED account at https://fred.stlouisfed.org/
    (click My Account at top right)

  5. Get your FRED key at https://fredaccount.stlouisfed.org/apikeys

  6. Copy that 32-character key and in the Posit Cloud console, set the key:
    ecodata::ecodata_set_fredkey("abcd1234efgh5678ijkl9012mnop3456")
    (insert your own 32-character key, this one won’t work)

QR Code

Great Recession & Slow Recovery

QR Code

QR Code

The Good, The Bad, and the Ugly

QR Code


The Good:

  • Student explored data on both FRED and World Bank Data

  • Student picked relevant variables, explained it correctly

  • Cogent international comparisons of the great recession

The Bad:

  • Great interactive data tools can make for poor presentation visual aids

  • Time consuming to reproduce - Return to website, select features and time periods, download image, etc.

The Ugly:

  • Inconsistent style across FRED and World Bank Data graphs

  • Very difficult to see much of the text

QR Code

QR Code

Introducing the ECODATA R Package

  • I created an R package, ecodata, to facilitate visualizing data in undergraduate macroeconomics courses

  • Designed for students and novices in R, statistics, and data analysis

  • Wrapper for the fredr, wbstats, and ggplot2 packages

  • Makes it easy to:

    • Download data from Federal Reserve Economic Data (FRED) and World Bank Data

    • Produce professional-quality data visualizations

    • View descriptions of the data

    • Cite sources

  • Do everything with 3-4 lines of code

  • Not a substitute for fredr, wbstats, or ggplot2, nor for exploring data and using interactive tools in FRED and World Bank Data

Let’s Try It Out!

QR Code

QR Code

Fast Example: Two Lines of Code

mydata <- get_ecodata("https://fred.stlouisfed.org/series/MORTGAGE30US")
ggplot_ecodata_ts(mydata, title = "Mortgage Interest Rate - Fixed Rate 30-Year", plot.recessions = TRUE)

QR Code

QR Code

Student Example: Fetch the Data

# Identify what variables I want
# Includes both FRED and World Bank Data sources
variable_sources <- c(
  "https://fred.stlouisfed.org/series/UNRATE",
  "https://fred.stlouisfed.org/series/AURUKM",
  "https://data.worldbank.org/indicator/FP.CPI.TOTL.ZG?locations=US",
  "https://data.worldbank.org/indicator/FP.CPI.TOTL.ZG?locations=GB"
)

# Optionally, provide my own names for the variables
variable_names <- c(
  "Unemployment Rate: United States",
  "Unemployment Rate: United Kingdom",
  "Inflation Rate: United States",
  "Inflation Rate: United Kingdom"
)

# Fetch the data 
mydata <- get_ecodata(variable_sources, variable_names)

# Get only Great Recession + Recovery
mydata <- mydata |>
  filter(Date >= "2007-01-01" & Date <= "2016-12-31")

QR Code

QR Code

Student Example: Plot the Data

ggplot_ecodata_facet(mydata, 
                     ncol = 2, 
                     title = "Unemployment and Inflation in U.S. and U.K.",
                     plot.recession = TRUE)

QR Code

QR Code

Student Example: Data Description

ecodata_description_table(mydata)

Variable

Code

Description

Frequency

Units

Seasonal Adjustment

Source

URL

Access Date

Unemployment Rate: United States

UNRATE

Unemployment Rate

Monthly

%

Seasonally Adjusted

FRED (R) Federal Reserve Bank of St. Louis

https://fred.stlouisfed.org/series/UNRATE

November 05, 2024

Unemployment Rate: United Kingdom

AURUKM

Unemployment Rate in the United Kingdom

Monthly

%

Seasonally Adjusted

FRED (R) Federal Reserve Bank of St. Louis

https://fred.stlouisfed.org/series/AURUKM

November 05, 2024

Inflation Rate: United States

FP.CPI.TOTL.ZG?locations=US

Inflation as measured by the consumer price index reflects the annual percentage change in the cost to the average consumer of acquiring a basket of goods and services that may be fixed or changed at specified intervals, such as yearly. The Laspeyres formula is generally used.

Annual

Index

Not Seasonally Adjusted

World Bank Data

https://data.worldbank.org/indicator/FP.CPI.TOTL.ZG?locations=US

November 05, 2024

Inflation Rate: United Kingdom

FP.CPI.TOTL.ZG?locations=GB

Inflation as measured by the consumer price index reflects the annual percentage change in the cost to the average consumer of acquiring a basket of goods and services that may be fixed or changed at specified intervals, such as yearly. The Laspeyres formula is generally used.

Annual

Index

Not Seasonally Adjusted

World Bank Data

https://data.worldbank.org/indicator/FP.CPI.TOTL.ZG?locations=GB

November 05, 2024

QR Code

QR Code

Data for All U.S. States

unemployed <- get_ecodata_allstates_fred("https://fred.stlouisfed.org/series/MOUR")
ggplot_ecodata_bar(unemployed,
                   plot_at = "2020-04-01",
                   highest = 10, 
                   highlight = "Illinois",
                   title = "States with Highest Unemployment During COVID")

Documentation

  • All the documentation: https://murraylax.org/ecodata/

  • Getting Started Guide

    • Walks through many of the most common uses of the package.

    • Download data for multiple states or countries with ease

    • Plots with multiple variables, bar plots

    • Data transformations, computing new quantities

  • Full reference manual

  • Instructional Vignettes

    • Focus is learning macroeconomics, using data

    • Worked-out examples using the package to explore macroeconomic principles

QR Code

QR Code

Why Scripting Languages?

  • Reproducible: The code is the set of instructions for what you created

  • Efficient: Difficult to make the first graph, but it’s easy to make the 2nd and 100th graphs!

  • Flexible: Can easily change the data, the graph, the labels, etc.

  • Used in other courses: R is used in econometrics, statistics, data science, etc.

  • Used in industry, even among those who are not data scientists

  • Even more important / relevant with AI

    • AI assistance makes coding more accessible

    • Verification and reproducibility is key with AI-generated content

QR Code

QR Code

Why Avoid Scripting Languages?

  • Steep learning curve

  • Easy to do the hard things, but hard to do the easy things

  • This is Principles of Macroeconomics, not what I signed up for!

ECODATA Package

  • Gentle learning curve!

  • Easy to do easy things

  • Use simple reproducible code to analyze macroeconomics principles

QR Code

QR Code

Why ECODATA for Principles of Macroeconomics?

  • Data visualization is a key skill for everyone

  • Early introduction makes subsequent classes easier

  • Data is used throughout the economics discipline, it should be used throughout economics curriculum

  • Repeated practice throughout undergraduate degree builds mastery

QR Code

QR Code

Instructional Vignettes

  • Supply and Demand for Housing

    • Data on construction prices, new house prices, and housing starts.

    • Does a supply and demand model prediction explain the data?

  • Exchange Rates and Interest Rates

    • Data on exchange rates and interest rates interest rates in U.K. and U.S.

    • Does a supply and demand model for currencies explain the data?

  • Unemployment and Recessions

    • State and national data on unemployment and during the 2008-2009 recession.
  • And I will be making a lot more!

QR Code

QR Code

What Are Your Ideas?

  • Do you have or envision a short low-stakes assignment where students visualize macroeconomic data?

  • Answer these questions about one such assignment:

    1. What is the macroeconomics concept or theory that students apply?

    2. What variables do the students need?

    3. What will the students discover?

  • Share your ideas! Email: jmurray@uwlax.edu

  • What me to create instructional vignette around your idea? Let me know!




Thank you!

QR Code

QR Code