Forecasting existing time series data - r

I have a data frame with time series data, called rData. The data is distributed into quarters and there is four years of data available. I analyzed the data and fitted an ARIMA model to the series, now I can compute forecasting for the periods to follow. But I wish to create a new column in my data frame that displays the forecast value that corresponds to the available time stamp. Then I wish to plot the two graphs against each other in R. Is their a way to compute these forecast values in R without individually analyzing all of the data prior to the available time stamp. Also how many cycles of data is necessary before forecasting can be computed?
Date <- seq(as.Date("2000-01-01"), as.Date("2003-12-31"), by = "quarter")
Sales <- c(2.8,2.1,4,4.5,3.8,3.2,4.8,5.4,4,3.6,5.5,5.8,4.3,3.9,6,6.4)
rData <- data.frame(Date, Sales)
tsData <- ts(data = rData$Sales, start = c(2000, 1), frequency = 4)
> tsExcelData
Qtr1 Qtr2 Qtr3 Qtr4
2000 2.8 2.1 4.0 4.5
2001 3.8 3.2 4.8 5.4
2002 4.0 3.6 5.5 5.8
2003 4.3 3.9 6.0 6.4
myModel <- auto.arima(tsData)
myForcast <- forecast(myModel, level = 95, h = 8)
The end result should be a data frame with an additional column and a graph with to plots, one for the actual data and one for the forecast data. Something like this.
Actual Data vs Forecast Data:

did you mean something like this, for the past values? If so just add this to your code:
extract_fitted_values <- myModel$fitted
plot(tsData, xlab = "Time", ylab = "Sales", type = "b", pch = 19)
lines(extract_fitted_values, col = "red")
As you see, you can extract the fitted values from the model fit.
Regarding your question: the data prior the time for the forecast IS actually analyzed when you run the auto.arima model.
That is how the Arima model estimates the parameters (by using past data) and then proceeds to do the forecasts. It is just that with the auto-arima function it (in addition) chooses the model specification automatically.
So basically the prior data analysis is a pre-requisite for the subsequent forecasts. It is worth noting that the red line that you see here represents the fitted values, i.e. your model is using all the data-points up to the last time point to calculate them and produce the numbers.
Maybe see more here if that point is a bit unclear:
https://stats.stackexchange.com/questions/260899/what-is-difference-between-in-sample-and-out-of-sample-forecasts
If you wanted to do "out-of-sample" forecasts for the past data (2000-2004) then this is also possible, but you would just need to fit, say on 2000-2002, produce a forecast for 1 step, then roll 1 quarter forward and repeat the same etc. etc.

If you want them into a data.frame and plot the real values vs the fitted + the predicted, you can try this:
df <- data.frame( # your data and some NAs, for the forecasting
real = c(tsData, rep(NA,length(data.frame(myForcast)$Point.Forecast )))
# in a vector the fitted and the predicted
, pred = c(myModel$fitted, data.frame(myForcast)$Point.Forecast)
# the time for the plot
, time = c(time(tsData), seq(2004,2005.75, by = 0.25)
))
plot(df$real, xlab = "time", ylab = "real black, pred red", type = "b", pch = 19,xaxt="n")
lines(df$pred, col = "red")
axis(1, at=1:24, labels=df$time)
For the theory part, as already said, the fitted values are calculated when you run your model. Running the model is the base for the forecasting, but you can have the fitted without forecasting of course.

Related

Problems when adjusting time series for seasonality with seas function in R

