#Librerias
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.8     ✔ dplyr   1.0.9
## ✔ tidyr   1.2.0     ✔ stringr 1.4.1
## ✔ readr   2.1.2     ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(ggplot2)
library(scatterplot3d)
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
#Cargar Datos y convertirlos a serie de tiempo
potential_gdp=read.csv("C:/Users/zacar/OneDrive/Documents/UABC/Semestre 1/MicroAplicada/Tarea 1/Datos2/potential_gdp.csv")
real_gdp=read.csv("C:/Users/zacar/OneDrive/Documents/UABC/Semestre 1/MicroAplicada/Tarea 1/Datos2/real_gdp.csv")
potential_gdp_data=data.frame(potential_gdp)
real_gdp_data=data.frame(real_gdp)
potential_gdp_data=ts(potential_gdp_data, frequency = 4,start=c(1985,1))
real_gdp_data=ts(real_gdp_data,frequency = 4, start=c(1985,1))

consumer_price84=read.csv("C:/Users/zacar/OneDrive/Documents/UABC/Semestre 1/MicroAplicada/Tarea 1/Datos2/consumer_price84.csv")
consumer_price84_data=data.frame(consumer_price84)
consumer_price84_data=ts(consumer_price84_data,frequency = 4, start=c(1984,1))

interes_fedfunds=read.csv("C:/Users/zacar/OneDrive/Documents/UABC/Semestre 1/MicroAplicada/Tarea 1/Datos2/interes_fedfunds.csv")
interes_fedfunds_data=data.frame(interes_fedfunds)
interes_fedfunds_data=ts(interes_fedfunds_data,frequency = 4, start=c(1985,1))

