R Markdown

library(WDI)
tahıl <- WDI(country=c("US", "TR","JP"), indicator=c("AG.YLD.CREL.KG"), start=1990, end=2020)
names(tahıl) <- c("iso2c", "Ülke", "tahıl", "Sene")
head(tahıl)
##   iso2c  Ülke  tahıl Sene
## 1    JP Japan     NA 2020
## 2    JP Japan     NA 2019
## 3    JP Japan 5918.8 2018
## 4    JP Japan 6048.9 2017
## 5    JP Japan 6082.9 2016
## 6    JP Japan 6091.2 2015
library(ggplot2)
ggplot(tahıl, aes(Sene, tahıl, color=Ülke, linetype=Ülke)) + geom_line()
## Warning: Removed 6 row(s) containing missing values (geom_path).

TR <- cbind(tahıl$tahıl[tahıl$Ülke == "Turkey"], tahıl$Sene[tahıl$Ülke == "Turkey"])
TR <- TR[order(TR[,2]),]
TR
##         [,1] [,2]
##  [1,] 2214.2 1990
##  [2,] 2240.5 1991
##  [3,] 2124.4 1992
##  [4,] 2255.9 1993
##  [5,] 1922.3 1994
##  [6,] 2057.3 1995
##  [7,] 2112.3 1996
##  [8,] 2151.3 1997
##  [9,] 2387.5 1998
## [10,] 2099.8 1999
## [11,] 2370.6 2000
## [12,] 2178.3 2001
## [13,] 2242.0 2002
## [14,] 2308.6 2003
## [15,] 2477.9 2004
## [16,] 2634.4 2005
## [17,] 2661.9 2006
## [18,] 2410.2 2007
## [19,] 2601.3 2008
## [20,] 2808.0 2009
## [21,] 2727.1 2010
## [22,] 2970.0 2011
## [23,] 2958.3 2012
## [24,] 3256.9 2013
## [25,] 2831.5 2014
## [26,] 3307.8 2015
## [27,] 3105.4 2016
## [28,] 3257.4 2017
## [29,] 3163.9 2018
## [30,]     NA 2019
## [31,]     NA 2020
TR <- ts(TR[,1], start=min(tahıl$Sene), end=max(tahıl$Sene))
plot(TR, ylab="tahıl", xlab="Sene")

plot(TR, ylab="tahıl oranı", xlab="Sene")

library(dynlm)
## Zorunlu paket yükleniyor: 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 = 1991, End = 2018
## 
## Call:
## dynlm(formula = TR ~ L(TR, 1))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -395.90 -101.91    2.21  149.56  474.93 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 301.0757   257.3518   1.170    0.253    
## L(TR, 1)      0.8942     0.1007   8.878 2.38e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 212.4 on 26 degrees of freedom
##   (0 observations deleted due to missingness)
## Multiple R-squared:  0.752,  Adjusted R-squared:  0.7424 
## F-statistic: 78.82 on 1 and 26 DF,  p-value: 2.376e-09
Ikincigecikme <- dynlm(TR ~ L(TR, 2))
summary(Ikincigecikme)
## 
## Time series regression with "ts" data:
## Start = 1992, End = 2018
## 
## Call:
## dynlm(formula = TR ~ L(TR, 2))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -295.98 -129.58    2.62  136.45  324.30 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  91.94077  228.17649   0.403     0.69    
## L(TR, 2)      0.99235    0.09035  10.984 4.67e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 178.2 on 25 degrees of freedom
##   (0 observations deleted due to missingness)
## Multiple R-squared:  0.8283, Adjusted R-squared:  0.8215 
## F-statistic: 120.6 on 1 and 25 DF,  p-value: 4.671e-11
Ucuncugecikme <- dynlm(TR ~ L(TR, 3))
summary(Ucuncugecikme)
## 
## Time series regression with "ts" data:
## Start = 1993, End = 2018
## 
## Call:
## dynlm(formula = TR ~ L(TR, 3))
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -446.9 -197.4   24.7  196.9  433.1 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 275.6993   317.7155   0.868    0.394    
## L(TR, 3)      0.9344     0.1271   7.354 1.35e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 237.9 on 24 degrees of freedom
##   (0 observations deleted due to missingness)
## Multiple R-squared:  0.6926, Adjusted R-squared:  0.6798 
## F-statistic: 54.08 on 1 and 24 DF,  p-value: 1.355e-07
AR10 <- dynlm(TR ~ L(TR, c(1:10)))
summary(AR10)
## 
## Time series regression with "ts" data:
## Start = 2000, End = 2018
## 
## Call:
## dynlm(formula = TR ~ L(TR, c(1:10)))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -206.27  -58.65  -19.41   72.34  214.82 
## 
## Coefficients:
##                                         Estimate Std. Error t value Pr(>|t|)
## (Intercept)                           618.455516 778.029027   0.795    0.450
## L(TR, c(1:10))zoo(coredata(x), tt).1   -0.074638   0.318014  -0.235    0.820
## L(TR, c(1:10))zoo(coredata(x), tt).2    0.517753   0.370636   1.397    0.200
## L(TR, c(1:10))zoo(coredata(x), tt).3    0.247943   0.372564   0.666    0.524
## L(TR, c(1:10))zoo(coredata(x), tt).4    0.316526   0.287644   1.100    0.303
## L(TR, c(1:10))zoo(coredata(x), tt).5    0.070952   0.344872   0.206    0.842
## L(TR, c(1:10))zoo(coredata(x), tt).6    0.009035   0.364345   0.025    0.981
## L(TR, c(1:10))zoo(coredata(x), tt).7    0.408672   0.334509   1.222    0.257
## L(TR, c(1:10))zoo(coredata(x), tt).8    0.162034   0.368433   0.440    0.672
## L(TR, c(1:10))zoo(coredata(x), tt).9   -0.393058   0.375654  -1.046    0.326
## L(TR, c(1:10))zoo(coredata(x), tt).10  -0.493540   0.445766  -1.107    0.300
## 
## Residual standard error: 169.3 on 8 degrees of freedom
##   (0 observations deleted due to missingness)
## Multiple R-squared:  0.9037, Adjusted R-squared:  0.7834 
## F-statistic: 7.509 on 10 and 8 DF,  p-value: 0.004373

