1

I want to fit a linear model to electricity prices with a seasonal dummy. So "DK.days" contains the days of every year for a period of 10 years.

head(DK.days)
[1] "2007-01-01" "2007-01-02" "2007-01-03" "2007-01-04" "2007-01-05" "2007-01-06"

This is the rest of the code.

month <- as.numeric(format(DK.days, "%m"))
MD <- t(sapply(month, "==", c(1:12,0)))+0
MD <- MD[,-13]
dimnames(MD) <- list(NULL, c("Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Noe", "Dec"))
> head(MD)
      Jan Feb Mar Apr May June July Aug Sep Oct Noe Dec
[1,]   1   0   0   0   0    0    0   0   0   0   0   0
[2,]   1   0   0   0   0    0    0   0   0   0   0   0

So, I've created a monthly dummy and I want to transform the matrix to a seasonal matrix. This is how the seasons should be defined:

month.list <- list(c(3,4,5), c(6,7,8), c(9,10,11), c(12,1,2))

I have thought of merging the columns of the months, but I have struggled so far. I would be really thankful, if someone could help.

M--
  • 25,431
  • 8
  • 61
  • 93
hoppe_pr
  • 11
  • 1
  • 3
  • Please read [How to make a great reproducible example in R?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) So I can test the solution on your data; – M-- Jun 16 '17 at 15:55

1 Answers1

2

You can use zoo package:

library(zoo)
yq <- as.yearqtr(as.yearmon(DK.days) + 1/12) #+1/12 to shift one month
DK.seasons <- factor(format(yq, "%q"), levels = 1:4,
                     labels = c("winter", "spring", "summer", "fall"))

Example:

 #make an example data-set 
 DK.days <- c("2016-01-01","2016-02-29","2016-03-02","2016-04-03",
              "2016-05-04","2016-06-05","2016-07-06","2016-08-10",
              "2016-09-06","2016-10-06","2016-11-06","2016-12-06")
 library(anytime)
 DK.Days <- anytime(DK.days)

 library(zoo)
 yq <- as.yearqtr(as.yearmon(DK.days) + 1/12) #+1/12 to shift one month
 DK.seasons <- factor(format(yq, "%q"), levels = 1:4,
                 labels = c("winter", "spring", "summer", "fall"))

 DK.final <- data.frame(cbind(DK.days, data.frame(DK.seasons)))

This would be the output:

> DK.final

#       DK.days DK.seasons 
# 1  2016-01-01     winter 
# 2  2016-02-29     winter 
# 3  2016-03-02     spring 
# 4  2016-04-03     spring 
# 5  2016-05-04     spring 
# 6  2016-06-05     summer 
# 7  2016-07-06     summer 
# 8  2016-08-10     summer 
# 9  2016-09-06       fall 
# 10 2016-10-06       fall 
# 11 2016-11-06       fall 
# 12 2016-12-06     winter

Make Dummy Variables:

If you want columns as dummy variables for each season then use mlr package:

library(mlr)
DK.final <- cbind(DK.final,createDummyFeatures(DK.final[,2], cols = "var"))

This will give you:

> DK.final

#       DK.days DK.seasons winter spring summer fall 
# 1  2016-01-01     winter      1      0      0    0 
# 2  2016-02-29     winter      1      0      0    0 
# 3  2016-03-02     spring      0      1      0    0 
# 4  2016-04-03     spring      0      1      0    0 
# 5  2016-05-04     spring      0      1      0    0 
# 6  2016-06-05     summer      0      0      1    0 
# 7  2016-07-06     summer      0      0      1    0 
# 8  2016-08-10     summer      0      0      1    0 
# 9  2016-09-06       fall      0      0      0    1 
# 10 2016-10-06       fall      0      0      0    1 
# 11 2016-11-06       fall      0      0      0    1 
# 12 2016-12-06     winter      1      0      0    0
M--
  • 25,431
  • 8
  • 61
  • 93
  • Hey, thanks for the answer, but I need a matrix with four columns - one for every season. The matrix should include ones and zeros - one, if the date belongs to the season - for example in the row "01-01-2007" I need to have one in the first column("Winter) and zeros in the other three. If you need some additional information about the dataset, just tell me what I should provide. – hoppe_pr Jun 16 '17 at 17:55