Chapter 12. Time Series Analysis
A time series is a sequence of measurements from a system that varies in time. Many of the tools we used in previous chapters, like regression, can also be used with time series. But there are additional methods that are particularly useful for this kind of data.
As examples, we’ll look at two datasets: renewable electricity generation in the United States from 2001 to 2024, and weather data over the same interval. We will develop methods to decompose a time series into a long-term trend and a repeated seasonal component. We’ll use linear regression models to fit and forecast trends. And we’ll try out a widely used model for analyzing time series data, with the formal name “autoregressive integrated moving average” and the easier-to-say acronym ARIMA.
Electricity
As an example of time-series data, we’ll use a dataset from the US Energy Information Administration—it includes total electricity generation per month from renewable sources from 2001 to 2024. Instructions for downloading the data are in the notebook for this chapter.
After loading the data, we have to make some transformations to get it into a format that’s easy to work with:
elec=(pd.read_csv("Net_generation_for_all_sectors.csv",skiprows=4).drop(columns=["units","source key"]).set_index("description").replace("--",np.nan).transpose().astype(float))
In the reformatted dataset, each column is a sequence of monthly totals in gigawatt hours (GWh). Here are the column labels, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access