Time Series Analysis
Time Series Analysis
sales=c(79,11,74,86,65,23,45,49,99,24,40,48,51)
T1=ts(sales)
T1
plot(T1, col="blue", main= "time plot")
class(sales)
class(T1)
plot(sales)
sales.ts=ts(sales,start=2018, frequency = 12)
sales.ts
sales.ts2=ts(sales,start=2018, frequency = 1)
sales.ts2
sales.ts3=ts(sales,start=c(2018,4), frequency = 12)
sales.ts3
class(sales.ts)
sales.ts4=ts(sales, start=2018, frequency=4)
sales.ts4
plot(sales.ts)
sales1=c(5,22,12,45,67,24,36,43,11,72,25,65,42)
combine.ts=cbind(sales.ts,sales1.ts)
plot(combine.ts)
plot(combine.ts,type="o",plot.type = "single",col=c("red","blue"))
###############################################
# more example of time series dataset
# EuStockMarket
# AirPassengers
# JohnsonJohnson
# airmiles
# google: TSA package
# airpass: TSA
# electricity: TSA
# gold : TSA
##########################################
# more time series related commands
is.ts(EuStockMarkets)
start(EuStockMarkets)
end(EuStockMarkets)
summary(EuStockMarkets)
cycle(EuStockMarkets)
cycle(AirPassengers)
frequency(EuStockMarkets)
frequency(AirPassengers)
aggregate(AirPassengers)
#############################################
# Most of the models of forecasting are based on
# the assumption of stationarity (AR, ARMA, ARIMA)
# How to check stationarity?
# 1. plot(), 2. ACF, 3. Specific tests (DF, ADF)
################################################
install.packages("TSA")
library(TSA)
data(electricity)
plot(electricity) #trend and changing variance both, therefore NS
######################################
# forecasting using ARIMA
# acf and pacf determines order of AR and MA
# forecasting using "forecast" package
install.packages("forecast")
library(forecast)
#auto.arima function chooses best p,d,q
fit=auto.arima(AirPassengers)
summary(fit)
forecast(fit, h=10) #using forecast
plot(forecast(fit, h=10))
plot(forecast(fit, h=5*12))
predict(fit, n.ahead = 5*12)
#########################################
install.packages("quantmod")
library(quantmod)
d=getSymbols("AAPL",auto.assign=F)
d
head(d)
str(d)
dim(d)
plot(d$AAPL.Close)
plot(Cl(d))
df=data.frame(getSymbols("AAPL", auto.assign=F))
df
head(df)
str(df)
dim(df)
colnames(df)<-c("Open", "High","Low","Close","Vol", "Adjusted")
head(df)
write.csv(df, "AAPL.csv")
a=read.csv("AAPL.csv")
str(a)
a
df1=read.csv("AAPL.csv", row.names=1)
df1
head(df1)
plot(Cl(df))
plot(Cl(df1))
###################################################