So let's look at a real-world example. Here's some data of gold prices. I'm going to be using that first column. But this is typical time series data. We see the date here on the left. This particular dataset has AM PM data etc. I have actually uploaded a spreadsheet and I'm only using that first column to simplify things. So let's switch over to R. Here we are in the RStudio environment. I've already loaded the code here from the spreadsheet, it's called Gold and I've taken the liberty of changing the dates to periods, period 1, 2, 3 etc. The bigger the number, the more recent the event or the smaller the number the older the event. The reason I did that is for the sake of this course. In any programming language be it R, Python, even Excel, the manipulation of dates get a little tricky. So I just simplified it by using periods here. But just keep in mind that they are daily price data. To import a dataset into R, you will navigate to the directory where that file sits, here it's gold.csv. If you "Click" on it, you'll see "Import" dataset and that'll import the data into your R environment which I've already done and you can see it in my environment section here. It's got 13,001 observations of two variables. The two variables being the period column and the second variable being the US dollar amounts. Here's the code. I'm going to load the forecast library which has forecasting commands. I'm going to load this ggplot2 which has had RMA graphics along with this fortify. Let's load these libraries. Recall that if I just want to take a peek at the data, I can use this head function which just shows me the top few lines of the data as opposed to all 13,001 of them. So let's run that. Then you can see here the period 1, 2, 3, 4, 5, 6 and then the values 35.2, 35.2, etc. A couple of good commands to keep in your back pocket is the structure commands. So if you look at that, it'll tell you that it's a DataFrame in this example in the dimensions. There's 13,001 rows and there's two columns. But we're working with time series function. So one thing we have to do is make a time series out of that. So you just take this column vector and put it into a time series. So goldts is a time series construction. Now, I can use the Autoplot command and let's take a look. So there's a a plot of the time series data. Let me make it a little bigger. We can see a lot of things from here. We can see that it's spiking up and then it's dropping down and then somewhere in here, it just keeps on increasing. As I recall that happens in the 2000s. So let's try some different types of moving averages. The command for a moving average is really simple. There it is. To create a time series of moving averages, use this MA command and what do you put them in MA? You put in two arguments, the first argument is the time series data which is called Ts and then how many lags you want to use in your time series data? So in this case, I'm using 10 lags. I create this column vector here and I put it into this dataset name called goldts10 lags for gold time series 10 lags, and then I'm going to plot it. If you do just autoplot, that's enough. But here I wanted to add a little title, put some labels on there. So that's what these other three commands are doing. So let's run these three lines of code. That looks a little bit smoother, it doesn't have as much many squiggly lines. I'm going to do it again for let's say 100 lags. That's just 200. When you get this code, feel free to play around with the number of lags and you can have a better understanding of what's going on. There it looks a lot more smoother. Now we can see various long term or short term patterns. Here's one with 500 lags, so it's using 500 daily prices. Here, we see a much, more smoothing curve and that's one of the things about moving averages, is that it's smooths out the data. On this last point, slide in PowerPoint, I have overlaid all the data for a moving average of two, five, and 10 lags. As you can see, the dots are the actual data points. We can see with a lag of two, it follows the data a little bit more closely but not completely. You can see out here in 2012 or so, the data points are way above this blue line. If I increase the number of lags to let's say the red line which has 10 lags, you can see that the curve is a lot more smoother. So we can look at more longer term trends. Generally, the trends are smoother with a bigger K. Obviously, if we use the whole dataset, we would get the total average. That concludes this session on moving averages.