使用quantmod库访问R中的环境对象

时间:2021-10-15 23:22:53

I have some experience in R but so far never used my own environment. Over the last months I had to use my own environment from time to time and I have some questions about it.

我有一些R方面的经验,但到目前为止还没有使用过我自己的环境。在过去的几个月里,我不得不时不时地使用我自己的环境,我对此有一些疑问。

  • The main reason for using an environment as "data-container" is because it is much faster then others, right? For example, using the quantmod why do we load OHLC objects into an environment by using getSymbols?
  • 使用环境作为“数据容器”的主要原因是它比其他的更快,对吗?例如,使用quantmod为什么我们使用getSymbols将OHLC对象加载到一个环境中?

Having such a new environment coming from getSymbols, i.e.

有这样一个来自getSymbols的新环境。

tick = c( "VNO" , "VMC" , "WMT" , "WAG") 
data.env <- new.env()    start<-as.Date("2013-01-01")   
getSymbols(tick,env=data.env,src="yahoo",from=start)

How can we efficiently access members in data.env, e.g. if we want to check if all members in data.env are OHLC objects can we do something like: is.OHLC(data[[all elements]])?

如何有效地访问数据中的成员。env,例如,如果我们想检查数据中的所有成员。env是OHLC对象我们能做些什么像:is。OHLC(数据[[所有元素]])?

Moreover, for the above examples of data, how can I calculate the Value at Risk for such a portfolio. My idea was to us the function VaR from the PerformanceAnalytics library. The help page of VaR says as argument: "an xts, vector, matrix, data frame, timeSeries or zoo object of asset returns". For this purpose I wanted to use dailyReturn to get the returns, which then can be used for the function VaR. However dailyReturn needs an OHLC object as argument. Hence I can calculate VaR as soon as I know how to turn the elements in data.env to a OHLC matrix

此外,对于上述数据示例,如何计算这样一个投资组合的风险值。我的想法是用PerformanceAnalytics库中的函数VaR。VaR的帮助页面表示为参数:“一个xts、向量、矩阵、数据帧、时间序列或资产返回的zoo对象”。为此,我想使用dailyReturn来获取返回值,然后可以用于函数VaR。但是dailyReturn需要一个OHLC对象作为参数。因此,一旦我知道如何在数据中转换元素,我就可以计算VaR。env到OHLC矩阵

2 个解决方案

#1


2  

The main reason for using an environment as "data-container" is because it is much faster then others, right?

使用环境作为“数据容器”的主要原因是它比其他环境快得多,对吗?

No, I think the main reason is to keep things clean and avoid overwriting objects in your global environment. (In addition, to have all "new" objects as a "bundle" instead of several separate objects. For this purpose, a list would serve as well but see below for the differences between lists and environments.) With your call to getSymbols, four new objects are created:

