if(!require(tidyverse)) install.packages(tidyverse)
## Loading required package: tidyverse
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.0.4     
## ── 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
library(tidyverse)
library(forecast)
library(quantmod)
library(AER)
library(MASS)
library(tseries)
library(stats)
library(car)
library(lmtest)
library(urca)
# 1. Caminatas Aleatorias
set.seed(180188)
n <- 240
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]
}

data <- data.frame(X, Y)
# 2. Regresión Lineal
modelo <- lm(Y ~ X, data=data)
summary(modelo)
## 
## Call:
## lm(formula = Y ~ X, data = data)
## 
## 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
# 3. Pruebas de Estacionariedad (ADF)
adf.test(X)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  X
## Dickey-Fuller = -3.2472, Lag order = 6, p-value = 0.08071
## alternative hypothesis: stationary
adf.test(Y)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  Y
## Dickey-Fuller = -2.5759, Lag order = 6, p-value = 0.3337
## alternative hypothesis: stationary
# 4. Residuales y Cointegración
residuales <- residuals(modelo)
adf.test(residuales)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  residuales
## Dickey-Fuller = -2.4605, Lag order = 6, p-value = 0.3822
## alternative hypothesis: stationary
# 5. ACF
acf(X)

acf(Y)

acf(residuales)

# 6. Análisis de Datos Macroeconómicos
data("USMacroSWM", package = "AER")

# Filtrado de datos 1948-2004
TS <- ts(USMacroSWM[-(1:12), 1:2], start=1948, freq=12)
colnames(TS) <- c("production", "oil")
# 7. Regresión Producción ~ Petróleo
modelo2 <- lm(production ~ oil, data=as.data.frame(TS))
summary(modelo2)
## 
## Call:
## lm(formula = production ~ oil, data = as.data.frame(TS))
## 
## 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 ***
## oil          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
# 8. Pruebas de Estacionariedad
adf.test(TS[, "production"])
## 
##  Augmented Dickey-Fuller Test
## 
## data:  TS[, "production"]
## Dickey-Fuller = -1.9631, Lag order = 8, p-value = 0.5939
## alternative hypothesis: stationary
adf.test(TS[, "oil"])
## Warning in adf.test(TS[, "oil"]): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  TS[, "oil"]
## Dickey-Fuller = -6.7672, Lag order = 8, p-value = 0.01
## alternative hypothesis: stationary
# 9. Residuales y Cointegración
residuales2 <- residuals(modelo2)
adf.test(residuales2)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  residuales2
## Dickey-Fuller = -3.5316, Lag order = 8, p-value = 0.03927
## alternative hypothesis: stationary
acf(TS[, "production"])

acf(TS[, "oil"])

acf(residuales2)