library(timeSeries)
## Warning: package 'timeSeries' was built under R version 3.2.4
## Loading required package: timeDate
library(forecast)
## Warning: package 'forecast' was built under R version 3.2.4
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 3.2.4
## 
## Attaching package: 'zoo'
## The following object is masked from 'package:timeSeries':
## 
##     time<-
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## This is forecast 7.0
library(zoo)
library(foreign)
library(TTR)
## Warning: package 'TTR' was built under R version 3.2.4
library(urca)
## Warning: package 'urca' was built under R version 3.2.4
#read the file
ani<-read.csv("E:/2 presentation for class/R/importfile/anirudh.csv")
#check the structure of the file
str(ani)
## 'data.frame':    39 obs. of  2 variables:
##  $ year                     : int  1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 ...
##  $ Foreign.direct.investment: num  10326247 7706431 36060000 18090000 48570000 ...
#convert teh file into time series

anits<-ts(ani, start = c(1975, 1), end=c(2013,1), frequency = 1)

#str(anits)


#ploting the time series

plot.ts(anits)

#fitting  a linear equation

reg=lm(anits[,-1]~anits[,1])

reg=lm(ani$Foreign.direct.investment~ani$year)

#summary of the equation

summary(reg)
## 
## Call:
## lm(formula = ani$Foreign.direct.investment ~ ani$year)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -1.044e+10 -6.351e+09 -7.339e+08  5.065e+09  2.455e+10 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -1.625e+12  2.290e+11  -7.094 2.12e-08 ***
## ani$year     8.184e+08  1.148e+08   7.127 1.92e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 8.072e+09 on 37 degrees of freedom
## Multiple R-squared:  0.5785, Adjusted R-squared:  0.5671 
## F-statistic: 50.79 on 1 and 37 DF,  p-value: 1.924e-08
#plot the regerssion equation, where year is independent variable and FDI is depependent varaible

plot.ts(anits[,1],anits[,-1])
abline(reg)

#cyclic fluctuation

cycle(ani$Foreign.direct.investment)
##  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [36] 1 1 1 1
## attr(,"tsp")
## [1]  1 39  1
#no cyclic fluctuation

#To plot the cyclic fluctuation, use box plot with cycles
#boxplot(ani$Foreign.direct.investment~cycle(ani$Foreign.direct.investment))


#no seasonal fluctuation
#moving average

ma2<-ma(anits[,-1],2,centre = TRUE)

ma2<-ma(anits[,-1],2,centre = TRUE)
plot(ma2)

ma3<-ma(anits[,-1],3, centre = TRUE)
plot(ma3)

ma4<-ma(anits[,-1],4, centre = TRUE)
plot(ma4)

ma5<-ma(anits[,-1],5, centre = TRUE)
plot(ma5)

#simple moving average, with a smoothing the time series

sma2<-SMA(anits[,-1],2)

sma2<-SMA(anits[,-1],2)
plot.ts(sma2)

sma3<-SMA(anits[,-1],3)
plot.ts(sma3)

sma4<-SMA(anits[,-1],4)
plot.ts(sma4)

sma5<-SMA(anits[,-1],5)
plot.ts(sma5)

#exponential smoothing

es<-ses(anits[,-1], initial=c("simple"), alpha=.2)

summary(es)
## 
## Forecast method: Simple exponential smoothing
## 
## Model Information:
## Simple exponential smoothing 
## 
## Call:
##  ses(x = anits[, -1], initial = c("simple"), alpha = 0.2) 
## 
##   Smoothing parameters:
##     alpha = 0.2 
## 
##   Initial states:
##     l = 10326246.71 
## 
##   sigma:  7499037050
## Error measures:
##                      ME       RMSE        MAE      MPE     MAPE     MASE
## Training set 3287693469 7499037050 3361491009 16.76767 70.98136 1.397488
##                   ACF1
## Training set 0.7035792
## 
## Forecasts:
##      Point Forecast       Lo 80       Hi 80       Lo 95       Hi 95
## 2014    25654335308 16043932636 35264737980 10956492770 40352177845
## 2015    25654335308 15853609156 35455061459 10665418126 40643252489
## 2016    25654335308 15666911882 35641758733 10379889286 40928781330
## 2017    25654335308 15483641120 35825029495 10099600842 41209069773
## 2018    25654335308 15303614857 36005055758  9824274433 41484396182
## 2019    25654335308 15126666647 36182003968  9553655499 41755015116
## 2020    25654335308 14952643806 36356026810  9287510529 42021160086
## 2021    25654335308 14781405869 36527264746  9025624704 42283045911
## 2022    25654335308 14612823266 36695847349  8767799862 42540870753
## 2023    25654335308 14446776173 36861894442  8513852749 42794817866
plot(es)

#exponential smoothing is also done by holwinter method, this is also used for exponential forcasting

es_holt<-HoltWinters(anits[,-1], beta = FALSE, gamma = FALSE)

