找回密码
 注册
查看: 3186|回复: 0

R语言 zoo包 zoo()函数中文帮助文档(中英文对照)

[复制链接]
发表于 2012-10-2 07:54:34 | 显示全部楼层 |阅读模式
zoo(zoo)
zoo()所属R语言包:zoo

                                        Z's Ordered Observations
                                         Z的有序观察

                                         译者:生物统计家园网 机器人LoveR

描述----------Description----------

zoo is the creator for an S3 class of indexed totally ordered observations which includes irregular time series.
zoo是的S3类索引的全序的观测值,其中包括不规则时间序列的创造者。


用法----------Usage----------


zoo(x = NULL, order.by = index(x), frequency = NULL)
## S3 method for class 'zoo'
print(x, style = , quote = FALSE, ...)



参数----------Arguments----------

参数:x
a numeric vector, matrix or a factor.
一个数值向量,矩阵或一个因素。


参数:order.by
an index vector with unique entries by which the observations in x are ordered. See the details for support of non-unique indexes.
索引向量具有独特的项目的意见x是有序的。支持非唯一索引的详细信息,请参阅。


参数:frequency
numeric indicating frequency of order.by. If specified, it is checked whether order.by and frequency comply. If so, a regular "zoo" series is returned, i.e., an object of class c("zooreg", "zoo"). See below and zooreg for more details.
数字表示频率的order.by。如果指定,检查是否order.by和frequency遵守。如果是这样,定期"zoo"系列被返回,即,一个对象的类c("zooreg", "zoo")。请参阅下面zooreg更多的细节。


参数:style
a string specifying the printing style which can be "horizontal" (the default for vectors), "vertical" (the default for matrices) or "plain" (which first prints the data and then the index).
一个字符串,指定打印样式可以是"horizontal"(默认为向量),"vertical"(默认为矩阵)或"plain"(先打印数据,然后索引)。


参数:quote
logical. Should characters be quoted?
逻辑。如果字符被引用吗?


参数:...
further arguments passed to the print methods of the data and the index.
进一步传递的参数的数据和索引的打印方法。


Details

详细信息----------Details----------

zoo provides infrastructure for ordered observations which are stored internally in a vector or matrix with an index attribute (of arbitrary class, see below).  The index must have the same length as NROW(x) except in the  case of a zero length numeric vector in which case the index length can be any length. Emphasis has been given to make all methods independent of the index/time class (given in order.by). In principle, the data x could also be arbitrary, but currently there is only support for vectors and matrices and partial support for factors.
zoo提供基础设施,为有序存储在内部的索引属性(任意类的向量或矩阵的意见,见下文)。该指数必须具有相同的长度NROW(x)一个零长度,在这种情况下,索引的长度可以是任意长度的数值向量的情况下除外。目前的重点是使所有的方法,独立的索引/时间类(order.by)。的数据x的原则,也可以是任意的,但目前还只支持向量和矩阵的部分支持因素。

zoo is particularly aimed at irregular time series of numeric vectors/matrices, but it also supports regular time series (i.e., series with a certain frequency). zoo's key design goals are independence of a particular index/date/time class and consistency  with ts and base R by providing methods to standard generics. Therefore, standard functions can be used to work with "zoo" objects and  memorization of new commands is reduced.
zoo,特别是在不规则时间序列的数字向量/矩阵,但它也支持常规的时间序列(即一定frequency系列)。 zoo的主要设计目标是一个特定的索引/日期/时间类和一致性ts和碱基r的方法提供标准的仿制药的独立性。因此,标准的功能,可以使用工作"zoo"对象和记忆新的命令是减少了。

