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 = ___, ___)