homi <- read.csv ("C:/Users/jony2/OneDrive/Documentos/maestria/MATERIAS/regresion/TSLA.csv")
homi.ts <- ts(homi[,2], frequency=12, start=c(2021,2), end=c(2023,2))

#GRAFICA
autoplot(homi.ts) +
  geom_line(color='black', lwd=1) +
  labs(title='PRECIO ACCION TESLA', 
       x='FEBRERO 2021-FEBRERO 2023',
       subtitle='', 
       caption='Fuente: Elaborado a partir de datos obtenidos de INVESTING.')

#DESCOMPOCISION
autoplot(homi.ts.des <- decompose(homi.ts)) +
  geom_line(color='black', lwd=1) +
  labs(title='PRECIO ACCION TESLA', 
       subtitle='Descomposicion clasica', 
       caption = 'Fuente: Elaborado a partir de datos obtenidos de INVESTING.',
       x='', y='')
## Warning: Removed 6 row(s) containing missing values (geom_path).

#FAC y FACP, PRUEBA DE ESTACIONARIEDAD EN TENDENCIA Y GRAFICOS DE AUTOCORRELACION
ggAcf(homi.ts) +
  geom_line() +
  labs(title='PRECIO ACCION TESLA', 
       subtitle='Funcion de autocorrelacion', 
       caption = 'Fuente: Elaborado a partir de datos obtenidos de INVESTING.',
       x='Retardo (k)', y='FAC')

ggPacf(homi.ts) +
  geom_line() +
  labs(title='PRECIO ACCION TESLA', 
       subtitle='Funcion de autocorrelacion Parcial', 
       caption = 'Fuente: Elaborado a partir de datos obtenidos de INVESTING',
       x='Retardo (k)', y='FACP')

# ESTADISTICOS DE TENDENCIA Y PROCESOS DE DIFERENCIAS
homi.ts.dif <- diff(homi.ts)
fac.homi.dif1 <- ggAcf(homi.ts.dif)
facp.homi.dif1 <- ggPacf(homi.ts.dif)
ggarrange(fac.homi.dif1, facp.homi.dif1, ncol=2)

adf.test(homi.ts.dif)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  homi.ts.dif
## Dickey-Fuller = -4.1883, Lag order = 2, p-value = 0.01669
## alternative hypothesis: stationary
kpss.test(homi.ts.dif)
## Warning in kpss.test(homi.ts.dif): p-value greater than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  homi.ts.dif
## KPSS Level = 0.14718, Truncation lag parameter = 2, p-value = 0.1
ts_plot(homi.ts.dif, line.mode = 'lines+markers', color='black', 
        Xtitle = 'PERIODO ANUAL', Ytitle = 'PRECIO ACCION TESLA', 
        title = 'FEBRERO 2021-FEBRERO 2023')
# ESTACIONALIDAD
ggseasonplot(homi.ts)

ggsubseriesplot(homi.ts) +
  ylab("PRECIO ACCION TESLA") +
  ggtitle("FEBRERO 2021-FEBRERO 2023")

# SUPOCISION DE EC ARIMA
auto.arima(homi.ts)
## Series: homi.ts 
## ARIMA(1,0,0) with non-zero mean 
## 
## Coefficients:
##          ar1      mean
##       0.7475  248.6672
## s.e.  0.1249   29.2856
## 
## sigma^2 = 1789:  log likelihood = -128.45
## AIC=262.91   AICc=264.05   BIC=266.57
ec1 <- Arima(homi.ts, order=c(1,0,0))
summary(ec1)
## Series: homi.ts 
## ARIMA(1,0,0) with non-zero mean 
## 
## Coefficients:
##          ar1      mean
##       0.7475  248.6672
## s.e.  0.1249   29.2856
## 
## sigma^2 = 1789:  log likelihood = -128.45
## AIC=262.91   AICc=264.05   BIC=266.57
## 
## Training set error measures:
##                    ME    RMSE      MAE       MPE     MAPE      MASE      ACF1
## Training set 1.017906 40.5644 28.47421 -2.488279 11.80674 0.3087124 0.1188868
checkresiduals(ec1)

## 
##  Ljung-Box test
## 
## data:  Residuals from ARIMA(1,0,0) with non-zero mean
## Q* = 2.5805, df = 4, p-value = 0.6303
## 
## Model df: 1.   Total lags used: 5
#VALIDACION EC ARIMA ELEGIDA
fit3 <- Arima(homi.ts, order=c(1,0,0))
checkresiduals(fit3)

## 
##  Ljung-Box test
## 
## data:  Residuals from ARIMA(1,0,0) with non-zero mean
## Q* = 2.5805, df = 4, p-value = 0.6303
## 
## Model df: 1.   Total lags used: 5
summary(fit3)
## Series: homi.ts 
## ARIMA(1,0,0) with non-zero mean 
## 
## Coefficients:
##          ar1      mean
##       0.7475  248.6672
## s.e.  0.1249   29.2856
## 
## sigma^2 = 1789:  log likelihood = -128.45
## AIC=262.91   AICc=264.05   BIC=266.57
## 
## Training set error measures:
##                    ME    RMSE      MAE       MPE     MAPE      MASE      ACF1
## Training set 1.017906 40.5644 28.47421 -2.488279 11.80674 0.3087124 0.1188868
homi.ts.res <- fit3$residuals

autoplot(homi.ts.res) + xlab("AÑO") + ylab("") +
  ggtitle("RESIDUALES")

gghistogram(homi.ts.res) + ggtitle("Histograma de los residuales")
## Warning: Using `bins = 30` by default. Pick better value with the argument
## `bins`.
## Don't know how to automatically pick scale for object of type ts. Defaulting to continuous.

ggAcf(homi.ts.res) + ggtitle("ACF of residuals")