\[ Random-Walk-Örneği \]

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 
## -7.5494 -3.2922 -0.3345  3.3167  7.8616 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  6.99350    0.32801  21.321  < 2e-16 ***
## x            0.11871    0.04499   2.639  0.00898 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.859 on 198 degrees of freedom
## Multiple R-squared:  0.03397,    Adjusted R-squared:  0.02909 
## F-statistic: 6.963 on 1 and 198 DF,  p-value: 0.008984
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.30615 -0.68580  0.00498  0.68149  2.50495 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)  0.01652    0.07037   0.235    0.815
## d(x)        -0.02749    0.07180  -0.383    0.702
## 
## Residual standard error: 0.9904 on 197 degrees of freedom
## Multiple R-squared:  0.0007434,  Adjusted R-squared:  -0.004329 
## F-statistic: 0.1466 on 1 and 197 DF,  p-value: 0.7023
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 = -2.263, Lag order = 5, p-value = 0.4659
## alternative hypothesis: stationary
adf.test(y)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  y
## Dickey-Fuller = -2.1502, Lag order = 5, p-value = 0.5132
## alternative hypothesis: stationary
kpss.test(TR)
## Warning in kpss.test(TR): p-value smaller than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  TR
## KPSS Level = 0.91243, Truncation lag parameter = 2, p-value = 0.01
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.416, Lag order = 5, p-value = 0.01
## alternative hypothesis: stationary

\[ KisiBasiGSYH \]

library(WDI)
gsyh <- WDI(country=c("US", "TR","JP"), indicator=c("NY.GDP.PCAP.CD"), start=1990, end=2020)
names(gsyh) <- c("iso2c", "Ülke", "KisiBasiGSYH", "Sene")
head(gsyh)
##   iso2c  Ülke KisiBasiGSYH Sene
## 1    JP Japan     40193.25 2020
## 2    JP Japan     40777.61 2019
## 3    JP Japan     39808.17 2018
## 4    JP Japan     38891.09 2017
## 5    JP Japan     39400.74 2016
## 6    JP Japan     34960.64 2015
library(ggplot2)
ggplot(gsyh, aes(Sene, KisiBasiGSYH, color=Ülke, linetype=Ülke)) + geom_line()

TR <- cbind(gsyh$KisiBasiGSYH[gsyh$Ülke == "Turkey"], gsyh$Sene[gsyh$Ülke == "Turkey"])
TR <- TR[order(TR[,2]),]
TR
##            [,1] [,2]
##  [1,]  2794.350 1990
##  [2,]  2735.708 1991
##  [3,]  2842.370 1992
##  [4,]  3180.188 1993
##  [5,]  2270.337 1994
##  [6,]  2897.867 1995
##  [7,]  3053.947 1996
##  [8,]  3144.386 1997
##  [9,]  4499.738 1998
## [10,]  4116.171 1999
## [11,]  4337.478 2000
## [12,]  3142.921 2001
## [13,]  3687.956 2002
## [14,]  4760.104 2003
## [15,]  6101.632 2004
## [16,]  7456.296 2005
## [17,]  8101.857 2006
## [18,]  9791.882 2007
## [19,] 10941.172 2008
## [20,]  9103.474 2009
## [21,] 10742.775 2010
## [22,] 11420.555 2011
## [23,] 11795.633 2012
## [24,] 12614.782 2013
## [25,] 12157.990 2014
## [26,] 11006.280 2015
## [27,] 10894.603 2016
## [28,] 10589.668 2017
## [29,]  9454.348 2018
## [30,]  9121.515 2019
## [31,]  8536.433 2020
TR <- ts(TR[,1], start=min(gsyh$Sene), end=max(gsyh$Sene))
plot(TR, ylab="Kişi başı GSYH", xlab="Sene")

