# パッケージの呼び出し
library(quantmod)
# 開始日
startDate <- as.Date("2002-01-01")
# 終了日
endDate <- as.Date("2012-12-31")
# 終値
y <- data$USDJPY.Close
# 月足の終値
y <- apply.monthly(y, last)
# 変化率
deltaY <- diff(y) / lag(y) * 100
# 各月のリターン
jan <- deltaY[.indexmon(deltaY) == 0]
feb <- deltaY[.indexmon(deltaY) == 1]
mar <- deltaY[.indexmon(deltaY) == 2]
apr <- deltaY[.indexmon(deltaY) == 3]
may <- deltaY[.indexmon(deltaY) == 4]
june <- deltaY[.indexmon(deltaY) == 5]
july <- deltaY[.indexmon(deltaY) == 6]
aug <- deltaY[.indexmon(deltaY) == 7]
sep <- deltaY[.indexmon(deltaY) == 8]
oct <- deltaY[.indexmon(deltaY) == 9]
nov <- deltaY[.indexmon(deltaY) == 10]
dec <- deltaY[.indexmon(deltaY) == 11]
# 各月の累積リターン
jan <- cumsum(window(jan, start = startDate, end = endDate))
feb <- cumsum(window(feb, start = startDate, end = endDate))
mar <- cumsum(window(mar, start = startDate, end = endDate))
apr <- cumsum(window(apr, start = startDate, end = endDate))
may <- cumsum(window(may, start = startDate, end = endDate))
june <- cumsum(window(june, start = startDate, end = endDate))
july <- cumsum(window(july, start = startDate, end = endDate))
aug <- cumsum(window(aug, start = startDate, end = endDate))
sep <- cumsum(window(sep, start = startDate, end = endDate))
oct <- cumsum(window(oct, start = startDate, end = endDate))
nov <- cumsum(window(nov, start = startDate, end = endDate))
dec <- cumsum(window(dec, start = startDate, end = endDate))
# グラフ
par(mfrow=c(4,3))
matplot(jan,type="l")
matplot(feb,type="l")
matplot(mar,type="l")
matplot(apr,type="l")
matplot(may,type="l")
matplot(june,type="l")
matplot(july,type="l")
matplot(aug,type="l")
matplot(sep,type="l")
matplot(oct,type="l")
matplot(nov,type="l")
matplot(dec,type="l")
# 使用済みオブジェクトの削除
rm(apr, aug, dec, deltaY, endDate, feb, jan, july, june, mar,
may, nov, oct, sep, startDate, y
)