library(timeSeries)
## Loading required package: timeDate
library(forecast)
## Loading required package: zoo
##
## 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)
library(urca)
#read the file
nana<-read.csv("E:/2 presentation for class/R/importfile/nana.csv")
#check the structure of the file
str(nana)
## 'data.frame': 51 obs. of 5 variables:
## $ year : int 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 ...
## $ CO2.emission.gaseous.fuel: num 3.67 11 11 22 282.36 ...
## $ CO2.emissions : num 130402 143468 154084 150648 165972 ...
## $ CO2.emission.liquid.fuel : num 21327 24906 27128 28144 30839 ...
## $ CO2.emissions.solid.fuel : num 104957 114227 122144 117168 128818 ...
#convert teh file into time series
nanats<-ts(nana, start = c(1961, 1), end=c(2011,1), frequency = 1)
nanats
## Time Series:
## Start = 1961
## End = 2011
## Frequency = 1
## year CO2.emission.gaseous.fuel CO2.emissions CO2.emission.liquid.fuel
## 1961 1961 3.67 130402.2 21327.27
## 1962 1962 11.00 143467.7 24906.26
## 1963 1963 11.00 154083.7 27128.47
## 1964 1964 22.00 150647.7 28144.23
## 1965 1965 282.36 165972.1 30839.47
## 1966 1966 311.70 171766.0 35705.58
## 1967 1967 487.71 172239.0 32724.31
## 1968 1968 751.74 187336.0 42284.18
## 1969 1969 997.42 190724.3 45386.46
## 1970 1970 924.08 195143.1 49372.49
## 1971 1971 1085.43 205869.0 55489.04
## 1972 1972 1206.44 217849.1 58925.02
## 1973 1973 1169.77 224343.4 61799.95
## 1974 1974 1360.46 231992.8 60677.85
## 1975 1975 1734.49 252201.6 61887.96
## 1976 1976 2295.54 263785.7 63490.44
## 1977 1977 2482.56 315681.0 68613.24
## 1978 1978 2742.92 318035.2 75378.85
## 1979 1979 3032.61 331940.5 82731.19
## 1980 1980 2489.89 348581.3 83244.57
## 1981 1981 2885.93 374822.4 89786.50
## 1982 1982 4363.73 398419.5 94483.92
## 1983 1983 5247.48 432321.0 99251.02
## 1984 1984 6175.23 447110.0 106192.65
## 1985 1985 7205.66 490464.9 116856.29
## 1986 1986 10318.94 525862.5 121957.09
## 1987 1987 12786.83 561560.7 127776.62
## 1988 1988 15218.05 606298.1 133724.49
## 1989 1989 18533.02 662945.9 150838.38
## 1990 1990 21114.59 690576.8 158297.06
## 1991 1991 23582.48 737851.7 166558.81
## 1992 1992 27278.81 783634.2 188208.78
## 1993 1993 27018.46 814297.7 187031.67
## 1994 1994 29024.31 864931.6 199118.10
## 1995 1995 39082.89 920046.6 216459.34
## 1996 1996 39779.62 1002224.1 243360.46
## 1997 1997 46061.19 1043939.9 244401.88
## 1998 1998 47634.33 1071911.8 263107.25
## 1999 1999 42350.18 1144390.0 285483.28
## 2000 2000 43908.66 1186663.2 303752.28
## 2001 2001 43604.30 1203843.1 298068.43
## 2002 2002 47172.29 1226791.2 284834.23
## 2003 2003 50028.88 1281913.5 293851.38
## 2004 2004 49625.51 1348524.6 296858.32
## 2005 2005 50479.92 1411127.6 300011.94
## 2006 2006 49482.50 1504364.8 316902.14
## 2007 2007 52738.79 1611199.1 323451.40
## 2008 2008 63098.07 1793075.0 395071.58
## 2009 2009 91245.96 1965820.0 443919.69
## 2010 2010 100318.12 1950950.3 405485.86
## 2011 2011 91443.98 2074344.9 409508.56
## CO2.emissions.solid.fuel
## 1961 104956.9
## 1962 114227.1
## 1963 122144.1
## 1964 117168.0
## 1965 128818.0
## 1966 129404.8
## 1967 131740.6
## 1968 136973.5
## 1969 136463.7
## 1970 136669.1
## 1971 140449.8
## 1972 148641.9
## 1973 152440.9
## 1974 161102.3
## 1975 178465.6
## 1976 186855.6
## 1977 232986.5
## 1978 228175.4
## 1979 234849.4
## 1980 252854.3
## 1981 268618.8
## 1982 284533.5
## 1983 310858.9
## 1984 315065.0
## 1985 343975.6
## 1986 370466.0
## 1987 395926.0
## 1988 429585.4
## 1989 459625.5
## 1990 476853.0
## 1991 514436.1
## 1992 539639.4
## 1993 569712.4
## 1994 604306.9
## 1995 630295.0
## 1996 677966.0
## 1997 710004.5
## 1998 715464.7
## 1999 768665.5
## 2000 788507.7
## 2001 806575.0
## 2002 834689.9
## 2003 874660.2
## 2004 935308.7
## 2005 986632.0
## 2006 1056349.0
## 2007 1147807.7
## 2008 1240520.4
## 2009 1326515.2
## 2010 1333559.6
## 2011 1451625.9
#str(nanats)
#ploting the time series
plot.ts(nanats[,-1], type="l")
#fitting a linear equation
reg1=lm(nana$CO2.emissions~nana$year)
reg2=lm(nana$CO2.emission.gaseous.fuel~nana$year)
reg3=lm(nana$CO2.emission.liquid.fuel~nana$year)
reg4=lm(nana$CO2.emissions.solid.fuel~nana$year)
#summary of the equation
summary(reg1)
##
## Call:
## lm(formula = nana$CO2.emissions ~ nana$year)
##
## Residuals:
## Min 1Q Median 3Q Max
## -197563 -147871 -57194 95361 469858
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -69883860 3394953 -20.59 <2e-16 ***
## nana$year 35549 1709 20.80 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 179700 on 49 degrees of freedom
## Multiple R-squared: 0.8982, Adjusted R-squared: 0.8962
## F-statistic: 432.5 on 1 and 49 DF, p-value: < 2.2e-16
summary(reg2)
##
## Call:
## lm(formula = nana$CO2.emission.gaseous.fuel ~ nana$year)
##
## Residuals:
## Min 1Q Median 3Q Max
## -14351 -7625 -2331 4990 38159
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -3202321.0 219738.7 -14.57 <2e-16 ***
## nana$year 1624.1 110.6 14.68 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 11630 on 49 degrees of freedom
## Multiple R-squared: 0.8147, Adjusted R-squared: 0.8109
## F-statistic: 215.5 on 1 and 49 DF, p-value: < 2.2e-16
summary(reg3)
##
## Call:
## lm(formula = nana$CO2.emission.liquid.fuel ~ nana$year)
##
## Residuals:
## Min 1Q Median 3Q Max
## -44210 -27998 -3505 20356 101277
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.541e+07 6.386e+05 -24.14 <2e-16 ***
## nana$year 7.843e+03 3.215e+02 24.39 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 33800 on 49 degrees of freedom
## Multiple R-squared: 0.9239, Adjusted R-squared: 0.9224
## F-statistic: 595 on 1 and 49 DF, p-value: < 2.2e-16
summary(reg4)
##
## Call:
## lm(formula = nana$CO2.emissions.solid.fuel ~ nana$year)
##
## Residuals:
## Min 1Q Median 3Q Max
## -133488 -97758 -47173 75522 349796
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -47555242 2372753 -20.04 <2e-16 ***
## nana$year 24196 1195 20.25 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 125600 on 49 degrees of freedom
## Multiple R-squared: 0.8933, Adjusted R-squared: 0.8911
## F-statistic: 410.2 on 1 and 49 DF, p-value: < 2.2e-16
#plot the regression equation, where year is independent variable and FDI is depependent varaible
plot(nana$CO2.emissions~nana$year, type="l")
plot(nana$CO2.emission.gaseous.fuel~nana$year, type="l")
plot(nana$CO2.emission.liquid.fuel~nana$year, type="l")
plot(nana$CO2.emissions.solid.fuel~nana$year, type="l")
#cyclic fluctuation
#cycle(nana$Foreign.direct.investment)
#no cyclic fluctuation
#To plot the cyclic fluctuation, use box plot with cycles
#boxplot(nana$Foreign.direct.investment~cycle(nana$Foreign.direct.investment))
#no seasonal fluctuation
#moving average
ma2<-ma(nanats[,-1],2,centre = TRUE)
ma2
## Time Series:
## Start = 1961
## End = 2011
## Frequency = 1
## [,1] [,2] [,3] [,4]
## 1961 NA NA NA NA
## 1962 9.1675 142855.3 24567.06 113888.8
## 1963 13.7500 150570.7 26826.86 118920.8
## 1964 84.3400 155337.8 28564.10 121324.5
## 1965 224.6050 163589.5 31382.19 126052.2
## 1966 348.3675 170435.7 33743.74 129842.1
## 1967 509.7150 175895.0 35859.60 132464.9
## 1968 747.1525 184408.8 40669.78 135537.8
## 1969 917.6650 190981.9 45607.40 136642.5
## 1970 982.7525 196719.9 49905.12 137562.9
## 1971 1075.3450 206182.6 54818.90 141552.6
## 1972 1167.0200 216477.7 58784.76 147543.6
## 1973 1226.6100 224632.2 60800.69 153656.5
## 1974 1406.2950 235132.6 61260.90 163277.8
## 1975 1781.2450 250045.4 61986.05 176222.3
## 1976 2202.0325 273863.5 64370.52 196290.8
## 1977 2500.8950 303295.7 69023.94 220251.0
## 1978 2750.2525 320923.0 75525.53 231046.7
## 1979 2824.5075 332624.4 81021.45 237682.1
## 1980 2724.5800 350981.4 84751.71 252294.2
## 1981 3156.3700 374161.4 89325.37 268656.3
## 1982 4215.2175 400995.6 94501.34 287136.2
## 1983 5258.4800 427542.9 99794.65 305329.1
## 1984 6200.9000 454251.5 107123.15 321241.1
## 1985 7726.3725 488475.6 115465.58 343370.5
## 1986 10157.5925 525937.6 122136.77 370208.4
## 1987 12777.6625 563820.5 127808.70 397975.8
## 1988 15438.9875 609275.7 136515.99 428680.5
## 1989 18349.6700 655691.7 148424.58 456422.3
## 1990 21086.1700 695487.8 158497.83 481941.9
## 1991 23889.5900 737478.6 169905.86 511341.1
## 1992 26289.6400 779854.5 182502.01 540856.8
## 1993 27585.0100 819290.3 190347.56 570842.8
## 1994 31037.4925 866051.9 200431.80 602155.3
## 1995 36742.4275 926812.2 218849.31 635715.7
## 1996 41175.8300 992108.7 236895.54 674057.9
## 1997 44884.0825 1040503.9 248817.87 703359.9
## 1998 45920.0075 1083038.4 264024.92 727399.9
## 1999 44060.8375 1136838.8 284456.52 760325.9
## 2000 43442.9500 1180389.9 297764.07 788064.0
## 2001 44572.3875 1205285.1 296180.84 809086.9
## 2002 46994.4400 1234834.7 290397.07 837653.7
## 2003 49213.8900 1284785.7 292348.83 879829.7
## 2004 49939.9550 1347522.6 296894.99 932977.4
## 2005 50016.9625 1418786.1 303446.09 991230.4
## 2006 50545.9275 1507764.1 314316.91 1061784.4
## 2007 54514.5375 1629959.5 339719.13 1148121.2
## 2008 67545.2225 1790792.3 389378.56 1238840.9
## 2009 86477.0275 1918916.3 422099.21 1306777.6
## 2010 95831.5450 1985516.4 416099.99 1361315.1
## 2011 NA NA NA NA
ma2<-plot(ma2)
ma3<-ma(nanats[,-1],3, centre = TRUE)
plot(ma3)
ma4<-ma(nanats[,-1],4, centre = TRUE)
plot(ma4)
ma5<-ma(nanats[,-1],5, centre = TRUE)
plot(ma5)
#exponential smoothing
es1<-ses(nana$CO2.emissions, initial=c("simple"), alpha=.2)
summary(es1)
##
## Forecast method: Simple exponential smoothing
##
## Model Information:
## Simple exponential smoothing
##
## Call:
## ses(x = nana$CO2.emissions, initial = c("simple"), alpha = 0.2)
##
## Smoothing parameters:
## alpha = 0.2
##
## Initial states:
## l = 130402.19
##
## sigma: 199943.5
## Error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 154897.8 199943.5 154897.8 20.24169 20.24169 3.910466
## ACF1
## Training set 0.8985461
##
## Forecasts:
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 52 1710359 1454122 1966597 1318477 2102241
## 53 1710359 1449047 1971672 1310717 2110002
## 54 1710359 1444069 1976650 1303104 2117615
## 55 1710359 1439183 1981536 1295631 2125088
## 56 1710359 1434383 1986336 1288290 2132429
## 57 1710359 1429665 1991054 1281074 2139645
## 58 1710359 1425025 1995694 1273978 2146741
## 59 1710359 1420459 2000259 1266996 2153723
## 60 1710359 1415965 2004754 1260121 2160598
## 61 1710359 1411537 2009182 1253350 2167368
plot(es1)
es2<-ses(nana$CO2.emission.gaseous.fuel, initial=c("simple"), alpha=.2)
summary(es2)
##
## Forecast method: Simple exponential smoothing
##
## Model Information:
## Simple exponential smoothing
##
## Call:
## ses(x = nana$CO2.emission.gaseous.fuel, initial = c("simple"),
##
## Call:
## alpha = 0.2)
##
## Smoothing parameters:
## alpha = 0.2
##
## Initial states:
## l = 3.67
##
## sigma: 11452.19
## Error measures:
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 7048.373 11452.19 7048.373 41.76553 41.76553 2.819499 0.81107
##
## Forecasts:
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 52 71897.07 57220.49 86573.65 49451.18 94342.96
## 53 71897.07 56929.84 86864.30 49006.67 94787.48
## 54 71897.07 56644.73 87149.42 48570.62 95223.52
## 55 71897.07 56364.84 87429.30 48142.58 95651.57
## 56 71897.07 56089.91 87704.23 47722.11 96072.03
## 57 71897.07 55819.69 87974.46 47308.83 96485.31
## 58 71897.07 55553.93 88240.22 46902.39 96891.76
## 59 71897.07 55292.42 88501.72 46502.45 97291.70
## 60 71897.07 55034.97 88759.18 46108.71 97685.43
## 61 71897.07 54781.39 89012.76 45720.89 98073.25
plot(es2)
es3<-ses(nana$CO2.emission.liquid.fuel, initial=c("simple"), alpha=.2)
summary(es3)
##
## Forecast method: Simple exponential smoothing
##
## Model Information:
## Simple exponential smoothing
##
## Call:
## ses(x = nana$CO2.emission.liquid.fuel, initial = c("simple"),
##
## Call:
## alpha = 0.2)
##
## Smoothing parameters:
## alpha = 0.2
##
## Initial states:
## l = 21327.27
##
## sigma: 42464.74
## Error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 33613.79 42464.74 33613.79 21.75834 21.75834 3.273352
## ACF1
## Training set 0.8046592
##
## Forecasts:
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 52 364188 309767.2 418608.7 280958.6 447417.3
## 53 364188 308689.5 419686.5 279310.4 449065.6
## 54 364188 307632.3 420743.7 277693.5 450682.5
## 55 364188 306594.5 421781.5 276106.3 452269.6
## 56 364188 305575.0 422800.9 274547.2 453828.7
## 57 364188 304573.0 423802.9 273014.8 455361.2
## 58 364188 303587.6 424788.4 271507.7 456868.3
## 59 364188 302617.9 425758.0 270024.7 458351.2
## 60 364188 301663.3 426712.7 268564.7 459811.2
## 61 364188 300723.0 427652.9 267126.7 461249.2
plot(es3)
es4<-ses(nana$CO2.emissions.solid.fuel, initial=c("simple"), alpha=.2)
summary(es4)
##
## Forecast method: Simple exponential smoothing
##
## Model Information:
## Simple exponential smoothing
##
## Call:
## ses(x = nana$CO2.emissions.solid.fuel, initial = c("simple"),
##
## Call:
## alpha = 0.2)
##
## Smoothing parameters:
## alpha = 0.2
##
## Initial states:
## l = 104956.87
##
## sigma: 137453.3
## Error measures:
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 105536 137453.3 105536 19.10712 19.10712 3.859389 0.8925506
##
## Forecasts:
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 52 1181424 1005270.5 1357577 912020.4 1450827
## 53 1181424 1001781.9 1361066 906685.2 1456163
## 54 1181424 998359.9 1364488 901451.6 1461396
## 55 1181424 995000.6 1367847 896314.0 1466534
## 56 1181424 991700.8 1371147 891267.5 1471580
## 57 1181424 988457.5 1374390 886307.2 1476541
## 58 1181424 985267.7 1377580 881428.9 1481419
## 59 1181424 982129.0 1380719 876628.6 1486219
## 60 1181424 979039.0 1383809 871902.9 1490945
## 61 1181424 975995.4 1386852 867248.1 1495600
plot(es4)
#exponential smoothing is also done by holwinter method, this is also used for exponential forcasting
#emission__________________________________________________________
es_holt1<-HoltWinters(nana$CO2.emissions, beta = FALSE, gamma = FALSE)
summary(es_holt1)
## Length Class Mode
## fitted 100 mts numeric
## x 51 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
#forecast the values using exponential smoothing
es_holt_fore1<-forecast.HoltWinters(es_holt1, h=5)
es_holt_fore1
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 52 2074340 2022495 2126184 1995050 2153629
## 53 2074340 2001022 2147657 1962210 2186469
## 54 2074340 1984545 2164134 1937010 2211669
## 55 2074340 1970654 2178025 1915766 2232913
## 56 2074340 1958416 2190263 1897049 2251630
#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_fore1)
#_____________________________________________________________________
#emission liquid__________________________________________________________
es_holt2<-HoltWinters(nana$CO2.emission.gaseous.fuel, beta = FALSE, gamma = FALSE)
summary(es_holt2)
## Length Class Mode
## fitted 100 mts numeric
## x 51 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
#forecast the values using exponential smoothing
es_holt_fore2<-forecast.HoltWinters(es_holt2, h=5)
es_holt_fore2
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 52 91444.48 85181.17 97707.8 81865.57 101023.4
## 53 91444.48 82587.07 100301.9 77898.24 104990.7
## 54 91444.48 80596.51 102292.5 74853.94 108035.0
## 55 91444.48 78918.38 103970.6 72287.47 110601.5
## 56 91444.48 77439.92 105449.1 70026.35 112862.6
#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_fore2)
#_____________________________________________________________________
#emission gases__________________________________________________________
es_holt3<-HoltWinters(nana$CO2.emission.gaseous.fuel, beta = FALSE, gamma = FALSE)
summary(es_holt3)
## Length Class Mode
## fitted 100 mts numeric
## x 51 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
#forecast the values using exponential smoothing
es_holt_fore3<-forecast.HoltWinters(es_holt3, h=5)
es_holt_fore3
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 52 91444.48 85181.17 97707.8 81865.57 101023.4
## 53 91444.48 82587.07 100301.9 77898.24 104990.7
## 54 91444.48 80596.51 102292.5 74853.94 108035.0
## 55 91444.48 78918.38 103970.6 72287.47 110601.5
## 56 91444.48 77439.92 105449.1 70026.35 112862.6
#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_fore3)
#_____________________________________________________________________
#emission solid __________________________________________________________
es_holt4<-HoltWinters(nana$CO2.emissions.solid.fuel, beta = FALSE, gamma = FALSE)
summary(es_holt4)
## Length Class Mode
## fitted 100 mts numeric
## x 51 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
#forecast the values using exponential smoothing
es_holt_fore4<-forecast.HoltWinters(es_holt4, h=5)
es_holt_fore4
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 52 1451621 1416655 1486586 1398145 1505096
## 53 1451621 1402173 1501068 1375997 1527244
## 54 1451621 1391060 1512181 1359001 1544240
## 55 1451621 1381692 1521550 1344674 1558568
## 56 1451621 1373438 1529803 1332051 1571191
#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_fore4)
#_____________________________________________________________________
```