# UNIVERSIDAD NACIONAL DEL ALTIPLANO
# INGENIERIA ESTADISTICA E INFORMATICA
# CURSO: SERIES DE TIEMPO

library(readxl)
## Warning: package 'readxl' was built under R version 4.0.2
library(lubridate)
## Warning: package 'lubridate' was built under R version 4.0.5
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.5
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.3     v purrr   0.3.4
## v tibble  3.1.5     v dplyr   1.0.7
## v tidyr   1.1.4     v stringr 1.4.0
## v readr   2.0.2     v forcats 0.5.1
## Warning: package 'ggplot2' was built under R version 4.0.3
## Warning: package 'tibble' was built under R version 4.0.5
## Warning: package 'tidyr' was built under R version 4.0.5
## Warning: package 'readr' was built under R version 4.0.5
## Warning: package 'dplyr' was built under R version 4.0.5
## Warning: package 'forcats' was built under R version 4.0.5
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x lubridate::as.difftime() masks base::as.difftime()
## x lubridate::date()        masks base::date()
## x dplyr::filter()          masks stats::filter()
## x lubridate::intersect()   masks base::intersect()
## x dplyr::lag()             masks stats::lag()
## x lubridate::setdiff()     masks base::setdiff()
## x lubridate::union()       masks base::union()
library(car)
## Warning: package 'car' was built under R version 4.0.5
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
## The following object is masked from 'package:purrr':
## 
##     some
library(tseries)
## Warning: package 'tseries' was built under R version 4.0.5
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(astsa)
## Warning: package 'astsa' was built under R version 4.0.5
library(foreign)
## Warning: package 'foreign' was built under R version 4.0.3
library(timsac)
## Warning: package 'timsac' was built under R version 4.0.3
library(vars)
## Warning: package 'vars' was built under R version 4.0.5
## Loading required package: MASS
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
## 
##     select
## Loading required package: strucchange
## Warning: package 'strucchange' was built under R version 4.0.5
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: sandwich
## 
## Attaching package: 'strucchange'
## The following object is masked from 'package:stringr':
## 
##     boundary
## Loading required package: urca
## Warning: package 'urca' was built under R version 4.0.5
## Loading required package: lmtest
## Warning: package 'lmtest' was built under R version 4.0.2
library(lmtest)
library(mFilter)
## Warning: package 'mFilter' was built under R version 4.0.5
library(dynlm)
## Warning: package 'dynlm' was built under R version 4.0.5
library(nlme)
## Warning: package 'nlme' was built under R version 4.0.5
## 
## Attaching package: 'nlme'
## The following object is masked from 'package:dplyr':
## 
##     collapse
library(broom)
## Warning: package 'broom' was built under R version 4.0.5
library(kableExtra)
## Warning: package 'kableExtra' was built under R version 4.0.5
## 
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows
library(knitr)
library(MASS)
library(parallel)
library(car)
library(mlogit)
## Warning: package 'mlogit' was built under R version 4.0.5
## Loading required package: dfidx
## Warning: package 'dfidx' was built under R version 4.0.5
## 
## Attaching package: 'dfidx'
## The following object is masked from 'package:MASS':
## 
##     select
## The following object is masked from 'package:stats':
## 
##     filter
library(dplyr)
library(tidyr)
library(forecast)
## Warning: package 'forecast' was built under R version 4.0.5
## 
## Attaching package: 'forecast'
## The following object is masked from 'package:nlme':
## 
##     getResponse
## The following object is masked from 'package:astsa':
## 
##     gas
library(fpp2)
## Warning: package 'fpp2' was built under R version 4.0.5
## -- Attaching packages ---------------------------------------------- fpp2 2.4 --
## v fma       2.4     v expsmooth 2.3
## Warning: package 'fma' was built under R version 4.0.5
## Warning: package 'expsmooth' was built under R version 4.0.5
## -- Conflicts ------------------------------------------------- fpp2_conflicts --
## x forecast::getResponse() masks nlme::getResponse()
## x car::some()             masks purrr::some()
## 
## Attaching package: 'fpp2'
## The following object is masked from 'package:astsa':
## 
##     oil
library(stats)
library(quantmod)
## Warning: package 'quantmod' was built under R version 4.0.5
## Loading required package: xts
## Warning: package 'xts' was built under R version 4.0.5
## 
## Attaching package: 'xts'
## The following objects are masked from 'package:dplyr':
## 
##     first, last
## Loading required package: TTR
INFLACION <- read_excel("E:/SERIES DE TIEMPO/TAREA 03/INFLACION.xls")
attach(INFLACION)
names(INFLACION)
## [1] "Periodo" "INDICEG"
inflacion <- ts(INFLACION$INDICEG, start = c(1970,1),frequency = 12)
plot(inflacion, main = "Inflacion en Mexico", col = "blue")