I have an issue when I try to adjust my quarterly time series dataset for seasonality in R. I have loaded in the dataset 'ASPUS' to R and specified it's date by using the following code:
ASPUS <- read.csv(file = "ASPUS.csv", header=TRUE, sep=",")
ASPUS$DATE <- as.Date(ASPUS$DATE, "%Y-%m-%d")
The head of the dataset looks like this:
DATE ASPUS
1 1963-01-01 100.00000
2 1963-04-01 100.51813
3 1963-07-01 99.48187
4 1963-10-01 101.55440
5 1964-01-01 101.55440
The purpose of the dataset is to analyze it as a Time Series. Therefore, I use the ts function to create a time series object:
ASPUSts <- ts(ASPUS, frequency = 4, start = 1963)
However, this function returns negative numbers within the date column like this:
DATE ASPUS
1963 Q1 -2557 100.00000
1963 Q2 -2467 100.51813
1963 Q3 -2376 99.48187
1963 Q4 -2284 101.55440
1964 Q1 -2192 101.55440
1964 Q2 -2101 104.66321
My problem then occurs in the next step, where I try to adjust for seasonality with the function seas:
ASPUS1 <- seas(ASPUSts)
Because I get this error:
Error: X-13 run failed
Errors:
- Seasonal MA polynomial with initial parameters is
noninvertible with root(s) inside the unit circle. RESPECIFY
model with different initial parameters.
Warnings:
- Automatic transformation selection cannot be done on a series
with zero or negative values.
- The covariance matrix of the ARMA parameters is singular, so
the standard errors and the correlation matrix of the ARMA
parameters will not be printed out.
- The covariance matrix of the ARMA parameters is singular, so
the standard errors and the correlation matrix of the ARMA
parameters will not be printed out.
Does anyone have any suggestions on how I can deal with these negative values or otherwise solve the problem so that I can seasonally adjust my dataset?
According to ?ts:
Description:
The function ‘ts’ is used to create time-series objects.
‘as.ts’ and ‘is.ts’ coerce an object to a time-series and test
whether an object is a time series.
Usage:
ts(data = NA, start = 1, end = numeric(), frequency = 1,
deltat = 1, ts.eps = getOption("ts.eps"), class = , names = )
as.ts(x, ...)
is.ts(x)
Arguments:
data: a vector or matrix of the observed time-series values. A data
frame will be coerced to a numeric matrix via ‘data.matrix’.
(See also ‘Details’.)
Here, the key thing to note is: "data: a vector or matrix of the observed time-series values". I.e. you are not expected to enter the date vector as an input to ts.
This is the correct syntax for ts.
ASPUSts <- ts(ASPUS$ASPUS, frequency = 4, start = 1963)
In your example, seas is actually trying to seasonally adjust the time series of dates that get transformed to -2557, -2467, ... (side note: internally R counts dates as differences from 1970-01-01; that's why dates in the '60s appear as negative numbers).

Time series prediction with and without NAs (ARIMA and Forecast package) in R

This is my first question on stack overflow.
Situation: I have 2 time series. Both series have the same values but the second series has 5 NAs at the start. Hence, first series has 105 observations, where 2nd series has 110 observations. I have fitted an ARIMA(0,1,0) using the Arima function to both series separately. And then I used the forecast package to predict 10 steps to the future.
Issue: Even though the ARIMA coefficient for both series are the same, the projections (10 steps) appear to be different. I am uncertain why this is the case. Has anyone come across this before? Any guidance is highly appreciated.
Tried: I tried setting seed, creating index manually, and using auto.ARIMA for the model fitting. However, none of the steps has helped me to reconcile the difference.
I have added a picture to show you what I see. Please note I have hidden the mid part of the series so that you can see the start and the end of the series. The yellow highlighted cells are the projection outputs from the 'Forecast' package. I have manually added the index to be years after extracting the results from R.
Time series projected and base in excel
Rates <- read.csv("Rates_for_ARIMA.csv")
set.seed(123)
#ARIMA with NA
Simple_Arima <- Arima(
ts(Rates$Rates1),
order = c(0,1,0),
include.drift = TRUE)
fcasted_Arima <- forecast(Simple_Arima, h = 10)
fcasted_Arima$mean
#ARIMA Without NA
Rates2 <- as.data.frame(Rates$Rates2)
##Remove the final spaces from the CSV
Rates2 <- Rates2[-c(106,107,108,109,110),]
Simple_Arima2 <- Arima(
ts(Rates2),
order = c(0,1,0),
include.drift = TRUE)
fcasted_Arima2 <- forecast(Simple_Arima2, h = 10)
fcasted_Arima2$mean
The link to data is here, CSV format
Could you share your data and code such that others can see if there is any issue with it?
I tried to come up with an example and got the same results for both series, one that includes NAs and one that doesn't.
library(forecast)
library(xts)
set.seed(123)
ts1 <- arima.sim(model = list(0, 1, 0), n = 105)
ts2 <- ts(c(rep(NA, 5), ts1), start = 1)
fit1 <- forecast::Arima(ts1, order = c(0, 1, 0))
fit2 <- forecast::Arima(ts2, order = c(0, 1, 0))
pred1 <- forecast::forecast(fit1, 10)
pred2 <- forecast::forecast(fit2, 10)
forecast::autoplot(pred1)
forecast::autoplot(pred2)
> all.equal(as.numeric(pred1$mean), as.numeric(pred2$mean))
[1] TRUE

