開始使用免費開始

資產報酬率的時間序列

在 R 中計算單一期的報酬率相當直接。不過,當需要對不同日期計算報酬率時,R 套件 PerformanceAnalytics 中的 Return.calculate()Return.portfolio() 就非常實用。這些函式要求輸入資料為 xts 時間序列類別,這裡已經預先載入。你將在本練習中探索 PerformanceAnalytics 套件的功能。

工作空間中已提供包含 Apple(aapl)與 Microsoft(msft)股價的物件 prices

本練習屬於課程

R 投資組合分析入門

檢視課程

練習說明

  • 在你的 R 工作階段中載入 PerformanceAnalytics 套件。
  • 分別使用 head()tail() 檢視 prices 的前 6 列與後 6 列。
  • 使用 Return.calculate(),將 prices 作為唯一引數,為每個日期計算相較於前一日期的價格百分比變動,並將結果命名為 returns
  • 列印 returns 的前 6 列。
  • 移除 returns 的第一列。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Load package PerformanceAnalytics 


# Print the first six rows and last six rows of prices
 


# Create the variable returns using Return.calculate()  
 
  
# Print the first six rows of returns. Note that the first observation is NA because there is no prior price.

  
# Remove the first row of returns
returns <- returns[-1, ]
編輯並執行程式碼