tableau / TabPy

Execute Python code on the fly and display results in Tableau visualizations:
https://tableau.github.io/TabPy/
MIT License
1.56k stars 598 forks source link

Hiding Early Forecast Periods using SARIMAX #429

Closed KasTabPy closed 4 years ago

KasTabPy commented 4 years ago

Environment information:

OS: Windows Python version: 3.8 TabPy release: unsure - recently installed Describe the issue I want to exclude early forecast periods from the predicted/forecasted line generated using the SARIMAX model from statsmodels. Tableau reads python code like a table calculation, so the predicted/forecast line must be the same number of data points as the data used to model. The early predictions are unreasonable because there is not have enough data to reasonably forecast.

To Reproduce Using any monthly timeseries,

SCRIPT_REAL("

from statsmodels.tsa.statespace.sarimax import SARIMAX

import pandas as pd

import numpy as np

dates = _arg1

assigned = _arg2

p = min(_arg3)

d = min(_arg4)

q = min(_arg5)

months_forecast = min(_arg6)

P = min(_arg7)

D = min(_arg8)

Q = min(_arg9)

s = min(_arg10)

df = pd.Series(data=assigned,index=dates)

model=SARIMAX(np.log(df), order=(p,d,q),seasonal_order=(P,D,Q,s))

results=model.fit()

pred_uc = results.get_forecast(steps=months_forecast)

pred = results.get_prediction(start=(pd.to_datetime('2013-01-01')) + pd.DateOffset(months=months_forecast), dynamic=False)

data = list(pred.predicted_mean)

data.extend(pred_uc.predicted_mean)

return data

"

,ATTR([Date]),ATTR([Assigned]), MIN([p]), MIN([d]), MIN([q]), min([Forecast Periods]), MIN([P]), MIN([D]), MIN([Q]), MIN([s]))

Expected behavior In python code environment, I can choose how many periods back to predict, but not so in Tableau

nmannheimer commented 4 years ago

Hey @KasTabPy this video should work as a guide for how to extend future dates in Tableau so you can return more future dates than exist in the original data. Using a Tableau date filter for the data in the viz can also control which data are passed to the model to forecast:

https://youtu.be/0BN_Y2CxdYY?t=1771

0golovatyi commented 4 years ago

@KasTabPy Were you able to solve your issue with the solution suggested above?