When creating a "zoo" object with the function zoo, the vector of indexes order.by can be of (a single) arbitrary class (if x is shorter or longer than order.by it is expanded accordingly), but it is essential that ORDER(order.by) works. For other functions it is assumed that c(), length(), MATCH() and subsetting [, work. If this is not the case for a particular index/date/time class, then methods for these  generic functions should be created by the user. Note, that to achieve this, new generic functions ORDER and MATCH are created in the zoo package with default methods corresponding to the non-generic base functions order  and match. Note that the order  and hence the default ORDER typically work if there is a xtfrm method. Furthermore, for certain (but not for all) operations the index class should have an as.numeric method (in  particular for regular series) and an as.character method might improve printed output (see also below).
当创建一个"zoo"对象的功能zoo,索引向量order.by可以是(单)任意类(如果x是短于或长于order.by它相应扩展),但它是必不可少的,ORDER(order.by)工程。对于其它功能,它假定这c(),length(),MATCH()和子集[,工作。如果这不是一个特定的索引/日期/时间类的情况下,那么这些通用功能的方法应该被由用户创建的。请注意,要实现这一目标,新的通用功能ORDER和MATCHzoo包中创建默认的方法对应的非泛型的基本功能order和match。请注意,order,因此默认ORDER的工作通常如果有一个xtfrm方法。此外,对于某些(但不是所有)操作的指数类应该有一个as.numeric方法(特别是常规系列)和一个as.character方法可能会提高打印输出(见下文)。

The index observations order.by should typically be unique, such that the observations can be totally ordered. Nevertheless, zoo() is able to create "zoo" objects with duplicated indexes (with a warning) and simple methods such as plot() or summary() will typically work for such objects. However, this is not formally supported as the bulk of functionality provided in zoo requires unique index observations/time stamps. See below for an example how to remove duplicated indexes.
指数观测order.by通常应该是唯一的,例如,可以完全排列的观测。然而,zoo()创建"zoo"对象的复制指标(警告)和简单的方法,如plot()或summary()通常会这样的对象。然而,这并不正式支持的大部分功能提供zoo需要独特的指数观察/时间戳。请参阅下面的一个例子如何删除重复的索引。

If a frequency is specified when creating a series via zoo, the object returned is actually of class "zooreg" which inherits from "zoo". This is a subclass of "zoo" which relies on having a "zoo" series with an additional "frequency" attribute (which has to comply with the index of that series). Regular "zooreg" series can also be created by zooreg, the zoo analogue of ts. See the  respective help page and is.regular for further details.
如果一个frequency创建时指定了一系列通过zoo,返回的对象实际上是类"zooreg"继承"zoo"。这是一个子类"zoo"依赖于一个"zoo"系列的一个额外的"frequency"属性(其中有遵守该系列指数)。 "zooreg"系列也可以创建zooreg,zoo的ts模拟。请参阅相应的帮助页面,并is.regular作进一步的细节。

Methods to standard generics for "zoo" objects currently include: print (see above), summary, str, head, tail, [ (subsetting), rbind, cbind, merge (see merge.zoo), aggregate (see aggregate.zoo), rev, split (see aggregate.zoo), barplot, plot and lines (see plot.zoo). For multivariate "zoo" series with column names the $ extractor is available, behaving similar as for "data.frame" objects.  Methods are also available for median and quantile.
方法标准的仿制药"zoo"对象包括:print(见上文),summary,str,head,tail, X>(子集),[,rbind,cbind(见merge)merge.zoo(见aggregate)<X >,aggregate.zoo(rev)split,aggregate.zoo和barplot(见plot)。多元lines系列列名plot.zoo提取器是,行为类似于"zoo"对象的。方法也可用于$和"data.frame"。

ifelse.zoo is not a method (because ifelse is not a generic)  but must be written out including the .zoo suffix.
ifelse.zoo是不是一个方法(因为ifelse是不是一个通用的),但必须是书面的,包括.zoo后缀。

To &ldquo;prettify&rdquo; printed output of "zoo" series the generic  function index2char is used for turning index values into character values. It defaults to using as.character but can be customized if a different printed display should be used (although this should not be necessary, usually).
为了“美化”"zoo"系列通用功能index2char用于将指数值转换为字符值的打印输出。它默认使用as.character但如果应使用不同的印刷显示(虽然这不应该是必要的,通常情况下),可定制的。

The subsetting method [ work essentially like the corresponding functions for vectors or matrices respectively, i.e., takes indexes of type "numeric", "integer" or "logical". But additionally, it can be used to index with observations from the index class of the series. If the index class of the series is one of the three classes above, the corresponding index has to be encapsulated in I() to enforce usage of the index class (see examples).   Subscripting by a zoo object whose data contains logical values is undefined.
子集的方法[工作基本上是一样的向量或矩阵相应的功能,即指标类型"numeric","integer"或"logical"。但此外,它可以用于索引从索引类的系列与观测。如果索引类的系列产品之一以上三类,对应的索引被封装在I()执行的索引类的用法(参见示例)。下标由动物园的对象,其数据包含逻辑值是不确定的。

Additionally, zoo provides several generic functions and methods to work (a) on the data contained in a "zoo" object, (b) the index (or time) attribute associated to it, and (c) on both data and index:
此外,zoo提供了一些通用的功能和工作方法(一)上的数据包含在"zoo"对象,(b)指数(或时间)相关联的属性,它和(c)于数据和索引:

(a) The data contained in "zoo" objects can be extracted by coredata (strips off all "zoo"-specific attributes) and modified using coredata<-. Both are new generic functions with methods for "zoo" objects, see coredata.
(a)数据包含在"zoo"对象可以提取coredata(带关闭所有"zoo"特定的属性)和修改的使用coredata<-,。两者都是新的通用功能的方法"zoo"对象,coredata。

(b) The index associated with a "zoo" object can be extracted by index and modified by index<-. As the interpretation of the index as &ldquo;time&rdquo; in time series applications is more natural, there are also synonymous methods time and time<-. The start and the end of the index/time vector can be queried by start and end. See index.
(b)指数与一个"zoo"对象可以提取index和index<-修改。由于指数的解释“时间”在时间序列中的应用更自然,也有同义的方法time和time<-。开始和结束的索引/时间向量可以查询start和end。见index。

(c) To work on both data and index/time, zoo provides methods lag, diff (see lag.zoo) and window,  window<- (see window.zoo).
(三)对数据和索引/时间,zoo提供的方法lag,diff(见lag.zoo)window,window<- (见window.zoo)。

In addition to standard group generic function (see Ops), the following mathematical operations are available as methods for "zoo" objects: transpose t which coerces to a matrix  first, and cumsum, cumprod, cummin, cummax which are applied column wise.
在除了标准组泛型函数(参见Ops),以下的数学运算提供作为"zoo"对象的方法,:移调t胁迫为一个矩阵,第一,和cumsum ,cumprod,cummin,cummax列明智的。

Coercion to and from "zoo" objects is available for objects of various classes, in particular "ts", "irts" and "its" objects can be coerced to "zoo", the reverse is available for "its" and for "irts" (the latter in package tseries). Furthermore, "zoo" objects can be coerced to vectors, matrices and lists and data frames (dropping the index/time attribute). See as.zoo.
胁迫和"zoo"对象是可用于各种类的对象,尤其是"ts","irts"和"its"对象可以强制转换为"zoo",反向可用为"its"和"irts"(后者在包tseries)。此外,"zoo"对象可以强制转换为向量,矩阵,列表和数据框(删除索引/时间属性)。见as.zoo。

Seven methods are available for NA handling in the data of  "zoo" objects:  na.approx which uses linear interpolation to fill  in NA values.  na.contiguous which extracts the longest consecutive  stretch of non-missing values in a "zoo" object, na.locf which replaces NAs by the last previous non-NA,  na.omit which returns a "zoo" object with incomplete observations removed, na.aggregate which uses group means to fill in NA values, na.spline which uses spline interpolation to fill  in NA values and na.trim which trims runs of NAs off the beginning and end but not in the interior.  An 8th NA routine can be found in the stinepack package where na.stinterp which   performs Stineman interpolation.
七个方法可用于NA处理中的数据"zoo"对象:na.approx使用线性内插填写NA值。 na.contiguous,提取最长连续拉伸的非缺失值在"zoo"对象,na.locf取代NA的前面的最后一个非NA, na.omit返回一个"zoo"不完整的观测对象被删除,na.aggregate使用组填写NA值,na.spline它采用样条插值填补NA的价值观和na.trim修剪运行NA的关闭的开头和结尾,但不是在室内。的第8 NA程序可以发现在stinepack包na.stinterp执行Stineman插的。

A typical task to be performed on ordered observations is to evaluate some function, e.g., computing the mean, in a window of observations that is moved over the full sample period. The generic function rollapply provides this functionality for arbitrary functions and more efficient versions rollmean, rollmax, rollmedian are available for the mean, maximum and median respectively.
一个典型的任务进行有序的观察是,在一个窗口中移动在整个样本期间的观察,评估一些功能,例如,计算平均。通用功能rollapply提供了这个功能对任意的功能和更高效的版本rollmean,rollmax,rollmedian平均值,最大值和中位数分别。

The zoo package has an as.Date numeric method which is similar to the one in the core of R except that the origin argument defaults to January 1, 1970 (whereas the one in the core of R has no default).
zoo包as.DatenumericR的核心,除了一个类似的方法,这是origin参数默认为1970年1月1日(而其中一个在核心的R有没有默认值)。

Note that since zoo uses date/time classes from base R and  other packages, it may inherit bugs or problems with those date/time classes. Currently, there is one such known problem with the c method for the POSIXct class in base R: If x and y are POSIXct objects with tzone attributes, the attribute will always be dropped in c(x, y), even if it is the same across both x and y. Although this is documented at c.POSIXct, one may want to employ a workaround as shown at https://stat.ethz.ch/pipermail/r-devel/2010-August/058112.html.
请注意,由于zoo使用日期/时间基础R和其他包类,它继承与日期/时间类的错误或问题。目前,有这样的一个已知的问题cPOSIXct类碱基r的方法:如果x和y是POSIXct的对象 X>属性,该属性将始终被丢弃在tzone,即使是相同的跨c(x, y)和x。虽然这是记录在y,可能要采用的一种替代方法在https://stat.ethz.ch/pipermail/r-devel/2010-August/058112.html显示。


值----------Value----------

A vector or matrix with an "index" attribute of the same dimension (NROW(x)) by which x is ordered.
一个向量或矩阵的"index"属性相同的尺寸(NROW(x))x是有序的。


参考文献----------References----------

<code>zoo</code>: S3 Infrastructure for Regular and Irregular Time Series. Journal of Statistical Software, 14(6), 1-27. URL http://www.jstatsoft.org/v14/i06/ and available as  <code>vignette("zoo")</code>.
<code>zoo</code> Quick Reference. Package vignette available as <code>vignette("zoo-quickref")</code>.  

参见----------See Also----------

zooreg, plot.zoo, index, merge.zoo
zooreg,plot.zoo,index,merge.zoo


实例----------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)

## subsetting with numeric indexes[#用数字索引的子集]
x[c(2, 4)]
## subsetting with index class[#指数类的子集]
x[as.Date("2003-02-01") + c(2, 8)]

## 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)]
## subsetting with index class can be enforced by I()[#指数类的子集,可以强制执行I()]
x[I(c(3, 9))]

## visualization[#可视化]
plot(x)
## or POSIXct[#或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[#创建一个0维的动物园系列]
z0 <- zoo(, 1:4)

## create a 2-dimensional zoo series[#创建一个2维的动物园系列]
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[#建立一个动物园系列0列]
z20 <- zoo(matrix(nrow = 4, ncol = 0), 1:4)

## arithmetic on zoo objects intersects them first[#动物园对象的算术相交他们第一]
x1 <- zoo(1:5, 1:5)
x2 <- zoo(2:6, 2:6)
10 * x1 + x2

## $ extractor for multivariate zoo series with column names[#$提取多元动物园与列名]
z <- zoo(cbind(foo = rnorm(5), bar = rnorm(5)))
z$foo
z$xyz <- zoo(rnorm(3), 2:4)
z

## add comments to a zoo object[#注释添加到动物园的对象]
comment(x1) <- c("This is a very simple example of a zoo object.",
  "It can be recreated using this R code: example(zoo)")
## comments are not output by default but are still there[#注释默认情况下,不输出,但仍然存在]
x1
comment(x1)

# ifelse does not work with zoo but this works[ifelse不与动物园工作,但这个工程]
# to create a zoo object which equals x1 at[创建一个动物园的对象,等于X1]
# time i if x1[i] &gt; x1[i-1] and 0 otherwise[一次我如果x1 [一]> X1 [I-1,否则为0]
(diff(x1) > 0) * x1

## zoo series with duplicated indexes[#动物园系列复制索引]
z3 <- zoo(1:8, c(1, 2, 2, 2, 3, 4, 5, 5))
plot(z3)
## remove duplicated indexes by averaging[#删除重复的平均指标]
lines(aggregate(z3, index(z3), mean), col = 2)
## or by using the last observation[#或使用最后一个观察]
lines(aggregate(z3, index(z3), tail, 1), col = 4)

## x1[x1 &gt; 3] is not officially supported since[#X1 [X1> 3]没有正式开始支持]
## x1 &gt; 3 is of class "zoo", not "logical".[#X1> 3类“动物园”,而不是“逻辑”。]
## Use one of these instead:[#使用其中的一个,而不是:]
x1[which(x1 > 3)]
x1[coredata(x1 > 3)]
x1[as.logical(x1 > 3)]
subset(x1, x1 > 3)

## any class supporting the methods discussed can be used[#支持的任何类,可以用讨论的方法]
## as an index class. Here are examples using complex numbers[#索引类。下面的例子使用复杂的数字]
## and letters as the time class.[#类和字母的时间。]

z4 <- zoo(11:15, complex(real = c(1, 3, 4, 5, 6), imag = c(0, 1, 0, 0, 1)))
merge(z4, lag(z4))

z5 <- zoo(11:15, letters[1:5])
merge(z5, lag(z5))

# index values relative to 2001Q1[指数值相对2001Q1]
zz <- zooreg(cbind(a = 1:10, b = 11:20), start = as.yearqtr(2000), freq = 4)
zz[] <- mapply("/", as.data.frame(zz), coredata(zz[as.yearqtr("2001Q1")]))


## even though time index must be unique zoo (and read.zoo)[#即使索引必须是唯一的动物园(和read.zoo)]
## will both allow creation of such illegal objects with[#将这些非法的对象都允许创建]
## a warning (rather than ana error) to give the user a [#警告(而不是模拟的错误),以给用户一个]
## chance to fix them up.  Extracting and replacing times[#机会来解决他们。提取和更换时间]
## and aggregate.zoo will still work.[#aggregate.zoo仍然可以工作。]
## Not run: [#不运行:]
# this gives a warning[这给出了一个警告]
# and then creates an illegal zoo object[然后创建一个非法的动物园对象]
z6 <- zoo(11:15, c(1, 1, 2, 2, 5))
z6

# fix it up by averaging duplicates[修复它的平均重复]
aggregate(z6, identity, mean)

# or, fix it up by taking last in each set of duplicates[,或者修复它以每个重复集的最后]
aggregate(z6, identity, tail, 1)

# fix it up via interpolation of duplicate times[修复它通过插值重复]
time(z6) <- na.approx(ifelse(duplicated(time(z6)), NA, time(z6)), na.rm = FALSE)
# if there is a run of equal times at end they[如果有一个运行相同的时间结束时,他们]
# wind up as NAs and we cannot have NA times[风NAS和我们不能有NA倍]
z6 <- z6[!is.na(time(z6))]
z6

x1. <- x1 <- zoo (matrix (1:12, nrow = 3), as.Date("2008-08-01") + 0:2)
colnames (x1) <- c ("A", "B", "C", "D")
x2 <- zoo (matrix (1:12, nrow = 3), as.Date("2008-08-01") + 1:3)
colnames (x2) <- c ("B", "C", "D", "E")

both.dates = as.Date (intersect (index (t1), index (t2)))
both.cols = intersect (colnames (t1), colnames (t2))

x1[both.dates, both.cols]
## there is "[.zoo" but no "[&lt;-.zoo" however four of the following[#有“动物园”,但没有“< - 动物园”,但以下四个]
## five examples work[#5例子工作]

## wrong[#错误]
## x1[both.dates, both.cols] &lt;- x2[both.dates, both.cols][#X1 [both.dates,both.cols] < -  X2 [both.dates,both.cols]]

# 4 correct alternatives[4正确的选择]
# #1[#1]
window(x1, both.dates)[, both.cols] <- x2[both.dates, both.cols]

# #2. restore x1 and show a different way[#2。恢复x1和显示以不同的方式]
x1 <- x1.
window(x1, both.dates)[, both.cols] <- window(x2, both.dates)[, both.cols]

# #3. restore x1 and show a different way[#3。恢复x1和显示以不同的方式]
x1 <- x1.
x1[time(x1)

# #4. restore x1 and show a different way[#4。恢复x1和显示以不同的方式]
x1 <- x1.
x1[time(x1)


## End(Not run)[#(不执行)]


转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。


注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|生物统计家园 网站价格

GMT+8, 2024-11-23 22:42 , Processed in 0.025490 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表