因爬股票的資源很多,在此只爬雇主要求的收盤價的月資料。
#載入所需套件
library(xts)
library(quantmod)
library(PerformanceAnalytics)
#開始統計的起始日期
d_from <- "2012-01-01"
d_to <- "2021-12-31"
#自Yahoo資料庫抓取資料
index_symbol <- c("^DJI", "AAPL", "2308.TW")
getSymbols(index_symbol, src="yahoo", from=d_from,to=d_to)
TSMCTW<-get("2308.TW") #因為台股股號是數字,用數字繪圖會有問題
TSMCTW = to.monthly(TSMCTW)
DJI = to.monthly(DJI)
AAPL = to.monthly(AAPL)
t = as.numeric(TSMCTW$TSMCTW.Close )
d = as.numeric(DJI$DJI.Close )
a = as.numeric(AAPL$AAPL.Close)
close_price = data.frame(TW2308 = t, DJI = d , AAPL= a )
pairs(close_price)
reference:
http://clover59.blogspot.com/2020/11/r_17.html