-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyScript.R
More file actions
191 lines (140 loc) · 5.83 KB
/
Copy pathmyScript.R
File metadata and controls
191 lines (140 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#TIME SERIES ANALYSIS
#RELATIONSHIP BETWEEN GDP GROWTH RATE AND UNEMPLOYMENT IN KENYA
#FROM YEAR 1991 TO 2019
?read_csv
problems(read_csv)
warnings()
.libPaths()
library(readr)
KENYA <- read_csv("D:/YOUTH UNEMPLOYMENT RATE.csv",show_col_types = FALSE)
View(KENYA)
head(KENYA)
tail(KENYA)
regression<-lm(KENYA$UnemploymentRate~KENYA$GDPgrowth)
summary(regression)
adf.test(residuals(regression))
library(data.table)
setnames(mydata,"UnemploymentRate","UnemploymentRate")
str(KENYA$DATE)
str(KENYA$UnemploymentRate)
str(KENYA$GDPgrowth)
X<- as.Date(KENYA$DATE)
head(X)
year <- as.numeric(format(X,'%Y'))
month <- as.numeric(format(X,'%m'))
day <- as.numeric(format(X,'%d'))
format(X, "%m/%d/%Y")
class(X)
months(X)
quarters(X)
class(KENYA$year)
mydata <- cbind(,year)
#VISUALIZING DATA
plot(KENYA$UnemploymentRate,type = "l",main= "Youth Unemployment Rate in Kenya",xlab = "Year",ylab = "Youth Unemployment Rate",lwd=2,col="blue", xlim=c(2005,2010))
par(mfrow=c(1,2))
plot(KENYA$GDPgrowth ,type = "l",main= "GDP Growth Rate in Kenya",xlab = "Year",ylab = "GDP Growth Rate",lwd=2, col=2, xlim=c(2005,2010))
KENYA$UnemploymentRate<- ts(data=KENYA$UnemploymentRate,frequency = 1, start=c(1991,1),end=c(2019,1))
unemprate <- window(KENYA$UnemploymentRate, start=c(1991,1),end=c(2019,1))
KENYA$GDPgrowth<- ts(data= KENYA$GDPgrowth, frequency=1, start=c(1991,1),end=c(2019,1))
GDPgrowth<- window(KENYA$GDPgrowth,start=c(1991,1),end=c(2019,1))
plot.ts(KENYA$GDPgrowth,KENYA$UnemploymentRate)
plot.ts(KENYA$UnemploymentRate)
plot.ts(KENYA$GDPgrowth)
unempGDP = cbind(KENYA$GDPgrowth,KENYA$UnemploymentRate)
class(unempGDP)
plot(unempGDP, main="Multiple time series plot",col=c("blue"), lty=1:2)
#plot(unempGDP.ts, plot.type="single", main="ANNUAL PLOT ON GDP AND UNEMPLOYMENT RATE", ylab="Adjusted close price", col=c("blue", "red"), lty=1:2)
#legend(1995, 45, legend=c("Unemployment Rate","GDP"), col=c("blue", "red"),lty=1:2)
class(unemprate)
class(GDPgrowth)
start(unemprate)
frequency(unemprate)
end(unemprate)
time(GDPgrowth)
#CHECKING STATIONARITY
diffunemprate<- diff(KENYA$UnemploymentRate ,differences=2)
diffGDPgrowth<- diff(KENYA$GDPgrowth,differences = 1)
plot.ts(diffGDPgrowth, main="Differenced GDP growth rate", col=2, xlab="Year")
plot.ts(diffunemprate, main="Differenced Unemployment Rate", col=4, xlab="Year")
par(mfrow=c(1,2))
diffmodel <- lm(diffunemprate~diffGDPgrowth)
summary(diffmodel)
acf(KENYA$UnemploymentRate,main="Correlogram for Unemployment Rate in Kenya")
pacf(KENYA$UnemploymentRate,main="Partial Correlogram for Unemployment Rate in Kenya")
acf(diffunemprate, main="Correlogram for Unemployment Rate")
pacf(diffunemprate, main="Partial Correlogram for Unemployment Rate")
acf(KENYA$GDPgrowth,main="Correlogram for GDP Growth")
pacf(KENYA$GDPgrowth,main="Partial Correlogram for GDP Growth ")
acf(diffGDPgrowth,main="Correlogram for GDP Growth")
pacf(diffGDPgrowth,main="Partial Correlogram for GDP Growth")
library(tseries)
library(zoo)
adf.test(KENYA$UnemploymentRate) #fail to reject, p=0.679 data is non-stationary
adf.test(diffunemprate) #reject null, diff=2, p-value=0.0199
adf.test(KENYA$GDPgrowth) # fail to reject null, p=0.05587, data is non-stationary
adf.test(diffGDPgrowth) #reject null, diff=1, p-value=0.01
Box.test(KENYA$UnemploymentRate, lag = 10, type = c("Ljung-Box"))
Box.test(diffunemprate, lag = 10, type = c("Ljung-Box"))
Box.test(KENYA$GDPgrowth, lag = 10, type = c("Ljung-Box"))
Box.test(diffGDPgrowth, lag = 10, type = c("Ljung-Box"))
install.packages("forecast")
library(forecast)
model1<-auto.arima(KENYA$UnemploymentRate)
summary(model1)
model2<- auto.arima(KENYA$GDPgrowth)
summary(model2)
library(lmtest)
coeftest(model2)
#checking for serial correlation
library(car)
summary(regression)
durbinWatsonTest(regression)
#p-value= 0, DW=0.927313 hence reject null, there is positive serial correlation of order 1
Box.test(resid(regression),lag= 12,type = c("Ljung-Box"))
#reject null, we have joint significance upto lag 1
#correctting for serial correlation
lag_GDPgrowth<-lag(KENYA$GDPgrowth,k=1)
lag_unemprate<-lag(KENYA$UnemploymentRate,k=1)
regression2<- lm(KENYA$UnemploymentRate~lag_unemprate+KENYA$GDPgrowth)
summary(regression2)
durbinWatsonTest(regression2)
#fail to reject null, p=0.798, DW=2.559977 there is non-autocorrelation of order 1
Box.test(resid(regression2),lag= 1,type = c("Ljung-Box"))
install.packages("prais")
library(prais)
pw <- prais_winsten(KENYA$UnemploymentRate~KENYA$GDPgrowth, data=KENYA)
summary(pw)
Box.test(resid(pw),lag=10,type=c("Ljung-Box"))
library(sandwich)
newey <- NeweyWest(lm(KENYA$UnemploymentRate~KENYA$GDPgrowth),lag=2)
summary(newey)
library(lmtest)
coeftest(regression,vcov=newey)
#RUNNING VAR MODEL
Data<- window(ts.union(KENYA$UnemploymentRate,KENYA$GDPgrowth),start = c(1991,1), end = c(2019, 1), frequency = 1)
View(Data)
install.packages("vars")
library(vars)
VARselect(y=Data,lag.max=8, type = "const")
##VARSelect allows you to select the optimal number of lags
var<- VAR(y=Data, p=1, type = c("const"), ic = "SC")
summary(var)
causality(var,cause="KENYA$UnemploymentRate",vcov=vcovHC(var))
###GRANGER CAUSALITY TEST -- USE ZOO PACKAGE
install.packages("zoo")
library(zoo)
grangertest(KENYA$UnemploymentRate~KENYA$GDPgrowth,order=1)
grangertest(KENYA$GDPgrowth~KENYA$UnemploymentRate,order=1)
#IMPULSE RESPONSE FUNCTIONS
# Calculate the IRF
ir1 <- irf(var, impulse = "KENYA.UnemploymentRate", response = "KENYA.GDPgrowth", n.ahead = 10)
# Plot the IRF
plot(ir1)
ir1 <- irf()
# Calculate the IRF
ir2 <- irf(var, impulse = "KENYA.GDPgrowth", response = "KENYA.UnemploymentRate", n.ahead = 20)
# Plot the IRF
plot(ir2)
# Calculate the variance decomposition
FEVD1 <- fevd(var, n.ahead = 10)
FEVD1