简体   繁体   English

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

[英]Accessing objects of environment in R using quantmod library

I have some experience in R but so far never used my own environment. 我有一些R的经验,但到目前为止从未使用过我自己的环境。 Over the last months I had to use my own environment from time to time and I have some questions about it. 在过去的几个月里,我不得不经常使用自己的环境,我对它有一些疑问。

  • 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 , ie 拥有来自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, eg if we want to check if all members in data.env are OHLC objects can we do something like: is.OHLC(data[[all elements]]) ? 我们如何有效地访问data.env中的成员,例如,如果我们想检查data.env中的所有成员是否是OHLC对象,我们可以执行以下操作: is.OHLC(data[[all elements]])

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. 我的想法是来自PerformanceAnalytics库的函数VaR The help page of VaR says as argument: "an xts, vector, matrix, data frame, timeSeries or zoo object of asset returns". VaR的帮助页面称为“xts,vector,matrix,dataframe,timeSeries或zoo对象的资产返回”。 For this purpose I wanted to use dailyReturn to get the returns, which then can be used for the function VaR . 为此,我想使用dailyReturn来获取返回值,然后可以将其用于函数VaR However dailyReturn needs an OHLC object as argument. 但是, dailyReturn需要一个OHLC对象作为参数。 Hence I can calculate VaR as soon as I know how to turn the elements in data.env to a OHLC matrix 因此,只要我知道如何将data.env的元素data.env为OHLC矩阵,我就可以计算出VaR

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”的文件中。 load("x.rda") ,工作区中的x的先前版本是丢失。)

Environments are not like ordinary R objects - because you can have different names pointing to the same environment. 环境与普通的R对象不同 - 因为您可以使用指向同一环境的不同名称。 For example, if x is any kind of object in R, then assigning y <- x creates a copy of x. 例如,如果x是R中的任何类型的对象,则分配y <- x会创建y <- x的副本。 Not with environments: 不适用于环境:

> 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. 这里当我们分配yummy <- data.env我们不会创建data.env的副本,而是创建另一个指向同一对象的指针。 So when we do something in yummy the same will happen in data.env . 因此,当我们在yummy执行某些操作时, data.env也会发生同样的事情。

How can we efficiently access members in data.env, eg if we want to check if all members in data.env are OHLC objects can we do something like: is.OHLC(data[[all elements]])? 我们如何有效地访问data.env中的成员,例如,如果我们想检查data.env中的所有成员是否是OHLC对象,我们可以执行以下操作:is.OHLC(data [[all elements]])?

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). 对于大多数目的而言,这可能并不慢,但缺点是你不能轻易地一起访问这四个新对象(例如,删除它们,或者eapply某些东西)。 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. 在执行此操作之前,您必须确保在工作区中没有任何值得的东西,因为可能会覆盖所有内容。

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. 它返回数据环境中对象的列表,如果是OHLC对象则返回逻辑值。 If you want to see a more compact form you can wrap "str" around the expression. 如果你想看一个更紧凑的形式,你可以在表达式周围包含"str" For example to see which of the objects in my current data environment are OHLC-objects I can do the following: 例如,要查看当前数据环境中的哪些对象是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM