R Markdown

library(WDI)
enflasyon <- WDI(country=c("TR", "CN","CL"), indicator=c("FP.CPI.TOTL.ZG"), start=1960, end=2020)
names(enflasyon) <- c("iso2c", "Ulke", "enflasyonorani", "Sene")
head(enflasyon)
##   iso2c  Ulke enflasyonorani Sene
## 1    CL Chile       3.045491 2020
## 2    CL Chile       2.557545 2019
## 3    CL Chile       2.434890 2018
## 4    CL Chile       2.182718 2017
## 5    CL Chile       3.786194 2016
## 6    CL Chile       4.348774 2015
library(ggplot2)
ggplot(enflasyon, aes(Sene, enflasyonorani, color=Ulke, linetype=Ulke)) + geom_line()
## Warning: Removed 38 row(s) containing missing values (geom_path).

TR <- cbind(enflasyon$enflasyonorani[enflasyon$Ulke == "Turkey"], enflasyon$Sene[enflasyon$Ulke == "Turkey"])
TR <- TR[order(TR[,2]),]
TR
##             [,1] [,2]
##  [1,]   5.664740 1960
##  [2,]   3.172857 1961
##  [3,]   3.888320 1962
##  [4,]   6.362707 1963
##  [5,]   1.119638 1964
##  [6,]   4.555534 1965
##  [7,]   8.471996 1966
##  [8,]  13.974893 1967
##  [9,]   6.046240 1968
## [10,]   4.924194 1969
## [11,]   7.923952 1970
## [12,]  19.011409 1971
## [13,]  15.416456 1972
## [14,]  13.938165 1973
## [15,]  23.898162 1974
## [16,]  21.227355 1975
## [17,]  17.455689 1976
## [18,]  25.985338 1977
## [19,]  61.897044 1978
## [20,]  63.543108 1979
## [21,]  94.260860 1980
## [22,]  37.614783 1981
## [23,]  29.137515 1982
## [24,]  31.390271 1983
## [25,]  48.392323 1984
## [26,]  44.961730 1985
## [27,]  34.610076 1986
## [28,]  38.855843 1987
## [29,]  68.809643 1988
## [30,]  63.272552 1989
## [31,]  60.303869 1990
## [32,]  65.978568 1991
## [33,]  70.076104 1992
## [34,]  66.093843 1993
## [35,] 105.214986 1994
## [36,]  89.113317 1995
## [37,]  80.412151 1996
## [38,]  85.669362 1997
## [39,]  84.641343 1998
## [40,]  64.867488 1999
## [41,]  54.915371 2000
## [42,]  54.400189 2001
## [43,]  44.964121 2002
## [44,]  21.602438 2003
## [45,]   8.598262 2004
## [46,]   8.179160 2005
## [47,]   9.597242 2006
## [48,]   8.756181 2007
## [49,]  10.444128 2008
## [50,]   6.250977 2009
## [51,]   8.566444 2010
## [52,]   6.471880 2011
## [53,]   8.891570 2012
## [54,]   7.493090 2013
## [55,]   8.854573 2014
## [56,]   7.670854 2015
## [57,]   7.775134 2016
## [58,]  11.144311 2017
## [59,]  16.332464 2018
## [60,]  15.176822 2019
## [61,]  12.278957 2020
TR <- ts(TR[,1], start=min(enflasyon$Sene), end=max(enflasyon$Sene))
plot(TR, ylab="Enflayon Orani", xlab="Sene")

acf(TR)

pacf(TR)

plot(TR, ylab="Enflayon Orani", xlab="Sene")

