Code Monkey home page Code Monkey logo

tsa_exp7's Introduction

[# Ex.No: 07 AUTO REGRESSIVE MODEL

Date:

Developed by: hanumanth rao
Reg No: 212221240016

AIM:

To Implementat an Auto Regressive Model using Python

ALGORITHM:

  1. Import necessary libraries
  2. Read the CSV file into a DataFrame
  3. Perform Augmented Dickey-Fuller test
  4. Split the data into training and testing sets.Fit an AutoRegressive (AR) model with 13 lags
  5. Plot Partial Autocorrelation Function (PACF) and Autocorrelation Function (ACF)
  6. Make predictions using the AR model.Compare the predictions with the test data
  7. Calculate Mean Squared Error (MSE).Plot the test data and predictions.

PROGRAM:

Import necessary libraries

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.stattools import adfuller
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from statsmodels.tsa.ar_model import AutoReg
from sklearn.metrics import mean_squared_error

Read the CSV file into a DataFrame

data = pd.read_csv("/content/Temperature.csv")  
data['date'] = pd.to_datetime(data['date'])
data.set_index('date', inplace=True)

Perform Augmented Dickey-Fuller test

result = adfuller(data['temp']) 
print('ADF Statistic:', result[0])
print('p-value:', result[1])

Split the data into training and testing sets

train_data = data.iloc[:int(0.8*len(data))]
test_data = data.iloc[int(0.8*len(data)):]

Fit an AutoRegressive (AR) model with 13 lags

lag_order = 13
model = AutoReg(train_data['temp'], lags=lag_order)
model_fit = model.fit()

Plot Partial Autocorrelation Function (PACF) and Autocorrelation Function (ACF)

plot_acf(data['temp'])
plt.title('Autocorrelation Function (ACF)')
plt.show()
plot_pacf(data['temp'])
plt.title('Partial Autocorrelation Function (PACF)')
plt.show()

Make predictions using the AR model

predictions = model_fit.predict(start=len(train_data), end=len(train_data)+len(test_data)-1)

Compare the predictions with the test data

mse = mean_squared_error(test_data['temp'], predictions)
print('Mean Squared Error (MSE):', mse)

Plot the test data and predictions

plt.plot(test_data.index, test_data['temp'], label='Test Data')
plt.plot(test_data.index, predictions, label='Predictions')
plt.xlabel('Date')
plt.ylabel('Temperature')
plt.title('AR Model Predictions vs Test Data')
plt.legend()
plt.show()

OUTPUT:

Given Data

image

Augmented Dickey-Fuller test

image

PACF-ACF image

image Mean Squared Error image PREDICTION: image

RESULT:

Thus we have successfully implemented the auto regression function using python. ](https://github.com/Hanumanth26/TSA_EXP8.git)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.