summary(es_holt)
##              Length Class  Mode     
## fitted       76     mts    numeric  
## x            39     ts     numeric  
## alpha         1     -none- numeric  
## beta          1     -none- logical  
## gamma         1     -none- logical  
## coefficients  1     -none- numeric  
## seasonal      1     -none- character
## SSE           1     -none- numeric  
## call          4     -none- call
es_holt$x
## Time Series:
## Start = 1975 
## End = 2013 
## Frequency = 1 
##  [1]    10326247     7706431    36060000    18090000    48570000
##  [6]    79160000    91920000    72080000     5640000    19240000
## [11]   106090000   117730000   212320000    91250000   252100000
## [16]   236690000    73537638   276512439   550370025   973271469
## [21]  2143628110  2426057022  3577330042  2634651658  2168591054
## [26]  3584217307  5471947158  5626039508  4322747673  5771297153
## [31]  7269407226 20029119267 25227740887 43406277076 35581372930
## [36] 27396885034 36498654598 23995685014 28153031270
es_holt$fitted
## Time Series:
## Start = 1976 
## End = 2013 
## Frequency = 1 
##             xhat       level
## 1976    10326247    10326247
## 1977     8005548     8005548
## 1978    32856887    32856887
## 1979    19776007    19776007
## 1980    45282450    45282450
## 1981    75292036    75292036
## 1982    90021505    90021505
## 1983    74128469    74128469
## 1984    13459661    13459661
## 1985    18580031    18580031
## 1986    96098562    96098562
## 1987   115260234   115260234
## 1988   201238216   201238216
## 1989   103807888   103807888
## 1990   235168771   235168771
## 1991   236516314   236516314
## 1992    92145704    92145704
## 1993   255462395   255462395
## 1994   516698991   516698991
## 1995   921142375   921142375
## 1996  2004050986  2004050986
## 1997  2377874547  2377874547
## 1998  3440382392  3440382392
## 1999  2726645843  2726645843
## 2000  2232306875  2232306875
## 2001  3429863138  3429863138
## 2002  5238792524  5238792524
## 2003  5581825642  5581825642
## 2004  4466502710  4466502710
## 2005  5622322445  5622322445
## 2006  7081351570  7081351570
## 2007 18550809849 18550809849
## 2008 24465403296 24465403296
## 2009 41243705678 41243705678
## 2010 36227868915 36227868915
## 2011 28405161282 28405161282
## 2012 35574581223 35574581223
## 2013 25317703737 25317703737
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
##      Point Forecast       Lo 80       Hi 80       Lo 95       Hi 95
## 2014    27829308178 21581745027 34076871328 18274484872 37384131484
## 2015    27829308178 19483054372 36175561984 15064814493 40593801862
## 2016    27829308178 17814925528 37843690828 12513631580 43144984775
## 2017    27829308178 16387465358 39271150998 10330519641 45328096715
## 2018    27829308178 15119324921 40539291435  8391066208 47267550148
## 2019    27829308178 13966711680 41691904676  6628296374 49030319981
## 2020    27829308178 12902838772 42755777584  5001243168 50657373188
## 2021    27829308178 11909905042 43748711314  3482682072 52175934284
## 2022    27829308178 10975367883 44683248473  2053430824 53605185531
## 2023    27829308178 10089995600 45568620756   699370766 54959245590
#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(anits[,-1],type="none",lags=0)

#null hypothesis, b-1= 0, that is the series is not stationary

summary(df)
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression none 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 - 1)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -1.217e+10 -1.866e+07  5.903e+07  1.178e+09  1.841e+10 
## 
## Coefficients:
##          Estimate Std. Error t value Pr(>|t|)
## z.lag.1 -0.009081   0.059346  -0.153    0.879
## 
## Residual standard error: 4.987e+09 on 37 degrees of freedom
## Multiple R-squared:  0.0006325,  Adjusted R-squared:  -0.02638 
## F-statistic: 0.02342 on 1 and 37 DF,  p-value: 0.8792
## 
## 
## Value of test-statistic is: -0.153 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau1 -2.62 -1.95 -1.61
#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(anits[,-1], differences=1)
plot(ani_diff1)

ani_diff2<- diff(anits[,-1], differences=2)
plot(ani_diff2)

ani_diff3<- diff(anits[,-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(anits[,-1], lag.max = 20)

ani_pacf<-pacf(anits[,-1],lag.max = 20)

#fitting of arima automatically usingfuntion auto arima


auto_arima<-auto.arima(anits[,-1])

auto_arima
## Series: anits[, -1] 
## ARIMA(0,1,0)                    
## 
## sigma^2 estimated as 2.423e+19:  log likelihood=-901.97
## AIC=1805.94   AICc=1806.05   BIC=1807.58
summary(auto_arima)
## Series: anits[, -1] 
## ARIMA(0,1,0)                    
## 
## sigma^2 estimated as 2.423e+19:  log likelihood=-901.97
## AIC=1805.94   AICc=1806.05   BIC=1807.58
## 
## Training set error measures:
##                     ME       RMSE        MAE      MPE     MAPE      MASE
## Training set 721608086 4859198278 2343705246 -22.3882 74.60231 0.9743591
##                   ACF1
## Training set -0.170285
tsdiag(auto_arima)

ani_for<-forecast.Arima(auto_arima, h=10)

ani_for
##      Point Forecast       Lo 80       Hi 80       Lo 95       Hi 95
## 2014    28153031270 21844311869 34461750671 18504677549 37801384991
## 2015    28153031270 19231154731 37074907809 14508198584 41797863956
## 2016    28153031270 17226008736 39080053804 11441592417 44864470123
## 2017    28153031270 15535592467 40770470073  8856323829 47449738711
## 2018    28153031270 14046305837 42259756703  6578656480 49727406060
## 2019    28153031270 12699887806 43606174734  4519487797 51786574743
## 2020    28153031270 11461728642 44844333898  2625886764 53680175776
## 2021    28153031270 10309278192 45996784348   863365898 55442696642
## 2022    28153031270  9226873066 47079189474  -792029892 57098092432
## 2023    28153031270  8203108842 48102953698 -2357742158 58663804698
plot.forecast(ani_for)

plot.ts(ani_for$residuals)

```