I. Introduction

Briefly describe what is time series analysis (1-2 paragraphs). Please cite at least 1 external reference to develop your explanation.

## A time series analysis is a technique used in data analysis that serves to interpret data that is structured sequentially in different time intervals. This type of information allows analysts to interpret and analyze changes in a dependent variable over time.

II. Background

III. Description of the Problem Situation

What is the problem situation? How to address the problem situation?

## The main problem that was raised in the evidence is the challenge of understanding the behavior of foreign investments in Mexico in order to detect trends and make predictions about this opportunity for the country in the future.

## Time series analysis works perfectly for this task, since it allows us to analyze past data that may be significant for this type of investment to happen.

IV. Data and Methodology

Briefly describe the dataset’s selected variables

## Within the database there are different variables that could be significant in determining foreign investment in the country.

## Variables:
## - IED_Flujos = This variable captures the amount of foreign direct investments that have been received in the country in monetary terms.
## - Exportaciones = This variable measures the amount of non-oil exports that have been carried out in monetary terms.
## - Empleo = This variable captures the employment index of the country
## - Educacion = This variable captures the average level of education in the country.
## - Salario_Diario = Reflects the minimum salary over the years.
## - Innovacion = This variable measures the innovative spirit of the country.
## - Inseguridad_Homicidio = This variable captures the rate of homicides committed per year.
## - Inseguridad_Robo = This variable captures the rate of robberies in the country that have occurred per year.
## - Tipo_de_Cambio = This variable shows how the cost of a dollar in pesos has been varying.
## - Densidad_Carretera = This variable measures the distribution of the population in the country.
## - Densidad_Poblacion = This variable measures the density of the population in the country.
## CO2_Emisiones = This variable measures the CO2 emissions generated in the country.
## PIB_per_Capita = This variable measures an average individual economic representation of the country.
## INPC = This variable measures the country's inflationary trends.

Loading Libraries

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.3
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.2.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(xts)
## Warning: package 'xts' was built under R version 4.2.3
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.2.3
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## ######################### Warning from 'xts' package ##########################
## #                                                                             #
## # The dplyr lag() function breaks how base R's lag() function is supposed to  #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or       #
## # source() into this session won't work correctly.                            #
## #                                                                             #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop           #
## # dplyr from breaking base R's lag() function.                                #
## #                                                                             #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning.  #
## #                                                                             #
## ###############################################################################
## 
## Attaching package: 'xts'
## The following objects are masked from 'package:dplyr':
## 
##     first, last
library(dplyr)
library(zoo)
library(tseries)
## Warning: package 'tseries' was built under R version 4.2.3
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(stats)
library(forecast)
## Warning: package 'forecast' was built under R version 4.2.3
library(astsa)
## 
## Attaching package: 'astsa'
## The following object is masked from 'package:forecast':
## 
##     gas
library(corrplot)
## Warning: package 'corrplot' was built under R version 4.2.3
## corrplot 0.92 loaded
library(AER)
## Warning: package 'AER' was built under R version 4.2.3
## Loading required package: car
## Warning: package 'car' was built under R version 4.2.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.2.3
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
## Loading required package: lmtest
## Warning: package 'lmtest' was built under R version 4.2.3
## Loading required package: sandwich
## Warning: package 'sandwich' was built under R version 4.2.3
## Loading required package: survival
library(vars)
## Warning: package 'vars' was built under R version 4.2.3
## Loading required package: MASS
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
## 
##     select
## Loading required package: strucchange
## Warning: package 'strucchange' was built under R version 4.2.3
## Loading required package: urca
## Warning: package 'urca' was built under R version 4.2.3
library(dynlm)
## Warning: package 'dynlm' was built under R version 4.2.3
library(vars)
library(TSstudio)
## Warning: package 'TSstudio' was built under R version 4.2.3
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2
## ──
## ✔ tibble  3.2.1     ✔ purrr   1.0.1
## ✔ tidyr   1.3.0     ✔ stringr 1.5.0
## ✔ readr   2.1.4     ✔ forcats 1.0.0
## Warning: package 'tibble' was built under R version 4.2.3
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ lubridate::as.difftime() masks base::as.difftime()
## ✖ stringr::boundary()      masks strucchange::boundary()
## ✖ lubridate::date()        masks base::date()
## ✖ dplyr::filter()          masks stats::filter()
## ✖ xts::first()             masks dplyr::first()
## ✖ lubridate::intersect()   masks base::intersect()
## ✖ dplyr::lag()             masks stats::lag()
## ✖ xts::last()              masks dplyr::last()
## ✖ car::recode()            masks dplyr::recode()
## ✖ MASS::select()           masks dplyr::select()
## ✖ lubridate::setdiff()     masks base::setdiff()
## ✖ purrr::some()            masks car::some()
## ✖ lubridate::union()       masks base::union()
library(sarima)
## Warning: package 'sarima' was built under R version 4.2.3
## Loading required package: stats4
## 
## Attaching package: 'sarima'
## 
## The following object is masked from 'package:astsa':
## 
##     sarima
## 
## The following object is masked from 'package:stats':
## 
##     spectrum
library(readxl)
## Warning: package 'readxl' was built under R version 4.2.3
library(dygraphs)
## Warning: package 'dygraphs' was built under R version 4.2.3

Reading the files

time_data <- read.csv("D:/JahirBotello/Documents/Carrera LIT/5to Semestre/1er Periodo/Economics/sp_data_time.csv", TRUE)
comp_data <- read.csv("D:/JahirBotello/Documents/Carrera LIT/5to Semestre/1er Periodo/Economics/sp_data.csv", TRUE)

Plot the variable IED_Flujos using a time series format:

time_data$Date <- as.Date(paste0(time_data$Año, "-", ifelse(time_data$Trimestre == "I", "01-01",
                                                ifelse(time_data$Trimestre == "II", "04-01",
                                                ifelse(time_data$Trimestre == "III", "07-01",
                                                       "10-01")))), format="%Y-%m-%d")
par(mfrow = c(1,1))
plot(time_data$Date, time_data$IED_Flujos, type = "l", col = "blue", lwd = 2, xlab = "Date", ylab = "IED_Flujos", main = "IED_Flujos")

i) decompose the time series data into trend, seasonal, and random components. Briefly, describe the decomposition time series plot. Do the time series data show a trend? Do the time series data show seasonality?

ts1 <- ts(time_data$IED_Flujos, start = c(1999,1), frequency = 4)
ts1
##          Qtr1     Qtr2     Qtr3     Qtr4
## 1999  3596.08  3395.89  3028.45  3939.90
## 2000  4600.64  4857.42  3056.95  5733.68
## 2001  3598.68  5218.83 16314.05  4925.63
## 2002  5067.98  6258.52  6114.34  6658.37
## 2003  3963.69  5547.34  2521.68  6217.27
## 2004  9363.46  4351.50  3284.91  8015.70
## 2005  6761.62  6773.62  5478.92  6781.66
## 2006  7436.81  6634.31  2346.57  4814.85
## 2007 10815.78  6137.64  7628.45  7811.47
## 2008  8546.44  8376.14  5643.68  6936.20
## 2009  6105.30  6094.18  2397.52  3252.95
## 2010  8722.26  9301.62  3932.89  5232.50
## 2011  8431.21  6697.41  4349.89  6154.00
## 2012  7892.69  5622.13  5736.29  2518.21
## 2013 10571.58 21019.14  4178.81 12584.89
## 2014 13828.00  5478.74  3222.08  7822.43
## 2015 12136.33  6656.85  9635.56  7515.02
## 2016 12805.36  6210.62  4317.27  7855.73
## 2017 13779.06  6814.16  6361.22  7062.61
## 2018 14067.52  9577.75  4132.97  6322.19
## 2019 15175.27  6504.62  8217.40  4679.87
## 2020 16807.60  7293.96  1340.58  2763.75
## 2021 16206.05  5883.73  6419.43  3044.31
## 2022 22794.16  8164.27  3479.68  1777.26
decomp_ts1<-decompose(ts1)
plot(decomp_ts1)