library(dynlm)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
Ilkgecikme <- dynlm(TR ~ L(TR, 1))
summary(Ilkgecikme)
## 
## Time series regression with "ts" data:
## Start = 1961, End = 2020
## 
## Call:
## dynlm(formula = TR ~ L(TR, 1))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -49.776  -4.939  -1.505   2.748  42.835 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  3.69314    2.55449   1.446    0.154    
## L(TR, 1)     0.88793    0.05951  14.921   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 13.2 on 58 degrees of freedom
## Multiple R-squared:  0.7933, Adjusted R-squared:  0.7898 
## F-statistic: 222.6 on 1 and 58 DF,  p-value: < 2.2e-16
Ikincigecikme <- dynlm(TR ~ L(TR, 2))
summary(Ikincigecikme)
## 
## Time series regression with "ts" data:
## Start = 1962, End = 2020
## 
## Call:
## dynlm(formula = TR ~ L(TR, 2))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -52.555  -7.680  -4.177   5.737  42.682 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  7.01786    3.41120   2.057   0.0442 *  
## L(TR, 2)     0.79221    0.07888  10.043 3.21e-14 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 17.45 on 57 degrees of freedom
## Multiple R-squared:  0.6389, Adjusted R-squared:  0.6326 
## F-statistic: 100.9 on 1 and 57 DF,  p-value: 3.212e-14
Ucuncugecikme <- dynlm(TR ~ L(TR, 3))
summary(Ucuncugecikme)
## 
## Time series regression with "ts" data:
## Start = 1963, End = 2020
## 
## Call:
## dynlm(formula = TR ~ L(TR, 3))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -45.333 -10.116  -5.289   6.610  65.824 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 10.05948    3.99142   2.520   0.0146 *  
## L(TR, 3)     0.70723    0.09162   7.719 2.25e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 20.22 on 56 degrees of freedom
## Multiple R-squared:  0.5155, Adjusted R-squared:  0.5068 
## F-statistic: 59.58 on 1 and 56 DF,  p-value: 2.247e-10
AR10 <- dynlm(TR ~ L(TR, c(1:10)))
summary(AR10)
## 
## Time series regression with "ts" data:
## Start = 1970, End = 2020
## 
## Call:
## dynlm(formula = TR ~ L(TR, c(1:10)))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -46.627  -5.646  -1.162   4.398  40.828 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       6.568378   4.017490   1.635    0.110    
## L(TR, c(1:10))1   0.848343   0.157606   5.383 3.47e-06 ***
## L(TR, c(1:10))2  -0.009341   0.206809  -0.045    0.964    
## L(TR, c(1:10))3  -0.052168   0.205972  -0.253    0.801    
## L(TR, c(1:10))4   0.249136   0.203930   1.222    0.229    
## L(TR, c(1:10))5  -0.140615   0.206982  -0.679    0.501    
## L(TR, c(1:10))6   0.115712   0.206704   0.560    0.579    
## L(TR, c(1:10))7  -0.195278   0.203437  -0.960    0.343    
## L(TR, c(1:10))8   0.068556   0.205668   0.333    0.741    
## L(TR, c(1:10))9   0.019648   0.205821   0.095    0.924    
## L(TR, c(1:10))10 -0.080839   0.156152  -0.518    0.608    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 14.99 on 40 degrees of freedom
## Multiple R-squared:  0.7837, Adjusted R-squared:  0.7296 
## F-statistic: 14.49 on 10 and 40 DF,  p-value: 2.121e-10
n<-200
u <- ts(rnorm(n))
v <- ts(rnorm(n))
y <- ts(rep(0,n))
for (t in 2:n){
  y[t]<- y[t-1]+u[t]
}
x <- ts(rep(0,n))
for (t in 2:n){
  x[t]<- x[t-1]+v[t]
}
plot(y,type='l', ylab="y[t-1]+u[t]")

plot(x,type='l', ylab="x[t-1]+v[t]")

Spurious <- lm(y~x)
summary(Spurious)
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.4971 -3.7901  0.3893  3.2400 12.5618 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -12.2802     0.3629  -33.84   <2e-16 ***
## x            -0.9057     0.0542  -16.71   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.642 on 198 degrees of freedom
## Multiple R-squared:  0.5851, Adjusted R-squared:  0.583 
## F-statistic: 279.2 on 1 and 198 DF,  p-value: < 2.2e-16
duragan <- dynlm(d(y) ~ d(x))
summary(duragan)
## 
## Time series regression with "ts" data:
## Start = 2, End = 200
## 
## Call:
## dynlm(formula = d(y) ~ d(x))
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -3.08935 -0.69304  0.01772  0.62137  2.84283 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.11201    0.07315  -1.531    0.127
## d(x)        -0.06684    0.08494  -0.787    0.432
## 
## Residual standard error: 1.026 on 197 degrees of freedom
## Multiple R-squared:  0.003133,   Adjusted R-squared:  -0.001927 
## F-statistic: 0.6191 on 1 and 197 DF,  p-value: 0.4323
library(tseries)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
adf.test(x)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  x
## Dickey-Fuller = -1.7185, Lag order = 5, p-value = 0.694
## alternative hypothesis: stationary
adf.test(y)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  y
## Dickey-Fuller = -2.0785, Lag order = 5, p-value = 0.5432
## alternative hypothesis: stationary
adf.test(TR)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  TR
## Dickey-Fuller = -1.3924, Lag order = 3, p-value = 0.8206
## alternative hypothesis: stationary
kpss.test(TR)
## Warning in kpss.test(TR): p-value greater than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  TR
## KPSS Level = 0.32137, Truncation lag parameter = 3, p-value = 0.1
Deltax <- diff(x)
adf.test(Deltax)
## Warning in adf.test(Deltax): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  Deltax
## Dickey-Fuller = -5.1882, Lag order = 5, p-value = 0.01
## alternative hypothesis: stationary
library(WDI)
nufus <- WDI(country=c("CN", "AF","SY"), indicator=c("SP.POP.GROW"), start=1960, end=2020)
names(nufus) <- c("iso2c", "Ulke", "NufusBuyumeOrani", "Sene")
head(nufus)
##   iso2c        Ulke NufusBuyumeOrani Sene
## 1    AF Afghanistan         2.303812 2020
## 2    AF Afghanistan         2.313073 2019
## 3    AF Afghanistan         2.384309 2018
## 4    AF Afghanistan         2.547833 2017
## 5    AF Afghanistan         2.778035 2016
## 6    AF Afghanistan         3.077054 2015
library(ggplot2)
ggplot(nufus, aes(Sene, NufusBuyumeOrani, color=Ulke, linetype=Ulke)) + geom_line()
## Warning: Removed 3 row(s) containing missing values (geom_path).

CN <- cbind(nufus$NufusBuyumeOrani[nufus$Ulke == "China"], nufus$Sene[nufus$Ulke == "China"])
CN <- CN[order(CN[,2]),]
CN
##             [,1] [,2]
##  [1,]         NA 1960
##  [2,] -1.0155278 1961
##  [3,]  0.8204555 1962
##  [4,]  2.4576474 1963
##  [5,]  2.3206830 1964
##  [6,]  2.3813681 1965
##  [7,]  2.7873318 1966
##  [8,]  2.5706976 1967
##  [9,]  2.6109026 1968
## [10,]  2.7400021 1969
## [11,]  2.7616756 1970
## [12,]  2.7469155 1971
## [13,]  2.4573569 1972
## [14,]  2.2833954 1973
## [15,]  2.0659550 1974
## [16,]  1.7663918 1975
## [17,]  1.5473380 1976
## [18,]  1.3627795 1977
## [19,]  1.3381823 1978
## [20,]  1.3339280 1979
## [21,]  1.2542211 1980
## [22,]  1.2809523 1981
## [23,]  1.4726748 1982
## [24,]  1.4449497 1983
## [25,]  1.3120688 1984
## [26,]  1.3616991 1985
## [27,]  1.4873989 1986
## [28,]  1.6036051 1987
## [29,]  1.6100711 1988
## [30,]  1.5331700 1989
## [31,]  1.4673032 1990
## [32,]  1.3644340 1991
## [33,]  1.2255362 1992
## [34,]  1.1496194 1993
## [35,]  1.1302606 1994
## [36,]  1.0865092 1995
## [37,]  1.0481415 1996
## [38,]  1.0234500 1997
## [39,]  0.9595504 1998
## [40,]  0.8658514 1999
## [41,]  0.7879566 2000
## [42,]  0.7263806 2001
## [43,]  0.6699996 2002
## [44,]  0.6228609 2003
## [45,]  0.5939328 2004
## [46,]  0.5881250 2005
## [47,]  0.5583744 2006
## [48,]  0.5222719 2007
## [49,]  0.5123869 2008
## [50,]  0.4973814 2009
## [51,]  0.4829597 2010
## [52,]  0.5464576 2011
## [53,]  0.6783455 2012
## [54,]  0.6660730 2013
## [55,]  0.6303264 2014
## [56,]  0.5814561 2015
## [57,]  0.5730509 2016
## [58,]  0.6052450 2017
## [59,]  0.4676721 2018
## [60,]  0.3547409 2019
## [61,]  0.2259476 2020
CN <- ts(CN[,1], start=min(nufus$Sene), end=max(nufus$Sene))
plot(CN, ylab="Nufus Buyume Orani", xlab="Sene")

