A notation for usual time intervals

In statistical production it is common to refer to natural time intervals such as months, terms, semesters, years, etc. We will use a compact notation to denote them according to the following convention:

Interval | Notation -----------|-------------------------- Fortnight | Q (Quincena in Spanish) Month | M Bimonth | B Term | T Semester | S Year | A (Año in Spanish)

Occasionally in statistical operations it is necessary to rotate the sample. This drives us to identify those data sets with a rotated sample from those without a rotation. We modify the preceding table by introducing a second letter with self-evident meaning:

Interval | Notation -------------|------------------------------- Fortnight | QQ, QR (Quincena in Spanish) Month | MM, MR Bimonth | BB, BR Term | TT, TR Semester | SS, SR Year | AA, AR (Año in Spanish)

Now each time interval is denoted with the following notation:

Interval | Initials | Notation -------------|--------------|----------------------------------------------------------- Fortnight | QQ, QR | qmmaaaa, q=1,2; mm= 01, ..., 12; aaaa= 4-digit year Month | MM, MR | mmaaaa, mm = 01, ..., 12; aaaa = 4-digit year Bimonth | BB, BR | baaaa, b = 1, ..., 6; aaaa = 4-digit year Term | TT, TR | taaaa, t = 1, ..., 4; aaaa = 4-digit year Semester | SS, SR | saaaa, s = 1, 2; aaaa = 4-digit year Year | AA, AR | aaaa = 4-digit year

A few examples:

The class RepoTimeInt

To implement this notation we have defined a class named RepoTimeInt with two attributes:

The attribute Repo contains the specification of several time intervals in the former notation whereas the attribute lubriInt expresses this same set of time intervals using the class Interval from the package lubridate.

The general idea of the package is to express time intervals with the agreed notation but to work internally with the functions already defined for Interval-class methods from the package lubridate.

To build an object of class RepoTimeInt we have defined a constructor:

library(RepoTime)
Months2015 <- newRepoTime(paste0('MM', c(paste0('0', 1:9), 10:12), '2015'))
str(Months2015)

Internally this constructor makes use of the function RepoTimeTolubri:

RepoTimeTolubri('MM012015')

This has an inverse function:

lapply(RepoTimeTolubri(c('MM012015', 'AA2016')), lubriToRepoTime)

Both attributes can be recoverd with the respective getters:

getRepo(Months2015)
getlubriInt(Months2015)

Operations with time intervals

We have adapted some methods to manipulate RepoTimeInt-class objects. Firstly, given an object RepoTimeInt with length greater than 1, we can obtain a new interval resulting from the union of all intervals contained in the object:

Length(Months2015)
Union(Months2015)

If we have two objects of length 1, we can obtain a new period as the union of both time intervals:

Union(Months2015['MM012015'], Months2015['MM032015'])

Similarly, we can obtain the common time intervals of two given objects:

Intersect(newRepoTime('AA2015'), newRepoTime('MM012015'))

Or of a set of intervals:

Intersect(Months2015)

We can also create a sequence of time intervals as a list of objects of class RepoTimeInt:

Seq(newRepoTime('MM112015'), newRepoTime('MM032016'))

This method allows us also to take into account whether to include or not data sets with rotated samples and/or to indicate which time interval contains a second dataset with the rotated sample:

Seq(newRepoTime('MM112015'), newRepoTime('MM032016'), Rot = FALSE)
Seq(newRepoTime('TT32015'), newRepoTime('TT12016'), Rot = TRUE, RotPer = '4')

Notice that to create a sequence of rotated time intervals, we must do it interval by interval:

newRepoTime(paste0('TR', 1:4, '2015'))

Finally, we can add duration objects to a time interval:

library(lubridate)
RepoPeriod <- newRepoTime('MM022015')
RepoPeriod + months(1)
RepoPeriod + years(1)
RepoPeriod + weeks(1)


david-salgado/RepoTime documentation built on Feb. 2, 2024, 2:07 p.m.