1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
|
R version 3.5.3 (2019-03-11) -- "Great Truth"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> ###################################################
> ### chunk number 1: preliminaries
> ###################################################
> library("zoo")
Attaching package: 'zoo'
The following objects are masked from 'package:base':
as.Date, as.Date.numeric
> library("tseries")
> library("strucchange")
Loading required package: sandwich
> library("timeDate")
> online <- FALSE ## if set to FALSE the local copy of MSFT.rda
> ## is used instead of get.hist.quote()
> options(prompt = "R> ")
R> Sys.setenv(TZ = "GMT")
R> suppressWarnings(RNGversion("3.5.0"))
R>
R>
R> ###################################################
R> ### chunk number 2: zoo-prelim
R> ###################################################
R> library("zoo")
R> set.seed(1071)
R>
R>
R> ###################################################
R> ### chunk number 3: zoo-vectors1
R> ###################################################
R> z1.index <- ISOdatetime(2004, rep(1:2,5), sample(28,10), 0, 0, 0)
R> z1.data <- rnorm(10)
R> z1 <- zoo(z1.data, z1.index)
R>
R>
R> ###################################################
R> ### chunk number 4: zoo-vectors2
R> ###################################################
R> z2.index <- as.POSIXct(paste(2004, rep(1:2, 5), sample(1:28, 10), sep = "-"))
R> z2.data <- sin(2*1:10/pi)
R> z2 <- zoo(z2.data, z2.index)
R>
R>
R> ###################################################
R> ### chunk number 5: zoo-matrix
R> ###################################################
R> Z.index <- as.Date(sample(12450:12500, 10))
R> Z.data <- matrix(rnorm(30), ncol = 3)
R> colnames(Z.data) <- c("Aa", "Bb", "Cc")
R> Z <- zoo(Z.data, Z.index)
R>
R>
R> ###################################################
R> ### chunk number 6: print1
R> ###################################################
R> z1
2004-01-05 2004-01-14 2004-01-19 2004-01-25 2004-01-27 2004-02-07
0.74675994 0.02107873 -0.29823529 0.68625772 1.94078850 1.27384445
2004-02-12 2004-02-16 2004-02-20 2004-02-24
0.22170438 -2.07607585 -1.78439244 -0.19533304
R> z1[3:7]
2004-01-19 2004-01-25 2004-01-27 2004-02-07 2004-02-12
-0.2982353 0.6862577 1.9407885 1.2738445 0.2217044
R>
R>
R> ###################################################
R> ### chunk number 7: print2
R> ###################################################
R> Z
Aa Bb Cc
2004-02-02 1.2554339 0.6815732 -0.63292049
2004-02-08 -1.4945833 1.3234122 -1.49442269
2004-02-09 -1.8746225 -0.8732929 0.62733971
2004-02-21 -0.1453861 0.4523490 -0.14597401
2004-02-22 0.2254242 0.5383894 0.23136133
2004-02-29 1.2069552 0.3181422 -0.01129202
2004-03-05 -1.2086102 1.4237978 -0.81614483
2004-03-10 -0.1103956 1.3477425 0.95522468
2004-03-14 0.8420238 -2.7384202 0.23150695
2004-03-20 -0.1901910 0.1230887 -1.51862157
R> Z[1:3, 2:3]
Bb Cc
2004-02-02 0.6815732 -0.6329205
2004-02-08 1.3234122 -1.4944227
2004-02-09 -0.8732929 0.6273397
R>
R>
R> ###################################################
R> ### chunk number 8: subset
R> ###################################################
R> z1[ISOdatetime(2004, 1, c(14, 25), 0, 0, 0)]
2004-01-14 2004-01-25
0.02107873 0.68625772
R>
R>
R> ###################################################
R> ### chunk number 9: summary
R> ###################################################
R> summary(z1)
Index z1
Min. :2004-01-05 00:00:00 Min. :-2.07608
1st Qu.:2004-01-20 12:00:00 1st Qu.:-0.27251
Median :2004-02-01 12:00:00 Median : 0.12139
Mean :2004-02-01 09:36:00 Mean : 0.05364
3rd Qu.:2004-02-15 00:00:00 3rd Qu.: 0.73163
Max. :2004-02-24 00:00:00 Max. : 1.94079
R> summary(Z)
Index Aa Bb Cc
Min. :2004-02-02 Min. :-1.8746 Min. :-2.7384 Min. :-1.51862
1st Qu.:2004-02-12 1st Qu.:-0.9540 1st Qu.: 0.1719 1st Qu.:-0.77034
Median :2004-02-25 Median :-0.1279 Median : 0.4954 Median :-0.07863
Mean :2004-02-25 Mean :-0.1494 Mean : 0.2597 Mean :-0.25739
3rd Qu.:2004-03-08 3rd Qu.: 0.6879 3rd Qu.: 1.1630 3rd Qu.: 0.23147
Max. :2004-03-20 Max. : 1.2554 Max. : 1.4238 Max. : 0.95522
R>
R>
R> ###################################################
R> ### chunk number 10: zooreg1
R> ###################################################
R> zr1 <- zooreg(sin(1:9), start = 2000, frequency = 4)
R> zr2 <- zoo(sin(1:9), seq(2000, 2002, by = 1/4), 4)
R> zr1
2000 Q1 2000 Q2 2000 Q3 2000 Q4 2001 Q1 2001 Q2 2001 Q3
0.8414710 0.9092974 0.1411200 -0.7568025 -0.9589243 -0.2794155 0.6569866
2001 Q4 2002 Q1
0.9893582 0.4121185
R> zr2
2000 Q1 2000 Q2 2000 Q3 2000 Q4 2001 Q1 2001 Q2 2001 Q3
0.8414710 0.9092974 0.1411200 -0.7568025 -0.9589243 -0.2794155 0.6569866
2001 Q4 2002 Q1
0.9893582 0.4121185
R>
R>
R> ###################################################
R> ### chunk number 11: zooreg2
R> ###################################################
R> zr1 <- zr1[-c(3, 5)]
R> zr1
2000 Q1 2000 Q2 2000 Q4 2001 Q2 2001 Q3 2001 Q4 2002 Q1
0.8414710 0.9092974 -0.7568025 -0.2794155 0.6569866 0.9893582 0.4121185
R> class(zr1)
[1] "zooreg" "zoo"
R> frequency(zr1)
[1] 4
R>
R>
R> ###################################################
R> ### chunk number 12: zooreg1b
R> ###################################################
R> zooreg(1:5, start = as.Date("2005-01-01"))
2005-01-01 2005-01-02 2005-01-03 2005-01-04 2005-01-05
1 2 3 4 5
R>
R>
R> ###################################################
R> ### chunk number 13: zooreg3
R> ###################################################
R> is.regular(zr1)
[1] TRUE
R> is.regular(zr1, strict = TRUE)
[1] FALSE
R>
R>
R> ###################################################
R> ### chunk number 14: zooreg4
R> ###################################################
R> zr1 <- as.zoo(zr1)
R> zr1
2000 Q1 2000 Q2 2000 Q4 2001 Q2 2001 Q3 2001 Q4 2002 Q1
0.8414710 0.9092974 -0.7568025 -0.2794155 0.6569866 0.9893582 0.4121185
R> class(zr1)
[1] "zoo"
R> is.regular(zr1)
[1] TRUE
R> frequency(zr1)
[1] 4
R>
R>
R> ###################################################
R> ### chunk number 15: zooreg5
R> ###################################################
R> as.ts(zr1)
Qtr1 Qtr2 Qtr3 Qtr4
2000 0.8414710 0.9092974 NA -0.7568025
2001 NA -0.2794155 0.6569866 0.9893582
2002 0.4121185
R> identical(zr2, as.zoo(as.ts(zr2)))
[1] TRUE
R>
R>
R> ###################################################
R> ### chunk number 16: plot1 eval=FALSE
R> ###################################################
R> ## plot(Z)
R>
R>
R> ###################################################
R> ### chunk number 17: plot2 eval=FALSE
R> ###################################################
R> ## plot(Z, plot.type = "single", col = 2:4)
R>
R>
R> ###################################################
R> ### chunk number 18: plot2-repeat
R> ###################################################
R> plot(Z, plot.type = "single", col = 2:4)
R>
R>
R> ###################################################
R> ### chunk number 19: plot1-repeat
R> ###################################################
R> plot(Z)
R>
R>
R> ###################################################
R> ### chunk number 20: plot3
R> ###################################################
R> plot(Z, type = "b", lty = 1:3, pch = list(Aa = 1:5, Bb = 2, Cc = 4), col = list(Bb = 2, 4))
R>
R>
R> ###################################################
R> ### chunk number 21: plot3-repeat eval=FALSE
R> ###################################################
R> ## plot(Z, type = "b", lty = 1:3, pch = list(Aa = 1:5, Bb = 2, Cc = 4), col = list(Bb = 2, 4))
R>
R>
R> ###################################################
R> ### chunk number 22: rbind
R> ###################################################
R> rbind(z1[5:10], z1[2:3])
2004-01-14 2004-01-19 2004-01-27 2004-02-07 2004-02-12 2004-02-16
0.02107873 -0.29823529 1.94078850 1.27384445 0.22170438 -2.07607585
2004-02-20 2004-02-24
-1.78439244 -0.19533304
R>
R>
R> ###################################################
R> ### chunk number 23: cbind
R> ###################################################
R> cbind(z1, z2)
z1 z2
2004-01-03 NA 0.94306673
2004-01-05 0.74675994 -0.04149429
2004-01-14 0.02107873 NA
2004-01-17 NA 0.59448077
2004-01-19 -0.29823529 -0.52575918
2004-01-24 NA -0.96739776
2004-01-25 0.68625772 NA
2004-01-27 1.94078850 NA
2004-02-07 1.27384445 NA
2004-02-08 NA 0.95605566
2004-02-12 0.22170438 -0.62733473
2004-02-13 NA -0.92845336
2004-02-16 -2.07607585 NA
2004-02-20 -1.78439244 NA
2004-02-24 -0.19533304 NA
2004-02-25 NA 0.56060280
2004-02-26 NA 0.08291711
R>
R>
R> ###################################################
R> ### chunk number 24: merge
R> ###################################################
R> merge(z1, z2, all = FALSE)
z1 z2
2004-01-05 0.7467599 -0.04149429
2004-01-19 -0.2982353 -0.52575918
2004-02-12 0.2217044 -0.62733473
R>
R>
R> ###################################################
R> ### chunk number 25: merge2
R> ###################################################
R> merge(z1, pi, 1:10)
z1 pi 1:10
2004-01-05 0.74675994 3.141593 1
2004-01-14 0.02107873 3.141593 2
2004-01-19 -0.29823529 3.141593 3
2004-01-25 0.68625772 3.141593 4
2004-01-27 1.94078850 3.141593 5
2004-02-07 1.27384445 3.141593 6
2004-02-12 0.22170438 3.141593 7
2004-02-16 -2.07607585 3.141593 8
2004-02-20 -1.78439244 3.141593 9
2004-02-24 -0.19533304 3.141593 10
R>
R>
R> ###################################################
R> ### chunk number 26: aggregate
R> ###################################################
R> firstofmonth <- function(x) as.Date(sub("..$", "01", format(x)))
R> aggregate(Z, firstofmonth(index(Z)), mean)
Aa Bb Cc
2004-02-01 -0.1377964 0.40676219 -0.2376514
2004-03-01 -0.1667933 0.03905223 -0.2870087
R> aggregate(Z, firstofmonth, head, 1)
Aa Bb Cc
2004-02-01 1.255434 0.6815732 -0.6329205
2004-03-01 -1.208610 1.4237978 -0.8161448
R>
R>
R> ###################################################
R> ### chunk number 27: disaggregate
R> ###################################################
R> Nile.na <- merge(as.zoo(Nile), zoo(, seq(start(Nile)[1], end(Nile)[1], 1/4)))
R> head(as.zoo(Nile))
1871 1872 1873 1874 1875 1876
1120 1160 963 1210 1160 1160
R> head(na.approx(Nile.na))
1871(1) 1871(2) 1871(3) 1871(4) 1872(1) 1872(2)
1120.00 1130.00 1140.00 1150.00 1160.00 1110.75
R> head(na.locf(Nile.na))
1871(1) 1871(2) 1871(3) 1871(4) 1872(1) 1872(2)
1120 1120 1120 1120 1160 1160
R> head(na.spline(Nile.na))
1871(1) 1871(2) 1871(3) 1871(4) 1872(1) 1872(2)
1120.000 1199.059 1224.985 1208.419 1160.000 1091.970
R>
R>
R> ###################################################
R> ### chunk number 28: Ops
R> ###################################################
R> z1 + z2
2004-01-05 2004-01-19 2004-02-12
0.7052657 -0.8239945 -0.4056304
R> z1 < z2
2004-01-05 2004-01-19 2004-02-12
FALSE FALSE FALSE
R>
R>
R> ###################################################
R> ### chunk number 29: cumsum
R> ###################################################
R> cumsum(Z)
Aa Bb Cc
2004-02-02 1.2554339 0.6815732 -0.6329205
2004-02-08 -0.2391494 2.0049854 -2.1273432
2004-02-09 -2.1137718 1.1316925 -1.5000035
2004-02-21 -2.2591579 1.5840415 -1.6459775
2004-02-22 -2.0337337 2.1224309 -1.4146162
2004-02-29 -0.8267785 2.4405731 -1.4259082
2004-03-05 -2.0353888 3.8643710 -2.2420530
2004-03-10 -2.1457844 5.2121135 -1.2868283
2004-03-14 -1.3037606 2.4736933 -1.0553214
2004-03-20 -1.4939516 2.5967820 -2.5739429
R>
R>
R> ###################################################
R> ### chunk number 30: coredata
R> ###################################################
R> coredata(z1)
[1] 0.74675994 0.02107873 -0.29823529 0.68625772 1.94078850 1.27384445
[7] 0.22170438 -2.07607585 -1.78439244 -0.19533304
R> coredata(z1) <- 1:10
R> z1
2004-01-05 2004-01-14 2004-01-19 2004-01-25 2004-01-27 2004-02-07 2004-02-12
1 2 3 4 5 6 7
2004-02-16 2004-02-20 2004-02-24
8 9 10
R>
R>
R> ###################################################
R> ### chunk number 31: index
R> ###################################################
R> index(z2)
[1] "2004-01-03 GMT" "2004-01-05 GMT" "2004-01-17 GMT" "2004-01-19 GMT"
[5] "2004-01-24 GMT" "2004-02-08 GMT" "2004-02-12 GMT" "2004-02-13 GMT"
[9] "2004-02-25 GMT" "2004-02-26 GMT"
R>
R>
R> ###################################################
R> ### chunk number 32: index2
R> ###################################################
R> index(z2) <- index(z1)
R> z2
2004-01-05 2004-01-14 2004-01-19 2004-01-25 2004-01-27 2004-02-07
0.94306673 -0.04149429 0.59448077 -0.52575918 -0.96739776 0.95605566
2004-02-12 2004-02-16 2004-02-20 2004-02-24
-0.62733473 -0.92845336 0.56060280 0.08291711
R>
R>
R> ###################################################
R> ### chunk number 33: startend
R> ###################################################
R> start(z1)
[1] "2004-01-05 GMT"
R> end(z1)
[1] "2004-02-24 GMT"
R>
R>
R> ###################################################
R> ### chunk number 34: window
R> ###################################################
R> window(Z, start = as.Date("2004-03-01"))
Aa Bb Cc
2004-03-05 -1.2086102 1.4237978 -0.8161448
2004-03-10 -0.1103956 1.3477425 0.9552247
2004-03-14 0.8420238 -2.7384202 0.2315069
2004-03-20 -0.1901910 0.1230887 -1.5186216
R> window(Z, index = index(Z)[5:8], end = as.Date("2004-03-01"))
Aa Bb Cc
2004-02-22 0.2254242 0.5383894 0.23136133
2004-02-29 1.2069552 0.3181422 -0.01129202
R>
R>
R> ###################################################
R> ### chunk number 35: window2
R> ###################################################
R> window(z1, end = as.POSIXct("2004-02-01")) <- 9:5
R> z1
2004-01-05 2004-01-14 2004-01-19 2004-01-25 2004-01-27 2004-02-07 2004-02-12
9 8 7 6 5 6 7
2004-02-16 2004-02-20 2004-02-24
8 9 10
R>
R>
R> ###################################################
R> ### chunk number 36: lagdiff
R> ###################################################
R> lag(z1, k = -1)
2004-01-14 2004-01-19 2004-01-25 2004-01-27 2004-02-07 2004-02-12 2004-02-16
9 8 7 6 5 6 7
2004-02-20 2004-02-24
8 9
R> merge(z1, lag(z1, k = 1))
z1 lag(z1, k = 1)
2004-01-05 9 8
2004-01-14 8 7
2004-01-19 7 6
2004-01-25 6 5
2004-01-27 5 6
2004-02-07 6 7
2004-02-12 7 8
2004-02-16 8 9
2004-02-20 9 10
2004-02-24 10 NA
R> diff(z1)
2004-01-14 2004-01-19 2004-01-25 2004-01-27 2004-02-07 2004-02-12 2004-02-16
-1 -1 -1 -1 1 1 1
2004-02-20 2004-02-24
1 1
R>
R>
R> ###################################################
R> ### chunk number 37: coercion
R> ###################################################
R> as.data.frame(Z)
Aa Bb Cc
2004-02-02 1.2554339 0.6815732 -0.63292049
2004-02-08 -1.4945833 1.3234122 -1.49442269
2004-02-09 -1.8746225 -0.8732929 0.62733971
2004-02-21 -0.1453861 0.4523490 -0.14597401
2004-02-22 0.2254242 0.5383894 0.23136133
2004-02-29 1.2069552 0.3181422 -0.01129202
2004-03-05 -1.2086102 1.4237978 -0.81614483
2004-03-10 -0.1103956 1.3477425 0.95522468
2004-03-14 0.8420238 -2.7384202 0.23150695
2004-03-20 -0.1901910 0.1230887 -1.51862157
R>
R>
R> ###################################################
R> ### chunk number 38: na
R> ###################################################
R> z1[sample(1:10, 3)] <- NA
R> z1
2004-01-05 2004-01-14 2004-01-19 2004-01-25 2004-01-27 2004-02-07 2004-02-12
9 NA 7 6 5 6 NA
2004-02-16 2004-02-20 2004-02-24
8 9 NA
R> na.omit(z1)
2004-01-05 2004-01-19 2004-01-25 2004-01-27 2004-02-07 2004-02-16 2004-02-20
9 7 6 5 6 8 9
R> na.contiguous(z1)
2004-01-19 2004-01-25 2004-01-27 2004-02-07
7 6 5 6
R> na.approx(z1)
2004-01-05 2004-01-14 2004-01-19 2004-01-25 2004-01-27 2004-02-07 2004-02-12
9.000000 7.714286 7.000000 6.000000 5.000000 6.000000 7.111111
2004-02-16 2004-02-20
8.000000 9.000000
R> na.approx(z1, 1:NROW(z1))
2004-01-05 2004-01-14 2004-01-19 2004-01-25 2004-01-27 2004-02-07 2004-02-12
9 8 7 6 5 6 7
2004-02-16 2004-02-20
8 9
R> na.locf(z1)
2004-01-05 2004-01-14 2004-01-19 2004-01-25 2004-01-27 2004-02-07 2004-02-12
9 9 7 6 5 6 6
2004-02-16 2004-02-20 2004-02-24
8 9 9
R>
R>
R> ###################################################
R> ### chunk number 39: rollapply
R> ###################################################
R> rollapply(Z, 5, sd)
Aa Bb Cc
2004-02-09 1.2814876 0.8018950 0.8218959
2004-02-21 1.2658555 0.7891358 0.8025043
2004-02-22 1.2102011 0.8206819 0.5319727
2004-02-29 0.8662296 0.5266261 0.6411751
2004-03-05 0.9363400 1.7011273 0.6356144
2004-03-10 0.9508642 1.6892246 0.9578196
R> rollapply(Z, 5, sd, fill = NA, align = "left")
Aa Bb Cc
2004-02-02 1.2814876 0.8018950 0.8218959
2004-02-08 1.2658555 0.7891358 0.8025043
2004-02-09 1.2102011 0.8206819 0.5319727
2004-02-21 0.8662296 0.5266261 0.6411751
2004-02-22 0.9363400 1.7011273 0.6356144
2004-02-29 0.9508642 1.6892246 0.9578196
2004-03-05 NA NA NA
2004-03-10 NA NA NA
2004-03-14 NA NA NA
2004-03-20 NA NA NA
R>
R>
R> ###################################################
R> ### chunk number 40: rollmean
R> ###################################################
R> rollmean(z2, 5, fill = NA)
2004-01-05 2004-01-14 2004-01-19 2004-01-25 2004-01-27
NA NA 0.0005792538 0.0031770388 -0.1139910497
2004-02-07 2004-02-12 2004-02-16 2004-02-20 2004-02-24
-0.4185778750 -0.2013054791 0.0087574946 NA NA
R>
R>
R> ###################################################
R> ### chunk number 41: strucchange1
R> ###################################################
R> library("strucchange")
R> data("Journals", package = "AER")
R> Journals$age <- 2000 - Journals$foundingyear
R> scus <- gefp(log(subs) ~ log(price/citations), order.by = ~ age,
+ data = Journals)
R>
R>
R> ###################################################
R> ### chunk number 42: strucchange2
R> ###################################################
R> plot(scus)
R>
R>
R> ###################################################
R> ### chunk number 43: tseries1 eval=FALSE
R> ###################################################
R> ## library("tseries")
R> ## MSFT <- get.hist.quote(instrument = "MSFT", start = "2001-01-01",
R> ## end = "2004-09-30", origin = "1970-01-01", retclass = "ts")
R>
R>
R> ###################################################
R> ### chunk number 44: tseries1a
R> ###################################################
R> if(online) {
+ MSFT <- get.hist.quote("MSFT", start = "2001-01-01",
+ end = "2004-09-30", origin = "1970-01-01", retclass = "ts")
+ save(MSFT, file = "MSFT.rda", compress = TRUE)
+ } else {
+ load(system.file("doc", "MSFT.rda", package = "zoo"))
+ }
R>
R>
R> ###################################################
R> ### chunk number 45: tseries2
R> ###################################################
R> MSFT <- as.zoo(MSFT)
R> index(MSFT) <- as.Date(index(MSFT))
R> MSFT <- na.omit(MSFT)
R>
R>
R> ###################################################
R> ### chunk number 46: tseries3
R> ###################################################
R> MSFT <- as.zoo(MSFT)
R>
R>
R> ###################################################
R> ### chunk number 47: tseries3
R> ###################################################
R> plot(diff(log(MSFT)))
R>
R>
R> ###################################################
R> ### chunk number 48: timeDate2
R> ###################################################
R> library("timeDate")
R> z2td <- zoo(coredata(z2), timeDate(index(z2), FinCenter = "GMT"))
R> z2td
2004-01-05 2004-01-14 2004-01-19 2004-01-25 2004-01-27 2004-02-07
0.94306673 -0.04149429 0.59448077 -0.52575918 -0.96739776 0.95605566
2004-02-12 2004-02-16 2004-02-20 2004-02-24
-0.62733473 -0.92845336 0.56060280 0.08291711
R>
R>
R> ###################################################
R> ### chunk number 49: yearmon1
R> ###################################################
R> zr3 <- zooreg(rnorm(9), start = as.yearmon(2000), frequency = 12)
R> zr3
Jan 2000 Feb 2000 Mar 2000 Apr 2000 May 2000 Jun 2000
-0.30969096 0.08699142 -0.64837101 -0.62786277 -0.61932674 -0.95506154
Jul 2000 Aug 2000 Sep 2000
-1.91736406 0.38108885 1.51405511
R>
R>
R> ###################################################
R> ### chunk number 50: yearmon2
R> ###################################################
R> aggregate(zr3, as.yearqtr, mean)
2000 Q1 2000 Q2 2000 Q3
-0.2903569 -0.7340837 -0.0074067
R>
R>
R> ###################################################
R> ### chunk number 51: yearmon3
R> ###################################################
R> as.Date(index(zr3))
[1] "2000-01-01" "2000-02-01" "2000-03-01" "2000-04-01" "2000-05-01"
[6] "2000-06-01" "2000-07-01" "2000-08-01" "2000-09-01"
R> as.Date(index(zr3), frac = 1)
[1] "2000-01-31" "2000-02-29" "2000-03-31" "2000-04-30" "2000-05-31"
[6] "2000-06-30" "2000-07-31" "2000-08-31" "2000-09-30"
R>
R>
R> ###################################################
R> ### chunk number 52: yearmon4
R> ###################################################
R> index(zr3) <- as.POSIXct(index(zr3))
R> as.irts(zr3)
2000-01-01 00:00:00 GMT -0.3097
2000-02-01 00:00:00 GMT 0.08699
2000-03-01 00:00:00 GMT -0.6484
2000-04-01 00:00:00 GMT -0.6279
2000-05-01 00:00:00 GMT -0.6193
2000-06-01 00:00:00 GMT -0.9551
2000-07-01 00:00:00 GMT -1.917
2000-08-01 00:00:00 GMT 0.3811
2000-09-01 00:00:00 GMT 1.514
R>
R>
R>
> proc.time()
user system elapsed
0.708 0.046 0.734
|