Martha Lizeth Pastrana Basilio 202050617 EconometrĆa ll
Sesión 1
#Bibliotecas
library(dynlm) #for the `dynlm()` function"readxl"))
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(orcutt) # for the `cochrane.orcutt()` function
## Loading required package: lmtest
library(nlWaldTest) # for the `nlWaldtest()` function
library(zoo) # for time series functions (not much used here)
library(pdfetch) # for retrieving data (just mentioned here)
library(lmtest) #for `coeftest()` and `bptest()
library(broom) #for `glance(`) and `tidy()`
library(car) #for `hccm()` robust standard errors
## Loading required package: carData
library(sandwich)
library(knitr) #for kable()
library(forecast)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(readxl)
#Base de datos
usmacro <- read_excel("usmacro.xlsx")
summary(usmacro)#Revisamos si estamos hablando de una serie de tiempo
## dateid01 g inf
## Min. :1948-01-01 00:00:00.000 Min. :-1.975 Min. :-2.2900
## 1st Qu.:1965-01-01 00:00:00.000 1st Qu.: 0.986 1st Qu.: 0.3900
## Median :1982-01-01 00:00:00.000 Median : 1.473 Median : 0.7640
## Mean :1981-12-31 02:17:08.570 Mean : 1.575 Mean : 0.8607
## 3rd Qu.:1999-01-01 00:00:00.000 3rd Qu.: 2.138 3rd Qu.: 1.1360
## Max. :2016-01-01 00:00:00.000 Max. : 6.123 Max. : 4.0660
## u
## Min. : 2.600
## 1st Qu.: 4.700
## Median : 5.600
## Mean : 5.819
## 3rd Qu.: 6.900
## Max. :10.700
rev.ts <- is.ts(usmacro)
En nuestra base de datos tenemos tres variables y una columna id con las fechas, con el codigo anterior sabemos que no tiene forma de serie de tiempo, por lo que vamos a declararla como una serie de tiempo.
usmacro.ts <- ts(usmacro, start = c(1948,1), end = c(2016,1), frequency = 4)
#Ponemos donde comienza y termina la serie de tiempo, y sabemos que esta dividida en semestres
Graficamos para en decrecimiento del GDP
plot(usmacro.ts[,"g"], ylab="Tasa de crecimiento GDP")
Graficamos para el crecimiento del desempleo
plot(usmacro.ts[,"u"], ylab="Tasa de desempleo")
Comprobamos autocorrelacion en los rezagos con un grafico para notar patrones
#Primer Rezago
ggL1 <- data.frame(cbind(usmacro.ts[, "g"],
lag(usmacro.ts[, "g"], -1)))
#Graficamos
names(ggL1) <- c("g", "gL1")
plot(ggL1)
meang <- mean(ggL1$g, na.rm=TRUE)
abline(v=meang, lty=2)
abline(h=mean(ggL1$gL1, na.rm=TRUE), lty=2)
Segundo Rezago
ggL2 <- data.frame(cbind(usmacro.ts[, "g"],
lag(usmacro.ts[, "g"], -2)))
names(ggL2) <- c("g", "gL2")
plot(ggL2)
meang <- mean(ggL2$g, na.rm=TRUE)
abline(v=meang, lty=2)
abline(h=mean(ggL2$gL2, na.rm=TRUE), lty=2)
Sesión 2
Autocorrelograma
acf(usmacro.ts[,"g"])
Datos sin grafico
acf(usmacro.ts[,"g"], plot = FALSE)
##
## Autocorrelations of series 'usmacro.ts[, "g"]', by lag
##
## 0.00 0.25 0.50 0.75 1.00 1.25 1.50 1.75 2.00 2.25 2.50
## 1.000 0.507 0.369 0.149 0.085 -0.024 0.053 0.098 0.126 0.220 0.232
## 2.75 3.00 3.25 3.50 3.75 4.00 4.25 4.50 4.75 5.00 5.25
## 0.186 0.078 0.062 0.105 0.106 0.235 0.232 0.271 0.199 0.192 0.089
## 5.50 5.75 6.00
## 0.080 0.061 0.126
FORECAST Trabajamos con un modelo AR(2) para el desempleo
usmacro.ts.tab <- cbind(
usmacro.ts,
lag(usmacro.ts[, 4], -1),
diff(usmacro.ts[, 4], lag = 1),
lag(usmacro.ts[, 2], -1),
lag(usmacro.ts[, 2], -2),
lag(usmacro.ts[, 2], -3)
)
kable(
head(usmacro.ts.tab),
caption = "Diversos rezagos y diferencias",
col.names = c("id", "g", "inf", "u", "uL1", "du", "gL1", "gL2", "gL3"),
digits = 2
)
| id | g | inf | u | uL1 | du | gL1 | gL2 | gL3 |
|---|---|---|---|---|---|---|---|---|
| -694310400 | 2.267 | 2.119 | 3.7 | NA | NA | NA | NA | NA |
| -686448000 | 2.517 | 1.592 | 3.7 | 3.7 | 0.0 | 2.267 | NA | NA |
| -678585600 | 2.418 | 1.684 | 3.8 | 3.7 | 0.1 | 2.517 | 2.267 | NA |
| -670636800 | 0.429 | -0.918 | 3.8 | 3.8 | 0.0 | 2.418 | 2.517 | 2.267 |
| -662688000 | -1.888 | -0.951 | 4.7 | 3.8 | 0.9 | 0.429 | 2.418 | 2.517 |
| -654912000 | -1.344 | -0.109 | 5.9 | 4.7 | 1.2 | -1.888 | 0.429 | 2.418 |
El modelo AR(2) para el desempleo:
# Definir la variable que proviene de la serie de tiempo
u <- usmacro.ts[, "u"]
u.ar2 <- dynlm(u ~ L(u, 1:2))#Rezagos del u del 1-2, si son mas rezagos
summary(u.ar2)
##
## Time series regression with "ts" data:
## Start = 1948(3), End = 2016(1)
##
## Call:
## dynlm(formula = u ~ L(u, 1:2))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.82533 -0.16751 -0.02107 0.15891 1.07456
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.28852 0.06661 4.332 2.09e-05 ***
## L(u, 1:2)1 1.61282 0.04570 35.295 < 2e-16 ***
## L(u, 1:2)2 -0.66209 0.04557 -14.528 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2947 on 268 degrees of freedom
## Multiple R-squared: 0.9678, Adjusted R-squared: 0.9675
## F-statistic: 4022 on 2 and 268 DF, p-value: < 2.2e-16
Para el pronóstico usamos AR(2) y forecast*.
#Modelos AR
ar2u<- ar (u, aic=FALSE, order.max = 2, method = "ols")
#Data frame del forcast
fcst <-data.frame(forecast(ar2u, 3))
fcst
La tasa de desempleo va a aumentar en los proximos 3 trimestres
GRAFICO
plot(forecast(ar2u, 3))
En el grafico observamos que aumentarĆ” el desempleo
Sesión 3
FORECAST Trabajamos con un modelo AR(3) para el desempleo
usmacro.ts.tab <- cbind(
usmacro.ts,
lag(usmacro.ts[, 4], -1),
diff(usmacro.ts[, 4], lag = 1),
lag(usmacro.ts[, 2], -1),
lag(usmacro.ts[, 2], -2),
lag(usmacro.ts[, 2], -3)
)
kable(
head(usmacro.ts.tab),
caption = "Diversos rezagos y diferencias",
col.names = c("id", "g", "inf", "u", "uL1", "du", "gL1", "gL2", "gL3"),
digits = 3
)
| id | g | inf | u | uL1 | du | gL1 | gL2 | gL3 |
|---|---|---|---|---|---|---|---|---|
| -694310400 | 2.267 | 2.119 | 3.7 | NA | NA | NA | NA | NA |
| -686448000 | 2.517 | 1.592 | 3.7 | 3.7 | 0.0 | 2.267 | NA | NA |
| -678585600 | 2.418 | 1.684 | 3.8 | 3.7 | 0.1 | 2.517 | 2.267 | NA |
| -670636800 | 0.429 | -0.918 | 3.8 | 3.8 | 0.0 | 2.418 | 2.517 | 2.267 |
| -662688000 | -1.888 | -0.951 | 4.7 | 3.8 | 0.9 | 0.429 | 2.418 | 2.517 |
| -654912000 | -1.344 | -0.109 | 5.9 | 4.7 | 1.2 | -1.888 | 0.429 | 2.418 |
El modelo AR(3) para el desempleo:
# Definir la variable que proviene de la serie de tiempo
u <- usmacro.ts[, "u"]
u.ar3 <- dynlm(u ~ L(u, 1:3))#Rezagos del u del 1-2, si son mas rezagos
summary(u.ar3)
##
## Time series regression with "ts" data:
## Start = 1948(4), End = 2016(1)
##
## Call:
## dynlm(formula = u ~ L(u, 1:3))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.78816 -0.16591 -0.02177 0.15577 1.04851
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.26075 0.06886 3.787 0.000189 ***
## L(u, 1:3)1 1.68035 0.06100 27.549 < 2e-16 ***
## L(u, 1:3)2 -0.82636 0.10845 -7.620 4.48e-13 ***
## L(u, 1:3)3 0.10155 0.06086 1.669 0.096361 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2943 on 266 degrees of freedom
## Multiple R-squared: 0.9679, Adjusted R-squared: 0.9675
## F-statistic: 2674 on 3 and 266 DF, p-value: < 2.2e-16
#Modelos AR
ar3u<- ar (u, aic=FALSE, order.max = 3, method = "ols")
#Data frame del forcast
fcst <-data.frame(forecast(ar3u, 3))
fcst
plot(forecast(ar3u, 3))
#Desempleo para la variable u
acf(u)
plot(u)
#Diferenciamos la variable (Du=u_t-u{t-1}), tiempo t menos t-1
Du<-diff(u)
plot(Du)
acf (Du) #Diferenciamos la variable para hacerla estacionaria-media constante
Hacemos un AR(2)
mdu2<-dynlm(formula = Du ~ L(Du, 1:2))
summary(mdu2)
##
## Time series regression with "ts" data:
## Start = 1948(4), End = 2016(1)
##
## Call:
## dynlm(formula = Du ~ L(Du, 1:2))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.81499 -0.16826 -0.01757 0.15664 1.02021
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.001395 0.018381 0.076 0.93957
## L(Du, 1:2)1 0.743088 0.060383 12.306 < 2e-16 ***
## L(Du, 1:2)2 -0.161742 0.060413 -2.677 0.00788 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.302 on 267 degrees of freedom
## Multiple R-squared: 0.4247, Adjusted R-squared: 0.4203
## F-statistic: 98.53 on 2 and 267 DF, p-value: < 2.2e-16
mdu3<-dynlm(formula = Du ~ L(Du, 1:3))
summary(mdu3)
##
## Time series regression with "ts" data:
## Start = 1949(1), End = 2016(1)
##
## Call:
## dynlm(formula = Du ~ L(Du, 1:3))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.8037 -0.1660 -0.0220 0.1561 1.0144
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.002079 0.018305 0.114 0.9097
## L(Du, 1:3)1 0.720964 0.060828 11.852 <2e-16 ***
## L(Du, 1:3)2 -0.058933 0.075141 -0.784 0.4336
## L(Du, 1:3)3 -0.138792 0.060854 -2.281 0.0234 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.3002 on 265 degrees of freedom
## Multiple R-squared: 0.4359, Adjusted R-squared: 0.4295
## F-statistic: 68.25 on 3 and 265 DF, p-value: < 2.2e-16
mdu4<-dynlm(formula = Du ~ L(Du, 1:4))
summary(mdu4)
##
## Time series regression with "ts" data:
## Start = 1949(2), End = 2016(1)
##
## Call:
## dynlm(formula = Du ~ L(Du, 1:4))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.80297 -0.16288 -0.02309 0.15286 1.05784
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.0009298 0.0179855 -0.052 0.9588
## L(Du, 1:4)1 0.7091222 0.0602425 11.771 <2e-16 ***
## L(Du, 1:4)2 -0.0708132 0.0737969 -0.960 0.3382
## L(Du, 1:4)3 -0.0598325 0.0737745 -0.811 0.4181
## L(Du, 1:4)4 -0.1057447 0.0602697 -1.755 0.0805 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2944 on 263 degrees of freedom
## Multiple R-squared: 0.4511, Adjusted R-squared: 0.4428
## F-statistic: 54.03 on 4 and 263 DF, p-value: < 2.2e-16
mdu5<-dynlm(formula = Du ~ L(Du, 1:5))
summary(mdu5)
##
## Time series regression with "ts" data:
## Start = 1949(3), End = 2016(1)
##
## Call:
## dynlm(formula = Du ~ L(Du, 1:5))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.77117 -0.15409 -0.01796 0.15343 1.08085
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.003301 0.017916 -0.184 0.8540
## L(Du, 1:5)1 0.693718 0.061308 11.315 <2e-16 ***
## L(Du, 1:5)2 -0.049028 0.074019 -0.662 0.5083
## L(Du, 1:5)3 -0.060644 0.073501 -0.825 0.4101
## L(Du, 1:5)4 -0.151959 0.073450 -2.069 0.0395 *
## L(Du, 1:5)5 0.063850 0.060273 1.059 0.2904
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2927 on 261 degrees of freedom
## Multiple R-squared: 0.4421, Adjusted R-squared: 0.4314
## F-statistic: 41.37 on 5 and 261 DF, p-value: < 2.2e-16
Probamos lo AR y ver cual es el mejor modelo posible para aplicarle forcast Vemos en la grafica cuantos rezagos tenemos que probar No es estable si cambian los p- values cuando metes mas,las significancias no deben cambiar , no es estable aunque la diferenciamos
1.Verificar estacionariedad 2.acf 3.Pruebo con los AR, debe ser estable y progresivo
Probamos con la tasa GDP
g<- usmacro.ts[,"g"]
plot(g)
acf(g)
AR(3)
#Modelos AR
ar3g <- dynlm(g~L(g,1:3),data=usmacro.ts)
summary(ar3g)
##
## Time series regression with "ts" data:
## Start = 1948(4), End = 2016(1)
##
## Call:
## dynlm(formula = g ~ L(g, 1:3), data = usmacro.ts)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.0061 -0.5432 -0.0652 0.4557 4.1511
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.72638 0.11820 6.145 2.91e-09 ***
## L(g, 1:3)1 0.44804 0.06094 7.353 2.41e-12 ***
## L(g, 1:3)2 0.20304 0.06566 3.092 0.0022 **
## L(g, 1:3)3 -0.11775 0.06095 -1.932 0.0544 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.958 on 266 degrees of freedom
## Multiple R-squared: 0.284, Adjusted R-squared: 0.276
## F-statistic: 35.17 on 3 and 266 DF, p-value: < 2.2e-16
AR(4)
#Modelos AR
ar4g <- dynlm(g~L(g,1:4),data=usmacro.ts)
summary(ar4g)
##
## Time series regression with "ts" data:
## Start = 1949(1), End = 2016(1)
##
## Call:
## dynlm(formula = g ~ L(g, 1:4), data = usmacro.ts)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.0079 -0.5307 -0.0694 0.4528 4.1396
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.72164 0.12647 5.706 3.10e-08 ***
## L(g, 1:4)1 0.45046 0.06127 7.353 2.44e-12 ***
## L(g, 1:4)2 0.20559 0.06674 3.080 0.00229 **
## L(g, 1:4)3 -0.11744 0.06673 -1.760 0.07958 .
## L(g, 1:4)4 0.00155 0.06128 0.025 0.97984
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.9564 on 264 degrees of freedom
## Multiple R-squared: 0.2891, Adjusted R-squared: 0.2784
## F-statistic: 26.84 on 4 and 264 DF, p-value: < 2.2e-16
inf<- usmacro.ts[,"inf"]
plot(inf)
acf(inf)
En estas graficas podemos notar que la variable no es estacionaria, hay un problema en los rezagos,entonces la diferenciamos.
#Diferenciamos la variable (Du=u_t-u{t-1}), tiempo t menos t-1
DU<-diff(inf)
plot(DU)
acf (DU)#Diferenciamos la variable para hacerla estacionaria-media constante
Pruebo con los AR, debe ser estable y progresivo, con la anterior
grafica vemos mas o menos cuantos ar vamos a hacer por las lineas que
salen del intervalo (4)
#Modelos AR
ar3g <- dynlm(inf~L(inf,1:2),data=usmacro.ts)
summary(ar3g)
##
## Time series regression with "ts" data:
## Start = 1948(3), End = 2016(1)
##
## Call:
## dynlm(formula = inf ~ L(inf, 1:2), data = usmacro.ts)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.6318 -0.2804 0.0201 0.2750 2.3477
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.19456 0.05111 3.806 0.000175 ***
## L(inf, 1:2)1 0.62890 0.06057 10.383 < 2e-16 ***
## L(inf, 1:2)2 0.13645 0.06038 2.260 0.024633 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.5534 on 268 degrees of freedom
## Multiple R-squared: 0.5386, Adjusted R-squared: 0.5351
## F-statistic: 156.4 on 2 and 268 DF, p-value: < 2.2e-16
mdU2<-dynlm(formula = DU ~ L(DU, 1:2))
summary(mdU2)
##
## Time series regression with "ts" data:
## Start = 1948(4), End = 2016(1)
##
## Call:
## dynlm(formula = DU ~ L(DU, 1:2))
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.6771 -0.2188 0.0375 0.2739 2.4356
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.01021 0.03380 -0.302 0.763
## L(DU, 1:2)1 -0.32872 0.05840 -5.629 4.58e-08 ***
## L(DU, 1:2)2 -0.30029 0.05832 -5.149 5.09e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.5554 on 267 degrees of freedom
## Multiple R-squared: 0.1483, Adjusted R-squared: 0.1419
## F-statistic: 23.25 on 2 and 267 DF, p-value: 4.917e-10
mdU3<-dynlm(formula = DU ~ L(DU, 1:3))
summary(mdU3)
##
## Time series regression with "ts" data:
## Start = 1949(1), End = 2016(1)
##
## Call:
## dynlm(formula = DU ~ L(DU, 1:3))
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.6844 -0.2174 0.0363 0.2525 2.3263
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.0005117 0.0323179 0.016 0.987
## L(DU, 1:3)1 -0.3063943 0.0584473 -5.242 3.24e-07 ***
## L(DU, 1:3)2 -0.2896966 0.0589712 -4.913 1.58e-06 ***
## L(DU, 1:3)3 0.0773632 0.0583990 1.325 0.186
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.5299 on 265 degrees of freedom
## Multiple R-squared: 0.1726, Adjusted R-squared: 0.1632
## F-statistic: 18.42 on 3 and 265 DF, p-value: 6.932e-11
mdU4<-dynlm(formula = DU ~ L(DU, 1:4))
summary(mdU4)
##
## Time series regression with "ts" data:
## Start = 1949(2), End = 2016(1)
##
## Call:
## dynlm(formula = DU ~ L(DU, 1:4))
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.5763 -0.2127 0.0371 0.2419 2.4454
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.001055 0.031833 0.033 0.97358
## L(DU, 1:4)1 -0.318497 0.060488 -5.265 2.91e-07 ***
## L(DU, 1:4)2 -0.345788 0.060360 -5.729 2.76e-08 ***
## L(DU, 1:4)3 0.012818 0.060552 0.212 0.83251
## L(DU, 1:4)4 -0.173074 0.058149 -2.976 0.00319 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.5209 on 263 degrees of freedom
## Multiple R-squared: 0.2065, Adjusted R-squared: 0.1944
## F-statistic: 17.11 on 4 and 263 DF, p-value: 1.751e-12
A pesar de diferenciar la variable inflación sigue sin ser estable, por lo que no podemos aplicar forecast.
Con esta base de datos vimos ejemplos de variables con las que podemos trabajar y con cuales no, fijandonos en su comportamiento mediante parametros y graficos, teniendo en claro que propiedades tienen que seguir.
This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.