###FÁTIMA GUADALUPE MARTÍN AVALOS 08 DE MARZO DE 2026######
####ECONOMETRÍA II#####
####LIBRARIES####
library(readxl)
library(ggplot2)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
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
library(summariser)
library(scales)
library(moments)
library(tseries)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(nortest)
library(mFilter)
library(lmtest)
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(forecast)
###DATABASE###
EXPORT <- read_excel("/cloud/project/Econometria2/EXPORT.xlsx",
sheet = "DB", range = "A1:D66")
#Reescalamiento de variables
EXPORT$pib <- EXPORT$PIB/1e9
EXPORT$exp <- EXPORT$Exportaciones/1e9
###Grafica###
plot(EXPORT$Exportaciones)

plot(EXPORT$Exportaciones, type="l",
col = "blue4", main = "Exportaciones",
ylab = "Pesos", xlab = "Indice")

plot(EXPORT$Año, EXPORT$TCN,
type="l",
col = "red4",
main = "TCN de México",
ylab = "Año", xlab = "Pesos/dl")

plot(EXPORT$pib, EXPORT$exp,
col = "orange3",
main = "Relacion exportaciones\n y PIB",
ylab = "Exportaciones", xlab = "PIB",
pch=20)

plot(EXPORT$TCN, EXPORT$Exportaciones,
col = "pink3",
main = "Relación exportaciones\n y TCN",
ylab = "Exportaciones", xlab = "TCN",
pch=20)

plot(EXPORT$TCN, EXPORT$Exportaciones,
col = "pink3",
main = "Relación exportaciones\n y TCN",
ylab = "Exportaciones", xlab = "TCN",
pch=20)
#Regresiones
modelo1 <- lm(EXPORT$exp~EXPORT$pib+EXPORT$TCN)
modelo1 <- lm(exp~pib+TCN, EXPORT)
summary(modelo1)
##
## Call:
## lm(formula = exp ~ pib + TCN, data = EXPORT)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1577.07 -292.96 86.64 240.43 1165.90
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -441.1529 241.1816 -1.829 0.072190 .
## pib 0.1054 0.0279 3.778 0.000357 ***
## TCN 359.1226 27.1142 13.245 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 554.7 on 62 degrees of freedom
## Multiple R-squared: 0.9725, Adjusted R-squared: 0.9716
## F-statistic: 1096 on 2 and 62 DF, p-value: < 2.2e-16
#Modelo logaritmico
modelo1_log <- lm(log(exp)~log(pib)+log(TCN), EXPORT)
summary(modelo1_log)
##
## Call:
## lm(formula = log(exp) ~ log(pib) + log(TCN), data = EXPORT)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.34215 -0.08633 -0.00919 0.13109 0.25167
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -7.26655 0.70826 -10.260 5.49e-15 ***
## log(pib) 1.56737 0.07509 20.873 < 2e-16 ***
## log(TCN) 0.14808 0.01514 9.779 3.52e-14 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.1496 on 62 degrees of freedom
## Multiple R-squared: 0.9892, Adjusted R-squared: 0.9889
## F-statistic: 2850 on 2 and 62 DF, p-value: < 2.2e-16
#Generar en la base de datos las variables en logaritmos
EXPORT$log_exp = log(EXPORT$exp)
EXPORT$log_pib = log(EXPORT$pib)
EXPORT$log_tcn = log(EXPORT$TCN)
modelo2 <- lm(exp~pib+TCN, EXPORT)
summary(modelo2)
##
## Call:
## lm(formula = exp ~ pib + TCN, data = EXPORT)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1577.07 -292.96 86.64 240.43 1165.90
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -441.1529 241.1816 -1.829 0.072190 .
## pib 0.1054 0.0279 3.778 0.000357 ***
## TCN 359.1226 27.1142 13.245 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 554.7 on 62 degrees of freedom
## Multiple R-squared: 0.9725, Adjusted R-squared: 0.9716
## F-statistic: 1096 on 2 and 62 DF, p-value: < 2.2e-16
#Evaluación de supuestos
##Modelos de series de tiempo
###Es muy comun que estas tengan autocorrelación y esta sea la razon de la R2 tan alta,
###para ello debemos quitar las tendencias y volver a evaluar
modeloLog <- lm(log_exp~log_pib+log_tcn, EXPORT)
summary(modeloLog)
##
## Call:
## lm(formula = log_exp ~ log_pib + log_tcn, data = EXPORT)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.34215 -0.08633 -0.00919 0.13109 0.25167
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -7.26655 0.70826 -10.260 5.49e-15 ***
## log_pib 1.56737 0.07509 20.873 < 2e-16 ***
## log_tcn 0.14808 0.01514 9.779 3.52e-14 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.1496 on 62 degrees of freedom
## Multiple R-squared: 0.9892, Adjusted R-squared: 0.9889
## F-statistic: 2850 on 2 and 62 DF, p-value: < 2.2e-16
modeloLog$residuals
## 1 2 3 4 5 6
## 0.149954128 0.158209829 0.146979868 0.051588656 -0.058001633 0.251672355
## 7 8 9 10 11 12
## 0.250868291 0.106193676 0.013910727 0.077505124 0.020629667 -0.009192191
## 13 14 15 16 17 18
## -0.002620383 -0.017655552 -0.107874996 -0.172973749 -0.201641152 -0.142922933
## 19 20 21 22 23 24
## -0.073565401 -0.047468419 0.020524812 -0.025041841 0.055771778 0.151047943
## 25 26 27 28 29 30
## 0.103309180 -0.035153189 -0.056884043 -0.118491443 -0.155534684 -0.200700796
## 31 32 33 34 35 36
## -0.264846286 -0.291587728 -0.333553470 -0.342154440 -0.328428225 -0.101209803
## 37 38 39 40 41 42
## -0.060996443 -0.092966504 -0.129139527 -0.112471110 -0.045636847 -0.018571198
## 43 44 45 46 47 48
## 0.007270794 -0.045507977 -0.051067008 -0.016889851 -0.007059646 -0.014803759
## 49 50 51 52 53 54
## -0.052990715 -0.086329119 0.055232700 0.093479171 0.106315726 0.124213122
## 55 56 57 58 59 60
## 0.152530959 0.152630316 0.131087866 0.131428896 0.161583309 0.179177368
## 61 62 63 64 65
## 0.226975148 0.211994499 0.246778003 0.138521288 0.144546859
modeloLog$fitted.values
## 1 2 3 4 5 6 7 8
## 4.563930 4.640403 4.711858 4.834035 5.010340 5.117851 5.210601 5.299783
## 9 10 11 12 13 14 15 16
## 5.440931 5.493618 5.592360 5.650249 5.774193 5.892803 5.980829 6.068375
## 17 18 19 20 21 22 23 24
## 6.167270 6.275906 6.411630 6.556957 6.689469 6.842703 6.965306 7.003011
## 25 26 27 28 29 30 31 32
## 7.106690 7.199513 7.265171 7.417444 7.510517 7.578125 7.678071 7.749640
## 33 34 35 36 37 38 39 40
## 7.808303 7.853595 7.932844 7.932555 8.052096 8.167142 8.282385 8.331705
## 41 42 43 44 45 46 47 48
## 8.406982 8.398116 8.399293 8.434195 8.495773 8.523370 8.596947 8.629574
## 49 50 51 52 53 54 55 56
## 8.646996 8.573820 8.639923 8.690482 8.753844 8.762605 8.807280 8.875114
## 57 58 59 60 61 62 63 64
## 8.926869 8.958002 8.991077 8.985059 8.864490 8.947929 9.003959 9.037120
## 65
## 9.063820
residuales <- modeloLog$residuals
#Normalidad
#Histograma
hist(residuales, main = "Modelo log_log",
ylab = "Frecuencia",
xlab = "Residuales",
col = "blue4")