persistencia=read.csv("C:/Users/zacar/OneDrive/Documents/UABC/Semestre 1/MicroAplicada/Tarea 1/Datos2/interes_fedfunds_84-4 al 07-3.csv")
persistencia=data.frame(persistencia)
persistencia = c((persistencia[,2]))
persistencia
##  [1] 9.2625000 8.4751111 7.9243956 7.9009783 8.1044565 7.8268889 6.9184615
##  [8] 6.2094565 6.2667391 6.2225556 6.6500000 6.8395652 6.9196739 6.6659341
## [15] 7.1549451 7.9834783 8.4684783 9.4464444 9.7257143 9.0835870 8.6144565
## [22] 8.2480000 8.2393407 8.1602174 7.7433696 6.4304444 5.8640659 5.6452174
## [29] 4.8184783 4.0242857 3.7740659 3.2592391 3.0348913 3.0423333 2.9972527
## [36] 3.0577174 2.9882609 3.2093333 3.9380220 4.4854348 5.1679348 5.8027778
## [43] 6.0191209 5.7971739 5.7194565 5.3709890 5.2439560 5.3061957 5.2808696
## [50] 5.2784444 5.5221978 5.5346739 5.5073913 5.5193333 5.4974725 5.5316304
## [57] 4.8605435 4.7345556 4.7476923 5.0955435 5.3040217 5.6776923 6.2719780
## [64] 6.5194565 6.4748913 5.5970000 4.3267033 3.5015217 2.1304348 1.7328889
## [71] 1.7515385 1.7408696 1.4442391 1.2496667 1.2467033 1.0168478 0.9967391
## [78] 1.0018681 1.0113187 1.4307609 1.9498913 2.4690000 2.9417582 3.4600000
## [85] 3.9782609 4.4541111 4.9075824 5.2453261 5.2429348 5.2545556 5.2524176
## [92] 5.0743478
##Obtener datos 
#1.1 Logaritmo de Real y Potential GDP
#1.1.1 Potential
log_potential_gdp_data= log(potential_gdp_data[,2])
log_potential_gdp_data
##          Qtr1     Qtr2     Qtr3     Qtr4
## 1985 8.984566 8.993396 9.002130 9.010733
## 1986 9.019189 9.027534 9.035787 9.043965
## 1987 9.052028 9.059994 9.067866 9.075706
## 1988 9.083443 9.091149 9.098775 9.106337
## 1989 9.113806 9.121175 9.128443 9.135629
## 1990 9.142675 9.149602 9.156338 9.162899
## 1991 9.169324 9.175622 9.181843 9.188063
## 1992 9.194308 9.200616 9.207054 9.213556
## 1993 9.220121 9.226802 9.233537 9.240309
## 1994 9.247089 9.253866 9.260692 9.267525
## 1995 9.274334 9.281164 9.287971 9.294879
## 1996 9.301847 9.309189 9.316992 9.325235
## 1997 9.333895 9.342904 9.352280 9.361904
## 1998 9.371717 9.381751 9.391864 9.402055
## 1999 9.412343 9.422648 9.432980 9.443382
## 2000 9.453773 9.463862 9.473477 9.482590
## 2001 9.491196 9.499302 9.506921 9.514111
## 2002 9.520911 9.527450 9.533830 9.540081
## 2003 9.546276 9.552491 9.558668 9.564884
## 2004 9.571190 9.577547 9.584016 9.590525
## 2005 9.596888 9.603056 9.609101 9.614987
## 2006 9.620704 9.626285 9.631620 9.636713
## 2007 9.641772 9.646837 9.651924 9.656958
#1.1.1 Real
log_real_gdp_data= log(real_gdp_data[,2])
log_real_gdp_data
##          Qtr1     Qtr2     Qtr3     Qtr4
## 1985 8.965623 8.974389 8.989545 8.996951
## 1986 9.006243 9.010737 9.020261 9.025616
## 1987 9.033039 9.043770 9.052408 9.069433
## 1988 9.074586 9.087639 9.093481 9.106718
## 1989 9.116833 9.124437 9.131818 9.133787
## 1990 9.144655 9.148278 9.148943 9.139797
## 1991 9.135108 9.142875 9.147915 9.151395
## 1992 9.163295 9.174080 9.183913 9.194288
## 1993 9.195956 9.201760 9.206520 9.220029
## 1994 9.229686 9.243146 9.248974 9.260364
## 1995 9.263905 9.266884 9.275354 9.282122
## 1996 9.289583 9.306125 9.315055 9.325385
## 1997 9.331820 9.348303 9.360734 9.369284
## 1998 9.379228 9.388444 9.400894 9.416927
## 1999 9.426321 9.434636 9.447779 9.464074
## 2000 9.467712 9.485754 9.486751 9.492677
## 2001 9.489429 9.495624 9.491613 9.494382
## 2002 9.502630 9.508766 9.512793 9.514099
## 2003 9.519253 9.528147 9.544679 9.556153
## 2004 9.561866 9.569623 9.578992 9.589160
## 2005 9.600208 9.605062 9.612887 9.618551
## 2006 9.631947 9.634400 9.635903 9.644305
## 2007 9.647237 9.653601 9.659606 9.665684
#1.2 Obtención de la brecha del producto 
brechaproducto= log_real_gdp_data -log_potential_gdp_data 
brechaproducto
##               Qtr1          Qtr2          Qtr3          Qtr4
## 1985 -0.0189431597 -0.0190062261 -0.0125845001 -0.0137820430
## 1986 -0.0129461570 -0.0167969831 -0.0155261642 -0.0183482154
## 1987 -0.0189888396 -0.0162240320 -0.0154580523 -0.0062735260
## 1988 -0.0088573471 -0.0035094116 -0.0052934758  0.0003812393
## 1989  0.0030274620  0.0032616591  0.0033751005 -0.0018426382
## 1990  0.0019807542 -0.0013244000 -0.0073946317 -0.0231013757
## 1991 -0.0342163232 -0.0327472401 -0.0339281454 -0.0366679499
## 1992 -0.0310129741 -0.0265355175 -0.0231411865 -0.0192689266
## 1993 -0.0241654049 -0.0250417682 -0.0270170180 -0.0202795112
## 1994 -0.0174030298 -0.0107205665 -0.0117173625 -0.0071604860
## 1995 -0.0104286162 -0.0142809388 -0.0126169008 -0.0127572464
## 1996 -0.0122632953 -0.0030644389 -0.0019369569  0.0001503957
## 1997 -0.0020746945  0.0053994644  0.0084532688  0.0073801570
## 1998  0.0075112940  0.0066933956  0.0090299088  0.0148714557
## 1999  0.0139776046  0.0119876717  0.0147993390  0.0206917707
## 2000  0.0139390421  0.0218918928  0.0132737150  0.0100871031
## 2001 -0.0017667016 -0.0036774880 -0.0153079725 -0.0197288040
## 2002 -0.0182804390 -0.0186835252 -0.0210368647 -0.0259818418
## 2003 -0.0270226211 -0.0243433748 -0.0139896729 -0.0087306834
## 2004 -0.0093237179 -0.0079242943 -0.0050244294 -0.0013646446
## 2005  0.0033193812  0.0020061371  0.0037865950  0.0035630469
## 2006  0.0112425864  0.0081145725  0.0042833010  0.0075920148
## 2007  0.0054651930  0.0067636321  0.0076814544  0.0087257461
#2.0 Consumer Price o brecha de inflacion
#2.1 Logaritmo de consumer price
log_consumer_price84_data= log(consumer_price84_data[,2])
log_consumer_price84_data
##          Qtr1     Qtr2     Qtr3     Qtr4
## 1984 4.630188 4.639572 4.648230 4.656813
## 1985 4.665952 4.675007 4.681205 4.691348
## 1986 4.696533 4.691654 4.697749 4.704714
## 1987 4.716712 4.727978 4.738535 4.747826
## 1988 4.755600 4.767006 4.779123 4.789989
## 1989 4.801285 4.817320 4.825109 4.835223
## 1990 4.852291 4.862135 4.879260 4.896097
## 1991 4.903545 4.909464 4.917057 4.925319
## 1992 4.932073 4.939736 4.947340 4.956062
## 1993 4.963311 4.970508 4.975123 4.983378
## 1994 4.988390 4.994054 5.003275 5.009079
## 1995 5.016396 5.024538 5.029566 5.035003
## 1996 5.043855 5.052417 5.058155 5.066806
## 1997 5.072880 5.075174 5.080161 5.085537
## 1998 5.087596 5.090883 5.095997 5.100679
## 1999 5.104328 5.111787 5.119191 5.126540
## 2000 5.136386 5.144194 5.153292 5.160395
## 2001 5.169916 5.176903 5.179722 5.178971
## 2002 5.182158 5.189989 5.195361 5.201256
## 2003 5.211488 5.209850 5.217288 5.221076
## 2004 5.229503 5.237328 5.243685 5.254365
## 2005 5.259403 5.266138 5.281171 5.290453
## 2006 5.295647 5.304631 5.314027 5.309917
## 2007 5.319673 5.330935 5.337245 5.349437
#2.2 Diferencia de 4 trimestres atrás o tasa de inflacion trimestral

inflacion_tri=diff(log_consumer_price84_data, lag = 4, differences = 1)
inflacion_tri=data.frame(inflacion_tri)


#2.3 Brecha Inflacion