acf(TR)

pacf(TR)

library(dynlm)
Ilkgecikme <- dynlm(CN ~ L(CN, 1))
summary(Ilkgecikme)
## 
## Time series regression with "ts" data:
## Start = 1962, End = 2020
## 
## Call:
## dynlm(formula = CN ~ L(CN, 1))
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.29126 -0.15387 -0.08725  0.00153  1.54702 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.21753    0.08092   2.688   0.0094 ** 
## L(CN, 1)     0.84478    0.05451  15.498   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3247 on 57 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.8082, Adjusted R-squared:  0.8048 
## F-statistic: 240.2 on 1 and 57 DF,  p-value: < 2.2e-16
Ikincigecikme <- dynlm(CN ~ L(CN, 2))
summary(Ikincigecikme)
## 
## Time series regression with "ts" data:
## Start = 1963, End = 2020
## 
## Call:
## dynlm(formula = CN ~ L(CN, 2))
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.49134 -0.25942 -0.12898  0.08636  2.79305 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.38536    0.12559   3.068  0.00331 ** 
## L(CN, 2)     0.70974    0.08393   8.457 1.38e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4939 on 56 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.5608, Adjusted R-squared:  0.553 
## F-statistic: 71.52 on 1 and 56 DF,  p-value: 1.378e-11
Ucuncugecikme <- dynlm(CN ~ L(CN, 3))
summary(Ucuncugecikme)
## 
## Time series regression with "ts" data:
## Start = 1964, End = 2020
## 
## Call:
## dynlm(formula = CN ~ L(CN, 3))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.5790 -0.2733 -0.1402  0.1115  2.6177 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.39348    0.13073   3.010  0.00394 ** 
## L(CN, 3)     0.67991    0.08668   7.844 1.56e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5051 on 55 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.528,  Adjusted R-squared:  0.5195 
## F-statistic: 61.53 on 1 and 55 DF,  p-value: 1.565e-10
AR10 <- dynlm(CN ~ L(CN, c(1:10)))
summary(AR10)
## 
## Time series regression with "ts" data:
## Start = 1971, End = 2020
## 
## Call:
## dynlm(formula = CN ~ L(CN, c(1:10)))
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.153799 -0.026897 -0.003844  0.028790  0.151033 
## 
## Coefficients:
##                                         Estimate Std. Error t value Pr(>|t|)
## (Intercept)                            0.0064475  0.0229402   0.281 0.780152
## L(CN, c(1:10))zoo(coredata(x), tt).1   1.7751671  0.1673650  10.607  4.7e-13
## L(CN, c(1:10))zoo(coredata(x), tt).2  -1.2474057  0.3328002  -3.748 0.000577
## L(CN, c(1:10))zoo(coredata(x), tt).3   0.8773870  0.3764456   2.331 0.025032
## L(CN, c(1:10))zoo(coredata(x), tt).4  -0.7368314  0.3704370  -1.989 0.053739
## L(CN, c(1:10))zoo(coredata(x), tt).5   0.5726644  0.3403353   1.683 0.100434
## L(CN, c(1:10))zoo(coredata(x), tt).6  -0.7134078  0.2835037  -2.516 0.016081
## L(CN, c(1:10))zoo(coredata(x), tt).7   0.5997102  0.2280475   2.630 0.012166
## L(CN, c(1:10))zoo(coredata(x), tt).8  -0.2021611  0.1831883  -1.104 0.276543
## L(CN, c(1:10))zoo(coredata(x), tt).9   0.0456964  0.1381970   0.331 0.742670
## L(CN, c(1:10))zoo(coredata(x), tt).10 -0.0004422  0.0541377  -0.008 0.993525
##                                          
## (Intercept)                              
## L(CN, c(1:10))zoo(coredata(x), tt).1  ***
## L(CN, c(1:10))zoo(coredata(x), tt).2  ***
## L(CN, c(1:10))zoo(coredata(x), tt).3  *  
## L(CN, c(1:10))zoo(coredata(x), tt).4  .  
## L(CN, c(1:10))zoo(coredata(x), tt).5     
## L(CN, c(1:10))zoo(coredata(x), tt).6  *  
## L(CN, c(1:10))zoo(coredata(x), tt).7  *  
## L(CN, c(1:10))zoo(coredata(x), tt).8     
## L(CN, c(1:10))zoo(coredata(x), tt).9     
## L(CN, c(1:10))zoo(coredata(x), tt).10    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.06345 on 39 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.9899, Adjusted R-squared:  0.9873 
## F-statistic: 383.1 on 10 and 39 DF,  p-value: < 2.2e-16
n<-200
u <- ts(rnorm(n))
v <- ts(rnorm(n))
y <- ts(rep(0,n))
for (t in 2:n){
  y[t]<- y[t-1]+u[t]
}
x <- ts(rep(0,n))
for (t in 2:n){
  x[t]<- x[t-1]+v[t]
}
plot(y,type='l', ylab="y[t-1]+u[t]")

