Para este trabajo tomaremos datos desde 1990 hasta 2021, la idea es probar si el crecimiento económico génera que haya más igualdad.
Cargamos el documento
#install.packages("readxl")#
#install.packages("tidyverse")#
#install.packages("tseries")
#install.packages("car")
#install.packages("timsac")
# Instalar el paquete "urca" (solo si aún no está instalado)
#install.packages("urca")
#install.packages("readxl")
#install.packages("lmtest")
library(lmtest)
## Warning: package 'lmtest' was built under R version 4.3.2
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
# Cargar el paquete "urca"
library(readxl)
library(urca)
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(tseries)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(lubridate)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ purrr 1.0.1 ✔ tidyr 1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(car)
## Loading required package: carData
##
## Attaching package: 'car'
##
## The following object is masked from 'package:dplyr':
##
## recode
##
## The following object is masked from 'package:purrr':
##
## some
library(foreign)
library(timsac)
data <- read_excel("C:\\Users\\david\\Desktop\\UNAL\\Octavo Semestre\\Eco II\\ginipib.xlsx")
Ahora, vamos a ajustar la variable tiempo
data$Año <- NULL
# Cambiar los nombres de las columnas en un marco de datos data
colnames(data) <- c("gini", "pib")
gini.ts = ts(data$gini, start = 1990, frequency = 1)
pib.ts = ts(data$pib, start = 1990, frequency = 1)
data$Año <- NULL
class(data$pib)
## [1] "numeric"
class(data$gini)
## [1] "numeric"
#Convierto PIB a variable en Log
data <- data %>%
mutate(lpib = log(pib.ts))
#hagamos lo mismo para lpib
lpib.ts = ts(data$lpib, start = 1990, frequency = 1)
plot(lpib.ts, main="Tendencia")
#Agrupemos ambos en un solo dataframe
datos=cbind(lpib.ts,gini.ts)
datos
## Time Series:
## Start = 1990
## End = 2021
## Frequency = 1
## lpib.ts gini.ts
## 1990 25.51482 0.523
## 1991 25.53464 0.513
## 1992 25.57429 0.515
## 1993 25.62674 0.538
## 1994 25.68326 0.541
## 1995 25.73398 0.562
## 1996 25.75433 0.569
## 1997 25.78806 0.567
## 1998 25.79374 0.538
## 1999 25.75079 0.569
## 2000 25.77962 0.569
## 2001 25.79626 0.587
## 2002 25.82099 0.587
## 2003 25.85942 0.575
## 2004 25.91138 0.560
## 2005 25.95854 0.536
## 2006 26.02355 0.550
## 2007 26.08876 0.539
## 2008 26.12106 0.553
## 2009 26.13240 0.543
## 2010 26.17636 0.546
## 2011 26.24353 0.535
## 2012 26.28191 0.526
## 2013 26.33198 0.526
## 2014 26.37599 0.526
## 2015 26.40512 0.510
## 2016 26.42578 0.506
## 2017 26.43928 0.497
## 2018 26.46460 0.504
## 2019 26.49597 0.513
## 2020 26.42068 0.535
## 2021 26.52519 0.515
siguiente, vamos a ver el autocorrelograma
autocorrelograma_pib <- acf(data$pib)
autocorrelograma_lpib <- acf(data$lpib)
autocorrelograma_gini <- acf(data$gini)
Pasamos al autocorrelograma parcial
autocorrelograma_parcial_pib <- pacf(data$pib)
autocorrelograma_parcial_lpib <- pacf(data$lpib)
autocorrelograma_parcial_gini <- pacf(data$gini)
plot(gini.ts)
plot(pib.ts)
Ahora, miremos estadística describtiva
summary(data)
## gini pib lpib
## Min. :0.4970 Min. :1.205e+11 Min. :25.51
## 1st Qu.:0.5210 1st Qu.:1.560e+11 1st Qu.:25.77
## Median :0.5380 Median :1.941e+11 Median :25.99
## Mean :0.5398 Mean :2.112e+11 Mean :26.03
## 3rd Qu.:0.5605 3rd Qu.:2.759e+11 3rd Qu.:26.34
## Max. :0.5870 Max. :3.309e+11 Max. :26.53
str(data)
## tibble [32 × 3] (S3: tbl_df/tbl/data.frame)
## $ gini: num [1:32] 0.523 0.513 0.515 0.538 0.541 0.562 0.569 0.567 0.538 0.569 ...
## $ pib : num [1:32] 1.20e+11 1.23e+11 1.28e+11 1.35e+11 1.43e+11 ...
## $ lpib: Time-Series [1:32] from 1990 to 2021: 25.5 25.5 25.6 25.6 25.7 ...
summary(gini.ts)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.4970 0.5210 0.5380 0.5398 0.5605 0.5870
summary(pib.ts)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.205e+11 1.560e+11 1.941e+11 2.112e+11 2.759e+11 3.309e+11
Analizado lo anterior, comenzaremos por una prueba de estacionariedad para ambas variables, para ellos nos remitimos a un Test Dickey-Fuller aumentado
dfu_gini <- ur.df(gini.ts, type = "trend", lags = 1)
print(summary(dfu_gini))
##
## ###############################################
## # Augmented Dickey-Fuller Test Unit Root Test #
## ###############################################
##
## Test regression trend
##
##
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + tt + z.diff.lag)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.028021 -0.007229 -0.000387 0.007365 0.024475
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.1796294 0.0665947 2.697 0.0121 *
## z.lag.1 -0.3074726 0.1179579 -2.607 0.0149 *
## tt -0.0007969 0.0003225 -2.471 0.0204 *
## z.diff.lag -0.0516674 0.1791724 -0.288 0.7754
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.01332 on 26 degrees of freedom
## Multiple R-squared: 0.267, Adjusted R-squared: 0.1825
## F-statistic: 3.157 on 3 and 26 DF, p-value: 0.04157
##
##
## Value of test-statistic is: -2.6066 2.9041 4.3548
##
## Critical values for test statistics:
## 1pct 5pct 10pct
## tau3 -4.15 -3.50 -3.18
## phi2 7.02 5.13 4.31
## phi3 9.31 6.73 5.61
Ahora, hacemos lo mismo para el PIB
dfu_pib <- ur.df(pib.ts, type = "trend", lags = 1)
print(summary(dfu_gini))
##
## ###############################################
## # Augmented Dickey-Fuller Test Unit Root Test #
## ###############################################
##
## Test regression trend
##
##
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + tt + z.diff.lag)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.028021 -0.007229 -0.000387 0.007365 0.024475
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.1796294 0.0665947 2.697 0.0121 *
## z.lag.1 -0.3074726 0.1179579 -2.607 0.0149 *
## tt -0.0007969 0.0003225 -2.471 0.0204 *
## z.diff.lag -0.0516674 0.1791724 -0.288 0.7754
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.01332 on 26 degrees of freedom
## Multiple R-squared: 0.267, Adjusted R-squared: 0.1825
## F-statistic: 3.157 on 3 and 26 DF, p-value: 0.04157
##
##
## Value of test-statistic is: -2.6066 2.9041 4.3548
##
## Critical values for test statistics:
## 1pct 5pct 10pct
## tau3 -4.15 -3.50 -3.18
## phi2 7.02 5.13 4.31
## phi3 9.31 6.73 5.61
Miremos que tal se comportan los residuos, una forma de observar estacionariedad
modelo_ver_estacionariedad=lm(gini.ts ~ pib.ts)
summary(modelo_ver_estacionariedad)
##
## Call:
## lm(formula = gini.ts ~ pib.ts)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.045585 -0.012977 0.001351 0.014131 0.037097
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.848e-01 1.201e-02 48.705 < 2e-16 ***
## pib.ts -2.131e-13 5.423e-14 -3.929 0.000464 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.02042 on 30 degrees of freedom
## Multiple R-squared: 0.3397, Adjusted R-squared: 0.3177
## F-statistic: 15.44 on 1 and 30 DF, p-value: 0.0004642
residuales_mv=modelo_ver_estacionariedad$residuals
summary(residuales_mv)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -0.045585 -0.012977 0.001351 0.000000 0.014131 0.037097
residualPlot(modelo_ver_estacionariedad)
y=ur.df(residuales_mv)
summary(y)
##
## ###############################################
## # Augmented Dickey-Fuller Test Unit Root Test #
## ###############################################
##
## Test regression none
##
##
## Call:
## lm(formula = z.diff ~ z.lag.1 - 1 + z.diff.lag)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.023150 -0.005587 0.002027 0.010266 0.025479
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## z.lag.1 -0.35349 0.12359 -2.860 0.00792 **
## z.diff.lag 0.01541 0.17335 0.089 0.92981
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.01254 on 28 degrees of freedom
## Multiple R-squared: 0.2384, Adjusted R-squared: 0.184
## F-statistic: 4.382 on 2 and 28 DF, p-value: 0.0221
##
##
## Value of test-statistic is: -2.8601
##
## Critical values for test statistics:
## 1pct 5pct 10pct
## tau1 -2.62 -1.95 -1.61
En definitiva ambas series de tiempo no son estacionarias, veamos la cointegración
X=ur.df(residuales_mv, type="trend", selectlags = "AIC" )
summary(X)
##
## ###############################################
## # Augmented Dickey-Fuller Test Unit Root Test #
## ###############################################
##
## Test regression trend
##
##
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + tt + z.diff.lag)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.0277274 -0.0065285 0.0008746 0.0075240 0.0211262
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.0066706 0.0050023 1.333 0.1939
## z.lag.1 -0.3444974 0.1244848 -2.767 0.0103 *
## tt -0.0002838 0.0002675 -1.061 0.2985
## z.diff.lag -0.0228160 0.1760920 -0.130 0.8979
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.01257 on 26 degrees of freedom
## Multiple R-squared: 0.28, Adjusted R-squared: 0.1969
## F-statistic: 3.37 on 3 and 26 DF, p-value: 0.0336
##
##
## Value of test-statistic is: -2.7674 3.3277 4.6949
##
## Critical values for test statistics:
## 1pct 5pct 10pct
## tau3 -4.15 -3.50 -3.18
## phi2 7.02 5.13 4.31
## phi3 9.31 6.73 5.61
X2=ur.df(residuales_mv, type="drift", selectlags = "AIC" )
summary(X2)
##
## ###############################################
## # Augmented Dickey-Fuller Test Unit Root Test #
## ###############################################
##
## Test regression drift
##
##
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.0250825 -0.0075625 0.0000228 0.0085729 0.0230146
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.0019652 0.0023182 0.848 0.40405
## z.lag.1 -0.3564103 0.1242647 -2.868 0.00792 **
## z.diff.lag -0.0006034 0.1752481 -0.003 0.99728
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.0126 on 27 degrees of freedom
## Multiple R-squared: 0.2488, Adjusted R-squared: 0.1932
## F-statistic: 4.472 on 2 and 27 DF, p-value: 0.02102
##
##
## Value of test-statistic is: -2.8682 4.4084
##
## Critical values for test statistics:
## 1pct 5pct 10pct
## tau2 -3.58 -2.93 -2.60
## phi1 7.06 4.86 3.94
X3=ur.df(residuales_mv, type="none", selectlags = "AIC" )
summary(X3)
##
## ###############################################
## # Augmented Dickey-Fuller Test Unit Root Test #
## ###############################################
##
## Test regression none
##
##
## Call:
## lm(formula = z.diff ~ z.lag.1 - 1 + z.diff.lag)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.023150 -0.005587 0.002027 0.010266 0.025479
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## z.lag.1 -0.35349 0.12359 -2.860 0.00792 **
## z.diff.lag 0.01541 0.17335 0.089 0.92981
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.01254 on 28 degrees of freedom
## Multiple R-squared: 0.2384, Adjusted R-squared: 0.184
## F-statistic: 4.382 on 2 and 28 DF, p-value: 0.0221
##
##
## Value of test-statistic is: -2.8601
##
## Critical values for test statistics:
## 1pct 5pct 10pct
## tau1 -2.62 -1.95 -1.61
Diferenciar las variables por que son no estacionarias
dltp1= diff(lpib.ts)
dltp2= diff(gini.ts)
datos1=cbind(dltp2,dltp1)
datos1
## Time Series:
## Start = 1991
## End = 2021
## Frequency = 1
## dltp2 dltp1
## 1991 -0.010 0.01981839
## 1992 0.002 0.03965263
## 1993 0.023 0.05245401
## 1994 0.003 0.05651891
## 1995 0.021 0.05071629
## 1996 0.007 0.02035007
## 1997 -0.002 0.03372771
## 1998 -0.029 0.00568167
## 1999 0.031 -0.04294941
## 2000 0.000 0.02882904
## 2001 0.018 0.01663977
## 2002 0.000 0.02473145
## 2003 -0.012 0.03843456
## 2004 -0.015 0.05195678
## 2005 -0.024 0.04715799
## 2006 0.014 0.06500905
## 2007 -0.011 0.06520887
## 2008 0.014 0.03230693
## 2009 -0.010 0.01133204
## 2010 0.003 0.04396577
## 2011 -0.011 0.06717154
## 2012 -0.009 0.03838032
## 2013 0.000 0.05006548
## 2014 0.000 0.04400760
## 2015 -0.016 0.02913057
## 2016 -0.004 0.02065895
## 2017 -0.009 0.01350204
## 2018 0.007 0.02531997
## 2019 0.009 0.03137129
## 2020 0.022 -0.07528727
## 2021 -0.020 0.10450589
plot(datos1)
Hagamos el test de Granger
grangertest(dltp2~dltp1, order=1)
## Granger causality test
##
## Model 1: dltp2 ~ Lags(dltp2, 1:1) + Lags(dltp1, 1:1)
## Model 2: dltp2 ~ Lags(dltp2, 1:1)
## Res.Df Df F Pr(>F)
## 1 27
## 2 28 -1 0.0609 0.807
grangertest(dltp1~dltp2, order=1)
## Granger causality test
##
## Model 1: dltp1 ~ Lags(dltp1, 1:1) + Lags(dltp2, 1:1)
## Model 2: dltp1 ~ Lags(dltp1, 1:1)
## Res.Df Df F Pr(>F)
## 1 27
## 2 28 -1 0.491 0.4895
print(dltp2)
## Time Series:
## Start = 1991
## End = 2021
## Frequency = 1
## [1] -0.010 0.002 0.023 0.003 0.021 0.007 -0.002 -0.029 0.031 0.000
## [11] 0.018 0.000 -0.012 -0.015 -0.024 0.014 -0.011 0.014 -0.010 0.003
## [21] -0.011 -0.009 0.000 0.000 -0.016 -0.004 -0.009 0.007 0.009 0.022
## [31] -0.020
#install.packages("vars")
library(vars)
## Warning: package 'vars' was built under R version 4.3.2
## 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.3.2
## Loading required package: sandwich
## Warning: package 'sandwich' was built under R version 4.3.2
##
## Attaching package: 'strucchange'
## The following object is masked from 'package:stringr':
##
## boundary
grangertest(dltp2~dltp1, order=8)
## Granger causality test
##
## Model 1: dltp2 ~ Lags(dltp2, 1:8) + Lags(dltp1, 1:8)
## Model 2: dltp2 ~ Lags(dltp2, 1:8)
## Res.Df Df F Pr(>F)
## 1 6
## 2 14 -8 0.6858 0.6972
VARselect(datos1)
## $selection
## AIC(n) HQ(n) SC(n) FPE(n)
## 10 10 10 1
##
## $criteria
## 1 2 3 4 5
## AIC(n) -1.546683e+01 -1.527105e+01 -1.517396e+01 -1.532545e+01 -1.539893e+01
## HQ(n) -1.540207e+01 -1.516310e+01 -1.502283e+01 -1.513115e+01 -1.516144e+01
## SC(n) -1.516840e+01 -1.477366e+01 -1.447761e+01 -1.443014e+01 -1.430466e+01
## FPE(n) 1.925522e-07 2.376623e-07 2.710413e-07 2.486664e-07 2.586315e-07
## 6 7 8 9 10
## AIC(n) -1.543278e+01 -1.512495e+01 -1.521875e+01 -1.681451e+01 -Inf
## HQ(n) -1.515212e+01 -1.480111e+01 -1.485173e+01 -1.640431e+01 -Inf
## SC(n) -1.413956e+01 -1.363277e+01 -1.352762e+01 -1.492442e+01 -Inf
## FPE(n) 3.013088e-07 5.581872e-07 8.704455e-07 5.344141e-07 NaN
Modelo del var= 10
#VAR 10
var1<-VAR(datos1, p=10)
var1
##
## VAR Estimation Results:
## =======================
##
## Estimated coefficients for equation dltp2:
## ==========================================
## Call:
## dltp2 = dltp2.l1 + dltp1.l1 + dltp2.l2 + dltp1.l2 + dltp2.l3 + dltp1.l3 + dltp2.l4 + dltp1.l4 + dltp2.l5 + dltp1.l5 + dltp2.l6 + dltp1.l6 + dltp2.l7 + dltp1.l7 + dltp2.l8 + dltp1.l8 + dltp2.l9 + dltp1.l9 + dltp2.l10 + dltp1.l10 + const
##
## dltp2.l1 dltp1.l1 dltp2.l2 dltp1.l2 dltp2.l3 dltp1.l3
## -2.73337763 -0.01139366 0.03285332 -1.13922779 0.93909323 -0.24642769
## dltp2.l4 dltp1.l4 dltp2.l5 dltp1.l5 dltp2.l6 dltp1.l6
## -1.17168615 -0.13448062 -2.31993702 -0.15115642 -0.73371762 -0.80063428
## dltp2.l7 dltp1.l7 dltp2.l8 dltp1.l8 dltp2.l9 dltp1.l9
## 0.43622375 0.27105907 -0.66430948 -0.46606373 -1.55699159 0.07058270
## dltp2.l10 dltp1.l10 const
## -1.26611539 -0.70636653 0.09928638
##
##
## Estimated coefficients for equation dltp1:
## ==========================================
## Call:
## dltp1 = dltp2.l1 + dltp1.l1 + dltp2.l2 + dltp1.l2 + dltp2.l3 + dltp1.l3 + dltp2.l4 + dltp1.l4 + dltp2.l5 + dltp1.l5 + dltp2.l6 + dltp1.l6 + dltp2.l7 + dltp1.l7 + dltp2.l8 + dltp1.l8 + dltp2.l9 + dltp1.l9 + dltp2.l10 + dltp1.l10 + const
##
## dltp2.l1 dltp1.l1 dltp2.l2 dltp1.l2 dltp2.l3 dltp1.l3 dltp2.l4
## -3.9979930 -0.7413869 -0.7640781 -1.5831324 5.6863816 -0.5008725 3.7614489
## dltp1.l4 dltp2.l5 dltp1.l5 dltp2.l6 dltp1.l6 dltp2.l7 dltp1.l7
## -0.2505515 -0.4118996 1.7831710 -1.8937725 -1.7349386 1.2978090 -0.5219832
## dltp2.l8 dltp1.l8 dltp2.l9 dltp1.l9 dltp2.l10 dltp1.l10 const
## 1.9852735 -0.5036385 -3.9510251 -0.5231779 -5.9968408 -2.0069085 0.2866016