Weekly and Yearly Seasonality in R

I have daily electric load data from 1-1-2007 till 31-12-2016. I use ts() function to load the data like so
ts_load <- ts(data, start = c(2007,1), end = c(2016,12),frequency = 365)
I want to remove the yearly and weekly seasonality from my data, to decompose the data and remove the seasonality, I use the following code
decompose_load = decompose(ts_load, "additive")
deseasonalized = ts_load - decompose_load$seasonal
My question is, am I doing it right? is this the right way to remove the yearly seasonality? and what is the right way to remove the weekly seasonality?
A few points:
a ts series must have regularly spaced points and the same number of points in each cycle. In the question a frequency of 365 is specified but some years, i.e. leap years, would have 366 points. In particular, if you want the frequency to be a year then you can't use daily or weekly data without adjustment since different years have different numbers of days and the number of weeks in a year is not integer.
decompose does not handle multiple seasonalities. If by weekly you mean remove the effect of Monday, of Tuesday, etc. and if by yearly you mean remove the effect of being 1st of the year, 2nd of the year, etc. then you are asking for multiple seasonalities.
end = c(2017, 12) means the 12th day of 2017 since frequency is 365.
The msts function in the forecast package can handle multiple and non-integer seasonalities.
Staying with base R, another approach is to approximate it by a linear model avoiding all the above problems (but ignoring correlations) and we will discuss that.
Assuming the data shown reproducibly in the Note at the end we define the day of week, dow, and day of year, doy, variables and regress on those with an intercept and trend and then construct just the intercept plus trend plus residuals in the last line of code to deseasonalize. This isn't absolutely necessary but we have used scale to remove the mean of trend in order that the three terms defining data.ds are mutually orthogonal -- Whether or not we do this the third term will be orthogonal to the other 2 by the properties of linear models.
trend <- scale(seq_along(d), TRUE, FALSE)
dow <- format(d, "%a")
doy <- format(d, "%j")
fm <- lm(data ~ trend + dow + doy)
data.ds <- coef(fm)[1] + coef(fm)[2] * trend + resid(fm)
Note
Test data used in reproducible form:
set.seed(123)
d <- seq(as.Date("2007-01-01"), as.Date("2016-12-31"), "day")
n <- length(d)
trend <- 1:n
seas_week <- rep(1:7, length = n)
seas_year <- rep(1:365, length = n)
noise <- rnorm(n)
data <- trend + seas_week + seas_year + noise
you can use the dsa function in the dsa package to adjust a daily time series. The advantage over the regression solution is, that it takes into account that the impact of the season can change over time, which is usually the case.
In order to use that function, your data should be in the xts format (from the xts package). Because in that case the leap year is not ignored.
The code will then look something like this:
install.packages(c("xts", "dsa"))
data = rnorm(365.25*10, 100, 1)
data_xts <- xts::xts(data, seq.Date(as.Date("2007-01-01"), by="days", length.out = length(data)))
sa = dsa::dsa(data_xts, fourier_number = 24)
# the fourier_number is used to model monthly recurring seasonal patterns in the regARIMA part
data_adjusted <- sa$output[,1]

STL ts frequency = 1

