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.
Related
I am trying to create a raster with predictions for a model, using glmmTMB.
This is based on a model, and a rasterstack.
I converted the rasterstack to a data frame, as I think this is a requirement for the function predict.glmmTMB to run.
The model
model6 <- glmmTMB(Used~scale(Road_density)+scale(nonforprop)+scale(devprop)+
scale(forprop)+scale(nonfordist_cap3000)+scale(fordist_cap3000)+
scale(agridist_cap3000)+scale(devdist_cap3000)+(1|animal_ID),
data=rasterpoints3,na.action=na.omit,family=binomial(link="logit"))
The data frame containing the rasterstack values to predict for
predstack <- as.data.frame(stack2)
The error
glmmTMB:::predict.glmmTMB(model6,predstack,re.form=NA)
Error in eval(predvars, data, env) : object 'animal_ID' not found
I was hoping someone more experienced could help me resolve this. animal_ID is the random intercept in my glmmTMB object model 6. I am using this package, and not e.g. raster::predict, exactly because it should be able to deal with random effects. To my understanding, re.form=NA should take care of this?
There's an open issue about this, but the workaround should be easy: define
predstack$animal_ID <- NA
The random effect variable has to exist in the data, but it's not used. (Due to the internal structure of glmmTMB it's not completely trivial to fix this at the package level.)
Given Ben's answer, this ought to work as well with either the raster or terra package:
p <- predict(stack2, model6, const=data.frame(animal_ID=NA), re.form=NA)
(But in the absence of an example I cannot check it)
I have a script in R for Time series forecasting. Initially when I created a time series on my univariate data and applied tsclean() function it worked. But today, when I'm trying to re-run my script again at a point where I use my tsclean() function it says:
Error in tsclean(sales_ts) : could not find function "tsclean"
I'm calling in my forecast package as well before where it says forecast package is already built in R 3.6.1.
It's weird why the function is not working today but it worked yesterday.
#creating time series data
sales_ts = ts(sdld_10[, c('Sales')])
#cleaning time series data
library(forecast)
sdld_10$clean_sales = tsclean(sales_ts)
sdld_10$test<- c(sdld_10$clean_sales - sdld_10$Sales)
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 using R to read a time series from Excel (using XLConnect), then running some forecast models on that time series, and then outputting the results back to Excel. It's a long story, but the company I'm doing a Masters by Research for want to keep using Excel! Anyhow, I can extract the time series I want from Excel. I use ts() to make it a time series. I then run forecasts on that series using (in this order) ets(), auto.arima(), tbats(), mapaest(), theta(), and finally stlf(). To check that it's doing the forecasts I get R to print off the results of the forecasts. It runs through all the forecast models fine until it gets to the stlf() forecast function. When I get this error:
Error in stl(x, s.window = s.window, t.window = t.window, robust =
robust) : only univariate series are allowed
My question is how come the time series (it is univariate) works fine in the other forecast functions but not in the stlf() function?
I have figured it out now after doing some more digging around. It turns out that I was passing a time series that was also a matrix to the stlf() function. The other forecasting functions don't seem to mind being given a time series that is also a matrix, but for some reason stlf() doesn't like it.
I converted my raw data into a vector using as.vector() and then turned that vector into a ts() object. And then passed that onto the stlf() function. Not massivly sure how this works as I originally passed a ts() object to stlf() previously. Fairly new to R, but I will look further into this..
Here is the code I used ;
altVector <- as.vector(t(rawdata))
tsy2 <- ts(altVector, start=1, end=c(3,9), frequency=12)
f.stlf <- stlf(tsy2, method="arima", h=3)
It's all working fine now.
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!