ii) detect the presence of stationary

adf.test(ts1)
## Warning in adf.test(ts1): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  ts1
## Dickey-Fuller = -4.1994, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary

iii) detect the presence of serial autocorrelation

acf(ts1, main="Serial Autocorrelation")

V. Time Series Regression Analysis

a. Time Series Model 1

Estimate 2 different time series regression models. You might want to consider ARMA (p,q) and / or ARIMA (p,d,q).

Model 1 - ARMA

time_data_arma <- arima(ts1, order=c(1,0,1))
summary(time_data_arma)
## 
## Call:
## arima(x = ts1, order = c(1, 0, 1))
## 
## Coefficients:
##          ar1      ma1  intercept
##       0.9707  -0.9342   6927.254
## s.e.  0.0429   0.0524    770.938
## 
## sigma^2 estimated as 15377549:  log likelihood = -930.63,  aic = 1869.25
## 
## Training set error measures:
##                    ME     RMSE     MAE       MPE     MAPE      MASE        ACF1
## Training set 177.3561 3921.422 2720.92 -28.88909 50.83414 0.6989972 -0.04302898

Model 2 - ARIMA

time_data_arima <- arima(ts1, order=c(1,1,1))
summary(time_data_arima)
## 
## Call:
## arima(x = ts1, order = c(1, 1, 1))
## 
## Coefficients:
##           ar1      ma1
##       -0.0520  -0.9399
## s.e.   0.1072   0.0323
## 
## sigma^2 estimated as 15534684:  log likelihood = -922.46,  aic = 1850.91
## 
## Training set error measures:
##                   ME     RMSE      MAE      MPE     MAPE      MASE       ACF1
## Training set 623.143 3920.824 2632.375 -19.3116 47.12741 0.6762502 -0.0533437

Based on diagnostic tests, compare the 2 estimated time series regression models, and select the results that you consider might generate the best forecast.

Model ARMA - Diagnostic Tests

AIC(time_data_arma)
## [1] 1869.251
acf(residuals(time_data_arma))

adf.test(residuals(time_data_arma))
## Warning in adf.test(residuals(time_data_arma)): p-value smaller than printed
## p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  residuals(time_data_arma)
## Dickey-Fuller = -4.3116, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary
plot(residuals(time_data_arma))

Model ARIMA - Diagnostic Tests

AIC(time_data_arima)
## [1] 1850.911
acf(residuals(time_data_arima))

adf.test(residuals(time_data_arima))
## Warning in adf.test(residuals(time_data_arima)): p-value smaller than printed
## p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  residuals(time_data_arima)
## Dickey-Fuller = -4.3162, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary
plot(residuals(time_data_arima))

By using the selected model, make a forecast for the next 5 periods. In doing so, include a time series plot showing your forecast.

Forecast_time_data_arima <- forecast(time_data_arima, h = 5) # Forecasting the next 5 periods
plot(Forecast_time_data_arima, main= "Forecast for next 5 periods", xlab= "Time",
ylab="IED_Flujos")

b. Time Series Model 2

From the time series dataset, select the explanatory variables that might explain the Nearshoring in Mexico.

## Dependent Variable
## - IED_Flujos - Foreign direct investments

## Independetn Variables
## - Exportaciones
## - Empleo
## - Educacion
## - Salario_Diario
## - Inseguridad_Robo
## - Tipo_de_Cambio
## - PIB_per_Capita

Cleaning the Database

#Identify Missing Values
missingval <- colSums(is.na(comp_data))
missingval
##               periodo            IED_Flujos                 IED_M 
##                     0                     0                     0 
##         Exportaciones       Exportaciones_m                Empleo 
##                     0                     0                     3 
##             Educacion        Salario_Diario            Innovacion 
##                     3                     0                     2 
##      Inseguridad_Robo Inseguridad_Homicidio        Tipo_de_Cambio 
##                     0                     1                     0 
##    Densidad_Carretera    Densidad_Poblacion         CO2_Emisiones 
##                     0                     0                     3 
##        PIB_Per_Capita                  INPC     crisis_financiera 
##                     0                     0                     0
medianaempleo <- median(comp_data$Empleo, na.rm = TRUE)
comp_data$Empleo <- ifelse(is.na(comp_data$Empleo), medianaempleo, comp_data$Empleo)

medianaedu <- median(comp_data$Educacion, na.rm = TRUE)
comp_data$Educacion <- ifelse(is.na(comp_data$Educacion), medianaedu, comp_data$Educacion)

medianaino <- median(comp_data$Innovacion, na.rm = TRUE)
comp_data$Innovacion <- ifelse(is.na(comp_data$Innovacion), medianaino, comp_data$Innovacion)

medianainshom <- median(comp_data$Inseguridad_Homicidio, na.rm = TRUE)
comp_data$Inseguridad_Homicidio <- ifelse(is.na(comp_data$Inseguridad_Homicidio), medianainshom, comp_data$Inseguridad_Homicidio)

medianaco2 <- median(comp_data$CO2_Emisiones, na.rm = TRUE)
comp_data$CO2_Emisiones <- ifelse(is.na(comp_data$CO2_Emisiones), medianaco2, comp_data$CO2_Emisiones)

