library(readr)
Evidencia_eco <- read_csv("C:/Users/Fercs/OneDrive/Tec de Monterrey/Uni/S4/Econometria/Evidencia_eco.csv")
## Rows: 9 Columns: 43
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (2): Location, Indicator
## num (41): 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, ...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
DATOS<-Evidencia_eco

Limpieza de base

library(dplyr)
## 
## 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
#Arreglo de columnas y variables 

DATOS <- t(DATOS)
DATOS1 <- DATOS[-1, ]

nom_var<- DATOS1[1, ]
colnames(DATOS1) <- nom_var

DATOSF <- DATOS1[-1, ]

DATOSF_1 <- as.data.frame(DATOSF)

# Limpieza 

convertir_numerico <- function(x) {
  x <- as.numeric(gsub(",", "", x))
  return(x)
}

# Aplicar la función a las columnas problemáticas
DATOSF_1$empleo.total <- convertir_numerico(DATOSF_1$empleo.total)
DATOSF_1$inversion.US <- convertir_numerico(DATOSF_1$inversion.US)
DATOSF_1$PIB.real <- convertir_numerico(DATOSF_1$PIB.real)
DATOSF_1$inversion.real <- convertir_numerico(DATOSF_1$inversion.real)

# Convertir las demás columnas a numérico
DATOSF_1$poblacion.total <- as.numeric(DATOSF_1$poblacion.total)
DATOSF_1$poblacion.crece <- as.numeric(DATOSF_1$poblacion.crece)
DATOSF_1$tasa.desempleo <- as.numeric(DATOSF_1$tasa.desempleo)
DATOSF_1$inflacion <- as.numeric(DATOSF_1$inflacion)

# Asignar el resultado a Data_final
data_final <- DATOSF_1

Modelo general

#PASO 1 : serie de tiempo

library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0     ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1     ✔ tibble  3.2.1
## ✔ purrr   1.0.2     ✔ tidyr   1.3.1
## ── 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(stargazer)
## 
## Please cite as: 
## 
##  Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
DATOSF.ts <- ts(data_final, start = 1980, frequency = 1)

#Variables a ts

fecha.ts=ts(DATOSF.ts[,1],start=1980,frequency=1)
poblacion.total.ts=ts(DATOSF.ts[,2],start=1980,frequency=1)
poblacion.crece.ts=ts(DATOSF.ts[,3],start=1980,frequency=1)
empleo.total.ts=ts(DATOSF.ts[,4],start=1980,frequency=1)
inversion.US.ts=ts(DATOSF.ts[,5],start=1980,frequency=1)
PIB.real.ts=ts(DATOSF.ts[,6],start=1980,frequency=1)
inversion.real.ts=ts(DATOSF.ts[,7],start=1980,frequency=1)
tasa.desempleo.ts=ts(DATOSF.ts[,8],start=1980,frequency=1)
inflacion.ts=ts(DATOSF.ts[,9],start=1980,frequency=1)


#PASO 2: modelo regresión lineal

Model_general <- lm(tasa.desempleo.ts ~ poblacion.total.ts + poblacion.crece.ts + empleo.total.ts + inversion.US.ts + PIB.real.ts + inversion.real.ts + inflacion.ts)

stargazer(Model_general,type="text")
## 
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                          tasa.desempleo.ts     
## -----------------------------------------------
## poblacion.total.ts           0.403***          
##                               (0.077)          
##                                                
## poblacion.crece.ts            2.738**          
##                               (1.040)          
##                                                
## empleo.total.ts             -0.0004***         
##                              (0.0001)          
##                                                
## inversion.US.ts              -0.00002          
##                              (0.00002)         
##                                                
## PIB.real.ts                   -0.0005          
##                              (0.0003)          
##                                                
## inversion.real.ts              0.001           
##                              (0.0005)          
##                                                
## inflacion.ts                  0.013**          
##                               (0.005)          
##                                                
## Constant                    -17.335***         
##                               (4.503)          
##                                                
## -----------------------------------------------
## Observations                    41             
## R2                             0.549           
## Adjusted R2                    0.454           
## Residual Std. Error       0.671 (df = 33)      
## F Statistic            5.746*** (df = 7; 33)   
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

Modelo óptimo

# df para matriz de correlación
datos_ts <- data.frame(poblacion.total.ts,
                       poblacion.crece.ts,
                       empleo.total.ts,
                       inversion.US.ts,
                       PIB.real.ts,
                       inversion.real.ts,
                       tasa.desempleo.ts,
                       inflacion.ts)

Multicolinealidad

# Análisis de multicolinealidad

library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:purrr':
## 
##     some
## The following object is masked from 'package:dplyr':
## 
##     recode
library(corrplot)
## corrplot 0.92 loaded
library(dplyr)

# VIF
V1 <- vif(Model_general)
barplot(V1, main = "VIF", horiz = TRUE, col = "pink",cex.names=0.8,las=1)
abline(v = 10, lwd = 3, lty = 2)

# Matriz de correlación
corrM<-round(cor(datos_ts),2)
corrplot(corrM,method="number",type="upper",tl.col="black",tl.cex=0.5,number.digits=1,number.cex=0.5)

# Modelo tras haber corregido multicolinealidad
Model_op2 <- lm(tasa.desempleo.ts ~ poblacion.crece.ts + inversion.US.ts + inversion.real.ts + inflacion.ts)

vif(Model_op2)
## poblacion.crece.ts    inversion.US.ts  inversion.real.ts       inflacion.ts 
##           4.041619           3.045987           5.437193           2.463518

Autocorrelación

# Análisis de autocorrelación

#Durbin - Watson

library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
dwtest(Model_op2)  
## 
##  Durbin-Watson test
## 
## data:  Model_op2
## DW = 0.86827, p-value = 3.434e-06
## alternative hypothesis: true autocorrelation is greater than 0
# Corrección por Prais-Winsten

library(prais)
## Loading required package: sandwich
## Loading required package: pcse
## 
## Attaching package: 'pcse'
## The following object is masked from 'package:sandwich':
## 
##     vcovPC
Model_op22 <- prais_winsten(Model_op2,index=fecha.ts,data=DATOSF.ts)
## Iteration 0: rho = 0
## Iteration 1: rho = 0.5611
## Iteration 2: rho = 0.7944
## Iteration 3: rho = 0.9218
## Iteration 4: rho = 0.9805
## Iteration 5: rho = 0.9869
## Iteration 6: rho = 0.9865
## Iteration 7: rho = 0.9866
## Iteration 8: rho = 0.9866
## Iteration 9: rho = 0.9866
summary(Model_op22)
## 
## Call:
## prais_winsten(formula = Model_op2, data = DATOSF.ts, index = fecha.ts)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -3.819 -2.735 -1.468  2.219  2.836 
## 
## AR(1) coefficient rho after 9 iterations: 0.9866
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         6.478e+00  2.865e+00   2.261   0.0299 *  
## poblacion.crece.ts  1.579e+00  8.185e-01   1.929   0.0616 .  
## inversion.US.ts    -6.696e-06  1.005e-05  -0.666   0.5095    
## inversion.real.ts  -1.205e-03  2.131e-04  -5.654 2.02e-06 ***
## inflacion.ts        8.339e-04  3.638e-03   0.229   0.8200    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4691 on 36 degrees of freedom
## Multiple R-squared:  0.5329, Adjusted R-squared:  0.481 
## F-statistic: 10.27 on 4 and 36 DF,  p-value: 1.188e-05
## 
## Durbin-Watson statistic (original): 0.8683 
## Durbin-Watson statistic (transformed):  1.07