When a data frame object is passed to ts(
) function it will be coerced to a numeric matrix via data.matrix()
(see help(ts)
).
The ts()
function will return a Time-Series object which will be a matrix of class 'ts', with a new attribute 'tsp', and the original data frame attributes. These lasts are converted into 'dimnames' as a list of 2 attributes: dimnames[[1]]
(originally 'row.names') and dimnames[[2]]
(originally 'names').
When a vector is passed to ts()
it will return a Time-Series object composed of the vector and the 'tsp' attribute.
Finally, when a Time-Series object is passed to stl()
it will check weather the object is a matrix via is-matrix() and will return an error if true.
if (is.matrix(x))
stop("only univariate series are allowed")
Note:
- A data frame is a list of vectors of equal length with attributes like 'names' and 'row.names'.
- "A Time-Series object is a vector or matrix with class 'ts' and additional attributes" (
help(ts)
).
To check a R object attributes call the functions: attributes(obj)
It's also possible to verify an object dimension, i.e, if it's a vector, with the functions is.vector(obj)
, dim(obj)
, and dimnames(obj)
.
See the code bellow for an example:
# Exploring stl() arguments #
# load packages ####
if (!require("pacman")) install.packages("pacman")
pacman::p_load("vctrs")
pacman::p_load("stats")
# create a data frame "df" ####
df <- vctrs::data_frame(x = 133:165, y = pi, z = seq(25.25, 1.25, by = -0.75))
# visualizing and checking attributes
head(df)
str(df)
attributes(df)
dimnames(df)
# accessing single vectors/variables with subsetting ####
# 1 - double brackets
head(df[[3]]) # return a vector
is.vector(df[[3]])
# 2 - $ operator
head(df$z) # returns a vector
is.vector(df$z)
# 3 - single brackets
head(df[3]) # return a data frame!
is.vector(df[3])
dim(df[3])
dimnames(df[3])
attributes(df[3])
# creating a Time-Series object using ts() ####
MySeriesTs1 <- ts(df[[3]], start = c(1999, 10), frequency = 12)
MySeriesTs1
str(MySeriesTs1)
dim(MySeriesTs1) # time-series is a vector
MySeriesTs2 <- ts(df$z, start = c(1999, 10), frequency = 12)
MySeriesTs2
str(MySeriesTs2)
dim(MySeriesTs2) # time-series is a vector
MySeriesTs3 <- ts(df[3], start = c(1999, 10), frequency = 12)
MySeriesTs3
str(MySeriesTs3)
dim(MySeriesTs3) # time-series is a matrix!
dimnames(MySeriesTs3)
attributes(MySeriesTs3)
# Note: ts() accepts either a vector, a matrix or a data frame object as argument.
# here I pass the data frame with all three variables as argument to ts()
MySeriesTs <- ts(df, start = c(1999, 10), frequency = 12)
MySeriesTs
dim(MySeriesTs)
str(MySeriesTs) #returns a matrix with a List of 2 "dimnames" attributes: NULL and "x" "y" "z"
# calling stl() function ####
# stl() accepts only univariate time series as argument.
# Therefore, if the Time-Series object is a matrix, it has to be subset when passed to stl.
# passing a vector
MyDecompose1 <- stl(MySeriesTs1, s.window = 'p') # RUN
# passing a vector
MyDecompose2 <- stl(MySeriesTs2, s.window = 'p') # RUN
# passing a matrix
MyDecompose3 <- stl(MySeriesTs3, s.window = 'p') # ERROR!
# passing a subset of a matrix
MyDecompose3 <- stl(MySeriesTs3[,"z"], s.window = 'p') # RUN
# passing a matrix
MyDecompose <- stl(MySeriesTs, s.window = 'p') #ERROR!
# passing a subset of a matrix
MyDecompose <- stl(MySeriesTs[,"z"], s.window = 'p') #RUN