I am using the stats::stl function for first time in order to identify and delete the tecnological signal of a crop yields serie. I am not familiar with this method and I am a newbie on programming, in advance I apologize for any mistaken.
These are the original data I am working with:
dat <- data.frame(year= seq(1962,2014,1),yields=c(1100,1040,1130,1174,1250,1350,1450,1226,1070,1474,1526,1719,1849,1766,1342,2000,1750,1750,2270,1550,1220,2400,2750,3200,2125,3125,3737,2297,3665,2859,3574,4519,3616,3247,3624,2964,4326,4321,4219,2818,4052,3770,4170,2854,3598,4767,4657,3564,4340,4573,3834,4700,4168))
This is the ts with frequency =1 (annual) created as input for STL function:
time.series <- ts(data=dat$yields, frequency = 1, start=c(1962, 1), end=c(2014, 1))
plot(time.series, xlab="Years", ylab="Kg/ha", main="Crop yields")
When I try to run the function I get the following error message:
decomposed <- stl(time.series, s.window='periodic')
> Error in stl(time.series, s.window = "periodic") : series is not periodic or has less than two periods
I know that my serie is annual and therefore I can not vary the frequency in the ts which it is seems what causes the error because when I change the frequency I get the seasonal, trend and remainder signals:
time.series <- ts(data=dat$yields, frequency = 12, start=c(1962, 1), end=c(2014, 1))
decomposed <- stl(time.series, s.window='periodic')
plot(decomposed)
I would like to know if there is a method to apply STL function with annual data with a frequency of observation per unit of time = 1.
On the other hand, to remove the tecnological signal, it is only necessary to obviate the trend and remainder signal from the original serie or I am mistaken?
Many thanks for your help.
Since your using annual data, there is no seasonal component, therefore seasonal decomposition of time series would not be appropriate. However, the stats::stl function calls the loess function to estimate trend, which is a local polynomial regression you can adjust to your liking. You can call loess directly and estimate your own trend as followings.
dat <- data.frame(year= seq(1962,2014,1),yields=c(1100,1040,1130,1174,1250,1350,1450,1226,1070,1474,1526,1719,1849,1766,1342,2000,1750,1750,2270,1550,1220,2400,2750,3200,2125,3125,3737,2297,3665,2859,3574,4519,3616,3247,3624,2964,4326,4321,4219,2818,4052,3770,4170,2854,3598,4767,4657,3564,4340,4573,3834,4700,4168))
dat$trend <- loess(yields ~ year, data = dat)$fitted
plot(y = dat$yields, x = dat$year, type = "l", xlab="Years", ylab="Kg/ha", main="Crop yields")
lines(y = dat$trend, x = dat$year, col = "blue", type = "l")

ARIMA forecasts with R - how to update data

I've been trying to develop an ARIMA model to forecast wind speed values. I have a four year data series (from january 2008 until december 2011). The series presents 10 minute data, which means that in a day we have 144 observations. Well, I'm using the first three years (observations 1 to 157157) to generate the model and the last year to validate the model.
The thing is I want to update the forecast. On other words, when one forecast ends up, more data is added to the dataset and another forecast is performed. But the result seems like I had just lagged the original series. Here's the code:
#1 - Load data:
z=read.csv('D:/Faculdade/Mestrado/Dissertação/velocidade/tudo_10m.csv', header=T, dec=".")
vel=ts(z, start=c(2008,1), frequency=52000)
# 5 - ARIMA Forecasts:
library(forecast)
n=157157
while(n<=157200){
amostra <- vel[1:n] # Only data until 2010
pred <- auto.arima(amostra, seasonal=TRUE,
ic="aicc", stepwise=FALSE, trace=TRUE,
approximation=TRUE, xreg=NULL,
test="adf",
allowdrift=TRUE, lambda=NULL, parallel=TRUE, num.cores=4)
velpred <- arima(pred) # Is this step really necessary?
velpred
predvel<- forecast(pred, h=12) # h means the forecast steps ahead
predvel
plot(amostra, xlim=c(157158, n), ylim=c(0,20), col="blue", main="Previsões e Observações", type="l", lty=1)
lines(fitted(predvel), xlim=c(157158, n), ylim=c(0,20), col="red", lty=2)
n=n+12
}
But when it plot the results (I couldn't post the picture here), it exhibits the observed series and the forecasted plot, which seems just the same as the observed series, but one step lagged.
Can anyone help me examining my code and/or giving tips on how to get the best of my model? Thanks! (Hope my English is understandable...)

Resources