I am using the package ROBETS in R to make forecast. I need to re-train my model on a extended time series. Below is the MWE:
library(robets)
ts.train <- ts(c(60,209,51,34,208,64,122,99,82,194,136,177,110,332,300,151,128,206,129,92,164,814,1286,826,893,949,1014,830,877,605,773,870,1610,970,1192,1222,466,1963,841), start=c(20001, 1), frequency=12)
model.robets <- robets(ts.train)
ts.train.dev <- ts(c(60,209,51,34,208,64,122,99,82,194,136,177,110,332,300,151,128,206,129,92,164,814,1286,826,893,949,1014,830,877,605,773,870,1610,970,1192,1222,466,1963,841,830,812,160,238,53,760), start=c(20001, 1), frequency=12)
model.robets.retrain <- robets(ts.train.dev, model=model.robets)
I get the following error:
Error in robetsTargetFunctionInit(par = par, y = y, errortype = errortype, :
k Problem!
An easy solution to your problem is adding the argument use.initial.values = TRUE. This argument states that the same initial values are used for model.robets as for model.robets.retrain. That makes sense, since by default the initial values are estimated in a short startup period, which is the same for both time series.
The solution:
library(robets)
ts.train <- ts(c(60,209,51,34,208,64,122,99,82,194,136,177,110,332,300,151,128,206,129,92,164,814,1286,826,893,949,1014,830,877,605,773,870,1610,970,1192,1222,466,1963,841), start=c(20001, 1), frequency=12)
model.robets <- robets(ts.train)
ts.train.dev <- ts(c(60,209,51,34,208,64,122,99,82,194,136,177,110,332,300,151,128,206,129,92,164,814,1286,826,893,949,1014,830,877,605,773,870,1610,970,1192,1222,466,1963,841,830,812,160,238,53,760), start=c(20001, 1), frequency=12)
model.robets.retrain <- robets(ts.train.dev, model=model.robets, use.initial.values = TRUE)
However, the error you describe should not happen. Therefore I changed the default setting, and I solved the bug you found. A new version of robets will soon appear on CRAN.
Related
I am doing a SVAR (structural vector auto regression) analysis in which I want to plot IRFs (impulse response functions). My time series have length 137 and I only use 3 variables, furthermore I select 1 lag when specifying the VAR model.
Specifying the VAR model works fine, but when I want to summarize it I get the following error message
VAR_reduced <- VAR(VAR_data_1, p = 1, type = "both")
summary(VAR_reduced)
Error in solve.default(Sigma) :
system is computationally singular: reciprocal condition number = 1.03353e-16
From what I read in another question this error usually come up when there are not enough observations leading to overfitting, but in my example this should not be a problem, as I have enough observations.
As R does not display an error message if I don't run the summary command it is still possible to calculate the IRFs using:
plot(irf(VAR_reduced, n.ahead = 40))
But, the plot seems rather counter-intuitive, as there is no reaction from any variable other than assets. Therefore, my guess is that the error message hints at something I got wrong but haven't realised yet.
Is this correct, that is do I need to solve that error, or do my IRFs have nothing to do with this?
For completeness here is all the code:
library(quantmod)
library(urca)
library(vars)
library(tseries)
getSymbols('CPILFESL',src='FRED')
getSymbols('INDPRO',src='FRED')
getSymbols('WALCL',src='FRED')
CPI <- ts(CPILFESL, frequency = 12, start = c(1957,1))
output <- ts(INDPRO, frequency = 12, start = c(1919,1))
assets <- as.xts(WALCL)
assets <- to.monthly(assets, indexAt='yearmon', drop.time = TRUE)
assets <- ts(assets[,4], frequency = 12, start = c(2002,12))
assets <- window(assets, start = c(2008,9), end = c(2020,1))
CPI <- window(CPI, start = c(2008,9), end = c(2020,1))
output <- window(output, start = c(2008,9), end = c(2020,1))
loutput <- log(output)
lCPI <- log(CPI)
data_0 <- cbind(loutput, lCPI, assets)
plot(data_0)
VAR_data_1 <- ts.intersect(diff(loutput), diff(lCPI), diff(assets, differences = 2))
VAR_reduced <- VAR(VAR_data_1, p = 1, type = "both")
summary(VAR_reduced)
I'm trying to use Holt Winters and prediction function for stock index weekly volume from last 10 years, however i am still getting error. Can you help me please?
This is what i'm trying to do now:
volumen<-read.csv(file.choose(), header = TRUE, sep = ";")
lines(volumen[,6])
HoltWinters(volumen)
This is error I'm getting on third row:
Error in decompose(ts(x[1L:wind], start = start(x), frequency = f), seasonal) :
the time series has no periods or has less than 2
For prediction i have below code, however it does not seems to work with previous error:
lines(predict(volumen.hw,n.ahead=12),col=2)
Data in R Studio looks correct. I have decided to use file.choose() to make this code more universal. I am using *.csv file. Could someone guide me or advise what the code should look like to apply the Holt and Winters method and prediction?
It's hard to be 100% sure but
HoltWinters(lynx)
generates the same message as you are gettin,g but
HoltWinters(lynx, gamma = FALSE)
generates
Holt-Winters exponential smoothing with trend and without seasonal
component.
Call: HoltWinters(x = lynx, gamma = FALSE)
Smoothing parameters:
alpha: 1
beta : 0
gamma: FALSE
Coefficients: [,1]
a 3396
b 52
Which I learned from reading the examples in the HoltWinters documentation.
first of all it would be nice if you put your data here (if it is not private).
Secondly as far as I know you only can user HoltWinters() or any other method in the forecasting package to a vector or a time series so loading the entire dataset (volume) without specifying the rows could lead you to a problem.
Finally I recommend you to try the HW to an auxiliary vector containing the data that you want to study and also specify the frequency of the time series:
aux_train<-as.ts(volumen$variable, start=1, end=0.9*nrow(volume), freq="yourfrecuency")
prediction<-forecast(aux_train, h="number of forecast", method="hw")
accuracy(prediction, volumen$value)
I have finally won this battle - I have deleted my code and started from scratch. Here is what I came with:
dane2<-read.csv2(file.choose(), header = TRUE, sep = ";", dec=",")
dane2 <-ts(dane2[,5], start=c(2008,1),frequency=52)
past <- window(dane2, end = 2017)
future <- window(dane2, start = 2017)
model <- HoltWinters(past, seasonal = "additive")
model2 <- HoltWinters(past, seasonal = "multiplicative")
pred <- predict(model, n.ahead = 52)
pred2 <- predict(model2, n.ahead = 52)
dane2.hw<-HoltWinters(dane2)
predict(dane2.hw,n.ahead=52)
par(mfrow = c(2,1))
plot(model, predicted.values = pred)
lines(future, col="blue")
plot(model2, predicted.values = pred2)
lines(future, col="blue")
Now it works, so thank you for your answers.
I am trying to estimate a MIDAS regression on a subsample of my data using the window function. However, when I use this, the midas_r() function throws me back the error:
Error in prepmidas_r(y, X, mt, Zenv, cl, args, start, Ofunction, weight_gradients, :
Starting values for weight parameters must be supplied
Here is my code:
install.packages("midasr")
library(midasr)
yrs <- 10
x <- ts(rnorm(12*yrs),start=c(1900,1),frequency = 12)
y <- ts(rnorm(yrs),start=c(1900,1))
midas_r(y~fmls(x,3,12,nealmon),start=list(x=rep(0,3)))
x_est <- window(x,end=c(1910,0))
y_est <- window(y,end=(1910))
midas_r(y_est~fmls(x_est,3,12,nealmon)+1,start=list(x=rep(0,3)))
Does anyone know what's the issue? Thanks in advance!
The issue is in list(x=rep(0, 3)). This list has indeed to be named, but this name needs to coincide with the variable name. Hence,
midas_r(y_est ~ fmls(x_est, 3, 12, nealmon), start = list(x_est = rep(0, 3)))
works.
I'm working on dataset to forecast with ARIMA, and I'm so close to the last step but I'm getting error and couldn't find reference to figure out what I'm missing.
I keep getting error message when I do the following command:
ForcastData<-forecast(fitModel,testData)
Error in rep(1, n.ahead) : invalid 'times' argument
I'll give brief view on the work I did where I have changed my dataset from data frame to Time series and did all tests to check volatility, and Detect if data stationary or not.
Then I got the DataAsStationary as good clean data to apply ARIMA, but since I wanna train the model on train data and test it on the other part of the data, I splitted dataset into training 70% and testing 30%:
ind <-sample(2, nrow(DataAsStationary), replace = TRUE, prob = c(0.7,0.3))
traingData<- DataStationary1[ind==1,]
testData<- DataStationary1[ind==2,]
I used Automatic Selection Algorithm and found that Arima(2,0,3) is the best.
autoARIMAFastTrain1<- auto.arima(traingData, trace= TRUE, ic ="aicc", approximation = FALSE, stepwise = FALSE)
I have to mentioned that I did check the if residuals are Uncorrelated (White Noise) and deal with it.
library(tseries)
library(astsa)
library(forecast)
After that I used the training dataset to fit the model:
fitModel <- Arima(traingData, order=c(2,0,3))
fitted(fitModel)
ForcastData<-forecast(fitModel,testData)
output <- cbind(testData, ForcastData)
accuracy(testData, ForcastData)
plot(outp)
Couldn't find any resource about the error:
Error in rep(1, n.ahead) : invalid 'times' argument
Any suggestions!! Really
I tried
ForcastData<-forecast.Arima(fitModel,testData)
but I get error that
forecast.Arima not found !
Any idea why I get the error?
You need to specify the arguments to forecast() a little differently; since you didn't post example data, I'll demonstrate with the gold dataset in the forecast package:
library(forecast)
data(gold)
trainingData <- gold[1:554]
testData <- gold[555:1108]
fitModel <- Arima(trainingData, order=c(2, 0, 3))
ForcastData <- forecast(fitModel, testData)
# Error in rep(1, n.ahead) : invalid 'times' argument
ForcastData <- forecast(object=testData, model=fitModel) # no error
accuracy(f=ForcastData) # you only need to give ForcastData; see help(accuracy)
ME RMSE MAE MPE MAPE MASE
Training set 0.4751156 6.951257 3.286692 0.09488746 0.7316996 1.000819
ACF1
Training set -0.2386402
You may want to spend some time with the forecast package documentation to see what the arguments for the various functions are named and in what order they appear.
Regarding your forecast.Arima not found error, you can see this answer to a different question regarding the forecast package -- essentially that function isn't meant to be called by the user, but rather called by the forecast function.
EDIT:
After receiving your comment, it seems the following might help:
library(forecast)
# Read in the data
full_data <- read.csv('~/Downloads/onevalue1.csv')
full_data$UnixHour <- as.Date(full_data$UnixHour)
# Split the sample
training_indices <- 1:floor(0.7 * nrow(full_data))
training_data <- full_data$Lane1Flow[training_indices]
test_data <- full_data$Lane1Flow[-training_indices]
# Use automatic model selection:
autoARIMAFastTrain1 <- auto.arima(training_data, trace=TRUE, ic ="aicc",
approximation=FALSE, stepwise=FALSE)
# Fit the model on test data:
fit_model <- Arima(training_data, order=c(2, 0, 3))
# Do forecasting
forecast_data <- forecast(object=test_data, model=fit_model)
# And plot the forecasted values vs. the actual test data:
plot(x=test_data, y=forecast_data$fitted, xlab='Actual', ylab='Predicted')
# It could help more to look at the following plot:
plot(test_data, type='l', col=rgb(0, 0, 1, alpha=0.7),
xlab='Time', ylab='Value', xaxt='n', ylim=c(0, max(forecast_data$fitted)))
ticks <- seq(from=1, to=length(test_data), by=floor(length(test_data)/4))
times <- full_data$UnixHour[-training_indices]
axis(1, lwd=0, lwd.ticks=1, at=ticks, labels=times[ticks])
lines(forecast_data$fitted, col=rgb(1, 0, 0, alpha=0.7))
legend('topright', legend=c('Actual', 'Predicted'), col=c('blue', 'red'),
lty=1, bty='n')
I was able to run
ForcastData <- forecast(object=testData, model=fitModel)
without no error
and Now want to plot the testData and the forecasting data and check if my model is accurate:
so I did:
output <- cbind(testData, ForcastData)
plot(output)
and gave me the error:
Error in error(x, ...) :
improper length of one or more arguments to merge.xts
So when I checked ForcastData, it gave the output:
> ForcastData
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
2293201 -20.2831770 -308.7474 268.1810 -461.4511 420.8847
2296801 -20.1765782 -346.6400 306.2868 -519.4593 479.1061
2300401 -18.3975657 -348.8556 312.0605 -523.7896 486.9945
2304001 -2.2829565 -332.7483 328.1824 -507.6860 503.1201
2307601 2.7023277 -327.8611 333.2658 -502.8509 508.2555
2311201 4.5777316 -328.6756 337.8311 -505.0893 514.2447
2314801 4.3198927 -331.4470 340.0868 -509.1913 517.8310
2318401 3.8277285 -332.7898 340.4453 -510.9844 518.6398
2322001 1.4364973 -335.2403 338.1133 -513.4662 516.3392
2325601 -0.4013561 -337.0807 336.2780 -515.3080 514.5053
I thought I will get list of result as I have in my testData. I need to get the chart that shows 2 lines of actual data(testData), and expected data(ForcastData).
I have really went through many documentation about forcast, but I can't find something explain what I wanna do.
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))