Working with time series in R, I find out that unfortunately, two packages seem to be incompatible with each other. These are forecast and TSA.
The first snippet shows forecast working as it should.
require(forecast)
test<-ts(rnorm(100), frequency=12, start=c(2000,1))
mod<-arima(test, order=c(1,1,2))
mod2<-forecast.Arima(mod, h=12)
This MRE shows my problem:
require(forecast)
require(TSA)
test<-ts(rnorm(100), frequency=12, start=c(2000,1))
mod<-arima(test, order=c(1,1,2))
mod2<-forecast.Arima(mod, h=12)
This is the error:
Error in ts(x) : 'ts' object must have one or more observations
What can be done in order to make both work (as I seem to need both), or what other packages could I use to work around this problem? I prioritize having forecast work over TSA.
It looks like the arima from TSA masks stats::arima, which is the one that works with forecast.Arima. So to get your snippet to work,
library(forecast)
library(TSA)
test<-ts(rnorm(100), frequency=12, start=c(2000,1))
mod<-stats::arima(test, order=c(1,1,2)) # Use arima from stats package
mod2<-forecast.Arima(mod, h=12)
Make sure to check the conflicts between packages when you load them!
Related
I am new to R so I tried predicting using linear reg model and I am getting the error as shown in the image below, can you help me out?
Libraries included:
library(tidyverse)
library(ggplot2)
library(caret)
library(dplyr)
library(tidypredict)
png
Try running the library(caret) again, if the package is loaded, createDataPartition is there. If you still face the issue, check for Caret updates.
When I try to forecast a time series with sma using the forecasting function I get this error:
fc <- forecast(sma(ts),h=3)
Error: The provided model is not Simple Moving Average!
Anyone knows how to fix it?
The forecast is from the fpp2 package and the moving average function is from the smooth package.
This is an example:
library(smooth)
library(fpp2)
library(readxl)
setwd("C:\\Users\\lferreira\\Desktop\\FORECASTING")
data<- read_xlsx("BASE_TESTE.xlsx")
ts <- ts(data$`1740`,start=c(2014,1),frequency=4)
> fc <- forecast(sma(ts),h=3)
Error: The provided model is not Simple Moving Average!
Your example is not reproducible because you have not provided the data.
The following example is reproducible, and does not give an error.
library(smooth)
forecast(sma(USAccDeaths))
Note that the forecast function being used here is not part of the fpp2 package. It is from the smooth package.
To check what's going on with your example:
First check that your data is correctly read in.
Then check that the sma function is returning something sensible. The error message suggests that the function is not returning the model it should.
I'm trying to do a hierarchical cluster dendrogram of time series in R using the dtwclust package. For my test dataset I have 4 columns with unequal lengths. the dtwclust package offers a way to equalize the lengths using a reinterpolate function. However I get this error when I try to use it with the following code
data <- reinterpolate(fshdtw, new.length = max(lengths(fshdtw)))
Error in check_consistency(x, "ts") : There are missing values in the series. This makes me think (I could be wrong) that the data table is not organized properly. Can anyone suggest how to 1. read in the data (I just imported it using RStudio) or any other way to resolve this issue. PS: I tried the analysis using the data provided in the package and it worked as advertised. Thanks in advance!
The data file is here https://www.dropbox.com/s/dih39ji0zop9xa0/fshdtw.txt?dl=0
I am working with the R package 'zoib' for performing beta regression in R. I am trying to replicate the example included on page 41 in the paper the package authors published in The R Journal:
Lui F and Kong Y. 2015. zoib: An R Package for Bayesian Inference for Beta Regression and Zero/One Inflated Beta Regression. The R Journal 7(2)
I believe I am using the exact same data and code that they use:
library(zoib)
data("GasolineYield", package="zoib")
GasolineYield$batch <- as.factor(GasolineYield$batch)
d <- GasolineYield
eg1.fixed <- zoib(yield ~ temp + as.factor(batch) | 1, data=GasolineYield, joint=FALSE,
random=0, EUID=1:nrow(d), zero.inflation=F, one.inflation=F,
n.iter=1050, n.thin=5, n.burn=50)
sample1 <- eg1$coeff
traceplot(sample1)
autocorr.plot(sample1)
gelman.diag(sample1)
However, I am getting an error when I try to do the diagnostic plots on the samples. This is the error message:
Error in ts(seq(from = start(x), to = end(x), by = thin(x)), start = start(x), :
invalid time series parameters specified
I cannot understand why the code isn't working or what I can do to fix the problem. I can trace the error to the time function which is called by zoib, and it seems like maybe it is a problem that the sample object does not have a tsp attribute, but the zoib package authors make it clear that their model output is meant to be used with coda, so I am very confused. I don't have much experience working with MCMC or time series objects, so maybe I am just missing something obvious. Can anyone explain why the example provided by the package authors is failing, and what the solution is?
I e-mailed the package author (Fang Liu) and she informed me that there was in fact a bug in the version of the package I have, but that the bug is fixed in the most recent version of zoib (Version 1.4.2). Using the most recent version, the code now works.
I am trying to use the forecast function (forecast package in R) and running into problem. Consider the following example:
library(forecast)
fit=arima(WWWusage,order=c(1,1,1))
fcast=forecast(fit)
plot(fcast)
At the forecast step, I am getting the error message:
Error in .forecast.transform(x, xv, a, h, 1) :
argument "xv" is missing, with no default
This happens even if I replace arima by Arima or auto.arima functions in the forecast package.
Thank you Andrie and Rob for your helpful suggestions. I have fixed the problem, but not sure how. I installed an update to RStudio and reinstalled the forecast library and it worked! Maybe something got messed up recently.
Also, I want to take this opportunity to thank you, Rob, for developing this extremely useful R package.