Introduction
Data visualization is an important step in any data analysis process. In time series analysis, data visualization can help guide the testing, diagnostics, and model determination. This tutorial provides an overview of how to plot time series data using the U.S. Wholesale Price Index data as an example.
In this tutorial, we will learn how to:
- Create a quick time series plot using formula strings.
- Customize time series plots.
- How to add a plot of first differences to your original graph.
Step 1: Load the TSMT library and WPI data
For this example, we will use the U.S. WPI data which begins in 1960 and is collected each quarter.
// Load tsmt library library tsmt; // Load wpi.gdat data // from the GAUSS // tsmt/examples directory fname = getGAUSSHome("pkgs/tsmt/examples/wpi.gdat"); y = loadd(fname);
loadd
function to load the data from a stored dataset. You can learn more about loading data in the loading data tutorial series. Step 2: Create a quick time series plot with plotXY
We can learn more about the way the WPI has evolved across time by plotting the natural log of the U.S. WPI index on the y-axis against the quarter it was collected.
The plotXY
procedure is useful function for this because:
- It automatically creates a time series plot when date variables are detected.
- Is compatible with formula strings for quick, intuitive plotting.
// Plot `ln_wpi` against `Date // using plotXY formula string plotXY(y, "ln_wpi~Date");
The above code will produce a plot similar to the plot below.
Step 3: Customize the plot
While this graph does show us the data, we would like to give the graph a title, label the Y-axis and change the format of the X-axis tick labels. To accomplish these goals, we will create a plotControl
structure to hold our plot settings and use the plotSet
functions to modify the settings held in the plotControl
structure.
// Declare 'myPlot' to be a plotControl structure // and fill with default settings for XY plots struct plotControl myPlot; myplot = plotGetDefaults("XY"); // Set the title text, font and font-size plotSetTitle(&myPlot, "U.S. Wholesale Price Index ", "Helvetica Neue", 18); // Set the Y-axis text, font and font-size plotSetYLabel(&myPlot, "ln(wpi)", "Helvetica Neue", 14); // Set the X-tick label format to 4 digit year plotSetXTicLabel(&myPlot, "YYYY"); // Place an X-tick every 10 years (40 quarters), // starting with January of 1960 plotSetXTicInterval(&myPlot, 40); // Draw the plot, using the settings // in 'myPlot' plotXY(myPlot, y, "ln_wpi~Date");
In this plot, we see that the WPI series shows a fairly stable increase over time. This suggests that the data may not be mean reverting and should be subjected to unit root testing.
Step 4: Add a graph of the first differences
Visualizing the first differences alongside the level data will provide some insight into the potential benefits of transforming our data.
We will difference the data with the tsdiff
function from the GAUSS time series package, TSMT.
// Calculate the first differences wpi_diff = tsdiff(y[. ,"ln_wpi"], 1);
We can use the plotControl
structure created above and just modify the Y-label.
// Change the Y-label text, but keep the // previously set font and font-size plotSetYlabel(&myPlot, "First Differences ln(wpi)"); // Plot the first differences // this time using data matrices // trimming one observation off date plotXY(myPlot, trimr(y[., "Date"], 1, 0), wpi_diff);
The added plot of the first differences shows that the new series is centered around zero. This suggests that the first differences are mean reverting and may be a more appropriate series for estimation.
Conclusion
Time series graphs give us valuable insight into the behavior of time series data. Visualizing time series data provides a preliminary tool for detecting if your data:
- Is mean-reverting.
- Demonstrates structural breaks.
- Has a time trend, and other important time series characteristics.
Now that you have graphed your time series data you can make more informed decisions about what steps to take next.
For your convenience, the entire code is available here.
Extra Credit: Combine the time series and first differences plot
Below is the full code to create the graph at the top of this tutorial which combines the original time series and the first differences into a stacked series.
/* ** Extra Credit: Combining the plots */ // Load library to make 'tsmt' available library tsmt; // Load wpi.gdat data // from the GAUSS tsmt/examples directory fname = getGAUSSHome("pkgs/tsmt/examples/wpi.gdat"); y = loadd(fname); // Calculate the first differences wpi_diff = tsdiff(y[. ,"ln_wpi"], 1); // Open new window for plot plotOpenWindow(); // Declare 'myPlot' to be a plotControl structure // and fill with default settings for XY plots struct plotControl myPlot; myplot = plotGetDefaults("XY"); // Set up common plot features // Set the X-tick label format to 4 digit year plotSetXTicLabel(&myPlot, "YYYY"); // Place an X-tick every 5 years (20 quarters), // starting with January of 1960 plotSetXTicInterval(&myPlot, 40); // Set the axes lines to be black and 1 pixel thick plotSetAxesPen(&myPlot, 1, "black"); // Set the title text, font and font-size plotSetTitle(&myPlot, "U.S. Wholesale Price Index ", "Helvetica Neue", 12); /******************** Plot level WPI *********************/ // Set the Y-axis text, font and font-size plotSetYLabel(&myPlot, "ln(wpi)", "Helvetica Neue", 10); // Divide the plot canvas into a 2x1 // grid and place the next plot // into the first cell plotLayout(2, 1, 1); // Draw the plot, using the settings // in 'myPlot' plotXY(myPlot, y, "ln_wpi~Date"); /******************** Plot differences WPI *********************/ // Set the Y-axis text, font and font-size plotSetYLabel(&myPlot, " First Differences ln(wpi)", "Helvetica Neue", 10); // Turn off the title for the second plot plotSetTitle(&myPlot, ""); // Divide the plot canvas into a 2x1 // grid and place the next plot // into the second cell plotLayout(2, 1, 2); // Plot the first differences // this time using data matrices // trimming one observation off date plotXY(myPlot, trimr(y[., "Date"], 1, 0), wpi_diff); // Restore the plot layout to the // default setting for individual plots plotClearLayout();