Expanding mean
Expanding means, also called cumulative means, allow you to calculate the mean value of a time series up to each point. By doing so, you can follow how the mean of your time series changes with time.
In this exercise, you'll calculate an expanding mean of the card_price
time series. Once you create the expanding mean, you'll plot it on top of an existing plot of the original time series, called card_price_plot
, a ggplot
object which has already been created for you.
Remember, expanding windows must be right
-aligned.
card_price
, card_price_plot
, and the zoo
and ggplot2
packages are available to you.
This is a part of the course
“Manipulating Time Series Data in R”
Exercise instructions
- Create a vector of window widths from
card_price
and assign it toprice_windows
. - Using
price_windows
, create an expanding mean of thecard_price
time series; assign it toprice_exp_mean
. - Add a
ggplot2
line plot tocard_price_plot
by completing the statement; color the expanding window with a blue line.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create the vector of window widths
___
# Create the expanding window
___
# Plot the expanding window over the original time series
card_price_plot +
geom_line(data = ___, ___)
This exercise is part of the course
Manipulating Time Series Data in R
Master time series data manipulation in R, including importing, summarizing and subsetting, with zoo, lubridate and xts.
You’ll learn to create “rolling” windows of a time series that move, or "roll" along with data, making it possible to summarize trends in the data across time, such as the average over success months of observations or the sum over several weeks of sales. Overall summary statistics, like mean, median, sum, maximum, and so on, do not always provide insight into how data changes over time, and rolling windows will allow you to compute statistics dynamically. In addition to rolling windows, you will also learn how to create expanding windows, which show how these summary statistics approach their final value.
Exercise 1: What is a rolling window?Exercise 2: Rolling window functionsExercise 3: Rolling windows versus aggregationExercise 4: Applying functions to rolling windowsExercise 5: Rolling minimumExercise 6: Rolling apply with a custom functionExercise 7: Expanding windowsExercise 8: Rolling versus expanding windowsExercise 9: Expanding sumExercise 10: Expanding meanExercise 11: Congratulations!What is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.