I. Ozkan
March 2025
Time series data set composed of a sequence of observation with information about the times those numbers were recorded
Our textbook Forecasting: Principles and Practice (3rd
ed) uses tsibble
object for the analysis
There are several time series objects available in R, among them here are the most widely used objects:
ts
reguler time series (comes with base packages)xts
extensible time-series object (comes with
xts
package)zoo
class of indexed totally ordered observations which
includes irregular time series (comes with zoo
package)timeSeries
“timeSeries” object (comes with
timeSeries
package)tsibble
tsibble object (comes with
tsibble
) packagets(data = NA, start = 1, end = numeric(), frequency = 1,
deltat = 1, ts.eps = getOption("ts.eps"),
class = if(nseries > 1) c("mts", "ts", "matrix", "array") else "ts",
names = )
Most frequently used arguments:
data: a vector/matrix of the observed values
start: the time of the observation
end: the time of the last observation
frequency: the number of observations per unit of
time
Example
Qtr1 Qtr2 Qtr3 Qtr4
1959 1 2 3
1960 4 5 6 7
1961 8 9 10
Time-Series [1:10] from 1959 to 1962: 1 2 3 4 5 6 7 8 9 10
[1] 1959.25 1961.50 4.00
[1] 1959 2
[1] 1961 3
[1] 4
xts(
x = NULL,
order.by = index(x),
frequency = NULL,
unique = TRUE,
tzone = Sys.getenv("TZ"),
...
)
x: An object containing the underlying data order.by: A corresponding vector of dates/times of a known time-based
example
[,1]
2000-01-01 1
2000-04-01 2
2000-07-01 3
2000-10-01 4
2001-01-01 5
2001-04-01 6
2001-07-01 7
2001-10-01 8
2002-01-01 9
2002-04-01 10
An xts object on 2000-01-01 / 2002-04-01 containing:
Data: integer [10, 1]
Index: Date [10] (TZ: "UTC")
[1] TRUE
[1] "2000-01-01" "2000-04-01" "2000-07-01" "2000-10-01" "2001-01-01"
[6] "2001-04-01" "2001-07-01" "2001-10-01" "2002-01-01" "2002-04-01"
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5
[6,] 6
[7,] 7
[8,] 8
[9,] 9
[10,] 10
zoo(x = NULL, order.by = index(x), frequency = NULL,
calendar = getOption("zoo.calendar", TRUE))
x: a numeric vector, matrix or a factor
order.by: an index vector with unique entries by which
the observations in x are ordered
example
2000-01-01 2000-04-01 2000-07-01 2000-10-01 2001-01-01 2001-04-01 2001-07-01
1 2 3 4 5 6 7
2001-10-01 2002-01-01 2002-04-01
8 9 10
'zoo' series from 2000-01-01 to 2002-04-01
Data: int [1:10] 1 2 3 4 5 6 7 8 9 10
Index: Date[1:10], format: "2000-01-01" "2000-04-01" "2000-07-01" "2000-10-01" "2001-01-01" ...
[1] TRUE
[1] "2000-01-01" "2000-04-01" "2000-07-01" "2000-10-01" "2001-01-01"
[6] "2001-04-01" "2001-07-01" "2001-10-01" "2002-01-01" "2002-04-01"
[1] 1 2 3 4 5 6 7 8 9 10
timeSeries(data, charvec, units = NULL, format = NULL, zone = "",
FinCenter = "", recordIDs = data.frame(), title = NULL,
documentation = NULL, ...)
data: a matrix object or any objects which can be
coerced to a matrix
charvec: a character vector of dates and times or any
objects which can be coerced to a “timeDate” object
Example
library(timeSeries)
timeSeries(1:10, charvec = seq(as.Date("2000/1/1"), by = "quarter", length.out = 10))
GMT
TS.1
2000-01-01 1
2000-04-01 2
2000-07-01 3
2000-10-01 4
2001-01-01 5
2001-04-01 6
2001-07-01 7
2001-10-01 8
2002-01-01 9
2002-04-01 10
[1] "2000-01-01" "2000-04-01" "2000-07-01" "2000-10-01" "2001-01-01"
[6] "2001-04-01" "2001-07-01" "2001-10-01" "2002-01-01" "2002-04-01"
GMT
TS.1
2000-01-01 1
2000-04-01 2
2000-07-01 3
2000-10-01 4
2001-01-01 5
2001-04-01 6
2001-07-01 7
2001-10-01 8
2002-01-01 9
2002-04-01 10
x <- timeSeries(1:10, charvec = seq(as.Date("2000/1/1"), by = "quarter", length.out = 10))
is.timeSeries(x)
[1] TRUE
GMT
[1] [2000-01-01]
GMT
[1] [2002-04-01]
GMT
[1] [2000-01-01] [2000-04-01] [2000-07-01] [2000-10-01] [2001-01-01]
[6] [2001-04-01] [2001-07-01] [2001-10-01] [2002-01-01] [2002-04-01]
[1] 1 2 3 4 5 6 7 8 9 10
tsibble(..., key = NULL, index, regular = TRUE, .drop = TRUE)
index: A variable to specify the time index variable.
Example
library(tsibble)
library(dplyr)
x <- tsibble(
Date = seq(as.Date("2000/1/1"), by = "quarter", length.out = 10),
Observation = 1:10,
index = Date
)
x
# A tsibble: 10 x 2 [1D]
Date Observation
<date> <int>
1 2000-01-01 1
2 2000-04-01 2
3 2000-07-01 3
4 2000-10-01 4
5 2001-01-01 5
6 2001-04-01 6
7 2001-07-01 7
8 2001-10-01 8
9 2002-01-01 9
10 2002-04-01 10
[1] TRUE
Date
[1] "Observation"
# A tibble: 10 × 1
Observation
<int>
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
# A tsibble: 10 x 3 [1D]
Date Observation Month
<date> <int> <chr>
1 2000-01-01 1 January
2 2000-04-01 2 April
3 2000-07-01 3 July
4 2000-10-01 4 October
5 2001-01-01 5 January
6 2001-04-01 6 April
7 2001-07-01 7 July
8 2001-10-01 8 October
9 2002-01-01 9 January
10 2002-04-01 10 April
There are several packages that are very useful for time series data wrangling
Among them (where I use them frequently):
lubridate
(we covered in MIS 205 Course)anytime
Anything to ‘POSIXct’ or ‘Date’ Converterchron
Chronological Objects which Can Handle Dates and
Timestsbox
a set of functions that convert time series of
different classes to each othe**For Our Course Please Read Section 2.1 of Forecasting: Principles and Practice (3rd ed)