[英]Add months of zero demand to zoo time series
I have some intermittent demand data that only includes lines where demand is present. 我有一些间歇性需求数据,只包括需求存在的行。 I bring it in via read.csv, and my 2 columns are Date (as date) and Quantity (as integer).
我通过read.csv把它带进来,我的2列是Date(作为日期)和Quantity(作为整数)。 Then I convert it to a zoo series and combine the daily demand into monthly demand.
然后我将它转换为动物园系列,并将每日需求与月需求结合起来。 My final output is a zoo series with the date being the first day of the month and the summed demand for that month.
我的最终输出是动物园系列,其中日期是该月的第一天以及该月的总需求。
My problem is that this zoo series is missing the in between months that have zero demand and I need these to forecast intermittent demand correctly. 我的问题是这个动物园系列在缺少需求零的月份之间缺少,我需要这些来正确预测间歇性需求。
For example: I have quantity 2 in date 2013-01-01 and then the next line is quantity 3 in 2013-10-01. 例如:我在2013-01-01日期有数量2,然后在2013-10-01中下一行是数量3。 I need to add quantity zero to 2013-02-01 through 2013-09-01.
我需要将数量零添加到2013-02-01到2013-09-01。
Date <- c('1/1/2013','10/1/2013','11/1/2013')
Quantity <- c('2','3','6')
Date <- as.Date(Date, "%m/%d/%Y")
df <- data.frame(Date, Quantity)
df <- read.zoo(df)
df
The zoo series output: 动物园系列输出:
2013-01-01 2013-10-01 2013-11-01
2 3 6
Because "df" is a zoo
object, you may use merge.zoo
and its fill
argument. 因为“df”是一个
zoo
对象,所以可以使用merge.zoo
及其fill
参数。 The current data set is merged with an empty zoo
object which contains all the desired dates. 当前数据集与包含所有所需日期的空
zoo
对象合并。
tt <- seq(min(Date), max(Date), "month")
merge(df, zoo(, tt), fill = 0)
# 2013-01-01 2013-02-01 2013-03-01 2013-04-01 2013-05-01 2013-06-01 2013-07-01 2013-08-01 2013-09-01 2013-10-01 2013-11-01
# 2 0 0 0 0 0 0 0 0 3 6
For further examples, see ?merge.zoo
("extend an irregular series to a regular one"). 有关更多示例,请参阅
?merge.zoo
(“将不规则系列扩展为常规系列”)。
You can use merge
to add the missing rows and then set their values to zero. 您可以使用
merge
添加缺少的行,然后将其值设置为零。
First, let's create some fake data: 首先,让我们创建一些假数据:
# Vector of dates from Jan 1, 2015, to Mar 31, 2015
dates = seq(as.Date("2015-01-01"), as.Date("2015-03-31"), by="1 day")
# Let's create data for few of these dates, leaving some out
set.seed(55)
dat = data.frame(dates=dates[sample(1:length(dates), 70)],
quantity=sample(1:10, 70, replace=TRUE))
dat = dat[order(dat$dates),]
Now let's make believe dat
is what you imported from a csv file. 现在让我们相信
dat
是您从csv文件导入的内容。 We want to fill in quantity
=0 for the missing dates. 我们想要为缺少的日期填写
quantity
= 0。 So first we need to add rows for the missing dates. 首先,我们需要为缺少的日期添加行。 You can do this by creating a date vector containing all dates from the first date to the last date in your csv file and using the
merge
function. 您可以通过创建包含csv文件中第一个日期到最后一个日期的所有日期并使用
merge
功能的日期向量来完成此操作。 In this case, we've already created that date vector above. 在这种情况下,我们已经在上面创建了日期向量。
Now merge in rows for the missing dates. 现在合并为缺少日期的行。 The new rows will have
NA
for quantity
. 新行的
quantity
将为NA
。 We'll change those NA
s to zero below. 我们将下面的
NA
更改为零。
dat = merge(data.frame(dates), dat, by="dates", all.x=TRUE)
# Set missing values to zero
dat$quantity[is.na(dat$quantity)] = 0
Now you can aggregate by month, convert to a zoo
series, etc. 现在您可以按月汇总,转换为
zoo
系列等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.