Me: Prof. Kim Joseph Ruhl
School: BS Bowling Green State University (Ohio); PhD U. Minnesota
This course is about using data and 'the right kind of analysis' to answer questions.
Let's look at some visualizations:
Go to www.gapminder.org/tools/ (open in new tab)
We will able to make this figure by the end of the semester.
Go to https://www.cnn.com/election/2020/results/state/wisconsin/president (new tab) and focus on the presidential results
Take 5 minutes (work with those around you) and try to answer:
We will able to make this figure by the end of the semester.
Go to data.oecd.org/price/inflation-cpi.htm
Take 5 minutes (work with those around you) and try to answer:
We will able to make this figure better by the end of the semester.
We want to:
Python gives us
Let's plot the dollar-yuan exchange rate to get a feel for what we can do with Python. (I do not expect you to follow all of this today!)
We will get the data from the St. Louis FRB FRED database. We will work with FRED often. It is an easy place to get economic data.
# Do some preliminary things to get Python set up.
# Import needed packages
import pandas as pd # the workhorse data package
import pandas_datareader.data as web # for FRED data calls
import matplotlib.pyplot as plt # for plots
import datetime as dt # for dates
# IPython command to include plots in the notebook
%matplotlib inline
# Create datetime object to hold the begin date
start = dt.datetime(1990, 1, 1)
# Get monthly Yuan-per-dollar exchange rate. 'fred' tells the data reader to use the FRED repository.
# 'EXCHUS' is the name of the data series in FRED. You can find the series codes (names) on the FRED website.
exchus = web.DataReader('EXCHUS', 'fred', start)
# Print out the first 3 observations.
print(exchus.head(3))
# Print out the last 3 observations.
print(exchus.tail(3))
EXCHUS DATE 1990-01-01 4.7339 1990-02-01 4.7339 1990-03-01 4.7339 EXCHUS DATE 2023-06-01 7.1614 2023-07-01 7.1863 2023-08-01 7.2486
# The basic plotting command. The 'b--' means 'make the line blue and dashed.'
plt.plot(exchus.index, exchus['EXCHUS'], 'b--' )
plt.xlabel('date') # Label the axes
plt.ylabel('yuan per dollar')
plt.show()
That worked pretty well! We could continue to customize the plot by adding data markers, changing colors, adding legends, etc. We will leave that stuff for later.
We could have made that figure in Excel. Did Python buy us much? In this case, maybe not, although I would argue that
exchus = web.DataReader('EXCHUS', 'fred', start)
is easier than going to FRED, downloading the data, getting set up in a workbook and then plotting.
What if we need to make several plots? What if we need to update the plot every day?
Python just requires a few extra lines of code. Again, don't worry about the details, we are just taking a quick look at what Python can do for us. All of this will make more sense later.
# Make list with the names of the data we would like to plot.
ctry_list = ['EXCHUS', 'EXJPUS', 'EXCAUS', 'EXUSEU'] # China, Japan, Canada, Euro
# Make a list of the units for the y axis.
units = ['yuan per USD', 'yen per USD', 'CAD per USD', 'USD per Euro']
# Read the data. Pass a list of codes rather a single string.
ex_many = web.DataReader(ctry_list, 'fred', start)
print(ex_many)
EXCHUS EXJPUS EXCAUS EXUSEU DATE 1990-01-01 4.7339 144.9819 1.1720 NaN 1990-02-01 4.7339 145.6932 1.1965 NaN 1990-03-01 4.7339 153.3082 1.1800 NaN 1990-04-01 4.7339 158.4586 1.1641 NaN 1990-05-01 4.7339 154.0441 1.1747 NaN ... ... ... ... ... 2023-04-01 6.8876 133.4745 1.3484 1.0962 2023-05-01 6.9854 137.0532 1.3517 1.0867 2023-06-01 7.1614 141.3581 1.3286 1.0840 2023-07-01 7.1863 140.9360 1.3211 1.1067 2023-08-01 7.2486 144.7804 1.3478 1.0910 [404 rows x 4 columns]
To plot, we loop over the list of variable names (and the units) and plot them.
fig, ax = plt.subplots(2, 2, figsize=(15,6))
for ctry, unit, axi in zip(ctry_list, units, fig.axes) :
axi.plot(ex_many.index, ex_many[ctry], 'r-')
axi.set_ylabel(unit)
Used for assignments and announcements (at students' request)
I am pretty new to Canvas
Did you receive an announcement from me?
Deliverable | Weight in final grade |
---|---|
Student survey and winstat logon | 1% |
Best four coding practices | 4% |
In-class exam 1 | 20% |
In-class exam 2 | 30% |
Project roster | 1% |
Project proposal | 9% |
Project | 35% |