brechainflacion = c((inflacion_tri[,1])-(0.02))
brechainflacion = ts (brechainflacion,frequency = 4, start=c(1985,1))
brechainflacion
##               Qtr1          Qtr2          Qtr3          Qtr4
## 1985  0.0157637093  0.0154355329  0.0129751968  0.0145344631
## 1986  0.0105815338 -0.0033534997 -0.0034555050 -0.0066340659
## 1987  0.0001783687  0.0163239694  0.0207855308  0.0231126701
## 1988  0.0188881043  0.0190279333  0.0205885951  0.0221621366
## 1989  0.0256853999  0.0303146473  0.0259851132  0.0252345226
## 1990  0.0310055818  0.0248150900  0.0341516986  0.0408738430
## 1991  0.0312542413  0.0273282385  0.0177966422  0.0092224600
## 1992  0.0085280836  0.0102723201  0.0102834966  0.0107423233
## 1993  0.0112377505  0.0107716587  0.0077828071  0.0073165133
## 1994  0.0050789631  0.0035466359  0.0081516888  0.0057002423
## 1995  0.0080067594  0.0104840603  0.0062911421  0.0059241231
## 1996  0.0074586874  0.0078786288  0.0085887283  0.0118029150
## 1997  0.0090243867  0.0027569871  0.0020065467 -0.0012689633
## 1998 -0.0052831834 -0.0042907063 -0.0041641935 -0.0048574966
## 1999 -0.0032683303  0.0009038560  0.0031935374  0.0058609180
## 2000  0.0120584945  0.0124074991  0.0141008938  0.0138553734
## 2001  0.0135291523  0.0127082805  0.0064299061 -0.0014247881
## 2002 -0.0077576397 -0.0069132550 -0.0043601355  0.0022850448
## 2003  0.0093297792 -0.0001391044  0.0019266961 -0.0001793675
## 2004 -0.0019847407  0.0074777097  0.0063971097  0.0132891928
## 2005  0.0099002225  0.0088103733  0.0174860368  0.0160877125
## 2006  0.0162438649  0.0184922604  0.0128554525 -0.0005366891
## 2007  0.0040255713  0.0063038652  0.0032181072  0.0195200601
#3.0 Obtener tasa de interes
interes_fedfunds_data
##         DATE       DFF
## 1985 Q1    1 8.4751111
## 1985 Q2    2 7.9243956
## 1985 Q3    3 7.9009783
## 1985 Q4    4 8.1044565
## 1986 Q1    5 7.8268889
## 1986 Q2    6 6.9184615
## 1986 Q3    7 6.2094565
## 1986 Q4    8 6.2667391
## 1987 Q1    9 6.2225556
## 1987 Q2   10 6.6500000
## 1987 Q3   11 6.8395652
## 1987 Q4   12 6.9196739
## 1988 Q1   13 6.6659341
## 1988 Q2   14 7.1549451
## 1988 Q3   15 7.9834783
## 1988 Q4   16 8.4684783
## 1989 Q1   17 9.4464444
## 1989 Q2   18 9.7257143
## 1989 Q3   19 9.0835870
## 1989 Q4   20 8.6144565
## 1990 Q1   21 8.2480000
## 1990 Q2   22 8.2393407
## 1990 Q3   23 8.1602174
## 1990 Q4   24 7.7433696
## 1991 Q1   25 6.4304444
## 1991 Q2   26 5.8640659
## 1991 Q3   27 5.6452174
## 1991 Q4   28 4.8184783
## 1992 Q1   29 4.0242857
## 1992 Q2   30 3.7740659
## 1992 Q3   31 3.2592391
## 1992 Q4   32 3.0348913
## 1993 Q1   33 3.0423333
## 1993 Q2   34 2.9972527
## 1993 Q3   35 3.0577174
## 1993 Q4   36 2.9882609
## 1994 Q1   37 3.2093333
## 1994 Q2   38 3.9380220
## 1994 Q3   39 4.4854348
## 1994 Q4   40 5.1679348
## 1995 Q1   41 5.8027778
## 1995 Q2   42 6.0191209
## 1995 Q3   43 5.7971739
## 1995 Q4   44 5.7194565
## 1996 Q1   45 5.3709890
## 1996 Q2   46 5.2439560
## 1996 Q3   47 5.3061957
## 1996 Q4   48 5.2808696
## 1997 Q1   49 5.2784444
## 1997 Q2   50 5.5221978
## 1997 Q3   51 5.5346739
## 1997 Q4   52 5.5073913
## 1998 Q1   53 5.5193333
## 1998 Q2   54 5.4974725
## 1998 Q3   55 5.5316304
## 1998 Q4   56 4.8605435
## 1999 Q1   57 4.7345556
## 1999 Q2   58 4.7476923
## 1999 Q3   59 5.0955435
## 1999 Q4   60 5.3040217
## 2000 Q1   61 5.6776923
## 2000 Q2   62 6.2719780
## 2000 Q3   63 6.5194565
## 2000 Q4   64 6.4748913
## 2001 Q1   65 5.5970000
## 2001 Q2   66 4.3267033
## 2001 Q3   67 3.5015217
## 2001 Q4   68 2.1304348
## 2002 Q1   69 1.7328889
## 2002 Q2   70 1.7515385
## 2002 Q3   71 1.7408696
## 2002 Q4   72 1.4442391
## 2003 Q1   73 1.2496667
## 2003 Q2   74 1.2467033
## 2003 Q3   75 1.0168478
## 2003 Q4   76 0.9967391
## 2004 Q1   77 1.0018681
## 2004 Q2   78 1.0113187
## 2004 Q3   79 1.4307609
## 2004 Q4   80 1.9498913
## 2005 Q1   81 2.4690000
## 2005 Q2   82 2.9417582
## 2005 Q3   83 3.4600000
## 2005 Q4   84 3.9782609
## 2006 Q1   85 4.4541111
## 2006 Q2   86 4.9075824
## 2006 Q3   87 5.2453261
## 2006 Q4   88 5.2429348
## 2007 Q1   89 5.2545556
## 2007 Q2   90 5.2524176
## 2007 Q3   91 5.0743478
## 2007 Q4   92 4.4956522
interes_porcentaje= c((interes_fedfunds_data[,2])/100)
interes_porcentaje=ts (interes_porcentaje,frequency = 4, start=c(1985,1))
interes_porcentaje
##             Qtr1        Qtr2        Qtr3        Qtr4
## 1985 0.084751111 0.079243956 0.079009783 0.081044565
## 1986 0.078268889 0.069184615 0.062094565 0.062667391
## 1987 0.062225556 0.066500000 0.068395652 0.069196739
## 1988 0.066659341 0.071549451 0.079834783 0.084684783
## 1989 0.094464444 0.097257143 0.090835870 0.086144565
## 1990 0.082480000 0.082393407 0.081602174 0.077433696
## 1991 0.064304444 0.058640659 0.056452174 0.048184783
## 1992 0.040242857 0.037740659 0.032592391 0.030348913
## 1993 0.030423333 0.029972527 0.030577174 0.029882609
## 1994 0.032093333 0.039380220 0.044854348 0.051679348
## 1995 0.058027778 0.060191209 0.057971739 0.057194565
## 1996 0.053709890 0.052439560 0.053061957 0.052808696
## 1997 0.052784444 0.055221978 0.055346739 0.055073913
## 1998 0.055193333 0.054974725 0.055316304 0.048605435
## 1999 0.047345556 0.047476923 0.050955435 0.053040217
## 2000 0.056776923 0.062719780 0.065194565 0.064748913
## 2001 0.055970000 0.043267033 0.035015217 0.021304348
## 2002 0.017328889 0.017515385 0.017408696 0.014442391
## 2003 0.012496667 0.012467033 0.010168478 0.009967391
## 2004 0.010018681 0.010113187 0.014307609 0.019498913
## 2005 0.024690000 0.029417582 0.034600000 0.039782609
## 2006 0.044541111 0.049075824 0.052453261 0.052429348
## 2007 0.052545556 0.052524176 0.050743478 0.044956522
#4.0 Union de los dataframe con "ts.union"

