install_and_load <- function(package) {
tryCatch(
{
library(package, character.only = TRUE)
},
error = function(err) {
install.packages(package)
library(package, character.only = TRUE)
}
)
}
library(readxl)
Dataframe <- read_excel("C:/Users/DELL/Downloads/CMO-Historical-Data-Monthly.xlsx",sheet = "Monthly Prices")
## New names:
## • `` -> `...2`
## • `` -> `...3`
## • `` -> `...4`
## • `` -> `...5`
## • `` -> `...6`
## • `` -> `...7`
## • `` -> `...8`
## • `` -> `...9`
## • `` -> `...10`
## • `` -> `...11`
## • `` -> `...12`
## • `` -> `...13`
## • `` -> `...14`
## • `` -> `...15`
## • `` -> `...16`
## • `` -> `...17`
## • `` -> `...18`
## • `` -> `...19`
## • `` -> `...20`
## • `` -> `...21`
## • `` -> `...22`
## • `` -> `...23`
## • `` -> `...24`
## • `` -> `...25`
## • `` -> `...26`
## • `` -> `...27`
## • `` -> `...28`
## • `` -> `...29`
## • `` -> `...30`
## • `` -> `...31`
## • `` -> `...32`
## • `` -> `...33`
## • `` -> `...34`
## • `` -> `...35`
## • `` -> `...36`
## • `` -> `...37`
## • `` -> `...38`
## • `` -> `...39`
## • `` -> `...40`
## • `` -> `...41`
## • `` -> `...42`
## • `` -> `...43`
## • `` -> `...44`
## • `` -> `...45`
## • `` -> `...46`
## • `` -> `...47`
## • `` -> `...48`
## • `` -> `...49`
## • `` -> `...50`
## • `` -> `...51`
## • `` -> `...52`
## • `` -> `...53`
## • `` -> `...54`
## • `` -> `...55`
## • `` -> `...56`
## • `` -> `...57`
## • `` -> `...58`
## • `` -> `...59`
## • `` -> `...60`
## • `` -> `...61`
## • `` -> `...62`
## • `` -> `...63`
## • `` -> `...64`
## • `` -> `...65`
## • `` -> `...66`
## • `` -> `...67`
## • `` -> `...68`
## • `` -> `...69`
## • `` -> `...70`
## • `` -> `...71`
## • `` -> `...72`
#Adaptar Dataframe
DataF2 <- Dataframe[486:nrow(Dataframe),70]
View(DataF2)
#Cambiar NOmbre a la Columna de la data
colnames(DataF2)[1] <- "Gold"
View(DataF2)
summary(DataF2$Gold)
## Length Class Mode
## 298 character character
DataF2$Gold <- as.numeric(DataF2$Gold)
summary(DataF2$Gold)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 260.5 564.3 1224.8 1125.6 1592.8 2690.1
DataF2 <-DataF2 %>%
mutate(fecha=seq(ymd('2000-01-01'),length.out=n(),by='1 month'),
year=year(fecha),
mes=month(fecha),
trim=quarter(fecha))
View(DataF2)
Gold <- ts(DataF2$Gold , frequency = 12, start = 2000)
summary(Gold)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 260.5 564.3 1224.8 1125.6 1592.8 2690.1
#Media
median(Gold)
## [1] 1224.785
#desviacion Estandar:
sd(Gold)
## [1] 579.4892
library(ggplot2)
plot(decompose(Gold))
# Tendencia
trend <- decompose(Gold)$trend
# Componente estacional
seasonal <- decompose(Gold)$seasonal
# Componente aleatorio
random <- decompose(Gold)$random
# Grafico serie de tiempo:
plot(Gold, main = "Serie de Tiempo", xlab = "Meses", ylab = "Gold", col="darkblue")
#Grafico de la Tendencia:
plot(decompose(Gold)$trend, main = "Componente de Tendencia", xlab = "Meses", ylab = "Tendencia", col="darkred")
# Grafico de Estacionalidad:
plot(decompose(Gold)$seasonal, main = "Componente de Estacionalidad", xlab = "Meses", ylab = "Estacionalidad", col="red")
# Grafico del componente residual:
plot(decompose(Gold)$random, main = "Componente Residual", xlab = "Meses", ylab = "Residual", col= "red")
#Por Mes
ggplot(DataF2, aes(x=as.factor(mes),y=Gold)) +
geom_boxplot() +
labs(x="Mes", y="Gold", title = "Grafico de Caja del Oro")
# Por Trimestres
ggplot(DataF2, aes(x=as.factor(trim),y=Gold)) +
geom_boxplot() +
labs(x="Trimestre", y="Gold", title = "Grafico de caja del Oro")
#library(forecast)
library(forecast)
seasonplot(Gold, col = "darkred")
library(stats)
par(mfrow = c(1, 2))
acf(Gold, frequency=1,main="",sub="Figura 1: Funcion de Autocorrelacion Simple")
## Warning in plot.window(...): "frequency" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "frequency" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "frequency" is not
## a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "frequency" is not
## a graphical parameter
## Warning in box(...): "frequency" is not a graphical parameter
## Warning in title(...): "frequency" is not a graphical parameter
pacf(Gold, frequency=1, main="", sub="Figura 2: Funcion de Autocorrelacion Parcial", col="red")
## Warning in plot.window(...): "frequency" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "frequency" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "frequency" is not
## a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "frequency" is not
## a graphical parameter
## Warning in box(...): "frequency" is not a graphical parameter
## Warning in title(...): "frequency" is not a graphical parameter
Se debe trabajar con una serie estacionaria. Se aplica el text de DFA.
adf.test(Gold)
##
## Augmented Dickey-Fuller Test
##
## data: Gold
## Dickey-Fuller = -1.3598, Lag order = 6, p-value = 0.8461
## alternative hypothesis: stationary
La diferencia a la serie:
adf.test(diff(Gold))
## Warning in adf.test(diff(Gold)): p-value smaller than printed p-value
##
## Augmented Dickey-Fuller Test
##
## data: diff(Gold)
## Dickey-Fuller = -5.3668, Lag order = 6, p-value = 0.01
## alternative hypothesis: stationary
Determinacion de los componentes AR y MA con el correlograma de la serie estacionaria
par(mfrow = c(1, 2))
acf(diff(Gold), frequency=1,main="",sub="Funcion de Autocorrelacion Simple")
## Warning in plot.window(...): "frequency" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "frequency" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "frequency" is not
## a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "frequency" is not
## a graphical parameter
## Warning in box(...): "frequency" is not a graphical parameter
## Warning in title(...): "frequency" is not a graphical parameter
pacf(diff(Gold), frequency=1, main="", sub="Funcion de Autocorrelacion Parcial")
## Warning in plot.window(...): "frequency" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "frequency" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "frequency" is not
## a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "frequency" is not
## a graphical parameter
## Warning in box(...): "frequency" is not a graphical parameter
## Warning in title(...): "frequency" is not a graphical parameter
CONFIRMACION DEL TIPO DE ARIMA
Modelo_Confir <- auto.arima(Gold)
summary(Modelo_Confir)
## Series: Gold
## ARIMA(0,1,1) with drift
##
## Coefficients:
## ma1 drift
## 0.2562 8.1906
## s.e. 0.0580 3.1059
##
## sigma^2 = 1830: log likelihood = -1536.03
## AIC=3078.06 AICc=3078.14 BIC=3089.14
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set -0.009060713 42.56712 30.98466 -0.3151722 2.845666 0.2151784
## ACF1
## Training set -0.003877504
Es un ARIMA(0,1,1) con deriva. Esto significa que fue necesario diferenciar la serie para que sea estacionaria. El término de media móvil de orden 1 (MA(1)) nos muestra que los valores actuales están influenciados por los errores del período anterior. Además, la deriva positiva indica que existe una tendencia creciente en los precios del oro a lo largo del tiempo.
ARIMA_GOLD <- arima(c(0,1,1), method = "ML")
ARIMA_GOLD
##
## Call:
## arima(x = c(0, 1, 1), method = "ML")
##
## Coefficients:
## intercept
## 0.6667
## s.e. 0.2722
##
## sigma^2 estimated as 0.2222: log likelihood = -2, aic = 8
ARIMA_GOLD <- Arima(Gold, order = c(0,1,1))
par(mfrow = c(1,1))
plot.ts(ARIMA_GOLD$residuals, main= "", sub= "Residuos Del Modelo ARIMA (0,1,1)", xlab= "Tiempo", ylab= "Residuos", col= "darkred")
par(mfrow = c(1,2))
acf(ARIMA_GOLD$residuals, main= "", sub="Autocorrelaciones de Los Residuos", xlab="Tiempo", ylab="Autocorrelacion")
pacf(ARIMA_GOLD$residuals, main="", sub="Autocorrelaciones Parciales de los Residuos", xlab="Tiempo", ylab="Autocorrelacion")
mtext("Análisis de los Residuos del Modelo ARIMA(0,1,1) del Oro",
outer = TRUE,
cex = 1.5,
line = -2)
shapiro.test(ARIMA_GOLD$residuals)
##
## Shapiro-Wilk normality test
##
## data: ARIMA_GOLD$residuals
## W = 0.97486, p-value = 4.359e-05
El resultado del test de Shapiro-Wilk muestra un valor p muy bajo, lo que indica que se rechazala hipótesis nula de normalidad de los residuos. Esto sugiere que los residuos del modelo ARIMA no siguen una distribución normal.
Box.test(ARIMA_GOLD$residuals, lag = 8, type = "Ljung-Box")
##
## Box-Ljung test
##
## data: ARIMA_GOLD$residuals
## X-squared = 9.4826, df = 8, p-value = 0.3032
El P-valor de 0.3032, sugiere que no hay autocorrelación significativa en los residuos, indicando que el modelo ARIMA (0,1,1) está bien ajustado y captura las dependencias temporales de manera adecuada.
Forec_Gold <- forecast(ARIMA_GOLD, h= 12)
Forec_Gold
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## Nov 2024 2716.263 2660.900 2771.627 2631.592 2800.935
## Dec 2024 2716.263 2626.641 2805.886 2579.197 2853.330
## Jan 2025 2716.263 2602.249 2830.278 2541.893 2890.634
## Feb 2025 2716.263 2582.224 2850.302 2511.268 2921.258
## Mar 2025 2716.263 2564.825 2867.702 2484.658 2947.868
## Apr 2025 2716.263 2549.228 2883.298 2460.805 2971.721
## May 2025 2716.263 2534.969 2897.558 2438.997 2993.530
## Jun 2025 2716.263 2521.751 2910.775 2418.783 3013.744
## Jul 2025 2716.263 2509.377 2923.150 2399.858 3032.669
## Aug 2025 2716.263 2497.702 2934.825 2382.003 3050.524
## Sep 2025 2716.263 2486.620 2945.907 2365.054 3067.473
## Oct 2025 2716.263 2476.048 2956.479 2348.886 3083.641
par(mfrow = c(1, 1))
plot(forecast(ARIMA_GOLD, h=12), main="", sub = "Valores pronosticados")
El Grafico muestra las proyecciones del valor del Oro en los ultimos 20 años, muestra que hay una gran volatilidad y a medida que pasa el tiempo, el mismo tiene una tendencia alcista.
Proyecciones
par(mfrow = c(1, 1))
autoplot(Forec_Gold) + ggtitle("Proyecciones")
El modelo ARIMA (0,1,1) aplicado a los precios del oro muestra una
proyección de aumento en los valores futuros, aunque los residuos
indican pequeñas desviaciones de la normalidad. Los test de
autocorrelación no revelan patrones significativos, lo que sugiere que
el modelo está bien ajustado.