Issues with displays timeseries with streamlit

Viewed 687

im trying to displays forecasting timeseries using streamlit,but im stuck because i dont know what should i do first,bcs for timeseries im using jupyter notebook and i got confused how to displays it with streamlit(issue with stationary,etc).Can you guys give me reference or something? thank you! here is my code in spyder ( im just showing raw data and the graph and yeah got stuck with deploy my forecasting)

import streamlit as st
import pandas as pd
import numpy as np
import plotly.express as px
import matplotlib.pyplot as plt
from statsmodels.tsa.stattools import acf,pacf
from statsmodels.tsa.stattools import adfuller
from statsmodels.tsa.arima_model import ARIMA
import warnings                                  # `do not disturbe` mode
warnings.filterwarnings('ignore')

st.title('Forecasting Harga KCL')

DATE_COLUMN = 'month'


@st.cache
def load_data(nrows):
    data = pd.read_csv('kcl.csv', nrows=nrows)
    lowercase = lambda x: str(x).lower()
    data.rename(lowercase, axis='columns', inplace=True)
    data[DATE_COLUMN] = pd.to_datetime(data[DATE_COLUMN])
    data.set_index('month', inplace=True)
    data =data['price']
    return data

data_load_state = st.text('Loading data...')
data = load_data(300)
data_load_state.text("Done! (using st.cache)")

if st.checkbox('Show raw data'):
    st.subheader('Raw data')
    st.write(data)

st.subheader('Plot harga')
st.line_chart(data)

st.subheader('Harga Prediksi')

about this code below i dont know if its right because the timeseries doesnt pass the stationary test at all and the forecasting might go wrong

data = data [66:]
for a in range(1,30):
    model = ARIMA (data, order = (1, 1, 1))
    model_fit = model.fit(disp=False)
    yhat = model_fit.predict(len(data), len(data), typ='levels')
    data = data.append(yhat)
    st.write(yhat)
    # We display the prediction to see when it passes 0
if st.checkbox('Show hasil prediksi'):
    st.subheader('data prediction')
    st.write(yhat)

i hope you all can understand me with my lack of understanding and making question! thank you in advance

1 Answers

It sounds like you have two questions - one about streamlit and one about time series stationarity.

If you're trying to serve a streamlit model locally, just do streamlit run my_file.py where you insert the path to your file. You must have streamlit installed on your machine/virtual environment. If you're trying to deploy your app on server, check out the streamlit deployment guides here. Heroku is maybe the simplest free deployment.

If your data aren't stationary and there is a seasonality component, you'll want SARIMA instead of ARIMA.

If you want to try to automatically select values for SARIMA using p,d,q,P,D,Q or do any transforms prior to fitting, sktime's autoarima can be helpful. It wraps pmdarima, which uses statsmodels SARIMAX.

Forecasting Principles and Practice](https://otexts.com/fpp3/) by Rob Hyndman and George Athanasopoulos is a great free ebook on time series.

Related