Llamar librerias

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.2.1
## ✔ 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 conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(forecast)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(ggplot2)

Pronóstico #1: PIB en México

gdp_data <- wb_data(country = "MX", indicator = "NY.GDP.PCAP.CD", start_date=1950, end_date=2022)
# Consultar country en: https://es.wikipedia.org/wiki/ISO_3166-1
summary(gdp_data)
##     iso2c              iso3c             country               date     
##  Length:62          Length:62          Length:62          Min.   :1960  
##  Class :character   Class :character   Class :character   1st Qu.:1975  
##  Mode  :character   Mode  :character   Mode  :character   Median :1990  
##                                                           Mean   :1990  
##                                                           3rd Qu.:2006  
##                                                           Max.   :2021  
##  NY.GDP.PCAP.CD        unit            obs_status          footnote        
##  Min.   :  359.5   Length:62          Length:62          Length:62         
##  1st Qu.: 1353.7   Class :character   Class :character   Class :character  
##  Median : 3784.7   Mode  :character   Mode  :character   Mode  :character  
##  Mean   : 4724.5                                                           
##  3rd Qu.: 8267.6                                                           
##  Max.   :11076.1                                                           
##   last_updated       
##  Min.   :2023-03-01  
##  1st Qu.:2023-03-01  
##  Median :2023-03-01  
##  Mean   :2023-03-01  
##  3rd Qu.:2023-03-01  
##  Max.   :2023-03-01
head(gdp_data)
## # A tibble: 6 × 9
##   iso2c iso3c country  date NY.GDP.PCAP.CD unit  obs_status footnote last_upda…¹
##   <chr> <chr> <chr>   <dbl>          <dbl> <chr> <chr>      <chr>    <date>     
## 1 MX    MEX   Mexico   1960           360. <NA>  <NA>       <NA>     2023-03-01 
## 2 MX    MEX   Mexico   1961           378. <NA>  <NA>       <NA>     2023-03-01 
## 3 MX    MEX   Mexico   1962           393. <NA>  <NA>       <NA>     2023-03-01 
## 4 MX    MEX   Mexico   1963           424. <NA>  <NA>       <NA>     2023-03-01 
## 5 MX    MEX   Mexico   1964           486. <NA>  <NA>       <NA>     2023-03-01 
## 6 MX    MEX   Mexico   1965           511. <NA>  <NA>       <NA>     2023-03-01 
## # … with abbreviated variable name ¹​last_updated
PIB <- gdp_data$NY.GDP.PCAP.CD
# Generar serie de tiempo

PIB_st <- ts(data= PIB, start = c(1960), frequency = 1)

# Generar pronóstico
modelo_PIB <- auto.arima(PIB_st, D=1)
modelo_PIB
## Series: PIB_st 
## ARIMA(0,1,0) with drift 
## 
## Coefficients:
##          drift
##       158.7891
## s.e.   88.9207
## 
## sigma^2 = 490358:  log likelihood = -485.69
## AIC=975.38   AICc=975.59   BIC=979.6
pronostico_PIB <- forecast(modelo_PIB, level=c(95), h=5)
pronostico_PIB
##      Point Forecast    Lo 95    Hi 95
## 2022       10204.47 8831.993 11576.95
## 2023       10363.26 8422.284 12304.23
## 2024       10522.05 8144.849 12899.25
## 2025       10680.84 7935.884 13425.79
## 2026       10839.63 7770.675 13908.58
plot(pronostico_PIB, main="Pronostico a 5 años del PIB de México del 2022 al 2026")

Informacion de varios paises

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_path(mapping= aes( x=date, y=NY.GDP.PCAP.CD, color = country))