#Modelos Autoregresivos de Orden 1 (Consumo)
modelo1 <- dynlm(inflacion~L(inflacion), data = INFLACION)

#Modelos Autoregresivos de Orden 3
modelo2 <- dynlm(inflacion~L(inflacion,1:3),data = INFLACION)
#Modelos Autoregresivos con rezagos multiples
modelo3 <- dynlm(inflacion~L(inflacion, c(1,3,5)),data = INFLACION)

#Generar la Variable Tendencia
tend=seq_along(inflacion)
tend
##   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
##  [19]  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36
##  [37]  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54
##  [55]  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72
##  [73]  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90
##  [91]  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108
## [109] 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
## [127] 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
## [145] 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
## [163] 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
## [181] 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
## [199] 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
## [217] 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
## [235] 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
## [253] 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
## [271] 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
## [289] 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
## [307] 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
## [325] 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
## [343] 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
## [361] 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
## [379] 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
## [397] 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
## [415] 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
## [433] 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
## [451] 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
## [469] 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
## [487] 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
## [505] 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
## [523] 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
## [541] 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
## [559] 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
## [577] 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
## [595] 595
#Agregar tendencia al modelo Regresion
modelo4 <- dynlm(inflacion~L(inflacion, c(1,3,5)) + tend,data = INFLACION)#trend(inflacion)
summary(modelo4)
## 
## Time series regression with "ts" data:
## Start = 1970(6), End = 2019(7)
## 
## Call:
## dynlm(formula = inflacion ~ L(inflacion, c(1, 3, 5)) + tend, 
##     data = INFLACION)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.6528 -0.3500 -0.0786  0.2243  7.4912 
## 
## Coefficients:
##                             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                0.2967793  0.1012594   2.931  0.00351 ** 
## L(inflacion, c(1, 3, 5))1  0.7593185  0.0321612  23.610  < 2e-16 ***
## L(inflacion, c(1, 3, 5))3  0.0764182  0.0364587   2.096  0.03651 *  
## L(inflacion, c(1, 3, 5))5  0.0696191  0.0320499   2.172  0.03024 *  
## tend                      -0.0004948  0.0002464  -2.008  0.04513 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9333 on 585 degrees of freedom
## Multiple R-squared:  0.7941, Adjusted R-squared:  0.7927 
## F-statistic:   564 on 4 and 585 DF,  p-value: < 2.2e-16
#Agregar Estacionalidad al modelo Regresion
modelo5 <- dynlm(inflacion~L(inflacion, c(1,3,5))+trend(inflacion) + season(inflacion),data = INFLACION)
summary(modelo5)
## 
## Time series regression with "ts" data:
## Start = 1970(6), End = 2019(7)
## 
## Call:
## dynlm(formula = inflacion ~ L(inflacion, c(1, 3, 5)) + trend(inflacion) + 
##     season(inflacion), data = INFLACION)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.0669 -0.3448 -0.0342  0.2368  6.9837 
## 
## Coefficients:
##                            Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                0.938800   0.154206   6.088 2.09e-09 ***
## L(inflacion, c(1, 3, 5))1  0.751504   0.032744  22.951  < 2e-16 ***
## L(inflacion, c(1, 3, 5))3  0.102611   0.036594   2.804 0.005217 ** 
## L(inflacion, c(1, 3, 5))5  0.065226   0.032595   2.001 0.045849 *  
## trend(inflacion)          -0.005014   0.002749  -1.824 0.068697 .  
## season(inflacion)Feb      -1.413851   0.175729  -8.046 4.94e-15 ***
## season(inflacion)Mar      -0.884642   0.178473  -4.957 9.45e-07 ***
## season(inflacion)Apr      -0.855764   0.183638  -4.660 3.93e-06 ***
## season(inflacion)May      -1.174847   0.178281  -6.590 9.98e-11 ***
## season(inflacion)Jun      -0.673700   0.183319  -3.675 0.000260 ***
## season(inflacion)Jul      -0.700015   0.176661  -3.962 8.35e-05 ***
## season(inflacion)Aug      -0.516337   0.176372  -2.928 0.003552 ** 
## season(inflacion)Sep      -0.747915   0.175798  -4.254 2.45e-05 ***
## season(inflacion)Oct      -0.647694   0.176068  -3.679 0.000257 ***
## season(inflacion)Nov      -0.403294   0.176428  -2.286 0.022625 *  
## season(inflacion)Dec      -0.226236   0.175317  -1.290 0.197417    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.8657 on 574 degrees of freedom
## Multiple R-squared:  0.8261, Adjusted R-squared:  0.8216 
## F-statistic: 181.8 on 15 and 574 DF,  p-value: < 2.2e-16