Pronostico de número de turista en México.

Hoy en día, el turismo es una actividad primordial a nivel internacional, se ha convertido en un objeto de estudio que engloba a un gran número de elementos que permanecen en constante interacción y a partir de ellos se generan una serie de actividades para el abastecimiento de bienes y servicios.

Los siguientes datos proviene de INEGI https://www.inegi.org.mx/temas/turismo

Traer los datos.

datos <- read.csv("C:/Users/Erika/Documents/Turismo.csv")

Instalar el paquete TSstudio y llamar la libreria

#install.packages("TSstudio")
library(TSstudio)

El parametro start, puede definir una fecha de inicio: Agosto de 2018, frequency: los 12 meses del año)

#la función ts convierte un objeto a serie de tiempo y recibe como parametro un objeto timeSeries
stdatos<-ts(datos,start=c(2018,8),frequency=12)

Serie de tiempo

Grafica de serie de tiempo de 2018-2022. En 2020 decrece el numero de turistas.

#Grafica de numero de turista de 2018 a 2022
ts_plot(stdatos,
        title = "Número de turistas en México 2018-2022",
        Ytitle = "Unidades en miles",
        Xtitle = "Turismo en México", 
        slider = TRUE)
#Descomposicion de la serie de tiempo
ts_decompose(stdatos)
#Componente estacional
ts_seasonal(stdatos, type = "all")
#Componente estacional sin tendencia
ts_seasonal(stdatos - decompose(stdatos)$trend, 
            type = "all", 
            title = "Numero de Turistas (sin tendencia)")
## Warning: Can't display both discrete & non-discrete data on same axis
## Warning: Ignoring 2 observations
## Warning: Ignoring 1 observations

## Warning: Ignoring 1 observations

## Warning: Ignoring 1 observations

## Warning: Ignoring 1 observations
## Warning: Ignoring 2 observations

## Warning: Ignoring 2 observations

## Warning: Ignoring 2 observations
## Warning: Can't display both discrete & non-discrete data on same axis
#Analisis de correlacion
ts_cor(stdatos)
stdatos_df <- ts_to_prophet(stdatos) 
head(stdatos_df)
##           ds       y
## 1 2018-08-01 3266838
## 2 2018-09-01 2663744
## 3 2018-10-01 3319123
## 4 2018-11-01 3810981
## 5 2018-12-01 4710423
## 6 2019-01-01 3924215

Para el pronostico de la serie.

#Para la ruptura de la serie, se utiliza (flag) binaria con valor
#de 0 para cualquier observacion antes del 2020 y 1 despues.
#Esto por que en 2020 decrece el numero de turistas
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
stdatos_df$flag <- ifelse(year(stdatos_df$ds) >= 2020, 1, 0)
h1 <- 12 
h2 <- 60 
stdatos_split <- ts_split(stdatos, sample.out = h1)
train <- stdatos_split$train
test <- stdatos_split$test
train_df <- stdatos_df[1:(nrow(stdatos_df) - h1), ]
test_df <- stdatos_df[(nrow(stdatos_df) - h1 + 1):nrow(stdatos_df), ]
#semilla
set.seed(1234)
#Llamar las dos library
library(forecast)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
#Modelo  arima
mdsarima <- auto.arima(train, 
                  stepwise = FALSE, 
                  approximation = FALSE,
                  D = 1)
fc1 <- forecast(mdsarima, h = h1)
accuracy(fc1, test)
##                      ME      RMSE     MAE       MPE     MAPE     MASE
## Training set  -27716.34  802975.5  457883 -31.56495 80.59759 0.237676
## Test set     2619513.83 2632820.1 2619514  94.92307 94.92307 1.359726
##                    ACF1 Theil's U
## Training set -0.1414191        NA
## Test set      0.2265285  4.891089

Modelo SARIMA

#modelo Sarima
test_forecast(forecast.obj = fc1, actual = stdatos, test = test) %>% 
  layout(legend = list(x = 0.6, y = 0.95))
#Evaluación de residuos del Modelo SARIMA
check_res(mdsarima)
## Warning: Ignoring 31 observations
## Warning: Ignoring 30 observations
## Warning: Ignoring 3 observations
#Pronósticos del modelo SARIMA
md_final <- auto.arima(stdatos, 
                       stepwise = FALSE, 
                       approximation = FALSE,
                       D = 1)
fc_final <- forecast(md_final, h = 12)
plot_forecast(fc_final) %>% 
  layout(legend = list(x = 0.6, y = 0.95), title="Pronostico de abril de 2022 a marzo de 2023")