library(WDI)
tahıl <- WDI(country=c("US", "TR","JP"), indicator=c("AG.YLD.CREL.KG"), start=1960, 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 9 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,]     NA 1960
##  [2,]  989.4 1961
##  [3,] 1136.0 1962
##  [4,] 1343.4 1963
##  [5,] 1117.9 1964
##  [6,] 1138.6 1965
##  [7,] 1272.7 1966
##  [8,] 1303.4 1967
##  [9,] 1215.7 1968
## [10,] 1291.7 1969
## [11,] 1215.0 1970
## [12,] 1573.5 1971
## [13,] 1435.1 1972
## [14,] 1238.1 1973
## [15,] 1294.2 1974
## [16,] 1632.4 1975
## [17,] 1805.5 1976
## [18,] 1791.7 1977
## [19,] 1824.6 1978
## [20,] 1880.1 1979
## [21,] 1855.1 1980
## [22,] 1871.7 1981
## [23,] 1978.7 1982
## [24,] 1838.4 1983
## [25,] 1971.1 1984
## [26,] 1931.0 1985
## [27,] 2135.5 1986
## [28,] 2137.4 1987
## [29,] 2250.2 1988
## [30,] 1742.1 1989
## [31,] 2214.2 1990
## [32,] 2240.5 1991
## [33,] 2124.4 1992
## [34,] 2255.9 1993
## [35,] 1922.3 1994
## [36,] 2057.3 1995
## [37,] 2112.3 1996
## [38,] 2151.3 1997
## [39,] 2387.5 1998
## [40,] 2099.8 1999
## [41,] 2370.6 2000
## [42,] 2178.3 2001
## [43,] 2242.0 2002
## [44,] 2308.6 2003
## [45,] 2477.9 2004
## [46,] 2634.4 2005
## [47,] 2661.9 2006
## [48,] 2410.2 2007
## [49,] 2601.3 2008
## [50,] 2808.0 2009
## [51,] 2727.1 2010
## [52,] 2970.0 2011
## [53,] 2958.3 2012
## [54,] 3256.9 2013
## [55,] 2831.5 2014
## [56,] 3307.8 2015
## [57,] 3105.4 2016
## [58,] 3257.4 2017
## [59,] 3163.9 2018
## [60,]     NA 2019
## [61,]     NA 2020
TR <- ts(TR[,1], start=min(tahıl$Sene), end=max(tahıl$Sene))

plot(TR, ylab="tahıl", 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 = 1962, End = 2018
## 
## Call:
## dynlm(formula = TR ~ L(TR, 1))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -535.45  -82.55    4.19  119.80  480.38 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 149.03349   94.11085   1.584    0.119    
## L(TR, 1)      0.94593    0.04405  21.476   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 199.6 on 55 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.8935, Adjusted R-squared:  0.8915 
## F-statistic: 461.2 on 1 and 55 DF,  p-value: < 2.2e-16
Ikincigecikme <- dynlm(TR ~ L(TR, 2))
summary(Ikincigecikme)
## 
## Time series regression with "ts" data:
## Start = 1963, End = 2018
## 
## Call:
## dynlm(formula = TR ~ L(TR, 2))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -467.84 -116.13   13.46  117.59  410.65 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 154.79140   93.71035   1.652    0.104    
## L(TR, 2)      0.96152    0.04439  21.662   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 193.7 on 54 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.8968, Adjusted R-squared:  0.8949 
## F-statistic: 469.2 on 1 and 54 DF,  p-value: < 2.2e-16
Ucuncugecikme <- dynlm(TR ~ L(TR, 3))
summary(Ucuncugecikme)
## 
## Time series regression with "ts" data:
## Start = 1964, End = 2018
## 
## Call:
## dynlm(formula = TR ~ L(TR, 3))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -498.73 -149.62  -20.58  188.16  447.05 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 186.84775  107.63272   1.736   0.0884 .  
## L(TR, 3)      0.96183    0.05153  18.666   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 217.8 on 53 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.868,  Adjusted R-squared:  0.8655 
## F-statistic: 348.4 on 1 and 53 DF,  p-value: < 2.2e-16
AR10 <- dynlm(TR ~ L(TR, c(1:10)))
summary(AR10)
## 
## Time series regression with "ts" data:
## Start = 1971, End = 2018
## 
## Call:
## dynlm(formula = TR ~ L(TR, c(1:10)))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -444.67  -88.57    3.98  131.24  277.66 
## 
## Coefficients:
##                                        Estimate Std. Error t value Pr(>|t|)  
## (Intercept)                           116.96525  117.22474   0.998   0.3249  
## L(TR, c(1:10))zoo(coredata(x), tt).1    0.22805    0.16303   1.399   0.1702  
## L(TR, c(1:10))zoo(coredata(x), tt).2    0.33265    0.16689   1.993   0.0536 .
## L(TR, c(1:10))zoo(coredata(x), tt).3   -0.01693    0.17483  -0.097   0.9234  
## L(TR, c(1:10))zoo(coredata(x), tt).4    0.22356    0.17817   1.255   0.2174  
## L(TR, c(1:10))zoo(coredata(x), tt).5    0.29460    0.18624   1.582   0.1222  
## L(TR, c(1:10))zoo(coredata(x), tt).6    0.01645    0.19139   0.086   0.9320  
## L(TR, c(1:10))zoo(coredata(x), tt).7    0.04692    0.18881   0.249   0.8051  
## L(TR, c(1:10))zoo(coredata(x), tt).8    0.05165    0.18324   0.282   0.7796  
## L(TR, c(1:10))zoo(coredata(x), tt).9   -0.09344    0.18300  -0.511   0.6127  
## L(TR, c(1:10))zoo(coredata(x), tt).10  -0.09415    0.17313  -0.544   0.5898  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 179 on 37 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.9056, Adjusted R-squared:  0.8801 
## F-statistic: 35.49 on 10 and 37 DF,  p-value: 6.042e-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 
## -12.3181  -2.5556  -0.0165   4.3529   9.3345 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 10.16493    0.55882   18.19   <2e-16 ***
## x            0.71204    0.06207   11.47   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.065 on 198 degrees of freedom
## Multiple R-squared:  0.3992, Adjusted R-squared:  0.3962 
## F-statistic: 131.6 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.2885 -0.7890  0.0608  0.6951  3.1722 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)  0.08391    0.06961   1.206    0.229
## d(x)        -0.08206    0.06396  -1.283    0.201
## 
## Residual standard error: 0.9815 on 197 degrees of freedom
## Multiple R-squared:  0.008287,   Adjusted R-squared:  0.003253 
## F-statistic: 1.646 on 1 and 197 DF,  p-value: 0.201