library(WDI)
## Warning: package 'WDI' was built under R version 4.1.2
gsyh <- WDI(country=c("US", "TR","JP"), indicator=c("NY.GDP.PCAP.CD"), start=1960, 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)
## Warning: package 'ggplot2' was built under R version 4.1.2
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,]   509.4240 1960
##  [2,]   283.8283 1961
##  [3,]   309.4466 1962
##  [4,]   350.6630 1963
##  [5,]   369.5835 1964
##  [6,]   386.3581 1965
##  [7,]   444.5495 1966
##  [8,]   481.6937 1967
##  [9,]   526.2135 1968
## [10,]   571.6178 1969
## [11,]   489.9304 1970
## [12,]   455.1049 1971
## [13,]   558.4209 1972
## [14,]   686.4901 1973
## [15,]   927.7992 1974
## [16,]  1136.3756 1975
## [17,]  1275.9566 1976
## [18,]  1427.3718 1977
## [19,]  1549.6444 1978
## [20,]  2079.2203 1979
## [21,]  1564.2472 1980
## [22,]  1579.0738 1981
## [23,]  1402.4064 1982
## [24,]  1310.2557 1983
## [25,]  1246.8245 1984
## [26,]  1368.4017 1985
## [27,]  1510.6763 1986
## [28,]  1705.8944 1987
## [29,]  1745.3649 1988
## [30,]  2021.8595 1989
## [31,]  2794.3505 1990
## [32,]  2735.7076 1991
## [33,]  2842.3700 1992
## [34,]  3180.1876 1993
## [35,]  2270.3373 1994
## [36,]  2897.8666 1995
## [37,]  3053.9472 1996
## [38,]  3144.3857 1997
## [39,]  4499.7375 1998
## [40,]  4116.1706 1999
## [41,]  4337.4780 2000
## [42,]  3142.9210 2001
## [43,]  3687.9561 2002
## [44,]  4760.1040 2003
## [45,]  6101.6321 2004
## [46,]  7456.2961 2005
## [47,]  8101.8569 2006
## [48,]  9791.8825 2007
## [49,] 10941.1721 2008
## [50,]  9103.4741 2009
## [51,] 10742.7750 2010
## [52,] 11420.5555 2011
## [53,] 11795.6335 2012
## [54,] 12614.7816 2013
## [55,] 12157.9904 2014
## [56,] 11006.2795 2015
## [57,] 10894.6034 2016
## [58,] 10589.6677 2017
## [59,]  9454.3484 2018
## [60,]  9121.5152 2019
## [61,]  8536.4333 2020
TR <- ts(TR[,1], start=min(gsyh$Sene), end=max(gsyh$Sene))
plot(TR, ylab="Kişi başı GSYH", xlab="Sene")

acf(TR)

pacf(TR)

plot(TR, ylab="Kişi başı GSYH", xlab="Sene")

library(dynlm)
## Warning: package 'dynlm' was built under R version 4.1.2
## Zorunlu paket yükleniyor: zoo
## Warning: package 'zoo' was built under R version 4.1.2
## 
## 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 
## -1903.5  -229.7   -58.7   223.2  1596.5 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 172.73437  122.19650   1.414    0.163    
## L(TR, 1)      0.99022    0.02182  45.383   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 665.2 on 58 degrees of freedom
## Multiple R-squared:  0.9726, Adjusted R-squared:  0.9721 
## F-statistic:  2060 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 
## -1719.8  -492.9  -260.8   275.6  2637.7 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 363.85199  180.93530   2.011   0.0491 *  
## L(TR, 2)      0.97998    0.03277  29.905   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 984.6 on 57 degrees of freedom
## Multiple R-squared:  0.9401, Adjusted R-squared:  0.939 
## F-statistic: 894.3 on 1 and 57 DF,  p-value: < 2.2e-16
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 
## -2281.4  -587.7  -239.6   450.4  3316.5 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 571.80137  234.78995   2.435   0.0181 *  
## L(TR, 3)      0.96755    0.04325  22.371   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1277 on 56 degrees of freedom
## Multiple R-squared:  0.8994, Adjusted R-squared:  0.8976 
## F-statistic: 500.4 on 1 and 56 DF,  p-value: < 2.2e-16
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 
## -2124.75  -277.50   -56.73   337.91  1423.78 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      236.40345  158.63707   1.490   0.1440    
## L(TR, c(1:10))1    1.03427    0.15747   6.568 7.53e-08 ***
## L(TR, c(1:10))2    0.06830    0.22724   0.301   0.7653    
## L(TR, c(1:10))3    0.12906    0.22957   0.562   0.5771    
## L(TR, c(1:10))4   -0.39876    0.23226  -1.717   0.0937 .  
## L(TR, c(1:10))5    0.25626    0.24058   1.065   0.2932    
## L(TR, c(1:10))6    0.01197    0.24336   0.049   0.9610    
## L(TR, c(1:10))7    0.02645    0.23639   0.112   0.9115    
## L(TR, c(1:10))8   -0.06448    0.24204  -0.266   0.7913    
## L(TR, c(1:10))9   -0.02921    0.24118  -0.121   0.9042    
## L(TR, c(1:10))10  -0.11036    0.19298  -0.572   0.5706    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 725 on 40 degrees of freedom
## Multiple R-squared:  0.9734, Adjusted R-squared:  0.9668 
## F-statistic: 146.7 on 10 and 40 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 
## -10.861  -6.643   1.312   5.031  13.735 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -10.94583    0.54307 -20.156   <2e-16 ***
## x             0.03003    0.08357   0.359     0.72    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.745 on 198 degrees of freedom
## Multiple R-squared:  0.0006519,  Adjusted R-squared:  -0.004395 
## F-statistic: 0.1292 on 1 and 198 DF,  p-value: 0.7197
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.5943 -0.7889 -0.0064  0.6524  3.6270 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.05484    0.07464  -0.735    0.463
## d(x)        -0.05699    0.07481  -0.762    0.447
## 
## Residual standard error: 1.053 on 197 degrees of freedom
## Multiple R-squared:  0.002937,   Adjusted R-squared:  -0.002124 
## F-statistic: 0.5803 on 1 and 197 DF,  p-value: 0.4471
library(tseries)
## Warning: package 'tseries' was built under R version 4.1.2
## 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.3057, Lag order = 5, p-value = 0.4481
## alternative hypothesis: stationary
adf.test(y)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  y
## Dickey-Fuller = -1.1648, Lag order = 5, p-value = 0.9099
## alternative hypothesis: stationary
adf.test(TR)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  TR
## Dickey-Fuller = -2.316, Lag order = 3, p-value = 0.447
## 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 = 1.348, Truncation lag parameter = 3, 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.4382, Lag order = 5, p-value = 0.01
## alternative hypothesis: stationary