comp_data
##    periodo IED_Flujos    IED_M Exportaciones Exportaciones_m Empleo Educacion
## 1     1997   12145.60 294151.2       9087.62        220090.8  96.53      7.20
## 2     1998    8373.50 210875.6       9875.07        248690.6  96.53      7.31
## 3     1999   13960.32 299734.4      10990.01        235960.5  96.53      7.43
## 4     2000   18248.69 362631.8      12482.96        248057.2  97.83      7.56
## 5     2001   30057.18 546548.4      11300.44        205482.9  97.36      7.68
## 6     2002   24099.21 468332.0      11923.10        231707.6  97.66      7.80
## 7     2003   18249.97 368752.8      13156.00        265825.7  97.06      7.93
## 8     2004   25015.57 481349.2      13573.13        261173.9  96.48      8.04
## 9     2005   25795.82 458544.8      16465.81        292695.1  97.17      8.14
## 10    2006   21232.54 368495.8      17485.93        303472.5  96.53      8.26
## 11    2007   32393.33 542793.7      19103.85        320110.6  96.60      8.36
## 12    2008   29502.46 586217.7      16924.76        336297.2  95.68      8.46
## 13    2009   17849.95 324318.4      19702.63        357980.1  95.20      8.56
## 14    2010   27189.28 449223.7      22673.14        374607.6  95.06      8.63
## 15    2011   25632.52 460653.8      24333.02        437299.9  95.49      8.75
## 16    2012   21769.32 350978.6      26297.98        423992.5  95.53      8.85
## 17    2013   48354.42 754437.5      27687.57        431988.2  95.75      8.95
## 18    2014   30351.25 512758.2      31676.78        535151.9  96.24      9.05
## 19    2015   35943.75 699904.1      29959.94        583386.1  96.04      9.15
## 20    2016   31188.98 700091.6      31375.06        704268.5  96.62      9.25
## 21    2017   34017.05 683318.0      33322.62        669368.6  96.85      9.35
## 22    2018   34100.43 671018.4      35341.90        695447.7  96.64      9.45
## 23    2019   34577.16 615945.4      36414.73        648679.3  97.09      9.58
## 24    2020   28205.89 514711.7      41077.34        749594.7  96.21      8.46
## 25    2021   31553.52 551937.8      44914.78        785654.5  96.49      8.46
## 26    2022   36215.37 555771.9      46477.59        713259.0  97.24      8.46
##    Salario_Diario Innovacion Inseguridad_Robo Inseguridad_Homicidio
## 1           24.30      11.30           266.51                 14.55
## 2           31.91      11.37           314.78                 14.32
## 3           31.91      12.46           272.89                 12.64
## 4           35.12      13.15           216.98                 10.86
## 5           37.57      13.47           214.53                 10.25
## 6           39.74      12.80           197.80                  9.94
## 7           41.53      11.81           183.22                  9.81
## 8           43.30      12.61           146.28                  8.92
## 9           45.24      13.41           136.94                  9.22
## 10          47.05      14.23           135.59                  9.60
## 11          48.88      15.04           145.92                  8.04
## 12          50.84      14.82           158.17                 12.52
## 13          53.19      12.59           175.77                 17.46
## 14          55.77      12.69           201.94                 22.43
## 15          58.06      12.10           212.61                 23.42
## 16          60.75      13.03           190.28                 22.09
## 17          63.12      13.22           185.56                 19.74
## 18          65.58      13.65           154.41                 16.93
## 19          70.10      15.11           180.44                 17.37
## 20          73.04      14.40           160.57                 20.31
## 21          88.36      14.05           230.43                 26.22
## 22          88.36      13.25           184.25                 29.59
## 23         102.68      12.70           173.45                 29.21
## 24         123.22      11.28           133.90                 28.98
## 25         141.70      13.09           127.13                 27.89
## 26         172.87      13.09           120.49                 16.93
##    Tipo_de_Cambio Densidad_Carretera Densidad_Poblacion CO2_Emisiones
## 1            8.06               0.05              47.44          3.68
## 2            9.94               0.05              48.76          3.85
## 3            9.52               0.06              49.48          3.69
## 4            9.60               0.06              50.58          3.87
## 5            9.17               0.06              51.28          3.81
## 6           10.36               0.06              51.95          3.82
## 7           11.20               0.06              52.61          3.95
## 8           11.22               0.06              53.27          3.98
## 9           10.71               0.06              54.78          4.10
## 10          10.88               0.06              55.44          4.19
## 11          10.90               0.06              56.17          4.22
## 12          13.77               0.07              56.96          4.19
## 13          13.04               0.07              57.73          4.04
## 14          12.38               0.07              58.45          4.11
## 15          13.98               0.07              59.15          4.19
## 16          12.99               0.07              59.85          4.20
## 17          13.07               0.08              59.49          4.06
## 18          14.73               0.08              60.17          3.89
## 19          17.34               0.08              60.86          3.93
## 20          20.66               0.08              61.57          3.89
## 21          19.74               0.09              62.28          3.84
## 22          19.66               0.09              63.11          3.65
## 23          18.87               0.09              63.90          3.59
## 24          19.94               0.09              64.59          3.93
## 25          20.52               0.09              65.16          3.93
## 26          19.41               0.09              65.60          3.93
##    PIB_Per_Capita   INPC crisis_financiera
## 1        127570.1  33.28                 0
## 2        126738.8  39.47                 0
## 3        129164.7  44.34                 0
## 4        130874.9  48.31                 0
## 5        128083.4  50.43                 0
## 6        128205.9  53.31                 0
## 7        128737.9  55.43                 0
## 8        132563.5  58.31                 0
## 9        132941.1  60.25                 0
## 10       135894.9  62.69                 0
## 11       137795.7  65.05                 0
## 12       135176.0  69.30                 1
## 13       131233.0  71.77                 1
## 14       134991.7  74.93                 0
## 15       138891.9  77.79                 0
## 16       141530.2  80.57                 0
## 17       144112.0  83.77                 0
## 18       147277.4  87.19                 0
## 19       149433.5  89.05                 0
## 20       152275.4  92.04                 0
## 21       153235.7  98.27                 0
## 22       153133.8  99.91                 0
## 23       150233.1 105.93                 0
## 24       142609.3 109.27                 0
## 25       142772.0 117.31                 0
## 26       146826.7 126.48                 0

Describe the hypothetical relationship / impact between each selected factor and the dependent variable IED_Flujos. For example, how does the exchange rate increase / reduce the foreign direct investment flows in Mexico?

comp_data$Date <- as.Date(paste0(comp_data$periodo, "-01-01"))

Explaining the variables

## Independent Variables
## - Exportaciones - Exports are one of the largest sources of income for the country, which is why I consider it could become a significant variable in foreign direct investment.
## - Empleo - Employment is one of the variables that I consider can be of great influence since if there is a great demand for jobs it becomes more economical for foreign companies to come and offer jobs.
## - Educacion - I consider education is also a variable that can be very significant when it comes to offering jobs by foreign companies.
## - Salario_Diario - Knowing the daily salary is a good starting point for foreign companies to see how they can offer a salary that is attractive and does not affect the company's expenses.
## - Inseguridad_Robo - To make foreign investments by other companies, it is also important to analyze the risks involved in positioning yourself in another country.
## - Tipo_de_Cambio - The exchange rate is a good metric for foreign companies as it can lead to investments at better prices for companies.
## - PIB_per_Capita - GDP per capita is a good metric that indicates a good future for countries and this can be very striking when making an investment.

In describing the above relationships, please include a time series plot that displays the selected variables’ performance over the time period.

ggplot(comp_data, aes(x = periodo, y = Exportaciones)) + geom_line(color = "blue") +  labs(title = "Time Series of Exportaciones", 
       x = "Date", 
       y = "Exportaciones") + 
  theme_minimal()

ggplot(comp_data, aes(x = periodo, y = Empleo)) + geom_line(color = "blue") +  labs(title = "Time Series of Employment", 
       x = "Date", 
       y = "Employment") + 
  theme_minimal()

ggplot(comp_data, aes(x = periodo, y = Educacion)) + geom_line(color = "blue") +  labs(title = "Time Series of Education", 
       x = "Date", 
       y = "Education") + 
  theme_minimal()

ggplot(comp_data, aes(x = periodo, y = Salario_Diario)) + geom_line(color = "blue") +  labs(title = "Time Series of Daily_Salary", 
       x = "Date", 
       y = "Daily_Salary") + 
  theme_minimal()

ggplot(comp_data, aes(x = periodo, y = Inseguridad_Robo)) + geom_line(color = "blue") +  labs(title = "Time Series of Insecurity of Theft", 
       x = "Date", 
       y = "Insecurity of Theft") + 
  theme_minimal()

ggplot(comp_data, aes(x = periodo, y = Tipo_de_Cambio)) + geom_line(color = "blue") +  labs(title = "Time Series of Exchange_Rate", 
       x = "Date", 
       y = "Exchange_Rate") + 
  theme_minimal()

ggplot(comp_data, aes(x = periodo, y = PIB_Per_Capita)) + geom_line(color = "blue") +  labs(title = "Time Series of GDP_per_capita", 
       x = "Date", 
       y = "GDP_per_capita") + 
  theme_minimal()

Estimate a VAR_Model that includes at least 1 explanatory factor that might affect the dependent variable IED_Flujos.

VAR Model 1

variables <- c("IED_Flujos", "Exportaciones", "Empleo", "Educacion", 
                   "Salario_Diario", "Innovacion", "Tipo_de_Cambio", 
                   "Densidad_Carretera", "Densidad_Poblacion", 
                   "CO2_Emisiones", "PIB_Per_Capita", "INPC")
lapply(variables, function(var){adf.test(comp_data[[var]])})
## Warning in adf.test(comp_data[[var]]): p-value greater than printed p-value

## Warning in adf.test(comp_data[[var]]): p-value greater than printed p-value

