library(WDI)
gsyh <- WDI(country=c("CA", "TR","US"), indicator=c("NY.GDP.PCAP.CD"), start=1960, end=2020)
names(gsyh) <- c("iso2c", "Ülke", "KisiBasiGSYH", "Sene")
head(gsyh)
##   iso2c   Ülke KisiBasiGSYH Sene
## 1    CA Canada     43294.65 2020
## 2    CA Canada     46338.34 2019
## 3    CA Canada     46548.52 2018
## 4    CA Canada     45129.36 2017
## 5    CA Canada     42315.60 2016
## 6    CA Canada     43596.14 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,]   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)
## 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 = 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 
## -15.812  -4.490   1.556   4.454  11.790 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -3.80522    0.81924  -4.645  6.2e-06 ***
## x           -1.24587    0.06448 -19.322  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.42 on 198 degrees of freedom
## Multiple R-squared:  0.6535, Adjusted R-squared:  0.6517 
## F-statistic: 373.4 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.7027 -0.6053  0.0583  0.7292  3.3299 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept)  0.12039    0.07034   1.712   0.0886 .
## d(x)         0.06241    0.07062   0.884   0.3779  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9886 on 197 degrees of freedom
## Multiple R-squared:  0.00395,    Adjusted R-squared:  -0.001106 
## F-statistic: 0.7812 on 1 and 197 DF,  p-value: 0.3779