1

我正在尝试创建一个指示变量,它将获取数据集中的日期字段并创建一个假期变量,将 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)
4

1 回答 1

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"
于 2013-10-11T22:08:52.563 回答