<jupyter_start><jupyter_text>DatasetIn this project, the Dataset used is the famous Kaggle [Dataset](https://www.kaggle.com/berkeleyearth/climate-change-earth-surface-temperature-data).<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pylab as plt
%matplotlib inline

df = pd.read_csv('E:/python/Dataset/climate change/GlobalLandTemperaturesByCountry.csv', delimiter=',')
df.head(10)
type(df)
df.describe()
df_country = df.Country.unique()
len(df_country)
df = df.drop('AverageTemperatureUncertainty', axis=1)
df = df[df.Country == "Portugal"]
df
df = df.drop('Country',axis=1)
df
df.index = pd.to_datetime(df.dt)
df
df = df.drop('dt', axis=1)<jupyter_output><empty_output><jupyter_text>Let us take the data from 1970 onwards<jupyter_code>df
# Filtering data starting from 1970-01-01
df = df.loc['1970-01-01':]
df.head()
# Testing whether there are null values
df[df.isnull()]
len(df[df.isnull()])
df = df.sort_index()
df.index
# Replacing NaN values with the previous effective data
df.AverageTemperature.fillna(method='pad', inplace=True)
df[df.AverageTemperature.isnull()]
df.describe()
df['Ticks'] = range(0,len(df.index.values))
df.head(10)
df.tail(10)
#very simple plotting
fig = plt.figure(1)
ax1 = fig.add_subplot(111)
ax1.set_xlabel('Ticks')
ax1.set_ylabel('Avg. Temp.')
ax1.set_title('Original Plot')
ax1.plot('Ticks', 'AverageTemperature', data = df);
df


from statsmodels.tsa.stattools import adfuller
def stationarity_check(ts):
    
    # Determing rolling statistics
    roll_mean = ts.rolling(12).mean()
    # Plot rolling statistics:
    plt.plot(ts, color='green',label='Original')
    plt.plot(roll_mean, color='blue', label='Rolling Mean')
    plt.legend(loc='best')
    plt.title('Rolling Mean')
    plt.show(block=False)
    
    # Perform Augmented Dickey-Fuller test:
    print('Augmented Dickey-Fuller test:')
    df_test = adfuller(ts)
    print("type of df_test: ",type(df_test))
    print("df_test: ",df_test)
    df_output = pd.Series(df_test[0:4], index=['Test Statistic','p-value','#Lags Used','Number of Observations Used'])
    print("df_output: \n",df_output)
    for key,value in df_test[4].items():
        df_output['Critical Value (%s)'%key] = value
    print(df_output)
stationarity_check(df.AverageTemperature)
df['Roll_Mean'] = df.AverageTemperature.rolling(window=12).mean()
df.AverageTemperature.rolling(window=12).mean()
df.head(40)
from statsmodels.graphics.tsaplots import plot_pacf,plot_acf
plot_acf(df.AverageTemperature, lags=50)
plot_pacf(df.AverageTemperature, lags=50)
plt.xlabel('lags')
plt.show()
from statsmodels.tsa.arima_model import ARMA
import itertools
p = q = range(0, 4)
pq = itertools.product(p, q)
for param in pq:
    try:
        mod = ARMA(df.AverageTemperature,order=param)
        results = mod.fit()
        print('ARMA{} - AIC:{}'.format(param, results.aic))
    except:
        continue
model = ARMA(df.AverageTemperature, order=(3,3))  
results_MA = model.fit()  
plt.plot(df.AverageTemperature)
plt.plot(results_MA.fittedvalues, color='red')
plt.title('Fitting data _ MSE: %.2f'% (((results_MA.fittedvalues-df.AverageTemperature)**2).mean()))
plt.show()
predictions = results_MA.predict('01/01/1970', '12/01/2023')
predictions<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15190651906519065, 'fraction_numerical': 0.023370233702337023, 'mean_word_length': 5.4161735700197235, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '176282', 'license': 'Apache-2.0', 'n_tokens_mistral': 1251, 'n_tokens_neox': 1177, 'n_words': 227, 'path': 'Time Series Analysis for Temperature Forecasting using ARIMA Model Portugal_prediction.ipynb', 'repo_name': 'DiogoRibeiro7/Time-Series', 'size': '3252'}