开始使用免费开始使用

探索 30 只道琼斯工业平均指数(DJIA)成分股的月度收益

工作区中已提供 30 只 DJIA 成分股在 1991-2015 年的月度收益,变量名为 returns。本练习将帮助您先熟悉后续练习要用到的数据。

回顾:若按列计算均值(使用 colMeans(returns)apply(returns, 2, "mean")),即可得到每个资产的平均收益。在本练习中,您将按行计算均值。可使用 rowMeans(),或在 apply() 函数中将第二个参数从 2 改为 1,以指示按行计算。这样,您就能得到等权重投资组合的收益时间序列。

本练习是课程的一部分

R 中的投资组合分析入门

查看课程

练习说明

  • 使用函数 class() 验证 returns 是否为 xts 类对象。
  • 使用 dim() 查看 returns 的维度。
  • 使用函数 rowMeans() 计算 returns 的行均值,并将结果赋值给 ew_preturns。注意,这里也可以使用 apply()
  • rowMeans() 得到的结果是数值向量。请使用 xts 将其转换回 xts 对象,并将时间戳设为 returns 中的日期。
  • 使用 plot.zoo() 绘制 ew_preturns

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Verify the class of returns 


# Investigate the dimensions of returns


# Create a vector of row means


# Cast the numeric vector back to an xts object
ew_preturns <- xts(___, order.by = time(returns))

# Plot ew_preturns

编辑并运行代码