## Warning in adf.test(comp_data[[var]]): p-value greater than printed p-value
## [[1]]
## 
##  Augmented Dickey-Fuller Test
## 
## data:  comp_data[[var]]
## Dickey-Fuller = -3.0832, Lag order = 2, p-value = 0.1597
## alternative hypothesis: stationary
## 
## 
## [[2]]
## 
##  Augmented Dickey-Fuller Test
## 
## data:  comp_data[[var]]
## Dickey-Fuller = -0.88228, Lag order = 2, p-value = 0.9379
## alternative hypothesis: stationary
## 
## 
## [[3]]
## 
##  Augmented Dickey-Fuller Test
## 
## data:  comp_data[[var]]
## Dickey-Fuller = -1.3805, Lag order = 2, p-value = 0.8084
## alternative hypothesis: stationary
## 
## 
## [[4]]
## 
##  Augmented Dickey-Fuller Test
## 
## data:  comp_data[[var]]
## Dickey-Fuller = 1.1162, Lag order = 2, p-value = 0.99
## alternative hypothesis: stationary
## 
## 
## [[5]]
## 
##  Augmented Dickey-Fuller Test
## 
## data:  comp_data[[var]]
## Dickey-Fuller = 6.0073, Lag order = 2, p-value = 0.99
## alternative hypothesis: stationary
## 
## 
## [[6]]
## 
##  Augmented Dickey-Fuller Test
## 
## data:  comp_data[[var]]
## Dickey-Fuller = -3.8976, Lag order = 2, p-value = 0.02874
## alternative hypothesis: stationary
## 
## 
## [[7]]
## 
##  Augmented Dickey-Fuller Test
## 
## data:  comp_data[[var]]
## Dickey-Fuller = -2.386, Lag order = 2, p-value = 0.4254
## alternative hypothesis: stationary
## 
## 
## [[8]]
## 
##  Augmented Dickey-Fuller Test
## 
## data:  comp_data[[var]]
## Dickey-Fuller = -2.4107, Lag order = 2, p-value = 0.4159
## alternative hypothesis: stationary
## 
## 
## [[9]]
## 
##  Augmented Dickey-Fuller Test
## 
## data:  comp_data[[var]]
## Dickey-Fuller = -1.8778, Lag order = 2, p-value = 0.6189
## alternative hypothesis: stationary
## 
## 
## [[10]]
## 
##  Augmented Dickey-Fuller Test
## 
## data:  comp_data[[var]]
## Dickey-Fuller = -2.1377, Lag order = 2, p-value = 0.5199
## alternative hypothesis: stationary
## 
## 
## [[11]]
## 
##  Augmented Dickey-Fuller Test
## 
## data:  comp_data[[var]]
## Dickey-Fuller = -2.5796, Lag order = 2, p-value = 0.3516
## alternative hypothesis: stationary
## 
## 
## [[12]]
## 
##  Augmented Dickey-Fuller Test
## 
## data:  comp_data[[var]]
## Dickey-Fuller = 2.4665, Lag order = 2, p-value = 0.99
## alternative hypothesis: stationary
comp_data$IED_Flujos_diff <- c(NA, diff(comp_data$IED_Flujos))
comp_data$Exportaciones_diff <- c(NA, diff(comp_data$Exportaciones))
comp_data$Empleo_diff <- c(NA, diff(comp_data$Empleo))
comp_data$Educacion_diff <- c(NA, diff(comp_data$Educacion))
comp_data$Salario_Diario_diff <- c(NA, diff(comp_data$Salario_Diario))
comp_data$Innovacion_diff <- c(NA, diff(comp_data$Innovacion))
comp_data$Tipo_de_Cambio_diff <- c(NA, diff(comp_data$Tipo_de_Cambio))
comp_data$Densidad_Carretera_diff <- c(NA, diff(comp_data$Densidad_Carretera))
comp_data$Densidad_Poblacion_diff <- c(NA, diff(comp_data$Densidad_Poblacion))
comp_data$CO2_Emisiones_diff <- c(NA, diff(comp_data$CO2_Emisiones))
comp_data$PIB_Per_Capita_diff <- c(NA, diff(comp_data$PIB_Per_Capita))
comp_data$INPC_diff <- c(NA, diff(comp_data$INPC))

comp_data_diff <- data.frame(
  IED_Flujos_diff = comp_data$IED_Flujos_diff,
  Exportaciones_diff = comp_data$Exportaciones_diff,
  Empleo_diff = comp_data$Empleo_diff,
  Educacion_diff = comp_data$Educacion_diff,
  Salario_Diario_diff = comp_data$Salario_Diario_diff,
  Innovacion_diff = comp_data$Innovacion_diff,
  Tipo_de_Cambio_diff = comp_data$Tipo_de_Cambio_diff,
  Densidad_Carretera_diff = comp_data$Densidad_Carretera_diff,
  Densidad_Poblacion_diff = comp_data$Densidad_Poblacion_diff,
  CO2_Emisiones_diff = comp_data$CO2_Emisiones_diff,
  PIB_Per_Capita_diff = comp_data$PIB_Per_Capita_diff,
  INPC_diff = comp_data$INPC_diff
)

comp_data_diff <- comp_data_diff[-1, ]
comp_data_diff
##    IED_Flujos_diff Exportaciones_diff Empleo_diff Educacion_diff
## 2         -3772.10             787.45        0.00           0.11
## 3          5586.82            1114.94        0.00           0.12
## 4          4288.37            1492.95        1.30           0.13
## 5         11808.49           -1182.52       -0.47           0.12
## 6         -5957.97             622.66        0.30           0.12
## 7         -5849.24            1232.90       -0.60           0.13
## 8          6765.60             417.13       -0.58           0.11
## 9           780.25            2892.68        0.69           0.10
## 10        -4563.28            1020.12       -0.64           0.12
## 11        11160.79            1617.92        0.07           0.10
## 12        -2890.87           -2179.09       -0.92           0.10
## 13       -11652.51            2777.87       -0.48           0.10
## 14         9339.33            2970.51       -0.14           0.07
## 15        -1556.76            1659.88        0.43           0.12
## 16        -3863.20            1964.96        0.04           0.10
## 17        26585.10            1389.59        0.22           0.10
## 18       -18003.17            3989.21        0.49           0.10
## 19         5592.50           -1716.84       -0.20           0.10
## 20        -4754.77            1415.12        0.58           0.10
## 21         2828.07            1947.56        0.23           0.10
## 22           83.38            2019.28       -0.21           0.10
## 23          476.73            1072.83        0.45           0.13
## 24        -6371.27            4662.61       -0.88          -1.12
## 25         3347.63            3837.44        0.28           0.00
## 26         4661.85            1562.81        0.75           0.00
##    Salario_Diario_diff Innovacion_diff Tipo_de_Cambio_diff
## 2                 7.61            0.07                1.88
## 3                 0.00            1.09               -0.42
## 4                 3.21            0.69                0.08
## 5                 2.45            0.32               -0.43
## 6                 2.17           -0.67                1.19
## 7                 1.79           -0.99                0.84
## 8                 1.77            0.80                0.02
## 9                 1.94            0.80               -0.51
## 10                1.81            0.82                0.17
## 11                1.83            0.81                0.02
## 12                1.96           -0.22                2.87
## 13                2.35           -2.23               -0.73
## 14                2.58            0.10               -0.66
## 15                2.29           -0.59                1.60
## 16                2.69            0.93               -0.99
## 17                2.37            0.19                0.08
## 18                2.46            0.43                1.66
## 19                4.52            1.46                2.61
## 20                2.94           -0.71                3.32
## 21               15.32           -0.35               -0.92
## 22                0.00           -0.80               -0.08
## 23               14.32           -0.55               -0.79
## 24               20.54           -1.42                1.07
## 25               18.48            1.81                0.58
## 26               31.17            0.00               -1.11
##    Densidad_Carretera_diff Densidad_Poblacion_diff CO2_Emisiones_diff
## 2                     0.00                    1.32               0.17
## 3                     0.01                    0.72              -0.16
## 4                     0.00                    1.10               0.18
## 5                     0.00                    0.70              -0.06
## 6                     0.00                    0.67               0.01
## 7                     0.00                    0.66               0.13
## 8                     0.00                    0.66               0.03
## 9                     0.00                    1.51               0.12
## 10                    0.00                    0.66               0.09
## 11                    0.00                    0.73               0.03
## 12                    0.01                    0.79              -0.03
## 13                    0.00                    0.77              -0.15
## 14                    0.00                    0.72               0.07
## 15                    0.00                    0.70               0.08
## 16                    0.00                    0.70               0.01
## 17                    0.01                   -0.36              -0.14
## 18                    0.00                    0.68              -0.17
## 19                    0.00                    0.69               0.04
## 20                    0.00                    0.71              -0.04
## 21                    0.01                    0.71              -0.05
## 22                    0.00                    0.83              -0.19
## 23                    0.00                    0.79              -0.06
## 24                    0.00                    0.69               0.34
## 25                    0.00                    0.57               0.00
## 26                    0.00                    0.44               0.00
##    PIB_Per_Capita_diff INPC_diff
## 2              -831.39      6.19
## 3              2425.96      4.87
## 4              1710.20      3.97
## 5             -2791.50      2.12
## 6               122.48      2.88
## 7               532.03      2.12
## 8              3825.62      2.88
## 9               377.57      1.94
## 10             2953.76      2.44
## 11             1900.87      2.36
## 12            -2619.71      4.25
## 13            -3943.06      2.47
## 14             3758.72      3.16
## 15             3900.17      2.86
## 16             2638.33      2.78
## 17             2581.80      3.20
## 18             3165.45      3.42
## 19             2156.01      1.86
## 20             2841.95      2.99
## 21              960.33      6.23
## 22             -101.90      1.64
## 23            -2900.76      6.02
## 24            -7623.73      3.34
## 25              162.61      8.04
## 26             4054.71      9.17
VAR_data <- as.data.frame(cbind(
  IED_Flujos_diff = comp_data_diff$IED_Flujos_diff,
  Tipo_de_Cambio_diff = comp_data_diff$Tipo_de_Cambio_diff,
  Innovacion_diff = comp_data_diff$Innovacion_diff,
  Salario_Diario_diff = comp_data_diff$Salario_Diario_diff,
  Exportaciones_diff = comp_data_diff$Exportaciones_diff
))

