View source: R/temporaldummy.R
temporaldummy | R Documentation |
Function generates the matrix of dummy variables for the months / weeks / days / hours / minutes / seconds of year / month / week / day / hour / minute.
temporaldummy(object, ...)
## Default S3 method:
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 class '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 class '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 class '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 class '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, ...)
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
|
of |
Specifies the frequency of what is needed. Used together with |
factors |
If |
h |
If not |
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.
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.
Ivan Svetunkov, ivan@svetunkov.com
xregExpander, xregMultiplier,
outlierdummy
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.