開始使用免費開始

探索 30 檔道瓊工業指數成分股的月報酬

1991–2015 年 30 檔道瓊工業指數(DJIA)成分股的月報酬已作為變數 returns 提供在工作區中。這個練習會幫助你先熟悉之後幾個練習都會用到的資料。

回想一下,若我們用 colMeans(returns),或 apply(returns, 2, "mean") 逐欄計算平均值,就能得到每一資產的平均報酬。在這個練習中,你將改為逐列計算平均值。你可以類似地使用 rowMeans(),以及在 apply() 函式中改用引數 1 而不是 2,來指定逐列計算。這樣做能得到等權重投資組合的報酬時間序列。

本練習屬於課程

R 投資組合分析入門

檢視課程

練習說明

  • 使用函式 class() 確認 returns 是否為 xts 類別的物件。
  • 使用 dim() 檢視 returns 的維度。
  • 使用函式 rowMeans() 建立 returns 的逐列平均向量。將結果指派給 ew_preturns。注意你也可以用 apply() 完成這件事。
  • rowMeans() 的結果是數值向量。請使用 xts,並以 returns 的日期作為時間戳記,將其轉回 xts 物件。
  • 使用 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

編輯並執行程式碼