how to use forecast function for simple moving average model in r?

六眼飞鱼酱① 提交于 2019-12-11 03:09:39

问题


I want to predict the future values for my simple moving average model. I used the following procedure:

x <- c(14,10,11,7,10,9,11,19,7,10,21,9,8,16,21,14,6,7)   
df <- data.frame(x)    
dftimeseries <- ts(df)  
library(TTR)      
smadf <- SMA(dftimeseries, 4) # lag is 4    
library(forecast)    
forecasteddf <- forecast(smadf, 4) # future 4 values     

When run the above code, my forecast values are the same for all the next 4 days. Am I coding it correctly? Or, am I conceptually wrong?

The same is the case with exponential moving average, weighted moving average, and ARIMA also.


回答1:


For a moving average model you can read here

"Since the model assumes a constant underlying mean, the forecast for any number of periods in the future is the same...".

So, your result are to be expected considering the characteristics of the moving average mode.




回答2:


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!



来源:https://stackoverflow.com/questions/30350133/how-to-use-forecast-function-for-simple-moving-average-model-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!