Una serie de tiempo es una colección de observaciones sobre un determinado fenómeno, efactuadas en momento sucesivos, usualmente equiespaciados.
Algunos ejemplos de series de tiempo son:
1. Precios de acciones
2. Niveles de inventario
3. Rotación de personal
4. Ventas 5. PIB (GDP)
Más información:
R for Data Science
(2e)
# install.packages("forecast")
library(forecast)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
Ejemplo:
Los siguientes datos de producción trimestral inician en el primer
trimestre del 2020.
Se busca pronosticar la producción de los sigientes cinco trimestres
produccion <- c(50, 53, 55, 57, 55, 60)
ts <- ts(data = produccion, start = c(2020,1), frequency = 4)
ts
## Qtr1 Qtr2 Qtr3 Qtr4
## 2020 50 53 55 57
## 2021 55 60
ARIMA sigifica AutoRegressive Integrated Moving Average o Modelo Autoregresivo Integrado de Promedio Movil.
arima <- auto.arima(ts, D = 1)
arima
## Series: ts
## ARIMA(0,0,0)(0,1,0)[4] with drift
##
## Coefficients:
## drift
## 1.5000
## s.e. 0.1768
##
## sigma^2 = 2.01: log likelihood = -2.84
## AIC=9.68 AICc=-2.32 BIC=7.06
summary(arima)
## Series: ts
## ARIMA(0,0,0)(0,1,0)[4] with drift
##
## Coefficients:
## drift
## 1.5000
## s.e. 0.1768
##
## sigma^2 = 2.01: log likelihood = -2.84
## AIC=9.68 AICc=-2.32 BIC=7.06
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.03333332 0.5787923 0.3666667 0.03685269 0.6429133 0.06111111
## ACF1
## Training set -0.5073047
pronostico <- forecast(arima, level = c(95), h = 5)
pronostico
## Point Forecast Lo 95 Hi 95
## 2021 Q3 61 58.22127 63.77873
## 2021 Q4 63 60.22127 65.77873
## 2022 Q1 61 58.22127 63.77873
## 2022 Q2 66 63.22127 68.77873
## 2022 Q3 67 63.07028 70.92972
plot(pronostico)

##Crear serie de tiempo
ventas <- read.csv("/Users/liz/Downloads/OCTAVO SEMESTRE/ESCENARIOS FUTUROS/MÓDULO 1 - PROF RAÚL/ventas_hersheys.csv")
ts_hersheys <- ts(data = ventas$Ventas, start = c(2017,1), frequency = 12)
ts_hersheys
## Jan Feb Mar Apr May Jun Jul Aug
## 2017 25520.51 23740.11 26253.58 25868.43 27072.87 27150.50 27067.10 28145.25
## 2018 28463.69 26996.11 29768.20 29292.51 29950.68 30099.17 30851.26 32271.76
## 2019 32496.44 31287.28 33376.02 32949.77 34004.11 33757.89 32927.30 34324.12
## Sep Oct Nov Dec
## 2017 27546.29 28400.37 27441.98 27852.47
## 2018 31940.74 32995.93 32197.12 31984.82
## 2019 35151.28 36133.07 34799.91 34846.17
arima_hersheys <- auto.arima(ts_hersheys)
arima_hersheys
## Series: ts_hersheys
## ARIMA(1,0,0)(1,1,0)[12] with drift
##
## Coefficients:
## ar1 sar1 drift
## 0.6383 -0.5517 288.8980
## s.e. 0.1551 0.2047 14.5025
##
## sigma^2 = 202700: log likelihood = -181.5
## AIC=371 AICc=373.11 BIC=375.72
summary(arima_hersheys)
## Series: ts_hersheys
## ARIMA(1,0,0)(1,1,0)[12] with drift
##
## Coefficients:
## ar1 sar1 drift
## 0.6383 -0.5517 288.8980
## s.e. 0.1551 0.2047 14.5025
##
## sigma^2 = 202700: log likelihood = -181.5
## AIC=371 AICc=373.11 BIC=375.72
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 25.22163 343.863 227.1699 0.08059942 0.7069541 0.06491041
## ACF1
## Training set 0.2081043
pronostico_hersheys <- forecast(arima_hersheys, level = c(95), h = 12)
pronostico_hersheys
## Point Forecast Lo 95 Hi 95
## Jan 2020 35498.90 34616.48 36381.32
## Feb 2020 34202.17 33155.29 35249.05
## Mar 2020 36703.01 35596.10 37809.92
## Apr 2020 36271.90 35141.44 37402.36
## May 2020 37121.98 35982.07 38261.90
## Jun 2020 37102.65 35958.91 38246.40
## Jul 2020 37151.04 36005.74 38296.35
## Aug 2020 38564.65 37418.71 39710.59
## Sep 2020 38755.23 37609.03 39901.42
## Oct 2020 39779.03 38632.73 40925.33
## Nov 2020 38741.63 37595.29 39887.97
## Dec 2020 38645.86 37499.50 39792.22
plot(pronostico_hersheys)
#install.packages("finreportr")
library(finreportr)
Con la función finreportr podemos obtener la siguiente
información:
- CompanyInfo() = Brinda información general como Nombre, Ubicación,
ZIP, etc.
- AnnualReports() = Brinda le nombre, fecha y número de acceso
- GetIncome() = Brinda el Estado de Resultados
- GetBalanceSheet() = Brinda el Balance General
- GetCashFlow() = Brinda el Flujo de Efectivo
options(HTTPUserAgent = "a a@gmail.com")
CompanyInfo("JPM")
## company CIK SIC state state.inc FY.end street.address
## 1 JPMORGAN CHASE & CO 0000019617 6021 NY DE 1231 383 MADISON AVENUE
## city.state
## 1 NEW YORK NY 10017
AnnualReports("BABA", foreign = TRUE)
## filing.name filing.date accession.no
## 1 20-F/A 2024-02-23 0001193125-24-044480
## 2 20-F 2023-07-21 0000950170-23-033752
## 3 20-F 2022-07-26 0001104659-22-082622
## 4 20-F 2021-07-27 0001104659-21-096092
## 5 20-F 2020-07-09 0001104659-20-082409
## 6 20-F 2019-06-05 0001047469-19-003492
## 7 20-F 2018-07-27 0001047469-18-005257
## 8 20-F 2017-06-15 0001047469-17-004019
## 9 20-F 2016-05-24 0001047469-16-013400
## 10 20-F 2015-06-25 0001047469-15-005768
google_income <- GetIncome("GOOG", 2016)
amazon_balance <- GetBalanceSheet("AMZN", 2015)
apple_cash <- GetCashFlow("AAPL", 2014)