For the next challenge, carry over your existing code once again (by copy-pasting the entire cell) and make some modifications.
Plot the search for "unemployment benefits" against the official unemployment rate.
Change the title to: Monthly Search of "Unemployment Benefits" in the U.S. vs the U/E Rate
Change the y-axis label to: FRED U/E Rate
Change the axis limits
Add a grey grid to the chart to better see the years and the U/E rate values. Use dashed lines for the line style.
Can you discern any seasonality in the searches? Is there a pattern?
.
.
..
...
..
.
.
Solution: Adding a grid to spot seasonality
Ok, so there are relatively few changes you had to make here. Just the labels and the dataset we're using. The line of code I wanted you to figure out from the documentation was this one:
ax1.grid(color='grey', linestyle='--')
This overlays a grid of dashed lines, so that we get the following look:
Notice how we can now clearly see the vertical dashed lines line up with spikes in searches for "Unemployment benefits". Many of the spikes are at year-end - in December. This clearly shows that there is seasonality in the job market. What else do we see? We see that the financial crisis in 2007/2008 caused a massive spike in unemployment. It took around 10 years (2007-2017) for the unemployment to reach the same level it had before the crisis.
Interestingly the big spike in searches for Unemployment benefits at the end of 2013 was not accompanied by a big increase in the unemployment rate. Something else must have been going on around that time.
Here's the full code for the cell:
plt.figure(figsize=(14,8), dpi=120) plt.title('Monthly Search of "Unemployment Benefits" in the U.S. vs the U/E Rate', fontsize=18) plt.yticks(fontsize=14) plt.xticks(fontsize=14, rotation=45) ax1 = plt.gca() ax2 = ax1.twinx() ax1.set_ylabel('FRED U/E Rate', color='purple', fontsize=14) ax2.set_ylabel('Search Trend', color='skyblue', fontsize=14) ax1.xaxis.set_major_locator(years) ax1.xaxis.set_major_formatter(years_fmt) ax1.xaxis.set_minor_locator(months) ax1.set_ylim(bottom=3, top=10.5) ax1.set_xlim([df_unemployment.MONTH.min(), df_unemployment.MONTH.max()]) # Show the grid lines as dark grey lines ax1.grid(color='grey', linestyle='--') # Change the dataset used ax1.plot(df_unemployment.MONTH, df_unemployment.UNRATE, color='purple', linewidth=3, linestyle='--') ax2.plot(df_unemployment.MONTH, df_unemployment.UE_BENEFITS_WEB_SEARCH, color='skyblue', linewidth=3) plt.show()
The search volume moves around quite a bit - month on month. Perhaps we can smooth out the search volumes to get a slightly different picture (pun intended!).
Calculate the 3-month or 6-month rolling average for the web searches. Plot the 6-month rolling average search data against the actual unemployment. What do you see? Which line moves first?
Hint: Take a look at our prior lesson on Programming Languages where we smoothed out time-series data.
.
.
..
...
..
.
.
Solution: Rolling Average
You can create a rolling average using .rolling()
and .mean()
functions together.
roll_df = df_unemployment[['UE_BENEFITS_WEB_SEARCH', 'UNRATE']].rolling(window=6).mean()
Your plot should look something like this:
What is this telling us? We see that searches for "Unemployment Benefits" happen before the actual official unemployment rate goes up. Similarly, the search popularity for the term goes down before the unemployment rate decreases. In other words, these searches seem to act as a leading economic indicator for the unemployment rate (which is a lagging indicator).
Here's the full code for the cell:
plt.figure(figsize=(14,8), dpi=120) plt.title('Rolling Monthly US "Unemployment Benefits" Web Searches vs UNRATE', fontsize=18) plt.yticks(fontsize=14) plt.xticks(fontsize=14, rotation=45) ax1 = plt.gca() ax2 = ax1.twinx() ax1.xaxis.set_major_locator(years) ax1.xaxis.set_major_formatter(years_fmt) ax1.xaxis.set_minor_locator(months) ax1.set_ylabel('FRED U/E Rate', color='purple', fontsize=16) ax2.set_ylabel('Search Trend', color='skyblue', fontsize=16) ax1.set_ylim(bottom=3, top=10.5) ax1.set_xlim([df_unemployment.MONTH[0], df_unemployment.MONTH.max()]) # Calculate the rolling average over a 6 month window roll_df = df_unemployment[['UE_BENEFITS_WEB_SEARCH', 'UNRATE']].rolling(window=6).mean() ax1.plot(df_unemployment.MONTH, roll_df.UNRATE, 'purple', linewidth=3, linestyle='-.') ax2.plot(df_unemployment.MONTH, roll_df.UE_BENEFITS_WEB_SEARCH, 'skyblue', linewidth=3) plt.show()