Forecasting future quarters using SMA or EMA in r by group - r

I am trying to forecast using the SMA function or EMA function (TTR Package https://www.rdocumentation.org/packages/TTR/versions/0.24.2/topics/SMA) in r for time series. Here is an example of the data frame I am working with:
Product<- c("Watch","Watch","Watch","Phone","Phone","Phone","Computer","Computer","Computer")
Region<- c("US","US","US","China","China","China","US","US","US")
Quarter<- c("03/31/2020","06/30/2020","09/30/2020","03/31/2020","06/30/2020","09/30/2020","03/31/2020","06/30/2020","09/30/2020")
Amount<-c(1000,2000,2100,500,600,620,100,110,105)
df<-data.frame(Product,Region,Quarter,Amount)
How can I use the sma function or ema to forecast a select amount of quarters in r? I was able to do something similar with the auto.arima function, but the forecast was way off so I want to just implement a simple moving average or an ema. I also want to keep the data frame in the same format, so I just want to add extra observations or even convert it to wide with the new columns being the quarter with the amount, either is fine.
Auto.Arima Example:
test1<-df %>% dplyr::group_by(Product,Region) %>% do(data.frame(amount_2 = forecast(auto.arima(.$Amount)$mean, h=3)))

Related

How to convert a weekly dataset into a time series in R

i have a WEEKLY dataset that start on 1986.01.03 and end on 2022-10-07.
The problem is when I forecast the time series with Arima +garch, because the date in T0 is wrong, i.e. 1975 enter image description here.
The function that I used to convert the dataset into time series is here, but I think that the problem is here, since it doesn't take on the right date.
FutureWeekly= ts(WeeklyFuture$FutureWeekly, start= c(1986,1), end = c(2022,10), frequency = 52)
does anyone know how to convert a weekly dataset to time series other than this?
There are the first rows of my dataset and then I have to transform that into returns (diff(log(FutureWeekly) to do the ARMA+GARCH
enter image description here
Try this:
futures<-c(WeeklyFuture$FutureWeekly) #convert to vector
FutureWeekly= ts(futures, start= c(1986,1,10), end = c(1986,3,7), frequency = 52) #add day of week ending on
One of the things ts() demands is a vector of values. I think it might also be easier for ts() to convert the data if it was able to see the 7-day increments.
Assuming you have full un-broken weekly data for the entire period, I think these two things will solve the problem.

Time Series data with twice daily frequency in R [duplicate]

I have some data coming from sensors with observations every 12 hours. I want to analyze it as time series, but I not sure what frequency to use to convert it to time series. The ts function needs a start, an end and a frequency. ts(data, start = 1, end = numeric(), frequency = 1,...)
I do not want to aggregate it to once daily frequency (will likely lose some information). Any recommendations?
The ts class does not handle high frequency data very well. I suggest you use the tsibble class instead. See https://cran.r-project.org/package=tsibble. There are many modelling tools designed for tsibble objects provided by the feasts and fable packages.

weekly time series in r, arima

I have a data frame with the following column names.
"week" "demand" "product-id"
The problem is to convert it into a time series object.
week is a number like 3,4,5,6,7,8,9 etc., and demand is in units and product-id is unique.
I want to convert the week column into time series, so as to prepare for modeling.
I want to predict weeks 10 and 11 demand by using an ARIMA model. How do I do that?
myTS <- ts(mydataframe[-1], frequency = 52)
will convert your demand and productId to a timeseries of 52 observations per year. For more elaborate timeseries, check package xts. Also compare this post on weekly data with ts.

Time Series for Periods Over One Year

I'm having trouble doing time series for my data set. Most examples have quarterly or monthly frequencies but my issue comes with data that is collect annually or every two years. Consider my code:
data<-data.frame(year=seq(1978,2012,2), number=runif(18,100,500))
time<-ts(data$number, start=1978, frequency=.5)
decomp<-decompose(time)
Error in decompose(time) : time series has no or less than 2 periods
How do I make R recognize time series values from data that is collected over an annual basis? Thanks!
Seasonal decomposition only makes sense with intra-yearly data, because you have seasons within years. So, trying to calculate seasonal effects with decompose on data collected every two years you get the error.

Calendar Year Return Calculation

I am trying to calculate calendar year GDP growth for the GDPC96 time series from FRED (i.e. for a xts object). I am looking for a simple function without loops which calculate the calendar year growth where the variables are the data object (here GDPC96), the frequency (here quarterly) and whether deprecated periods (such as 2013) shall be shown or not.
For example:
library(quantmod)
getSymbols("GDPC96",src="FRED")
a <- annualReturn(GDPC96,leading=FALSE)
tail(a)
I would like it to be such that the changes are per calendar year, i.e. it should calculate from 01.01.1947 to 01.01.1948 and so on. Then, for 2012, where data is only available through Oct, it should be omitted.
As far as I have seen none of the functions in PerformanceAnalytics and the related packages can do this properly.
It seems you want something like a year-over-year return calculation. I'm not aware of a function that does this automatically, but it's easy to do with the ROC function in the TTR package.
library(quantmod)
getSymbols("GDPC96",src="FRED")
ROC(GDPC96, 4) # 4-period returns for quarterly data
getSymbols("SPY")
spy <- to.monthly(SPY)
ROC(spy, 12) # 12-period returns for monthly data
Update based on comments:
first.obs.by.year <- lapply(split(GDPC96, "years"),first)
last.obs.by.year <- lapply(split(GDPC96, "years"),last)
ROC(do.call(rbind, first.obs.by.year))
ROC(do.call(rbind, last.obs.by.year))

Resources