Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R XTS::请帮助我了解用法&;期间之间的差异。应用();至.期间(_R_Xts - Fatal编程技术网

R XTS::请帮助我了解用法&;期间之间的差异。应用();至.期间(

R XTS::请帮助我了解用法&;期间之间的差异。应用();至.期间(,r,xts,R,Xts,我正在学习R的时间序列分析,在学习过程中遇到了这两个函数。我知道这两个的输出都是周期频率定义的周期性数据,我能看到的唯一区别是to.period()中的OHLC输出选项 除OHLC外,当使用这些功能中的某一特定功能时?至.period和所有至.minutes、至.weekly、至.quarterly确实用于OHLC数据 如果将函数设置为.period,则该函数将从该时段的第一天开始计算,从该时段的最后一天结束计算,并从指定时段的最高/最低低点开始计算。这些函数与quantmod/tidyquan

我正在学习R的时间序列分析,在学习过程中遇到了这两个函数。我知道这两个的输出都是周期频率定义的周期性数据,我能看到的唯一区别是to.period()中的OHLC输出选项


除OHLC外,当使用这些功能中的某一特定功能时?

至.period
和所有至.minutes、至.weekly、至.quarterly确实用于OHLC数据

如果将函数
设置为.period
,则该函数将从该时段的第一天开始计算,从该时段的最后一天结束计算,并从指定时段的最高/最低低点开始计算。这些函数与quantmod/tidyquant/quantstrat包配合使用非常好。参见代码示例1

如果您提供to.period非OHLC数据,但提供一个包含1个数据列的timeseries,则仍然会返回某种OHLC。参见代码示例2

现在,
period.apply
更有趣。在这里,您可以提供自己的函数来应用于数据。特别是与端点结合使用时,如果希望将函数聚合到不同的时间段,这在timeseries数据中可能是一个强大的函数。索引主要是通过端点指定的,因为通过端点,您可以创建达到更高时间级别(从一天到一周等)所需的索引。请参见代码示例3和4

记住使用带有句点的矩阵函数。如果您有超过1列的数据,请应用,因为xts基本上是一个矩阵和一个索引。参见代码示例5

更多信息

库(xts)
数据(样本矩阵)
动物园数据
library(xts)

data(sample_matrix)
zoo.data <- zoo(rnorm(31)+10,as.Date(13514:13744,origin="1970-01-01"))


# code example 1
to.quarterly(sample_matrix)
        sample_matrix.Open sample_matrix.High sample_matrix.Low sample_matrix.Close
2007 Q1           50.03978           51.32342          48.23648            48.97490
2007 Q2           48.94407           50.33781          47.09144            47.76719

# same as to.quarterly
to.period(sample_matrix, period = "quarters")
        sample_matrix.Open sample_matrix.High sample_matrix.Low sample_matrix.Close
2007 Q1           50.03978           51.32342          48.23648            48.97490
2007 Q2           48.94407           50.33781          47.09144            47.76719


# code example 2
to.period(zoo.data, period = "quarters")
           zoo.data.Open zoo.data.High zoo.data.Low zoo.data.Close
2007-03-31      9.039875      11.31391     7.451139       10.35057
2007-06-30     10.834614      11.31391     7.451139       11.28427
2007-08-19     11.004465      11.31391     7.451139       11.30360

# code example 3 using base standard deviation in the chosen period
period.apply(zoo.data, endpoints(zoo.data, on = "quarters"), sd)
2007-03-31 2007-06-30 2007-08-19 
  1.026825   1.052786   1.071758 

# self defined function of summing x + x for the period
period.apply(zoo.data, endpoints(zoo.data, on = "quarters"), function(x) sum(x + x) )
2007-03-31 2007-06-30 2007-08-19 
 1798.7240  1812.4736   993.5729 

# code example 5
period.apply(sample_matrix, endpoints(sample_matrix, on = "quarters"), colMeans)
               Open     High      Low    Close
2007-03-31 50.15493 50.24838 50.05231 50.14677
2007-06-30 48.47278 48.56691 48.36606 48.45318