FcDecompositionModelByRegression: Forecasting with Automatic Decomposition Model By Regression

View source: R/FcDecompositionModelByRegression.R

FcDecompositionModelByRegressionR Documentation

Forecasting with Automatic Decomposition Model By Regression

Description

Automatic approach for additive of multiplicative decomposition model generation. Dataset is divided into training and test data by SplitDataAt.

Usage

FcDecompositionModelByRegression(DataFrame, TimeColumnName = "Time", 

FeatureName ="Absatz", SplitDataAt, Frequency = "day",

                 ForecastPeriods = 10, Holidays = NULL, PlotIt = TRUE,
                 
                 xlab = "Time", ylab = "Feature", EquiDist = TRUE,
                 
                 MinLowerBound = NULL, MaxUpperBound = NULL, ...)

Arguments

DataFrame

data frame of at least two columns of Time and a Feauture, see example

TimeColumnName

name of time column in data frame, see example

FeatureName

Feature name of variable which should be forecasted, see example

SplitDataAt

index of row where the DataFrame is divided into test and train data, see example

Frequency

'day', 'week', 'month', 'quarter', 'year', 1(1 sec), 60(1 minute) or 3600(1 hour)

ForecastPeriods

how many days, weeks, etc,, should the model be foracested on?

Holidays

data frame of time and dates, see example

PlotIt

Plots data model and prediction with

black dots: data

dark green line: model based on training data

red dots: prediction

blue range: confidence intervall of model

xlab

see ggplot2

ylab

see ggplot2

EquiDist

TRUE: time difference between each two points is equidisant, if not set FALSE

MinLowerBound

due to bug from prophet does not work currently

MaxUpperBound

due to bug from prophet does not work currently

...

model paramters, see example

Details

training data is used for model generation, test data is used for evaluating the quality by a comparision to given data.

Value

Forecast

Forecast, see example

TestData

TestData, see example

TrainingData

TrainingData, see example

Model

model paramters, see example

ggObject

ggplot object to change plot with

black dots: data

dark green line: model based on training data

red dots: prediction

blue range: confidence intervall of model

Note

Usually, a model should also have a third part of data only used once and for manual verification.

Author(s)

Michael Thrun for the easy-to-use wrapper, algorithm of prophet is used (see url)

References

About the package behind it https://facebook.github.io/prophet/

Examplary published approach of an (manual) compund model:

Aubert, A. H., Thrun, M. C., Breuer, L., Ultsch, A.: Knowledge discovery from high-frequency stream nitrate

concentrations: hydrology and biology contributions, Scientific Reports, Nature, Vol. 6(31536), pp. 1-8, 2016.

Examples

dates=c("2015-01-01","2015-01-02")
names=c("New Year's Day","New Year's weekend")
holidays=data.frame(ds=as.Date(dates),holiday=names
)
data('WeeklySalesUniformRandom')


##Forcasting 10 weeks of 166 weeks data of sales

weeklyres=FcDecompositionModelByRegression(DataFrame = WeeklySalesUniformRandom,TimeColumnName ='Time' ,

FeatureName = 'Sales',SplitDataAt = 156,Frequency = 'week',

ForecastPeriods = 10,PlotIt = T,

Holidays = holidays)


#Plot components
prophet::plot_forecast_component(weeklyres$Forecast,name = c('seasonal'))
prophet::plot_forecast_component(weeklyres$Forecast,name = c('trend'))
#further compoents in names(weeklyres$Forecast)



###Forcasting thirdy days of 1093 days sales data frame
##Grid Search fo Best Parameters

#Generate all combinations of relevant paramters
prophetGrid <- expand.grid(changepoint_prior_scale = c(0.05, 0.5, 0.001),
                           seasonality_prior_scale = c(150,100,50, 10, 1,0.1),
                           holidays_prior_scale = c(150,100,50, 10, 1,0.1),
                           daily_seasonality=c(TRUE,FALSE)
)

results <- matrix(NaN, ncol=2,nrow = nrow(prophetGrid))
#calculate all
for (i in seq_len(nrow(prophetGrid))) {
  parameters <- prophetGrid[i, ]
  temp=FcDecompositionModelByRegression(
    DataFrame = DailySales,TimeColumnName ='Time' ,
    FeatureName = 'Sales',SplitDataAt = 1093,Frequency = 'day',
    ForecastPeriods = 30,PlotIt = F,Holidays = holidays2,
    seasonality.prior.scale = parameters$seasonality_prior_scale, 
    changepoint.prior.scale = parameters$changepoint_prior_scale,
    holidays.prior.scale = parameters$holidays_prior_scale,
    daily.seasonality=parameters$daily_seasonality
  )
  results[i,]=temp$Accuracy[1,2:3]
}
# choose rmse or mae
plot(results)
#rmse chosen
prophetGrid <- cbind(prophetGrid, results[,1])
#best paramters found
DailyParameters <- prophetGrid3[prophetGrid3results == min(results[,1]), ]

dailyres=FcDecompositionModelByRegression(DataFrame = DailySales,TimeColumnName ='Time',

FeatureName = 'Sales',SplitDataAt = 1093,Frequency = 'day',

ForecastPeriods = 30,PlotIt = T,Holidays = holidays2,

seasonality.prior.scale = DailyParameters$seasonality_prior_scale, 

changepoint.prior.scale = DailyParameters$changepoint_prior_scale,

holidays.prior.scale = DailyParameters$holidays_prior_scale,

daily.seasonality=DailyParameters$daily_seasonality)

dailyres$Accuracy

1-abs(mean(dailyres$TestData$y) - dailyres$Accuracy[2,3])/dailyres$Accuracy[2,3]


##multiplikative

data(AirPassengers)
DF=ConvertTS2DF(AirPassengers)
model=FcDecompositionModelByRegression(DataFrame = DF,TimeColumnName ='Time' ,

FeatureName = 'Data',SplitDataAt = 120,Frequency = 'month',

ForecastPeriods = 24,PlotIt = T,seasonality.mode = 'multiplicative')


Mthrun/TSAT documentation built on Feb. 5, 2024, 11:15 p.m.