library(WDI)
library(wbstats)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.0 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.1 ✔ tibble 3.1.8
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
library(forecast)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(ggplot2)
gdp_data <- wb_data(country = "MX", indicator = "NY.GDP.PCAP.CD", start_date = 1973, end_date = 2022)
# Consultar country en
# Condultar indicator en
PIB <- gdp_data$NY.GDP.PCAP.CD
#Generar serie de tiempo
PIB_st <- ts(data = PIB, start = c(1973), frequency = 1)
PIB_st
## Time Series:
## Start = 1973
## End = 2021
## Frequency = 1
## [1] 1000.938 1264.358 1499.356 1472.659 1314.020 1598.466 2039.646
## [8] 3029.887 3812.581 2612.759 2166.461 2507.312 2607.380 1765.185
## [15] 1902.438 2302.027 2759.446 3196.919 3756.890 4272.786 5778.939
## [22] 5976.524 4002.174 4487.292 5370.218 5555.737 6230.696 7232.879
## [29] 7613.177 7650.899 7120.385 7525.465 8321.853 9125.423 9719.920
## [36] 10119.836 8104.908 9399.971 10341.521 10376.058 10865.680 11076.092
## [43] 9753.380 8875.062 9434.386 9857.029 10145.170 8655.001 10045.681
## attr(,"label")
## [1] GDP per capita (current US$)
#Generar pronóstico
modelo_pib <- auto.arima(PIB_st, D=1)
modelo_pib
## Series: PIB_st
## ARIMA(0,1,0) with drift
##
## Coefficients:
## drift
## 188.4321
## s.e. 112.5901
##
## sigma^2 = 621419: log likelihood = -387.76
## AIC=779.52 AICc=779.78 BIC=783.26
pronostico_pib <- forecast(modelo_pib, level=c(95), h=5)
pronostico_pib
## Point Forecast Lo 95 Hi 95
## 2022 10234.11 8689.071 11779.15
## 2023 10422.54 8237.526 12607.56
## 2024 10610.98 7934.886 13287.07
## 2025 10799.41 7709.325 13889.49
## 2026 10987.84 7533.023 14442.66
plot(pronostico_pib, main = "Pronóstico a 5 años del PIB en México")
### Información varios paÃses
more_gdp_data <- wb_data(country = c("NG", "HT", "KE"), indicator = "NY.GDP.PCAP.CD", start_date = 1950, end_date = 2022)
ggplot(data = more_gdp_data) +
geom_point(mapping = aes(x = date, y = NY.GDP.PCAP.CD, color = country, shape = country)) +
labs(x="Año", y = "PIB", title = "Comparación del PIB de Nigeria, Haità y Kenia")