1
votes

I am trying to generate a time dummy variable in R. I am analyzing quarterly panel data (1990q1-2013q3). How do I generate a time dummy variable for 2007q1-2009q1 period, i.e. for 2007q1 dummy=1...

Data looks like in the picture. Asset rank is the id variable.

enter image description here

Regards & Thanks!

3
I think you will find StackOverflow to be more helpful for this kind of questions.usεr11852

3 Answers

3
votes

I would say model.matrix is probably your best bet.

date.f <- factor(dat$date)
dummies = model.matrix(~date.f)
1
votes

I used more simpler way following this answer. I guess there is no difference between time series and panel data here in terms of application.

print date
dummy <- as.numeric(date >= "2007 Q1" & date<="2008 Q4")
print (dummy)
1
votes

The answer of @Demet is useful, but it gets kind of tedious if you have many (e.g. 50 periods).

The answer of @Amstell is useful too, it returns a matrix including an intercept with ones. Depending on how you want to continue analyzing the data you have to take out which is the most useful for your follow-up analysis.

In addition to the answers proposed I propose the following code which shows you just a single dummy variable instead of a huge matrix.

dummies = table(1:length(date),as.factor(date))  

Furthermore it is important to take care which time period is the reference group for interpreting the model. You can change the reference group if you have two time periods by applying the following code.

abs(Date(-1))