Advertisement
Radeen10-_

R code (Granger Causality)

Oct 11th, 2023
1,316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.68 KB | None | 0 0
  1. library(readxl)
  2.  
  3. data1=read.csv("D:/4-2/research project/trade_value_by_section_and_year_ind_bd_imp_exp (1).csv")
  4.  
  5. View(data1)
  6.  
  7. #selecting cases of bangladesh
  8.  
  9.  
  10. bangladesh<-data1[data1$Country=="Bangladesh",]
  11. india<-data1[data1$Country=="India",]
  12. View(bangladesh)
  13.  
  14.  
  15.  
  16.  
  17. library(urca)
  18.  
  19. library(tseries)
  20.  
  21.  
  22.  
  23. # gdp1<-diff(log(bangladesh$Import.Total))
  24. #
  25. # gdp<-diff(LABOR)
  26. # plot(GDP,type='l')
  27. # adf.test(gdp,k=9)
  28. #
  29. #
  30. #
  31. # #AIC TEST FOR OPTIMAL LAG Length
  32. #
  33. # install.packages('urca')
  34. #
  35. # library(vars)
  36. # VARselect(gdp)
  37. #
  38. #
  39. #
  40. # # Define a custom function
  41. # plot_and_adf_test <- function(data, variable_name) {
  42. #   # Plot the variable
  43. #   # plot(data[[variable_name]], type = 'l', main = variable_name)
  44. #   plot.ts(data[[variable_name]])
  45. #  
  46. #   # Perform ADF test
  47. #   adf_result<-adf.test(data[[variable_name]])
  48. #   print(adf_result)
  49. #
  50. #   }
  51. #
  52. #
  53. #
  54. # adf_gdp <- plot_and_adf_test(bangladesh, "GDP.GROWTH..ANNUAL...")
  55. # adf_import<-plot_and_adf_test(bangladesh,"Import.Total")
  56. # adf_export<-plot_and_adf_test(bangladesh, "EXPORT.TOTAL")
  57. # adf_capital<-plot_and_adf_test(bangladesh,"Gross.Capital.Formation")
  58. # adf_labor<-plot_and_adf_test(bangladesh, "Total.Labor")
  59.  
  60.  
  61.  
  62.  
  63. #adf test of gdp growth, capital, import, export, labor
  64.  
  65.  
  66.  
  67. gdp<-bangladesh$GDP
  68. gdp_X=ur.df(gdp,type="drift",selectlags = "AIC")
  69. gdp_X
  70. plot(gdp,type="l")
  71. summary(gdp_X)
  72. gdp_X@lags
  73.  
  74.  
  75.  
  76.  
  77. import<-bangladesh$Import.Total
  78. import_x=ur.df(import,type="drift",selectlags ="AIC")
  79. import_x
  80. summary(import_x)
  81. import_x@lags
  82.  
  83.  
  84.  
  85. export<-bangladesh$EXPORT.TOTAL
  86. export_x=ur.df(export,type="drift",selectlags = "AIC")
  87. export_x
  88. plot(export,type=)
  89. summary(export_x)
  90. export_x@lags
  91.  
  92.  
  93. labor<-diff(bangladesh$Total.Labor)
  94. labor_x=ur.df(labor, type="drift", selectlags = "AIC")
  95. labor_x
  96. summary(labor_x)
  97. labor_x@lags
  98.  
  99.  
  100.  
  101. capital<-bangladesh$Gross.Capital.Formation
  102. capital_x=ur.df(capital,type="drift",selectlags = "AIC")
  103. capital_x
  104. summary(capital_x)
  105. capital_x@lags
  106.  
  107.  
  108. # gdp[1:,]
  109.  
  110. #granger causality test
  111.  
  112.  
  113. #null hypothesis: Time series X does not cause time series Y to Granger-cause itself.
  114.  
  115.  
  116.  
  117. library(lmtest)
  118. grangertest(gdp~import,order=2)
  119.  
  120.  
  121.  
  122.  
  123.  
  124. gdp_ind<-diff(india$GDP)
  125. gdp_x_ind=ur.df(gdp_ind,type="drift",selectlags = "AIC")
  126. gdp_x_ind
  127. plot(gdp_ind,type="l")
  128. summary(gdp_x_ind)
  129. gdp_x_ind@lags
  130.  
  131.  
  132.  
  133. import_ind<-diff(india$Import.Total)
  134. import_x_ind=ur.df(import_ind,type="drift",selectlags ="AIC")
  135. import_x_ind
  136. summary(import_x_ind)
  137. import_x_ind@lags
  138.  
  139.  
  140. export_ind<-diff(india$EXPORT.TOTAL)
  141. export_x_ind=ur.df(export_ind,type="drift",selectlags ="AIC")
  142. export_x_ind
  143. summary(export_x_ind)
  144. export_x_ind@lags
  145.  
  146.  
  147. grangertest(gdp_ind~export_ind,order=2)
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement