关于如何在 xts 对象中获取特定元素的简单问题 [英] Simple Questions on how to get specific elements in a xts object

查看:19
本文介绍了关于如何在 xts 对象中获取特定元素的简单问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相对较大的 xts 对象.它包含欧洲 STOXX 600 指数中每家公司 2012 年至 2021 年的每日调整后收盘价.我想计算每家公司每年股票的年度波动率.例如,我想计算Covestro AG"公司股票的波动性.从 01.01.2012 - 31.12.2012 到 2021 年的每一年,因此Covestro AG"的年度股票波动率从 2012 年到 2021 年,这适用于在欧洲 STOXX 600 指数中上市的 600 家公司.

I have a relative large xts object. It contains the daily adjusted closing prices from 2012-2021 for each company in the STOXX 600 Europe. I want to calculate the yearly volatility of the stocks for each year for each company. So for example I want to calculate the volatility of the stocks of the company "Covestro AG" from 01.01.2012 - 31.12.2012 and that's for every year till 2021, so the yearly stock volatility of "Covestro AG" from 2012 till 2021 and that's for every of the 600 companies that are listed in the STOXX 600 Europe.

所以首先我开始计算对数差异:

So first I started to calculate the log differences by:

XTS.LOGDIFFS <- diff(log(XTS.ADJCLOSE))

因此,下一步是计算每家公司 1 年特定期间(例如 01.01.2012-31.12.2021)的对数差异的标准差.但我真的不知道该怎么做.

So the next step would be to calculate the standard deviation for the log differences for the specific period of 1 year for each company, for example 01.01.2012-31.12.2021. But I don't really have a clue on how to do it.

所以我的数据看起来像这样:我有 600 家公司,平均交易日为 252 个(并非每年都有 252 个,因为假期等),2012 年我有 8 个月,2012 年我有 5 个月而不是 12. 这导致了 1.395.000 个元素的样本.

So my data looks like this: I have 600 companies with average 252 trading days ( not every years has 252, because of holidays etc) and for the year 2012 I have 8 month and for 2012 I have 5 months instead of 12. Thats leads to a sample of 1.395.000 elements.

这是我的数据集外观的链接:

Here is a link of how my dataset look like:

https://imgur.com/a/oS7ROCL

有没有办法考虑每年有不同的交易日?

Is there a way to consider the fact that there are different trading days for each year?

一个大问题是有一些NA"在我的数据集中,由于 yahoo Finance 中的数据缺失,或者只是因为该公司目前不存在.我该如何处理?

A big problem is that there are some "NA" in my data set, due to missing datas in yahoo finance or simply the fact that the company doesn't exist at this time.How do I handle this?

推荐答案

您可以使用 apply.yearly 作为帮手.由于您没有提供示例数据,我创建了一些.

You can use apply.yearly as a helper. Since you didn't provide sample data, I created some.

library(xts)

dates <- seq.Date(as.Date("2012-01-01"),
                  as.Date("2021-07-07"),
                  "days")

sampledat <- xts(matrix(rnorm(600 * length(dates)), ncol = 600),
                 order.by = dates)

result_list <- lapply(sampledat,
              FUN = apply.yearly, sd)

result_xts <- do.call(merge, result_list)

> head(result_xts[, 1:5])
                 x.1       x.2       x.3       x.4       x.5
2012-12-31 0.9928683 0.9863453 0.9656742 0.9924881 1.0086642
2013-12-31 0.9837968 0.9378844 1.0512200 1.0327317 0.9610698
2014-12-31 0.9911269 1.0304369 1.0329215 1.0193346 0.9715923
2015-12-31 1.0323760 1.0279656 1.0091391 0.9229457 0.9800465
2016-12-31 1.0258107 0.9320686 1.0288169 0.9994578 0.9424084
2017-12-31 1.0492600 0.9816240 0.9578318 1.0166213 1.0042377

这篇关于关于如何在 xts 对象中获取特定元素的简单问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