Creating a Quarterly Time-Series Dummy for Q4 Regression
Summary
The document explains two ways to mark fourth-quarter observations in quarterly time-series data for use in regression or other analysis. One approach creates a repeating sequence of zeroes with a one in each fourth-quarter position, optionally representing it as an R time-series object. Another converts the time index to a data frame, assigns quarter labels from the fractional part of the time value, and uses those labels to select Q4 observations.
The examples show how a quarter indicator can be aligned with the observations and how quarter-specific values could be transformed. The latter example increases Q4 values, illustrating a possible use rather than a general dummy-variable regression procedure. The discussion does not address missing observations, nonstandard time indexes, or the suitability of the chosen encoding for a particular model. Users should ensure the indicator has the same length and ordering as the data being modeled.
Key ideas
- A quarterly Q4 indicator can be built as a repeating sequence with a one in every fourth position.
- The indicator may be stored as a time-series object or used as a plain vector.
- Quarter labels can be derived from the fractional part of a quarterly time index.
- The example also shows selecting Q4 values for a separate transformation.
- The dummy must align in length and order with the observations used in analysis.
Tags
Full text
# adding dummy variable to ts object in r for particular quarter
# adding dummy variable to ts object in r for particular quarter
I've looked all over and can't seem to get a clear idea of how to do this; I have ts data with quarterly frequencies. I simply want to add a dummy variable only for the data corresponding to Q4 but I can't seem to make this work. I thought subset may be correct however I get an error that datasets are of different length when I try and regress data~trend+subset. I ran into a similar problem when trying to use the as.factor approach. I'm a newbie to R so any help would be appreciated.
## Answer by Sergey Bushmanov (score 0)
https://quant.stackexchange.com/a/22715
Time series of 4 years of quarterly data with `1` for 4th quarter:
```
ts(rep(c(0,0,0,1),4), f=4)
Qtr1 Qtr2 Qtr3 Qtr4
1 0 0 0 1
2 0 0 0 1
3 0 0 0 1
4 0 0 0 1
```
Is this what you want?
That was a dummy series of `ts` class. Actually, depending on your needs simple `rep(c(0,0,0,1),4)` may suffice, e.g. for ARIMA regression as exog dummy (w/o coercion to `ts`)
## Answer by Joel Alcedo (score 0)
https://quant.stackexchange.com/a/35475
Here's one solution.
For this example I will use the ausres data set.
```
df <- data.frame(time=time(austres), Value = as.matrix(austres))
df$Quarter <- ifelse(df$time - floor(df$time) == 0, 'Q1',
ifelse(df$time - floor(df$time) == 0.25, 'Q2',
ifelse(df$time - floor(df$time) == 0.5, 'Q3',
ifelse(df$time - floor(df$time) == 0.75, 'Q4',''))))
df$NewData <- ifelse(df$Quarter == 'Q4', df$Value*1.15, df$Value)
```
First line, convert your time series to a data frame. Then associate each ts to corresponding quarter. Last line you'll want to transform Q4 based on whatever you want to do. In this case I am increasing Q4 by 15%. If you need to transform the data frame back to a time series you can always do that since the first column is preserved.Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.