import statsmodels.api as smfrom scipy.stats import pearsonrfrom statsmodels.graphics.tsaplots import plot_acf
The first thing to do in any data analysis task is to plot the data. Graphs enable many features of the data to be visualised, including patterns, unusual observations, changes over time, and relationships between variables. The features that are seen in plots of the data must then be incorporated, as much as possible, into the forecasting methods to be used. Just as the type of data determines what forecasting method to use, it also determines what graphs are appropriate. But before we produce graphs, we need to set up our time series in Python.
2.1DataFrame objects
A time series consists of a sequence of numerical observations along with information about when those numbers were recorded. Sometimes a primary observation is accompanied by related data known as covariates or exogenous variables. This data is commonly stored in a two-dimensional tabular data structure called a dataframe. In Python, tables are often created as pandas DataFrame objects, or more recently, as Polars DataFrame objects. If the dataframe takes too much time or memory to process, distributed dataframe implementations such as Dask, Spark, or Ray can also be used. We will focus on the pandas DataFrame in this book.
Index and Columns
A pandas DataFrame can be thought of as an array of values arranged along two axes: the index which identifies rows, and the columns. For example, suppose you have annual observations for some recent years:
Year
Observation
0
2015
123
1
2016
39
2
2017
78
3
2018
52
4
2019
110
We can store the information as a DataFrame using the pd.DataFrame() constructor:
By inspecting the dataframe’s properties, we can see that the index runs from 0 up to (but not including) 5, and the columns include Year and Observation:
The index can be replaced by one of the columns using the .set_index() method. For example,
year_df = df.set_index("Year")year_df
Observation
Year
2015
123
2016
39
2017
78
2018
52
2019
110
Timestamps and Periods
Time series observations may be associated with instants in time or spans of time. pandas supports these concepts through pd.Timestamp and pd.Period classes, respectively:
pandas provides many utilities for converting between text strings and dedicated timestamp or period representations. Internally, timestamps are stored with nanosecond precision by default, but for convenience a partial specification is sufficient to refer to the earliest instant matching the input. For periods, the same logic is used to parse the start of the time span, and the duration is inferred from the precision provided unless the frequency is indicated via the freq= argument:
The .to_period() and .to_timestamp() methods allow conversions between timestamp and period. The .to_period() method will infer the period duration unless the freq= argument is specified. For example,
Index(['01/01/2020', '01/02/2020', '01/03/2020'], dtype='object')
Index(['2020 Jan ~ 00:00', '2020 Jan ~ 08:00', '2020 Jan ~ 16:00',
'2020 Jan ~ 00:00'],
dtype='object')
pandas automatically converts between Index and Series types as needed when these sequences are used as the data or index in a DataFrame. In Series form, date-time related methods and attributes are nested under the .dt accessor. For example:
In this book, we will usually store observation times as timestamps. In some cases we will use integer types such as the year or number of days from some reference date. Usually timing data will be stored in a column, often with the name ds as in “date stamp”, but occasionally we will use set_index() to access certain pandas functions.
Key variables
A DataFrame allows multiple time series to be stored in a single object. Suppose you are interested in a dataset containing the fastest running times for women’s and men’s track races at the Olympics, from 100m to 10000m:
This DataFrame contains 312 rows and 4 columns. The data is recorded every four years, and there are 14 separate time series in this DataFrame. A preview of the first 8 observations is also shown, in which we can see a missing value occurs in 1916. This is because the Olympics were not held during World War I.
The 14 time series are uniquely identified by the Length and Sex key variables. The .unique() method can be used to show the categories of a variable as an array:
print(olympic_running["Sex"].unique())
['men' 'women']
The .drop_duplicates() method can be used to show distinct values in a Series or distinct combinations of values in multiple Series:
Sex Length
0 men 100
31 women 100
54 men 200
84 women 200
102 men 400
133 women 400
147 men 800
178 women 800
201 men 1500
232 women 1500
244 men 5000
271 women 5000
277 men 10000
304 women 10000
Working with time series dataframes
We can manipulate DataFrame objects using pandas methods such as .assign(), .rename(), .drop(), .groupby(), and .agg(), along with slicing operations such as [] and .loc[]. To illustrate these, we will use the PBS dataset, containing sales data on pharmaceutical products in Australia.
This contains monthly data on Medicare Australia prescription data from July 1991 to June 2008. These are classified according to various concession types, and Anatomical Therapeutic Chemical (ATC) indexes. For this example, we are interested in the Cost time series (total cost of scripts in Australian dollars).
We have already used [] to select a subset of the available columns. We can use .loc[] to extract rows pertaining to A10 scripts.
a10 = pbs.loc[pbs["ATC2"] =="A10"]a10
Month
Concession
Type
ATC1
ATC2
Scripts
Cost
1524
1991-07-01
Concessional
Co-payments
A
A10
89733
2092878
1525
1991-08-01
Concessional
Co-payments
A
A10
77101
1795733
1526
1991-09-01
Concessional
Co-payments
A
A10
76255
1777231
1527
1991-10-01
Concessional
Co-payments
A
A10
78681
1848507
...
...
...
...
...
...
...
52340
2008-03-01
General
Safety net
A
A10
1119
51773
52341
2008-04-01
General
Safety net
A
A10
721
36289
52342
2008-05-01
General
Safety net
A
A10
1947
101233
52343
2008-06-01
General
Safety net
A
A10
4331
193179
Next we can simplify the resulting object by using .drop() to remove columns not needed in subsequent analysis.
Another useful method is .agg() which allows us to aggregate data either across all rows or, when used with .groupby(), within groups based on one or more columns in the DataFrame. For example, we may wish to compute total cost per month regardless of the Concession or Type keys.
The new column, which we named TotalC using the .rename() method, represents the sum of all Cost values for each month.
We can create or update columns using the assign() method. Here we remove the rename operation and change the units of Cost from dollars to millions of dollars:
In this operation, we also demonstrate the use of an unnamed lambda function. These are often used with .assign(), .loc[], and elsewhere in method chains to access column data even when the input dataframe may not be bound to a variable name.
Working with csv files
Real world data is found in diverse storage formats such as spreadsheets, databases, and various text-based and binary file types. Almost all the data in this book is stored in csv files. The first step in working with time series data is to read the data into a DataFrame and identify the time column and any key variables.
For example, suppose we have the following quarterly data stored in a csv file (only the first 10 rows are shown). This data set provides information on the size of the prison population in Australia, disaggregated by state, gender, legal status and indigenous status. (Here, ATSI stands for Aboriginal or Torres Strait Islander.)
Date
State
Gender
Legal
Indigenous
Count
0
2005-03-01
ACT
Female
Remanded
ATSI
0
1
2005-03-01
ACT
Female
Remanded
Non-ATSI
2
2
2005-03-01
ACT
Female
Sentenced
ATSI
0
3
2005-03-01
ACT
Female
Sentenced
Non-ATSI
5
4
2005-03-01
ACT
Male
Remanded
ATSI
7
5
2005-03-01
ACT
Male
Remanded
Non-ATSI
58
6
2005-03-01
ACT
Male
Sentenced
ATSI
5
7
2005-03-01
ACT
Male
Sentenced
Non-ATSI
101
8
2005-03-01
NSW
Female
Remanded
ATSI
51
9
2005-03-01
NSW
Female
Remanded
Non-ATSI
131
We can read (or load) the file into a DataFrame by calling the pd.read_csv() function. By setting parse_dates=["Date"], we ensure the Date column gives a timestamp associated with each observation. In this case the data is quarterly, and the Date column should be interpreted as the first date of the last month of the quarter.
This dataframe contains 64 unique time series corresponding to the unique combinations of the 8 states, 2 genders, 2 legal statuses and 2 indigenous statuses. Each of these series is 48 observations in length, from 2005 Q1 to 2016 Q4.
For a time series dataframe to be valid for forecasting or other analysis, each distinct time series must be indicated by a unique combination of identifying key variables; in this case, these are the State, Gender, Legal and Indigenous columns.
We can also write dataframes into csv files using the .to_csv() method. For example, here we save the total_cost_df dataframe from the previous section.
Make sure to specify the correct path to save the file in your own working directory if you run this code. If unsure, the current working directory can be checked using os.getcwd().
The seasonal period
Seasonalities are particularly important for time series analysis. Some graphics and some models rely on the repeated seasonal period of the data. The seasonal period is the number of observations before a seasonal pattern repeats. Some algorithms can detect this automatically using a time index or column.
Some common periods for different time intervals are shown in the table below:
Minute
Hour
Day
Week
Year
Data
Quarters
4
Months
12
Weeks
52
Days
7
365.25
Hours
24
168
8766
Minutes
60
1440
10080
525960
Seconds
60
3600
86400
604800
31557600
For quarterly, monthly and weekly data, there is only one seasonal period — the number of observations within each year. Let it be noted that actually there are not 52 weeks in a year, but 365.25/7 = 52.18 on average, allowing for a leap year every fourth year. Approximating seasonal periods to integers can be useful as many seasonal terms in models only support integer seasonal periods.
If the data is observed more than once per week, then there is often more than one seasonal pattern in the data. For example, data with daily observations might have weekly (period =7) or annual (period =365.25) seasonal patterns. Similarly, data that are observed every minute might have hourly (period =60), daily (period =24\times60=1440), weekly (period =24\times60\times7=10080) and annual seasonality (period =24\times60\times365.25=525960).
More complicated (and unusual) seasonal patterns can be analysed using specialised models from the statsforecast package, such as MSTL, MFLES, and TBATS.
2.2 Time plots
For time series data, the obvious graph to start with is a time plot. That is, the observations are plotted against the time of observation, with consecutive observations joined by straight lines. Figure 2.1 shows the weekly economy passenger load on Ansett airlines between Australia’s two largest cities (Melbourne and Sydney).
By arranging a dataframe with column names matching the defaults anticipated by utilsforecast and related packages, we can simplify the function call. For example,
There was a period in 1989 when no passengers were carried — this was due to an industrial dispute.
There was a period of reduced load in 1992. This was due to a trial in which some economy class seats were replaced by business class seats.
A large increase in passenger load occurred in the second half of 1991.
There are some large dips in load around the start of each year. These are due to holiday effects.
There is a long-term fluctuation in the level of the series which increases during 1987, decreases in 1989, and increases again through 1990 and 1991.
Any model will need to take all these features into account in order to effectively forecast the passenger load into the future.
A simpler time series is shown in Figure 2.2, using the total_cost_df data saved earlier. In this case, given that we aggregated the data before, we have just one unique time series, and we don’t have a unique_id column so we need to create one.
Figure 2.2: Monthly sales of antidiabetic drugs in Australia.
Here, there is a clear and increasing trend. There is also a strong seasonal pattern that increases in size as the level of the series increases. The sudden drop at the start of each year is caused by a government subsidisation scheme that makes it cost-effective for patients to stockpile drugs at the end of the calendar year. Any forecasts of this series would need to capture the seasonal pattern, and the fact that the trend is changing slowly.
2.3 Time series patterns
In describing these time series, we often use terms like “trend” and “seasonal” which require further clarification.
Trend
A trend exists when there is a long-term increase or decrease in the data. It does not have to be linear. Sometimes we will refer to a trend as “changing direction”, when it might go from an increasing trend to a decreasing trend. There is a trend in the antidiabetic drug sales data shown in Figure 2.2.
Seasonal
A seasonal pattern occurs when a time series is affected by seasonal factors such as the time of the year, the day of the week or the hour of the day. Seasonality is always of a fixed and known period. The monthly sales of antidiabetic drugs (Figure 2.2) shows seasonality which is induced partly by the change in the cost of the drugs at the end of the calendar year. (Note that one series can have more than one seasonal pattern.)
Cyclic
A cycle occurs when the data exhibit rises and falls that are not of a fixed frequency. These fluctuations are usually due to economic conditions, and are often related to the “business cycle”. The duration of these fluctuations is usually at least 2 years.
Many people confuse cyclic behaviour with seasonal behaviour, but they are really quite different. If the fluctuations are not of a fixed frequency then they are cyclic; if the frequency is unchanging and associated with some aspect of the calendar, then the pattern is seasonal. In general, the average length of cycles is longer than the length of a seasonal pattern, and the magnitudes of cycles tend to be more variable than the magnitudes of seasonal patterns.
Many time series include trend, cycles and seasonality. When choosing a forecasting method, we will first need to identify the time series patterns in the data, and then choose a method that is able to capture the patterns properly.
The examples in Figure 2.3 show different combinations of these components.
Figure 2.3: Four examples of time series showing different patterns.
The monthly housing sales (top left) show strong seasonality within each year, as well as some strong cyclic behaviour with a period of about 6–10 years. There is no apparent trend in the data over this period.
The US treasury bill contracts (top right) show results from the Chicago market for 100 consecutive trading days in 1981. Here there is no seasonality, but an obvious downward trend. Possibly, if we had a much longer series, we would see that this downward trend is actually part of a long cycle, but when viewed over only 100 days it appears to be a trend.
The Australian quarterly electricity production (bottom left) shows a strong increasing trend, with strong seasonality. There is no evidence of any cyclic behaviour here.
The daily change in the Google closing stock price (bottom right) has no trend, seasonality or cyclic behaviour. There are random fluctuations which do not appear to be predictable, and no strong patterns that would help with developing a forecasting model.
2.4 Seasonal plots
A seasonal plot is similar to a time plot except that the data are plotted against the individual “seasons” in which the data were observed. An example is given in Figure 2.4 showing the antidiabetic drug sales.
Figure 2.4: Seasonal plot of monthly antidiabetic drug sales in Australia.
This is the same data shown earlier, but now the data from each year is overlapped. A seasonal plot shows the underlying seasonal pattern more clearly and helps identify years when the pattern changes.
There is a large jump in sales in January each year. These are probably sales in late December as customers stockpile before the end of the calendar year, but the sales are not registered with the government until a week or two later. The graph also shows an unusually small number of sales in March 2008 (most other years show an increase between February and March). The small number of sales in June 2008 is probably due to incomplete counting of sales when the data were collected.
Multiple seasonal periods
Where the data has more than one seasonal pattern, we can group the data according to different seasonalities and plot. The vic_elec data contains half-hourly (freq="30min") electricity demand for the state of Victoria, Australia. We can plot the daily pattern, weekly pattern or yearly pattern by grouping according to date, week and year as shown in Figures 2.5–2.7.
Figure 2.8: Seasonal subseries plot of monthly antidiabetic drug sales in Australia.
The blue horizontal lines indicate the means for each month. This form of plot enables the underlying seasonal pattern to be seen clearly, and also shows the changes in seasonality over time. It is especially useful in identifying changes within particular seasons. In this example, the plot is not particularly revealing; but in some cases, this is the most useful way of viewing seasonal changes over time.
Example: Australian holiday tourism
Australian quarterly vacation data provides an interesting example of how these plots can reveal information. First we need to extract the relevant data from the tourism dataframe. All the usual pandas wrangling functions apply. To get the total visitor nights spent on Holiday by State for each quarter (i.e., ignoring Regions) we can use the following code.
Figure 2.9: Time plots of Australian domestic holidays by state.
To see the timing of the seasonal peaks in each state, we can use a season plot. Figure 2.10 makes it clear that the southern states of Australia (Tasmania, Victoria and South Australia) have the strongest tourism in Q1 (their summer), while the northern states (Queensland and the Northern Territory) have the strongest tourism in Q3 (their dry season).
Figure 2.11: Seasonal subseries plot of Australian domestic holidays by state.
The corresponding subseries plots are shown in Figure 2.11. This figure makes it evident that Western Australian tourism has jumped markedly in recent years, while Victorian tourism has increased in Q1 and Q4 but not in the middle of the year.
2.6 Scatterplots
The graphs discussed so far are useful for visualising individual time series. It is also useful to explore relationships between time series.
Figures 2.12 and 2.13 show two time series: half-hourly electricity demand (in Gigawatts) and temperature (in degrees Celsius), for 2014 in Victoria, Australia. The temperatures are for Melbourne, the largest city in Victoria, while the demand values are for the entire state.
Figure 2.14: Half-hourly electricity demand plotted against temperature for 2014 in Victoria, Australia.
This scatterplot helps us to visualise the relationship between the variables. It is clear that high demand occurs when temperatures are high due to the effect of air-conditioning. But there is also a heating effect, where demand increases for low temperatures.
Correlation
It is common to compute correlation coefficients to measure the strength of the linear relationship between two variables. The correlation between variables x and y is given by
r = \frac{\sum (x_{t} - \bar{x})(y_{t}-\bar{y})}{\sqrt{\sum (x_{t} - \bar{x})^2} \sqrt{\sum (y_{t} - \bar{y})^2}}.
The value of r always lies between -1 and 1 with negative values indicating a negative relationship and positive values indicating a positive relationship. The graphs in Figure 2.15 show examples of data sets with varying levels of correlation.
Figure 2.15: Examples of data sets with different levels of correlation.
The correlation coefficient only measures the strength of the linear relationship between two variables, and can sometimes be misleading. For example, the correlation for the electricity demand and temperature data shown in Figure 2.14 is 0.28, but the non-linear relationship is stronger than that.
Figure 2.16: Each of these plots has a correlation coefficient of 0.82. Data from Anscombe (1973).
The plots in Figure 2.16 all have correlation coefficients of 0.82, but they have very different relationships. This shows how important it is to look at the plots of the data and not simply rely on correlation values.
Scatterplot matrices
When there are several potential predictor variables, it is useful to plot each variable against each other variable. Consider the eight time series shown in Figure 2.17, showing quarterly visitor numbers across states and territories of Australia.
Figure 2.17: Quarterly visitor nights for the states and territories of Australia.
To see the relationships between these eight time series, we can plot each time series against the others. These plots can be arranged in a scatterplot matrix, as shown in Figure 2.18.
Figure 2.18: A scatterplot matrix of the quarterly visitor nights in the states and territories of Australia.
For each panel, the variable on the vertical axis is given by the variable name in that row, and the variable on the horizontal axis is given by the variable name in that column. There are many options available to produce different plots within each panel. In the default version, the correlations are shown in the upper right half of the plot, while the scatterplots are shown in the lower half. On the diagonal are shown density plots.
The value of the scatterplot matrix is that it enables a quick view of the relationships between all pairs of variables. In this example, mostly positive relationships are revealed, with the strongest relationships being between the neighbouring states located in the south and south-east coast of Australia, namely, New South Wales, Victoria and South Australia. Some negative relationships are also revealed between the Northern Territory and other regions. The Northern Territory is located in the north of Australia, famous for its outback desert landscapes visited mostly in winter. Hence, the peak visitation in the Northern Territory is in the July (winter) quarter in contrast to January (summer) quarter for the rest of the regions.
2.7 Lag plots
Figure 2.19 displays scatterplots of quarterly Australian beer production (introduced in Figure 1.1), where the horizontal axis shows lagged values of the time series. Each graph shows y_{t} plotted against y_{t-k} for different values of k.
Figure 2.19: Lagged scatterplots for quarterly beer production.
Here the colours indicate the quarter of the variable on the vertical axis. The relationship is strongly positive at lags 4 and 8, reflecting the strong seasonality in the data. The negative relationship seen for lags 2 and 6 occurs because peaks (in Q4) are plotted against troughs (in Q2).
2.8 Autocorrelation
Just as correlation measures the extent of a linear relationship between two variables, autocorrelation measures the linear relationship between lagged values of a time series.
There are several autocorrelation coefficients, corresponding to each panel in the lag plot. For example, r_{1} measures the relationship between y_{t} and y_{t-1}, r_{2} measures the relationship between y_{t} and y_{t-2}, and so on.
The value of r_{k} can be written as
r_{k} = \frac{\sum\limits_{t=k+1}^T (y_{t}-\bar{y})(y_{t-k}-\bar{y})}
{\sum\limits_{t=1}^T (y_{t}-\bar{y})^2},
where T is the length of the time series. The autocorrelation coefficients make up the autocorrelation function or ACF.
The autocorrelation coefficients for the beer production data can be computed using the acf() function.
The values in the ACF column are r_1,\dots,r_9, corresponding to the nine scatterplots in Figure 2.19. We usually plot the ACF to see how the correlations change with the lag k. The plot is sometimes known as a correlogram.
fig, ax = plt.subplots(figsize=(8, 3))plot_acf(beer["y"], lags=16, ax=ax, zero=False, bartlett_confint=False, auto_ylims=True)ax.set( title="Autocorrelation Function for Beer", xlabel="lag [1Q]", ylabel="acf",)fig.show()
Figure 2.20: Autocorrelation function for quarterly beer production.
r_{4} is higher than for the other lags. This is due to the seasonal pattern in the data: the peaks tend to be four quarters apart and the troughs tend to be four quarters apart.
r_{2} is more negative than for the other lags because troughs tend to be two quarters behind peaks.
The gray shaded regions indicate whether the correlations are significantly different from zero (as explained in Section 2.9).
Trend and seasonality in ACF plots
When data have a trend, the autocorrelations for small lags tend to be large and positive because observations nearby in time are also nearby in value. So the ACF of a trended time series tends to have positive values that slowly decrease as the lags increase.
When data are seasonal, the autocorrelations will be larger for the seasonal lags (at multiples of the seasonal period) than for other lags.
When data are both trended and seasonal, you see a combination of these effects. The total_cost_df data plotted in Figure 2.2 shows both trend and seasonality. Its ACF is shown in Figure 2.21. The slow decrease in the ACF as the lags increase is due to the trend, while the “scalloped” shape is due to the seasonality.
fig, ax = plt.subplots(figsize=(8, 3))plot_acf(total_cost_df["Cost"], lags=48, ax=ax, zero=False, bartlett_confint=False, auto_ylims=True)ax.set( title="Autocorrelation Function for Antidiabetic Drug Sales", xlabel="lag [1M]", ylabel="acf", ylim=(-0.3, None),)fig.show()
Figure 2.21: Autocorrelation function for monthly antidiabetic drug sales in Australia.
2.9 White noise
Time series that show no autocorrelation are called white noise. Figure 2.22 gives an example of a white noise series.
fig, ax = plt.subplots(figsize=(8, 3))plot_acf(wn["y"], lags=16, ax=ax, zero=False, bartlett_confint=False, auto_ylims=True)ax.set( title="Autocorrelation Function for White Noise", xlabel="lag [1]", ylabel="acf",)fig.show()
Figure 2.23: Autocorrelation function for the white noise series.
For white noise series, we expect each autocorrelation to be close to zero. Of course, they will not be exactly equal to zero as there is some random variation. For a white noise series, we expect 95% of the spikes in the ACF to lie within \pm 1.96/\sqrt{T} where T is the length of the time series. It is common to plot these bounds on a graph of the ACF (the gray shaded regions above). If one or more large spikes are outside these bounds, or if substantially more than 5% of spikes are outside these bounds, then the series is probably not white noise.
In this example, T=50 and so the bounds are at \pm 1.96/\sqrt{50} = 0.28. All but the first of the autocorrelation coefficients lie within these limits, and it is only just beyond the bounds, confirming that the data are white noise.
2.10 Exercises
Explore the following four time series: Bricks from aus_production, Lynx from pelt, GOOG_Close from gafa_stock, Demand from vic_elec.
Use .info() to find out about the data in each series.
What is the time interval of each series?
Use plot_series() to produce a time plot of each series.
For the last plot, modify the axis labels and title.
Use .loc[] or query() to find what days corresponded to the peak closing price for each of the four stocks in gafa_stock.
Download the file tute1.csv from the book website, open it in Excel (or some other spreadsheet application), and review its contents. You should find four columns of information. Columns B through D each contain a quarterly series, labelled Sales, AdBudget and GDP. Sales contains the quarterly sales for a small company over the period 1981–2005. AdBudget is the advertising budget and GDP is the gross domestic product. All series have been adjusted for inflation.
You can read the data into Python with the following script:
Construct time series plots of each of the three series
plot_series(tute1)
The us_total.csv contains data on the demand for natural gas in the US.
Download us_total.csv from the book website read in the csv file using pd.read_csv().
Create a dataframe from us_total with year as the index.
Plot the annual natural gas consumption by state for the New England area (comprising the states of Maine, Vermont, New Hampshire, Massachusetts, Connecticut and Rhode Island).
Download tourism.xlsx from the book website and read in it using pd.read_excel().
Create a dataframe using tourism.xlsx.
Find what combination of Region and Purpose had the maximum number of overnight trips on average.
Create a new dataframe which combines the Purposes and Regions, and just has total trips by State.
The aus_arrivals data set comprises quarterly international arrivals to Australia from Japan, New Zealand, UK and the US.
Use plot_series() to visualise the data.
Use seaborn or matplotlib to create seasonal and subseries plots to compare the differences between the arrivals from these four countries and identify any unusual observations.
Monthly Australian retail data is provided in aus_retail. Select one of the time series as follows (but choose your own seed value):
Can you spot any seasonality, cyclicity and trend? What do you learn about the series?
Use the following graphics functions: plot_series(), seasonal_decompose(), lag_plot(), plot_acf() and explore features from the following time series: “Total Private” Employed from us_employment, Bricks from aus_production, Hare from pelt, “H02” Cost from PBS, and Barrels from us_gasoline.
Can you spot any seasonality, cyclicity and trend?
What do you learn about the series?
What can you say about the seasonal patterns?
Can you identify any unusual years?
The following time plots and ACF plots correspond to four different time series. Your task is to match each time plot in the first row with one of the ACF plots in the second row.
The aus_livestock data contains the monthly total number of pigs slaughtered in Victoria, Australia, from Jul 1972 to Dec 2018. Use .loc[] to extract pig slaughters in Victoria between 1990 and 1995. Use plot_series() and plot_acf() for this data. How do they differ from white noise? If a longer period of data is used, what difference does it make to the ACF?
Use the following code to compute the daily changes in Google closing stock prices.
Do the changes in the stock prices look like white noise?
2.11 Further reading
Cleveland (1993) is a classic book on the principles of visualisation for data analysis. While it is more than 30 years old, the ideas are timeless.
Unwin (2015) is a modern introduction to graphical data analysis using R. It does not have much information on time series graphics, but plenty of excellent general advice on using graphics for data analysis.