Un ejemplo aplicado

Analisis del producto interno bruto en terminos reales (miles de millones de pesos de 2013)

library(tidyverse)
library(kableExtra)
pib=read.csv("pibreal2008.csv",header=TRUE,sep=',')
names(pib)<-tolower(names(pib))
pib=ts(pib$pibreal,st=1921,freq=1)
tcrecimiento=round(diff(log(pib))*100,1)
periodo=1922:2019

Crecimiento economico

Cuadro 1 Tasa de crecimiento de Mexico, 1922-2019
periodo tcrecimiento
1922 2.2
1923 3.4
1924 -1.7
1925 6.2
1926 5.5
1927 -4.2
1928 0.4
1929 -3.6
1930 -6.8
1931 3.4
1932 -16.1
1933 10.4
1934 6.5
1935 7.3
1936 7.9
1937 3.2
1938 1.4
1939 5.4
1940 1.3
1941 9.2
1942 5.7
1943 3.5
1944 7.7
1945 3.2
1946 6.3
1947 3.5
1948 3.8
1949 5.5
1950 9.3
1951 7.5
1952 3.9
1953 0.3
1954 9.5
1955 8.1
1956 6.6
1957 7.3
1958 5.1
1959 3.0
1960 7.8
1961 4.2
1962 4.4
1963 7.3
1964 10.4
1965 6.0
1966 5.9
1967 5.7
1968 9.0
1969 3.4
1970 6.3
1971 3.7
1972 7.9
1973 7.6
1974 5.6
1975 5.6
1976 4.3
1977 3.3
1978 8.6
1979 9.3
1980 8.8
1981 8.4
1982 -0.6
1983 -4.3
1984 3.5
1985 2.6
1986 -3.8
1987 1.8
1988 1.2
1989 3.3
1990 4.3
1991 3.6
1992 6.4
1993 0.1
1994 4.8
1995 -6.5
1996 6.6
1997 6.6
1998 5.0
1999 2.7
2000 4.8
2001 -0.4
2002 0.0
2003 1.4
2004 3.8
2005 2.3
2006 4.4
2007 2.3
2008 1.1
2009 -5.4
2010 5.0
2011 3.6
2012 3.6
2013 1.3
2014 2.8
2015 3.2
2016 2.9
2017 2.1
2018 2.1
2019 -0.9

Grafica 1 Tasa de crecimiento de Mexico, 1922-2019

par(mar=c(2,2.5,2,2))
plot(tcrecimiento,ylab='%',xlab='Tiempo',col=rgb(0.5,.7,.63,.9))
(fit=StructTS(tcrecimiento,type="level"))
## 
## Call:
## StructTS(x = tcrecimiento, type = "level")
## 
## Variances:
##   level  epsilon  
##  0.3226  13.9054
k.filter=KalmanRun(tcrecimiento,fit$model)
filter=k.filter$states
filter=filter[,1]
filter=ts(filter,st=1922,freq=1)
lines(filter,lty=2,col=rgb(.2,.4, .99))
legend('bottomright',lty=c(1,2),col=c(rgb(0.5,0.7,.63,0.9),rgb(.2, .4, .99)),
       legend=c('Tasa de crecimiento','Kalman Filter'),cex=.8)
abline(h=0,lty=2)
abline(v=1982,lty=2)
abline(v=1933,lty=2)
text(1945,10,'Sustitucion de importaciones',cex=.6)
text(1933,-10,"1933", cex=.6)
text(1982,-10,"1981", cex=.6)
text(1995,10,'Neoliberalismo',cex=.6)

Grafica 3. Tasa de crecimiento predicion 5 años

Cuadro 2 Crecimiento economico en 5 años
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
2020 2.378156 -2.779473 7.535784 -5.509756 10.26607
2021 2.460200 -2.737224 7.657625 -5.488575 10.40897
2022 2.535735 -2.695184 7.766655 -5.464265 10.53574
2023 2.605278 -2.653865 7.864421 -5.437887 10.64844
2024 2.669303 -2.613645 7.952251 -5.410269 10.74887

Grafica 3. Tasa de crecimiento por presidentes