不,我认为主要的原因是保持事物整洁,避免在全局环境中覆盖对象。(另外,将所有“新”对象作为“包”,而不是多个单独的对象。出于这个目的,列表也可以发挥作用,但是请参阅下面列表和环境之间的差异。通过调用getSymbols,创建了四个新对象:

"VMC" "VNO" "WAG" "WMT"

If these objects were created in your global environment (aka workspace), any pre-existing objects with the same names would be overwritten. (This happens when you use load ... for example, you have x in your workspace and also saved in a file named "x.rda". After load("x.rda") the previous version of x in your workspace is lost.)

如果这些对象是在全局环境(即工作区)中创建的,则将覆盖具有相同名称的任何已存在对象。(这发生在你使用load时……)例如,您的工作区中有x,并且还保存在一个名为“x.rda”的文件中。在加载(“x.rda”)后,工作空间中的x的前一个版本将丢失。

Environments are not like ordinary R objects - because you can have different names pointing to the same environment. For example, if x is any kind of object in R, then assigning y <- x creates a copy of x. Not with environments:

环境不像普通的R对象——因为可以有不同的名称指向同一个环境。例如,如果x是R中的任何类型的对象,那么分配y <- x会创建一个x的副本,而不是环境:

> ls(data.env)
[1] "VMC" "VNO" "WAG" "WMT"
> yummy <- data.env
> yummy$x <- "bingo"
> ls(data.env)
[1] "VMC" "VNO" "WAG" "WMT" "x"  
> data.env$x
[1] "bingo"
> data.env$x <- "Florida"
> yummy$x
[1] "Florida"
> with(yummy, rm(x))
> ls(data.env)
[1] "VMC" "VNO" "WAG" "WMT"

Here when we assign yummy <- data.env we do not create a copy of data.env but just another pointer to the same object. So when we do something in yummy the same will happen in data.env.

当我们分配yummy <- data时。env我们不创建数据的拷贝。只是指向同一个对象的另一个指针。当我们用yummy做某事时,同样的事情也会发生在data.env中。

How can we efficiently access members in data.env, e.g. if we want to check if all members in data.env are OHLC objects can we do something like: is.OHLC(data[[all elements]])?

如何有效地访问数据中的成员。env,例如,如果我们想检查数据中的所有成员。env是OHLC对象我们能做些什么像:is。OHLC(数据[[所有元素]])?

As suggested above, eapply is the answer, - use unlist for converting the resulting list to avector if you like:

如上所述,eapply是答案,如果您愿意,可以使用unlist将结果列表转换为avector:

unlist(eapply(data.env, is.OHLC))

Finally, you can fetch the objects directly to your workspace like this:

最后,您可以直接将对象获取到您的工作区中,如下所示:

getSymbols(tick,env=.GlobalEnv,src="yahoo",from=start)

This is probably not slower for most purposes but the downside is that you can't then easily access the four new objects together (for example, deleting them, or eapply'ing something). And you will have to make sure that you have nothing worthwhile in your workspace before you do it, because, potentially, everything can be overwritten.

这在大多数情况下可能不会更慢,但缺点是您不能很容易地一起访问四个新对象(例如,删除它们,或者eapply'ing)。你必须确保你的工作空间中没有任何有价值的东西,因为,很有可能,所有的东西都可以被覆盖。

#2


1  

As Roman suggested the easiest method to check if a certain object in your data environment is an OHLC object is by using eapply like so:

正如Roman所建议的,检查数据环境中的某个对象是否是OHLC对象的最简单方法是使用eapply,如下所示:

eapply(data.env,is.OHLC)

which returns a list of your objects in your data environment and a logical value if it is a OHLC object or not. If you want to see a more compact form you can wrap "str" around the expression. For example to see which of the objects in my current data environment are OHLC-objects I can do the following:

返回数据环境中对象的列表,如果是OHLC对象,则返回逻辑值。如果你想看到一个更紧凑的形式,你可以用“str”环绕这个表达式。例如,要查看当前数据环境中的哪些对象是ohlc对象,我可以执行以下操作:

> str(eapply(data,is.OHLC))
List of 9
 $ VXZlong        : logi TRUE
 $ prices         : logi FALSE
 $ XIVlong        : logi TRUE
 $ dates          : logi FALSE
 $ VXXlong        : logi TRUE
 $ weight         : logi FALSE
 $ ZIVlong        : logi TRUE
 $ symbolnames    : logi FALSE
 $ execution.price: logi FALSE

#1


2  

The main reason for using an environment as "data-container" is because it is much faster then others, right?

使用环境作为“数据容器”的主要原因是它比其他环境快得多,对吗?

No, I think the main reason is to keep things clean and avoid overwriting objects in your global environment. (In addition, to have all "new" objects as a "bundle" instead of several separate objects. For this purpose, a list would serve as well but see below for the differences between lists and environments.) With your call to getSymbols, four new objects are created:

不,我认为主要的原因是保持事物整洁,避免在全局环境中覆盖对象。(另外,将所有“新”对象作为“包”,而不是多个单独的对象。出于这个目的,列表也可以发挥作用,但是请参阅下面列表和环境之间的差异。通过调用getSymbols,创建了四个新对象:

"VMC" "VNO" "WAG" "WMT"

If these objects were created in your global environment (aka workspace), any pre-existing objects with the same names would be overwritten. (This happens when you use load ... for example, you have x in your workspace and also saved in a file named "x.rda". After load("x.rda") the previous version of x in your workspace is lost.)

如果这些对象是在全局环境(即工作区)中创建的,则将覆盖具有相同名称的任何已存在对象。(这发生在你使用load时……)例如,您的工作区中有x,并且还保存在一个名为“x.rda”的文件中。在加载(“x.rda”)后,工作空间中的x的前一个版本将丢失。

Environments are not like ordinary R objects - because you can have different names pointing to the same environment. For example, if x is any kind of object in R, then assigning y <- x creates a copy of x. Not with environments:

环境不像普通的R对象——因为可以有不同的名称指向同一个环境。例如,如果x是R中的任何类型的对象,那么分配y <- x会创建一个x的副本,而不是环境:

> ls(data.env)
[1] "VMC" "VNO" "WAG" "WMT"
> yummy <- data.env
> yummy$x <- "bingo"
> ls(data.env)
[1] "VMC" "VNO" "WAG" "WMT" "x"  
> data.env$x
[1] "bingo"
> data.env$x <- "Florida"
> yummy$x
[1] "Florida"
> with(yummy, rm(x))
> ls(data.env)
[1] "VMC" "VNO" "WAG" "WMT"

Here when we assign yummy <- data.env we do not create a copy of data.env but just another pointer to the same object. So when we do something in yummy the same will happen in data.env.

当我们分配yummy <- data时。env我们不创建数据的拷贝。只是指向同一个对象的另一个指针。当我们用yummy做某事时,同样的事情也会发生在data.env中。

How can we efficiently access members in data.env, e.g. if we want to check if all members in data.env are OHLC objects can we do something like: is.OHLC(data[[all elements]])?

如何有效地访问数据中的成员。env,例如,如果我们想检查数据中的所有成员。env是OHLC对象我们能做些什么像:is。OHLC(数据[[所有元素]])?

As suggested above, eapply is the answer, - use unlist for converting the resulting list to avector if you like:

如上所述,eapply是答案,如果您愿意,可以使用unlist将结果列表转换为avector:

unlist(eapply(data.env, is.OHLC))

Finally, you can fetch the objects directly to your workspace like this:

最后,您可以直接将对象获取到您的工作区中,如下所示:

getSymbols(tick,env=.GlobalEnv,src="yahoo",from=start)

This is probably not slower for most purposes but the downside is that you can't then easily access the four new objects together (for example, deleting them, or eapply'ing something). And you will have to make sure that you have nothing worthwhile in your workspace before you do it, because, potentially, everything can be overwritten.

这在大多数情况下可能不会更慢,但缺点是您不能很容易地一起访问四个新对象(例如,删除它们,或者eapply'ing)。你必须确保你的工作空间中没有任何有价值的东西,因为,很有可能,所有的东西都可以被覆盖。

#2


1  

As Roman suggested the easiest method to check if a certain object in your data environment is an OHLC object is by using eapply like so:

正如Roman所建议的,检查数据环境中的某个对象是否是OHLC对象的最简单方法是使用eapply,如下所示:

eapply(data.env,is.OHLC)

which returns a list of your objects in your data environment and a logical value if it is a OHLC object or not. If you want to see a more compact form you can wrap "str" around the expression. For example to see which of the objects in my current data environment are OHLC-objects I can do the following:

返回数据环境中对象的列表,如果是OHLC对象,则返回逻辑值。如果你想看到一个更紧凑的形式,你可以用“str”环绕这个表达式。例如,要查看当前数据环境中的哪些对象是ohlc对象,我可以执行以下操作:

> str(eapply(data,is.OHLC))
List of 9
 $ VXZlong        : logi TRUE
 $ prices         : logi FALSE
 $ XIVlong        : logi TRUE
 $ dates          : logi FALSE
 $ VXXlong        : logi TRUE
 $ weight         : logi FALSE
 $ ZIVlong        : logi TRUE
 $ symbolnames    : logi FALSE
 $ execution.price: logi FALSE