This report is a summary of lesson by Arnaud Amsellem, Data Camp

library(tidyverse)
library(xts)
library(fpp2)
library(TTR)

1 R Time series visualization Tools

1.1 The plot() function

  • plot()

    • main: title
    • sub: subtitle
    • xlab
    • ylab
    • col
    • lty: line type
    • lwd: line widths
  • lines: add plot in an existing plot

  • axis

    • side: an integer specifying which side of the plot the axis should be drawn on
      • 아래(1), 좌(2), 위(3), 우(4)
    • at: breaks
  • legend(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 margins
    • cex: to adjust the size of the characters
    • mar: 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")

2 Univariate Time Series

2.1 Univariate time series analysis

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 data

2.1.1 Other visualizaition tools

  • hist(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 distributed
    • 오른쪽 꼬리가 위로 휘면 오른쪽 꼬리가 두꺼운 분포(평균보다 큰 값들이 많음)
    • 왼쪽 꼬리가 아래로 휘면 왼쪽 꼬리가 두꺼운 분포(평균보다 작은 값들이 많음)

Histogram of returns

  • density()
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)

3 Multivariate Time Series

3.1 Dealing with higher dimensions

cor_mat <- cor(xts_euStock)
corrplot::corrplot(cor_mat,
                   method = "number",
                   type = "upper", order = "hclust")

4 Case study:Visually selecting a stock that improves your existing portfolio