#Histograma, bien hecho, sin frecuencias (con curva normal)
#(las curvas de densidad, matematicamente, solo se calculan con probabilidades(densidad), no frecuencias)
hist(residuales, main = "Modelo log_log",
ylab = "Densidad",
xlab = "Residuales",
col = "red4",
freq = F)
curve(dnorm(x,mean = mean(residuales),
sd=sd(residuales)),
from = -0.4, to= 0.3,
add=TRUE, col="pink2", lwd = 3)

#Diagrama de caja
boxplot(residuales, horizontal = T,
main = "Diagrama de caja",
col = "brown")

#para que sea una distribucion normal, la caja deberia estar justo
#al medio de los bigotes y la media justo al medio de la caja
#Cuantil1-cuantil
qqnorm(residuales, pch = 20,
main = "Grafica de caurtiles",
col = "orange2")
qqline(residuales, col = "red2", lwd=3)

#la t y F se basan en el supuesto de normalidad, si un modelo no cumple el supuesto de normalidad,
#ni aunque la t o f digan "significancia", estas no estarian diciendo nada y se debe
#correggir el modelo para quitar los sesgos
#Juntar las 3 grafícas en una sola
#Particionar en una fila y 3 col
#Reajustar las matrices
par(mfrow = c(1,1))
hist(residuales, main = "Modelo log_log",
ylab = "Densidad",
xlab = "Residuales",
col = "red4",
freq = F)
curve(dnorm(x,mean = mean(residuales),
sd=sd(residuales)),
from = -0.4, to= 0.3,
add=TRUE, col="pink2", lwd = 3)

boxplot(residuales, horizontal = F,
main = "Diagrama de caja",
col = "brown")

#Cuantil1-cuantil
qqnorm(residuales, pch = 20,
main = "Grafica de caurtiles",
col = "orange2")
qqline(residuales, col = "red2", lwd=3)