VAR_Model <- VAR(VAR_data, p=3, type = "const")
summary(VAR_Model)
## 
## VAR Estimation Results:
## ========================= 
## Endogenous variables: IED_Flujos_diff, Tipo_de_Cambio_diff, Innovacion_diff, Salario_Diario_diff, Exportaciones_diff 
## Deterministic variables: const 
## Sample size: 22 
## Log Likelihood: -416.094 
## Roots of the characteristic polynomial:
##  1.38 0.9756 0.9756 0.968 0.968 0.9133 0.9133 0.9125 0.9125 0.8799 0.8799  0.86 0.728 0.4879 0.4879
## Call:
## VAR(y = VAR_data, p = 3, type = "const")
## 
## 
## Estimation results for equation IED_Flujos_diff: 
## ================================================ 
## IED_Flujos_diff = IED_Flujos_diff.l1 + Tipo_de_Cambio_diff.l1 + Innovacion_diff.l1 + Salario_Diario_diff.l1 + Exportaciones_diff.l1 + IED_Flujos_diff.l2 + Tipo_de_Cambio_diff.l2 + Innovacion_diff.l2 + Salario_Diario_diff.l2 + Exportaciones_diff.l2 + IED_Flujos_diff.l3 + Tipo_de_Cambio_diff.l3 + Innovacion_diff.l3 + Salario_Diario_diff.l3 + Exportaciones_diff.l3 + const 
## 
##                          Estimate Std. Error t value Pr(>|t|)  
## IED_Flujos_diff.l1        -1.0742     0.4596  -2.337   0.0581 .
## Tipo_de_Cambio_diff.l1 -3792.6473  2509.3547  -1.511   0.1814  
## Innovacion_diff.l1      3194.6573  3505.6246   0.911   0.3973  
## Salario_Diario_diff.l1  -231.2875   560.2450  -0.413   0.6941  
## Exportaciones_diff.l1      0.7730     1.9955   0.387   0.7119  
## IED_Flujos_diff.l2        -0.6026     0.5187  -1.162   0.2895  
## Tipo_de_Cambio_diff.l2   299.8188  2549.1848   0.118   0.9102  
## Innovacion_diff.l2      1454.4888  4258.6137   0.342   0.7443  
## Salario_Diario_diff.l2    56.9423   569.5032   0.100   0.9236  
## Exportaciones_diff.l2      0.8389     2.6346   0.318   0.7610  
## IED_Flujos_diff.l3        -0.2180     0.4459  -0.489   0.6422  
## Tipo_de_Cambio_diff.l3   457.6367  2520.5520   0.182   0.8619  
## Innovacion_diff.l3      1708.9884  3837.9165   0.445   0.6717  
## Salario_Diario_diff.l3  -182.2992   689.4410  -0.264   0.8003  
## Exportaciones_diff.l3      1.4929     2.4168   0.618   0.5595  
## const                   1113.4131  7038.3596   0.158   0.8795  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 9197 on 6 degrees of freedom
## Multiple R-Squared: 0.7183,  Adjusted R-squared: 0.01404 
## F-statistic:  1.02 on 15 and 6 DF,  p-value: 0.5282 
## 
## 
## Estimation results for equation Tipo_de_Cambio_diff: 
## ==================================================== 
## Tipo_de_Cambio_diff = IED_Flujos_diff.l1 + Tipo_de_Cambio_diff.l1 + Innovacion_diff.l1 + Salario_Diario_diff.l1 + Exportaciones_diff.l1 + IED_Flujos_diff.l2 + Tipo_de_Cambio_diff.l2 + Innovacion_diff.l2 + Salario_Diario_diff.l2 + Exportaciones_diff.l2 + IED_Flujos_diff.l3 + Tipo_de_Cambio_diff.l3 + Innovacion_diff.l3 + Salario_Diario_diff.l3 + Exportaciones_diff.l3 + const 
## 
##                          Estimate Std. Error t value Pr(>|t|)  
## IED_Flujos_diff.l1      1.006e-04  5.060e-05   1.989   0.0938 .
## Tipo_de_Cambio_diff.l1  6.389e-01  2.763e-01   2.313   0.0600 .
## Innovacion_diff.l1      2.036e-01  3.859e-01   0.528   0.6167  
## Salario_Diario_diff.l1  2.100e-02  6.168e-02   0.340   0.7451  
## Exportaciones_diff.l1   5.064e-04  2.197e-04   2.305   0.0607 .
## IED_Flujos_diff.l2      4.124e-05  5.711e-05   0.722   0.4974  
## Tipo_de_Cambio_diff.l2 -4.551e-01  2.806e-01  -1.622   0.1560  
## Innovacion_diff.l2     -7.806e-01  4.688e-01  -1.665   0.1470  
## Salario_Diario_diff.l2 -1.590e-01  6.270e-02  -2.535   0.0444 *
## Exportaciones_diff.l2   2.948e-05  2.900e-04   0.102   0.9223  
## IED_Flujos_diff.l3      1.185e-04  4.909e-05   2.414   0.0523 .
## Tipo_de_Cambio_diff.l3  2.051e-01  2.775e-01   0.739   0.4877  
## Innovacion_diff.l3      7.468e-01  4.225e-01   1.767   0.1276  
## Salario_Diario_diff.l3 -4.344e-02  7.590e-02  -0.572   0.5879  
## Exportaciones_diff.l3   4.851e-04  2.661e-04   1.823   0.1181  
## const                  -7.780e-01  7.749e-01  -1.004   0.3541  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 1.012 on 6 degrees of freedom
## Multiple R-Squared: 0.828,   Adjusted R-squared: 0.3979 
## F-statistic: 1.925 on 15 and 6 DF,  p-value: 0.2151 
## 
## 
## Estimation results for equation Innovacion_diff: 
## ================================================ 
## Innovacion_diff = IED_Flujos_diff.l1 + Tipo_de_Cambio_diff.l1 + Innovacion_diff.l1 + Salario_Diario_diff.l1 + Exportaciones_diff.l1 + IED_Flujos_diff.l2 + Tipo_de_Cambio_diff.l2 + Innovacion_diff.l2 + Salario_Diario_diff.l2 + Exportaciones_diff.l2 + IED_Flujos_diff.l3 + Tipo_de_Cambio_diff.l3 + Innovacion_diff.l3 + Salario_Diario_diff.l3 + Exportaciones_diff.l3 + const 
## 
##                          Estimate Std. Error t value Pr(>|t|)  
## IED_Flujos_diff.l1     -1.286e-05  4.192e-05  -0.307   0.7694  
## Tipo_de_Cambio_diff.l1 -2.581e-02  2.289e-01  -0.113   0.9139  
## Innovacion_diff.l1      2.090e-01  3.197e-01   0.654   0.5376  
## Salario_Diario_diff.l1  1.377e-03  5.110e-02   0.027   0.9794  
## Exportaciones_diff.l1   4.366e-04  1.820e-04   2.399   0.0534 .
## IED_Flujos_diff.l2     -9.538e-06  4.731e-05  -0.202   0.8469  
## Tipo_de_Cambio_diff.l2  5.727e-02  2.325e-01   0.246   0.8136  
## Innovacion_diff.l2      4.620e-01  3.884e-01   1.189   0.2792  
## Salario_Diario_diff.l2 -8.048e-03  5.194e-02  -0.155   0.8820  
## Exportaciones_diff.l2  -1.757e-05  2.403e-04  -0.073   0.9441  
## IED_Flujos_diff.l3      3.300e-06  4.067e-05   0.081   0.9380  
## Tipo_de_Cambio_diff.l3 -2.590e-01  2.299e-01  -1.127   0.3030  
## Innovacion_diff.l3     -5.286e-01  3.501e-01  -1.510   0.1818  
## Salario_Diario_diff.l3 -1.037e-01  6.288e-02  -1.650   0.1501  
## Exportaciones_diff.l3  -2.234e-04  2.204e-04  -1.013   0.3500  
## const                   1.818e-01  6.420e-01   0.283   0.7866  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.8388 on 6 degrees of freedom
## Multiple R-Squared: 0.7846,  Adjusted R-squared: 0.246 
## F-statistic: 1.457 on 15 and 6 DF,  p-value: 0.3362 
## 
## 
## Estimation results for equation Salario_Diario_diff: 
## ==================================================== 
## Salario_Diario_diff = IED_Flujos_diff.l1 + Tipo_de_Cambio_diff.l1 + Innovacion_diff.l1 + Salario_Diario_diff.l1 + Exportaciones_diff.l1 + IED_Flujos_diff.l2 + Tipo_de_Cambio_diff.l2 + Innovacion_diff.l2 + Salario_Diario_diff.l2 + Exportaciones_diff.l2 + IED_Flujos_diff.l3 + Tipo_de_Cambio_diff.l3 + Innovacion_diff.l3 + Salario_Diario_diff.l3 + Exportaciones_diff.l3 + const 
## 
##                          Estimate Std. Error t value Pr(>|t|)  
## IED_Flujos_diff.l1     -0.0001445  0.0001784  -0.810   0.4488  
## Tipo_de_Cambio_diff.l1  0.6295677  0.9738757   0.646   0.5419  
## Innovacion_diff.l1     -0.5720571  1.3605261  -0.420   0.6888  
## Salario_Diario_diff.l1  0.2233235  0.2174300   1.027   0.3440  
## Exportaciones_diff.l1  -0.0001001  0.0007744  -0.129   0.9014  
## IED_Flujos_diff.l2     -0.0003004  0.0002013  -1.492   0.1863  
## Tipo_de_Cambio_diff.l2 -1.1462949  0.9893336  -1.159   0.2906  
## Innovacion_diff.l2     -0.3872793  1.6527597  -0.234   0.8225  
## Salario_Diario_diff.l2  0.7792812  0.2210231   3.526   0.0124 *
## Exportaciones_diff.l2  -0.0002202  0.0010225  -0.215   0.8366  
## IED_Flujos_diff.l3     -0.0002295  0.0001730  -1.326   0.2330  
## Tipo_de_Cambio_diff.l3 -0.3172878  0.9782213  -0.324   0.7567  
## Innovacion_diff.l3      1.3362503  1.4894879   0.897   0.4042  
## Salario_Diario_diff.l3  0.9882984  0.2675707   3.694   0.0102 *
## Exportaciones_diff.l3   0.0014479  0.0009380   1.544   0.1736  
## const                  -2.0280851  2.7315737  -0.742   0.4858  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 3.569 on 6 degrees of freedom
## Multiple R-Squared: 0.9457,  Adjusted R-squared: 0.8101 
## F-statistic: 6.973 on 15 and 6 DF,  p-value: 0.01233 
## 
## 
## Estimation results for equation Exportaciones_diff: 
## =================================================== 
## Exportaciones_diff = IED_Flujos_diff.l1 + Tipo_de_Cambio_diff.l1 + Innovacion_diff.l1 + Salario_Diario_diff.l1 + Exportaciones_diff.l1 + IED_Flujos_diff.l2 + Tipo_de_Cambio_diff.l2 + Innovacion_diff.l2 + Salario_Diario_diff.l2 + Exportaciones_diff.l2 + IED_Flujos_diff.l3 + Tipo_de_Cambio_diff.l3 + Innovacion_diff.l3 + Salario_Diario_diff.l3 + Exportaciones_diff.l3 + const 
## 
##                          Estimate Std. Error t value Pr(>|t|)
## IED_Flujos_diff.l1      6.653e-03  9.955e-02   0.067    0.949
## Tipo_de_Cambio_diff.l1 -1.292e+02  5.435e+02  -0.238    0.820
## Innovacion_diff.l1     -7.130e+02  7.593e+02  -0.939    0.384
## Salario_Diario_diff.l1  1.697e+02  1.213e+02   1.398    0.212
## Exportaciones_diff.l1  -5.209e-01  4.322e-01  -1.205    0.273
## IED_Flujos_diff.l2     -1.393e-02  1.124e-01  -0.124    0.905
## Tipo_de_Cambio_diff.l2  3.906e+01  5.521e+02   0.071    0.946
## Innovacion_diff.l2     -4.029e+02  9.224e+02  -0.437    0.678
## Salario_Diario_diff.l2 -4.762e+01  1.234e+02  -0.386    0.713
## Exportaciones_diff.l2  -1.952e-02  5.706e-01  -0.034    0.974
## IED_Flujos_diff.l3     -5.999e-03  9.657e-02  -0.062    0.952
## Tipo_de_Cambio_diff.l3  1.704e+02  5.459e+02   0.312    0.766
## Innovacion_diff.l3     -7.474e+02  8.313e+02  -0.899    0.403
## Salario_Diario_diff.l3 -3.221e+01  1.493e+02  -0.216    0.836
## Exportaciones_diff.l3   2.264e-01  5.235e-01   0.433    0.680
## const                   1.603e+03  1.524e+03   1.052    0.333
## 
## 
## Residual standard error: 1992 on 6 degrees of freedom
## Multiple R-Squared: 0.6136,  Adjusted R-squared: -0.3526 
## F-statistic: 0.6351 on 15 and 6 DF,  p-value: 0.778 
## 
## 
## 
## Covariance matrix of residuals:
##                     IED_Flujos_diff Tipo_de_Cambio_diff Innovacion_diff
## IED_Flujos_diff            84581094          -6465.1815       3689.4359
## Tipo_de_Cambio_diff           -6465              1.0251         -0.1332
## Innovacion_diff                3689             -0.1332          0.7036
## Salario_Diario_diff           -6562              1.5170          0.3632
## Exportaciones_diff         -3315869           -732.2829         42.4366
##                     Salario_Diario_diff Exportaciones_diff
## IED_Flujos_diff              -6562.4836         -3.316e+06
## Tipo_de_Cambio_diff              1.5170         -7.323e+02
## Innovacion_diff                  0.3632          4.244e+01
## Salario_Diario_diff             12.7396          2.619e+03
## Exportaciones_diff            2619.0729          3.968e+06
## 
## Correlation matrix of residuals:
##                     IED_Flujos_diff Tipo_de_Cambio_diff Innovacion_diff
## IED_Flujos_diff              1.0000             -0.6943          0.4782
## Tipo_de_Cambio_diff         -0.6943              1.0000         -0.1568
## Innovacion_diff              0.4782             -0.1568          1.0000
## Salario_Diario_diff         -0.1999              0.4198          0.1213
## Exportaciones_diff          -0.1810             -0.3631          0.0254
##                     Salario_Diario_diff Exportaciones_diff
## IED_Flujos_diff                 -0.1999            -0.1810
## Tipo_de_Cambio_diff              0.4198            -0.3631
## Innovacion_diff                  0.1213             0.0254
## Salario_Diario_diff              1.0000             0.3684
## Exportaciones_diff               0.3684             1.0000

