R tibble 数据帧切片和 rollapply 函数
我正在编写一个脚本来计算 S&P500 的标准差,并希望将滚动标准差与 SD 的长期平均值进行比较。
我可以让我的代码工作,但看起来有点笨拙。我想问两个问题以使我的代码更容易理解。
- 假设我有两个索引。我可以使用一行代码来查找两个股票代码的年化 SD 吗?现在我必须一个接一个地执行
GSPC.new <- merge(GSPC.new,rollapply(GSPC.new$GSPC.adj.ret, 252, sd))
- 它 对 xts 对象进行切片更容易。我可以在 tibbble 数据框中做同样的事情吗?
gspc.avg10yr <- mean(na.omit(GSPC.new$GSPC.adj.std.annualised["2011/2022"]))
下面的所有代码都可以直接在RStudio中直接执行。
library(tidyverse)
library(quantmod)
library(ggplot2)
tickers <- c("^HSI","^GSPC")
getSymbols(Symbols = tickers,
src = "yahoo",
index.class = "POSIXct",
from = "1997-01-01")
GSPC.new <- na.omit(GSPC)
GSPC.new <- merge(GSPC.new, dailyReturn(GSPC.new$GSPC.Adjusted))
colnames(GSPC.new)[7] <- "GSPC.adj.ret"
GSPC.new <- merge(GSPC.new,rollapply(GSPC.new$GSPC.adj.ret, 252, sd)) # how to annualised both tickers in one line
colnames(GSPC.new)[8] <- "GSPC.adj.std"
GSPC.new <- merge(GSPC.new, GSPC.new$GSPC.adj.std*sqrt(252))
colnames(GSPC.new)[9] <- "GSPC.adj.std.annualised"
GSPC.new.tbl <- as_tibble(fortify(GSPC.new))
p.gspc <- ggplot(GSPC.new.tbl, aes(x=Index,y=GSPC.adj.std.annualised))
p.gspc + geom_line()
gspc.avg10yr <- mean(na.omit(GSPC.new$GSPC.adj.std.annualised["2011/2022"])) # is it possible to do in tibble way?
gspc.avg15yr <- mean(na.omit(GSPC.new$GSPC.adj.std.annualised["2006/2022"]))
gspc.avg20yr <- mean(na.omit(GSPC.new$GSPC.adj.std.annualised["2001/2022"]))
gspc.avg25yr <- mean(na.omit(GSPC.new$GSPC.adj.std.annualised["1997/2022"]))
p.gspc + geom_line()+
geom_hline(yintercept = gspc.avg10yr, color = "red")+
geom_hline(yintercept = gspc.avg15yr, color = "blue")+
geom_hline(yintercept = gspc.avg20yr, color = "green")+
geom_hline(yintercept = gspc.avg25yr, color = "black")
I am writing a script to calculate the standard deviation of S&P500 and want to compare the rolling standard deviation to the long term average of the SD.
I can make my codes work but seems kind of clumsy. I want to ask two questions to make my code easier to understand.
- Suppose I have two indices. Can I use one line of code to find the annualised SD for both tickers? Now I have to do it ticker by ticker
GSPC.new <- merge(GSPC.new,rollapply(GSPC.new$GSPC.adj.ret, 252, sd))
- It is easier to slice a xts object. Can I do the same in tibble dataframe?
gspc.avg10yr <- mean(na.omit(GSPC.new$GSPC.adj.std.annualised["2011/2022"]))
All code below can be executed directly in RStudio directly.
library(tidyverse)
library(quantmod)
library(ggplot2)
tickers <- c("^HSI","^GSPC")
getSymbols(Symbols = tickers,
src = "yahoo",
index.class = "POSIXct",
from = "1997-01-01")
GSPC.new <- na.omit(GSPC)
GSPC.new <- merge(GSPC.new, dailyReturn(GSPC.new$GSPC.Adjusted))
colnames(GSPC.new)[7] <- "GSPC.adj.ret"
GSPC.new <- merge(GSPC.new,rollapply(GSPC.new$GSPC.adj.ret, 252, sd)) # how to annualised both tickers in one line
colnames(GSPC.new)[8] <- "GSPC.adj.std"
GSPC.new <- merge(GSPC.new, GSPC.new$GSPC.adj.std*sqrt(252))
colnames(GSPC.new)[9] <- "GSPC.adj.std.annualised"
GSPC.new.tbl <- as_tibble(fortify(GSPC.new))
p.gspc <- ggplot(GSPC.new.tbl, aes(x=Index,y=GSPC.adj.std.annualised))
p.gspc + geom_line()
gspc.avg10yr <- mean(na.omit(GSPC.new$GSPC.adj.std.annualised["2011/2022"])) # is it possible to do in tibble way?
gspc.avg15yr <- mean(na.omit(GSPC.new$GSPC.adj.std.annualised["2006/2022"]))
gspc.avg20yr <- mean(na.omit(GSPC.new$GSPC.adj.std.annualised["2001/2022"]))
gspc.avg25yr <- mean(na.omit(GSPC.new$GSPC.adj.std.annualised["1997/2022"]))
p.gspc + geom_line()+
geom_hline(yintercept = gspc.avg10yr, color = "red")+
geom_hline(yintercept = gspc.avg15yr, color = "blue")+
geom_hline(yintercept = gspc.avg20yr, color = "green")+
geom_hline(yintercept = gspc.avg25yr, color = "black")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将股票数据放入环境 e 中,迭代股票名称,计算列表 ret.list 中的回报,然后将其形成 xts 对象 ret。由此计算标准化年化收益 std_ann,作为 xts 对象。
要创建绘图,请创建一个网格数据框 g 并从中获取数据框 Means,其中包含列 Series、col(颜色)和要在 geom_hline 中使用的平均值。 autoplot 将使用 Series 来引用构面,geom_hline 将使用 Means 中的 Series 列将每条水平线与相应的构面匹配。
仅使用下面列出的包(以及它们所依赖的包)。
Place the ticker data in environment, e, iterate over the ticker names calculating the returns in a list, ret.list, and then form that into an xts object, ret. From that calculate the standardized returns annualized, std_ann, as an xts object.
To create the plot create a grid data frame g and from it data frame Means with columns Series, col (colors) and means to be used in geom_hline. autoplot will use Series to refer to the facets and geom_hline will use the Series column in Means to match each horizontal line to the appropriate facet.
Only the packages listed below (and the packages they depend on) are used.