What does differencing mean? Differencing helps to stabilize the mean. The difference time series is the change between each observation in the original time series. The first difference is equal to y_t minus y_t minus 1. So if take today's value of Apple stock price and yesterday's stock price and I take that difference, I get y sub t. If I do it for y_t minus 1 and y_t minus 2 and take that difference, I get y_t minus 1 and so on and so forth. I'm just taking the difference of consecutive prices. If that doesn't work, sometimes you'll have to do a second-order differencing, and that's exactly the same concept where you take the difference of the differences, if that makes sense. Here's the second difference of y_t, and I took that first difference of y_t minus 1 and took the difference of those and I get y_t minus 1. Generally speaking, you will not need to go beyond a second-order difference. I should note, sometimes you'll see log differences. That's another technique which gets a percent change. But let's focus on this and master this skill first. Another type of differencing that you might is seasonal differencing. I showed here for completeness. The seasonal difference of the time series takes into account any seasonal changes in the data. For example, a Ski company might have increased sales in the fall just before the ski season or a bathing suit company might have increased sales just before the summer, in the spring for people getting ready to go to the beach. Those are some seasonal differences. Like first-order differences or regular differencing, this is the same thing except you take the difference by the length of the seasonal periods. So if you have monthly data, you might take the difference of today's sales and the sales from 12 months ago. This helps to remove any seasonal patterns in the data, and it's also sometimes called seasonal differencing. Let's look at an illustration. Here's some data of beer sales in Australia. Here are the dates. You can see, here's the first difference. The 17 is really the difference between these two numbers here, 4.96 minus 4.79. Then to get the 68 is this 5.64 minus 4.96. To get 34, it's these two numbers et cetera. That's how you get the first difference. The second difference is not really shown here on the graph but it would be similar by taking the difference of these two numbers and then these two numbers and then these two numbers et cetera. Here's some seasonal differences. Here, we can see that the seasonal differences is $5.17, right here in January of 2018 minus 4.79, which is January of 2017. There's the seasonal difference there, and then here's the next seasonal difference, the difference between those two numbers et cetera. Then here is the difference of the differences. Now, let's go back to that Apple stock's price series. Since we know that when we looked at the data in its raw form, it was not stationary. We can do a first difference and then perform another stationery test or an ADF test on that data series. This is how we would take first-order differencing. You could do this in a spreadsheet. Here are the Apple prices, I've got about six price days there. I notice that there's the first difference there, there's the second difference there et cetera. I also note there's a long weekend here, from the third to the fourth, and I mentioned before the time series that it should occur at regular intervals. In this case, it's business days, so don't let that little gap of two to three days distract you. Here's a chart of the original time series, and notice the upward trend. Then here's the chart of just the differences. We are just charting this column of data here and we get this more flat first difference in data, that looks a little bit better. Then we're going to perform our ADF test. Here we are back in the RStudio environment, and we're going to do the stationary tests on first difference data. The command in difference data is just simply diff open paren and then your time-series name; in this case Apple, AAPL. I'm going to put it in this variable AAPL.diff. Let's run this one line of code. Just as a refresher, you can see this is the Apple rod closing prices: 156, 141. That first difference is about a difference of say, 15 and the next one is the difference of about six. So now, we can look at the difference data, and you can see a difference of a drop of price around 15.6 and then an increase of 6.02. So you should look at these numbers and eyeball them just to make sure you're confident with what's going on. Now, I have this difference dataset. This is an R thing here, this na.omit, but if you look at that dataset, since I did first differencing, that first number is not available obviously since you can't subtract the January 2nd price from the first prices. So there's no value here and it's just na, so we want to just truncate that out. That's what this na.omit does. So when you run that code. Let's close this dataset. Run this code. Now, if we look at that dataset, you'll see it just knocked out that first value. Now, we can do the augmented Dickey-Fuller test. It's the same command except now we're doing the tests on this Apple stock price data that's been differenced. We're going to put the result of that test in this variable diff.adf. This is the standard procedure where you have to list out the value. Here, we can see the value of the P value, it's 0.01 less than 0.05, so now we know we have some stationary data. Also, just for reference, here is the code to create the two plots that were on the PowerPoint slides. Let's run this real quick. Here's the stock price data for the first quarter of 2019 and here's the difference data. You can see here that it looks more stationary, whereas this has a trend, et cetera. Just a last note on the graph, let me just clear this, delete these graphs. If you don't run this par function, we're not going to go through this, but it's something you can look up. If you just use these plot commands one at a time, there's the first one, there's the second one, and this par command just helps you format your graphical windows so you can put two graphs on one frame. To summarize what we've done with the Apple price series data. First, we learned that in its raw form, the data was not stationary, but then we took first differences and we found that the time series of the first differences was stationary. The way we talk about this, the vocabulary is that it has an order of integration of one. Sometimes it's denoted as I(1). If we had to do a second-order difference, it would be I(2). So this order of integration is used to identify how many times we had to difference the data. In practice, you don't have to do more than two, generally speaking. When the data is stationary at zero, it has an order of zero denoted I(0), and then if it's stationary at the first order, the notation is here, I(1). So far we've learned how to identify or distinguish between non-stationary data and stationary data, and also we should have at least a conceptual understanding of strong and weak sense stationarity. I wouldn't worry too much about strict stationarity. In practice, this is a more of a theoretical construct and it's more important when developing time series theory, but in practice, weak-sense stationary is enough. We also should have an understanding of why stationary is important and that's basically so that it makes prediction a little more accurate if we know the statistical properties of the time series, we know how to test for using the augmented Dickey-Fuller test, and we know when to use first and second-order differencing. To recap the procedure, I would first start with plotting the data and see if it looks stationary. Just give it a sanity check by eyeballing it. We can actually perform stationarity test. I focused on the augmented Dickey-Fuller test, and I will use that for now. If the time series in its raw form is stationary, you just say it is integrated at order zero otherwise you have to do the differencing. Plot the time series on the first differencing, and again see if it looks okay. If it doesn't, try second-order differencing, and again make sure that your intuition by looking at the graphs is correct by doing the augmented Dickey-Fuller test. That wraps up stationarity and how to difference your data to get stationarity.