2017年3月23日木曜日

mondate and Calculate date by months

mondate and Calculate date by months

When you have to generate date sequence from 2017 march and other 9 following months, you can do as below.

seq(as.Date("2017-03-01"),as.Date("2017-12-01"),by="months")

If you like to do it in an arithmetic way. use mondate. you also need as.Date to make the generated sequence to use as as.xts argument.

as.Date(seq(mondate("2017-03-01"), by=1, length.out=10)))

below will create xts format data with forecast by auto regression of PAYEMS from 2017 MAR. to DEC. Don't forget to put [1:10] at the end of the first argument.

as.xts(forecast(auto.arima(PA),h=10)$mean[1:10],as.Date(seq(mondate("2017-03-01"), by=1, length.out=10)))

see below for output.

[,1]
2017-03-01 145989.9
2017-04-01 146182.0
<SKIP>
2017-11-01 147361.8
2017-12-01 147513.5


Without index number [1:10], it will end as an error like below.

> as.xts(forecast(auto.arima(PA),h=10)$mean,as.Date(seq(mondate("2017-03-01"), by=1, length.out=10)))
Error in do.call(paste("as", dateFormat, sep = "."), list(as.numeric(time(x)),  :
  'what' must be a function or character string

0 件のコメント: