Importing and manipulating periodic time series data

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
pd <- packageDescription("pcts")

subtitle: "This vignette is part of package pcts, version r pd[["Version"]]."

library(pcts)

The intended mode of work with time series in package pcts() is that users create periodic time series using function pcts() and then operate on the object created by it. pcts() accepts base R data objects, such as "ts", "mts", "numeric", "matrix", "data. frame", as well as some time series classes with regular time from other packages, including "zooreg" (other classes can be added if required).

Package pcts provides two native classes for periodic time series: "PeriodicTS" and "PeriodicMTS" for univariate and multivariate time series, respectively. There are also some classes serving as wrappers for the base "ts" and "mts" classes and some classes from other packages, such as "zoo", if the original classes need to be preserved.

When supplied with a time series object, pcts() gets the time information from it. For example, AirPassengers is a monthly time series starting on r start(AirPassengers) from base R, so it is converted to a periodic time series with a monthly cycle and the same start time:

ap <- pcts(AirPassengers)
ap

Get the data from Mar 1952 to Feb 1959:

window(ap, start = c(1952, 3), end = c(1959, 2))

Take the summer months only:

window(ap, seasons = 7:9)

The dataset dataFranses1996 contains a multivariate quarterly time series, see ?dataFranses1996 for more information. The object has the standard R class "mts", so can be loaded using data() without loading pcts. Here is some information about it:

data(dataFranses1996)
class(dataFranses1996)
dim(dataFranses1996) # c(148, 19)
colnames(dataFranses1996)

It can be converted to periodic time series with

pcfr <- pcts(dataFranses1996)
colnames(pcfr)[2:3]

The Franses' time series is quite large and for convenience in presentation we work with parts of it below.

Let's take one of the time series:

tipi <- dataFranses1996[ , "USTotalIPI"]
plot(tipi)

Convert tipi to PeriodicTS and remove NAs at the start and end:

pctipi <- pcts(tipi)
pctipi <- window(pctipi, start = availStart(pctipi), end = availEnd(pctipi))
plot(pctipi)
boxplot(pctipi)

Subset a "PeriodicMTS" (shorten the names to reduce clutter in plots):

pcfr2to3 <- pcfr[2:3]
colnames(pcfr2to3) <- substr(colnames(pcfr2to3), 1, 5)
plot(pcfr2to3)

"[" gives "PeriodicMTS" even with length one argument:

pcfr2to2  <- pcfr[2]
pcfr2to2a <- pcfr["USTotalIPI"] # same

Use "[[" or $ to get "PeriodicTS"

pcfr2 <- pcfr[[2]]
pcfr2a <- pcfr[["USTotalIPI"]] # same
pcfr2b <- pcfr$USTotalIPI      # same
identical(pcfr2, pcfr2a) # TRUE
identical(pcfr2, pcfr2b) # TRUE

c1 <- cycle(pcfr)
head(c1, 8)
frequency(pcfr)


Try the pcts package in your browser

Any scripts or data that you put into this service are public.

pcts documentation built on April 4, 2025, 1:47 a.m.