Get startedGet started for free

Time series

1. Time series

Moving on! In this lesson, we will review time series!

2. Time series

Time-dependent data may pop up during an interview for a position in which you analyze how variables change in time. Companies that collect time-dependent data operate in sectors such as finance, agriculture, and energy.

3. Time series

Wrangling time-dependent data using base R classes, like vectors, is hard, since time is irregular.

4. Time series

It's good to know at least one package for handling time series that will do the hard work for you. In this lesson, we'll use the xts package because of its general application.

5. Time series

To check your skills, the interviewer might ask you to analyze time series in search of trends, seasonal variation, or serial correlation. Another common task is to develop a time series prediction model, such as ARIMA.

6. Time series - object

A time-series object

7. Time series - object

consists of a set of dates or date times

8. Time series - object

and a set of corresponding values.

9. Time series - object

To initiate an xts object in R, you need to pass values and corresponding dates as arguments to the xts function.

10. Analysis - plot

The first step of a time series analysis is usually the visualization. The plot function applied on an xts object outputs a nicely formatted linear plot.

11. Analysis - subsetting

Often, we are interested in a specific period rather than the whole history. You can create a period as a set of dates using the sequence function.

12. Analysis - subsetting

The cool thing is that you can set the "by" argument using standard words, like "3 weeks" or "1 month".

13. Analysis - subsetting

Subsetting an xts object is analogous to subsetting a data frame.

14. Analysis - merging

During an interview, you may be asked to compare two time-series. The merge function makes it easy to join time-dependent datasets.

15. Analysis - merging

By default, the merge function joins all data.

16. Analysis - merging

If you prefer to join only common dates, set the all parameter to FALSE.

17. Analysis - merging

You can also carry the last observation forward. Filling the gaps with the last observation can be achieved with the na.locf function applied upon the merge function.

18. Analysis - applying a function by calendar period

Another common task associated with a time series is performing calculations on a specific period.

19. Analysis - applying a function by calendar period

The apply.monthly function, as the name suggests, applies a given function on all data within one month.

20. Analysis - applying a function by calendar period

The apply.yearly function performs calculations on data within one year.

21. Summary

To summarize, we covered what a time series is, how to create a time series object in R, and went over a few ways to work with time-dependent data, namely: subsetting, merging, and applying a function over calendar periods.

22. Let's practice!

Let's wrangle some time-dependent data!