開始使用免費開始

繪製時間數列物件

在進行時間數列分析時,將資料繪圖通常非常有幫助。若你正在分析的資料集屬於 ts 類別,plot() 函式有對應的方法,會自動把時間索引資訊加入圖形中。

我們來看看 eu_stocks 資料集(在 R 中的預設名稱為 EuStockMarkets)。這個資料集包含 1991–1998 年間歐洲主要股價指數的每日收盤價,分別來自德國(DAX)、瑞士(SMI)、法國(CAC)與英國(FTSE)。資料只在市場開盤日觀測,因此週末與假日沒有觀測值。我們將以近似的方式視此資料集為等間隔觀測,且為四維的時間數列。

作為本章的結尾,本練習要你把已經學過的多個函式套用到這個新資料集上。

本練習屬於課程

R 的時間序列分析

檢視課程

練習說明

  • 使用 is.ts() 檢查 eu_stocks 是否為 ts 物件。
  • 分別使用 start()end()frequency() 查看 eu_stocks 的起始時間、結束時間與頻率。
  • 使用 plot() 指令為 eu_stocks 資料產生一張簡單的圖。
  • 使用 ts.plot() 指令為 eu_stocks 資料產生一張更完整的時間數列圖。請把 eu_stocks 資料集帶入已寫好的程式碼中,其餘引數維持不變。
  • 使用已寫好的程式碼,為你的時間數列圖加入圖例。

動手互動練習

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

# Check whether eu_stocks is a ts object


# View the start, end, and frequency of eu_stocks




# Generate a simple plot of eu_stocks


# Use ts.plot with eu_stocks
ts.plot(___, col = 1:4, xlab = "Year", ylab = "Index Value", main = "Major European Stock Indices, 1991-1998")

# Add a legend to your ts.plot
legend("topleft", colnames(eu_stocks), lty = 1, col = 1:4, bty = "n")
編輯並執行程式碼