#Revisar######
skewness(residuales)
## [1] -0.3865773
sesgo = skewness(residuales)
kurtosis(residuales)
## [1] 2.687589
curtosis=kurtosis(residuales)
nobs(modeloLog)
## [1] 65
nobs(modeloLog)*((sesgo^2)/6+((curtosis-3)^2)/24)
## [1] 1.88329
jb=nobs(modeloLog)*((sesgo^2)/6+((curtosis-3)^2)/24)
summary(jb)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.883 1.883 1.883 1.883 1.883 1.883
#2gl para JB
#Valor critico
valcrit_ji2=qchisq(.95,2)
jarque.bera.test(residuales)
##
## Jarque Bera Test
##
## data: residuales
## X-squared = 1.8833, df = 2, p-value = 0.39
#Cuando evaluamos los supuestos se busca que los estadisticos
#de prueba no sean significativos para que cumplan el supuesto de normalidad
#Prueba de Shapiro_Wilk:
shapiro.test(residuales)
##
## Shapiro-Wilk normality test
##
## data: residuales
## W = 0.96716, p-value = 0.08161
#No se rechaza H0 porque p mayor que .05
#Prueba de Anderson-Darling
ad.test(residuales)
##
## Anderson-Darling normality test
##
## data: residuales
## A = 0.53284, p-value = 0.1668
###Conclusion
#Pasa el supuesto de normalidad (los 3 test)
#COrregir la no normalidad con modelo en logaritmos
g1 <- ggplot(EXPORT, aes(x = Año, y = exp)) +
geom_line(color = "maroon", linewidth = 1) +
geom_vline(xintercept = 2009, color = "pink2", linetype = "dashed", linewidth = .8) +
labs(title = "Exportaciones de México",
x = "Año",
y = "Exportaciones (mdm pesos)") +
theme_minimal() +
theme(
plot.background = element_rect(fill = "gray15", color = NA),
panel.background = element_rect(fill = "gray15", color = NA),
panel.grid.major = element_line(color = "gray25"),
panel.grid.minor = element_line(color = "gray"),
axis.text = element_text(color = "white"),
axis.title = element_text(color = "white"),
plot.title = element_text(color = "white")
) + theme(
plot.title = element_text(
hjust = 0.5,
face = "bold",
size = 14
)
)
ggplotly(g1)
g2 <- ggplot(EXPORT, aes(x = Año, y = pib)) +
geom_line(color = "aquamarine", linewidth = 1) +
geom_vline(xintercept = 2009, color = "pink2", linetype = "dashed", linewidth = .8) +
labs(title = "Comportamiento del PIB, 1960-2024",
x = "Año",
y = "Miles de millones de pesos cosntantes") +
theme_minimal() +
theme(
plot.background = element_rect(fill = "gray15", color = NA),
panel.background = element_rect(fill = "gray15", color = NA),
panel.grid.major = element_line(color = "gray25"),
panel.grid.minor = element_line(color = "gray"),
axis.text = element_text(color = "white"),
axis.title = element_text(color = "white"),
plot.title = element_text(color = "white")
) + theme(
plot.title = element_text(
hjust = 0.5,
face = "bold",
size = 14
)
)
ggplotly(g2)
g3 <- ggplot(EXPORT, aes(x = Año, y = TCN)) +
geom_line(color = "red4", linewidth = 1) +
geom_vline(xintercept = 2009, color = "pink2", linetype = "dashed", linewidth = .8) +
labs(title = "Tipo de Cambio Nominal (TCN), 1960-2024",
x = "Año",
y = "Pesos por dólar") +
theme_minimal() +
theme(
plot.background = element_rect(fill = "gray15", color = NA),
panel.background = element_rect(fill = "gray15", color = NA),
panel.grid.major = element_line(color = "gray25"),
panel.grid.minor = element_line(color = "gray"),
axis.text = element_text(color = "white"),
axis.title = element_text(color = "white"),
plot.title = element_text(color = "white")
) + theme(
plot.title = element_text(
hjust = 0.5,
face = "bold",
size = 14
)
)
ggplotly(g3)
g4 <-ggplot(EXPORT, aes(x = pib, y = exp)) +
geom_point(color = "purple3", size = 2) +
geom_smooth(se = TRUE, color = "pink2") +
labs(title = "Relación entre Exportaciones y PIB 1960-2024",
x = "PIB (miles de millones de pesos)",
y = "Exportaciones (miles de millones de pesos)") +
theme_minimal() +
theme(
plot.background = element_rect(fill = "gray15", color = NA),
panel.background = element_rect(fill = "gray15", color = NA),
panel.grid.major = element_line(color = "gray25"),
panel.grid.minor = element_line(color = "gray"),
axis.text = element_text(color = "white"),
axis.title = element_text(color = "white"),
plot.title = element_text(color = "white")
) + theme(
plot.title = element_text(
hjust = 0.5,
face = "bold",
size = 14
)
)
ggplotly(g4)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
###DUMS####
EXPORT$tc_pib <- ((EXPORT$pib/lag(EXPORT$pib, default = NA ))-1)*100
EXPORT$tc_tcn <- round(((EXPORT$TCN / lag(EXPORT$TCN, default = NA)) - 1) * 100, 2)
EXPORT$tc_exp <- round(((EXPORT$exp/lag(EXPORT$exp, default = NA ))-1)*100, 2)
modelo_dums = lm(tc_exp~tc_pib+tc_tcn, EXPORT)
summary(modelo_dums)
##
## Call:
## lm(formula = tc_exp ~ tc_pib + tc_tcn, data = EXPORT)
##
## Residuals:
## Min 1Q Median 3Q Max
## -14.29 -4.61 -0.60 2.41 41.62
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.78693 1.79467 0.996 0.323334
## tc_pib 1.17735 0.31175 3.777 0.000363 ***
## tc_tcn 0.10892 0.03589 3.035 0.003531 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 8.405 on 61 degrees of freedom
## (1 observation deleted due to missingness)
## Multiple R-squared: 0.2097, Adjusted R-squared: 0.1838
## F-statistic: 8.094 on 2 and 61 DF, p-value: 0.0007623
g5 <- ggplot(EXPORT, aes(x = Año, y = tc_pib)) +
geom_line(color = "#9AFF9A", linewidth = .5) +
geom_vline(xintercept = 1981, color = "deeppink", linetype = "dashed", linewidth = .5) +
labs(title = " Tasa de crecimiento PIB, 1960-2024",
x = "Año",
y = "Tasa crecimiento PIB") +theme_minimal()+
theme(
plot.background = element_rect(fill = "gray15", color = NA),
panel.background = element_rect(fill = "gray15", color = NA),
panel.grid.major = element_line(color = "gray15"),
panel.grid.minor = element_line(color = "gray"),
axis.text = element_text(color = "white"),
axis.title = element_text(color = "white"),
plot.title = element_text(color = "white")
)
ggplotly(g5)
##abline(v = (1981))
g6<- ggplot(EXPORT, aes(x = Año, y = tc_tcn)) +
geom_line(color = "#BF3EFF", linewidth = .5) +
geom_vline(xintercept = 1977, color = "#BBFFFF", linetype = "dashed", linewidth = .5) +
geom_vline(xintercept = 1981, color = "#BBFFFF", linetype = "dashed", linewidth = .5) +
geom_vline(xintercept = 1984, color = "#BBFFFF", linetype = "dashed", linewidth = .5) +
geom_vline(xintercept = 1986, color = "#BBFFFF", linetype = "dashed", linewidth = .5) +
geom_vline(xintercept = 1989, color = "#BBFFFF", linetype = "dashed", linewidth = .5) +
geom_vline(xintercept = 1995, color = "#BBFFFF", linetype = "dashed", linewidth = .5) +
labs(title = "Tasa de crecimiento TCN, 1960-2024",
x = "Año",
y = "Tasa crecimiento TCN porcentual") + theme_minimal()+
theme(
plot.background = element_rect(fill = "gray15", color = NA),
panel.background = element_rect(fill = "gray15", color = NA),
panel.grid.major = element_line(color = "gray15"),
panel.grid.minor = element_line(color = "white"),
axis.text = element_text(color = "white"),
axis.title = element_text(color = "white"),
plot.title = element_text(color = "white")
)
ggplotly(g6)
g7 <- ggplot(EXPORT, aes(x = Año, y = tc_exp))+
geom_line(color = "aquamarine1", linewidth = .5) +
geom_vline(xintercept = 1966, color = "maroon1", linetype = "dashed", linewidth = .5) +
geom_vline(xintercept = 2009, color = "maroon1", linetype = "dashed", linewidth = .5) +
labs(title = "Tasa de crecimiento Exportaciones, 1960-2024",
x = "Año",
y = "Tasa crecimiento Exportaciones ") +theme_minimal()+
theme(
plot.background = element_rect(fill = "gray15", color = NA),
panel.background = element_rect(fill = "gray15", color = NA),
panel.grid.major = element_line(color = "gray15"),
panel.grid.minor = element_line(color = "gray"),
axis.text = element_text(color = "white"),
axis.title = element_text(color = "white"),
plot.title = element_text(color = "white")
)
ggplotly(g7)
#Estimar tasa de crecimiento por periodos
#Calcula las tasas de crecimiento para periodos especificos
filter(EXPORT, Año>=1961 & Año<=1981) %>% summarise(mean(tc_pib))
## # A tibble: 1 × 1
## `mean(tc_pib)`
## <dbl>
## 1 6.87
filter(EXPORT, Año>=1981) %>% summarise(mean(tc_pib))
## # A tibble: 1 × 1
## `mean(tc_pib)`
## <dbl>
## 1 2.09
mean(EXPORT$tc_pib, na.rm = T)
## [1] 3.541608
periodo1=filter(EXPORT, Año>=1961&Año<=1981)%>%summarise(mean(tc_pib))
periodo2=filter(EXPORT, Año>1981)%>%summarise(mean(tc_pib))
g8 <- ggplot(EXPORT, aes(x = Año, y = tc_pib)) +
geom_line(color = "red4", linewidth = .5) +
geom_vline(xintercept = 1981, color = "pink2", linetype = "dashed", linewidth = .5) +
geom_hline(yintercept = c(6.87, 1.92), color = "pink2", linetype = "l", linewidth = .5) +
labs(title = "Tasa de crecimiento PIB, 1960-2024",
x = "Año",
y = "Tasa crecimiento PIB") +theme_minimal()+
theme(
plot.background = element_rect(fill = "gray5", color = NA),
panel.background = element_rect(fill = "gray5", color = NA),
panel.grid.major = element_line(color = "gray15"),
panel.grid.minor = element_line(color = "gray"),
axis.text = element_text(color = "white"),
axis.title = element_text(color = "white"),
plot.title = element_text(color = "white")
)
ggplotly(g8)
#Variable dummy para diferenciar los dos periodos
EXPORT<-EXPORT%>%
mutate(dum_81=ifelse(Año>1981,1,0))
summary(lm(tc_pib~dum_81, EXPORT))
##
## Call:
## lm(formula = tc_pib ~ dum_81, data = EXPORT)
##
## Residuals:
## Min 1Q Median 3Q Max
## -10.2706 -1.1004 0.2138 1.8168 5.2823
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.8691 0.6790 10.117 9.50e-15 ***
## dum_81 -4.9526 0.8283 -5.979 1.21e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.111 on 62 degrees of freedom
## (1 observation deleted due to missingness)
## Multiple R-squared: 0.3657, Adjusted R-squared: 0.3555
## F-statistic: 35.75 on 1 and 62 DF, p-value: 1.208e-07
#El resultado muestra dos cosas:la diferencia entre los dos periodos es estaditicamente significativa
#y muestra el promedio del periodo1 (B0) y el periodo2 (B0+B1)
#Estimacion del modelo en tasas de crecimiento
modelo_tc<-lm(tc_exp~tc_pib+tc_tcn,EXPORT)
summary(modelo_tc)
##
## Call:
## lm(formula = tc_exp ~ tc_pib + tc_tcn, data = EXPORT)
##
## Residuals:
## Min 1Q Median 3Q Max
## -14.29 -4.61 -0.60 2.41 41.62
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.78693 1.79467 0.996 0.323334
## tc_pib 1.17735 0.31175 3.777 0.000363 ***
## tc_tcn 0.10892 0.03589 3.035 0.003531 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 8.405 on 61 degrees of freedom
## (1 observation deleted due to missingness)
## Multiple R-squared: 0.2097, Adjusted R-squared: 0.1838
## F-statistic: 8.094 on 2 and 61 DF, p-value: 0.0007623
dwtest(modelo_tc)
##
## Durbin-Watson test
##
## data: modelo_tc
## DW = 1.7814, p-value = 0.1535
## alternative hypothesis: true autocorrelation is greater than 0
#Modelo en diferencias loaritmicas
EXPORT$dif_Lexp = c(NA, diff(EXPORT$log_exp))
EXPORT$dif_Lpib = c(NA, diff(EXPORT$log_pib))
EXPORT$dif_Ltcn = c(NA, diff(EXPORT$log_tcn))
#Graficas
plot(EXPORT$dif_Lexp, type = "l", main = "Dif. Log(Exportaciones)")