Detect if the estimated VAR_Model residuals are stationary.

VAR_resid <- data.frame(residuals(VAR_Model))
adf.test(VAR_resid$IED_Flujos)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  VAR_resid$IED_Flujos
## Dickey-Fuller = -2.1785, Lag order = 2, p-value = 0.5044
## alternative hypothesis: stationary

Detect if the estimated VAR_Model residuals show serial autocorrelation.

acf(VAR_resid)

Box.test(VAR_resid$IED_Flujos_diff, lag=3, type="Ljung-Box")
## 
##  Box-Ljung test
## 
## data:  VAR_resid$IED_Flujos_diff
## X-squared = 0.76374, df = 3, p-value = 0.8581
# P-value is greater than 0.05 so it shows no serial auto correlation

VAR Model 2

VAR_data2 <- as.data.frame(cbind(
  IED_Flujos_diff = comp_data_diff$IED_Flujos_diff,
  PIB_Per_Capita_diff = comp_data_diff$PIB_Per_Capita_diff,
  Salario_Diario_diff = comp_data_diff$Salario_Diario_diff,
  Innovacion_diff = comp_data_diff$Innovacion_diff
))

VAR_Model2 <- VAR(VAR_data2, p=2, type = "const")
summary(VAR_Model2)
## 
## VAR Estimation Results:
## ========================= 
## Endogenous variables: IED_Flujos_diff, PIB_Per_Capita_diff, Salario_Diario_diff, Innovacion_diff 
## Deterministic variables: const 
## Sample size: 23 
## Log Likelihood: -532.221 
## Roots of the characteristic polynomial:
## 1.264 0.7285 0.7285 0.6331 0.6275 0.6275 0.2215 0.2056
## Call:
## VAR(y = VAR_data2, p = 2, type = "const")
## 
## 
## Estimation results for equation IED_Flujos_diff: 
## ================================================ 
## IED_Flujos_diff = IED_Flujos_diff.l1 + PIB_Per_Capita_diff.l1 + Salario_Diario_diff.l1 + Innovacion_diff.l1 + IED_Flujos_diff.l2 + PIB_Per_Capita_diff.l2 + Salario_Diario_diff.l2 + Innovacion_diff.l2 + const 
## 
##                         Estimate Std. Error t value Pr(>|t|)   
## IED_Flujos_diff.l1       -0.8565     0.2508  -3.416  0.00418 **
## PIB_Per_Capita_diff.l1    0.2584     0.9205   0.281  0.78301   
## Salario_Diario_diff.l1 -154.9015   413.2980  -0.375  0.71343   
## Innovacion_diff.l1     2550.7529  2397.6874   1.064  0.30541   
## IED_Flujos_diff.l2       -0.3991     0.2597  -1.537  0.14660   
## PIB_Per_Capita_diff.l2    0.4381     0.8806   0.498  0.62655   
## Salario_Diario_diff.l2  210.5822   438.6045   0.480  0.63856   
## Innovacion_diff.l2     -997.7784  2831.2854  -0.352  0.72978   
## const                  1285.0357  3303.6451   0.389  0.70315   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 8091 on 14 degrees of freedom
## Multiple R-Squared: 0.4945,  Adjusted R-squared: 0.2056 
## F-statistic: 1.712 on 8 and 14 DF,  p-value: 0.1811 
## 
## 
## Estimation results for equation PIB_Per_Capita_diff: 
## ==================================================== 
## PIB_Per_Capita_diff = IED_Flujos_diff.l1 + PIB_Per_Capita_diff.l1 + Salario_Diario_diff.l1 + Innovacion_diff.l1 + IED_Flujos_diff.l2 + PIB_Per_Capita_diff.l2 + Salario_Diario_diff.l2 + Innovacion_diff.l2 + const 
## 
##                          Estimate Std. Error t value Pr(>|t|)
## IED_Flujos_diff.l1       -0.09009    0.09655  -0.933    0.367
## PIB_Per_Capita_diff.l1    0.47594    0.35442   1.343    0.201
## Salario_Diario_diff.l1  -76.96421  159.12601  -0.484    0.636
## Innovacion_diff.l1     -186.13817  923.14615  -0.202    0.843
## IED_Flujos_diff.l2       -0.10910    0.09999  -1.091    0.294
## PIB_Per_Capita_diff.l2   -0.26887    0.33906  -0.793    0.441
## Salario_Diario_diff.l2   95.39409  168.86942   0.565    0.581
## Innovacion_diff.l2      334.68800 1090.08796   0.307    0.763
## const                   750.23224 1271.95362   0.590    0.565
## 
## 
## Residual standard error: 3115 on 14 degrees of freedom
## Multiple R-Squared: 0.309,   Adjusted R-squared: -0.08585 
## F-statistic: 0.7826 on 8 and 14 DF,  p-value: 0.6255 
## 
## 
## Estimation results for equation Salario_Diario_diff: 
## ==================================================== 
## Salario_Diario_diff = IED_Flujos_diff.l1 + PIB_Per_Capita_diff.l1 + Salario_Diario_diff.l1 + Innovacion_diff.l1 + IED_Flujos_diff.l2 + PIB_Per_Capita_diff.l2 + Salario_Diario_diff.l2 + Innovacion_diff.l2 + const 
## 
##                          Estimate Std. Error t value Pr(>|t|)  
## IED_Flujos_diff.l1     -2.781e-05  1.665e-04  -0.167   0.8698  
## PIB_Per_Capita_diff.l1  2.651e-04  6.112e-04   0.434   0.6711  
## Salario_Diario_diff.l1  6.293e-01  2.744e-01   2.293   0.0378 *
## Innovacion_diff.l1     -1.535e-01  1.592e+00  -0.096   0.9245  
## IED_Flujos_diff.l2      6.588e-05  1.724e-04   0.382   0.7081  
## PIB_Per_Capita_diff.l2 -5.155e-04  5.847e-04  -0.882   0.3929  
## Salario_Diario_diff.l2  7.572e-01  2.912e-01   2.600   0.0210 *
## Innovacion_diff.l2      1.231e+00  1.880e+00   0.655   0.5232  
## const                   1.502e-02  2.194e+00   0.007   0.9946  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 5.372 on 14 degrees of freedom
## Multiple R-Squared: 0.715,   Adjusted R-squared: 0.5521 
## F-statistic:  4.39 on 8 and 14 DF,  p-value: 0.007793 
## 
## 
## Estimation results for equation Innovacion_diff: 
## ================================================ 
## Innovacion_diff = IED_Flujos_diff.l1 + PIB_Per_Capita_diff.l1 + Salario_Diario_diff.l1 + Innovacion_diff.l1 + IED_Flujos_diff.l2 + PIB_Per_Capita_diff.l2 + Salario_Diario_diff.l2 + Innovacion_diff.l2 + const 
## 
##                          Estimate Std. Error t value Pr(>|t|)
## IED_Flujos_diff.l1     -2.479e-05  3.433e-05  -0.722    0.482
## PIB_Per_Capita_diff.l1  1.065e-04  1.260e-04   0.845    0.412
## Salario_Diario_diff.l1  1.370e-02  5.657e-02   0.242    0.812
## Innovacion_diff.l1     -4.715e-02  3.282e-01  -0.144    0.888
## IED_Flujos_diff.l2      1.777e-06  3.555e-05   0.050    0.961
## PIB_Per_Capita_diff.l2  3.714e-05  1.205e-04   0.308    0.763
## Salario_Diario_diff.l2  5.332e-02  6.004e-02   0.888    0.389
## Innovacion_diff.l2      3.791e-02  3.875e-01   0.098    0.923
## const                  -3.385e-01  4.522e-01  -0.749    0.466
## 
## 
## Residual standard error: 1.108 on 14 degrees of freedom
## Multiple R-Squared: 0.1438,  Adjusted R-squared: -0.3455 
## F-statistic: 0.2939 on 8 and 14 DF,  p-value: 0.9564 
## 
## 
## 
## Covariance matrix of residuals:
##                     IED_Flujos_diff PIB_Per_Capita_diff Salario_Diario_diff
## IED_Flujos_diff            65468527             1095805           -3109.222
## PIB_Per_Capita_diff         1095805             9704840           -7805.392
## Salario_Diario_diff           -3109               -7805              28.863
## Innovacion_diff                3110                1825              -1.706
##                     Innovacion_diff
## IED_Flujos_diff            3110.222
## PIB_Per_Capita_diff        1824.892
## Salario_Diario_diff          -1.706
## Innovacion_diff               1.227
## 
## Correlation matrix of residuals:
##                     IED_Flujos_diff PIB_Per_Capita_diff Salario_Diario_diff
## IED_Flujos_diff             1.00000             0.04347            -0.07153
## PIB_Per_Capita_diff         0.04347             1.00000            -0.46637
## Salario_Diario_diff        -0.07153            -0.46637             1.00000
## Innovacion_diff             0.34707             0.52891            -0.28666
##                     Innovacion_diff
## IED_Flujos_diff              0.3471
## PIB_Per_Capita_diff          0.5289
## Salario_Diario_diff         -0.2867
## Innovacion_diff              1.0000
VAR_resid2 <- data.frame(residuals(VAR_Model2))
adf.test(VAR_resid2$IED_Flujos)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  VAR_resid2$IED_Flujos
## Dickey-Fuller = -3.3022, Lag order = 2, p-value = 0.09135
## alternative hypothesis: stationary
Box.test(VAR_resid2$IED_Flujos_diff, lag=2, type="Ljung-Box")
## 
##  Box-Ljung test
## 
## data:  VAR_resid2$IED_Flujos_diff
## X-squared = 1.1654, df = 2, p-value = 0.5584
# P-value is greater than 0.05 so it shows no serial auto correlation
## The selected model was VAR model 2 because it showed overall better results when the variable Tipo_de_Cambio was not in play, the VAR model showed almost no auto correlation at all and it showed stationary values.