datos_taylor= ts.union(interes_porcentaje, brechaproducto, brechainflacion)
datos_taylor= data.frame(datos_taylor)
cor(datos_taylor)
##                    interes_porcentaje brechaproducto brechainflacion
## interes_porcentaje          1.0000000     0.22467264      0.56345475
## brechaproducto              0.2246726     1.00000000     -0.05085399
## brechainflacion             0.5634548    -0.05085399      1.00000000
#5.0 Observación de datos
#5.1 Coeficiente de correlacioón.Se calcula el coeficiente de correlación de Pearson entre las variables regresoras.
cor(datos_taylor$brechaproducto, datos_taylor$brechainflacion)
## [1] -0.05085399
#5.2Matriz de Correlación
round(cor(datos_taylor), 2) # Matriz de correlación redondeada a 2 decimales
##                    interes_porcentaje brechaproducto brechainflacion
## interes_porcentaje               1.00           0.22            0.56
## brechaproducto                   0.22           1.00           -0.05
## brechainflacion                  0.56          -0.05            1.00
#5.3 Matriz de dispersión. Permite comparar varios subconjuntos de la base de datos para buscar patrones y relacione
plot(datos_taylor, main='Matriz de dispersión', pch = 19, col = "red")

#5.4 Mostramos visualmente la relación de la variable dependiente con cada una de las variables independientes
plot(datos_taylor$brechaproducto,datos_taylor$interes_porcentaje, pch = 19, col = "red", main = "Interes y Producto")

plot(datos_taylor$brechainflacion,datos_taylor$interes_porcentaje, pch = 22, col = "red", main = "Interes e Inflación")

#6.0 Grafico sin ajuste (Posible en 3D Porque solo son 3 variables)

#3D con libraria plotly
scatterplot3d(x=brechaproducto, y=brechainflacion, z=interes_porcentaje, pch=16, cex.lab=1,
              highlight.3d=TRUE, type="h", xlab='GDP',
              ylab='Inflacion', zlab='Interes')

#Movimiento 3D con libreria scatterplot3d
plot_ly(x=brechaproducto, y=brechainflacion, z=interes_porcentaje, type="scatter3d", color=brechaproducto,) %>% 
  layout(scene = list(xaxis = list(title = 'GDP'),
                      yaxis = list(title = 'Inflación'),
                      zaxis = list(title = 'Interes')))