plot(EXPORT$dif_Lpib, type = "l", main = "Dif. Log(PIB)")

plot(EXPORT$dif_Ltcn, type = "l", main = "Dif. Log(TCN)")

modelo_dif <- lm(dif_Lexp ~ dif_Lpib + dif_Ltcn, data = EXPORT)
summary(modelo_dif)
##
## Call:
## lm(formula = dif_Lexp ~ dif_Lpib + dif_Ltcn, data = EXPORT)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.14492 -0.04496 -0.00452 0.02480 0.32523
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.01540 0.01585 0.972 0.334899
## dif_Lpib 1.11606 0.27807 4.014 0.000166 ***
## dif_Ltcn 0.14722 0.04662 3.158 0.002473 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.07305 on 61 degrees of freedom
## (1 observation deleted due to missingness)
## Multiple R-squared: 0.2273, Adjusted R-squared: 0.202
## F-statistic: 8.973 on 2 and 61 DF, p-value: 0.0003837
dwtest(modelo_dif)
##
## Durbin-Watson test
##
## data: modelo_dif
## DW = 1.7423, p-value = 0.1184
## alternative hypothesis: true autocorrelation is greater than 0
# FILTRO HODRICK-PRESCOTT
# Residuales del modelo
residual2 <- resid(modelo_tc)
# Convertir a serie de tiempo
residual2ts <- ts(residual2, start = 1961, frequency = 1)
# Aplicar filtro HP (datos anuales)
hp_residual2 <- hpfilter(residual2ts, freq = 100)
# Graficar residuales y tendencia
plot(residual2ts,
type = "l",
col = "black",
lwd = 1,
main = "Residuales y tendencia HP",
xlab = "Año",
ylab = "Residuales")
lines(hp_residual2$trend,
col = "maroon2",
lwd = 2)
legend("topright",
legend = c("Residuales", "Tendencia HP"),
col = c("black", "maroon2"),
lwd = c(1,2))

