concept time - series object in category R

This is an excerpt from Manning's book R in Action, Second Edition: Data analysis and graphics with R.
Table 15.1. Functions for time-series analysis
Function
Package
Use
ts() stats Creates a time-series object. plot() graphics Plots a time series. start() stats Returns the starting time of a time series. end() stats Returns the ending time of a time series. frequency() stats Returns the period of a time series. window() stats Subsets a time-series object. ma() forecast Fits a simple moving-average model. stl() stats Decomposes a time series into seasonal, trend, and irregular components using loess. monthplot() stats Plots the seasonal components of a time series. seasonplot() forecast Generates a season plot. HoltWinters() stats Fits an exponential smoothing model. forecast() forecast Forecasts future values of a time series. accuracy() forecast Reports fit measures for a time-series model. ets() forecast Fits an exponential smoothing model. Includes the ability to automate the selection of a model. lag() stats Returns a lagged version of a time series. Acf() forecast Estimates the autocorrelation function. Pacf() forecast Estimates the partial autocorrelation function. diff() base Returns lagged and iterated differences. ndiffs() forecast Determines the level of differencing needed to remove trends in a time series. adf.test() tseries Computes an Augmented Dickey–Fuller test that a time series is stationary. arima() stats Fits autoregressive integrated moving-average models. Box.test() stats Computes a Ljung–Box test that the residuals of a time series are independent. bds.test() tseries Computes the BDS test that a series consists of independent, identically distributed random variables. auto.arima() forecast Automates the selection of an ARIMA model.
In order to work with a time series in R, you have to place it into a time-series object—an R structure that contains the observations, the starting and ending time of the series, and information about its periodicity (for example, monthly, quarterly, or annual data). Once the data are in a time-series object, you can use numerous functions to manipulate, model, and plot the data.
A vector of numbers, or a column in a data frame, can be saved as a time-series object using the ts() function. The format is
Listing 15.1. Creating a time-series object
> sales <- c(18, 33, 41, 7, 34, 35, 24, 25, 24, 21, 25, 20, 22, 31, 40, 29, 25, 21, 22, 54, 31, 25, 26, 35) > tsales <- ts(sales, start=c(2003, 1), frequency=12) #1 > tsales Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 2003 18 33 41 7 34 35 24 25 24 21 25 20 2004 22 31 40 29 25 21 22 54 31 25 26 35 > plot(tsales) > start(tsales) #2 [1] 2003 1 > end(tsales) [1] 2004 12 > frequency(tsales) [1] 12 > tsales.subset <- window(tsales, start=c(2003, 5), end=c(2004, 6)) #3 > tsales.subset Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 2003 34 35 24 25 24 21 25 20 2004 22 31 40 29 25 21 #1: 1 Creates a time-series object #2: 2 Gets information about the object #3: 3 Subsets the objectIn this listing, the ts() function is used to create the time-series object
. Once it’s created, you can print and plot it; the plot is given in figure 15.2. You can modify the plot using the techniques described in chapter 3. For example, plot(tsales, type="o", pch=19) would create a time-series plot with connected, solid-filled circles.
Figure 15.2. Time-series plot for the sales data in listing 15.1. The decimal notation on the time dimension is used to represent the portion of a year. For example, 2003.5 represents July 1 (halfway through 2003).
![]()
. Once it’s created, you can print and plot it; the plot is given in
. You can also use the window() function to create a new time series that’s a subset of the original
.