R 语言:如何绘制显示 x 的 returns 与 y 的 returns 的散点图? (量子模组)

R language: How can I plot scatter plot showing the returns of x against the returns of y? (Quantmod)

我目前在 R 中使用 quantmod 包。我正在尝试获取 x 的 returns 与 y 的 returns 的散点图,但是每当我使用plot 函数,它显示了一个时间序列而不是散点图。

install.packages('quantmod')
library('quantmod')

# set date
from.dat <- as.Date('01/01/2020', format='%d/%m/%y')
to.dat <- as.Date('31/12/2020', format='%d/%m/%y')

# extract data for SPY and VXX 
getSymbols(c('SPY', 'VXX'), from=from.dat, to=to.dat)

# calculate returns
spy <- SPY$SPY.Close
vxx <- VXX$VXX.Close
spy_returns <- diff(log(spy))
vxx_returns <- diff(log(vxx))

# create a matrix containing both SPY and VXX returns
matrix <- merge(spy_returns, vxx_returns)

plot(spy_returns, vxx_returns)

(spy_returns,vxx_returns的class我查过了,都是xts zoo对象

要创建散点图,您的数据需要为矢量形式。因此,您需要将数据从时间序列转换为向量。您可以使用 zoo 包中的 coredata 函数从时间序列中获取数据。一旦您加载 quantmod 或 xts,就会加载此包。

下面的代码行将 return 您正在寻找的散点图。

plot(coredata(spy_returns), coredata(vxx_returns)