##tendencia <- hp_residual2$trend
##plot(tendencia)
#o bien de modo manual
plot(residual2ts)
lines(hp_residual2$trend,col="red4",lwd=2)

#Conclusion, si hay una tendencia bastante notoria
#Coeficientes de autocorrelacion (acf) y autocorrelacion parcial (pacf)
acf(modelo_tc$residuals)

acf_tc <-acf(modelo_tc$residuals)
acf_tc
##
## Autocorrelations of series 'modelo_tc$residuals', by lag
##
## 0 1 2 3 4 5 6 7 8 9 10
## 1.000 0.109 -0.219 -0.074 0.144 -0.046 -0.087 -0.068 -0.140 -0.230 -0.151
## 11 12 13 14 15 16 17 18
## -0.028 0.047 0.010 0.026 0.157 0.019 0.191 0.176
pacf(modelo_tc$residuals)

pacf_tc <- pacf(modelo_tc$residuals)
pacf_tc
##
## Partial autocorrelations of series 'modelo_tc$residuals', by lag
##
## 1 2 3 4 5 6 7 8 9 10 11
## 0.109 -0.234 -0.021 0.113 -0.108 -0.016 -0.075 -0.189 -0.231 -0.208 -0.159
## 12 13 14 15 16 17 18
## -0.064 -0.071 -0.033 0.095 -0.123 0.187 0.063
#Usar intervalos de confianza para saber si estan dentro, y si se salen son arc tipo 1 o 2
#prueba de durbin-watson
dwtest(modelo_tc)
##
## Durbin-Watson test
##
## data: modelo_tc
## DW = 1.7814, p-value = 0.1535
## alternative hypothesis: true autocorrelation is greater than 0
#Breusch-goldfrey
bgtest(modelo_tc)
##
## Breusch-Godfrey test for serial correlation of order up to 1
##
## data: modelo_tc
## LM test = 0.78815, df = 1, p-value = 0.3747
bgtest(modelo_tc, order =1)
##
## Breusch-Godfrey test for serial correlation of order up to 1
##
## data: modelo_tc
## LM test = 0.78815, df = 1, p-value = 0.3747
bgtest(modelo_tc, order =2)
##
## Breusch-Godfrey test for serial correlation of order up to 2
##
## data: modelo_tc
## LM test = 4.3376, df = 2, p-value = 0.1143
bgtest(modelo_tc, order =3)#order es para la periodicidad, 1 = anual
##
## Breusch-Godfrey test for serial correlation of order up to 3
##
## data: modelo_tc
## LM test = 4.3577, df = 3, p-value = 0.2253
bgtest(modelo_tc, order =4)
##
## Breusch-Godfrey test for serial correlation of order up to 4
##
## data: modelo_tc
## LM test = 5.1453, df = 4, p-value = 0.2727
#Estan mal las pruebas, resultados mal
Box.test(modelo_tc$residuals, lag =1, type = "Ljung-Box")
##
## Box-Ljung test
##
## data: modelo_tc$residuals
## X-squared = 0.79804, df = 1, p-value = 0.3717
Box.test(modelo_tc$residuals, lag =2, type = "Ljung-Box")
##
## Box-Ljung test
##
## data: modelo_tc$residuals
## X-squared = 4.0758, df = 2, p-value = 0.1303
Box.test(modelo_tc$residuals, lag =3, type = "Ljung-Box")
##
## Box-Ljung test
##
## data: modelo_tc$residuals
## X-squared = 4.4579, df = 3, p-value = 0.2161
Box.test(modelo_tc$residuals, lag =4, type = "Ljung-Box")
##
## Box-Ljung test
##
## data: modelo_tc$residuals
## X-squared = 5.9178, df = 4, p-value = 0.2054
Box.test(modelo_tc$residuals, lag =1, type = "Ljung-Box")
##
## Box-Ljung test
##
## data: modelo_tc$residuals
## X-squared = 0.79804, df = 1, p-value = 0.3717
#Conclusión: No hay autocorrelacion positiva
#Esos 4 años pasados no afectan al presente
#Pruebas de normalidad
jarque.bera.test(modelo_tc$residuals)
##
## Jarque Bera Test
##
## data: modelo_tc$residuals
## X-squared = 258.56, df = 2, p-value < 2.2e-16
#no pasa la prueba de normalidad
ad.test(modelo_tc$residuals)
##
## Anderson-Darling normality test
##
## data: modelo_tc$residuals
## A = 1.9811, p-value = 4.19e-05
shapiro.test(modelo_tc$residuals)
##
## Shapiro-Wilk normality test
##
## data: modelo_tc$residuals
## W = 0.83591, p-value = 6.286e-07
#Recordar residuo
plot(modelo_tc$residuals, type = "l")
abline(v=(1965))

