在用户定义功能中定义假日周末指示变量

时间:2013-10-11 21:39:00

标签: r datetime user-defined-functions

我正在尝试创建一个指标变量,该变量将在数据集中采用日期字段并创建一个假日变量,该假日变量将1分配给实际假日日期,并将1分配给假日前的周末。我越来越接近,但我似乎无法超越我的新手思考这个问题的方法 对于完整的上下文,我已经给出了假数据集的代码和我正在处理的用户定义函数。我有一个早期的帖子具有相同的功能,但一个完全不同的问题。 我的问题:我如何在用户定义函数中创建一个指标变量,该函数可以读取节假日和假期前的周末,并为那些天分配1,为非假期/假日周末分配0?

创建响应和按日期添加的基本数据集

library(timeDate)
library(lubridate)
library(forecast)
library(plyr)


# setting up some fake data
set.seed(31)
foo <- function(myHour, myDate){
   rlnorm(1, meanlog=0,sdlog=1)*(myHour) + (150*myDate) 
}
Hour <- 1:24
Day <-1:1080
dates <-seq(as.Date("2010-01-01"), by = "day", length.out= 1080)
myData <- expand.grid( Day, Hour)
names(myData) <- c("Date","Hour")

myData$Adspend <- apply(myData, 1, function(x) foo(x[2], x[1]))
myData$Date <-dates

myData$Demand <-(rnorm(1,mean = 0, sd=1)+.75*myData$Adspend)

myData$Hour<-as.factor(myData$Hour)
AddCal <-function(DF,Date,Time,Seasonal=TRUE, Holiday=TRUE, Intraday = TRUE){
#Create variables of calendar effects from Date field
DF$Date<-as.Date(DF[[Date]], format="%m/%d/%Y")
DF[[Time]]<-factor(DF[[Time]], levels = c(1:24))
monthly <- months(DF[[Date]])
dow <-weekdays(DF[[Date]])
year1<-year(DF[[Date]])
quarter<-quarters(DF[[Date]])

为协变量创建季节性指标矩阵

hmatx <- model.matrix(~as.factor(DF[[Time]]))[,2:24] # Matrix of hours
mmatx <- model.matrix(~as.factor(monthly))[,2:12] #Matrix of months
dmatx <- model.matrix(~as.factor(dow))[,2:7] #matrix of days of week
qmatx<-model.matrix(~as.factor(dow))[,1:3] #matrix of Quarters of the year

如果在假日的2天内标记假日和周末,则创建假日指示变量

LaborWkend<-ifelse(isWeekend(as.Date(USLaborDay(year1)+2)),1,0)

1 个答案:

答案 0 :(得分:1)

这是一个简单的函数,它将采用一系列日期并将最近的周末附加到每个日期,除非星期三有日期。

plusWeekends<-function(h){
  h<-as.Date(h)
  w<-as.POSIXlt(h)$wday 
  sort(unique(c(h,h[w %in% 0:2]-1,
        h[w %in% 1:2]-2,
        h[w == 2]-3,
        h[w ==4]+3,
        h[w %in% 4:5]+2,
        h[w %in% 4:6]+1)))
}

例如:

> plusWeekends(USLaborDay(2010:2012))
[1] "2010-09-04" "2010-09-05" "2010-09-06" "2011-09-03" "2011-09-04" "2011-09-05"
[7] "2012-09-01" "2012-09-02" "2012-09-03"
> plusWeekends(NewYearsDay(2010:2012))
[1] "2010-01-01" "2010-01-02" "2010-01-03" "2011-01-01" "2011-01-02" "2011-12-31"
[7] "2012-01-01"

要创建指标,您可以运行类似

的内容
> indicator<-rep(0,length(dates))
> indicator[dates %in% plusWeekends(USLaborDay(2010:2012))]<-1
> dates[indicator==1]
[1] "2010-09-04" "2010-09-05" "2010-09-06" "2011-09-03" "2011-09-04" "2011-09-05"
[7] "2012-09-01" "2012-09-02" "2012-09-03"