Week 09 Coding Exercise
docx
keyboard_arrow_up
School
University Of Dallas *
*We aren’t endorsed by this school
Course
BANA 7350
Subject
Aerospace Engineering
Date
Apr 3, 2024
Type
docx
Pages
7
Uploaded by MinisterRook17046
# Getting the current working directory: import os
import pandas as pd import matplotlib.pyplot as plt
os.chdir(r'C:\Users\divye\Desktop\Forecasting Methods\Data') os.getcwd()
# Setting up charting formats: # optional plot parameters plt.rcParams['lines.linewidth'] = 3
plt.rcParams['figure.figsize'] = [14.0, 5.0]
plt.rcParams['font.size']= 18 plt.style.available # Check what are the styles available for Chart formats
plt.style.use('fivethirtyeight')
import os
import pandas as pd
df=pd.read_csv('HOUST1FNSA.csv',index_col=0,parse_dates=True)
df.plot()
df.fillna(0)
df.index.freq='MS'
y=df['HOUST1FNSA'] y
type(y)
y.plot()
# do decomposition using Statsmodel
decomposition = seasonal_decompose(y)
decomposition.plot()
plt.show()
# Seasonal Plot : Done on the dataframe, not on y
df['month'] = pd.to_datetime(df.index).month
df['year'] = pd.to_datetime(df.index).year
df
sns.lineplot(x='month',y='HOUST1FNSA',hue='year',data=df)
# now split into train and test
train_size = int(len(y) * 0.85)
train = y[1:train_size]
test = y[train_size:]
train
test
# plotting data
train.plot(label='Train')
test.plot(label='Test')
plt.legend()
model = SARIMAX(train,order=(1,1,0),seasonal_order=(0,1,1,24))
results = model.fit()
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
results.summary()
# model validation results.plot_diagnostics()
plt.show()
# now do the test on the test dataset, to see how well the model can predict the test data
forecast_object = results.get_forecast(steps=len(test)) # note, here SARIMA has get_prediction() ,ETS uses get_prediction()
mean = forecast_object.predicted_mean
conf_int = forecast_object.conf_int()
dates = mean.index
#### Now do the Plot of the Test vs. Predicted Test data with Prediction Intervals using statsmodel
plt.figure(figsize=(16,8))
plt.plot(train.index, train, label='Training')
plt.plot(test.index, test, label='Test')
plt.plot(dates, mean, label='predicted test')
plt.fill_between(dates, conf_int.iloc[:,0], conf_int.iloc[:,1], alpha=0.5)
plt.legend()
plt.show()
# Check Accuracy measures def accuracy(y_test,y_pred):
from statsmodels.tools.eval_measures import rmse, rmspe, meanabs, mse
from sktime.performance_metrics.forecasting import mean_absolute_percentage_error from sklearn.metrics import r2_score
R_sq= r2_score(y_test,y_pred)
RMSE = rmse(y_test,y_pred)
RMSPE = rmspe(y_test,y_pred)
MAE = meanabs(y_test,y_pred)
MSE = mse(y_test,y_pred)
MAPE = mean_absolute_percentage_error(y_test,y_pred) print('R-SQ : %f' % R_sq, ' RMSE : %f' % RMSE, ' RMSPE : %f' % RMSPE, ' MAE : %f' % MAE, ' MSE : %f' %
MSE , ' MAPE : %f' % MAPE)
accuracy(test,mean)
R-SQ : -2.311981 RMSE : 28.446848 RMSPE : 3.522701 MAE : 23.629606 MSE : 809.223179 MAPE : 0.304779
# now Forecast
forecast_future=results.get_forecast(steps=60)
# now make the prediction
pred_f = results.get_forecast(steps=60)
pred_ci = pred_f.conf_int()
ax = train.plot(label='Train', figsize=(14, 7))
test.plot(ax=ax, label='Test')
pred_f.predicted_mean.plot(ax=ax, label='Forecast')
ax.fill_between(pred_ci.index,
pred_ci.iloc[:, 0],
pred_ci.iloc[:, 1], color='k', alpha=.25)
ax.set_xlabel('Month')
ax.set_ylabel('New Privately-Owned Housing Units Started: Single-Family Units')
plt.legend()
plt.show()
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
# Obtain the forecast data
forecast = forecast_future.summary_frame(alpha=0.05)
forecast.index.name ='Month'
full_data_with_forecast = df.join(forecast)
full_data_with_forecast.to_csv('forecast_output.csv') # Store the Forecast Outputs