Reputation: 151
I am working with bunch of different file sets that data is monthly but they are all different months. Some of them include past 2 years of monthly data and some of them has couple months of monthly data.
I like use this data to create forecast graphs. If all data was consistent, it will be straight forward. But the data is monthly and each file has different start and end times.
z output:
Month Value
1 2010-01-01 100
2 2010-02-01 200
3 2010-03-01 600
4 2010-04-01 400
5 2010-05-01 300
6 2010-06-01 700
I tried to do this to set the start to s object, but is not working
s <- head(z,1)$Month
s <- sub("-", "," s)
t.ser <- ts(z$Value, start=s,freq=12)
Since the data that I am working with all varies, I wont know the start date.
This works but, Since I dont know the start date of my data set, can I put a variable in start?
t.ser <- ts(z$Value, start=c(2010,1),freq=12)
t.ets <- ets(t.ser)
t.fc <- forecast(t.ets,h=12)
plot(t.fc, xaxt="n")
I would like to format the x-axis to show the dates of the actual data and dates of the forecasted values. Since I wont know the start date and I cannot do this either. I'd like to forecast out 12 months regardless of the data.
This works if I know the start time.
a = seq(as.Date("2011-01-01"), by="month", length=36)
axis(1, at = decimal_date(a), labels = format(a, "%Y %b %d"), cex.axis=0.6)
When I do this:
t.ser <- ts(z$Value/1000000, start(z[1,1]),freq=12)
I get
t.ser
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1 45 51 56 56 59 60 60 60 64 65 75 73
2 74 80 87 91 92 96 109 108 123 129 133 143
3 127 127 123 121 130
But when I do this:
t.ser <- ts(z$Value/1000000, start=c(2010,1),freq=12)
I get this back, this is the result I want when using variable in start, rather than explicitly putting the date. Anyone has any idea, how would I accomplish this?
t.ser
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2010 45 51 56 56 59 60 60 60 64 65 75 73
2011 74 80 87 91 92 96 109 108 123 129 133 143
2012 127 127 123 121 130
Upvotes: 0
Views: 268
Reputation: 270195
Try this (specify something like file = "mydata.dat"
in place of text=Lines
if your data comes from a file and modify appropriately if that is not the format of your data):
Lines <- "Month Value
1 2010-01-01 100
2 2010-02-01 200
3 2010-03-01 600
4 2010-04-01 400
5 2010-05-01 300
6 2010-06-01 700"
library(zoo)
z <- read.zoo(text = Lines, header = TRUE, FUN = as.yearmon)
t.ser <- as.ts(z)
This gives:
> t.ser
Jan Feb Mar Apr May Jun
2010 100 200 600 400 300 700
Upvotes: 1