Based on the regression results and diagnostic tests, select the VAR_Model that you consider might generate the best forecast.

Briefly interpret the regression results. That is, is there a statistically significant relationship between the explanatory variable(s) and the main dependent variable?

## Based on the diagnostic test the VAR model that might be better to generate a forecast is VAR Model 2, this is mainly becuase of presenting almost no autocorrelation and presenting overall better fit data.

## The overall interpretation of the VAR model 2 is that the model has 2 statistically significant coefficients, the overall explanatory variables inside of the VAR model suggest a decent fit in the data indicated based on the r^2 of the equations and overall the best equation was Salario_Diario_diff.

## There is an statistical significance with the dependent variables own lagged value and theres also a statistical significance with the variable Salario_Diario

Is there an instantaneous causality between IED_Flujos and the selected explanatory variables? Estimate a Granger Causality Test to either reject or fail to reject the hypothesis of instantaneous causality.

Granger_tst <- causality(VAR_Model2, cause = "IED_Flujos_diff")
Granger_tst
## $Granger
## 
##  Granger causality H0: IED_Flujos_diff do not Granger-cause
##  PIB_Per_Capita_diff Salario_Diario_diff Innovacion_diff
## 
## data:  VAR object VAR_Model2
## F-Test = 0.52766, df1 = 6, df2 = 56, p-value = 0.7849
## 
## 
## $Instant
## 
##  H0: No instantaneous causality between: IED_Flujos_diff and
##  PIB_Per_Capita_diff Salario_Diario_diff Innovacion_diff
## 
## data:  VAR object VAR_Model2
## Chi-squared = 2.9847, df = 3, p-value = 0.394
## The granger causality test helps us identify that the variations in the independent variables of Education, Salario_Diario and PIB_Per_Capita diffs do not granger cause any sort of variations in the IED_M because the p-value is greater than the significance level 0.05. This test also helps us identify that there is no causality between the dependent variable IED_M and the explanatory variables.