plot(x,type='l', ylab="x[t-1]+v[t]")

Spurious <- lm(y~x)
summary(Spurious)
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.8331 -2.9055 -0.5775  2.2306 10.1425 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  3.16623    0.29516   10.73   <2e-16 ***
## x           -0.52251    0.04787  -10.91   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.892 on 198 degrees of freedom
## Multiple R-squared:  0.3757, Adjusted R-squared:  0.3725 
## F-statistic: 119.1 on 1 and 198 DF,  p-value: < 2.2e-16
duragan <- dynlm(d(y) ~ d(x))
summary(duragan)
## 
## Time series regression with "ts" data:
## Start = 2, End = 200
## 
## Call:
## dynlm(formula = d(y) ~ d(x))
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.48629 -0.65269  0.03276  0.69392  2.81807 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)  0.03842    0.07160   0.537    0.592
## d(x)        -0.06295    0.07157  -0.880    0.380
## 
## Residual standard error: 1.009 on 197 degrees of freedom
## Multiple R-squared:  0.003911,   Adjusted R-squared:  -0.001145 
## F-statistic: 0.7736 on 1 and 197 DF,  p-value: 0.3802
library(tseries)
adf.test(x)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  x
## Dickey-Fuller = -2.4068, Lag order = 5, p-value = 0.4057
## alternative hypothesis: stationary
adf.test(y)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  y
## Dickey-Fuller = -2.2666, Lag order = 5, p-value = 0.4645
## alternative hypothesis: stationary
adf.test(TR)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  TR
## Dickey-Fuller = -1.3924, Lag order = 3, p-value = 0.8206
## alternative hypothesis: stationary
kpss.test(TR)
## Warning in kpss.test(TR): p-value greater than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  TR
## KPSS Level = 0.32137, Truncation lag parameter = 3, p-value = 0.1
Deltax <- diff(x)
adf.test(Deltax)
## Warning in adf.test(Deltax): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  Deltax
## Dickey-Fuller = -5.3435, Lag order = 5, p-value = 0.01
## alternative hypothesis: stationary