1
\$\begingroup\$

Hi I have wrote a code here for predicting temperature across months. This is the dataset that I was using: https://www.kaggle.com/sumanthvrao/daily-climate-time-series-data

I have used a SARIMA model and looking at the predicted results, it does seem to be pretty decent.

Given that I just started learning about a month ago and most of my reference materials are based off Youtube tutorial. I would just like to seek opinions from professional data scientist/ML experts on what are some caveats which I might have missed out or done wrongly here. (For instance, I saw in a video somewhere that mentioned RSS should be low whereas mine is pretty high here)

%tensorflow_version 2.x  # this line is not required unless you are in a notebook
 
import tensorflow as tf
from numpy import array
from numpy import argmax
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import os

#read data and filter temperature column
df = pd.read_csv('/content/drive/MyDrive/Colab Notebooks/Weather Parameter/DailyDelhiClimateTrain.csv')
df["date"] = pd.to_datetime(df["date"])
df.date.freq ="D"
df.set_index("date", inplace=True)
df_monthly = df['meantemp'].resample('MS').mean()
plt.figure(figsize=(30,8))
plt.plot(df_monthly)

from statsmodels.tsa.seasonal import seasonal_decompose 

sd = seasonal_decompose(df_monthly, model='additive')
sd.plot()
plt.show()

#make data stationary
df_monthly_diff = np.log(df_monthly).diff(12)
plt.figure(figsize=(15,5))
plt.plot(df_monthly_diff)
print(df_monthly_diff)

from statsmodels.tsa.stattools import adfuller
adf_result = adfuller(df_monthly_diff.dropna())
print('ADF Statistic: %f' % adf_result[0])
print('p-value: %f' % adf_result[1])
print('Critical Values:')
for key, value in adf_result[4].items():
    print('\t%s: %.3f' % (key, value))

from statsmodels.tsa.stattools import acf

lag_acf = acf(df_monthly_diff.dropna())

plt.figure(figsize=(20,5))
plt.plot(lag_acf)
plt.show

from statsmodels.tsa.stattools import pacf

lag_pacf = pacf(df_monthly_diff.dropna())

plt.figure(figsize=(20,5))
plt.plot(lag_pacf)
plt.show

from statsmodels.tsa.arima_model import ARIMA
import statsmodels.api as sm

# fit model
model = sm.tsa.statespace.SARIMAX(df_monthly,order=(0, 1, 0),seasonal_order=(0,1,0,12))
model_fit = model.fit()
# summary of fit model
print(model_fit.summary())
# line plot of residuals
residuals = model_fit.resid
residuals.plot()
plt.show()
# density plot of residuals
residuals.plot(kind='kde')
plt.show()
# summary stats of residuals
print(residuals.describe())

plt.figure(figsize=(20,5))
plt.plot(df_monthly)
plt.plot(model_fit.fittedvalues, color='red')
rss = sum((model_fit.fittedvalues-df_monthly)**2)
plt.show
print(rss)

df_test = pd.read_csv('/content/drive/MyDrive/Colab Notebooks/Weather Parameter/DailyDelhiClimateTest.csv')
df_test["date"] = pd.to_datetime(df_test["date"])
df_test.date.freq ="D"
df_test.set_index("date", inplace=True)
df_test_monthly = df_test['meantemp'].resample('MS').mean()

pred = model_fit.predict(start=48,end=61,dynamic=True)
pred_df_monthly = df_monthly.append(pred)
df_monthly = df_monthly.append(df_test_monthly)

plt.figure(figsize=(20,8))
plt.plot(pred_df_monthly)
plt.plot(df_monthly)
plt.show

This is the predicted results

This is the predicted result from my code. Would really appreciate any comments. Thanks!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.