Title: | Time Series for 'iNZight' |
---|---|
Description: | Provides a collection of functions for working with time series data, including functions for drawing, decomposing, and forecasting. Includes capabilities to compare multiple series and fit both additive and multiplicative models. Used by 'iNZight', a graphical user interface providing easy exploration and visualisation of data for students of statistics, available in both desktop and online versions. Holt (1957) <doi:10.1016/j.ijforecast.2003.09.015>, Winters (1960) <doi:10.1287/mnsc.6.3.324>, Cleveland, Cleveland, & Terpenning (1990) "STL: A Seasonal-Trend Decomposition Procedure Based on Loess". |
Authors: | Tom Elliott [aut, cre] , Zhaoming Su [aut], Junjie Zeng [ctb], Simon Potter [ctb], David Banks [ctb], Marco Kuper [ctb], Dongning Zhang [ctb] |
Maintainer: | Tom Elliott <[email protected]> |
License: | GPL-3 |
Version: | 2.0.0 |
Built: | 2024-10-30 03:00:25 UTC |
Source: | https://github.com/inzightvit/inzightts |
The iNZightTS package provides some simple analysis tools for exploring time series data. It is used in the iNZight software.
Tom Elliott (previously: Marco Kuper, Simon Potter, and David Banks)
Decomposes a time series represented by an inz_ts
object into its
seasonal, trend, and remainder components using the specified smoothing
method.
decomp( x, var = NULL, sm_model = c("stl"), mult_fit = FALSE, model_range = NULL, ... ) ## S3 method for class 'inz_dcmp' plot( x, recompose.progress = c(0, 0), recompose = any(recompose.progress > 0), ylab = NULL, title = NULL, colour = c("#1B9E46", "#45a8ff", "orangered"), ... )
decomp( x, var = NULL, sm_model = c("stl"), mult_fit = FALSE, model_range = NULL, ... ) ## S3 method for class 'inz_dcmp' plot( x, recompose.progress = c(0, 0), recompose = any(recompose.progress > 0), ylab = NULL, title = NULL, colour = c("#1B9E46", "#45a8ff", "orangered"), ... )
x |
An 'inz_dcmp' object representing the decomposed time series. |
var |
A character vector of length one, or |
sm_model |
The smoothing method to be used. Currently on "stl" is available. |
mult_fit |
If |
model_range |
The range of data to be decomposed by the model. It can be
specified as dates or years. If part of |
... |
Additional arguments (ignored). |
recompose.progress |
A numeric vector of length 2, controlling the display of recomposition progress when 'recompose' is 'TRUE'. The first component shows the progress for the seasonal component (0 to 1), and the second component tracks the number of observations recomposed so far. |
recompose |
Logical indicating whether the recomposition should be displayed or not. |
ylab |
The label for the y-axis of the plot. |
title |
The title for the plot. |
colour |
A vector of three colors specifying the colors for the trend, seasonal, and residuals components, respectively. |
An inz_dcmp
object, which is a sub-class of dable
,
representing the decomposed components of the time series.
R. B. Cleveland, W. S. Cleveland, J.E. McRae, and I. Terpenning (1990) STL: A Seasonal-Trend Decomposition Procedure Based on Loess. Journal of Official Statistics, 6, 3iV73.
ts <- inzightts(visitorsQ) d <- decomp(ts) ## Not run: plot(d) ## End(Not run)
ts <- inzightts(visitorsQ) d <- decomp(ts) ## Not run: plot(d) ## End(Not run)
Check if a plot generated by iNZightTS can be passed to plotly::ggplotly().
ggplotable(x)
ggplotable(x)
x |
a |
a logical
x <- inzightts(visitorsQ) ## Not run: ggplotable(plot(x)) ggplotable(plot(x, names(x)[-1])) ## End(Not run)
x <- inzightts(visitorsQ) ## Not run: ggplotable(plot(x)) ggplotable(plot(x, names(x)[-1])) ## End(Not run)
The function inzightts
creates temporal data frames for use in iNZight.
Unlike ts
objects, these are tsibble objects that enable temporal data
wrangling, adapting to tidy data principles, which are both data- and
model-oriented.
inzightts(x, ...) ## S3 method for class 'character' inzightts(x, stringsAsFactors = TRUE, as.is = TRUE, ...) ## S3 method for class 'data.frame' inzightts( x, var = NULL, index = NULL, key = NULL, start = NULL, end = NULL, freq = NULL, ... ) ## S3 method for class 'ts' inzightts(x, var_name = NULL, pivot_longer = FALSE, ...) ## S3 method for class 'tbl_ts' inzightts(x, ...)
inzightts(x, ...) ## S3 method for class 'character' inzightts(x, stringsAsFactors = TRUE, as.is = TRUE, ...) ## S3 method for class 'data.frame' inzightts( x, var = NULL, index = NULL, key = NULL, start = NULL, end = NULL, freq = NULL, ... ) ## S3 method for class 'ts' inzightts(x, var_name = NULL, pivot_longer = FALSE, ...) ## S3 method for class 'tbl_ts' inzightts(x, ...)
x |
A |
... |
Additional arguments to be passed to or from methods. |
stringsAsFactors |
See |
as.is |
See |
var |
The column number or name in |
index |
The column number or name in |
key |
The variable(s) that uniquely determine time indices. |
start |
The time of the first observation. It can be a single number or a vector of two integers representing a natural time unit and a (1-based) number of samples into the time unit. |
end |
The time of the last observation, specified in the same way as
|
freq |
The number of observations per unit of time. |
var_name |
The new name for the variable column of the univariate time
series, applicable only if |
pivot_longer |
Logical; set to |
If a ts
object is used to create the inzightts object, all the domain
information is extracted from that object.
The index
parameter should be a character
, Date
,
yearweek
, yearmonth
, or yearquarter
object.
If index
is a character
, the function recognizes the following
time variable formats without case sensitivity:
"(Y)yyyy": annually data, e.g., "(Y)1991"
"(Y)yyyyMmm": monthly data, e.g., "(Y)1991M01"
"(Y)yyyyQqq": quarterly data, e.g., "(Y)1991Q01"
"(Y)yyyyWww": weekly data with yearly seasonality, e.g., "(Y)1991W01"
"(Y)yyyyDdd": daily data with yearly seasonality, e.g., "(Y)1991D01"
"WwwDdd": daily data with weekly seasonality, e.g., "W01D01"
"DddHhh": hourly data with daily seasonality, e.g., "D01H01"
The length of digits of each time unit could be flexible, and spaces between the time unit are allowed.
In case data
is a data.frame or path to a .csv
file, and
start
is omitted, the starting date and the freq
are extracted
from the column that includes the time information. This column is either
named "Time"
or is the first column. If end
is omitted, all of
the data will be used for the time-series.
An inzightts
(inz_ts
) object, a sub-class of tsibble,
which includes the index variable, temporal variable, and, if
applicable, relevant keys.
tsibble
, as_tsibble
and new_tsibble
# create from a ts object z <- inzightts(UKgas) ## Not run: plot(z) ## End(Not run) # create from a data.frame x <- inzightts( data.frame(Return = rnorm(100), Time = 1900:1999), var = "Return" ) # or specify a time column x <- inzightts( data.frame(Return = rnorm(100), Year = 1900:1999), var = "Return", index = "Year" ) # create from a data.frame with modified time frame y <- inzightts( data.frame(Return = rnorm(100)), start = c(1990, 1), end = c(1993, 5), freq = 12, var = 1 ) ## Not run: plot(y) ## End(Not run)
# create from a ts object z <- inzightts(UKgas) ## Not run: plot(z) ## End(Not run) # create from a data.frame x <- inzightts( data.frame(Return = rnorm(100), Time = 1900:1999), var = "Return" ) # or specify a time column x <- inzightts( data.frame(Return = rnorm(100), Year = 1900:1999), var = "Return", index = "Year" ) # create from a data.frame with modified time frame y <- inzightts( data.frame(Return = rnorm(100)), start = c(1990, 1), end = c(1993, 5), freq = 12, var = 1 ) ## Not run: plot(y) ## End(Not run)
Log-transforms the input x
if mult_fit
is TRUE; otherwise, returns the
original input x
unchanged.
log_if(x, mult_fit)
log_if(x, mult_fit)
x |
A numeric vector to be transformed. |
mult_fit |
Logical; set to TRUE to apply logarithmic transformation, and FALSE to keep the original input. |
A numeric
vector after applying the logarithmic transformation (if
mult_fit = TRUE
); otherwise, it returns the original input.
x <- runif(1e4, 1, 100) all.equal(log_if(x, TRUE), log(x)) all.equal(log_if(x, FALSE), x)
x <- runif(1e4, 1, 100) all.equal(log_if(x, TRUE), log(x)) all.equal(log_if(x, FALSE), x)
Draws a plot of a given inzightts
(inz_ts
) object with the trend
superimposed.
## S3 method for class 'inz_ts' plot( x, var = NULL, xlab = NULL, ylab = NULL, title = NULL, xlim = NULL, aspect = NULL, compare = TRUE, pal = NULL, smoother = TRUE, sm_model = "stl", t = 0, mult_fit = FALSE, emphasise = NULL, non_emph_opacity = 0.2, show_iso_obs = TRUE, iso_obs_size = 1, seasonal_adjustment = FALSE, ... )
## S3 method for class 'inz_ts' plot( x, var = NULL, xlab = NULL, ylab = NULL, title = NULL, xlim = NULL, aspect = NULL, compare = TRUE, pal = NULL, smoother = TRUE, sm_model = "stl", t = 0, mult_fit = FALSE, emphasise = NULL, non_emph_opacity = 0.2, show_iso_obs = TRUE, iso_obs_size = 1, seasonal_adjustment = FALSE, ... )
x |
An |
var |
A character vector specifying the variable(s) to be plotted,
or set to |
xlab |
A title for the x-axis of the plot. |
ylab |
A title for the y-axis of the plot. |
title |
A title for the graph. |
xlim |
Axis limits, specified as dates or years. |
aspect |
The aspect ratio of the plot; it will be about |
compare |
Logical; set to |
pal |
(Only if a categorical variable is passed to |
smoother |
Logical; if |
sm_model |
The smoothing method to be used. |
t |
The smoothing parameter (between 0 and 100). |
mult_fit |
Logical; set to |
emphasise |
Integer vector to specify the key level(s) to focus in the
plot. The integer maps to the specific key level(s)
corresponding to the ith row of |
non_emph_opacity |
Numeric. If |
show_iso_obs |
Logical; set to |
iso_obs_size |
Numeric; scaling the size of isolated observations,
if |
seasonal_adjustment |
Logical; set to |
... |
Additional arguments (ignored). |
A time series plot (constructed with ggplot2) is returned, which can be added to if desired.
t <- inzightts(visitorsQ, var = c(2, 4)) ## Not run: plot(t) plot(t, var = names(t)[-1]) plot(t, var = "Japan") plot(t, mult_fit = TRUE) ## End(Not run)
t <- inzightts(visitorsQ, var = c(2, 4)) ## Not run: plot(t) plot(t, var = names(t)[-1]) plot(t, var = "Japan") plot(t, mult_fit = TRUE) ## End(Not run)
Generates future predictions of the time series from an inzightts
object.
The output object includes predicted means, prediction intervals, raw data,
and fitted values.
## S3 method for class 'inz_ts' predict( object, var = NULL, h = 8, mult_fit = FALSE, pred_model = "auto", confint_width = 0.95, model_range = NULL, ... ) ## S3 method for class 'inz_frct' plot(x, t_range = NULL, xlab = NULL, ylab = NULL, title = NULL, ...)
## S3 method for class 'inz_ts' predict( object, var = NULL, h = 8, mult_fit = FALSE, pred_model = "auto", confint_width = 0.95, model_range = NULL, ... ) ## S3 method for class 'inz_frct' plot(x, t_range = NULL, xlab = NULL, ylab = NULL, title = NULL, ...)
object |
An |
var |
A character vector specifying the variable(s) to forecast, or set
to |
h |
The forecast horizon, either the number of observations to predict,
or a character string specifying the time interval to predict
(e.g., |
mult_fit |
Logical; set to |
pred_model |
The name of a |
confint_width |
A decimal representing the width of the prediction interval. |
model_range |
The range of data to be used for fitting forecasts, specified as dates or years. |
... |
Additional arguments (ignored). |
x |
An |
t_range |
The range of data to be plotted, specified as dates or years. |
xlab |
A title for the x-axis of the plot. |
ylab |
A title for the y-axis of the plot. |
title |
A title for the graph. |
An inz_frct
object containing the forecasts.
t <- inzightts(visitorsQ, var = c(2, 4)) ## The following two examples are equivalent pred <- predict(t, names(t)[-1], h = "2 years") pred <- predict(t, names(t)[-1], h = 8) ## Not run: plot(pred) ## End(Not run)
t <- inzightts(visitorsQ, var = c(2, 4)) ## The following two examples are equivalent pred <- predict(t, names(t)[-1], h = "2 years") pred <- predict(t, names(t)[-1], h = 8) ## Not run: plot(pred) ## End(Not run)
A dataset containing sea ice measurements from 1990 to 2011.
seaice
seaice
A data frame with 265 rows and 3 variables:
The time variable
Sea ice measurement for the Arctic
Sea ice measurement for Antarctica
This function plots the seasonal components of a time series together with the estimated seasonal effects of that series.
seasonplot(x, ...)
seasonplot(x, ...)
x |
An 'inzightts' ('inz_ts') object representing the time series. |
... |
Further arguments to be passed onto specific methods and the 'gg_season' function. |
The resulting window will contain two plots. On the left, every seasonal subseries of the time series is plotted. On the right will be the average seasonal effect of the series.
A 'patchwork' object of seasonal plots.
## Not run: seasonplot(inzightts(visitorsQ)) ## End(Not run)
## Not run: seasonplot(inzightts(visitorsQ)) ## End(Not run)
Time series subseries plot by seasonal period
subseries( x, var = NULL, show_mean = TRUE, xlab = NULL, ylab = NULL, title = NULL )
subseries( x, var = NULL, show_mean = TRUE, xlab = NULL, ylab = NULL, title = NULL )
x |
A time series object represented by an |
var |
A character vector specifying the variable(s) to be plotted, or
set to |
show_mean |
Logical; set to |
xlab |
A title for the x-axis of the plot. |
ylab |
A title for the y-axis of the plot. |
title |
A title for the graph. |
Plots seasonal subseries of a time series represented by an inz_ts
or
tbl_ts
object. Each subseries represents one seasonal period.
A ggplot object of the seasonal subseries plot.
t <- inzightts(visitorsQ) ## Not run: subseries(t) ## End(Not run)
t <- inzightts(visitorsQ) ## Not run: subseries(t) ## End(Not run)
Summary method for objects of class inz_frct
.
## S3 method for class 'inz_frct' summary(object, var = NULL, ...) ## S3 method for class 'summary_inz_frct' print(x, show_details = FALSE, ...)
## S3 method for class 'inz_frct' summary(object, var = NULL, ...) ## S3 method for class 'summary_inz_frct' print(x, show_details = FALSE, ...)
object |
An |
var |
A character vector specifying the variable to summarize,
or set to |
... |
Additional arguments (ignored). |
x |
A 'summary_inz_frct' object containing forecast summaries. |
show_details |
Logical; set to 'TRUE' to show model details only when 'pred_model' is an "ARIMA" model. |
A summary_inz_frct
object containing the first few forecast
observations, the forecasting model used, and its details (such as
call, coefficients, and goodness of fit statistics).
ts <- inzightts(visitorsQ, var = 2:5) p <- predict(ts, "Japan") s <- summary(p, "Japan") s print(s, show_details = TRUE)
ts <- inzightts(visitorsQ, var = 2:5) p <- predict(ts, "Japan") s <- summary(p, "Japan") s print(s, show_details = TRUE)
A dataset containing annual visitor numbers for several countries.
visitorsA2
visitorsA2
A data frame with 13 rows and 5 variables:
The time variable (year)
Visitor counts for Australia
Visitor counts for China
Visitor counts for Japan
Visitor counts for the UK
A dataset containing monthly visitor numbers for several countries.
visitorsM2
visitorsM2
A data frame with 164 rows and 5 variables:
The time variable (year/month)
Visitor counts for Australia
Visitor counts for China
Visitor counts for Japan
Visitor counts for the UK
A dataset containing quarterly visitor numbers for several countries.
visitorsQ
visitorsQ
A data frame with 54 rows and 5 variables:
The time variable (year/quarter)
Visitor counts for Australia
Visitor counts for China
Visitor counts for Japan
Visitor counts for the UK