0% found this document useful (0 votes)
4 views4 pages

Code File

The document outlines the process of importing and analyzing time series data using R, specifically focusing on a dataset named BCP. It includes steps for visualizing moving averages, testing for stationarity, and applying the ARIMA model for forecasting. The final output involves generating forecasts and converting them into a data frame for further analysis.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Code File

The document outlines the process of importing and analyzing time series data using R, specifically focusing on a dataset named BCP. It includes steps for visualizing moving averages, testing for stationarity, and applying the ARIMA model for forecasting. The final output involves generating forecasts and converting them into a data frame for further analysis.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Importing data:

library("readxl",lib.loc="/Library/Frameworks/R.framework/Versions/3.5/Resources/library)

library(readxl)

BCP <- read_excel("-/Desktop/Spring 2019/Capstone/Data/BCP.xlsx")

View(BCP)

Library Including:

library(quantmod)

library(ggplot2)

library(forecast)

library(tseries)

libraryCreadxl)

library(default)

library(zoo)

Load Time Series Data

#///Load Time Series Data///

class (BCP)

frequency(BCP)

summary(BCP)

par(mar=c(1,1,1,1))

bcpxts<-xts(BCP$Cloture, order.by = BCP$Date)

chartSeries(bcpxts)

df = data.frame(date = index(bcpxts), bcpxts, row. names=NULL)

Moving Average Vizualisation and Decomposition

df$ma7 = ma(df$bcpxts, order=7)

df$ma30 = ma(df$bcpxts, order=30)

ggplot() +
geom_line(data = df, aes(x = df$date, y = df$bcpxts, colour - "Daily Price"))
+

geom_line(data = df, aes(x = df$date, y = df$ma7, colour = "Weekly


Moving Average")) +

geom_line (data = df, aes(x = df$date, y = df$ma30, colour = "Monthly


Moving Average"))+

ylab('BCP Stock Price')

++

#/7/Decomposite Moving Average///

price_ma = ts(na.omit(df$ma7), frequency=30)

decomp = stl (price_ma, s.window="periodic")

deseasonal_ma <- seasadj(decomp)

plot(decomp)

Stationary Or No Stationary( Dickey Fuller)

#///Test Stationary or Non-Stationary///

adf.test(price_ma, alternative = "stationary")

Auto-Correlations and Choosing Model Order[ACF AND PACF]

#///Auto-Correlations and choosing Model Order///

Acf(df$ma7, main="",xlab="Lag",ylab="ACF")

Pacf(df$ma7, main="",xlab="Lag", ylab="Partial ACF")

df_d1 = diff(deseasonal_ma, differences = 1)

plot(df_d1, main="",xlab="Time",ylab="Re-evaluate")

adf.test(df_di, alternative = "stationary") #/// rester stationarité avec dikey


fuller///

Analyze of ACF and PACF

#///Analyze ACF and PACF///


Acf(df_di, main='ACF for Differenced Series')

Pacf(df_di, main='PACF for Differenced Series')

auto.arima(deseasonal_ma, seasonal=FALSE)

Auto Defined ARIMA Model

#///Forecasting Equation///

#If d=0: yt = Yt

#If d=1: yt = Yt - Yt-1

#If d=2: yt = (Yt - Yt-1) - (Yt-1 - Yt-2) = Yt - 2Yt-1 + Yt-2

# Evaluating and Iterating an ARIMA Model Auto define order

fit<-auto.arima(deseasonal_ma, seasonal=FALSE)

tsdisplay(residuals(fit), lag.max=45, main='Model Residuals')

Forecast without Seasonality Component

# Forecast without Seasonality Component

fcast <- forecast(fit, h=30)

plot(fcast)

#/// Converting Forcast Into data///

> my.data <- as.data.frame(fcast)

> my.data <- data.frame(id, my.data)

> my.data

You might also like