R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0 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 a HTML browser interface to help. Type 'q()' to quit R. > ### *
> ### > attach(NULL, name = "CheckExEnv") > assign(".CheckExEnv", as.environment(2), pos = length(search())) # base > ## add some hooks to label plot pages for base and grid graphics > setHook("plot.new", ".newplot.hook") > setHook("persp", ".newplot.hook") > setHook("grid.newpage", ".gridplot.hook") > > assign("cleanEx", + function(env = .GlobalEnv) { + rm(list = ls(envir = env, all.names = TRUE), envir = env) + RNGkind("default", "default") + set.seed(1) + options(warn = 1) + delayedAssign("T", stop("T used instead of TRUE"), + assign.env = .CheckExEnv) + delayedAssign("F", stop("F used instead of FALSE"), + assign.env = .CheckExEnv) + sch <- search() + newitems <- sch[! sch %in% .oldSearch] + for(item in rev(newitems)) + eval(substitute(detach(item), list(item=item))) + missitems <- .oldSearch[! .oldSearch %in% sch] + if(length(missitems)) + warning("items ", paste(missitems, collapse=", "), + " have been removed from the search path") + }, + env = .CheckExEnv) > assign("..nameEx", "__{must remake R-ex/*.R}__", env = .CheckExEnv) # for now > assign("ptime", proc.time(), env = .CheckExEnv) > grDevices::postscript("zoo-Examples.ps") > assign("par.postscript", graphics::par(no.readonly = TRUE), env = .CheckExEnv) > options(contrasts = c(unordered = "contr.treatment", ordered = "contr.poly")) > options(warn = 1) > library('zoo') > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "MATCH" > > ### * MATCH > > flush(stderr()); flush(stdout()) > > ### Name: MATCH > ### Title: Value Matching > ### Aliases: MATCH MATCH.default > ### Keywords: manip > > ### ** Examples > > MATCH(1:5, 2:3) [1] NA 1 2 NA NA > > > > cleanEx(); ..nameEx <- "ORDER" > > ### * ORDER > > flush(stderr()); flush(stdout()) > > ### Name: ORDER > ### Title: Ordering Permutation > ### Aliases: ORDER ORDER.default > ### Keywords: manip > > ### ** Examples > > ORDER(rnorm(5)) [1] 3 1 2 5 4 > > > > cleanEx(); ..nameEx <- "aggregate.zoo" > > ### * aggregate.zoo > > flush(stderr()); flush(stdout()) > > ### Name: aggregate.zoo > ### Title: Compute Summary Statistics of zoo Objects > ### Aliases: aggregate.zoo > ### Keywords: ts > > ### ** Examples > > ## averaging over values in a month: > # long series > x.date <- as.Date(paste(2004, rep(1:4, 4:1), seq(1,20,2), sep = "-")) > x <- zoo(rnorm(12), x.date) > # coarser dates > x.date2 <- as.Date(paste(2004, rep(1:4, 4:1), 1, sep = "-")) > x2 <- aggregate(x, x.date2, mean) > # compare time series > plot(x) > lines(x2, col = 2) > > ## aggregate a daily time series to a quarterly series > # create zoo series > tt <- as.Date("2000-1-1") + 0:300 > z.day <- zoo(0:300, tt) > > # function which returns corresponding first "Date" of quarter > first.of.quarter <- function(tt) as.Date(as.yearqtr(tt)) > > # average z over quarters > # 1. via "yearqtr" index (regular) > # 2. via "Date" index (not regular) > z.qtr1 <- aggregate(z.day, as.yearqtr, mean) > z.qtr2 <- aggregate(z.day, first.of.quarter, mean) > > ## aggregate on month and extend to monthly time series > if(require(chron)) { + y <- zoo(matrix(11:15,nr=5,nc=2), chron(c(15,20,80,100,110))) + colnames(y) <- c("A", "B") + + # aggregate by month using first of month as times for coarser series + # replacing each such group with the first observation in that month. + # Uses fact that chron dates format, by default, to mm/dd/yy + firstofmonth <- function(x) chron(sub("/../", "/01/", format(x))) + y2 <- aggregate(y, firstofmonth(time(y)), head, 1) + + # fill in missing months by merging with an empty series containing + # a complete set of 1st of the months + yrt2 <- range(time(y2)) + y0 <- zoo(,seq(from = yrt2[1], to = yrt2[2], by = "month")) + merge(y2, y0) + } Loading required package: chron A B 01/01/70 11 11 02/01/70 NA NA 03/01/70 13 13 04/01/70 14 14 > > > > cleanEx(); ..nameEx <- "as.Date.ts" > > ### * as.Date.ts > > flush(stderr()); flush(stdout()) > > ### Name: as.Date.numeric > ### Title: Date Conversion Functions from Numeric, Integer and ts Objects > ### Aliases: as.Date.numeric as.Date.integer as.Date.ts > ### Keywords: utilities chron > > ### ** Examples > > > as.Date(0) # origin, i.e. 1970-01-01 [1] "1970-01-01" > > # all three result in origin and next 9 days > as.Date(0:9) [1] "1970-01-01" "1970-01-02" "1970-01-03" "1970-01-04" "1970-01-05" [6] "1970-01-06" "1970-01-07" "1970-01-08" "1970-01-09" "1970-01-10" > as.Date(0) + 0:9 [1] "1970-01-01" "1970-01-02" "1970-01-03" "1970-01-04" "1970-01-05" [6] "1970-01-06" "1970-01-07" "1970-01-08" "1970-01-09" "1970-01-10" > myts <- ts(rnorm(10), start = 0) > as.Date(unclass(time(myts))) [1] "1970-01-01" "1970-01-02" "1970-01-03" "1970-01-04" "1970-01-05" [6] "1970-01-06" "1970-01-07" "1970-01-08" "1970-01-09" "1970-01-10" > > # both result in ten firsts of year starting 2001-01-01 > as.Date(ts(rnorm(10)), offset = 2000) [1] "2001-01-01" "2002-01-01" "2003-01-01" "2004-01-01" "2005-01-01" [6] "2006-01-01" "2007-01-01" "2008-01-01" "2009-01-01" "2010-01-01" > as.Date(ts(rnorm(10), start = 2001)) [1] "2001-01-01" "2002-01-01" "2003-01-01" "2004-01-01" "2005-01-01" [6] "2006-01-01" "2007-01-01" "2008-01-01" "2009-01-01" "2010-01-01" > > # both result in ten firsts of month starting with 1990-01-01 > as.Date(ts(rnorm(10), start = c(90,1), freq = 12), offset = 1900) [1] "1990-01-01" "1990-02-01" "1990-03-01" "1990-04-01" "1990-05-01" [6] "1990-06-01" "1990-07-01" "1990-08-01" "1990-09-01" "1990-10-01" > as.Date(ts(rnorm(10), start = c(1990,1), freq = 12)) [1] "1990-01-01" "1990-02-01" "1990-03-01" "1990-04-01" "1990-05-01" [6] "1990-06-01" "1990-07-01" "1990-08-01" "1990-09-01" "1990-10-01" > > > > > cleanEx(); ..nameEx <- "as.zoo" > > ### * as.zoo > > flush(stderr()); flush(stdout()) > > ### Name: as.zoo > ### Title: Coercion from and to zoo > ### Aliases: as.zoo as.zoo.default as.zoo.its as.zoo.irts as.zoo.zoo > ### as.matrix.zoo as.vector.zoo as.data.frame.zoo as.list.zoo as.list.ts > ### as.zoo.ts as.ts.zoo > ### Keywords: ts > > ### ** Examples > > ## coercion to zoo: > ## default method > as.zoo(rnorm(5)) 1 2 3 4 5 -0.6264538 0.1836433 -0.8356286 1.5952808 0.3295078 > ## method for "ts" objects > as.zoo(ts(rnorm(5), start = 1981, freq = 12)) 1981(1) 1981(2) 1981(3) 1981(4) 1981(5) -0.8204684 0.4874291 0.7383247 0.5757814 -0.3053884 > > ## coercion from zoo: > x.date <- as.POSIXct(paste("2003-", rep(1:4, 4:1), "-", sample(1:28, 10, replace = TRUE), sep = "")) > x <- zoo(matrix(rnorm(24), ncol = 2), x.date) > as.matrix(x) x.1 x.2 [1,] 0.82122120 1.35867955 [2,] -0.01619026 -0.47815006 [3,] 0.94383621 0.41794156 [4,] -0.04493361 -1.47075238 [5,] 0.78213630 -0.05380504 [6,] 0.59390132 -0.10278773 [7,] 0.91897737 0.38767161 [8,] 0.07456498 -1.37705956 [9,] -1.98935170 -0.41499456 [10,] 0.61982575 -0.39428995 > as.vector(x) [1] 0.82122120 -0.01619026 0.94383621 -0.04493361 0.78213630 0.59390132 [7] 0.91897737 0.07456498 -1.98935170 0.61982575 1.35867955 -0.47815006 [13] 0.41794156 -1.47075238 -0.05380504 -0.10278773 0.38767161 -1.37705956 [19] -0.41499456 -0.39428995 > as.data.frame(x) x.1 x.2 2003-01-04 0.82122120 1.35867955 2003-01-06 -0.01619026 -0.47815006 2003-01-19 0.94383621 0.41794156 2003-01-27 -0.04493361 -1.47075238 2003-02-01 0.78213630 -0.05380504 2003-02-08 0.59390132 -0.10278773 2003-02-11 0.91897737 0.38767161 2003-03-11 0.07456498 -1.37705956 2003-03-25 -1.98935170 -0.41499456 2003-04-10 0.61982575 -0.39428995 > as.list(x) $x.1 2003-01-04 2003-01-06 2003-01-19 2003-01-27 2003-02-01 2003-02-08 0.82122120 -0.01619026 0.94383621 -0.04493361 0.78213630 0.59390132 2003-02-11 2003-03-11 2003-03-25 2003-04-10 0.91897737 0.07456498 -1.98935170 0.61982575 $x.2 2003-01-04 2003-01-06 2003-01-19 2003-01-27 2003-02-01 2003-02-08 1.35867955 -0.47815006 0.41794156 -1.47075238 -0.05380504 -0.10278773 2003-02-11 2003-03-11 2003-03-25 2003-04-10 0.38767161 -1.37705956 -0.41499456 -0.39428995 > > > > cleanEx(); ..nameEx <- "coredata" > > ### * coredata > > flush(stderr()); flush(stdout()) > > ### Name: coredata > ### Title: Extracting/Replacing the Core Data of Objects > ### Aliases: coredata coredata.default coredata.zoo coredata.ts > ### coredata.its coredata.irts coredata<- coredata<-.zoo coredata<-.ts > ### coredata<-.irts coredata<-.its > ### Keywords: ts > > ### ** Examples > > x.date <- as.Date(paste(2003, rep(1:4, 4:1), seq(1,20,2), sep = "-")) > x <- zoo(matrix(rnorm(20), ncol = 2), x.date) > > ## the full time series > x 2003-01-01 -0.62645381 1.51178117 2003-01-03 0.18364332 0.38984324 2003-01-05 -0.83562861 -0.62124058 2003-01-07 1.59528080 -2.21469989 2003-02-09 0.32950777 1.12493092 2003-02-11 -0.82046838 -0.04493361 2003-02-13 0.48742905 -0.01619026 2003-03-15 0.73832471 0.94383621 2003-03-17 0.57578135 0.82122120 2003-04-19 -0.30538839 0.59390132 > ## and only matrix of observations > coredata(x) [,1] [,2] [1,] -0.6264538 1.51178117 [2,] 0.1836433 0.38984324 [3,] -0.8356286 -0.62124058 [4,] 1.5952808 -2.21469989 [5,] 0.3295078 1.12493092 [6,] -0.8204684 -0.04493361 [7,] 0.4874291 -0.01619026 [8,] 0.7383247 0.94383621 [9,] 0.5757814 0.82122120 [10,] -0.3053884 0.59390132 > > ## change the observations > coredata(x) <- matrix(1:20, ncol = 2) > x 2003-01-01 1 11 2003-01-03 2 12 2003-01-05 3 13 2003-01-07 4 14 2003-02-09 5 15 2003-02-11 6 16 2003-02-13 7 17 2003-03-15 8 18 2003-03-17 9 19 2003-04-19 10 20 > > > > cleanEx(); ..nameEx <- "frequency" > > ### * frequency > > flush(stderr()); flush(stdout()) > > ### Name: frequency<- > ### Title: Replacing the Index of Objects > ### Aliases: frequency<- frequency<-.zoo frequency<-.zooreg > ### Keywords: ts > > ### ** Examples > > z <- zooreg(1:5) > z 1 2 3 4 5 1 2 3 4 5 > as.ts(z) Time Series: Start = 1 End = 5 Frequency = 1 [1] 1 2 3 4 5 > frequency(z) <- 3 > z 1(1) 2(1) 3(1) 4(1) 5(1) 1 2 3 4 5 > as.ts(z) Time Series: Start = c(1, 1) End = c(5, 1) Frequency = 3 [1] 1 NA NA 2 NA NA 3 NA NA 4 NA NA 5 > > > > cleanEx(); ..nameEx <- "index" > > ### * index > > flush(stderr()); flush(stdout()) > > ### Name: index > ### Title: Extracting/Replacing the Index of Objects > ### Aliases: index index.default index.zoo index.ts time.zoo index<- > ### index<-.zoo time<- time<-.zoo value value<- value.default > ### value<-.default start.zoo end.zoo > ### Keywords: ts > > ### ** Examples > > x.date <- as.Date(paste(2003, 2, c(1, 3, 7, 9, 14), sep = "-")) > x <- zoo(rnorm(5), x.date) > > ## query index/time of a zoo object > index(x) [1] "2003-02-01" "2003-02-03" "2003-02-07" "2003-02-09" "2003-02-14" > time(x) [1] "2003-02-01" "2003-02-03" "2003-02-07" "2003-02-09" "2003-02-14" > > ## change class of index from Date to POSIXct > ## relative to current time zone > x 2003-02-01 2003-02-03 2003-02-07 2003-02-09 2003-02-14 -0.6264538 0.1836433 -0.8356286 1.5952808 0.3295078 > index(x) <- as.POSIXct(format(time(x)),tz="") > x 2003-02-01 2003-02-03 2003-02-07 2003-02-09 2003-02-14 -0.6264538 0.1836433 -0.8356286 1.5952808 0.3295078 > > ## replace index/time of a zoo object > index(x) <- 1:5 > x 1 2 3 4 5 -0.6264538 0.1836433 -0.8356286 1.5952808 0.3295078 > time(x) <- 6:10 > x 6 7 8 9 10 -0.6264538 0.1836433 -0.8356286 1.5952808 0.3295078 > > ## query start and end of a zoo object > start(x) [1] 6 > end(x) [1] 10 > > ## query index of a usual matrix > xm <- matrix(rnorm(10), ncol = 2) > index(xm) [1] 1 2 3 4 5 > > > > cleanEx(); ..nameEx <- "is.regular" > > ### * is.regular > > flush(stderr()); flush(stdout()) > > ### Name: is.regular > ### Title: Check Regularity of a Series > ### Aliases: is.regular is.regular.zoo is.regular.ts is.regular.zooreg > ### is.regular.default > ### Keywords: ts > > ### ** Examples > > ## checking of a strictly regular zoo series > z <- zoo(1:10, seq(2000, 2002.25, by = 0.25), frequency = 4) > z 2000(1) 2000(2) 2000(3) 2000(4) 2001(1) 2001(2) 2001(3) 2001(4) 2002(1) 2002(2) 1 2 3 4 5 6 7 8 9 10 > class(z) [1] "zooreg" "zoo" > frequency(z) ## extraction of frequency attribute [1] 4 > is.regular(z) [1] TRUE > is.regular(z, strict = TRUE) [1] TRUE > ## by omitting observations, the series is not strictly regular > is.regular(z[-3]) [1] TRUE > is.regular(z[-3], strict = TRUE) [1] FALSE > > ## checking of a plain zoo series without frequency attribute > ## which is in fact regular > z <- zoo(1:10, seq(2000, 2002.25, by = 0.25)) > z 2000 2000.25 2000.5 2000.75 2001 2001.25 2001.5 2001.75 2002 2002.25 1 2 3 4 5 6 7 8 9 10 > class(z) [1] "zoo" > frequency(z) ## data driven computation of frequency [1] 4 > is.regular(z) [1] TRUE > is.regular(z, strict = TRUE) [1] TRUE > ## by omitting observations, the series is not strictly regular > is.regular(z[-3]) [1] TRUE > is.regular(z[-3], strict = TRUE) [1] FALSE > > ## checking of an irregular zoo series > z <- zoo(1:10, rnorm(10)) > z -0.8356 -0.8205 -0.6265 -0.3054 0.1836 0.3295 0.4874 0.5758 0.7383 1.5953 3 6 1 10 2 5 7 9 8 4 > class(z) [1] "zoo" > frequency(z) ## attempt of data-driven frequency computation NULL > is.regular(z) [1] FALSE > is.regular(z, strict = TRUE) [1] FALSE > > > > cleanEx(); ..nameEx <- "lag.zoo" > > ### * lag.zoo > > flush(stderr()); flush(stdout()) > > ### Name: lag.zoo > ### Title: Lags and Differences of zoo Objects > ### Aliases: lag.zoo diff.zoo > ### Keywords: ts > > ### ** Examples > > x <- zoo(11:21) > > lag(x, k = 1) 1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20 21 > lag(x, k = -1) 2 3 4 5 6 7 8 9 10 11 11 12 13 14 15 16 17 18 19 20 > # this pairs each value of x with the next or future value > merge(x, lag1 = lag(x, k=1)) x lag1 1 11 12 2 12 13 3 13 14 4 14 15 5 15 16 6 16 17 7 17 18 8 18 19 9 19 20 10 20 21 11 21 NA > diff(x^2) 2 3 4 5 6 7 8 9 10 11 23 25 27 29 31 33 35 37 39 41 > > > > cleanEx(); ..nameEx <- "merge.zoo" > > ### * merge.zoo > > flush(stderr()); flush(stdout()) > > ### Name: merge.zoo > ### Title: Merge Two or More zoo Objects > ### Aliases: merge.zoo rbind.zoo c.zoo cbind.zoo > ### Keywords: ts > > ### ** Examples > > ## simple merging > x.date <- as.Date(paste(2003, 02, c(1, 3, 7, 9, 14), sep = "-")) > x <- zoo(rnorm(5), x.date) > > y1 <- zoo(matrix(1:10, ncol = 2), 1:5) > y2 <- zoo(matrix(rnorm(10), ncol = 2), 3:7) > > ## using arguments `fill' and `suffixes' > merge(y1, y2, all = FALSE) y1.1 y1.2 y2.1 y2.2 3 3.0000000 8.0000000 -0.8204684 1.5117812 4 4.0000000 9.0000000 0.4874291 0.3898432 5 5.0000000 10.0000000 0.7383247 -0.6212406 > merge(y1, y2, all = FALSE, suffixes = c("a", "b")) a.1 a.2 b.1 b.2 3 3.0000000 8.0000000 -0.8204684 1.5117812 4 4.0000000 9.0000000 0.4874291 0.3898432 5 5.0000000 10.0000000 0.7383247 -0.6212406 > merge(y1, y2, all = TRUE) y1.1 y1.2 y2.1 y2.2 1 1.0000000 6.0000000 NA NA 2 2.0000000 7.0000000 NA NA 3 3.0000000 8.0000000 -0.8204684 1.5117812 4 4.0000000 9.0000000 0.4874291 0.3898432 5 5.0000000 10.0000000 0.7383247 -0.6212406 6 NA NA 0.5757814 -2.2146999 7 NA NA -0.3053884 1.1249309 > merge(y1, y2, all = TRUE, fill = 0) y1.1 y1.2 y2.1 y2.2 1 1.0000000 6.0000000 0.0000000 0.0000000 2 2.0000000 7.0000000 0.0000000 0.0000000 3 3.0000000 8.0000000 -0.8204684 1.5117812 4 4.0000000 9.0000000 0.4874291 0.3898432 5 5.0000000 10.0000000 0.7383247 -0.6212406 6 0.0000000 0.0000000 0.5757814 -2.2146999 7 0.0000000 0.0000000 -0.3053884 1.1249309 > > ## if different index classes are merged, as in > ## the next merge example then ## a warning is issued and > ### the indexes are coerced. > ## It is up to the user to ensure that the result makes sense. > merge(x, y1, y2, all = TRUE) Warning in merge.zoo(x, y1, y2, all = TRUE) : Index vectors are of different classes: Date integer integer x y1.1 y1.2 y2.1 y2.2 1970-01-02 NA 1.0000000 6.0000000 NA NA 1970-01-03 NA 2.0000000 7.0000000 NA NA 1970-01-04 NA 3.0000000 8.0000000 -0.8204684 1.5117812 1970-01-05 NA 4.0000000 9.0000000 0.4874291 0.3898432 1970-01-06 NA 5.0000000 10.0000000 0.7383247 -0.6212406 1970-01-07 NA NA NA 0.5757814 -2.2146999 1970-01-08 NA NA NA -0.3053884 1.1249309 2003-02-01 -0.6264538 NA NA NA NA 2003-02-03 0.1836433 NA NA NA NA 2003-02-07 -0.8356286 NA NA NA NA 2003-02-09 1.5952808 NA NA NA NA 2003-02-14 0.3295078 NA NA NA NA > > ## extend an irregular series to a regular one: > # create a constant series > z <- zoo(1, seq(4)[-2]) > # create a 0 dimensional zoo series > z0 <- zoo(, 1:4) > # do the extension > merge(z, z0) 1 2 3 4 1 NA 1 1 > # same but with zero fill > merge(z, z0, fill = 0) 1 2 3 4 1 0 1 1 > > merge(z, coredata(z), 1) z coredata(z) 1 1 1 1 1 3 1 1 1 4 1 1 1 > > > > cleanEx(); ..nameEx <- "na.approx" > > ### * na.approx > > flush(stderr()); flush(stdout()) > > ### Name: na.approx > ### Title: Replace NA by Interpolation > ### Aliases: na.approx na.approx.default > ### Keywords: ts > > ### ** Examples > > > z <- zoo(c(2,NA,1,4,5,2), c(1,3,4,6,7,8)) > > ## use underlying time scale for interpolation > na.approx(z) 1 3 4 6 7 8 2.000000 1.333333 1.000000 4.000000 5.000000 2.000000 > ## use equidistant spacing > na.approx(z, 1:6) 1 3 4 6 7 8 2.0 1.5 1.0 4.0 5.0 2.0 > > na.approx(zoo(c(NA,9,3,2,3,2))) 2 3 4 5 6 9 3 2 3 2 > > > > cleanEx(); ..nameEx <- "na.locf" > > ### * na.locf > > flush(stderr()); flush(stdout()) > > ### Name: na.locf > ### Title: Last Observation Carried Forward > ### Aliases: na.locf na.locf.data.frame na.locf.list na.locf.default > ### Keywords: ts > > ### ** Examples > > az <- zoo(1:6) > > bz <- zoo(c(2,NA,1,4,5,2)) > na.locf(bz) 1 2 3 4 5 6 2 2 1 4 5 2 > > cz <- zoo(c(NA,9,3,2,3,2)) > na.locf(cz) 2 3 4 5 6 9 3 2 3 2 > > > > cleanEx(); ..nameEx <- "plot.zoo" > > ### * plot.zoo > > flush(stderr()); flush(stdout()) > > ### Name: plot.zoo > ### Title: Plotting zoo Objects > ### Aliases: plot.zoo lines.zoo > ### Keywords: ts > > ### ** Examples > > x.Date <- as.Date(paste(2003, 02, c(1, 3, 7, 9, 14), sep = "-")) > > ## univariate plotting > x <- zoo(rnorm(5), x.Date) > x2 <- zoo(rnorm(5, sd = 0.2), x.Date) > plot(x) > lines(x2, col = 2) > > ## multivariate plotting > z <- cbind(x, x2, zoo(rnorm(5, sd = 0.5), x.Date)) > colnames(z) <- LETTERS[1:3] > plot(z, plot.type = "single", col = list(B = 2)) > plot(z, type = "b", pch = 1:3, col = 1:3) > plot(z, type = "b", pch = list(A = 1:5, B = 3), col = list(C = 4, 2)) > plot(z, type = "b", screen = c(1,2,1), col = 1:3) > > ## plot one zoo series against the other. This does NOT dispatch plot.zoo. > plot(coredata(merge(x, x2))) > > > > cleanEx(); ..nameEx <- "rapply" > > ### * rapply > > flush(stderr()); flush(stdout()) > > ### Name: rapply > ### Title: Apply Rolling Functions > ### Aliases: rapply rapply.zoo rapply.ts > ### Keywords: iteration array ts > > ### ** Examples > > z <- zoo(11:15, as.Date(31:35)) > rapply(z, 2, mean) 1970-02-01 1970-02-02 1970-02-03 1970-02-04 11.5 12.5 13.5 14.5 > > z2 <- zoo(rnorm(6)) > rapply(z2, 3, mean, by = 3) # means of nonoverlapping groups of 3 2 5 -0.4261464 0.3681067 > aggregate(z2, c(3,3,3,6,6,6), mean) # same 3(1) 6(1) -0.4261464 0.3681067 > > rapply(z2, 3, mean) # uses rollmean which is optimized for mean 2 3 4 5 -0.4261464 0.3144318 0.3630533 0.3681067 > rollmean(z2, 3) # same 2 3 4 5 -0.4261464 0.3144318 0.3630533 0.3681067 > rapply(z2, 3, (mean)) # does not use rollmean 2 3 4 5 -0.4261464 0.3144318 0.3630533 0.3681067 > > > > cleanEx(); ..nameEx <- "read.zoo" > > ### * read.zoo > > flush(stderr()); flush(stdout()) > > ### Name: read.zoo > ### Title: Read zoo Series via read.table > ### Aliases: read.zoo > ### Keywords: ts > > ### ** Examples > > ## not run > ## Not run: > ##D ## turn (numeric) first column into yearmon index > ##D z <- read.zoo("foo.csv", sep = ",", FUN = as.yearmon) > ##D > ##D ## turn (character) first column into Date index > ##D z <- read.zoo("foo.tab", format = " > ## End(Not run) > > > > cleanEx(); ..nameEx <- "rollmean" > > ### * rollmean > > flush(stderr()); flush(stdout()) > > ### Name: rollmean > ### Title: Rolling Means/Maximums/Medians > ### Aliases: rollmean rollmax rollmedian rollmean.zoo rollmedian.zoo > ### rollmax.zoo rollmean.ts rollmedian.ts rollmax.ts rollmean.default > ### rollmedian.default rollmax.default > ### Keywords: ts > > ### ** Examples > > x.Date <- as.Date(paste(2004, rep(1:4, 4:1), sample(1:28, 10), sep = "-")) > x <- zoo(rnorm(12), x.Date) > > rollmean(x, 3) 2004-01-11 2004-01-15 2004-01-23 2004-02-05 2004-02-21 2004-02-25 2004-03-13 0.1350951 0.6005117 0.3362392 0.5940580 0.5320787 -0.1043585 -0.8153657 2004-03-14 -0.5703365 > rollmax(x, 3) 2004-01-11 2004-01-15 2004-01-23 2004-02-05 2004-02-21 2004-02-25 2004-03-13 0.7383247 0.7383247 0.7383247 1.5117812 1.5117812 1.5117812 0.3898432 2004-03-14 1.1249309 > rollmedian(x, 3) 2004-01-11 2004-01-15 2004-01-23 2004-02-05 2004-02-21 2004-02-25 2004-03-13 0.4874291 0.5757814 0.5757814 0.5757814 0.3898432 0.3898432 -0.6212406 2004-03-14 -0.6212406 > > xm <- zoo(matrix(1:12, 4, 3), x.Date[1:4]) > rollmean(xm, 3) 2004-01-11 2 6 10 2004-01-15 3 7 11 > rollmax(xm, 3) 2004-01-11 3 7 11 2004-01-15 4 8 12 > rollmedian(xm, 3) 2004-01-11 2 6 10 2004-01-15 3 7 11 > > rapply(xm, 3, mean) # uses rollmean 2004-01-11 2 6 10 2004-01-15 3 7 11 > rapply(xm, 3, function(x) mean(x)) # does not use rollmean 2004-01-11 2 6 10 2004-01-15 3 7 11 > > > > > cleanEx(); ..nameEx <- "window.zoo" > > ### * window.zoo > > flush(stderr()); flush(stdout()) > > ### Name: window.zoo > ### Title: Extract/Replacing the Time Windows of Objects > ### Aliases: window.zoo window<-.zoo > ### Keywords: ts > > ### ** Examples > > ## zoo example > x.date <- as.Date(paste(2003, rep(1:4, 4:1), seq(1,19,2), sep = "-")) > x <- zoo(matrix(rnorm(20), ncol = 2), x.date) > x 2003-01-01 -0.62645381 1.51178117 2003-01-03 0.18364332 0.38984324 2003-01-05 -0.83562861 -0.62124058 2003-01-07 1.59528080 -2.21469989 2003-02-09 0.32950777 1.12493092 2003-02-11 -0.82046838 -0.04493361 2003-02-13 0.48742905 -0.01619026 2003-03-15 0.73832471 0.94383621 2003-03-17 0.57578135 0.82122120 2003-04-19 -0.30538839 0.59390132 > > window(x, start = as.Date("2003-02-01"), end = as.Date("2003-03-01")) 2003-02-09 0.32950777 1.12493092 2003-02-11 -0.82046838 -0.04493361 2003-02-13 0.48742905 -0.01619026 > window(x, index = x.date[1:6], start = as.Date("2003-02-01")) 2003-02-09 0.32950777 1.12493092 2003-02-11 -0.82046838 -0.04493361 > window(x, index = x.date[c(4, 8, 10)]) 2003-01-07 1.5952808 -2.2146999 2003-03-15 0.7383247 0.9438362 2003-04-19 -0.3053884 0.5939013 > window(x, index = x.date[c(4, 8, 10)]) <- matrix(1:6, ncol = 2) > x 2003-01-01 -0.62645381 1.51178117 2003-01-03 0.18364332 0.38984324 2003-01-05 -0.83562861 -0.62124058 2003-01-07 1.00000000 4.00000000 2003-02-09 0.32950777 1.12493092 2003-02-11 -0.82046838 -0.04493361 2003-02-13 0.48742905 -0.01619026 2003-03-15 2.00000000 5.00000000 2003-03-17 0.57578135 0.82122120 2003-04-19 3.00000000 6.00000000 > > ## zooreg example (with plain numeric index) > z <- zooreg(rnorm(10), start = 2000, freq = 4) > window(z, start = 2001.75) 2001(4) 2002(1) 2002(2) -1.4707524 -0.4781501 0.4179416 > window(z, start = c(2001, 4)) 2001(4) 2002(1) 2002(2) -1.4707524 -0.4781501 0.4179416 > > > > cleanEx(); ..nameEx <- "yearmon" > > ### * yearmon > > flush(stderr()); flush(stdout()) > > ### Name: yearmon > ### Title: An Index Class for Monthly Data > ### Aliases: yearmon as.yearmon as.yearmon.default as.yearmon.numeric > ### as.yearmon.integer as.yearmon.dates as.yearmon.Date as.yearmon.POSIXt > ### as.Date.yearmon as.POSIXct.yearmon as.POSIXlt.yearmon > ### as.numeric.yearmon as.character.yearmon c.yearmon format.yearmon > ### print.yearmon [.yearmon axis.yearmon MATCH.yearmon Ops.yearmon > ### Keywords: ts > > ### ** Examples > > x <- yearmon(2000 + seq(0, 23)/12) > x [1] "Jan 2000" "Feb 2000" "Mar 2000" "Apr 2000" "May 2000" "Jun 2000" [7] "Jul 2000" "Aug 2000" "Sep 2000" "Oct 2000" "Nov 2000" "Dec 2000" [13] "Jan 2001" "Feb 2001" "Mar 2001" "Apr 2001" "May 2001" "Jun 2001" [19] "Jul 2001" "Aug 2001" "Sep 2001" "Oct 2001" "Nov 2001" "Dec 2001" > > # returned Date is the fraction of the way through > # the period given by frac (= 0 by default) > as.Date(x) [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" "2000-10-01" [11] "2000-11-01" "2000-12-01" "2001-01-01" "2001-02-01" "2001-03-01" [16] "2001-04-01" "2001-05-01" "2001-06-01" "2001-07-01" "2001-08-01" [21] "2001-09-01" "2001-10-01" "2001-11-01" "2001-12-01" > as.Date(x, 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" "2000-10-31" [11] "2000-11-30" "2000-12-31" "2001-01-31" "2001-02-28" "2001-03-31" [16] "2001-04-30" "2001-05-31" "2001-06-30" "2001-07-31" "2001-08-31" [21] "2001-09-30" "2001-10-31" "2001-11-30" "2001-12-31" > as.POSIXct(x) [1] "2000-01-01 01:00:00 CET" "2000-02-01 01:00:00 CET" [3] "2000-03-01 01:00:00 CET" "2000-04-01 02:00:00 CEST" [5] "2000-05-01 02:00:00 CEST" "2000-06-01 02:00:00 CEST" [7] "2000-07-01 02:00:00 CEST" "2000-08-01 02:00:00 CEST" [9] "2000-09-01 02:00:00 CEST" "2000-10-01 02:00:00 CEST" [11] "2000-11-01 01:00:00 CET" "2000-12-01 01:00:00 CET" [13] "2001-01-01 01:00:00 CET" "2001-02-01 01:00:00 CET" [15] "2001-03-01 01:00:00 CET" "2001-04-01 02:00:00 CEST" [17] "2001-05-01 02:00:00 CEST" "2001-06-01 02:00:00 CEST" [19] "2001-07-01 02:00:00 CEST" "2001-08-01 02:00:00 CEST" [21] "2001-09-01 02:00:00 CEST" "2001-10-01 02:00:00 CEST" [23] "2001-11-01 01:00:00 CET" "2001-12-01 01:00:00 CET" > > z <- zoo(rnorm(24), x, frequency = 12) > z Jan 2000 Feb 2000 Mar 2000 Apr 2000 May 2000 Jun 2000 -0.62645381 0.18364332 -0.83562861 1.59528080 0.32950777 -0.82046838 Jul 2000 Aug 2000 Sep 2000 Oct 2000 Nov 2000 Dec 2000 0.48742905 0.73832471 0.57578135 -0.30538839 1.51178117 0.38984324 Jan 2001 Feb 2001 Mar 2001 Apr 2001 May 2001 Jun 2001 -0.62124058 -2.21469989 1.12493092 -0.04493361 -0.01619026 0.94383621 Jul 2001 Aug 2001 Sep 2001 Oct 2001 Nov 2001 Dec 2001 0.82122120 0.59390132 0.91897737 0.78213630 0.07456498 -1.98935170 > as.ts(z) Jan Feb Mar Apr May Jun 2000 -0.62645381 0.18364332 -0.83562861 1.59528080 0.32950777 -0.82046838 2001 -0.62124058 -2.21469989 1.12493092 -0.04493361 -0.01619026 0.94383621 Jul Aug Sep Oct Nov Dec 2000 0.48742905 0.73832471 0.57578135 -0.30538839 1.51178117 0.38984324 2001 0.82122120 0.59390132 0.91897737 0.78213630 0.07456498 -1.98935170 > > > > cleanEx(); ..nameEx <- "yearqtr" > > ### * yearqtr > > flush(stderr()); flush(stdout()) > > ### Name: yearqtr > ### Title: An Index Class for Quaterly Data > ### Aliases: yearqtr as.yearqtr as.yearqtr.default as.yearqtr.numeric > ### as.yearqtr.integer as.yearqtr.dates as.yearqtr.Date as.yearqtr.POSIXt > ### as.Date.yearqtr as.POSIXct.yearqtr as.POSIXlt.yearqtr > ### as.numeric.yearqtr as.character.yearqtr c.yearqtr format.yearqtr > ### print.yearqtr [.yearqtr axis.yearqtr MATCH.yearqtr Ops.yearqtr > ### Keywords: ts > > ### ** Examples > > x <- yearqtr(2000 + seq(0, 7)/4) > x [1] "2000 Q1" "2000 Q2" "2000 Q3" "2000 Q4" "2001 Q1" "2001 Q2" "2001 Q3" [8] "2001 Q4" > > # returned Date is the fraction of the way through > # the period given by frac (= 0 by default) > as.Date(x) [1] "2000-01-01" "2000-04-01" "2000-07-01" "2000-10-01" "2001-01-01" [6] "2001-04-01" "2001-07-01" "2001-10-01" > as.Date(x, frac = 1) [1] "2000-03-31" "2000-06-30" "2000-09-30" "2000-12-31" "2001-03-31" [6] "2001-06-30" "2001-09-30" "2001-12-31" > as.POSIXct(x) [1] "2000-01-01 01:00:00 CET" "2000-04-01 02:00:00 CEST" [3] "2000-07-01 02:00:00 CEST" "2000-10-01 02:00:00 CEST" [5] "2001-01-01 01:00:00 CET" "2001-04-01 02:00:00 CEST" [7] "2001-07-01 02:00:00 CEST" "2001-10-01 02:00:00 CEST" > > z <- zoo(rnorm(8), x, frequency = 4) > z 2000 Q1 2000 Q2 2000 Q3 2000 Q4 2001 Q1 2001 Q2 2001 Q3 -0.6264538 0.1836433 -0.8356286 1.5952808 0.3295078 -0.8204684 0.4874291 2001 Q4 0.7383247 > as.ts(z) Qtr1 Qtr2 Qtr3 Qtr4 2000 -0.6264538 0.1836433 -0.8356286 1.5952808 2001 0.3295078 -0.8204684 0.4874291 0.7383247 > > > > cleanEx(); ..nameEx <- "zoo" > > ### * zoo > > flush(stderr()); flush(stdout()) > > ### Name: zoo > ### Title: Z's Ordered Observations > ### Aliases: zoo range.zoo print.zoo as.zoo.factor summary.zoo str.zoo > ### is.zoo [.zoo head.zoo tail.zoo Ops.zoo t.zoo cumsum.zoo cumprod.zoo > ### cummin.zoo cummax.zoo na.contiguous na.contiguous.data.frame > ### na.contiguous.list na.contiguous.default na.contiguous.zoo index2char > ### index2char.default index2char.numeric head.ts tail.ts > ### Keywords: ts > > ### ** Examples > > ## simple creation and plotting > x.Date <- as.Date("2003-02-01") + c(1, 3, 7, 9, 14) - 1 > x <- zoo(rnorm(5), x.Date) > plot(x) > time(x) [1] "2003-02-01" "2003-02-03" "2003-02-07" "2003-02-09" "2003-02-14" > > ## subsetting with numeric indexes > x[c(2, 4)] 2003-02-03 2003-02-09 0.1836433 1.5952808 > ## subsetting with index class > x[as.Date("2003-02-01") + c(2, 8)] 2003-02-03 2003-02-09 0.1836433 1.5952808 > > ## different classes of indexes/times can be used, e.g. numeric vector > x <- zoo(rnorm(5), c(1, 3, 7, 9, 14)) > ## subsetting with numeric indexes then uses observation numbers > x[c(2, 4)] 3 9 0.4874291 0.5757814 > ## subsetting with index class can be enforced by I() > x[I(c(3, 9))] 3 9 0.4874291 0.5757814 > > ## visualization > plot(x) > ## or POSIXct > y.POSIXct <- ISOdatetime(2003, 02, c(1, 3, 7, 9, 14), 0, 0, 0) > y <- zoo(rnorm(5), y.POSIXct) > plot(y) > > ## create a constant series > z <- zoo(1, seq(4)[-2]) > > ## create a 0 dimensional zoo series > z0 <- zoo(, 1:4) > > ## create a 2 dimensional zoo series > z2 <- zoo(matrix(1:12, 4, 3), as.Date("2003-01-01") + 0:3) > > ## create a factor zoo object > fz <- zoo(gl(2,5), as.Date("2004-01-01") + 0:9) > > ## create a zoo series with 0 columns > z20 <- zoo(matrix(nrow = 4, ncol = 0), 1:4) > > ## arithmetic on zoo ojbects intersects them first > x1 <- zoo(1:5, 1:5) > x2 <- zoo(2:6, 2:6) > 10 * x1 + x2 2 3 4 5 22 33 44 55 > > > > > cleanEx(); ..nameEx <- "zooreg" > > ### * zooreg > > flush(stderr()); flush(stdout()) > > ### Name: zooreg > ### Title: Regular zoo Series > ### Aliases: zooreg frequency.zooreg frequency.zoo deltat.zooreg deltat.zoo > ### cycle.zooreg cycle.zoo as.zooreg as.zooreg.default as.zooreg.ts > ### as.ts.zooreg as.zoo.zooreg as.zooreg.zoo index<-.zooreg time<-.zooreg > ### lag.zooreg > ### Keywords: ts > > ### ** Examples > > ## equivalent specifications of a quarterly series > ## starting in the second quarter of 1959. > zooreg(1:10, frequency = 4, start = c(1959, 2)) 1959(2) 1959(3) 1959(4) 1960(1) 1960(2) 1960(3) 1960(4) 1961(1) 1961(2) 1961(3) 1 2 3 4 5 6 7 8 9 10 > as.zoo(ts(1:10, frequency = 4, start = c(1959, 2))) 1959(2) 1959(3) 1959(4) 1960(1) 1960(2) 1960(3) 1960(4) 1961(1) 1961(2) 1961(3) 1 2 3 4 5 6 7 8 9 10 > zoo(1:10, seq(1959.25, 1961.5, by = 0.25), frequency = 4) 1959(2) 1959(3) 1959(4) 1960(1) 1960(2) 1960(3) 1960(4) 1961(1) 1961(2) 1961(3) 1 2 3 4 5 6 7 8 9 10 > > ## use yearqtr class for indexing the same series > z <- zoo(1:10, yearqtr(seq(1959.25, 1961.5, by = 0.25)), frequency = 4) > z 1959 Q2 1959 Q3 1959 Q4 1960 Q1 1960 Q2 1960 Q3 1960 Q4 1961 Q1 1961 Q2 1961 Q3 1 2 3 4 5 6 7 8 9 10 > z[-(3:4)] 1959 Q2 1959 Q3 1960 Q2 1960 Q3 1960 Q4 1961 Q1 1961 Q2 1961 Q3 1 2 5 6 7 8 9 10 > > ## create a regular series with a "Date" index > zooreg(1:5, start = Sys.Date()) 2005-07-13 2005-07-14 2005-07-15 2005-07-16 2005-07-17 1 2 3 4 5 > ## or with "yearmon" index > zooreg(1:5, end = yearmon(2000)) Jan 1996 Jan 1997 Jan 1998 Jan 1999 Jan 2000 1 2 3 4 5 > > ## standard methods available for regular series > frequency(z) [1] 4 > deltat(z) [1] 0.25 > cycle(z) 1959 Q2 1959 Q3 1959 Q4 1960 Q1 1960 Q2 1960 Q3 1960 Q4 1961 Q1 1961 Q2 1961 Q3 2 3 4 1 2 3 4 1 2 3 > cycle(z[-(3:4)]) 1959 Q2 1959 Q3 1960 Q2 1960 Q3 1960 Q4 1961 Q1 1961 Q2 1961 Q3 2 3 2 3 4 1 2 3 > > > > ### *