Forma de dar la clase

Un ejemplo aplicado

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

Requisitos: - Tener instalado R - Opcional instalar R studio aunque es mas amigable la pantalla - Guardar todo en la misma carpeta

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=diff(log(pib))*100
periodo=1922:2019
cuadro=data.frame(periodo,tcrecimiento)
  kable(cuadro,align="ccc",caption="Cuadro 1 Tasa de crecimiento de Mexico, 1922-2019")%>%
  kable_styling(bootstrap_options = c("striped","hover","resposive"), full_width = TRUE,
                font_size=16, position="center")  
Cuadro 1 Tasa de crecimiento de Mexico, 1922-2019
periodo tcrecimiento
1922 2.19806937
1923 3.41934522
1924 -1.69488234
1925 6.21303881
1926 5.47008840
1927 -4.24075588
1928 0.36427786
1929 -3.62192040
1930 -6.78237319
1931 3.44851143
1932 -16.05259550
1933 10.38654274
1934 6.51006808
1935 7.29059834
1936 7.88397131
1937 3.19752685
1938 1.38885216
1939 5.37051127
1940 1.29866386
1941 9.23738023
1942 5.71586906
1943 3.54726621
1944 7.73586211
1945 3.17490604
1946 6.28846852
1947 3.54808482
1948 3.84658555
1949 5.50598320
1950 9.27812827
1951 7.48831321
1952 3.94787822
1953 0.32198519
1954 9.50188110
1955 8.13902240
1956 6.64740468
1957 7.29367620
1958 5.14192213
1959 2.96148928
1960 7.79615675
1961 4.22859928
1962 4.36217940
1963 7.27123110
1964 10.44626557
1965 5.96715690
1966 5.91754480
1967 5.68993363
1968 9.00534605
1969 3.36148237
1970 6.29981624
1971 3.69341168
1972 7.90773757
1973 7.56743061
1974 5.61612815
1975 5.58554683
1976 4.32265860
1977 3.33442491
1978 8.57825785
1979 9.25625095
1980 8.83153300
1981 8.40892985
1982 -0.62988340
1983 -4.28688418
1984 3.54654080
1985 2.56032626
1986 -3.82619496
1987 1.83873815
1988 1.23774667
1989 3.29365866
1990 4.34869339
1991 3.56261329
1992 6.36857251
1993 0.10224917
1994 4.82288727
1995 -6.49784218
1996 6.55373280
1997 6.62263376
1998 5.03501348
1999 2.71632558
2000 4.82419538
2001 -0.40520955
2002 -0.03985229
2003 1.43602194
2004 3.84568764
2005 2.28157949
2006 4.39697810
2007 2.26558688
2008 1.13709505
2009 -5.43056598
2010 4.99144654
2011 3.59751433
2012 3.57755836
2013 1.34500590
2014 2.76573859
2015 3.23509318
2016 2.86899464
2017 2.09596546
2018 2.11346350
2019 -0.90333592

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.3233  13.8965
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 2. Tasa de crecimiento por presidentes

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.19806937
## 2    1923 Expansion        3.41934522
## 3    1924  Recesion       -1.69488234
## 4    1925 Expansion        6.21303881
## 5    1926 Expansion        5.47008840
## 6    1927  Recesion       -4.24075588
## 7    1928  Recesion        0.36427786
## 8    1929  Recesion       -3.62192040
## 9    1930  Recesion       -6.78237319
## 10   1931 Expansion        3.44851143
## 11   1932  Recesion      -16.05259550
## 12   1933 Expansion       10.38654274
## 13   1934 Expansion        6.51006808
## 14   1935 Expansion        7.29059834
## 15   1936 Expansion        7.88397131
## 16   1937 Expansion        3.19752685
## 17   1938  Recesion        1.38885216
## 18   1939 Expansion        5.37051127
## 19   1940  Recesion        1.29866386
## 20   1941 Expansion        9.23738023
## 21   1942 Expansion        5.71586906
## 22   1943  Recesion        3.54726621
## 23   1944 Expansion        7.73586211
## 24   1945  Recesion        3.17490604
## 25   1946 Expansion        6.28846852
## 26   1947  Recesion        3.54808482
## 27   1948  Recesion        3.84658555
## 28   1949 Expansion        5.50598320
## 29   1950 Expansion        9.27812827
## 30   1951 Expansion        7.48831321
## 31   1952  Recesion        3.94787822
## 32   1953  Recesion        0.32198519
## 33   1954 Expansion        9.50188110
## 34   1955 Expansion        8.13902240
## 35   1956 Expansion        6.64740468
## 36   1957 Expansion        7.29367620
## 37   1958  Recesion        5.14192213
## 38   1959  Recesion        2.96148928
## 39   1960 Expansion        7.79615675
## 40   1961  Recesion        4.22859928
## 41   1962  Recesion        4.36217940
## 42   1963 Expansion        7.27123110
## 43   1964 Expansion       10.44626557
## 44   1965  Recesion        5.96715690
## 45   1966  Recesion        5.91754480
## 46   1967  Recesion        5.68993363
## 47   1968 Expansion        9.00534605
## 48   1969  Recesion        3.36148237
## 49   1970 Expansion        6.29981624
## 50   1971  Recesion        3.69341168
## 51   1972 Expansion        7.90773757
## 52   1973 Expansion        7.56743061
## 53   1974  Recesion        5.61612815
## 54   1975  Recesion        5.58554683
## 55   1976  Recesion        4.32265860
## 56   1977  Recesion        3.33442491
## 57   1978 Expansion        8.57825785
## 58   1979 Expansion        9.25625095
## 59   1980 Expansion        8.83153300
## 60   1981 Expansion        8.40892985
## 61   1982  Recesion       -0.62988340
## 62   1983  Recesion       -4.28688418
## 63   1984  Recesion        3.54654080
## 64   1985  Recesion        2.56032626
## 65   1986  Recesion       -3.82619496
## 66   1987  Recesion        1.83873815
## 67   1988  Recesion        1.23774667
## 68   1989 Expansion        3.29365866
## 69   1990 Expansion        4.34869339
## 70   1991 Expansion        3.56261329
## 71   1992 Expansion        6.36857251
## 72   1993  Recesion        0.10224917
## 73   1994 Expansion        4.82288727
## 74   1995  Recesion       -6.49784218
## 75   1996 Expansion        6.55373280
## 76   1997 Expansion        6.62263376
## 77   1998 Expansion        5.03501348
## 78   1999  Recesion        2.71632558
## 79   2000 Expansion        4.82419538
## 80   2001  Recesion       -0.40520955
## 81   2002  Recesion       -0.03985229
## 82   2003  Recesion        1.43602194
## 83   2004 Expansion        3.84568764
## 84   2005  Recesion        2.28157949
## 85   2006 Expansion        4.39697810
## 86   2007  Recesion        2.26558688
## 87   2008  Recesion        1.13709505
## 88   2009  Recesion       -5.43056598
## 89   2010 Expansion        4.99144654
## 90   2011 Expansion        3.59751433
## 91   2012 Expansion        3.57755836
## 92   2013  Recesion        1.34500590
## 93   2014 Expansion        2.76573859
## 94   2015 Expansion        3.23509318
## 95   2016 Expansion        2.86899464
## 96   2017  Recesion        2.09596546
## 97   2018  Recesion        2.11346350
## 98   2019  Recesion       -0.90333592
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('blue','red','green'),0.2))


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

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)