Learn R Programming

greybox (version 2.0.2)

temporaldummy: Dummy variables for provided seasonality type

Description

Function generates the matrix of dummy variables for the months / weeks / days / hours / minutes / seconds of year / month / week / day / hour / minute.

Usage

temporaldummy(object, ...)

# S3 method for default temporaldummy(object, type = c("month", "quarter", "week", "day", "hour", "halfhour", "minute", "second"), of = c("year", "quarter", "month", "week", "day", "hour", "minute"), factors = FALSE, h = 0, ...)

# S3 method for ts temporaldummy(object, type = c("month", "quarter", "week", "day", "hour", "halfhour", "minute", "second"), of = c("year", "quarter", "month", "week", "day", "hour", "minute"), factors = FALSE, h = 0, ...)

# S3 method for Date temporaldummy(object, type = c("month", "quarter", "week", "day", "hour", "halfhour", "minute", "second"), of = c("year", "quarter", "month", "week", "day", "hour", "minute"), factors = FALSE, h = 0, ...)

# S3 method for POSIXt temporaldummy(object, type = c("month", "quarter", "week", "day", "hour", "halfhour", "minute", "second"), of = c("year", "quarter", "month", "week", "day", "hour", "minute"), factors = FALSE, h = 0, ...)

# S3 method for zoo temporaldummy(object, type = c("month", "quarter", "week", "day", "hour", "halfhour", "minute", "second"), of = c("year", "quarter", "month", "week", "day", "hour", "minute"), factors = FALSE, h = 0, ...)

Value

One of the two is returned, depending on the value of factors variable:

  • factors=FALSE: Class "dgCMatrix" with all the dummy variables is returned in case of numeric variable. Feel free to drop one (making it a reference variable) or convert the object into matrix (this will consume more memory than the returned class). In other cases the object of the same class as the provided is returned.

  • factors=TRUE: The categorical variable (factor) containing specific values for each observation.

Arguments

object

Either a ts / msts / zoo / xts / tsibble object or a vector of dates.

...

Other parameters.

type

Specifies what type of frequency to produce. For example, if type="month", then the matrix with dummies for months of the year will be created.

of

Specifies the frequency of what is needed. Used together with type e.g. type="day" and of="month" will produce a matrix with dummies for days of month (31 dummies).

factors

If TRUE, the function will return the categorical variable instead of the matrix with dummies.

h

If not NULL, then the function will produce dummies for this set of observations ahead as well, binding them to the original matrix.

Author

Ivan Svetunkov, ivan@svetunkov.ru

Details

The function extracts dates from the provided object and returns a matrix with dummy variables for the specified frequency type, with the number of rows equal to the length of the object + the specified horizon. If a numeric vector is provided then it will produce dummies based on typical values (e.g. 30 days in month). So it is recommended to use proper classes with this method.

Several notes on how the dummies are calculated in some special cases:

  • In case of weeks of years, the first week is defined according to ISO 8601.

Note that not all the combinations of type and of are supported. For example, there is no such thing as dummies for months of week. Also note that some combinations are not very useful and would take a lot of memory (e.g. minutes of year).

The function will return all the dummy variables. If you want to avoid the dummy variables trap, you will need to exclude one of them manually.

If you want to have a different type of dummy variables, let me know, I will implement it.

See Also

xregExpander, xregMultiplier, outlierdummy

Examples

Run this code
# Generate matrix with dummies for a ts object
x <- ts(rnorm(100,100,1),frequency=12)
temporaldummy(x)

# Generate matrix with monthly dummies for a zoo object
x <- as.Date("2003-01-01")+0:99
temporaldummy(x, type="month", of="year", h=10)

Run the code above in your browser using DataLab