library(timeSeries)
library(forecast)
library(zoo)
library(foreign)
library(TTR)
library(urca)
ani<-read.csv("D:/1 Teaching Material/R/R Self/TSA Vinod sharma1.csv")
#check the structure of the file
# total funding time series
#str(ani)
#fix(ani)
#convert the file into time series
anits<-ts(ani, start = c(1980, 1), end=c(2008,1), frequency = 1)
#ploting the time series
plot(ani$Year,ani$Total) # better plot
plot.ts(anits[,])
plot.ts(ani[,-1])
#fitting a linear equation
reg=lm(ani$Total~ani$Year)
#summary of the equation
summary(reg)
#plot the regerssion equation, where year is independent variable and FDI is depependent varaible
#-------------------dont use_______________________#
#cyclic fluctuation
#To plot the cyclic fluctuation, use box plot with cycles
# boxplot(ani$Total~cycle(ani$Total))
# seasonal fluctuation
#moving average
ma2<-ma(ani[,-1],2,centre = TRUE)
ma2
plot(ma2)
ma3<-ma(ani[,-1],3, centre = TRUE)
plot(ma3)
ma4<-ma(ani[,-1],4, centre = TRUE)
plot(ma4)
ma5<-ma(ani[,-1],5, centre = TRUE)
plot(ma5)
#simple moving average, with a smoothing the time series, different method in R
sma2<-SMA(ani$Total,2)
plot.ts(sma2)
sma3<-SMA(ani$Total,3)
plot.ts(sma3)
sma4<-SMA(ani$Total,4)
plot.ts(sma4)
sma5<-SMA(ani$Total,5)
plot.ts(sma5)
#exponential smoothing
es<-ses(ani$Total, initial=c("simple"), alpha=.2)
summary(es)
plot(es)
# here we can change the values of alpha from 0-1
#exponential smoothing is also done by holwinter method, this is also used for exponential forcasting
es_holt<-HoltWinters(ani[,-1], beta = FALSE, gamma = FALSE)
summary(es_holt)
plot(es_holt$fitted)
plot(es_holt$x)
#forecast the values using exponential smoothing
es_holt_fore<-forecast.HoltWinters(es_holt, h=10)
es_holt_fore
#gives the value of alfa, less is the value of alfa, means the prediction depends on the recent values, higher the value of alfa, prediction depends on the further values
plot(es_holt_fore)
#decompostion of a time series
ani_decompose<-decompose(anits)
plot(ani_decompose)
#since the time series does not have more then two periods has no decomposition is done
#smoothing the time series data by moving average
#no cyclic fluctuation, no seasonal variation
#stationarity test, dickey fuller test
#-----------------------------------------------------------------------#
df=ur.df(ani$Total,type="none",lags=0)
#null hypothesis, b-1= 0, that is the series is not stationary
summary(df)
#----------------------------------------------#
#if series is stationaly, we can apply ARIMA Model, if not then it has to be converted into stationary by taking lag.
#differencing the series to make it stationary
ani_diff1<- diff(ani[,-1], differences=1)
plot(ani_diff1)
ani_diff2<- diff(ani[,-1], differences=2)
plot(ani_diff2)
ani_diff3<- diff(ani[,-1], differences=3)
plot(ani_diff3)
#to apply ARMA, AR and MA has to be found which is done by ACF and PCAF
#ploting two graphs in a matrix form
#par(mfrow=c(2,1))
#ploting the ACF and pacf
ani_acf<-acf(ani[,-1], lag.max = 20)
ani_pacf<-pacf(ani[,-1],lag.max = 20)
#fitting of arima automatically usingfuntion auto arima
auto_arima<-auto.arima(ani$Total)
auto_arima
summary(auto_arima)
tsdiag(auto_arima)
ani_for<-forecast.Arima(auto_arima, h=10)
ani_for
plot.forecast(ani_for)
plot.ts(ani_for$residuals)