pApaAPPApapapa
pApaAPPApapapa

Reputation: 385

Making a date vector start from specific date and go n units forward

In order to use ggplot to work with time-series I need to take a bunch of forecasted values and put them into a dataframe together with the corresponding dates.

I have used the following code to declare my initial vector a ts

name <- ts(name, frequency=12, start=c(2007,1))

I then fit the model using

fit <- auto.arima (name)

and extract prediction and prediction intervals as follows

name.for <- forecast(fit, 15)
name.for.low <- name.for$lower[,2]
name.for.up <- name.for$upper[,2]

I know hope to make a vector of dates, which starts where the time-series declared above ends and go forward n months

I guess I can use something like:

seq(as.Date('X'), length.out=n, by='1 month')

However, I want X to be dynamic, and be calculated from the time-series vector above

Upvotes: 4

Views: 65

Answers (2)

R.S.
R.S.

Reputation: 2140

Does this work?

ts2<- ts(1:28, frequency=12, start=last(time(name )) )
as.Date(ts2)

Edit: if you want it to start from next month then add the fractional frequency to start ts2<- ts(1:28, frequency=12, start=last(time(name )+1*deltat(name)) )

Upvotes: 0

Sandipan Dey
Sandipan Dey

Reputation: 23099

Try this:

library(lubridate)
jan31 <- ymd("2016-01-31")
n <- 4
jan31 %m+% months(n)
#[1] "2016-05-31"
n <- 5
jan31 %m+% months(n)
#[1] "2016-06-30"

Upvotes: 2

Related Questions