#Introducir una dummie en 1965
EXPORT = EXPORT %>%
mutate(dum65 = ifelse(Año == 1965, 1, 0))
summary(modelo_dums)
##
## Call:
## lm(formula = tc_exp ~ tc_pib + tc_tcn, data = EXPORT)
##
## Residuals:
## Min 1Q Median 3Q Max
## -14.29 -4.61 -0.60 2.41 41.62
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.78693 1.79467 0.996 0.323334
## tc_pib 1.17735 0.31175 3.777 0.000363 ***
## tc_tcn 0.10892 0.03589 3.035 0.003531 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 8.405 on 61 degrees of freedom
## (1 observation deleted due to missingness)
## Multiple R-squared: 0.2097, Adjusted R-squared: 0.1838
## F-statistic: 8.094 on 2 and 61 DF, p-value: 0.0007623
#### Modelo en tasas de crecimiento con dummies #####
modelo_tc_dum <- lm(tc_exp ~ tc_pib + tc_tcn + dum_81 + dum65, EXPORT)
summary(modelo_tc_dum)
##
## Call:
## lm(formula = tc_exp ~ tc_pib + tc_tcn + dum_81 + dum65, data = EXPORT)
##
## Residuals:
## Min 1Q Median 3Q Max
## -13.9146 -3.9840 -0.1508 2.7033 19.9805
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.57790 2.54212 0.621 0.537187
## tc_pib 1.02566 0.29596 3.465 0.000993 ***
## tc_tcn 0.10946 0.02799 3.910 0.000241 ***
## dum_81 0.10053 2.21286 0.045 0.963918
## dum65 42.90994 6.70366 6.401 2.77e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 6.541 on 59 degrees of freedom
## (1 observation deleted due to missingness)
## Multiple R-squared: 0.537, Adjusted R-squared: 0.5056
## F-statistic: 17.11 on 4 and 59 DF, p-value: 2.297e-09
#La dummy 1981 no es significativa
#La dummy 1965 si es significativa, hay un crecimiento de las exportaciones
#no se explica por PIB o tipo de cambio
#####PRUEBAS######
##AUTOCORRELACIÓN##
dwtest(modelo_tc_dum)
##
## Durbin-Watson test
##
## data: modelo_tc_dum
## DW = 1.3498, p-value = 0.001807
## alternative hypothesis: true autocorrelation is greater than 0
#Existe autocorrelación positiva
bgtest(modelo_tc_dum)
##
## Breusch-Godfrey test for serial correlation of order up to 1
##
## data: modelo_tc_dum
## LM test = 7.2315, df = 1, p-value = 0.007164
bgtest(modelo_tc_dum, order = 1)#Muestra autocorrelación
##
## Breusch-Godfrey test for serial correlation of order up to 1
##
## data: modelo_tc_dum
## LM test = 7.2315, df = 1, p-value = 0.007164
bgtest(modelo_tc_dum, order = 2)#Muestra autocorrelación
##
## Breusch-Godfrey test for serial correlation of order up to 2
##
## data: modelo_tc_dum
## LM test = 7.7298, df = 2, p-value = 0.02096
bgtest(modelo_tc_dum, order = 3) #Muestra autocorrelación
##
## Breusch-Godfrey test for serial correlation of order up to 3
##
## data: modelo_tc_dum
## LM test = 7.8737, df = 3, p-value = 0.04869
bgtest(modelo_tc_dum, order = 4)#No autocorrelación
##
## Breusch-Godfrey test for serial correlation of order up to 4
##
## data: modelo_tc_dum
## LM test = 8.1969, df = 4, p-value = 0.08462
####LJUNG_BOX###
Box.test(modelo_tc_dum$residuals, lag = 1, type = "Ljung-Box")#Muestra autocorrelación
##
## Box-Ljung test
##
## data: modelo_tc_dum$residuals
## X-squared = 7.0461, df = 1, p-value = 0.007944
Box.test(modelo_tc_dum$residuals, lag = 2, type = "Ljung-Box")#Muestra autocorrelación
##
## Box-Ljung test
##
## data: modelo_tc_dum$residuals
## X-squared = 7.06, df = 2, p-value = 0.0293
Box.test(modelo_tc_dum$residuals, lag = 3, type = "Ljung-Box")#No autocorrelación
##
## Box-Ljung test
##
## data: modelo_tc_dum$residuals
## X-squared = 7.0881, df = 3, p-value = 0.06914
Box.test(modelo_tc_dum$residuals, lag = 4, type = "Ljung-Box")#No autocorrelación
##
## Box-Ljung test
##
## data: modelo_tc_dum$residuals
## X-squared = 7.8224, df = 4, p-value = 0.0983
##NORMALIDAD##
jarque.bera.test(modelo_tc_dum$residuals) #rechaza normalidad
##
## Jarque Bera Test
##
## data: modelo_tc_dum$residuals
## X-squared = 6.8158, df = 2, p-value = 0.03311
shapiro.test(modelo_tc_dum$residuals)
##
## Shapiro-Wilk normality test
##
## data: modelo_tc_dum$residuals
## W = 0.9685, p-value = 0.1011
ad.test(modelo_tc_dum$residuals)
##
## Anderson-Darling normality test
##
## data: modelo_tc_dum$residuals
## A = 0.60553, p-value = 0.1109
# Residuales del modelo
residual_tc_dum <- resid(modelo_tc_dum)
# Convertir a serie de tiempo
residualdum_ts <- ts(residual_tc_dum, start = 1961, frequency = 1)
# Aplicar filtro HP (datos anuales)
hp_residualdum <- hpfilter(residualdum_ts, freq = 100)
##GRAFICA##
# Graficar residuales y tendencia
plot(residualdum_ts,
type = "l",
col = "black",
lwd = 1,
main = "Residuales y tendencia HP",
xlab = "Año",
ylab = "Residuales")
lines(hp_residualdum$trend,
col = "maroon4",
lwd = 2)
legend("topright",
legend = c("Residuales", "Tendencia HP"),
col = c("black", "maroon4"),
lwd = c(1,2))

