What is time series data?
1. What is time series data?
Welcome to Manipulating Time Series Data in R! This course is all about the foundations of working with time series datasets and how to best prepare our data for performing time series analysis.2. What is a time series?
Let's begin by defining a "time series". A time series is a list of observations ordered successively over time. In a time series, observations are often recorded at evenly-spaced intervals, such as daily, weekly, monthly, and so on. Time series datasets are ubiquitous in the real world – a time series analysis studies how a variable changes over time, rather than only measuring the variable at different points in time. Let's look at some real-world uses of time series! One field that makes frequent use of time series data is marketing and analytics — for example, the monthly count of passengers in an international airline.3. What is a time series?
Another example is finance — the closing price of a particular stock market each business day.4. What is a time series?
There's also scientific research — this last graph shows the concentration of carbon dioxide in the atmosphere over time.5. Time series in R
Let's see what a time series looks like in R. R has dedicated classes for time series objects: 'ts' in base R and 'zoo' from the zoo package, which we'll explore later in the course. For example, the time series 'AirPassengers', from the datasets package, tracks the monthly count of international airline passengers. Let's print our time series to the console. In a time series — as shown in the output — each observation is comprised of two parts: a point in time and the value of the observation at that time. Here's a plot of AirPassengers, representing passengers in thousands.6. Summary statistics
Our dataset tracks monthly passengers from January 1949 to December 1960. The first observation has 112 thousand passengers, the next has 118 thousand, and so on. Let's get some more information about the dataset. Calling the summary function, we get some summary statistics about the dataset, including the mean, median, and quartiles.7. Why use time series objects in R?
All of this begs the question: what's the point of using dedicated time series objects; why can't we just use normal R vectors and data frame columns? Well, we'll cover them later in the course, but many of the functions and workflows in R and packages like zoo are designed to work with these objects. In fact, many statistical models used in time series analysis work best when we use time series objects, rather than vectors or data frames. Also, time series objects are made to "tie together" each observation's value and its time, making sure they aren't easily separated during our analysis. In real-world time series analysis, time series objects in R make workflows much smoother!8. Plotting with autoplot
Now, let's take a look at how to generate a plot of a time series object using ggplot2. Although this package is designed to work primarily with data stored in a data frame, there is a very helpful function that allows us to plot time series objects: it's called autoplot To use it, we wrap the autoplot function around our time series, like so, and it returns a lovely plot made by ggplot2! For clarity, the code for adding in axis labels and the title has been omitted. You can also use themes from ggplot2 to make your plots stand out, like so!9. Let's practice!
Alright, let's dive in to some exercises so we can get a feel for how time series objects work!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.