library(ggplot2)
library(plotly)
## 
## 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
ciclo=tcrecimiento-filter
cuadro1=ifelse(ciclo>0,'Expansion','Recesion')
tiempo=1922:2019
cuadro=data.frame(tiempo,cuadro1,tcrecimiento[-99])
cuadro
##    tiempo   cuadro1 tcrecimiento..99.
## 1    1922 Expansion               2.2
## 2    1923 Expansion               3.4
## 3    1924  Recesion              -1.7
## 4    1925 Expansion               6.2
## 5    1926 Expansion               5.5
## 6    1927  Recesion              -4.2
## 7    1928  Recesion               0.4
## 8    1929  Recesion              -3.6
## 9    1930  Recesion              -6.8
## 10   1931 Expansion               3.4
## 11   1932  Recesion             -16.1
## 12   1933 Expansion              10.4
## 13   1934 Expansion               6.5
## 14   1935 Expansion               7.3
## 15   1936 Expansion               7.9
## 16   1937 Expansion               3.2
## 17   1938  Recesion               1.4
## 18   1939 Expansion               5.4
## 19   1940  Recesion               1.3
## 20   1941 Expansion               9.2
## 21   1942 Expansion               5.7
## 22   1943  Recesion               3.5
## 23   1944 Expansion               7.7
## 24   1945  Recesion               3.2
## 25   1946 Expansion               6.3
## 26   1947  Recesion               3.5
## 27   1948  Recesion               3.8
## 28   1949 Expansion               5.5
## 29   1950 Expansion               9.3
## 30   1951 Expansion               7.5
## 31   1952  Recesion               3.9
## 32   1953  Recesion               0.3
## 33   1954 Expansion               9.5
## 34   1955 Expansion               8.1
## 35   1956 Expansion               6.6
## 36   1957 Expansion               7.3
## 37   1958  Recesion               5.1
## 38   1959  Recesion               3.0
## 39   1960 Expansion               7.8
## 40   1961  Recesion               4.2
## 41   1962  Recesion               4.4
## 42   1963 Expansion               7.3
## 43   1964 Expansion              10.4
## 44   1965  Recesion               6.0
## 45   1966  Recesion               5.9
## 46   1967  Recesion               5.7
## 47   1968 Expansion               9.0
## 48   1969  Recesion               3.4
## 49   1970 Expansion               6.3
## 50   1971  Recesion               3.7
## 51   1972 Expansion               7.9
## 52   1973 Expansion               7.6
## 53   1974  Recesion               5.6
## 54   1975  Recesion               5.6
## 55   1976  Recesion               4.3
## 56   1977  Recesion               3.3
## 57   1978 Expansion               8.6
## 58   1979 Expansion               9.3
## 59   1980 Expansion               8.8
## 60   1981 Expansion               8.4
## 61   1982  Recesion              -0.6
## 62   1983  Recesion              -4.3
## 63   1984  Recesion               3.5
## 64   1985  Recesion               2.6
## 65   1986  Recesion              -3.8
## 66   1987  Recesion               1.8
## 67   1988  Recesion               1.2
## 68   1989 Expansion               3.3
## 69   1990 Expansion               4.3
## 70   1991 Expansion               3.6
## 71   1992 Expansion               6.4
## 72   1993  Recesion               0.1
## 73   1994 Expansion               4.8
## 74   1995  Recesion              -6.5
## 75   1996 Expansion               6.6
## 76   1997 Expansion               6.6
## 77   1998 Expansion               5.0
## 78   1999  Recesion               2.7
## 79   2000 Expansion               4.8
## 80   2001  Recesion              -0.4
## 81   2002  Recesion               0.0
## 82   2003  Recesion               1.4
## 83   2004 Expansion               3.8
## 84   2005  Recesion               2.3
## 85   2006 Expansion               4.4
## 86   2007  Recesion               2.3
## 87   2008  Recesion               1.1
## 88   2009  Recesion              -5.4
## 89   2010 Expansion               5.0
## 90   2011 Expansion               3.6
## 91   2012 Expansion               3.6
## 92   2013  Recesion               1.3
## 93   2014 Expansion               2.8
## 94   2015 Expansion               3.2
## 95   2016 Expansion               2.9
## 96   2017  Recesion               2.1
## 97   2018  Recesion               2.1
## 98   2019  Recesion              -0.9
start=c(1920,1924,1928,1934,1940,1946,1952,1958,1964,1970,1976,
        1982,1988,1994,2000,2006,2012,2018)
end=c(1924,1928,1934,1940,1946,1952,1958,1964,1970,1976,
      1982,1988,1994,2000,2006,2012,2018,2019)


presidentes=c('AO', 'PEC','Varios', 'LC','MAC','MAV','ARC','ALM','GDO',
              'LEA','JLP','MDH','CSG','EZPL','FOX','FCH','EPN','AMLO')
modelo=c('Primario-exportador','Primario-exportador','Primario-exportador',
         'Sustitución de importaciones','Sustitución de importaciones',
         'Sustitución de importaciones','Sustitución de importaciones',
         'Sustitución de importaciones','Sustitución de importaciones',
         'Sustitución de importaciones','Sustitución de importaciones',
         'Neoliberalismo','Neoliberalismo','Neoliberalismo','Neoliberalismo',
         'Neoliberalismo','Neoliberalismo','Neoliberalismo')
nuevo=data.frame(start,end,presidentes,modelo)
datos1=data.frame(tiempo,tcrecimiento)
yrng=range(datos1$tcrecimiento)
xrng=range(datos1$tiempo)
p=datos1%>%
  ggplot(aes(tiempo,tcrecimiento))+geom_line()+theme_bw()

p=p+geom_vline(aes(xintercept=start),data=nuevo)
p=p+geom_hline(yintercept=0)
p=p+geom_rect(aes(NULL, NULL, xmin=start,xmax=end,
                fill=modelo),ymin=yrng[1],ymax=yrng[2],
            data=nuevo)+scale_fill_manual(values=
                                            alpha(c('#1a97ad','#436899','#131c80'),0.2))


p=p+geom_text(aes(x=start,y=yrng[2],
                          label=presidentes),
                      data=nuevo,size=2,hjust=0,vjust=0)

ggplotly(p)

Grafica 4 Años maximos de crecimiento economico por periodo

colnames(cuadro)=c("tiempo","ciclo","crecimiento")
with(cuadro,plot(tiempo,crecimiento,type='n'))
with(subset(cuadro,ciclo=='Recesion'),points(tiempo,crecimiento,col='#193B53',pch=16))
with(subset(cuadro,ciclo=='Expansion'),points(tiempo,crecimiento,col='#0A7DCB',pch=20))
abline(h=c(-5,0,5),lty=2)
legend('bottomright',pch=c(16,20),col=c('#193B53','#0A7DCB'),legend=c('Recesion','Expansion'))
with(cuadro,identify(tiempo,crecimiento,labels=tiempo,n=15,cex=.5))
## integer(0)
lines(filter,lty=2)
abline(v=c(1933,1982),lty=2)

Grafica 5 Masa salarial en Mexico