Reputation: 61
I have an issue with transforming a data frame into a time series object.
The data frame is olympic
from fpp
package.
library(fpp)
olympic
How do I transform this data frame having a Year column with gaps into a time series to be used with fpp
package for forecasting?
I tried
olympic_new <- ts(olympic[,-1], start=1896, end=1996, deltat=4)
but this does not work because the olympics
during the war had a gap longer than 4 years.
How can I use the Year
column in the olympic
to be understood as the index of the new ts
object?
My end goal is to use a forecasting function on the new object, such as:
meanf(olympic_new, h=4)
Upvotes: 2
Views: 583
Reputation: 73325
If you use dput(olympic)
and copy it in your question, it would be easier for us.
olympic <-
structure(list(Year = c(1896L, 1900L, 1904L, 1908L, 1912L, 1920L,
1924L, 1928L, 1932L, 1936L, 1948L, 1952L, 1956L, 1960L, 1964L,
1968L, 1972L, 1976L, 1980L, 1984L, 1988L, 1992L, 1996L), time = c(54.2,
49.4, 49.2, 50, 48.2, 49.6, 47.6, 47.8, 46.2, 46.5, 46.2, 45.9,
46.7, 44.9, 45.1, 43.8, 44.66, 44.26, 44.6, 44.27, 43.87, 43.5,
43.49)), .Names = c("Year", "time"), class = "data.frame", row.names = c(NA,
-23L))
You just pad some NA
where it is needed.
year <- seq.int(1896L, 1996L, by = 4L)
y <- rep.int(NA, length(year))
y[match(olympic$Year, year)] <- olympic$time
olympic_new <- ts(y, start = 1896, end = 1996, deltat = 4)
#Time Series:
#Start = 1896
#End = 1996
#Frequency = 0.25
# [1] 54.20 49.40 49.20 50.00 48.20 NA 49.60 47.60 47.80 46.20 46.50 NA
#[13] NA 46.20 45.90 46.70 44.90 45.10 43.80 44.66 44.26 44.60 44.27 43.87
#[25] 43.50 43.49
Then
meanf(olympic_new,h=4)
# Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
#2000 46.51957 42.93628 50.10285 40.91319 52.12594
#2004 46.51957 42.93628 50.10285 40.91319 52.12594
#2008 46.51957 42.93628 50.10285 40.91319 52.12594
#2012 46.51957 42.93628 50.10285 40.91319 52.12594
Upvotes: 2