library(dynlm)
Ilkgecikme <- dynlm(TR ~ L(TR, 1))
summary(Ilkgecikme)
## 
## Time series regression with "ts" data:
## Start = 1991, End = 2020
## 
## Call:
## dynlm(formula = TR ~ L(TR, 1))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1824.0  -447.3  -105.5   704.5  1558.4 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 549.82770  360.81241   1.524    0.139    
## L(TR, 1)      0.94849    0.04605  20.597   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 908.4 on 28 degrees of freedom
## Multiple R-squared:  0.9381, Adjusted R-squared:  0.9359 
## F-statistic: 424.3 on 1 and 28 DF,  p-value: < 2.2e-16
Ikincigecikme <- dynlm(TR ~ L(TR, 2))
summary(Ikincigecikme)
## 
## Time series regression with "ts" data:
## Start = 1992, End = 2020
## 
## Call:
## dynlm(formula = TR ~ L(TR, 2))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1690.8 -1101.0  -389.1  1051.9  2552.8 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 1.163e+03  5.261e+02    2.21   0.0358 *  
## L(TR, 2)    8.919e-01  6.755e-02   13.20 2.71e-13 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1324 on 27 degrees of freedom
## Multiple R-squared:  0.8659, Adjusted R-squared:  0.8609 
## F-statistic: 174.3 on 1 and 27 DF,  p-value: 2.709e-13
Ucuncugecikme <- dynlm(TR ~ L(TR, 3))
summary(Ucuncugecikme)
## 
## Time series regression with "ts" data:
## Start = 1993, End = 2020
## 
## Call:
## dynlm(formula = TR ~ L(TR, 3))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2421.1 -1394.1  -412.8  1064.4  2930.0 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 1.840e+03  6.698e+02   2.747   0.0108 *  
## L(TR, 3)    8.277e-01  8.674e-02   9.542 5.58e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1685 on 26 degrees of freedom
## Multiple R-squared:  0.7779, Adjusted R-squared:  0.7693 
## F-statistic: 91.04 on 1 and 26 DF,  p-value: 5.581e-10
AR10 <- dynlm(TR ~ L(TR, c(1:10)))
summary(AR10)
## 
## Time series regression with "ts" data:
## Start = 2000, End = 2020
## 
## Call:
## dynlm(formula = TR ~ L(TR, c(1:10)))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2026.2  -210.8   112.6   427.8  1275.0 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)  
## (Intercept)      1403.27555  835.49728   1.680   0.1240  
## L(TR, c(1:10))1     0.92175    0.30040   3.068   0.0119 *
## L(TR, c(1:10))2    -0.00597    0.40601  -0.015   0.9886  
## L(TR, c(1:10))3     0.15829    0.40679   0.389   0.7054  
## L(TR, c(1:10))4    -0.25383    0.41195  -0.616   0.5515  
## L(TR, c(1:10))5     0.15320    0.41970   0.365   0.7227  
## L(TR, c(1:10))6     0.05197    0.41509   0.125   0.9029  
## L(TR, c(1:10))7     0.06575    0.39901   0.165   0.8724  
## L(TR, c(1:10))8    -0.05601    0.40824  -0.137   0.8936  
## L(TR, c(1:10))9    -0.02648    0.40727  -0.065   0.9494  
## L(TR, c(1:10))10   -0.23250    0.33731  -0.689   0.5063  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1183 on 10 degrees of freedom
## Multiple R-squared:  0.9167, Adjusted R-squared:  0.8334 
## F-statistic: 11.01 on 10 and 10 DF,  p-value: 0.0003791
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 
## -11.1344  -4.8408  -0.4194   5.0102  10.3299 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -4.78470    0.51101  -9.363  < 2e-16 ***
## x            0.47439    0.07947   5.969 1.08e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.695 on 198 degrees of freedom
## Multiple R-squared:  0.1525, Adjusted R-squared:  0.1482 
## F-statistic: 35.63 on 1 and 198 DF,  p-value: 1.084e-08
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.16062 -0.59726  0.00619  0.74295  2.82267 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.06798    0.07476  -0.909    0.364
## d(x)        -0.06526    0.07148  -0.913    0.362
## 
## Residual standard error: 1.054 on 197 degrees of freedom
## Multiple R-squared:  0.004214,   Adjusted R-squared:  -0.0008412 
## F-statistic: 0.8336 on 1 and 197 DF,  p-value: 0.3624
library(tseries)
adf.test(x)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  x
## Dickey-Fuller = -1.8374, Lag order = 5, p-value = 0.6442
## alternative hypothesis: stationary
adf.test(y)
## Warning in adf.test(y): p-value greater than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  y
## Dickey-Fuller = -0.2258, Lag order = 5, p-value = 0.99
## alternative hypothesis: stationary
kpss.test(TR)
## Warning in kpss.test(TR): p-value smaller than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  TR
## KPSS Level = 0.95855, Truncation lag parameter = 2, p-value = 0.01
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 = -4.6767, Lag order = 5, p-value = 0.01
## alternative hypothesis: stationary