modelo_dif_dum <- lm(dif_Lexp ~ dif_Lpib + dif_Ltcn + dum_81 + dum65, EXPORT)
summary(modelo_dif_dum)
##
## Call:
## lm(formula = dif_Lexp ~ dif_Lpib + dif_Ltcn + dum_81 + dum65,
## data = EXPORT)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.142338 -0.040197 -0.000013 0.027238 0.178039
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.013014 0.023312 0.558 0.578780
## dif_Lpib 1.003230 0.277841 3.611 0.000631 ***
## dif_Ltcn 0.149047 0.038736 3.848 0.000295 ***
## dum_81 0.001172 0.020293 0.058 0.954126
## dum65 0.335356 0.062100 5.400 1.25e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.06059 on 59 degrees of freedom
## (1 observation deleted due to missingness)
## Multiple R-squared: 0.4858, Adjusted R-squared: 0.451
## F-statistic: 13.94 on 4 and 59 DF, p-value: 4.603e-08
EXPORT$L_dif_Lexp <- lag(EXPORT$dif_Lexp)
modelo_dif_corr <- lm(dif_Lexp ~ L_dif_Lexp + dif_Lpib + dif_Ltcn + dum_81 + dum65, EXPORT)
summary(modelo_dif_corr)
##
## Call:
## lm(formula = dif_Lexp ~ L_dif_Lexp + dif_Lpib + dif_Ltcn + dum_81 +
## dum65, data = EXPORT)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.139671 -0.035323 -0.001082 0.028891 0.181007
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.0004842 0.0246476 -0.020 0.984395
## L_dif_Lexp 0.1614493 0.0972339 1.660 0.102321
## dif_Lpib 0.9467952 0.2797379 3.385 0.001296 **
## dif_Ltcn 0.1401892 0.0389492 3.599 0.000669 ***
## dum_81 0.0078154 0.0207637 0.376 0.708019
## dum65 0.3419544 0.0617908 5.534 8.22e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.06013 on 57 degrees of freedom
## (2 observations deleted due to missingness)
## Multiple R-squared: 0.5106, Adjusted R-squared: 0.4676
## F-statistic: 11.89 on 5 and 57 DF, p-value: 6.7e-08
##PRUEBAS##
dwtest(modelo_dif_corr)
##
## Durbin-Watson test
##
## data: modelo_dif_corr
## DW = 1.608, p-value = 0.03431
## alternative hypothesis: true autocorrelation is greater than 0
bgtest(modelo_dif_corr)
##
## Breusch-Godfrey test for serial correlation of order up to 1
##
## data: modelo_dif_corr
## LM test = 5.803, df = 1, p-value = 0.016
Box.test(resid(modelo_dif_corr), lag = 1, type = "Ljung-Box")
##
## Box-Ljung test
##
## data: resid(modelo_dif_corr)
## X-squared = 2.5121, df = 1, p-value = 0.113
jarque.bera.test(resid(modelo_dif_corr))
##
## Jarque Bera Test
##
## data: resid(modelo_dif_corr)
## X-squared = 10.6, df = 2, p-value = 0.004992
# Residuales del modelo
residual_dum_corr <- resid(modelo_dif_corr)
# Convertir a serie de tiempo
residualdumcorr_ts <- ts(residual_dum_corr, start = 1961, frequency = 1)
# Aplicar filtro HP (datos anuales)
hp_residualdum_corr <- hpfilter(residualdumcorr_ts, freq = 100)
##GRAFICA##
# Graficar residuales y tendencia
plot(residualdumcorr_ts,
type = "l",
col = "black",
lwd = 1,
main = "Residuales y tendencia HP",
xlab = "Año",
ylab = "Residuales")
lines(hp_residualdum_corr$trend,
col = "red3",
lwd = 2)
legend("topright",
legend = c("Residuales", "Tendencia HP"),
col = c("black", "red3"),
lwd = c(1,2))
