I'm a beginner learning time series analysis.
I want to clean up the data I will be using for ARIMA. However, tsclean gives me an error message:
("Error in stl(xx, s.window = "periodic", robust = TRUE) : only univariate series are allowed")
And I don't understand why.
My code looks like that:
read.csv("FILENAME.csv",header=TRUE)
mydata<-read.csv("FILENAME.csv")
mydata2<-ts(mydata,freq=12,start=c(2011,1))
class(mydata2) # the result here is "ts"
mydataclean<-tsclean(mydata2) # the result is "Error in stl(xx, s.window = "periodic", robust = TRUE) : only univariate series are allowed"
The file FILENAME.csv is composed of just one column with data. The data is for full 7 years monthly.
Related
summary function in R doesn't print the Error Measure values and accuracy function throws error. I don't understand why. Anyone please help me to understand.
Below is the error code:
> summary(forecast1)
Length Class Mode
pred 12 ts numeric
se 12 ts numeric
> accuracy(forecast1)
Error in accuracy.default(forecast1) :
First argument should be a forecast object or a time series.
I am trying to forecast hourly sales based on past years of data, display the plot of the forecast with x axis SaleDateTime, and check the accuracy against a test set of dates. I keep running in to errors.
I tried using dput to generate a small sample of data but for some reason it still tries to output many more dates then I have in the subset sample data.
My data looks like this: SaleDateTime = "2015-01-02 23:00:00.000" and SaleCount = "1".
It looks like my main issue is with how I'm trying to partition the data into training and test sets.
Also I would like to x axis on the plot to have the form "2015-03-01 23:00:00". I'm pretty new to forecasting so all help is very much appreciated.
Code:
library("forecast")
library("zoo")
SampleData <- read.csv("SampleDataAll.csv")
Value<-SampleData[,c("SaleDateTime","SaleCount")]
rDateTime<-as.POSIXct(SampleData$SaleDateTime, format="%Y-%m-%d %H:%M:%S")
eventdata <- zoo(Value, order.by = rDateTime)
timeseries <- ts(eventdata$SaleCount, frequency=24)
##Partitioning data Training/Testing
ts1Train <- window(timeseries,start="2011-08-01 00:00:00", end="2014-08-01 00:00:00")
Error:
Error in window.default(x, ...) : 'start' cannot be after 'end'
In addition: Warning message:
In window.default(x, ...) : 'end' value not changed
ts1Test <- window(timeseries,start="2014-08-01 01:00:00", end="2015-08-01 00:00:00")
Error in window.default(x, ...) : 'start' cannot be after 'end'
In addition: Warning message:
In window.default(x, ...) : 'end' value not changed
fcast2<-forecast(ts1Train,h=8764)
Error:
Error in forecast(ts1Train, h = 8764) : object 'ts1Train' not found
plot(fcast2)
accuracy(fcast2,ts1Test)
Error:
Error in frequency(x) : object 'ts1Test' not found
UPDATE:
I made the changes below to how I partition the training and testing data as per the suggestion. Now I'm getting the error message below when I try to run accuracy on the ts1Test data.
New Code:
library("forecast")
library("zoo")
SampleData<-SampleData
Value<-SampleData[,c("SaleDateTime","SaleCount")]
rDateTime<-as.POSIXct(SampleData$SaleDateTime, format="%Y-%m-%d %H:%M:%S")
eventdata <- zoo(Value, order.by = rDateTime)
##Partitioning data Training/Testing
ts1SampleTrain<-eventdata[1:2000,]
ts1Train<-ts(ts1SampleTrain$SaleCount, frequency=24)
ts1SampleTest<-eventdata[2001:28567,]
ts1Test<-ts(ts1SampleTest$SaleCount, frequency=24)
#Training Model
fcast2<-forecast(ts1Train,h=8567)
plot(fcast2)
accuracy(fcast2,ts1Test)
New Error:
Error in -.default(xx, ff[1:n]) :
non-numeric argument to binary operator
You can try to split the function before converting the data to a time series function. For example:
train <- sampleData[1:100,] # choose the first 100 row as training set
test <- sampleData[101:200,] # choose the following 100 row as testing set
There are several issues in your code:
The start parameter in window function accept integer (as year) or vector (year and month). ?window will give more info.
The error from the first window function will not give expected input for the following code, especailly the forecast part.
forecast as the name tells, is a forecasting function. You need to build a time series model (for example, using arima function) on your training data.
I would suggest you read some time series tutorials, and here is the one:
https://a-little-book-of-r-for-time-series.readthedocs.org/en/latest/
I am getting error while decomposing a Time series i created. Becuase it doesn't have frequency/periods i guess.
I created:-
ts.sim=arima.sim(n=100, list(ar=0.9), innov=rnorm(100))
When decompose:-
decompose(ts.sim)
Error in decompose(ts.sim) : time series has no or less than 2 periods.
So, I thought of adding frequency to it, as in (TS <- ts(1:8, frequency = 4)
But this frequency option is not there in "arima.sim"
ts.sim = arima.sim(n=100, list(ar=0.9), innov=rnorm(100), frequency=10)
Error in rand.gen(n.start, ...) : unused argument (frequency = 10)
kind regards,
I have following R code which does not work when trying to pre-whiten other series by the model generated for the other series.
-- Libraries;
library(forecast);
library(TSA);
library(xts);
-- Read from csv;
....
-- Do transforms;
Power=xts(data1[2],seq(from=as.Date("2011-01-01"), to=as.Date("2013-09-18"),by="day"),frequency=7);
Temp=xts(data2[1],seq(from=as.Date("2011-01-01"), to=as.Date("2013-09-18"),by="day"),frequency=7);
-- Prewhiten for CCF;
mod1=Arima(Temp,order=c(2,0,1),seasonal=list(order=c(1,1,1)));
Box.test(mod1$residuals,lag=365,type=c("Ljung-Box"));
x_series=mod1$residuals;
y_filtered=residuals(Arima(Power,model=mod1));
Last Part does not work since I get error:
Error in stats::arima(x = x, order = order, seasonal = seasonal, include.mean = include.mean, :
wrong length for 'fixed'
What goes wrong here?
Arima and stats::arima both require ts objects. The error is caused by xts objects being used. Try this instead:
Power <- ts(data1[2], frequency=7)
Temp <- ts(data2[1], frequency=7)
mod1 <- Arima(Temp,order=c(2,0,1),seasonal=c(1,1,1))
Box.test(residuals(mod1),lag=365,type=c("Ljung-Box"))
x_series <- residuals(mod1)
y_filtered <- residuals(Arima(Power,model=mod1))
I am relatively new to R. I was using auto.arima and predict to predict a time series data. Here is the code I am using:
train.arima=auto.arima(train, seasonal=F, xreg=NULL)
train.pd=predict(train.arima, n.ahead=numahead, newxreg=NULL)
I am getting this error message, although I already set the xreg and newxreg as NULL
Error in predict.Arima(train.arima, n.ahead = numahead, newxreg = NULL) :
'xreg' and 'newxreg' have different numbers of columns: 1 != 0
Could anyone help please??
Use forecast not predict. auto.arima sometimes selects a drift term which cannot be handled by a simple call to predict.