This report is a summary of lesson by Arnaud Amsellem, Data Camp
library(tidyverse)
library(xts)
library(fpp2)
library(TTR)
plot()
main
: titlesub
: subtitlexlab
ylab
col
lty
: line typelwd
: line widthslines
: add plot in an existing
plot
axis
side
: an integer specifying which side of the plot the
axis should be drawn on
at
: breakslegend(x = ..., legend = ..., col = ..., lty = ...)
:
add a legend
xpd = TF
: legend가 plot 내부에만 표시abline(v = ..., col = ...)
par
: to tailor the window layout
mfrow = c(nr, nc)
mex
: to adjust the size of the
marginscex
: to adjust the size of the
charactersmar
: c(bottom, left, top, right)# convert ts to xts object
xts_euStock <- xts(coredata(EuStockMarkets), order.by = as_date(date_decimal(index(EuStockMarkets))))
head(xts_euStock)
## DAX SMI CAC FTSE
## 1991-07-01 1628.75 1678.1 1772.8 2443.6
## 1991-07-02 1613.63 1688.5 1750.5 2460.2
## 1991-07-03 1606.51 1678.6 1718.0 2448.2
## 1991-07-05 1621.04 1684.1 1708.1 2470.4
## 1991-07-06 1618.16 1686.6 1723.1 2484.7
## 1991-07-08 1610.61 1671.6 1714.3 2466.8
# Plot the "DAX" series
plot(xts_euStock$DAX,
main = "DAX",
sub = "Daily closing price since 1991")
# # Add the "SMI" series in red
lines(xts_euStock$SMI, col = "red")
# Add a Y axis on the right side of the chart
axis(side = 4, at = pretty(xts_euStock$SMI))
# Add a legend in the bottom right corner
legend(x = "bottomright",
legend = c("DAX", "SMI"),
col = c("black", "red"),
lty = c(1,1),
xpd = TRUE)
# Create vert_line to identify August 2th, 1995 in DAX
vert_line <- which(index(xts_euStock) == as.Date("1995-08-02"))
# Add a red vertical line using vert_line
abline(v = .index(xts_euStock)[vert_line], col = "red")
# Create hori_line to identify average price of DAX
hori_line <- mean(xts_euStock$DAX)
# Add a blue horizontal line using hori_line
abline(h = hori_line, col = "blue")
The very first step in the analysis of any time series is to address if the time series have the right mathematical properties to apply the standard statistical framework. If not, you must transform the time series first. In finance, price series are often transformed to differenced data, making it a return series.
TTR::ROC()
: automatically transform to return datahist(probability = TF)
boxplot(horizontal = TF)
acf(lag.max = ...)
qqnorm
: check for normality
qqline
: draws a line where all the points should have
been if the perfectly normally distributeddensity()
rtn <- ROC(xts_euStock$DAX) %>%
na.omit()
hist(rtn,
main = "DAX return distribution",
probability = TRUE,
ylim = c(0, max(density(rtn)$y)),
xlab = "return")
# Redraw a thicker, red density line
lines(density(rtn), col = "red", lwd = 2)
set.seed(123)
x <- rnorm(100) # 정규분포에서 샘플 생성
qqnorm(x) # QQ Plot 생성
qqline(x, col = "red", lwd = 2) # 기준선 추가
set.seed(123)
x <- rt(100, df = 3) # t-분포에서 샘플 생성 (자유도 3)
qqnorm(x)
qqline(x, col = "red", lwd = 2)
set.seed(123)
x <- rbeta(100, 2, 5) # 왼쪽 꼬리가 긴 베타분포
qqnorm(x)
qqline(x, col = "red", lwd = 2)
barplot(beside = TF)
cor()
method
: “pearson”(default) / “spearman”pairs()
corrplot()
method
: number, color, square, …type
: upper / lowerorder
: 정렬 기준
cor_mat <- cor(xts_euStock)
corrplot::corrplot(cor_mat,
method = "number",
type = "upper", order = "hclust")
PerformanceAnalytics::charts.PerformanceSummary()