3.1 ts objects

In base R, time-series data can be stored as a ts object. A ts object (time series object) stores information about the time steps of the data and often seasonal information (the quarter or month). If you pass in a ts object as data into MARSS(), it will convert this to a matrix but it will ignore any information about the seasonality. It will store this information, but it does not use it as part of model specification.

If you have your data stored as a ts object, then you may be using year and season (quarter, month) as covariates to estimate trend and seasonality. The next sections give examples of converting your data to matrix form with the season information.

3.1.1 Univariate ts object

This converts a univariate ts object with year and quarter into a matrix with a row for the response (here called Temp), year, and quarter.

z <- ts(rnorm(10), frequency = 4, start = c(1959, 2))
dat <- data.frame(Yr = floor(time(z) + .Machine$double.eps), 
      Qtr = cycle(z), Temp=z)
dat <- t(dat)
class(dat)

Notice that the class of dat is matrix, which is what we want. There are three rows, first is the reponse and the second and third are the covariates, Year and Quarter. When you call MARSS(), dat["Temp",] is the data. dat[c("Yr","Qtr"),] are your covariates.

3.1.2 Multivariate ts object

In this example, we have two temperature readings, our responses, and a salinity reading, a covariate. The data are monthly.

z <- ts(matrix(rnorm(300), 100, 3), start = c(1961, 1),
     frequency = 12, names=c("Temp1","Temp2","Sal"))
dat <- data.frame(Yr = floor(time(z) + .Machine$double.eps), 
     Month = cycle(z), z)
dat <- t(dat)

When you call MARSS(), dat[c("Temp1","Temp2"),] are the data and dat[c("Yr","Month","Sal"),] are your covariates.

See the chapters in the ATSA book that discuss seasonality for examples of how to model season. The brute force method of treating month or quarter as a factor requires estimation of more parameters than is necessary in many cases.