Based on the selected VAR_Model, forecast the increasing / decreasing trend of FDI inflows in Mexico for the next 5 periods. Display the forecast in a time series plot.

ForecastModel <- predict(VAR_Model2, n.ahead=5)$fcst$IED_Flujos_diff[,1]
Cumulative <- cumsum(ForecastModel)

# Last value to provide better data
LastValue <- tail(comp_data_diff$IED_Flujos_diff, n=1)
MainForecast <- LastValue + Cumulative

# Linking original data with forcast
LinkData <- c(comp_data$IED_Flujos, MainForecast)

#Time Series
TSA <- 1:length(LinkData)
plot(TSA, LinkData, type="l", main="Forecast IED_M", xlab="Time", ylab="Value", col="green", lwd=2)
lines((length(comp_data$IED_Flujos)+1):length(LinkData), MainForecast, col="orange", lwd=2)

VI. Conclusions and Recommendations

Briefly describe the main insights from previous sections.

# Basically, from the entire project, it can be detected that some temporary data can have great relevance when trying to predict what will happen with foreign investment in Mexico. In addition to this, the Daily Salary and the Innovation index when doing tests were among the variables. that most generated a difference between a positive forecasting and a completely opposite one, so although it is not shown as much for the Innovation variable, I do believe that it can be a highly influential variable for the final decision of whether or not to make a foreign investment.

Based on the selected results, please share at least 1 recommendation that address the problem situation.

# Based on the results of the forecast of the selected model, it can be seen that there may be a positive trend for the country of Mexico in terms of foreign investment. I consider that there may be other possible variables that may indicate more precise results. But comparing the results of the model that indicate a possible growth of foreign investment in the country with a previous investigation about the state of foreign investment in Mexico, it seems that the data agree and both indicate results that show positive, for the same reason it seems that This forecast is on the right track.
# A very big recommendation that I consider to be a crucial factor is to continue offering the labor that we currently offer, since it seems to be one of the most attractive aspects through which foreign investments can come to the country.