linkedin / greykite

A flexible, intuitive and fast forecasting library
BSD 2-Clause "Simplified" License
1.81k stars 105 forks source link

Regressors Already Forecastd, No Lag Needed. But, getting AttributeError: 'dict' object has no attribute 'lagged_regressors' #94

Closed KathyGCY closed 1 year ago

KathyGCY commented 1 year ago

Hi, Thank you for providing this wonderful forecasting package! I've having the best time exploring the greykite package!

However I ran into a tiny issue about regressors:

I'm forecasting with regressor on this type of data: fake data So my regressor is already forecasted and no lagging needed. But I keep getting this error saying "AttributeError: 'dict' object has no attribute 'lagged_regressors'"

FYI This is the config I've been using:

metadata = MetadataParam(
    time_col='date', 
    value_col='fake_target',
    freq='D', 
    train_end_date = '2022-01-10'
)
evaluation_period = EvaluationPeriodParam(
    cv_max_splits=0 # This is to disable CV for demo purposes and just train it on the full data
)
evaluation_metric = EvaluationMetricParam(
    cv_selection_metric=EvaluationMetricEnum.RootMeanSquaredError.name
)

model_components = dict(
    growth=dict(growth_term=None),
    regressors=dict(
        regressor_cols=["fake_regressor"]
    ),
    autoregression=dict(autoreg_dict=None),
    lagged_regressors = dict(lagged_regressor_dict = None) # This is the confusing part, I explicitly said NO lagged terms
)

config = ForecastConfig(
    model_template=ModelTemplateEnum.SILVERKITE.name,
    forecast_horizon=5, 
    coverage=0.95, 
    metadata_param=metadata,
    evaluation_period_param=evaluation_period,
    evaluation_metric_param=evaluation_metric,
    model_components_param=model_components
)

# And run
forecaster = Forecaster()
result = forecaster.run_forecast_config( 
    df=train,
    config=config
)

And here's a screenshot of the error

Screenshot 2022-12-06 at 3 06 17 PM

I was wondering where I did wrong?

Thank you in advance for your support and have a wonderful day! All the best, Kathy Gao

yuncxu commented 1 year ago

Hi Kathy, can you try the following?

model_components = ModelComponentsParam( growth=dict(growth_term=None), regressors=dict( regressor_cols=["fake_regressor"] ), autoregression=dict(autoreg_dict=None), lagged_regressors=None )

KathyGCY commented 1 year ago

Hi @yuncxu I tried the above model_components but is still hitting the same error

KathyGCY commented 1 year ago

@yuncxu Here's a replicable code to reach this error. I'm using the latest version of greykite greykite==0.4.0

import pandas as pd
import random
import plotly
from greykite.common.evaluation import EvaluationMetricEnum
from greykite.framework.templates.autogen.forecast_config import MetadataParam
from greykite.framework.templates.autogen.forecast_config import (
    ComputationParam, EvaluationMetricParam, EvaluationPeriodParam,
    ForecastConfig, MetadataParam, ModelComponentsParam)
from greykite.algo.changepoint.adalasso.changepoint_detector import ChangepointDetector
from greykite.framework.benchmark.data_loader_ts import DataLoaderTS
from greykite.framework.templates.autogen.forecast_config import ForecastConfig
from greykite.framework.templates.forecaster import Forecaster
from greykite.framework.templates.model_templates import ModelTemplateEnum

# Generate Replicable Random Table
random.seed(1008)
fake_target = random.sample(range(0, 100), 31) 
fake_regressor = [1 if x > 30 else 0 for x in random.sample(range(0, 100), 100) ]
datelist = pd.date_range('2022-01-01', periods=100).tolist()
train = pd.DataFrame({'date': pd.Series(datelist), 
                      'fake_target': pd.Series(fake_target),
                      'fake_regressor': pd.Series(fake_regressor)
                     })
train.head(3)

# Set up configuration
metadata = MetadataParam(
    time_col='date', 
    value_col='fake_target',
    freq='D', 
    train_end_date = '2022-01-31'
)
evaluation_period = EvaluationPeriodParam(
    cv_max_splits=0 # This is to disable CV for demo purposes and just train it on the full data
)
evaluation_metric = EvaluationMetricParam(
    cv_selection_metric=EvaluationMetricEnum.RootMeanSquaredError.name
)
model_components = dict(growth=dict(growth_term=None), 
                        regressors=dict(regressor_cols=["fake_regressor"]),
                        autoregression=dict(autoreg_dict=None), 
                        lagged_regressors=None)
config = ForecastConfig(
    model_template=ModelTemplateEnum.SILVERKITE.name,
    forecast_horizon=30, 
    coverage=0.95, 
    metadata_param=metadata,
    evaluation_period_param=evaluation_period,
    evaluation_metric_param=evaluation_metric,
    model_components_param=model_components
)

# And run
forecaster = Forecaster()
result = forecaster.run_forecast_config( 
    df=train,
    config=config
)
yuncxu commented 1 year ago

@KathyGCY model_components should be an object of the ModelComponentsParam class. I have updated the previous comment. Also, there are a few other places that are not correctly specified. Please see the following updated code for your example

`metadata = MetadataParam( time_col='date', value_col='fake_target', freq='D', train_end_date = pd.to_datetime('2022-01-31') ) evaluation_period = EvaluationPeriodParam( test_horizon=0, # need to define test_horizon otherwise it is taking forecast_horizon as default. cv_max_splits=0,
) evaluation_metric = EvaluationMetricParam( cv_selection_metric=EvaluationMetricEnum.RootMeanSquaredError.name ) model_components = ModelComponentsParam( growth=dict(growth_term=None), regressors=dict(regressor_cols=["fake_regressor"]), autoregression=dict(autoreg_dict=None), lagged_regressors=None)

config = ForecastConfig( model_template=ModelTemplateEnum.SILVERKITE.name, forecast_horizon=30, coverage=0.95, metadata_param=metadata, evaluation_period_param=evaluation_period, evaluation_metric_param=evaluation_metric, model_components_param=model_components )

And run

forecaster = Forecaster() result = forecaster.run_forecast_config( df=train, config=config )`

KathyGCY commented 1 year ago

Yes @yuncxu It seems like it might be the same error message

Screenshot 2022-12-07 at 12 17 52 PM
yuncxu commented 1 year ago

Yes @yuncxu It seems like it might be the same error message Screenshot 2022-12-07 at 12 17 52 PM

Please see above for the answer.

KathyGCY commented 1 year ago

Hi @yuncxu Great News!!! It Worked!!! Thank you so much for your help!!! 🙏 And thank you again for this great package!!! Have a wonderful day!

Screenshot 2022-12-07 at 1 11 36 PM