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(ani[,-1])
#fitting a linear equation
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(ani$year,ani$Foreign.direct.investment)
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(ani[,-1],2,centre = TRUE)
ma2
## Time Series:
## Start = 1
## End = 39
## Frequency = 1
## [1] NA 15449777 24479108 30202500 48597500
## [6] 74702500 83770000 60430000 25650000 37552500
## [11] 87287500 138467500 158405000 161730000 208035000
## [16] 199754410 165069429 294233135 587630989 1160135268
## [21] 1921646178 2643268049 3053842191 2753806103 2639012768
## [26] 3702243207 5038537783 5261693462 5010708002 5783687301
## [31] 10084807718 18138846662 28472719529 36905416992 35491476993
## [36] 31718449399 31097469811 28160763974 NA
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
sma2<-SMA(ani$Foreign.direct.investment,2)
plot.ts(sma2)
sma3<-SMA(ani$Foreign.direct.investment,3)
plot.ts(sma3)
sma4<-SMA(ani$Foreign.direct.investment,4)
plot.ts(sma4)
sma5<-SMA(ani$Foreign.direct.investment,5)
plot.ts(sma5)
#exponential smoothing
es<-ses(ani$Foreign.direct.investment, initial=c("simple"), alpha=.2)
summary(es)
##
## Forecast method: Simple exponential smoothing
##
## Model Information:
## Simple exponential smoothing
##
## Call:
## ses(x = ani$Foreign.direct.investment, initial = c("simple"),
##
## Call:
## 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
## 40 25654335308 16043932636 35264737980 10956492770 40352177845
## 41 25654335308 15853609156 35455061459 10665418126 40643252489
## 42 25654335308 15666911882 35641758733 10379889286 40928781330
## 43 25654335308 15483641120 35825029495 10099600842 41209069773
## 44 25654335308 15303614857 36005055758 9824274433 41484396182
## 45 25654335308 15126666647 36182003968 9553655499 41755015116
## 46 25654335308 14952643806 36356026810 9287510529 42021160086
## 47 25654335308 14781405869 36527264746 9025624704 42283045911
## 48 25654335308 14612823266 36695847349 8767799862 42540870753
## 49 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(ani[,-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 = 1
## End = 39
## 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 = 2
## End = 39
## Frequency = 1
## xhat level
## 2 10326247 10326247
## 3 8005548 8005548
## 4 32856887 32856887
## 5 19776007 19776007
## 6 45282450 45282450
## 7 75292036 75292036
## 8 90021505 90021505
## 9 74128469 74128469
## 10 13459661 13459661
## 11 18580031 18580031
## 12 96098562 96098562
## 13 115260234 115260234
## 14 201238216 201238216
## 15 103807888 103807888
## 16 235168771 235168771
## 17 236516314 236516314
## 18 92145704 92145704
## 19 255462395 255462395
## 20 516698991 516698991
## 21 921142375 921142375
## 22 2004050986 2004050986
## 23 2377874547 2377874547
## 24 3440382392 3440382392
## 25 2726645843 2726645843
## 26 2232306875 2232306875
## 27 3429863138 3429863138
## 28 5238792524 5238792524
## 29 5581825642 5581825642
## 30 4466502710 4466502710
## 31 5622322445 5622322445
## 32 7081351570 7081351570
## 33 18550809849 18550809849
## 34 24465403296 24465403296
## 35 41243705678 41243705678
## 36 36227868915 36227868915
## 37 28405161282 28405161282
## 38 35574581223 35574581223
## 39 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
## 40 27829308178 21581745027 34076871328 18274484872 37384131484
## 41 27829308178 19483054372 36175561984 15064814493 40593801862
## 42 27829308178 17814925528 37843690828 12513631580 43144984775
## 43 27829308178 16387465358 39271150998 10330519641 45328096715
## 44 27829308178 15119324921 40539291435 8391066208 47267550148
## 45 27829308178 13966711680 41691904676 6628296374 49030319981
## 46 27829308178 12902838772 42755777584 5001243168 50657373188
## 47 27829308178 11909905042 43748711314 3482682072 52175934284
## 48 27829308178 10975367883 44683248473 2053430824 53605185531
## 49 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(ani$Foreign.direct.investment,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(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$Foreign.direct.investment)
auto_arima
## Series: ani$Foreign.direct.investment
## 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: ani$Foreign.direct.investment
## 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
## 40 28153031270 21844311869 34461750671 18504677549 37801384991
## 41 28153031270 19231154731 37074907809 14508198584 41797863956
## 42 28153031270 17226008736 39080053804 11441592417 44864470123
## 43 28153031270 15535592467 40770470073 8856323829 47449738711
## 44 28153031270 14046305837 42259756703 6578656480 49727406060
## 45 28153031270 12699887806 43606174734 4519487797 51786574743
## 46 28153031270 11461728642 44844333898 2625886764 53680175776
## 47 28153031270 10309278192 45996784348 863365898 55442696642
## 48 28153031270 9226873066 47079189474 -792029892 57098092432
## 49 28153031270 8203108842 48102953698 -2357742158 58663804698
plot.forecast(ani_for)
plot.ts(ani_for$residuals)
```