How to create a class for Time Series (stats models) - fit data issue

Viewed 20

I am trying to learn OOP programming and I want to learn how to "re-use" existing classes. I decided to practice using time series stats model. My goal is to import several methods (ARIMA, SARIMA and so on). Create a class where I indicate the time series model and create fit and predict methods. Maybe this is a bit trivial but suits for learning purposes.

import pandas as pd
import numpy as np

from statsmodels.tsa.holtwinters import ExponentialSmoothing
from statsmodels.tsa.statespace.sarimax import SARIMAX
from statsmodels.tsa.arima.model import ARIMA
from abc import ABCMeta
from typing import Any, Dict, Tuple, List

class TimeSeriesModels:
    def __init__(self, model_name: str, model_config: Dict[str, Any]):
        self.model_name = model_name
        self.model_config = model_config
        self.model = None
        ##endog = ???
    
    self._instantiate_ts_model()

    
def _instantiate_ts_model(self):
    available_models = self.available_models()
    available_model_names = [el.lower() for el in available_models.keys()]

    if self.model_name.lower() in available_model_names:
        self.model = available_models[self.model_name](**self.model_config)
    else:
        raise ValueError(f"Model {self.model_name} is not implemented yet.")
    
@staticmethod
def available_models() -> list:
    return {"ARIMA": ARIMA, "SARIMA": SARIMAX, "ExponentialSmoothing": ExponentialSmoothing}


def fit(self):
    self.model.fit()
    
def predict(self,Y: pd.DataFrame):
    return self.model.predict(Y)

In statsmodels the way to fit data is a bit different. Currently, when I call the methods

import random
randomlist = random.sample(range(10, 100), 80)
Y_train = randomlist[:60]
Y_test = randomlist[60:]

TimeSeriesModels.available_models()
#start the model with parameters 
ts = TimeSeriesModels(model_name="ExponentialSmoothing", model_config={'initialization_method': 'estimated'})
ts.fit()
ts.predict(Y_test)

This is the error that I have, I am not sure in my case how I can pass the data? I am not able to use it in fit function and I am not sure how to add it to the class. Please can someone help/explain me what is wrong my code?

Input In [99], in TimeSeriesModels.__init__(self, model_name, model_config)
     17 self.model = None
     18 ##endog = ???
---> 20 self._instantiate_ts_model()

Input In [99], in TimeSeriesModels._instantiate_ts_model(self)
     25 available_model_names = [el.lower() for el in available_models.keys()]
     27 if self.model_name.lower() in available_model_names:
---> 28     self.model = available_models[self.model_name](**self.model_config)
     29 else:
     30     raise ValueError(f"Model {self.model_name} is not implemented yet.")

File ~\AppData\Roaming\Python\Python39\site-packages\pandas\util\_decorators.py:199, in deprecate_kwarg.<locals>._deprecate_kwarg.<locals>.wrapper(*args, **kwargs)
    197     else:
    198         kwargs[new_arg_name] = new_arg_value
--> 199 return func(*args, **kwargs)

TypeError: __init__() missing 1 required positional argument: 'endog'
0 Answers
Related