plot_ts {vctsfr} | R Documentation |
Create a ggplot object with a time series and forecast
Description
Create a ggplot
object associated with a time series and, optionally, its
future values, a forecast for its future values and a prediction interval of
the forecast.
Usage
plot_ts(
ts,
future = NULL,
prediction = NULL,
method = NULL,
lpi = NULL,
upi = NULL,
level = NULL,
sdp = TRUE
)
Arguments
ts |
a time series of class |
future |
NULL (default) or a time series of class |
prediction |
NULL (default) or a time series of class |
method |
NULL (default) a character string with the name of the method used to forecast the future values of the time series. This name will appear in the legend. |
lpi |
NULL (default) or a time series of class |
upi |
NULL (default) or a time series of class |
level |
NULL (default) a number in the interval (0, 100) indicating the level of the prediction interval. |
sdp |
logical. Should data points be shown? (default value |
Details
If future
or prediction
are vectors then they are supposed to
start after the last data of the time series.
Value
The ggplot
object representing the time series and its forecast.
Examples
library(ggplot2)
plot_ts(USAccDeaths) # plot a time series
# plot a time series, not showing data points
plot_ts(USAccDeaths, sdp = FALSE)
# plot a time series, its future values and a prediction
ts <- window(USAccDeaths, end = c(1977, 12))
f <- window(USAccDeaths, start = c(1978, 1))
p <- ts(window(USAccDeaths, start = c(1976, 1), end = c(1976, 12)),
start = c(1978, 1),
frequency = 12
)
plot_ts(ts, future = f, prediction = p)
# plot a time series and a prediction
plot_ts(USAccDeaths, prediction = rep(mean(USAccDeaths), 12),
method = "Mean")
# plot a time series, a prediction and a prediction interval
if (require(forecast)) {
timeS <- window(USAccDeaths, end = c(1977, 12))
f <- window(USAccDeaths, start = c(1978, 1))
ets_fit <- ets(timeS)
p <- forecast(ets_fit, h = length(f), level = 90)
plot_ts(timeS, future = f, prediction = p$mean, method = "ES",
lpi = p$lower, upi = p$upper, level = 90
)
}