if(!require(tidyverse)) install.packages(tidyverse)
## Loading required package: tidyverse
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.0 ✔ readr 2.1.6
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.2 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.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
if(!require(forecast)) install.packages(forecast)
## Loading required package: forecast
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
if(!require(quantmod)) install.packages(quantmod)
## Loading required package: quantmod
## Loading required package: xts
## Loading required package: zoo
##
## 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
##
## Loading required package: TTR
if(!require("AER")) install.packages("AER")
## Loading required package: AER
## Loading required package: 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
##
## Loading required package: lmtest
## Loading required package: sandwich
## Loading required package: survival
if(!require(MASS)) install.packages(MASS)
## Loading required package: MASS
##
## Attaching package: 'MASS'
##
## The following object is masked from 'package:dplyr':
##
## select
if(!require(tseries)) install.packages(tseries)
## Loading required package: tseries
if(!require(stats)) install.packages(stats)
if(!require(car)) install.packages(car)
if(!require(lmtest)) install.packages(lmtest)
if(!require(urca)) install.packages(urca)
## Loading required package: urca
if(!require("aTSA")) install.packages("aTSA")
## Loading required package: aTSA
##
## Attaching package: 'aTSA'
##
## The following objects are masked from 'package:tseries':
##
## adf.test, kpss.test, pp.test
##
## The following object is masked from 'package:forecast':
##
## forecast
##
## The following object is masked from 'package:graphics':
##
## identify
library(tidyverse)
library(forecast)
library(quantmod)
library("AER")
library(MASS)
library(tseries)
library(car)
library(lmtest)
library(urca)
library ("aTSA")
n <- 240
set.seed(180188)
u <- rnorm(n)
v <- rnorm(n)
X <- numeric(n)
Y <- numeric(n)
for (t in 2:n){
X[t] <- X[t-1] + u[t]
Y[t] <- Y[t-1] + v[t]
}
modelo <- lm(Y ~ X)
summary(modelo)
##
## Call:
## lm(formula = Y ~ X)
##
## Residuals:
## Min 1Q Median 3Q Max
## -9.0492 -3.3070 -0.0299 2.5518 14.4081
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.03357 0.28586 17.61 <2e-16 ***
## X 1.29574 0.08664 14.96 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.365 on 238 degrees of freedom
## Multiple R-squared: 0.4845, Adjusted R-squared: 0.4823
## F-statistic: 223.7 on 1 and 238 DF, p-value: < 2.2e-16
adf.test(X)
## Augmented Dickey-Fuller Test
## alternative: stationary
##
## Type 1: no drift no trend
## lag ADF p.value
## [1,] 0 -1.80 0.0734
## [2,] 1 -1.68 0.0903
## [3,] 2 -1.72 0.0849
## [4,] 3 -1.79 0.0748
## [5,] 4 -1.33 0.2020
## Type 2: with drift no trend
## lag ADF p.value
## [1,] 0 -1.74 0.431
## [2,] 1 -1.60 0.485
## [3,] 2 -1.62 0.476
## [4,] 3 -1.69 0.448
## [5,] 4 -1.23 0.616
## Type 3: with drift and trend
## lag ADF p.value
## [1,] 0 -3.47 0.0459
## [2,] 1 -3.18 0.0911
## [3,] 2 -3.15 0.0962
## [4,] 3 -3.32 0.0675
## [5,] 4 -2.98 0.1643
## ----
## Note: in fact, p.value = 0.01 means p.value <= 0.01
adf.test(Y)
## Augmented Dickey-Fuller Test
## alternative: stationary
##
## Type 1: no drift no trend
## lag ADF p.value
## [1,] 0 -0.608 0.461
## [2,] 1 -0.659 0.443
## [3,] 2 -0.526 0.491
## [4,] 3 -0.580 0.471
## [5,] 4 -0.630 0.454
## Type 2: with drift no trend
## lag ADF p.value
## [1,] 0 -1.20 0.628
## [2,] 1 -1.22 0.618
## [3,] 2 -1.06 0.674
## [4,] 3 -1.17 0.637
## [5,] 4 -1.29 0.595
## Type 3: with drift and trend
## lag ADF p.value
## [1,] 0 -2.18 0.498
## [2,] 1 -2.32 0.442
## [3,] 2 -2.23 0.478
## [4,] 3 -2.21 0.485
## [5,] 4 -2.17 0.504
## ----
## Note: in fact, p.value = 0.01 means p.value <= 0.01
acf(X)
acf(Y)
coint.test(Y, X)
## Response: Y
## Input: X
## Number of inputs: 1
## Model: y ~ X + 1
## -------------------------------
## Engle-Granger Cointegration Test
## alternative: cointegrated
##
## Type 1: no trend
## lag EG p.value
## 4.00 -2.57 0.10
## -----
## Type 2: linear trend
## lag EG p.value
## 4.000 0.296 0.100
## -----
## Type 3: quadratic trend
## lag EG p.value
## 4.000 0.228 0.100
## -----------
## Note: p.value = 0.01 means p.value <= 0.01
## : p.value = 0.10 means p.value >= 0.10
#La prueba de cointegración de Engle-Granger arroja valores p altos, por lo que no se rechaza la hipótesis nula de no cointegración. En consecuencia, no hay evidencia de un equilibrio de largo plazo entre las variables. Por lo tanto, se concluye que se trata de una regresión espuria y no de un fenómeno de cointegración.
data("USMacroSWM", package = "AER")
head(USMacroSWM)
## production oil cpi expenditure
## Jan 1947 17.04 NA 21.48 NA
## Feb 1947 17.14 NA 21.62 NA
## Mar 1947 17.24 NA 22.00 NA
## Apr 1947 17.10 NA 22.00 NA
## May 1947 17.17 NA 21.95 NA
## Jun 1947 17.17 NA 22.08 NA
TS = ts(USMacroSWM[-(1:12),1:2], start=1948, freq=12)
head(TS)
## production oil
## Jan 1948 17.87 0.06
## Feb 1948 17.91 0.00
## Mar 1948 17.71 0.00
## Apr 1948 17.74 0.00
## May 1948 18.04 0.00
## Jun 1948 18.28 0.00
plot(TS[,1], main="Producción")
plot(TS[,2], main="Petróleo")
adf.test(TS[,1])
## Augmented Dickey-Fuller Test
## alternative: stationary
##
## Type 1: no drift no trend
## lag ADF p.value
## [1,] 0 8.79 0.99
## [2,] 1 5.91 0.99
## [3,] 2 4.67 0.99
## [4,] 3 4.07 0.99
## [5,] 4 3.97 0.99
## [6,] 5 4.02 0.99
## [7,] 6 3.85 0.99
## Type 2: with drift no trend
## lag ADF p.value
## [1,] 0 2.53 0.99
## [2,] 1 1.69 0.99
## [3,] 2 1.25 0.99
## [4,] 3 1.06 0.99
## [5,] 4 1.08 0.99
## [6,] 5 1.14 0.99
## [7,] 6 1.07 0.99
## Type 3: with drift and trend
## lag ADF p.value
## [1,] 0 -0.752 0.966
## [2,] 1 -1.221 0.903
## [3,] 2 -1.545 0.770
## [4,] 3 -1.774 0.673
## [5,] 4 -1.833 0.648
## [6,] 5 -1.804 0.660
## [7,] 6 -1.849 0.641
## ----
## Note: in fact, p.value = 0.01 means p.value <= 0.01
adf.test(TS[,2])
## Augmented Dickey-Fuller Test
## alternative: stationary
##
## Type 1: no drift no trend
## lag ADF p.value
## [1,] 0 -15.97 0.01
## [2,] 1 -12.17 0.01
## [3,] 2 -10.97 0.01
## [4,] 3 -9.34 0.01
## [5,] 4 -7.67 0.01
## [6,] 5 -7.18 0.01
## [7,] 6 -6.45 0.01
## Type 2: with drift no trend
## lag ADF p.value
## [1,] 0 -16.83 0.01
## [2,] 1 -13.00 0.01
## [3,] 2 -11.89 0.01
## [4,] 3 -10.24 0.01
## [5,] 4 -8.52 0.01
## [6,] 5 -8.05 0.01
## [7,] 6 -7.30 0.01
## Type 3: with drift and trend
## lag ADF p.value
## [1,] 0 -17.22 0.01
## [2,] 1 -13.38 0.01
## [3,] 2 -12.30 0.01
## [4,] 3 -10.66 0.01
## [5,] 4 -8.92 0.01
## [6,] 5 -8.47 0.01
## [7,] 6 -7.72 0.01
## ----
## Note: in fact, p.value = 0.01 means p.value <= 0.01
modelo2 <- lm(TS[,1] ~ TS[,2])
summary(modelo2)
##
## Call:
## lm(formula = TS[, 1] ~ TS[, 2])
##
## Residuals:
## Min 1Q Median 3Q Max
## -51.018 -26.163 -2.519 19.464 60.811
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 57.099 1.127 50.68 < 2e-16 ***
## TS[, 2] 196.485 38.007 5.17 3.08e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 28.32 on 682 degrees of freedom
## Multiple R-squared: 0.03771, Adjusted R-squared: 0.0363
## F-statistic: 26.73 on 1 and 682 DF, p-value: 3.085e-07
coint.test(TS[,1], TS[,2])
## Response: TS[, 1]
## Input: TS[, 2]
## Number of inputs: 1
## Model: y ~ X + 1
## -------------------------------
## Engle-Granger Cointegration Test
## alternative: cointegrated
##
## Type 1: no trend
## lag EG p.value
## 6.000 -0.544 0.100
## -----
## Type 2: linear trend
## lag EG p.value
## 6.00 2.26 0.10
## -----
## Type 3: quadratic trend
## lag EG p.value
## 6.0 -1.2 0.1
## -----------
## Note: p.value = 0.01 means p.value <= 0.01
## : p.value = 0.10 means p.value >= 0.10
#Los valores p obtenidos son altos, por lo que no se rechaza la hipótesis nula de no cointegración. Esto implica que no existe evidencia de una relación de equilibrio de largo plazo entre ambas variables.