Problem when doing pre-whitening before ccf analysis - r

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))

Related

Error in panel regression in case of different independent variable r

I am trying to run Fama Macbeth regression by the following code:
require(foreign)
require(plm)
require(lmtest)
fpmg <- pmg(return~max_1,df_all_11, index=c("yearmonth","firms" ))
Fama<-fpmg
coeftest(Fama)
It is working when I regress the data using the independent variable named 'max_1'. However when I change it and use another independent variable named 'ivol_1' the result is showing an error. The code is
require(foreign)
require(plm)
require(lmtest)
fpmg <- pmg(return~ivol_1,df_all_11, index=c("yearmonth","firms" ))
Fama<-fpmg
coeftest(Fama)
the error message is like this:
Error in pmg(return ~ ivol_1, df_all_11, index = c("yearmonth", "firms")) :
Insufficient number of time periods
or sometimes the error is like this
Error in model.frame.default(terms(formula, lhs = lhs, rhs = rhs, data = data, :
object is not a matrix
For your convenience, I am sharing my data with you. The data link is
data frame
I am wondering why this is happening in case of the different variable in the same data frame. I would be grateful if you can solve this problem.
This problem can be solved by mice function
library(mice)
library(dplyr)
require(foreign)
require(plm)
require(lmtest)
df_all_11<-read.csv("df_all_11.csv.part",sep = ",",header = TRUE,stringsAsFactor = F)
x<-data.frame(ivol_1=df_all_11$ivol_1,month=df_all_11$Month)
imputed_Data <- mice(x, m=3, maxit =5, method = 'pmm', seed = 500)
completeData <- complete(imputed_Data, 3)
df_all_11<-mutate(df_all_11,ivol_1=completeData$ivol_1)
fpmg2 <- pmg(return~ivol_1,df_all_11, index=c("yearmonth","firms"))
coeftest(fpmg2)
this problem because the variable ivol_1 have a lots of NA so you should impute the NA first then run the pmg function.

Holt Winters For Weekly Volume And Errors In R

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.

Error in retraining ROBETS time series forecasting model in R

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.

Error when doing linear regression using zoo objects ... Error in `$<-.zoo`(`*tmp*`

I am new to R and slowly getting acquainted. My question refers to the following piece of code.
I am creating a zoo object with the following headers and then filtering by date. On the filtered dates I am subtracting two columns (Tom from Elena). Everything works fine until here.
Code below:
b <- read.zoo(b1, header = TRUE, index.column = 1, format = "%d/%m/%Y")
startDate = "2013/11/02"
endDate = "2013/12/20"
dates <- seq(as.Date(startDate), as.Date(endDate), by=1)
TE = b[dates]$Tom - b[dates]$Elena
However I am then regressing the results from my subtraction (see above TE) on Elena. However i get an error message every time i try and to this regression
TE$model <- lm(TE ~ b[dates]$Elena)
Error in $<-.zoo(*tmp*, "model", value = list(coefficients = c(-0.0597128230859905, :
not possible for univariate zoo series
I have tried creating a data frame and then doing the regression but with no avail. Any help would be appreciated. Thanks.
You can not add the outcome of a regression (a list of class lm) to a time series of class zoo.
I recommend saving the model in a separate object, e.g.,
fit <- lm(TE ~ b[dates]$Elena)

How to use egarch on a ts (in OSX)

stock.ba is a ts object of monthly stock returns and I would like to fit an eGARCH model, but I keep getting this error using a new download of rgarch in R 2.10.1:
> stock.ba.egarch <- ugarchfit(stock.ba, spec)
Error in UseMethod("ugarchfit") :
no applicable method for 'ugarchfit' applied to an object of class "ts"
Here's the (relevant) code
stock.ba <- window(stock, start=c(1831, 1), end=c(2010, 2))
spec <- ugarchspec(variance.model = list(model = "eGARCH"))
stock.ba.egarch <- ugarchfit(stock.ba, spec)
Thanks!
Got a fix from the library-writer! I had to unclass the ts:
stock.ba.egarch <- ugarchfit(spec=spec, data=unclass(stock.ba))
I was also being a little casual with my argument placement (as a result of a few unsuccessful iterations).

Resources