## No scatter3d mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
#7.0 Regresion de Taylor
taylor_reg= lm(interes_porcentaje ~ brechaproducto + brechainflacion, data = datos_taylor)
taylor_reg
## 
## Call:
## lm(formula = interes_porcentaje ~ brechaproducto + brechainflacion, 
##     data = datos_taylor)
## 
## Coefficients:
##     (Intercept)   brechaproducto  brechainflacion  
##         0.04135          0.39851          1.23923
summary(taylor_reg)
## 
## Call:
## lm(formula = interes_porcentaje ~ brechaproducto + brechainflacion, 
##     data = datos_taylor)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.037774 -0.010219  0.000979  0.011032  0.038686 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     0.041348   0.002678  15.443  < 2e-16 ***
## brechaproducto  0.398515   0.130937   3.044  0.00307 ** 
## brechainflacion 1.239233   0.179422   6.907 7.11e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.01732 on 89 degrees of freedom
## Multiple R-squared:  0.3818, Adjusted R-squared:  0.3679 
## F-statistic: 27.49 on 2 and 89 DF,  p-value: 5.065e-10
anova(taylor_reg)
## Analysis of Variance Table
## 
## Response: interes_porcentaje
##                 Df    Sum Sq   Mean Sq F value    Pr(>F)    
## brechaproducto   1 0.0021796 0.0021796  7.2674  0.008396 ** 
## brechainflacion  1 0.0143073 0.0143073 47.7041 7.107e-10 ***
## Residuals       89 0.0266927 0.0002999                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
fitted(taylor_reg)
##          1          2          3          4          5          6          7 
## 0.05333406 0.05290224 0.05241246 0.05386752 0.04930203 0.03049866 0.03087870 
##          8          9         10         11         12         13         14 
## 0.02581509 0.03400198 0.05511196 0.06094613 0.06749017 0.06122526 0.06352977 
##         15         16         17         18         19         20         21 
## 0.06475281 0.06896426 0.07438496 0.08021501 0.07489491 0.07188541 0.08056078 
##         22         23         24         25         26         27         28 
## 0.07157216 0.08072332 0.08279425 0.06644386 0.06216407 0.04988160 0.03816434 
##         29         30         31         32         33         34         35 
## 0.03955743 0.04350328 0.04486982 0.04698157 0.04564420 0.04471736 0.04022631 
##         36         37         38         39         40         41         42 
## 0.04233346 0.04070693 0.04147108 0.04678058 0.04555865 0.04711456 0.04864931 
##         43         44         45         46         47         48         49 
## 0.04411645 0.04360569 0.04570422 0.04989051 0.05121981 0.05603477 0.05170480 
##         50         51         52         53         54         55         56 
## 0.04691659 0.04720361 0.04271684 0.03779454 0.03869851 0.03978642 0.04125520 
##         57         58         59         60         61         62         63 
## 0.04286833 0.04724563 0.05120357 0.05685729 0.06184647 0.06544830 0.06411234 
##         64         65         66         67         68         69         70 
## 0.06253817 0.05740999 0.05563126 0.04321598 0.03172041 0.02444973 0.02533548 
##         71         72         73         74         75         76         77 
## 0.02756155 0.03382583 0.04214113 0.03147470 0.03816081 0.03764669 0.03517308 
##         78         79         80         81         82         83         84 
## 0.04745695 0.04727348 0.05727285 0.05493978 0.05306586 0.06452656 0.06270463 
##         85         86         87         88         89         90         91 
## 0.06595855 0.06749827 0.05898614 0.04370872 0.04851486 0.05185564 0.04839743 
##         92 
## 0.06901552
#8 Graficas SOLO del interes (Federal Fund Rate) vs valores ajustados del FFR
#valores ajustados
interes_calculado=(taylor_reg$fitted.values)
#Valores ajustados a serie de tiempo
interes_calculado=ts(interes_calculado, freq = 4, start = c(1985, 1))
#Grafica con plot
plot(interes_porcentaje, main="Tasa de interés observada y ajustada 
     1985-2007 (Trimestral)",xlab="Periodo",ylab="Tasa de interés",col=1)
lines(interes_calculado,col=2)

# *** Obtain and save standardized residuals
taylor_reg$stdres <- rstandard(taylor_reg)
taylor_reg$stdre
##           1           2           3           4           5           6 
##  1.83441202  1.53784965  1.54624439  1.58153635  1.68352333  2.27589744 
##           7           8           9          10          11          12 
##  1.83532229  2.18270376  1.65509425  0.66409426  0.43600793  0.10002154 
##          13          14          15          16          17          18 
##  0.31686295  0.46790422  0.88113775  0.92192041  1.18594690  1.01645989 
##          19          20          21          22          23          24 
##  0.94224966  0.83940013  0.11456136  0.63669314  0.05271576 -0.33069169 
##          25          26          27          28          29          30 
## -0.12999972 -0.21180796  0.39072975  0.59724852  0.04048657 -0.33831979 
##          31          32          33          34          35          36 
## -0.71823712 -0.96990087 -0.89133036 -0.86422057 -0.56708659 -0.72699892 
##          37          38          39          40          41          42 
## -0.50244794 -0.12173238 -0.11193293  0.35572686  0.63398787  0.67114266 
##          43          44          45          46          47          48 
##  0.80584334  0.79051671  0.46536843  0.14809471  0.10704234 -0.18762326 
##          49          50          51          52          53          54 
##  0.06272980  0.48559131  0.47753284  0.72639431  1.02875521  0.96023396 
##          55          56          57          58          59          60 
##  0.91780105  0.43762172  0.26563308  0.01362424 -0.01463758 -0.22681104 
##          61          62          63          64          65          66 
## -0.29825780 -0.16248151  0.06367373  0.12959087 -0.08373330 -0.71839491 
##          67          68          69          70          71          72 
## -0.47747737 -0.61238100 -0.42268178 -0.46354257 -0.60021012 -1.14178289 
##          73          74          75          76          77          78 
## -1.74157537 -1.12051784 -1.63373347 -1.61646095 -1.47241876 -2.16899418 
##          79          80          81          82          83          84 
## -1.91550828 -2.19658118 -1.76170607 -1.37628413 -1.74928713 -1.33818570 
##          85          86          87          88          89          90 
## -1.25889994 -1.08156557 -0.38091396  0.51228503  0.23547098  0.03905055 
##          91          92 
##  0.13736062 -1.41484539
#Datos ajustados Fitted values are also called predicted values.

#Residual Standard Error (RSE)
sigma(taylor_reg)
## [1] 0.01731813
# This finds the R Squared value of a linear regression model named model.
summary(taylor_reg)$r.squared
## [1] 0.381822
# Para regresion con 3 valores.  Para incluir el plano de regresión que representa el modelo ajustado anterior se puede usar el siguiente código.
##Se crea el grafico 3d y se guarda en un objeto, por ejemplo mi_3d
taylor_3d <- scatterplot3d(x=brechaproducto, y=brechainflacion, z=interes_porcentaje, pch=16, cex.lab=1,
                       highlight.3d=TRUE, type="h", xlab='GDP',
                       ylab='Inflacion', zlab='Interes')
# Para agregar el plano usamos $plane3d( ) con argumento modelo ajustado
taylor_3d$plane3d(taylor_reg, lty.box="solid", col='mediumblue')

#plot predicción vs. Valores actuales
plot(x=predict(taylor_reg), y=datos_taylor$interes_porcentaje,
     xlab='Prediccion',
     ylab='Valores Actuales',
     main='Prediccion vs Valores Actuales')
abline(a=0, b=1)

#9.0 Estima la regla de Taylor con Persistencia

##Persistencia en porcentaje

persistencia_porcentaje= c((persistencia/100))
persistencia_porcentaje
##  [1] 0.092625000 0.084751111 0.079243956 0.079009783 0.081044565 0.078268889
##  [7] 0.069184615 0.062094565 0.062667391 0.062225556 0.066500000 0.068395652
## [13] 0.069196739 0.066659341 0.071549451 0.079834783 0.084684783 0.094464444
## [19] 0.097257143 0.090835870 0.086144565 0.082480000 0.082393407 0.081602174
## [25] 0.077433696 0.064304444 0.058640659 0.056452174 0.048184783 0.040242857
## [31] 0.037740659 0.032592391 0.030348913 0.030423333 0.029972527 0.030577174
## [37] 0.029882609 0.032093333 0.039380220 0.044854348 0.051679348 0.058027778
## [43] 0.060191209 0.057971739 0.057194565 0.053709890 0.052439560 0.053061957
## [49] 0.052808696 0.052784444 0.055221978 0.055346739 0.055073913 0.055193333
## [55] 0.054974725 0.055316304 0.048605435 0.047345556 0.047476923 0.050955435
## [61] 0.053040217 0.056776923 0.062719780 0.065194565 0.064748913 0.055970000
## [67] 0.043267033 0.035015217 0.021304348 0.017328889 0.017515385 0.017408696
## [73] 0.014442391 0.012496667 0.012467033 0.010168478 0.009967391 0.010018681
## [79] 0.010113187 0.014307609 0.019498913 0.024690000 0.029417582 0.034600000
## [85] 0.039782609 0.044541111 0.049075824 0.052453261 0.052429348 0.052545556
## [91] 0.052524176 0.050743478
interes_porcentaje=ts (interes_porcentaje,frequency = 4, start=c(1985,1))
interes_porcentaje
##             Qtr1        Qtr2        Qtr3        Qtr4
## 1985 0.084751111 0.079243956 0.079009783 0.081044565
## 1986 0.078268889 0.069184615 0.062094565 0.062667391
## 1987 0.062225556 0.066500000 0.068395652 0.069196739
## 1988 0.066659341 0.071549451 0.079834783 0.084684783
## 1989 0.094464444 0.097257143 0.090835870 0.086144565
## 1990 0.082480000 0.082393407 0.081602174 0.077433696
## 1991 0.064304444 0.058640659 0.056452174 0.048184783
## 1992 0.040242857 0.037740659 0.032592391 0.030348913
## 1993 0.030423333 0.029972527 0.030577174 0.029882609
## 1994 0.032093333 0.039380220 0.044854348 0.051679348
## 1995 0.058027778 0.060191209 0.057971739 0.057194565
## 1996 0.053709890 0.052439560 0.053061957 0.052808696
## 1997 0.052784444 0.055221978 0.055346739 0.055073913
## 1998 0.055193333 0.054974725 0.055316304 0.048605435
## 1999 0.047345556 0.047476923 0.050955435 0.053040217
## 2000 0.056776923 0.062719780 0.065194565 0.064748913
## 2001 0.055970000 0.043267033 0.035015217 0.021304348
## 2002 0.017328889 0.017515385 0.017408696 0.014442391
## 2003 0.012496667 0.012467033 0.010168478 0.009967391
## 2004 0.010018681 0.010113187 0.014307609 0.019498913
## 2005 0.024690000 0.029417582 0.034600000 0.039782609
## 2006 0.044541111 0.049075824 0.052453261 0.052429348
## 2007 0.052545556 0.052524176 0.050743478 0.044956522
##9.1 Union de los dataframe con "ts.union"

datos_taylor2= ts.union(interes_porcentaje, brechaproducto, brechainflacion, persistencia_porcentaje)
datos_taylor2= data.frame(datos_taylor2)
datos_taylor2
##    interes_porcentaje brechaproducto brechainflacion persistencia_porcentaje
## 1         0.084751111  -0.0189431597    0.0157637093             0.092625000
## 2         0.079243956  -0.0190062261    0.0154355329             0.084751111
## 3         0.079009783  -0.0125845001    0.0129751968             0.079243956
## 4         0.081044565  -0.0137820430    0.0145344631             0.079009783
## 5         0.078268889  -0.0129461570    0.0105815338             0.081044565
## 6         0.069184615  -0.0167969831   -0.0033534997             0.078268889
## 7         0.062094565  -0.0155261642   -0.0034555050             0.069184615
## 8         0.062667391  -0.0183482154   -0.0066340659             0.062094565
## 9         0.062225556  -0.0189888396    0.0001783687             0.062667391
## 10        0.066500000  -0.0162240320    0.0163239694             0.062225556
## 11        0.068395652  -0.0154580523    0.0207855308             0.066500000
## 12        0.069196739  -0.0062735260    0.0231126701             0.068395652
## 13        0.066659341  -0.0088573471    0.0188881043             0.069196739
## 14        0.071549451  -0.0035094116    0.0190279333             0.066659341
## 15        0.079834783  -0.0052934758    0.0205885951             0.071549451
## 16        0.084684783   0.0003812393    0.0221621366             0.079834783
## 17        0.094464444   0.0030274620    0.0256853999             0.084684783
## 18        0.097257143   0.0032616591    0.0303146473             0.094464444
## 19        0.090835870   0.0033751005    0.0259851132             0.097257143
## 20        0.086144565  -0.0018426382    0.0252345226             0.090835870
## 21        0.082480000   0.0019807542    0.0310055818             0.086144565
## 22        0.082393407  -0.0013244000    0.0248150900             0.082480000
## 23        0.081602174  -0.0073946317    0.0341516986             0.082393407
## 24        0.077433696  -0.0231013757    0.0408738430             0.081602174
## 25        0.064304444  -0.0342163232    0.0312542413             0.077433696
## 26        0.058640659  -0.0327472401    0.0273282385             0.064304444
## 27        0.056452174  -0.0339281454    0.0177966422             0.058640659
## 28        0.048184783  -0.0366679499    0.0092224600             0.056452174
## 29        0.040242857  -0.0310129741    0.0085280836             0.048184783
## 30        0.037740659  -0.0265355175    0.0102723201             0.040242857
## 31        0.032592391  -0.0231411865    0.0102834966             0.037740659
## 32        0.030348913  -0.0192689266    0.0107423233             0.032592391
## 33        0.030423333  -0.0241654049    0.0112377505             0.030348913
## 34        0.029972527  -0.0250417682    0.0107716587             0.030423333
## 35        0.030577174  -0.0270170180    0.0077828071             0.029972527
## 36        0.029882609  -0.0202795112    0.0073165133             0.030577174
## 37        0.032093333  -0.0174030298    0.0050789631             0.029882609
## 38        0.039380220  -0.0107205665    0.0035466359             0.032093333
## 39        0.044854348  -0.0117173625    0.0081516888             0.039380220
## 40        0.051679348  -0.0071604860    0.0057002423             0.044854348
## 41        0.058027778  -0.0104286162    0.0080067594             0.051679348
## 42        0.060191209  -0.0142809388    0.0104840603             0.058027778
## 43        0.057971739  -0.0126169008    0.0062911421             0.060191209
## 44        0.057194565  -0.0127572464    0.0059241231             0.057971739
## 45        0.053709890  -0.0122632953    0.0074586874             0.057194565
## 46        0.052439560  -0.0030644389    0.0078786288             0.053709890
## 47        0.053061957  -0.0019369569    0.0085887283             0.052439560
## 48        0.052808696   0.0001503957    0.0118029150             0.053061957
## 49        0.052784444  -0.0020746945    0.0090243867             0.052808696
## 50        0.055221978   0.0053994644    0.0027569871             0.052784444
## 51        0.055346739   0.0084532688    0.0020065467             0.055221978
## 52        0.055073913   0.0073801570   -0.0012689633             0.055346739
## 53        0.055193333   0.0075112940   -0.0052831834             0.055073913
## 54        0.054974725   0.0066933956   -0.0042907063             0.055193333
## 55        0.055316304   0.0090299088   -0.0041641935             0.054974725
## 56        0.048605435   0.0148714557   -0.0048574966             0.055316304
## 57        0.047345556   0.0139776046   -0.0032683303             0.048605435
## 58        0.047476923   0.0119876717    0.0009038560             0.047345556
## 59        0.050955435   0.0147993390    0.0031935374             0.047476923
## 60        0.053040217   0.0206917707    0.0058609180             0.050955435
## 61        0.056776923   0.0139390421    0.0120584945             0.053040217
## 62        0.062719780   0.0218918928    0.0124074991             0.056776923
## 63        0.065194565   0.0132737150    0.0141008938             0.062719780
## 64        0.064748913   0.0100871031    0.0138553734             0.065194565
## 65        0.055970000  -0.0017667016    0.0135291523             0.064748913
## 66        0.043267033  -0.0036774880    0.0127082805             0.055970000
## 67        0.035015217  -0.0153079725    0.0064299061             0.043267033
## 68        0.021304348  -0.0197288040   -0.0014247881             0.035015217
## 69        0.017328889  -0.0182804390   -0.0077576397             0.021304348
## 70        0.017515385  -0.0186835252   -0.0069132550             0.017328889
## 71        0.017408696  -0.0210368647   -0.0043601355             0.017515385
## 72        0.014442391  -0.0259818418    0.0022850448             0.017408696
## 73        0.012496667  -0.0270226211    0.0093297792             0.014442391
## 74        0.012467033  -0.0243433748   -0.0001391044             0.012496667
## 75        0.010168478  -0.0139896729    0.0019266961             0.012467033
## 76        0.009967391  -0.0087306834   -0.0001793675             0.010168478
## 77        0.010018681  -0.0093237179   -0.0019847407             0.009967391
## 78        0.010113187  -0.0079242943    0.0074777097             0.010018681
## 79        0.014307609  -0.0050244294    0.0063971097             0.010113187
## 80        0.019498913  -0.0013646446    0.0132891928             0.014307609
## 81        0.024690000   0.0033193812    0.0099002225             0.019498913
## 82        0.029417582   0.0020061371    0.0088103733             0.024690000
## 83        0.034600000   0.0037865950    0.0174860368             0.029417582
## 84        0.039782609   0.0035630469    0.0160877125             0.034600000
## 85        0.044541111   0.0112425864    0.0162438649             0.039782609
## 86        0.049075824   0.0081145725    0.0184922604             0.044541111
## 87        0.052453261   0.0042833010    0.0128554525             0.049075824
## 88        0.052429348   0.0075920148   -0.0005366891             0.052453261
## 89        0.052545556   0.0054651930    0.0040255713             0.052429348
## 90        0.052524176   0.0067636321    0.0063038652             0.052545556
## 91        0.050743478   0.0076814544    0.0032181072             0.052524176
## 92        0.044956522   0.0087257461    0.0195200601             0.050743478
#9.2Matriz de Correlación
round(cor(datos_taylor2), 2) # Matriz de correlación redondeada a 2 decimales
##                         interes_porcentaje brechaproducto brechainflacion
## interes_porcentaje                    1.00           0.22            0.56
## brechaproducto                        0.22           1.00           -0.05
## brechainflacion                       0.56          -0.05            1.00
## persistencia_porcentaje               0.98           0.13            0.55
##                         persistencia_porcentaje
## interes_porcentaje                         0.98
## brechaproducto                             0.13
## brechainflacion                            0.55
## persistencia_porcentaje                    1.00
#9.3 Matriz de dispersión. Permite comparar varios subconjuntos de la base de datos para buscar patrones y relacione
plot(datos_taylor2, main='Matriz de dispersión', pch = 19, col = "red")

#9.4 Modelo 2 o segunda regresión
taylor_reg_2= lm(interes_porcentaje ~ persistencia+ brechaproducto + brechainflacion, data = datos_taylor2)
taylor_reg_2
## 
## Call:
## lm(formula = interes_porcentaje ~ persistencia + brechaproducto + 
##     brechainflacion, data = datos_taylor2)
## 
## Coefficients:
##     (Intercept)     persistencia   brechaproducto  brechainflacion  
##        0.003794         0.009126         0.160450         0.131002
summary(taylor_reg_2)
## 
## Call:
## lm(formula = interes_porcentaje ~ persistencia + brechaproducto + 
##     brechainflacion, data = datos_taylor2)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.012679 -0.001909  0.000296  0.001742  0.009538 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     0.0037939  0.0011513   3.295  0.00142 ** 
## persistencia    0.0091258  0.0002341  38.975  < 2e-16 ***
## brechaproducto  0.1604496  0.0314135   5.108 1.87e-06 ***
## brechainflacion 0.1310015  0.0509057   2.573  0.01174 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.004076 on 88 degrees of freedom
## Multiple R-squared:  0.9661, Adjusted R-squared:  0.965 
## F-statistic: 837.2 on 3 and 88 DF,  p-value: < 2.2e-16
#10 Graficas SOLO del interes (Federal Fund Rate) vs valorescon persistencia
#valores ajustados
interes_calculado_2=(taylor_reg_2$fitted.values)
#Valores ajustados a serie de tiempo
interes_calculado_2=ts(interes_calculado_2, freq = 4, start = c(1985, 1))#aqui pongo 1985-1 pero la persistencia tiempo menos 1, 1984-4
interes_calculado_2
##            Qtr1       Qtr2       Qtr3       Qtr4
## 1985 0.08734747 0.08010878 0.07579110 0.07558952
## 1986 0.07706271 0.07208630 0.06398669 0.05664724
## 1987 0.05795964 0.06011514 0.06472330 0.06823175
## 1988 0.06799481 0.06655561 0.07093644 0.07961413
## 1989 0.08492630 0.09449506 0.09649465 0.08969920
## 1990 0.08678748 0.08210198 0.08227210 0.07991051
## 1991 0.07306286 0.06080273 0.05419593 0.05063592
## 1992 0.04390762 0.03760686 0.03586948 0.03185267
## 1993 0.02908458 0.02895082 0.02783095 0.02940269
## 1994 0.02893725 0.03182618 0.03891940 0.04432500
## 1995 0.05033116 0.05583105 0.05752308 0.05542703
## 1996 0.05499808 0.05334899 0.05246364 0.05378761
## 1997 0.05283548 0.05319153 0.05580766 0.05532023
## 1998 0.05456643 0.05467420 0.05486616 0.05602433
## 1999 0.04996488 0.04904241 0.04991338 0.05438268
## 2000 0.05601363 0.06074544 0.06500784 0.06672283
## 2001 0.06437146 0.05594586 0.04166477 0.03239601
## 2002 0.01928648 0.01570449 0.01583155 0.01581130
## 2003 0.01386018 0.01127399 0.01317882 0.01164910
## 2004 0.01113393 0.01264487 0.01305484 0.01837268
## 2005 0.02341776 0.02780158 0.03353809 0.03804842
## 2006 0.04403061 0.04816579 0.05095094 0.05280962
## 2007 0.05304421 0.05365705 0.05338057 0.05405867
#Grafica con plot
plot(interes_porcentaje, main="Tasa de interés observada y ajustada 
    (Trimestral) con Persistencia",xlab="Periodo",ylab="Tasa de interés",col=1)

lines(interes_calculado_2,col=2)