如何创建季节性假人

时间:2017-06-16 15:47:46

标签: r time-series dummy-variable

我希望通过季节性假人将线性模型拟合到电价中。所以" DK.days"包含每年10年的日子。

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

这是代码的其余部分。

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

所以,我创建了一个月度假人,我想将矩阵转换为季节矩阵。这就是季节的定义方式:

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

我想过合并几个月的专栏,但到目前为止我一直在努力。如果有人可以提供帮助,我会非常感激。

1 个答案:

答案 0 :(得分:1)

您可以使用zoo包:

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"))

实施例

 #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)))

这将是输出:

> 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

制作虚拟变量:

如果您希望将列作为每个季节的虚拟变量,请使用mlr包:

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

这会给你:

> 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