library(tidyverse)
library(forecast)
library(quantmod)
library(AER)
library(MASS)
library(tseries)
library(stats)
BD <- read.csv("Sesión 03 Actividad.csv")
head(BD)
## Serie1 Serie2 Serie3 Serie4
## 1 0.09210486 14.79616 19.8418913 -1.1894537
## 2 0.18858804 17.34335 0.2143768 -0.8008725
## 3 0.38236941 23.13333 8.0262283 -1.1452059
## 4 -0.63867933 30.73897 22.3957743 -1.6931020
## 5 0.27310060 32.48042 18.6783424 -0.7124398
## 6 -1.12289720 32.49183 20.8187109 -0.9490858
str(BD)
## 'data.frame': 240 obs. of 4 variables:
## $ Serie1: num 0.0921 0.1886 0.3824 -0.6387 0.2731 ...
## $ Serie2: num 14.8 17.3 23.1 30.7 32.5 ...
## $ Serie3: num 19.842 0.214 8.026 22.396 18.678 ...
## $ Serie4: num -1.189 -0.801 -1.145 -1.693 -0.712 ...
summary(BD)
## Serie1 Serie2 Serie3 Serie4
## Min. :-3.41889 Min. :-95.028 Min. :-196.06 Min. :-1.693
## 1st Qu.:-0.67303 1st Qu.:-26.964 1st Qu.:-139.11 1st Qu.: 4.342
## Median :-0.02871 Median : 6.081 Median : -88.35 Median : 8.101
## Mean :-0.08270 Mean : -3.056 Mean : -88.57 Mean : 9.386
## 3rd Qu.: 0.57456 3rd Qu.: 25.515 3rd Qu.: -33.56 3rd Qu.:15.578
## Max. : 2.80006 Max. : 57.235 Max. : 22.40 Max. :22.825
serie1 <- ts(BD$Serie1)
# Gráfica de la serie
plot(serie1,
main = "Serie 1",
ylab = "Valor",
xlab = "Tiempo",
col = "red",
lwd = 1.5)
# Función de Autocorrelación (ACF)
acf(serie1,
main = "ACF - Serie 1",
lag.max = 40,
col = "red",)
# Función de Autocorrelación Parcial (PACF)
pacf(serie1,
main = "PACF - Serie 1",
lag.max = 40,
col = "red")
serie2 <- ts(BD$Serie2)
# Gráfica de la serie
plot(serie2,
main = "Serie 2",
ylab = "Valor",
xlab = "Tiempo",
col = "darkred",
lwd = 1.5)
# Función de Autocorrelación (ACF)
acf(serie2,
main = "ACF - Serie 2",
lag.max = 40,
col = "darkred")
# Función de Autocorrelación Parcial (PACF)
pacf(serie2,
main = "PACF - Serie 2",
lag.max = 40,
col = "darkred")
# Prueba Aumentada de Dickey-Fuller - Serie 2
cat("=== Prueba Aumentada de Dickey-Fuller - Serie 2 ===\n")
## === Prueba Aumentada de Dickey-Fuller - Serie 2 ===
adf_serie2 <- adf.test(serie2)
print(adf_serie2)
##
## Augmented Dickey-Fuller Test
##
## data: serie2
## Dickey-Fuller = -2.3615, Lag order = 6, p-value = 0.4238
## alternative hypothesis: stationary
cat("\n--- Comprobación de conclusiones Serie 2 ---\n")
##
## --- Comprobación de conclusiones Serie 2 ---
if (adf_serie2$p.value < 0.05) {
cat("p-valor =", round(adf_serie2$p.value, 4), "< 0.05\n")
cat("Se RECHAZA H0 (raíz unitaria).\n")
cat("Conclusión: La Serie 2 es un proceso ESTACIONARIO.\n")
cat("Esto CONFIRMA lo observado en la ACF.\n")
} else {
cat("p-valor =", round(adf_serie2$p.value, 4), "> 0.05\n")
cat("NO se rechaza H0 (raíz unitaria).\n")
cat("Conclusión: La Serie 2 es un proceso NO ESTACIONARIO.\n")
cat("Esto CONFIRMA lo observado en la ACF.\n")
}
## p-valor = 0.4238 > 0.05
## NO se rechaza H0 (raíz unitaria).
## Conclusión: La Serie 2 es un proceso NO ESTACIONARIO.
## Esto CONFIRMA lo observado en la ACF.
Las posibilidades son:
serie3 <- ts(BD$Serie3)
t3 <- 1:length(serie3)
# Gráfica del comportamiento a través del tiempo
plot(serie3,
main = "Serie 3 - Comportamiento en el Tiempo",
ylab = "Valor",
xlab = "Tiempo",
col = "darkgreen",
lwd = 1.5)
# Línea de tendencia para apoyo visual
abline(lm(serie3 ~ t3), col = "red", lwd = 2, lty = 2)
legend("topleft",
legend = c("Serie 3", "Tendencia lineal"),
col = c("darkgreen", "red"),
lty = c(1, 2), lwd = 2)
#La Serie 3 corresponde a un proceso no estacionario con tendencia determinística negativa. No se comporta como ruido alrededor de una media fija, sino que sigue un patrón descendente claro a lo largo del tiempo.
serie4 <- ts(BD$Serie4)
t4 <- 1:length(serie4)
# Gráfica del comportamiento a través del tiempo
plot(serie4,
main = "Serie 4 - Comportamiento en el Tiempo",
ylab = "Valor",
xlab = "Tiempo",
col = "yellow",
lwd = 1.5)
# Línea de tendencia para apoyo visual
abline(lm(serie4 ~ t4), col = "red", lwd = 2, lty = 2)
legend("topleft",
legend = c("Serie 4", "Tendencia lineal"),
col = c("yellow", "red"),
lty = c(1, 2), lwd = 2)
# Estimación de β0 y β1 mediante regresión lineal (si existe tendencia determinística)
modelo_serie4 <- lm(serie4 ~ t4)
cat("=== Regresión Lineal - Serie 4 ===\n")
## === Regresión Lineal - Serie 4 ===
print(summary(modelo_serie4))
##
## Call:
## lm(formula = serie4 ~ t4)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.0049 -3.1084 -0.1155 2.4084 11.8921
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.778823 0.602123 2.954 0.00345 **
## t4 0.063131 0.004332 14.574 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.649 on 238 degrees of freedom
## Multiple R-squared: 0.4716, Adjusted R-squared: 0.4693
## F-statistic: 212.4 on 1 and 238 DF, p-value: < 2.2e-16
cat("\n--- Coeficientes estimados ---\n")
##
## --- Coeficientes estimados ---
cat("β0 (Intercepto) =", round(coef(modelo_serie4)[1], 4), "\n")
## β0 (Intercepto) = 1.7788
cat("β1 (Pendiente) =", round(coef(modelo_serie4)[2], 4), "\n")
## β1 (Pendiente) = 0.0631