Challenge Summary

Your organization wants to know which companies are similar to each other to help in identifying potential customers of a SAAS software solution (e.g. Salesforce CRM or equivalent) in various segments of the market. The Sales Department is very interested in this analysis, which will help them more easily penetrate various market segments.

You will be using stock prices in this analysis. You come up with a method to classify companies based on how their stocks trade using their daily stock returns (percentage movement from one day to the next). This analysis will help your organization determine which companies are related to each other (competitors and have similar attributes).

You can analyze the stock prices using what you’ve learned in the unsupervised learning tools including K-Means and UMAP. You will use a combination of kmeans() to find groups and umap() to visualize similarity of daily stock returns.

Objectives

Apply your knowledge on K-Means and UMAP along with dplyr, ggplot2, and purrr to create a visualization that identifies subgroups in the S&P 500 Index. You will specifically apply:

Libraries

Load the following libraries.

# install.packages("plotly")

library(tidyverse)
library(tidyquant)
library(broom)
library(umap)

Data

We will be using stock prices in this analysis. Although some of you know already how to use an API to retrieve stock prices I obtained the stock prices for every stock in the S&P 500 index for you already. The files are saved in the session_6_data directory.

We can read in the stock prices. The data is 1.2M observations. The most important columns for our analysis are:

# STOCK PRICES
sp_500_prices_tbl <- readRDS("C:\\Users\\ADMIN\\Downloads\\sp_500_prices_tbl.rds")
sp_500_prices_tbl

The second data frame contains information about the stocks the most important of which are:

# SECTOR INFORMATION
sp_500_index_tbl <- readRDS("C:\\Users\\ADMIN\\Downloads\\sp_500_index_tbl.rds")
sp_500_index_tbl

Question

Which stock prices behave similarly?

Answering this question helps us understand which companies are related, and we can use clustering to help us answer it!

Even if you’re not interested in finance, this is still a great analysis because it will tell you which companies are competitors and which are likely in the same space (often called sectors) and can be categorized together. Bottom line - This analysis can help you better understand the dynamics of the market and competition, which is useful for all types of analyses from finance to sales to marketing.

Let’s get started.

Step 1 - Convert stock prices to a standardized format (daily returns)

What you first need to do is get the data in a format that can be converted to a “user-item” style matrix. The challenge here is to connect the dots between what we have and what we need to do to format it properly.

We know that in order to compare the data, it needs to be standardized or normalized. Why? Because we cannot compare values (stock prices) that are of completely different magnitudes. In order to standardize, we will convert from adjusted stock price (dollar value) to daily returns (percent change from previous day). Here is the formula.

\[ return_{daily} = \frac{price_{i}-price_{i-1}}{price_{i-1}} \]

First, what do we have? We have stock prices for every stock in the SP 500 Index, which is the daily stock prices for over 500 stocks. The data set is over 1.2M observations.

sp_500_prices_tbl %>% glimpse()
## Rows: 1,225,765
## Columns: 8
## $ symbol   <chr> "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT…
## $ date     <date> 2009-01-02, 2009-01-05, 2009-01-06, 2009-01-07, 2009-01-08, …
## $ open     <dbl> 19.53, 20.20, 20.75, 20.19, 19.63, 20.17, 19.71, 19.52, 19.53…
## $ high     <dbl> 20.40, 20.67, 21.00, 20.29, 20.19, 20.30, 19.79, 19.99, 19.68…
## $ low      <dbl> 19.37, 20.06, 20.61, 19.48, 19.55, 19.41, 19.30, 19.52, 19.01…
## $ close    <dbl> 20.33, 20.52, 20.76, 19.51, 20.12, 19.52, 19.47, 19.82, 19.09…
## $ volume   <dbl> 50084000, 61475200, 58083400, 72709900, 70255400, 49815300, 5…
## $ adjusted <dbl> 15.86624, 16.01451, 16.20183, 15.22628, 15.70234, 15.23408, 1…

Your first task is to convert to a tibble named sp_500_daily_returns_tbl by performing the following operations:

  • Select the symbol, date and adjusted columns
  • Filter to dates beginning in the year 2018 and beyond.
  • Compute a Lag of 1 day on the adjusted stock price. Be sure to group by symbol first, otherwise we will have lags computed using values from the previous stock in the data frame.
  • Remove a NA values from the lagging operation
  • Compute the difference between adjusted and the lag
  • Compute the percentage difference by dividing the difference by that lag. Name this column pct_return.
  • Return only the symbol, date, and pct_return columns
  • Save as a variable named sp_500_daily_returns_tbl
# Apply your data transformation skills!
library(dplyr)

# Assuming sp_500_data is your initial data frame
sp_500_daily_returns_tbl <- sp_500_prices_tbl %>%
  select(symbol, date, adjusted) %>%
  filter(as.Date(date) >= as.Date("2018-01-01")) %>%
  group_by(symbol) %>%
  mutate(lag_adjusted = lag(adjusted, 1)) %>%
  filter(!is.na(lag_adjusted)) %>%
  mutate(diff = adjusted - lag_adjusted,
         pct_return = diff / lag_adjusted) %>%
  select(symbol, date, pct_return)


# Output: sp_500_daily_returns_tbl
# Check the result
print(sp_500_daily_returns_tbl)
## # A tibble: 141,340 × 3
## # Groups:   symbol [502]
##    symbol date       pct_return
##    <chr>  <date>          <dbl>
##  1 MSFT   2018-01-03   0.00465 
##  2 MSFT   2018-01-04   0.00880 
##  3 MSFT   2018-01-05   0.0124  
##  4 MSFT   2018-01-08   0.00102 
##  5 MSFT   2018-01-09  -0.000680
##  6 MSFT   2018-01-10  -0.00453 
##  7 MSFT   2018-01-11   0.00296 
##  8 MSFT   2018-01-12   0.0173  
##  9 MSFT   2018-01-16  -0.0140  
## 10 MSFT   2018-01-17   0.0203  
## # ℹ 141,330 more rows

Step 2 - Convert to User-Item Format

The next step is to convert to a user-item format with the symbol in the first column and every other column the value of the daily returns (pct_return) for every stock at each date.

We’re going to import the correct results first (just in case you were not able to complete the last step).

sp_500_daily_returns_tbl <- read_rds("C:\\Users\\ADMIN\\Downloads\\sp_500_daily_returns_tbl.rds")
sp_500_daily_returns_tbl

Now that we have the daily returns (percentage change from one day to the next), we can convert to a user-item format. The user in this case is the symbol (company), and the item in this case is the pct_return at each date.

  • Spread the date column to get the values as percentage returns. Make sure to fill an NA values with zeros.
  • Save the result as stock_date_matrix_tbl
# Convert to User-Item Format
library(dplyr)
library(tidyr)

# Assuming sp_500_daily_returns_tbl is the tibble we created earlier
stock_date_matrix_tbl <- sp_500_daily_returns_tbl %>%
  spread(key = date, value = pct_return, fill = 0)

# Check the result
# Output: stock_date_matrix_tbl
print(stock_date_matrix_tbl)
## # A tibble: 502 × 283
##    symbol `2018-01-03` `2018-01-04` `2018-01-05` `2018-01-08` `2018-01-09`
##    <chr>         <dbl>        <dbl>        <dbl>        <dbl>        <dbl>
##  1 A          0.0254       -0.00750     0.0160        0.00215     0.0246  
##  2 AAL       -0.0123        0.00630    -0.000380     -0.00988    -0.000959
##  3 AAP        0.00905       0.0369      0.0106       -0.00704    -0.00808 
##  4 AAPL      -0.000174      0.00465     0.0114       -0.00371    -0.000115
##  5 ABBV       0.0156       -0.00570     0.0174       -0.0160      0.00754 
##  6 ABC        0.00372      -0.00222     0.0121        0.0166      0.00640 
##  7 ABMD       0.0173        0.0175      0.0154        0.0271      0.00943 
##  8 ABT        0.00221      -0.00170     0.00289      -0.00288     0.00170 
##  9 ACN        0.00462       0.0118      0.00825       0.00799     0.00333 
## 10 ADBE       0.0188        0.0120      0.0116       -0.00162     0.00897 
## # ℹ 492 more rows
## # ℹ 277 more variables: `2018-01-10` <dbl>, `2018-01-11` <dbl>,
## #   `2018-01-12` <dbl>, `2018-01-16` <dbl>, `2018-01-17` <dbl>,
## #   `2018-01-18` <dbl>, `2018-01-19` <dbl>, `2018-01-22` <dbl>,
## #   `2018-01-23` <dbl>, `2018-01-24` <dbl>, `2018-01-25` <dbl>,
## #   `2018-01-26` <dbl>, `2018-01-29` <dbl>, `2018-01-30` <dbl>,
## #   `2018-01-31` <dbl>, `2018-02-01` <dbl>, `2018-02-02` <dbl>, …

Step 3 - Perform K-Means Clustering

Next, we’ll perform K-Means clustering.

We’re going to import the correct results first (just in case you were not able to complete the last step).

stock_date_matrix_tbl <- read_rds("C:\\Users\\ADMIN\\Downloads\\stock_date_matrix_tbl.rds")

Beginning with the stock_date_matrix_tbl, perform the following operations:

  • Drop the non-numeric column, symbol
  • Perform kmeans() with centers = 4 and nstart = 20
  • Save the result as kmeans_obj
# Create kmeans_obj for 4 centers

library(dplyr)

# Assuming stock_date_matrix_tbl is the tibble we created earlier
# Drop the non-numeric 'symbol' column
stock_date_matrix_numeric <- stock_date_matrix_tbl %>%
  select(-symbol)

# Perform k-means clustering
set.seed(123)  # Setting seed for reproducibility
kmeans_obj <- kmeans(stock_date_matrix_numeric, centers = 4, nstart = 20)

# Check the result
print(kmeans_obj)
## K-means clustering with 4 clusters of sizes 84, 317, 30, 71
## 
## Cluster means:
##     2018-01-03   2018-01-04   2018-01-05  2018-01-08    2018-01-09
## 1 -0.003333954 -0.006912672 0.0012340690 0.005406745 -0.0089961936
## 2  0.005217181  0.004542594 0.0065882996 0.001682355  0.0035926461
## 3  0.014549433  0.011343742 0.0006474687 0.005827603 -0.0061031977
## 4  0.014025528  0.004891946 0.0092884855 0.008080790 -0.0003389287
##      2018-01-10   2018-01-11   2018-01-12   2018-01-16  2018-01-17   2018-01-18
## 1 -0.0120427739 -0.004826786 -0.005236483  0.001309974 0.007494267 -0.006708276
## 2 -0.0003417889  0.011272867  0.008286783 -0.007601889 0.007489971 -0.001424046
## 3  0.0012472656  0.023712257  0.010296386 -0.014205000 0.008879761 -0.009114860
## 4 -0.0020594364  0.009308274  0.009571419 -0.008449825 0.015404285  0.001998036
##    2018-01-19  2018-01-22    2018-01-23   2018-01-24    2018-01-25   2018-01-26
## 1 0.004566404 0.006985181  0.0080806520 -0.003852982  5.819039e-03 0.0008990918
## 2 0.007923882 0.004611634  0.0009158796  0.002736219  6.261335e-05 0.0112167041
## 3 0.001324402 0.024772066 -0.0005949105 -0.006334400 -1.029610e-02 0.0060671176
## 4 0.006715878 0.009735809  0.0093980814 -0.007636793 -4.868228e-03 0.0166806037
##     2018-01-29   2018-01-30   2018-01-31    2018-02-01  2018-02-02  2018-02-05
## 1 -0.011666174 -0.003053078  0.010838715 -0.0147069869 -0.01321817 -0.02624758
## 2 -0.006083350 -0.011773883 -0.003893409 -0.0002198492 -0.01945987 -0.03929605
## 3 -0.015217440 -0.021314151  0.001732506  0.0088676780 -0.03741749 -0.03711698
## 4 -0.005021227 -0.009723839  0.005783896  0.0021775377 -0.02295550 -0.03846852
##     2018-02-06    2018-02-07  2018-02-08  2018-02-09  2018-02-12   2018-02-13
## 1 -0.000769148 -0.0052130982 -0.02013127  0.01909034 0.005043885  0.003804489
## 2  0.014327389  0.0006988956 -0.03645141  0.01201566 0.012262163  0.002250963
## 3  0.013342351 -0.0190853955 -0.03955006 -0.00291801 0.022509484 -0.006038450
## 4  0.025070219 -0.0074124405 -0.04115078  0.02162704 0.018492458  0.008702354
##     2018-02-14   2018-02-15    2018-02-16    2018-02-20   2018-02-21
## 1 -0.004153567  0.016542153  0.0050883535 -0.0117393604 -0.016373701
## 2  0.017405799  0.009018172  0.0009879948 -0.0076134581 -0.002297952
## 3  0.028709277 -0.005064078 -0.0050865513  0.0004252287 -0.016731442
## 4  0.024552532  0.016512878 -0.0064207255  0.0060618583 -0.004441781
##     2018-02-22 2018-02-23   2018-02-26   2018-02-27   2018-02-28   2018-03-01
## 1  0.007334973 0.01800925 2.523189e-03 -0.016890593 -0.004953530 -0.000381493
## 2 -0.001782200 0.01341795 8.558352e-03 -0.013163471 -0.012030574 -0.013488875
## 3  0.009769590 0.02115968 9.924278e-05 -0.014163508 -0.023712904  0.001114025
## 4 -0.002997867 0.02195730 1.220083e-02 -0.009308126 -0.005461278 -0.014143419
##    2018-03-02 2018-03-05    2018-03-06   2018-03-07  2018-03-08  2018-03-09
## 1 0.001200669 0.01370457 -0.0019166661 -0.002145171 0.007270226 0.004711574
## 2 0.005346057 0.01038346  0.0061748492 -0.001323156 0.001313594 0.017891556
## 3 0.007737213 0.01108851  0.0003806278 -0.007205835 0.001223280 0.021309965
## 4 0.021342169 0.01379969  0.0087369561  0.007337585 0.008156522 0.017801101
##      2018-03-12   2018-03-13    2018-03-14   2018-03-15   2018-03-16
## 1  0.0031507710  0.001106390  0.0009870808 -0.002700715 0.0054389622
## 2 -0.0035181932 -0.003675462 -0.0084294900 -0.001428887 0.0043333233
## 3 -0.0005388962 -0.008199908 -0.0017622280 -0.012297035 0.0129985324
## 4  0.0027135945 -0.012076285  0.0002114269 -0.002123033 0.0008341013
##     2018-03-19   2018-03-20    2018-03-21   2018-03-22   2018-03-23 2018-03-26
## 1 -0.007648673 -0.003879733 -0.0090580220 -0.003464591 -0.013739589 0.01267397
## 2 -0.010398300  0.000171361  0.0004388795 -0.027250330 -0.019522841 0.02449549
## 3 -0.017670238  0.013784848  0.0337207010 -0.021459951 -0.007163383 0.01896047
## 4 -0.017248805  0.004318024 -0.0017923612 -0.026251915 -0.025754376 0.03407693
##     2018-03-27    2018-03-28  2018-03-29  2018-04-02  2018-04-03  2018-04-04
## 1  0.007715361  0.0112983042 0.004369175 -0.01422130 0.007764893 0.010279533
## 2 -0.013135617 -0.0004245702 0.013363731 -0.02238548 0.012621133 0.012434839
## 3 -0.013465892 -0.0233216532 0.023735936 -0.02377674 0.019605101 0.001227871
## 4 -0.032635868 -0.0121534871 0.018762622 -0.02940422 0.012355715 0.015777681
##    2018-04-05   2018-04-06    2018-04-09   2018-04-10    2018-04-11
## 1 0.002828578 -0.007734639 -0.0001039607 -0.001881378 -0.0007348127
## 2 0.007176211 -0.023302068  0.0013397648  0.015147638 -0.0067400406
## 3 0.019447924 -0.022406930  0.0025547209  0.038827519  0.0130939406
## 4 0.001453854 -0.029807187  0.0072958976  0.025353351 -0.0056141513
##     2018-04-12   2018-04-13  2018-04-16  2018-04-17    2018-04-18   2018-04-19
## 1 -0.010752601  0.005563910 0.008682951 0.007863409 -0.0046798570 -0.013599108
## 2  0.008179894 -0.005095188 0.011016656 0.006195900  0.0032189057 -0.003267487
## 3  0.001740217  0.013155802 0.008616509 0.006042663  0.0206900471  0.001786194
## 4  0.015962569 -0.007832302 0.004860507 0.022161608  0.0002988631 -0.018124119
##     2018-04-20   2018-04-23  2018-04-24    2018-04-25  2018-04-26   2018-04-27
## 1 -0.012187591  0.001157400  0.00221505  0.0002285739 0.010261824  0.011958287
## 2 -0.005666901  0.002505648 -0.01123779  0.0039772566 0.003569482  0.003206567
## 3 -0.003468391  0.006591688 -0.01599992  0.0059983491 0.011457675 -0.009243902
## 4 -0.010631547 -0.003682519 -0.01618772 -0.0050286856 0.020824552 -0.004572574
##      2018-04-30    2018-05-01   2018-05-02   2018-05-03   2018-05-04
## 1 -5.828280e-03  0.0007776130 -0.009884618  0.001865179 0.0115802919
## 2 -1.188822e-02 -0.0005850741 -0.010327390 -0.005367388 0.0118618730
## 3  5.650547e-05 -0.0050385450  0.008551843 -0.003354957 0.0002403534
## 4 -4.215671e-03  0.0119120995 -0.003166279  0.004031093 0.0152252115
##      2018-05-07    2018-05-08  2018-05-09  2018-05-10   2018-05-11
## 1 -0.0003423209 -0.0117845845 0.002356665 0.008776411 -0.001332108
## 2  0.0006844511  0.0007354292 0.006015187 0.008198444  0.003294055
## 3 -0.0006381188  0.0103725962 0.019005395 0.003092002 -0.001898366
## 4  0.0107081957  0.0042713777 0.018287598 0.012255246 -0.004284660
##      2018-05-14   2018-05-15   2018-05-16   2018-05-17    2018-05-18
## 1 -0.0055922557 -0.011670012 -0.002364305 -0.004893340 -0.0039987220
## 2  0.0005690565 -0.003929441  0.006577711  0.001955032 -0.0005493316
## 3  0.0057983409  0.004290063  0.008291791  0.020700579 -0.0059074763
## 4  0.0016992322 -0.004501408  0.008611112 -0.004584975 -0.0022359815
##    2018-05-21   2018-05-22    2018-05-23    2018-05-24    2018-05-25
## 1 0.005779365  0.002482762  0.0085556910  1.298424e-03  0.0047986326
## 2 0.005961593 -0.004840369  0.0006489749  8.611148e-05 -0.0012343964
## 3 0.008283126 -0.013478278 -0.0044105608 -1.261852e-02 -0.0266239832
## 4 0.005922644 -0.003192814  0.0057628973  1.489904e-03  0.0006639455
##     2018-05-29  2018-05-30   2018-05-31   2018-06-01   2018-06-04   2018-06-05
## 1  0.001238291 0.011695739 -0.007053274 -0.003547776  0.003162351 -0.005049502
## 2 -0.015959552 0.012555582 -0.010942939  0.010095531  0.005981400  0.003198089
## 3 -0.001696881 0.028669361 -0.012173706  0.002155198 -0.012253263 -0.002154042
## 4 -0.006059243 0.009546631 -0.001903148  0.022277656  0.003563116  0.007483896
##     2018-06-06   2018-06-07   2018-06-08  2018-06-11   2018-06-12    2018-06-13
## 1 -0.005781605  0.002795166  0.005472490 0.001419442  0.008011873 -0.0102443629
## 2  0.011968862  0.001149033  0.005784347 0.001753974  0.001399618 -0.0055633906
## 3  0.003809198  0.016374668 -0.004469895 0.005602012 -0.005129724 -0.0031941768
## 4  0.008494834 -0.013241385  0.004352640 0.002487955  0.008414511  0.0006958373
##      2018-06-14   2018-06-15    2018-06-18    2018-06-19  2018-06-20
## 1  0.0089727967  0.005189750 -0.0037548361  0.0055750955 0.004292090
## 2  0.0009142176  0.001952892 -0.0026534461 -0.0061898455 0.001076239
## 3 -0.0051560680 -0.022707272  0.0173805699  0.0006035218 0.010461839
## 4  0.0095731668 -0.001900661  0.0009221515 -0.0088464161 0.006518084
##     2018-06-21   2018-06-22   2018-06-25   2018-06-26   2018-06-27   2018-06-28
## 1  0.003094840  0.008221106  0.009402708  0.000226482 -0.001854346  0.007254731
## 2 -0.005383937  0.001021374 -0.011979354 -0.001118698 -0.010185976  0.002804796
## 3 -0.022917019  0.024278987 -0.026779988  0.017510326  0.015372888 -0.003154605
## 4 -0.010378518 -0.007791614 -0.025003868  0.003632608 -0.020827173  0.010604506
##      2018-06-29   2018-07-02   2018-07-03   2018-07-05  2018-07-06  2018-07-09
## 1  6.340609e-04 -0.002681042  0.004025346  0.012383700 0.005028909 -0.01604752
## 2 -6.855821e-05  0.001471821 -0.001898671  0.006614258 0.006908309  0.01402647
## 3  6.801777e-03 -0.016776871  0.009084328 -0.001488715 0.013008260  0.01913839
## 4  3.736572e-03  0.007468319 -0.011619902  0.013982441 0.012671282  0.00729805
##    2018-07-10   2018-07-11   2018-07-12    2018-07-13   2018-07-16   2018-07-17
## 1 0.006173772  0.002633196 7.831687e-04  0.0004614193 -0.004724125 -0.001728087
## 2 0.001312620 -0.009738650 4.563560e-03  0.0012950176 -0.001679823  0.004588254
## 3 0.003473632 -0.024131440 7.366942e-05  0.0050874219 -0.015230587 -0.001992870
## 4 0.003275802 -0.008306789 1.727150e-02 -0.0023338092 -0.002482895  0.008792052
##     2018-07-18   2018-07-19   2018-07-20   2018-07-23    2018-07-24  2018-07-25
## 1 -0.007769495  0.008153929 -0.005246509 -0.004892180 -0.0005005501 0.007607577
## 2  0.005521811 -0.001518489 -0.002652620  0.001778067 -0.0004441636 0.007192060
## 3  0.001663189 -0.001796304 -0.003616946 -0.003431901  0.0104494824 0.008122618
## 4  0.002761209 -0.004980070 -0.004154766  0.002027684 -0.0080874714 0.016187298
##      2018-07-26   2018-07-27   2018-07-30   2018-07-31   2018-08-01
## 1  0.0079688330 -0.005027542 -0.002331982  0.012574433 -0.002755748
## 2  0.0029787150 -0.003446809 -0.003156086  0.008038429 -0.007249964
## 3  0.0131985204  0.001280992  0.012986403 -0.002268616 -0.015730558
## 4 -0.0006751857 -0.020919476 -0.021618702  0.002895817 -0.002711583
##     2018-08-02    2018-08-03   2018-08-06   2018-08-07    2018-08-08
## 1  0.004132915  0.0134441240 0.0002851608 -0.003699128 -0.0047101273
## 2  0.002993146  0.0051696868 0.0014640732  0.003157818 -0.0006984346
## 3 -0.002134323 -0.0073460616 0.0052397976  0.003959465 -0.0085479395
## 4  0.014229673 -0.0002601396 0.0082356891  0.003361766  0.0003471306
##      2018-08-09   2018-08-10    2018-08-13  2018-08-14   2018-08-15  2018-08-16
## 1  0.0015381073 -0.005940741  0.0008234634 0.004398117  0.008029299 0.009135530
## 2 -0.0007957794 -0.008728654 -0.0054871905 0.010200628 -0.006691091 0.008967913
## 3 -0.0108872145  0.007638768 -0.0144547914 0.004542780 -0.042438317 0.006339482
## 4 -0.0008920641 -0.009182561 -0.0022671805 0.006780214 -0.015767129 0.004438533
##     2018-08-17   2018-08-20   2018-08-21   2018-08-22   2018-08-23  2018-08-24
## 1  0.007732354 2.692069e-07 -0.009589298 -0.006575943 -0.001255369 0.004500810
## 2  0.006126832 5.942634e-03  0.005122876 -0.003894045 -0.003921203 0.003449513
## 3  0.003224037 8.421130e-03  0.008349307  0.013378383 -0.005292100 0.008073458
## 4 -0.001760454 4.049175e-03  0.008199431  0.006730186  0.001981029 0.015124691
##     2018-08-27    2018-08-28  2018-08-29   2018-08-30    2018-08-31
## 1 -0.002951911  1.332308e-03 0.002524474 -0.002927028  0.0011652746
## 2  0.007595955  4.536910e-06 0.003219045 -0.007957643  0.0014553869
## 3  0.009180901 -5.525657e-03 0.007257279 -0.002890487 -0.0073978491
## 4  0.009845194  3.794339e-03 0.007306630 -0.006688627  0.0006588377
##      2018-09-04   2018-09-05   2018-09-06   2018-09-07   2018-09-10
## 1 -0.0024738521  0.010518901  0.005232278 -0.009005451 0.0056867858
## 2 -0.0012439741  0.002183078 -0.000759536 -0.001954284 0.0029803154
## 3 -0.0073232978 -0.004506993 -0.019382860 -0.004880020 0.0006737391
## 4 -0.0009675634 -0.018543501 -0.011209654 -0.004386366 0.0087879660
##      2018-09-11   2018-09-12    2018-09-13   2018-09-14    2018-09-17
## 1 -0.0019538731  0.003579706  4.775531e-03 -0.005895538  0.0034210890
## 2  0.0005049789  0.001161493  4.018751e-03  0.003053672 -0.0031778985
## 3  0.0122762856  0.009381701 -2.306182e-06  0.004538664  0.0006103131
## 4  0.0018295441 -0.002137449  6.462456e-03  0.001758224 -0.0163136879
##     2018-09-18   2018-09-19   2018-09-20    2018-09-21   2018-09-24
## 1 -0.005822705 -0.012892316  0.006698837  0.0026245042 -0.014558807
## 2  0.005551467  0.004069505  0.006322862  0.0003726642 -0.010072955
## 3  0.011631923  0.002892712 -0.004188770  0.0082752213  0.013828416
## 4  0.010072334 -0.002499493  0.012692587 -0.0003286405  0.006489045
##      2018-09-25   2018-09-26    2018-09-27    2018-09-28   2018-10-01
## 1 -0.0056531970 -0.008520217  0.0010554973  0.0117954826 -0.004743913
## 2 -0.0034740979 -0.002645426 -0.0009807794 -0.0007368244  0.001618323
## 3  0.0079921453 -0.009821787 -0.0002961086  0.0005533699  0.013778724
## 4 -0.0008218263 -0.002833221  0.0055217433  0.0020502699 -0.004922243
##      2018-10-02   2018-10-03    2018-10-04   2018-10-05   2018-10-08
## 1  0.0063449319 -0.010963752  0.0005911404  0.007115132  0.011777449
## 2 -0.0021978236  0.001595167 -0.0056339596 -0.005892240  0.002315831
## 3 -0.0008793832  0.013208412 -0.0075673719 -0.002222662 -0.004422415
## 4 -0.0096354843  0.003528746 -0.0233264347 -0.017356286 -0.016146227
##     2018-10-09  2018-10-10  2018-10-11   2018-10-12    2018-10-15 2018-10-16
## 1  0.001382168 -0.01068745 -0.02451301 0.0009265891  0.0063396100 0.01495123
## 2 -0.008888514 -0.02905836 -0.02220585 0.0079626327  0.0003925018 0.01900308
## 3  0.011660485 -0.04102685 -0.03066850 0.0059181717 -0.0069465232 0.01198762
## 4 -0.002245737 -0.04697040 -0.01005558 0.0285292180 -0.0109009681 0.03496553
##      2018-10-17    2018-10-18   2018-10-19   2018-10-22   2018-10-23
## 1 -0.0014392965  0.0003157027  0.014528266 -0.009878428  0.002693487
## 2 -0.0022031050 -0.0143254165 -0.004171677 -0.006903643 -0.006763992
## 3 -0.0121314041 -0.0110406026 -0.013063143 -0.010377735 -0.029278040
## 4 -0.0001182299 -0.0250782664 -0.012541599  0.003844884 -0.003859615
##    2018-10-24   2018-10-25   2018-10-26   2018-10-29 2018-10-30   2018-10-31
## 1  0.01353611 -0.003340079 -0.020372884  0.014181789 0.01125114 -0.011297427
## 2 -0.03132646  0.015441294 -0.012819263  0.001596918 0.01998755  0.008027797
## 3 -0.04585749  0.015249528 -0.009554609 -0.024947273 0.02341653  0.006087733
## 4 -0.05208563  0.028991360 -0.023718562 -0.018802822 0.03454404  0.021504480
##    2018-11-01   2018-11-02   2018-11-05  2018-11-06  2018-11-07    2018-11-08
## 1 0.003747117 -0.009108626  0.013772235 0.008152953 0.008792495  0.0004581358
## 2 0.015634660 -0.001293155  0.006938079 0.007426739 0.014704492  0.0004365969
## 3 0.010308997 -0.009730453  0.018923407 0.002004953 0.022724471 -0.0277091571
## 4 0.032081398 -0.003883827 -0.005924196 0.005474840 0.027114895 -0.0034784146
##     2018-11-09   2018-11-12    2018-11-13   2018-11-14   2018-11-15
## 1  0.005367340  0.001904854  0.0001916946 -0.004133400 -0.004406745
## 2 -0.008675371 -0.015211256  0.0008174817 -0.007268626  0.008872325
## 3 -0.004971914 -0.029063167 -0.0251968257  0.003142088  0.020515330
## 4 -0.024619687 -0.033687808  0.0041571717 -0.005272713  0.023355178
##     2018-11-16   2018-11-19   2018-11-20   2018-11-21    2018-11-23 2018-11-26
## 1  0.010063612  0.001588685 -0.008086658 -0.006933761 -7.387090e-06 0.00222745
## 2  0.002216731 -0.012261967 -0.018868549  0.007797251 -1.868033e-03 0.01391096
## 3  0.007680349 -0.006087903 -0.040522927  0.018658251 -3.653804e-02 0.02157185
## 4 -0.005298491 -0.044615610 -0.007829112  0.012297653 -2.928696e-03 0.02792190
##     2018-11-27  2018-11-28    2018-11-29   2018-11-30  2018-12-03   2018-12-04
## 1  0.005847793 0.002822177  0.0002154275  0.011292338 0.003797532 -0.008022436
## 2 -0.001218930 0.020257793 -0.0037995937  0.005229962 0.008915859 -0.034976107
## 3 -0.006230058 0.016374453  0.0053696702 -0.006236157 0.026350978 -0.035131989
## 4  0.000669214 0.032255508 -0.0073804570  0.008241973 0.021707438 -0.044736643
##     2018-12-06   2018-12-07   2018-12-10   2018-12-11  2018-12-12   2018-12-13
## 1  0.009630455 -0.007000921 -0.000701633  0.004753714 -0.00950557  0.008867633
## 2 -0.006218295 -0.023407900 -0.003215619 -0.003893997  0.00769762 -0.008218803
## 3 -0.025829610 -0.007277311 -0.022566657 -0.003490909  0.01204804 -0.002388297
## 4  0.008023883 -0.037844459  0.009075633  0.001617522  0.01094937 -0.009546741
##     2018-12-14  2018-12-17   2018-12-18   2018-12-19   2018-12-20  2018-12-21
## 1 -0.006215472 -0.03181874 -0.002335696 -0.005963248 -0.009304501 -0.01104888
## 2 -0.014658242 -0.01759075 -0.001704006 -0.015390412 -0.016571956 -0.01838267
## 3 -0.028984553 -0.01903142 -0.022103904 -0.013217782 -0.028551975 -0.01564321
## 4 -0.020016222 -0.02540093  0.010933420 -0.029032982 -0.017370866 -0.03004544
##    2018-12-24 2018-12-26  2018-12-27    2018-12-28  2018-12-31   2019-01-02
## 1 -0.03925786 0.02519597 0.005720476  0.0005493469 0.003374524 -0.017623973
## 2 -0.02226213 0.04534322 0.008903106 -0.0019643956 0.009390790  0.002999051
## 3 -0.04288499 0.07139516 0.003921202 -0.0097765403 0.006628301  0.019069103
## 4 -0.02366252 0.06254006 0.010954290 -0.0021091840 0.008540171  0.003843006
##     2019-01-03 2019-01-04  2019-01-07  2019-01-08   2019-01-09  2019-01-10
## 1  0.003428785 0.01352601 0.002960760 0.014579573 -0.004437992 0.014320030
## 2 -0.020498572 0.03395158 0.009397651 0.009803304  0.006131962 0.004161268
## 3 -0.007154041 0.04511775 0.020515467 0.011879849  0.021232497 0.007079545
## 4 -0.042207626 0.04949310 0.021900070 0.011169862  0.017527869 0.006892676
##      2019-01-11    2019-01-14  2019-01-15    2019-01-16  2019-01-17  2019-01-18
## 1  0.0006439650 -7.487770e-03 0.011233657  0.0018849468 0.005550432 0.005066536
## 2  0.0019697839 -2.211265e-03 0.004755527  0.0034638122 0.009621661 0.016372572
## 3 -0.0044939986 -5.779135e-05 0.004220701  0.0005469789 0.013001543 0.020885746
## 4 -0.0000643815 -1.263161e-02 0.016148265 -0.0003493891 0.009778713 0.018933426
##     2019-01-22    2019-01-23   2019-01-24    2019-01-25   2019-01-28
## 1 -0.003042516  0.0054318391 -0.002902421 -0.0007662691  0.003788257
## 2 -0.013525856  0.0008376363  0.002910626  0.0104049820 -0.003409732
## 3 -0.026342440 -0.0139855530  0.002068247  0.0200460247 -0.015255646
## 4 -0.022274543 -0.0051502604  0.025276525  0.0213786649 -0.011670435
##     2019-01-29  2019-01-30    2019-01-31   2019-02-01  2019-02-04   2019-02-05
## 1  0.004292367 0.007180800  0.0144310699 -0.004675601 0.004866655  0.001337625
## 2  0.003457064 0.007812059  0.0051356841  0.002655385 0.004837957  0.004876282
## 3  0.006852493 0.020060173 -0.0004307433  0.008515918 0.006715036 -0.001705462
## 4 -0.013352553 0.025220812  0.0100370290  0.008579466 0.006612912  0.007444096
##      2019-02-06   2019-02-07    2019-02-08  2019-02-11    2019-02-12
## 1 -0.0032362265  0.005879985  0.0041796637 0.001155150 -0.0007469276
## 2 -0.0007242068 -0.005505920  0.0003808557 0.002443940  0.0144575364
## 3 -0.0072253794 -0.032870943 -0.0073106553 0.009675432  0.0109777684
## 4  0.0011515879 -0.016007614  0.0108905276 0.002164051  0.0187379927
##    2019-02-13    2019-02-14  2019-02-15
## 1 0.001301848 -0.0018576514 0.006505341
## 2 0.003986743 -0.0036341950 0.013290681
## 3 0.018636127  0.0069304497 0.016988955
## 4 0.002882583  0.0008287979 0.003344893
## 
## Clustering vector:
##   [1] 2 4 2 4 2 2 4 2 2 4 4 2 2 2 4 1 1 1 2 2 2 1 2 2 4 2 4 2 2 2 4 4 4 2 2 2 2
##  [38] 1 4 4 4 2 2 2 3 3 2 2 2 1 2 1 4 1 4 2 1 2 2 2 2 2 2 2 2 2 2 3 2 2 2 2 2 2
##  [75] 2 2 2 1 2 1 2 2 2 2 2 2 1 4 2 2 2 2 2 1 2 2 2 2 1 1 2 2 2 2 2 1 2 1 2 2 2
## [112] 3 2 2 1 2 2 4 4 2 2 2 2 2 2 3 3 1 2 2 2 2 2 2 2 2 2 2 2 1 2 2 1 2 1 1 2 3
## [149] 2 2 4 2 2 1 2 1 2 2 2 3 1 1 1 1 2 2 1 1 4 1 2 2 1 2 3 2 4 2 3 2 1 4 2 2 2
## [186] 2 2 3 2 2 2 2 2 2 1 3 4 2 2 2 2 1 2 2 4 4 2 4 2 2 2 2 2 3 2 2 2 2 1 2 3 3
## [223] 2 2 2 2 2 2 3 4 2 2 1 2 2 2 1 2 2 2 4 2 4 4 2 4 4 2 2 4 2 2 1 4 2 2 2 2 2
## [260] 2 2 2 2 2 2 2 1 2 4 1 1 4 1 3 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 4 2 1 2
## [297] 2 4 1 1 2 2 4 1 4 2 2 1 2 2 4 2 1 2 2 2 2 1 2 3 2 3 2 4 4 2 2 2 4 4 2 3 2
## [334] 2 1 2 4 1 2 4 2 2 3 1 2 4 2 2 4 2 2 2 1 3 2 2 2 3 2 2 2 1 1 2 2 1 2 2 2 2
## [371] 2 1 1 2 2 1 2 1 2 2 1 3 2 2 3 4 2 4 2 2 1 2 2 4 2 2 2 2 2 2 2 2 2 2 1 2 2
## [408] 2 2 2 1 3 1 2 4 1 1 2 1 2 2 4 2 2 4 2 2 4 1 2 1 2 2 2 2 2 2 2 2 2 4 2 2 2
## [445] 1 4 4 4 4 2 4 4 2 1 2 2 2 2 2 2 4 2 2 4 2 2 2 3 2 1 2 4 4 1 1 2 2 2 4 1 1
## [482] 2 2 2 1 3 2 2 2 1 4 3 1 4 3 2 2 2 2 2 2 2
## 
## Within cluster sum of squares by cluster:
## [1]  2.554867 18.051976  1.588895  7.009809
##  (between_SS / total_SS =  13.1 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"

Use glance() to get the tot.withinss.

# Apply glance() to get the tot.withinss

# Install and load the broom package if not already installed
# install.packages("broom")
library(broom)

# Use glance() to get the total within-cluster sum of squares
kmeans_summary <- glance(kmeans_obj)

# Extract tot.withinss
tot_withinss <- kmeans_summary$tot.withinss

# Print the result
print(tot_withinss)
## [1] 29.20555

Step 4 - Find the optimal value of K

Now that we are familiar with the process for calculating kmeans(), let’s use purrr to iterate over many values of “k” using the centers argument.

We’ll use this custom function called kmeans_mapper():

kmeans_mapper <- function(center = 3) {
    stock_date_matrix_tbl %>%
        select(-symbol) %>%
        kmeans(centers = center, nstart = 20)
}

Apply the kmeans_mapper() and glance() functions iteratively using purrr.

  • Create a tibble containing column called centers that go from 1 to 30
  • Add a column named k_means with the kmeans_mapper() output. Use mutate() to add the column and map() to map centers to the kmeans_mapper() function.
  • Add a column named glance with the glance() output. Use mutate() and map() again to iterate over the column of k_means.
  • Save the output as k_means_mapped_tbl
kmeans_mapper <- function(data, centers) {
  kmeans(data, centers = centers, nstart = 20)
}
# Use purrr to map

library(tibble)
library(purrr)
library(dplyr)
library(broom)

# Step 1: Create a tibble with 'centers' column ranging from 1 to 30
centers_tbl <- tibble(centers = 1:30)

# Step 2: Apply kmeans_mapper() to each 'centers' value and then glance()
k_means_mapped_tbl <- centers_tbl %>%
  mutate(
    k_means = map(centers, ~ kmeans_mapper(stock_date_matrix_numeric, .)),
    glance = map(k_means, glance)
  )

# View the resulting tibble
# Output: k_means_mapped_tbl
print(k_means_mapped_tbl)
## # A tibble: 30 × 3
##    centers k_means  glance          
##      <int> <list>   <list>          
##  1       1 <kmeans> <tibble [1 × 4]>
##  2       2 <kmeans> <tibble [1 × 4]>
##  3       3 <kmeans> <tibble [1 × 4]>
##  4       4 <kmeans> <tibble [1 × 4]>
##  5       5 <kmeans> <tibble [1 × 4]>
##  6       6 <kmeans> <tibble [1 × 4]>
##  7       7 <kmeans> <tibble [1 × 4]>
##  8       8 <kmeans> <tibble [1 × 4]>
##  9       9 <kmeans> <tibble [1 × 4]>
## 10      10 <kmeans> <tibble [1 × 4]>
## # ℹ 20 more rows

Next, let’s visualize the “tot.withinss” from the glance output as a Scree Plot.

  • Begin with the k_means_mapped_tbl
  • Unnest the glance column
  • Plot the centers column (x-axis) versus the tot.withinss column (y-axis) using geom_point() and geom_line()
  • Add a title “Scree Plot” and feel free to style it with your favorite theme
# Visualize Scree Plot
library(tibble)
library(purrr)
library(dplyr)
library(broom)
library(ggplot2)

# Assuming k_means_mapped_tbl is the tibble obtained from the previous steps
# Unnest the 'glance' column to access 'tot.withinss'
k_means_mapped_tbl <- k_means_mapped_tbl %>%
  unnest(glance)

# Plotting the Scree Plot
ggplot(k_means_mapped_tbl, aes(x = centers, y = tot.withinss)) +
  geom_point(color = "blue") +
  geom_line(color = "blue", linetype = "dashed") +
  labs(
    title = "Scree Plot",
    x = "Number of Centers",
    y = "Total Within-cluster Sum of Squares"
  ) +
  theme_minimal()

We can see that the Scree Plot becomes linear (constant rate of change) between 5 and 10 centers for K.

Step 5 - Apply UMAP

Next, let’s plot the UMAP 2D visualization to help us investigate cluster assignments.

We’re going to import the correct results first (just in case you were not able to complete the last step).

k_means_mapped_tbl <- read_rds("C:\\Users\\ADMIN\\Downloads\\k_means_mapped_tbl.rds")

First, let’s apply the umap() function to the stock_date_matrix_tbl, which contains our user-item matrix in tibble format.

  • Start with stock_date_matrix_tbl
  • De-select the symbol column
  • Use the umap() function storing the output as umap_results
# Apply UMAP

library(dplyr)
library(umap)

# Assuming stock_date_matrix_tbl is your user-item matrix tibble

# Step 1: De-select the 'symbol' column
data_for_umap <- stock_date_matrix_tbl %>%
  select(-symbol)

# Step 2: Apply umap() function
umap_results <- umap(data_for_umap)

# Check the resulting UMAP embedding
# Store results as: umap_results
print(umap_results)

Next, we want to combine the layout from the umap_results with the symbol column from the stock_date_matrix_tbl.

  • Start with umap_results$layout
  • Convert from a matrix data type to a tibble with as_tibble()
  • Bind the columns of the umap tibble with the symbol column from the stock_date_matrix_tbl.
  • Save the results as umap_results_tbl.
# Convert umap results to tibble with symbols
library(dplyr)
library(umap)

# Assuming umap_results is obtained from previous steps and stock_date_matrix_tbl is defined
# Extract the layout component from umap_results
layout_tbl <- as_tibble(umap_results$layout)

# Combine layout_tbl with the symbol column from stock_date_matrix_tbl
umap_results_tbl <- bind_cols(stock_date_matrix_tbl$symbol, layout_tbl)

# Rename the columns appropriately
colnames(umap_results_tbl) <- c("symbol", "UMAP_1", "UMAP_2")  # Adjust if UMAP used different dimensions

# Check the resulting tibble
# Output: umap_results_tbl
print(umap_results_tbl)
## # A tibble: 502 × 3
##    symbol UMAP_1 UMAP_2
##    <chr>   <dbl>  <dbl>
##  1 A      -1.75  -0.649
##  2 AAL    -0.892  1.81 
##  3 AAP     0.114 -1.21 
##  4 AAPL   -2.90  -1.53 
##  5 ABBV    0.255  0.273
##  6 ABC     0.807 -0.211
##  7 ABMD   -3.12  -1.76 
##  8 ABT    -1.42  -0.632
##  9 ACN    -1.29  -0.910
## 10 ADBE   -3.20  -2.13 
## # ℹ 492 more rows

Finally, let’s make a quick visualization of the umap_results_tbl.

  • Pipe the umap_results_tbl into ggplot() mapping the columns to x-axis and y-axis
  • Add a geom_point() geometry with an alpha = 0.5
  • Apply theme_tq() and add a title “UMAP Projection”
# Visualize UMAP results

library(ggplot2)

# Assuming umap_results_tbl is prepared as described in the previous response
umap_results_tbl %>%
  ggplot(aes(x = UMAP_1, y = UMAP_2)) +
  geom_point(alpha = 0.5) +
  labs(title = "UMAP Projection") +
  theme_tq()

We can now see that we have some clusters. However, we still need to combine the K-Means clusters and the UMAP 2D representation.

Step 6 - Combine K-Means and UMAP

Next, we combine the K-Means clusters and the UMAP 2D representation

We’re going to import the correct results first (just in case you were not able to complete the last step).

k_means_mapped_tbl <- read_rds("C:\\Users\\ADMIN\\Downloads\\k_means_mapped_tbl.rds")
umap_results_tbl   <- read_rds("C:\\Users\\ADMIN\\Downloads\\umap_results_tbl.rds")

First, pull out the K-Means for 10 Centers. Use this since beyond this value the Scree Plot flattens. Have a look at the business case to recall how that works.

# Get the k_means_obj from the 10th center

# Assuming k_means_mapped_tbl is already created and contains k-means results
# Extract k-means results for 10 centers
kmeans_results_10_centers <- k_means_mapped_tbl %>%
  filter(centers == 10) %>%
  pull(k_means)

# Analyze the business case to understand the context and application of clustering
# Here, we assume some context or application specific to your business scenario

# Example interpretation based on a generic business context
business_case_context <- "
In our business case, we applied k-means clustering with 10 centers to segment our customer base 
based on their purchasing behavior"


# Store as k_means_obj
# Print the k-means results for 10 centers and the business case context
print(kmeans_results_10_centers)
## [[1]]
## K-means clustering with 10 clusters of sizes 29, 94, 1, 73, 43, 4, 126, 46, 58, 28
## 
## Cluster means:
##       2018-01-03   2018-01-04    2018-01-05    2018-01-08   2018-01-09
## 1   0.0145437485  0.011631004  0.0005726774  5.789677e-03 -0.006332892
## 2   0.0065642160  0.006738043  0.0059085037  2.974155e-03  0.001274760
## 3   0.0036426538  0.008295869 -0.0099417552 -6.232618e-03  0.185191579
## 4  -0.0041425774 -0.008439491  0.0006217945  5.537889e-03 -0.009736488
## 5  -0.0017493406 -0.001231831  0.0047699592 -8.345719e-05  0.001499131
## 6   0.0196614543  0.009614592  0.0083596086 -5.317769e-03 -0.021632842
## 7   0.0075626729  0.004996486  0.0082570988  2.317536e-03  0.003389852
## 8   0.0050761435  0.008697071  0.0041779930  3.826302e-04  0.011147386
## 9   0.0154637264  0.004805504  0.0101239450  1.033477e-02 -0.001709654
## 10 -0.0007381898 -0.003919336  0.0073316224  2.273495e-04 -0.001901621
##      2018-01-10   2018-01-11   2018-01-12   2018-01-16   2018-01-17
## 1   0.001130067  0.023196792  0.010086447 -0.015274874  0.009126537
## 2  -0.003242479  0.018020616  0.007094835 -0.009952770  0.005195221
## 3   0.012788520  0.014223439  0.030337766 -0.031388917 -0.004875194
## 4  -0.013601948 -0.005543798 -0.006214341  0.001676481  0.007333950
## 5   0.001741024  0.012702317  0.009891142 -0.004211081  0.002933372
## 6  -0.006726449  0.004202749 -0.002665092 -0.058360697  0.002897018
## 7  -0.003891251  0.005139493  0.006378343 -0.003863188  0.009419685
## 8   0.010289687  0.008619943  0.007922556 -0.009471434  0.008060192
## 9  -0.002485020  0.008047997  0.010321299 -0.005522328  0.017315690
## 10  0.004877772  0.017508752  0.016356885 -0.013608894  0.013597514
##       2018-01-18  2018-01-19  2018-01-22    2018-01-23    2018-01-24
## 1  -0.0105740416 0.001157641 0.025135535 -0.0007110677 -0.0064518342
## 2  -0.0024848393 0.005794388 0.002834018 -0.0003162568 -0.0011087779
## 3   0.0109510371 0.020381869 0.072496205  0.0131544935 -0.0161974802
## 4  -0.0067159634 0.003412544 0.007521111  0.0088803250 -0.0049853116
## 5  -0.0003027514 0.002334629 0.012645723 -0.0013341706  0.0049397478
## 6  -0.0099325229 0.040441835 0.017956822  0.0379076882 -0.0122245462
## 7  -0.0013277692 0.007329845 0.002938302  0.0026241276  0.0054174006
## 8  -0.0054462166 0.012582562 0.006860615  0.0013132145  0.0037624435
## 9   0.0026094959 0.004086150 0.007754607  0.0084308409 -0.0089395855
## 10  0.0068662854 0.020098382 0.004107398  0.0011842085 -0.0006335636
##       2018-01-25    2018-01-26   2018-01-29    2018-01-30   2018-01-31
## 1  -0.0106173787  6.101720e-03 -0.015787016 -0.0213550985  0.001603751
## 2  -0.0059748769  1.015495e-02 -0.009534035 -0.0096672600 -0.001700188
## 3   0.0313602644  1.266933e-03  0.042515501  0.0007282195  0.014069182
## 4   0.0073453686 -2.703617e-05 -0.011576359 -0.0022192040  0.012161502
## 5   0.0003778577  1.094782e-02 -0.004185305 -0.0121631915 -0.008028483
## 6  -0.0222533499 -1.315427e-03 -0.016453175 -0.0129734439 -0.010484817
## 7   0.0064845613  1.146105e-02 -0.007108523 -0.0089311642 -0.001548083
## 8  -0.0056489993  1.000797e-02 -0.004895856 -0.0175196151 -0.002431135
## 9  -0.0042205392  2.067828e-02 -0.002746892 -0.0112683178  0.006598105
## 10 -0.0029840426  1.062332e-02 -0.001387342 -0.0167714417 -0.012082577
##       2018-02-01   2018-02-02  2018-02-05   2018-02-06    2018-02-07
## 1   0.0093211650 -0.037766377 -0.03690706  0.013244579 -0.0196711210
## 2  -0.0037978447 -0.023060851 -0.04013647  0.018172827  0.0009898671
## 3   0.0613562605 -0.008338945 -0.06318180 -0.045002413 -0.0274355762
## 4  -0.0166136472 -0.012720241 -0.02432818 -0.002116927 -0.0051285970
## 5  -0.0022117398 -0.020399220 -0.03947534  0.013863817 -0.0026366719
## 6  -0.0190634365 -0.002076577 -0.03945030  0.018994374  0.0080707901
## 7   0.0017958753 -0.014860421 -0.03690204  0.008060500  0.0016302764
## 8   0.0107897836 -0.021591070 -0.04651543  0.015681255  0.0004776815
## 9   0.0008162164 -0.024417924 -0.03933827  0.027975062 -0.0106557091
## 10 -0.0087468676 -0.023724738 -0.03206853  0.026178339  0.0012778349
##     2018-02-08    2018-02-09   2018-02-12    2018-02-13   2018-02-14
## 1  -0.03986800 -0.0028315968  0.022470773 -0.0059466066  0.028741740
## 2  -0.03912016  0.0075367855  0.018609941 -0.0010055366  0.013540809
## 3  -0.02507507 -0.0001339987  0.054394401 -0.0386276127  0.110229918
## 4  -0.01876326  0.0213057564  0.003930112  0.0044494737 -0.005984465
## 5  -0.02824215  0.0059781838  0.009481471  0.0022354448  0.012950118
## 6   0.01438555  0.0408664979  0.031525687  0.0707301121  0.040448433
## 7  -0.03467253  0.0136490167  0.010629635  0.0045887267  0.015122358
## 8  -0.04743803  0.0192589939  0.013614735  0.0008103593  0.028478620
## 9  -0.04103868  0.0218574145  0.018835130  0.0049581980  0.023289477
## 10 -0.03707387  0.0137519768 -0.001253348  0.0060199275  0.025309434
##      2018-02-15    2018-02-16    2018-02-20    2018-02-21   2018-02-22
## 1  -0.005527208 -0.0051308189  0.0001945716 -0.0169459770  0.009698184
## 2   0.008110574 -0.0005391426 -0.0059456862 -0.0016953714  0.003347874
## 3  -0.022142869  0.0157049064  0.0002396860  0.0359496718  0.020358613
## 4   0.017038758  0.0059424591 -0.0117238557 -0.0177334881  0.008159557
## 5   0.011237394  0.0023648945 -0.0126842856 -0.0095531145 -0.003510870
## 6   0.025453792 -0.0327184475 -0.0132688464  0.0006828019 -0.018994639
## 7   0.012223535  0.0025440218 -0.0074921851 -0.0032985602 -0.001808385
## 8   0.001903319  0.0029184710 -0.0020222832  0.0026789595 -0.013078914
## 9   0.017024057 -0.0060666804  0.0084390366 -0.0067996418 -0.002970803
## 10  0.008745195 -0.0068438891 -0.0143390752  0.0029664385  0.005293028
##    2018-02-23    2018-02-26   2018-02-27   2018-02-28    2018-03-01
## 1  0.02186558 -0.0001523236 -0.014192763 -0.024117444  0.0018311893
## 2  0.01079368  0.0085391020 -0.015577972 -0.015291194 -0.0129581588
## 3  0.01575784  0.0107142860 -0.005631570 -0.038756301 -0.0232208300
## 4  0.01800265  0.0019481749 -0.017312381 -0.004604333 -0.0006035957
## 5  0.01531811  0.0096761067 -0.016220496 -0.018772323 -0.0099838154
## 6  0.01273791 -0.0045193885 -0.022410596 -0.010970884 -0.0220073109
## 7  0.01423814  0.0083471619 -0.009934123 -0.008745837 -0.0138927158
## 8  0.01601208  0.0108017773 -0.010516015 -0.013439415 -0.0156374403
## 9  0.02303929  0.0144215633 -0.008194996 -0.004895418 -0.0142134580
## 10 0.01446056  0.0021400959 -0.019078129  0.001355020 -0.0083934481
##       2018-03-02   2018-03-05    2018-03-06    2018-03-07    2018-03-08
## 1   0.0075032777  0.011184483  8.554807e-05 -0.0075082198  0.0015585011
## 2  -0.0004789283  0.010920220  9.140912e-03 -0.0019756670  0.0039724745
## 3   0.2166765151 -0.007679605 -3.908705e-02  0.0327251211  0.0704836998
## 4   0.0001678387  0.014350013 -3.402989e-03 -0.0012632247  0.0066435197
## 5   0.0148490034  0.008137398  2.581041e-03 -0.0050556772  0.0054613384
## 6   0.0449641008  0.007615378  4.742766e-03 -0.0145753490 -0.0052765302
## 7   0.0071400867  0.010964616  4.895555e-03  0.0043559616  0.0030516198
## 8   0.0052386317  0.013347723  8.904611e-03  0.0002367185  0.0004781494
## 9   0.0183294948  0.014701953  9.132188e-03  0.0089743251  0.0066681599
## 10  0.0042628566  0.004851129  5.995494e-03 -0.0227305573 -0.0120761984
##       2018-03-09    2018-03-12    2018-03-13   2018-03-14    2018-03-15
## 1   0.0212305234 -0.0004160591 -0.0082164133 -0.001626219 -0.0126071311
## 2   0.0215717736 -0.0049164968 -0.0027043300 -0.010738006 -0.0018786067
## 3  -0.0009222243 -0.0486431253  0.0008732318  0.013086448 -0.0239211554
## 4   0.0041083850  0.0036975439  0.0013347335  0.002128121 -0.0031399519
## 5   0.0090941626  0.0037892843 -0.0060379286 -0.005629742 -0.0078040522
## 6  -0.0200553581  0.0027151220 -0.0283910751 -0.024853761 -0.0174523642
## 7   0.0168491646 -0.0045347757 -0.0031700407 -0.004811551  0.0014994115
## 8   0.0233337884 -0.0048729272 -0.0103730486 -0.013344646 -0.0019811544
## 9   0.0196518612  0.0041171809 -0.0110802771  0.001503922 -0.0017691999
## 10  0.0136130866 -0.0013651835  0.0039647383 -0.009450711  0.0002169385
##       2018-03-16   2018-03-19    2018-03-20    2018-03-21   2018-03-22
## 1   0.0132482035 -0.017800963  0.0140539214  0.0342080617 -0.020780419
## 2   0.0057620794 -0.012283948  0.0015326711  0.0029637538 -0.030345295
## 3   0.0097049111  0.019514583  0.0208551086 -0.0027051587 -0.027125637
## 4   0.0067465768 -0.007155659 -0.0036677505 -0.0090931253 -0.002382522
## 5  -0.0014468421 -0.016598858 -0.0159511816  0.0009111288 -0.018445826
## 6   0.0128058372 -0.015527328 -0.0109413440 -0.0031234864 -0.010070408
## 7   0.0025664048 -0.008129643  0.0038283966 -0.0027663263 -0.023592091
## 8   0.0072818203 -0.008545107  0.0005299866  0.0028048163 -0.040158724
## 9  -0.0004359307 -0.019334763  0.0042849448 -0.0010661842 -0.027273994
## 10  0.0082006855 -0.007323849  0.0042209593 -0.0031006908 -0.019488451
##      2018-03-23 2018-03-26   2018-03-27    2018-03-28  2018-03-29  2018-04-02
## 1  -0.006762689 0.01869013 -0.013587364 -0.0241815069 0.023708555 -0.02384333
## 2  -0.020671257 0.02428748 -0.013607394 -0.0052102103 0.017859557 -0.02556097
## 3  -0.009710624 0.03912620 -0.023544773  0.0047842311 0.011903628 -0.07048753
## 4  -0.013253897 0.01174329  0.009301777  0.0115109479 0.003864247 -0.01386817
## 5  -0.019603345 0.01790434 -0.004012335  0.0077864280 0.006100777 -0.02219502
## 6   0.005586658 0.02901152 -0.004907839 -0.0061949106 0.004764184 -0.02008524
## 7  -0.015863577 0.02201133 -0.012779723 -0.0005986344 0.011692887 -0.01812655
## 8  -0.033051565 0.03536920 -0.021159480  0.0001888029 0.014369638 -0.02507659
## 9  -0.027953986 0.03558076 -0.036691024 -0.0127492679 0.019706327 -0.03027664
## 10 -0.011026993 0.02666101 -0.011269919  0.0033580517 0.015304763 -0.02451768
##     2018-04-03   2018-04-04   2018-04-05   2018-04-06    2018-04-09  2018-04-10
## 1  0.019486383 0.0008065077  0.019815487 -0.022107520  0.0025333819  0.03916040
## 2  0.014715841 0.0115904929  0.010750231 -0.027596936 -0.0013867491  0.01671778
## 3  0.005973514 0.0250603560 -0.004025489 -0.074625383  0.1127089982 -0.01637146
## 4  0.006779156 0.0100189261  0.002128531 -0.007286219 -0.0002659433 -0.00354477
## 5  0.010573408 0.0168262644  0.003888427 -0.016300976  0.0006668385  0.01251590
## 6  0.034235146 0.0327929895 -0.010307551 -0.018687501 -0.0107724779  0.01111685
## 7  0.011368407 0.0089518300  0.005310227 -0.020248669  0.0039530326  0.01337158
## 8  0.014514244 0.0115674879  0.006445418 -0.028473234  0.0060287051  0.01701159
## 9  0.011091022 0.0153590611  0.001131254 -0.030142060  0.0068904541  0.02864014
## 10 0.011941497 0.0245527566  0.010908214 -0.021364331 -0.0066578116  0.01662728
##       2018-04-11    2018-04-12   2018-04-13   2018-04-16   2018-04-17
## 1   0.0134218012  0.0018882349  0.014021218  0.008889818  0.005828624
## 2  -0.0071466640  0.0095589639 -0.002515237  0.012322901  0.008572858
## 3   0.0002919895  0.0094385621 -0.031231907 -0.069950239  0.002567647
## 4  -0.0007258075 -0.0116922688  0.005843584  0.008824431  0.008071086
## 5  -0.0059624297  0.0023023998 -0.002116732  0.013521629  0.008258630
## 6   0.0194276384  0.0102286064 -0.024313246  0.001783420 -0.009204711
## 7  -0.0047569307  0.0068326103 -0.002144741  0.011259635  0.008219502
## 8  -0.0124702068  0.0187358687 -0.014549366  0.006392001 -0.005228799
## 9  -0.0057755927  0.0152748724 -0.006289212  0.005107120  0.026027200
## 10 -0.0065070155  0.0006261891 -0.013650122  0.008475915  0.006776166
##       2018-04-18   2018-04-19   2018-04-20    2018-04-23   2018-04-24
## 1   0.0209803229  0.001987134 -0.003255698  0.0051119198 -0.015570104
## 2   0.0099686724 -0.010952613 -0.006941401 -0.0004020304 -0.021116335
## 3  -0.0124853059 -0.025826658 -0.043704969 -0.0088157059 -0.011000527
## 4  -0.0041866844 -0.011586026 -0.012086509  0.0012062362  0.003911963
## 5  -0.0009691888 -0.012196601 -0.011894216  0.0062815660 -0.005058383
## 6  -0.0167710481 -0.015619790 -0.015123451  0.0353373495  0.007331051
## 7   0.0015039281 -0.002994698 -0.004478610  0.0007673909 -0.011855198
## 8  -0.0009129215  0.016755237  0.001144140  0.0024502710 -0.003212332
## 9  -0.0001925461 -0.018897988 -0.010199111 -0.0071034281 -0.016939156
## 10  0.0005093188 -0.009936155 -0.011820071  0.0153302777 -0.001127297
##       2018-04-25    2018-04-26    2018-04-27   2018-04-30   2018-05-01
## 1   6.257147e-03  0.0119568379 -0.0100034081  0.000785677 -0.005264935
## 2   6.736282e-03 -0.0059074636  0.0009474261 -0.015695753  0.002021579
## 3  -2.271928e-02  0.0193728790 -0.0062952606  0.000000000  0.005498434
## 4   5.395037e-05  0.0110083812  0.0127658727 -0.005845951  0.001892285
## 5   8.836164e-03  0.0002847735 -0.0015254802 -0.012112274 -0.001784067
## 6  -2.038064e-02  0.0251171389  0.0301978798  0.014655499 -0.002154140
## 7   1.048921e-03  0.0050302227  0.0023543514 -0.008048128  0.001261753
## 8  -8.194749e-04  0.0014155280  0.0074304687 -0.011858982 -0.002134304
## 9  -4.806665e-03  0.0257372537 -0.0064280065 -0.005138238  0.013484231
## 10  6.889402e-03  0.0320344375  0.0133433562 -0.012735289 -0.013002988
##      2018-05-02    2018-05-03   2018-05-04    2018-05-07    2018-05-08
## 1   0.009021952 -0.0036174070  0.007982274  0.0002968910  0.0105907631
## 2  -0.007293515  0.0001890548  0.011767646  0.0016470808  0.0043503749
## 3   0.014146397 -0.0300081826 -0.067069523  0.0202073194 -0.0083799388
## 4  -0.007684412  0.0011809787  0.010283303 -0.0009191323 -0.0131161149
## 5  -0.012658796 -0.0197507981  0.018466776 -0.0040066535 -0.0131674013
## 6   0.001715860 -0.0356584363  0.005718492  0.0262778717  0.0040248183
## 7  -0.012132110 -0.0004455295  0.009236322  0.0023215080  0.0008205109
## 8  -0.015829563 -0.0103559062  0.015932978  0.0061273534  0.0047754632
## 9  -0.004583453  0.0074488279  0.017274560  0.0098316368  0.0036974782
## 10 -0.001480742 -0.0084251189  0.003012113 -0.0094507256  0.0042875069
##      2018-05-09   2018-05-10    2018-05-11    2018-05-14   2018-05-15
## 1   0.019081795  0.002484973 -2.105145e-03  0.0057834594  0.003230659
## 2   0.007346739  0.008275428  1.437453e-03  0.0003666706 -0.004829891
## 3  -0.003457055 -0.006938211  2.781732e-02  0.0187562684  0.030520227
## 4   0.001706413  0.009265730 -9.737764e-04 -0.0059797035 -0.012241363
## 5   0.002195217  0.016548100  6.851217e-03  0.0018401981 -0.007666763
## 6  -0.013015150 -0.002490280  9.526900e-03 -0.0220298167  0.026932336
## 7   0.005407329  0.008811339 -7.157434e-04 -0.0007310390 -0.005346392
## 8   0.010309673  0.007576544  1.257945e-03  0.0000384929  0.001126507
## 9   0.021007444  0.012657333 -9.617619e-05  0.0020080256 -0.008358058
## 10  0.006855169 -0.004103176  8.769280e-03  0.0072741919  0.004826529
##      2018-05-16    2018-05-17   2018-05-18   2018-05-21   2018-05-22
## 1   0.008030031  2.104331e-02 -0.006194309  0.008416722 -0.013949927
## 2   0.009172102  4.123888e-03  0.003438637  0.008690897 -0.009710431
## 3   0.028776990 -7.657342e-02  0.076612391 -0.021453716 -0.018210183
## 4  -0.003090867 -5.560361e-03 -0.004341187  0.006627933  0.003319260
## 5   0.009320814 -2.413515e-03 -0.005069944 -0.002153219  0.001258431
## 6   0.053727542 -1.703862e-02 -0.006411297 -0.005686704 -0.005045261
## 7   0.003422726  5.045988e-05  0.001997108  0.006466379 -0.006352082
## 8   0.002558516  4.832637e-03 -0.008123400  0.004794623  0.007862453
## 9   0.005683026 -3.396801e-03 -0.003809872  0.007006359 -0.001167281
## 10  0.012650264  4.609209e-03 -0.005808795  0.006947448 -0.013971087
##      2018-05-23    2018-05-24    2018-05-25   2018-05-29  2018-05-30
## 1  -0.003799118 -1.286371e-02 -0.0273864394 -0.001655917 0.028824217
## 2  -0.001215370  5.943826e-05  0.0007037725 -0.014488437 0.009966459
## 3   0.010372252 -2.681161e-02 -0.0076942540 -0.026638282 0.035590337
## 4   0.008864209  8.249051e-04  0.0055005368  0.002300602 0.011410992
## 5  -0.005902130 -2.844831e-03  0.0014090191 -0.012097902 0.009750346
## 6   0.004396211  1.052968e-02  0.0126268356 -0.010898882 0.024874911
## 7   0.001980473  1.381910e-03 -0.0018404690 -0.011661328 0.013319953
## 8  -0.007788370 -4.284822e-03 -0.0056191330 -0.038774079 0.019770315
## 9   0.006338701  1.349088e-03 -0.0001295464 -0.005308372 0.008013739
## 10  0.026624971  7.949624e-03 -0.0006711770 -0.001536402 0.010278605
##       2018-05-31   2018-06-01   2018-06-04    2018-06-05   2018-06-06
## 1  -0.0121672609  0.002045573 -0.012513933 -0.0024687159  0.003589474
## 2  -0.0113469693  0.010495083  0.002494881  0.0034657119  0.011874420
## 3  -0.0040943053  0.125576198 -0.418151620  0.0644854290  0.071837046
## 4  -0.0063160337 -0.004395287  0.002833296 -0.0054107634 -0.006994175
## 5  -0.0108755519  0.003768814  0.004091356  0.0008772653  0.013741639
## 6  -0.0129728466  0.012788744  0.038638682  0.0091358159  0.025319769
## 7  -0.0095870912  0.009473243  0.006387099  0.0025827292  0.009697714
## 8  -0.0067783550  0.013814430  0.003368935 -0.0042688724  0.019197647
## 9  -0.0008890349  0.022679037  0.009370188  0.0074432858  0.005554430
## 10 -0.0209360375  0.012156052  0.019754701  0.0177734116  0.004464315
##       2018-06-07   2018-06-08   2018-06-11   2018-06-12   2018-06-13
## 1   0.0168976076 -0.005089168  0.005802034 -0.004957193 -0.003041303
## 2   0.0011867260  0.005447637  0.003810804  0.001637788 -0.009294020
## 3  -0.1168722741  0.022087936 -0.020687089  0.010184855 -0.001680358
## 4   0.0024689257  0.005029354  0.001084370  0.008286966 -0.011036649
## 5   0.0043192493  0.006933643  0.009676922  0.002799132  0.003313122
## 6  -0.0021533389  0.034115404  0.001842297  0.006478598 -0.011782038
## 7  -0.0001199133  0.005976695 -0.001170107  0.002111238 -0.004617059
## 8  -0.0005056904  0.003260364 -0.003454057 -0.003001729 -0.006034655
## 9  -0.0130480661  0.003009932  0.002501295  0.008744624  0.002490950
## 10  0.0036871882  0.007225727  0.006170351  0.005481649 -0.010064940
##       2018-06-14    2018-06-15    2018-06-18    2018-06-19    2018-06-20
## 1  -4.950259e-03 -0.0233421646  1.725029e-02  0.0009711716  0.0104862440
## 2   2.346836e-03 -0.0014502131 -3.203424e-03 -0.0158695041  0.0013754952
## 3   5.498408e-02  0.0257046800  9.505686e-03 -0.0179763568  0.0141212864
## 4   9.677993e-03  0.0051368551 -2.855371e-03  0.0061054144  0.0046159044
## 5   1.307883e-02  0.0080650764 -1.083052e-02  0.0059046333  0.0112189975
## 6   6.708171e-03 -0.0049014024 -2.150519e-03 -0.0198213640  0.0156183786
## 7   8.440334e-05  0.0016942832 -2.079763e-03 -0.0049102162 -0.0022890739
## 8  -6.424824e-03 -0.0004686897 -9.139113e-04 -0.0023326027 -0.0005223572
## 9   1.039053e-02 -0.0011952428 -3.632523e-05 -0.0064698075  0.0054185685
## 10 -6.319777e-03  0.0071999880  7.523292e-03 -0.0050496552  0.0052552417
##      2018-06-21   2018-06-22   2018-06-25    2018-06-26    2018-06-27
## 1  -0.023013302  0.024528376 -0.026993165  0.0179577088  0.0160728586
## 2  -0.012317685  0.004047323 -0.016300246  0.0003925465 -0.0091149222
## 3  -0.039711141 -0.053884765 -0.029895895 -0.0284766725 -0.0714716107
## 4   0.004419211  0.008532335  0.010974704  0.0002788998 -0.0007296724
## 5  -0.006190287  0.007151838 -0.006624295 -0.0029080196 -0.0036461386
## 6  -0.005137809 -0.002430153 -0.008138573  0.0027829492 -0.0058557019
## 7  -0.004769932  0.002707633 -0.011117660 -0.0007922268 -0.0102439714
## 8  -0.001628419 -0.005297907 -0.011876749 -0.0077799474 -0.0180782807
## 9  -0.009342208 -0.007652501 -0.026071675  0.0055241982 -0.0213655367
## 10  0.006276093 -0.015094814 -0.009493673  0.0053312138 -0.0123244723
##       2018-06-28    2018-06-29    2018-07-02    2018-07-03    2018-07-05
## 1  -0.0030642273  0.0066213840 -0.0172210767  0.0095253240 -1.767987e-03
## 2  -0.0008224507  0.0028916577 -0.0008781831 -0.0031883081  8.944781e-03
## 3   0.0421621838  0.0130705599  0.0000000000 -0.0167930364 -9.789565e-03
## 4   0.0073889078  0.0006889191 -0.0024812634  0.0041746241  1.179255e-02
## 5  -0.0024405481 -0.0027333176 -0.0008492784  0.0007726969  1.141184e-02
## 6   0.0039705827 -0.0150809026 -0.0009759573  0.0020310113  9.359615e-05
## 7   0.0070371261  0.0020066487  0.0024962374 -0.0006873148  7.202007e-03
## 8   0.0042289102 -0.0029578166  0.0077655467 -0.0090663874  1.902414e-03
## 9   0.0110192846  0.0043957571  0.0098585427 -0.0127259939  1.572207e-02
## 10  0.0039341038 -0.0078641190 -0.0061327457  0.0042747254  1.661094e-03
##     2018-07-06   2018-07-09    2018-07-10   2018-07-11    2018-07-12
## 1  0.013230379  0.019348406  0.0035448367 -0.024326016  4.090626e-05
## 2  0.004085516  0.016470829  0.0023252625 -0.017387717  6.668402e-03
## 3  0.002103450 -0.001889169 -0.0090431127 -0.007640089  1.946108e-02
## 4  0.004636470 -0.017768040  0.0066470763  0.003459740  5.239076e-04
## 5  0.017406371  0.007780730  0.0002461581 -0.009658975  3.446156e-03
## 6  0.007841180  0.003569912  0.0099024402 -0.016783131 -7.701430e-03
## 7  0.006132032  0.010114855  0.0032847745 -0.003593779  8.512059e-03
## 8  0.005321490  0.026350690 -0.0062570640 -0.009160786 -2.599485e-03
## 9  0.013600114  0.005709863  0.0034480789 -0.007515485  1.908716e-02
## 10 0.007425574  0.007631400  0.0034885779 -0.010433248 -4.363527e-03
##       2018-07-13   2018-07-16    2018-07-17   2018-07-18   2018-07-19
## 1   4.867876e-03 -0.014807417 -0.0022767025  0.001214596 -0.002012756
## 2   5.235608e-03 -0.007084843  0.0080887547  0.007209239  0.001933292
## 3   2.517370e-03  0.000000000  0.0119271812 -0.009925558  0.025062677
## 4   1.874274e-05 -0.004256369 -0.0027140568 -0.007055234  0.009712834
## 5   2.669626e-03 -0.005283623 -0.0065694222 -0.004587763 -0.003762141
## 6  -1.541857e-02 -0.003578053 -0.0005944396 -0.009222596  0.005481916
## 7   8.325806e-04 -0.003616864  0.0052730498  0.003865291 -0.001972146
## 8  -7.159351e-03  0.012598295  0.0043159256  0.015016303 -0.015162640
## 9  -2.652942e-03 -0.002394465  0.0100067061  0.002875424 -0.006342116
## 10  4.434888e-03  0.003577212  0.0073994359  0.001621313  0.014010529
##       2018-07-20    2018-07-23    2018-07-24    2018-07-25    2018-07-26
## 1  -0.0034620002 -0.0034233673  0.0102084510  0.0078325140  0.0135305137
## 2  -0.0034162131 -0.0055206191  0.0023507571  0.0083359617  0.0096650627
## 3  -0.0071312548  0.0131335727 -0.0589426789  0.0725355806  0.0064218340
## 4  -0.0062096796 -0.0045346308 -0.0001479598  0.0066579812  0.0086039070
## 5  -0.0066459716  0.0016235432 -0.0013017226  0.0032500436 -0.0025782781
## 6  -0.0056548335  0.0049671313 -0.0244801448  0.0069277697  0.0121349973
## 7  -0.0001084197  0.0006892011 -0.0007583416  0.0121278133  0.0017179854
## 8  -0.0022476698  0.0157739015  0.0016265131 -0.0003614201 -0.0002047844
## 9  -0.0039916776  0.0017171076 -0.0064646147  0.0142021945 -0.0030455166
## 10 -0.0052198993  0.0045753947 -0.0121882429  0.0085691153  0.0020772885
##       2018-07-27   2018-07-30    2018-07-31    2018-08-01    2018-08-02
## 1   0.0014546713  0.013222113 -0.0027140426 -0.0161653379 -0.0026196342
## 2  -0.0008966617 -0.004236450  0.0155806867 -0.0133384042  0.0007723577
## 3   0.0075772081  0.009895112  0.0307661970  0.0020912738  0.0301650546
## 4  -0.0057343847 -0.002607408  0.0138063655 -0.0028345019  0.0043042312
## 5  -0.0041296590  0.005629008  0.0074027777 -0.0072752292  0.0029011263
## 6  -0.0344818122 -0.012369755  0.0023765964 -0.0263066435  0.0179087830
## 7  -0.0065698203 -0.009659305  0.0082086411 -0.0007021521  0.0041786003
## 8   0.0053453093  0.001904713 -0.0029612366 -0.0037971441 -0.0015952817
## 9  -0.0217630766 -0.023228530  0.0012193522  0.0010969962  0.0147379276
## 10 -0.0124641857  0.004916835  0.0007446667 -0.0231840308  0.0141771090
##      2018-08-03    2018-08-06   2018-08-07    2018-08-08   2018-08-09
## 1  -0.010233940  0.0050239144  0.004059245 -0.0088304597 -0.011146325
## 2   0.007919865  0.0018865369  0.004700065 -0.0035467835 -0.001621424
## 3  -0.006629834  0.0268817209 -0.013901427  0.0245331385  0.060936384
## 4   0.012873725  0.0002266594 -0.003832789 -0.0043559555  0.001524320
## 5   0.015055349  0.0005402166 -0.005266106 -0.0072529766  0.005185337
## 6   0.011616620 -0.0065698148 -0.003229978  0.0053762910 -0.005674625
## 7   0.001366183  0.0021993749  0.002973420 -0.0005313402 -0.001324354
## 8   0.003872503  0.0013262833  0.004371695  0.0030043987 -0.006755802
## 9   0.001297297  0.0071028279  0.003605434  0.0002292512 -0.002651602
## 10  0.001037369  0.0041604422  0.008444286  0.0085681784  0.007913891
##      2018-08-10    2018-08-13  2018-08-14   2018-08-15  2018-08-16   2018-08-17
## 1   0.008197005 -0.0148045474 0.004164327 -0.043252247 0.006158433  0.002742595
## 2  -0.010985125 -0.0097350386 0.007439660 -0.006183594 0.008502705  0.007306792
## 3   0.006400556 -0.0073640000 0.018209374 -0.029309489 0.022688537  0.001334412
## 4  -0.005342456  0.0009719751 0.003529508  0.009325750 0.009226345  0.008078485
## 5  -0.007442825  0.0020533036 0.010328821 -0.005398462 0.013826139  0.005681307
## 6  -0.006707002 -0.0011200654 0.012016787 -0.032090836 0.006257623  0.008883416
## 7  -0.006326996 -0.0028082368 0.006857404 -0.002822767 0.006583832  0.004348640
## 8  -0.015638998 -0.0081579241 0.013580302 -0.008622291 0.014895499  0.001659169
## 9  -0.010200435 -0.0019347591 0.006603682 -0.015252216 0.003003307 -0.001837494
## 10 -0.001972391 -0.0080907188 0.027171989 -0.023489566 0.004748954  0.014589614
##       2018-08-20   2018-08-21    2018-08-22    2018-08-23   2018-08-24
## 1   8.602273e-03  0.008262139  0.0139055373 -0.0055045671  0.008267983
## 2   8.524252e-03  0.008757678 -0.0090557179 -0.0062899220  0.005617281
## 3   9.495236e-03  0.022442261  0.0182375891  0.0066571247  0.005983326
## 4  -1.900039e-05 -0.009680688 -0.0073255439 -0.0009873718  0.005195784
## 5   3.803443e-03  0.005795501 -0.0061483081 -0.0047590768  0.001428455
## 6   1.914385e-02 -0.007776288  0.0065063175 -0.0019539588  0.004117846
## 7   2.979135e-03  0.001429279 -0.0002109145 -0.0016070430  0.005108551
## 8   4.525031e-03  0.005591879 -0.0019818298 -0.0092653369  0.002762638
## 9   1.668143e-03  0.007931618  0.0081557305  0.0029850589  0.017675717
## 10  1.588253e-02  0.005609392 -0.0021440605  0.0042049829 -0.008546296
##      2018-08-27    2018-08-28   2018-08-29   2018-08-30    2018-08-31
## 1   0.008941696 -0.0055103468  0.007477945 -0.002647283 -0.0075750381
## 2   0.014400185 -0.0004318317  0.003482338 -0.010293555 -0.0011595493
## 3   0.020817061  0.0136461206  0.009680820 -0.011235955  0.0074242121
## 4  -0.003971634  0.0022368854  0.002586546 -0.002759299  0.0007757062
## 5   0.005035947 -0.0008977586  0.004427884 -0.006345992  0.0006697291
## 6  -0.012434286  0.0183920291  0.010118890 -0.015824365 -0.0105498117
## 7   0.004644889  0.0003135864  0.004871695 -0.003858023  0.0025254035
## 8   0.012982398 -0.0008705840 -0.002040786 -0.009952109  0.0003218929
## 9   0.010707135  0.0031534967  0.007478941 -0.005572729  0.0004631992
## 10 -0.006417500  0.0008250578  0.001488313 -0.016775669  0.0107940404
##       2018-09-04   2018-09-05   2018-09-06    2018-09-07    2018-09-10
## 1  -0.0070592750 -0.004888021 -0.019724100 -4.883160e-03  0.0001684534
## 2  -0.0035987329  0.006790031 -0.003093572 -4.258797e-03  0.0079611455
## 3   0.0300797121 -0.013578625 -0.023090557 -1.515152e-02  0.0243077231
## 4  -0.0020224432  0.010913473  0.005815731 -9.105094e-03  0.0057552048
## 5  -0.0120022215  0.003809216 -0.002024138 -8.473313e-06  0.0013504967
## 6   0.0037820465 -0.006230806  0.002126863 -1.155581e-02  0.0096196319
## 7  -0.0004285138 -0.001921624  0.004131752 -1.337027e-03  0.0010736121
## 8   0.0040865301  0.002150200 -0.010369325 -3.209852e-03 -0.0002207457
## 9  -0.0020112376 -0.018580855 -0.011470161 -3.122961e-03  0.0065329599
## 10  0.0090589522 -0.002634218 -0.000610998 -2.435663e-03  0.0095841030
##       2018-09-11    2018-09-12    2018-09-13    2018-09-14    2018-09-17
## 1   0.0125906552  0.0094216237  9.934933e-05  0.0044610842  0.0007565535
## 2   0.0009130264  0.0023031503  4.296344e-03  0.0048152947 -0.0005478927
## 3  -0.0325923391 -0.0245303820 -1.480185e-02 -0.0428110164 -0.0158649620
## 4  -0.0016248564  0.0020770529  5.488479e-03 -0.0065181328  0.0044735104
## 5  -0.0036985013  0.0138667133  8.487749e-03 -0.0032909437 -0.0004594928
## 6  -0.0048945618  0.0092948871 -9.913287e-03 -0.0210201443  0.0116812773
## 7   0.0012624806  0.0016374196  8.553440e-03  0.0012615991 -0.0051405746
## 8   0.0012947111 -0.0120416158 -4.999120e-03  0.0118714595 -0.0068291246
## 9   0.0017926979 -0.0022187221  8.009300e-03  0.0033791890 -0.0180158502
## 10  0.0018000222  0.0008785819 -1.058478e-02 -0.0001800824 -0.0057614177
##       2018-09-18   2018-09-19    2018-09-20    2018-09-21   2018-09-24
## 1   0.0114705801  0.002798174 -0.0043917559  8.309258e-03  0.014519942
## 2   0.0049761950  0.005135099  0.0056715041 -5.909433e-04 -0.015135406
## 3   0.0003430115 -0.028115943  0.0308696425  9.582495e-03  0.031694898
## 4  -0.0063531947 -0.014233984  0.0063179378  2.562292e-03 -0.014799505
## 5   0.0031411987  0.002407114  0.0086177181 -1.450535e-05 -0.008483643
## 6   0.0011495417 -0.002418902  0.0358724217  1.464962e-02 -0.005450741
## 7   0.0067298580 -0.002218726  0.0054453566  2.156660e-03 -0.005210554
## 8   0.0045740814  0.018599329  0.0121824641 -1.329279e-03 -0.013335200
## 9   0.0106929306 -0.002237771  0.0118807027 -2.181430e-03  0.008367619
## 10  0.0064143810  0.002846331 -0.0001757148  1.407939e-03 -0.011390935
##       2018-09-25   2018-09-26    2018-09-27    2018-09-28    2018-10-01
## 1   0.0085246357 -0.009883996 -0.0004367765  0.0007319574  0.0139511626
## 2  -0.0062096321 -0.004361171 -0.0035803046 -0.0027209147  0.0026813932
## 3   0.0044356827 -0.026496549  0.0233534946  0.0008208669 -0.0707020517
## 4  -0.0061245666 -0.009566338  0.0009599424  0.0131846709 -0.0051654815
## 5  -0.0059697355  0.004027241  0.0043660961  0.0008865426  0.0012467781
## 6  -0.0087824800  0.011815491 -0.0028367799  0.0006963594  0.0023327520
## 7  -0.0002905777 -0.002750915  0.0010664761  0.0024383750  0.0009372752
## 8  -0.0053299605 -0.014376826 -0.0051507609 -0.0079691640  0.0014877535
## 9  -0.0005526985 -0.003000878  0.0066080156  0.0028804017 -0.0028228503
## 10 -0.0003542208  0.011888628 -0.0014428506  0.0015011095 -0.0041500247
##       2018-10-02    2018-10-03    2018-10-04    2018-10-05    2018-10-08
## 1  -0.0018745900  0.0140702266 -0.0075792794 -0.0020717943 -0.0049331277
## 2  -0.0007192967  0.0008544285 -0.0068644051 -0.0143323602  0.0005466162
## 3  -0.0233009701 -0.0280137890 -0.0310523992  0.0057570331 -0.0124022515
## 4   0.0051680363 -0.0111430061  0.0004933203  0.0073847854  0.0120161932
## 5   0.0067763755 -0.0003061440 -0.0038010313 -0.0034132009  0.0080274136
## 6  -0.0292476393  0.0012603126 -0.0465294258 -0.0086338916  0.0044601527
## 7  -0.0016157409 -0.0020186796 -0.0075101510 -0.0014388058 -0.0018125260
## 8  -0.0004600591  0.0135710864  0.0071106813 -0.0066480633  0.0061169843
## 9  -0.0071298716  0.0023750307 -0.0233235260 -0.0176794913 -0.0177103382
## 10 -0.0229407256  0.0030379015 -0.0156392847 -0.0004590578  0.0100873473
##       2018-10-09  2018-10-10   2018-10-11    2018-10-12    2018-10-15
## 1   0.0124229185 -0.04097909 -0.025782633  0.0059888499 -0.0060934752
## 2  -0.0248496552 -0.03159005 -0.021253444  0.0037968000 -0.0005190411
## 3   0.0038640071 -0.06408772 -0.015011289  0.0210855524  0.0034758127
## 4   0.0020736921 -0.01013827 -0.024421821  0.0009588859  0.0065783940
## 5  -0.0016443597 -0.01799719 -0.028472345  0.0093620834  0.0048798307
## 6  -0.0020867178 -0.04497825 -0.006234847  0.0165520824  0.0047664216
## 7  -0.0008205698 -0.03058480 -0.020450426  0.0112629390 -0.0003980468
## 8  -0.0069734420 -0.02789570 -0.032854462 -0.0035857475 -0.0033363777
## 9  -0.0016138833 -0.04752720 -0.009528649  0.0317364880 -0.0133225512
## 10 -0.0033571614 -0.03091620 -0.010680525  0.0221345526  0.0058508853
##    2018-10-16    2018-10-17    2018-10-18   2018-10-19   2018-10-22
## 1  0.01115067 -0.0124540861 -0.0114582006 -0.013417791 -0.010410421
## 2  0.02029746 -0.0076945146 -0.0231302093 -0.006500575 -0.007068358
## 3  0.02832109  0.0023776302 -0.0203597351 -0.027643300 -0.172234907
## 4  0.01521553 -0.0020019157  0.0007101897  0.014616273 -0.009863440
## 5  0.01975858  0.0039252142 -0.0051469232  0.007457300 -0.011459308
## 6  0.02948928 -0.0119113046 -0.0305859004 -0.017219189  0.007156364
## 7  0.02211807  0.0003522718 -0.0103212858 -0.005394535 -0.001723674
## 8  0.01034178  0.0085457687 -0.0201742370  0.003795141 -0.026631828
## 9  0.03521865  0.0008375004 -0.0223033731 -0.012288437  0.006961321
## 10 0.01710068 -0.0203790064 -0.0102005636 -0.016339023  0.010879318
##      2018-10-23  2018-10-24   2018-10-25    2018-10-26   2018-10-29 2018-10-30
## 1  -0.029362516 -0.04562188  0.014966109 -0.0103189656 -0.025214074 0.02332618
## 2  -0.007092987 -0.03570604  0.023356378 -0.0148907828 -0.006224548 0.02816530
## 3  -0.009776862 -0.07316453  0.004370363  0.0008158553 -0.006249973 0.02570407
## 4   0.001911286  0.01528583 -0.003102589 -0.0203130185  0.015054345 0.01043888
## 5  -0.003311928 -0.03903726  0.005090339 -0.0199204871  0.009917465 0.01753953
## 6  -0.005566678 -0.02012756  0.016094313 -0.0311337726  0.001037755 0.14925401
## 7  -0.008607753 -0.02597772  0.010779794 -0.0088856802 -0.004867729 0.01495025
## 8  -0.005366016 -0.03467107  0.020370074 -0.0183638394  0.007990397 0.01949656
## 9  -0.002123629 -0.05350617  0.029149480 -0.0251540714 -0.020434232 0.02836827
## 10 -0.002233431 -0.01785801  0.016199598 -0.0057243680  0.029929712 0.01954942
##      2018-10-31  2018-11-01    2018-11-02   2018-11-05    2018-11-06
## 1   0.006816483 0.009901865 -0.0109428618  0.018555781  0.0014621014
## 2   0.010098708 0.030861240  0.0004294791  0.003012493  0.0135847704
## 3   0.031191737 0.051964814 -0.0403047442  0.024327811 -0.0979999500
## 4  -0.014055138 0.003469638 -0.0086105882  0.015065251  0.0089221744
## 5   0.003367997 0.017157149 -0.0143544530  0.011240377  0.0056355213
## 6  -0.020619102 0.051989082  0.0121737492  0.001490086 -0.0100087193
## 7   0.010817368 0.006399525  0.0004950808  0.006240543  0.0055197936
## 8   0.013075842 0.011193448 -0.0016310760  0.008834670  0.0066328269
## 9   0.023048845 0.030616530 -0.0079101802 -0.006904578  0.0067142368
## 10 -0.007517953 0.011497286  0.0086827077  0.009871627  0.0008205707
##      2018-11-07    2018-11-08   2018-11-09   2018-11-12   2018-11-13
## 1   0.022763584 -0.0279779331 -0.003977968 -0.028896189 -0.025667390
## 2   0.016644820 -0.0059255010 -0.014389669 -0.016491823  0.004996446
## 3   0.051829238 -0.0695652156 -0.036533587  0.087301646  0.009732251
## 4   0.008456520  0.0006626919  0.005734313  0.001970723  0.001387374
## 5   0.015334432 -0.0038562883 -0.008833036 -0.006459755 -0.002891639
## 6  -0.061400220 -0.0175014576 -0.003424412  0.003733015 -0.010312611
## 7   0.019408417  0.0016667421 -0.002407997 -0.016533978 -0.003089991
## 8   0.011117884  0.0020460200 -0.010078793 -0.017629228  0.005880597
## 9   0.028741090  0.0011837278 -0.026533606 -0.040657999  0.003822394
## 10  0.002686674  0.0125439971 -0.014677606 -0.009050224  0.001460708
##      2018-11-14    2018-11-15    2018-11-16   2018-11-19   2018-11-20
## 1   0.003056712  0.0204367796  0.0087739207 -0.005510447 -0.040142811
## 2  -0.002768186  0.0130204291  0.0030213262 -0.013364418 -0.017910934
## 3   0.008032236  0.0143425490  0.0010474208 -0.005754695  0.002894002
## 4  -0.003968489 -0.0046827359  0.0107219718  0.002030012 -0.007016541
## 5  -0.006680480  0.0009516148  0.0002236666 -0.008997463 -0.020629842
## 6   0.008997432  0.0049399759 -0.0030274281 -0.023873696 -0.025369756
## 7  -0.006670144  0.0116369279  0.0067402419 -0.015567541 -0.013064750
## 8  -0.014107766  0.0129622049 -0.0035958448 -0.002917412 -0.020750162
## 9  -0.006290939  0.0250978123 -0.0047271059 -0.047032801 -0.005977836
## 10 -0.013310246 -0.0125685232 -0.0116833017 -0.018725644 -0.040683528
##      2018-11-21    2018-11-23  2018-11-26   2018-11-27  2018-11-28
## 1   0.019085083 -0.0365977773 0.021449561 -0.006018243 0.016015822
## 2   0.010441620 -0.0011443963 0.014989363 -0.005379398 0.021022168
## 3  -0.020199397 -0.0050869615 0.049246555 -0.028212438 0.017418844
## 4  -0.007205334  0.0002767069 0.001992582  0.006488272 0.001633862
## 5   0.004695085 -0.0033997786 0.008529753  0.009436897 0.018012414
## 6   0.025140280  0.0013054052 0.024831329 -0.012790536 0.025601171
## 7   0.003713427 -0.0006914029 0.011219229 -0.001119745 0.022808032
## 8   0.006652269 -0.0043189544 0.024158785 -0.005284478 0.018002106
## 9   0.011098152 -0.0043427684 0.027146861  0.002509348 0.032765817
## 10  0.021905134 -0.0023011150 0.015542793  0.003006949 0.013702128
##       2018-11-29   2018-11-30  2018-12-03   2018-12-04   2018-12-06
## 1   6.148792e-03 -0.006325286 0.026717664 -0.034175756 -0.025456988
## 2  -4.583827e-03  0.008348400 0.014387256 -0.043469158 -0.005599437
## 3   1.841761e-02  0.028782503 0.003466205 -0.062422873  0.001052658
## 4   2.199132e-05  0.013052474 0.003942276 -0.006823531  0.010470297
## 5   1.114787e-03 -0.004001853 0.003912594 -0.023981973  0.000737530
## 6   9.790854e-03  0.011923396 0.008186916 -0.035915469  0.010435450
## 7  -1.373260e-03  0.004571330 0.008552870 -0.026274931 -0.005388248
## 8  -1.124658e-02  0.009000794 0.003082248 -0.052136636 -0.015896970
## 9  -8.013880e-03  0.007851787 0.022060319 -0.043983311  0.007674433
## 10 -8.881309e-03  0.003592097 0.012809931 -0.032821006  0.000835305
##      2018-12-07   2018-12-10    2018-12-11   2018-12-12   2018-12-13
## 1  -0.006780232 -0.022570773 -1.766926e-03  0.012147228 -0.001884312
## 2  -0.028208646 -0.006139098 -4.630299e-03  0.007634287 -0.011030741
## 3  -0.027602497 -0.008921384 -6.273868e-03  0.014822976  0.027860400
## 4  -0.005780874 -0.000668987  4.809378e-03 -0.010891587  0.009144740
## 5  -0.018912842 -0.004455976 -2.284799e-03  0.006800195 -0.004737673
## 6  -0.023800039 -0.030173882 -9.661122e-03 -0.041004302 -0.046881818
## 7  -0.019984046  0.004405560 -2.228687e-05  0.007237402 -0.002095760
## 8  -0.020740007 -0.018930524 -1.261705e-02  0.008863699 -0.014106619
## 9  -0.039234204  0.012720573  2.684961e-03  0.012600918 -0.007303151
## 10 -0.034276620  0.002452843 -4.247659e-03  0.011143110 -0.017358513
##      2018-12-14   2018-12-17   2018-12-18   2018-12-19   2018-12-20
## 1  -0.029336122 -0.019352282 -0.022654426 -0.013204994 -0.028575670
## 2  -0.009633473 -0.019609241  0.004430270 -0.019222002 -0.015347254
## 3  -0.039210579 -0.064365876  0.046252986 -0.041410294 -0.047577265
## 4  -0.004675184 -0.034101645 -0.000472998 -0.004732855 -0.007327090
## 5  -0.018665145 -0.015789359 -0.014756056 -0.014034106 -0.022073685
## 6  -0.013733872 -0.039133195  0.005274900 -0.038867541 -0.036916627
## 7  -0.018439996 -0.019062770 -0.001934337 -0.011227961 -0.018565500
## 8  -0.013830399 -0.008949145 -0.007170497 -0.019007629 -0.007302266
## 9  -0.020874974 -0.023941234  0.011680439 -0.029163313 -0.016142801
## 10 -0.010526239 -0.022412294  0.004111106 -0.018619423 -0.020096893
##      2018-12-21   2018-12-24 2018-12-26    2018-12-27    2018-12-28
## 1  -0.015271980 -0.044090019 0.07118951  0.0042271745 -0.0097704415
## 2  -0.019014858 -0.025740463 0.04904494  0.0094431419 -0.0052368214
## 3  -0.061599753 -0.006205127 0.07722639  0.0118974683  0.0135665067
## 4  -0.009893661 -0.041911660 0.02554223  0.0053546526  0.0005053989
## 5  -0.031031617 -0.020769290 0.04071062 -0.0007565322  0.0001404716
## 6  -0.045031128 -0.016539024 0.06331100  0.0002329840 -0.0045310636
## 7  -0.015247631 -0.023742530 0.03961691  0.0137016375 -0.0006260089
## 8  -0.018998426 -0.019527663 0.05029160  0.0078091181 -0.0014399951
## 9  -0.028028995 -0.024168568 0.06246057  0.0115646542 -0.0011372936
## 10 -0.014589913 -0.010947540 0.05443842  0.0021567094 -0.0025467586
##      2018-12-31   2019-01-02    2019-01-03 2019-01-04  2019-01-07  2019-01-08
## 1   0.006456026  0.018934200 -0.0068040359 0.04480931 0.020424497 0.011914149
## 2   0.008235904  0.007674563 -0.0287565498 0.04270896 0.010646452 0.013287479
## 3  -0.022308151  0.041070947 -0.0771478937 0.07536412 0.078327446 0.110868408
## 4   0.003151921 -0.019649719  0.0040544863 0.01122349 0.004258097 0.015108576
## 5   0.005502644  0.011648676  0.0009514952 0.03025055 0.010807924 0.011562557
## 6   0.008908671  0.015416813 -0.0317893837 0.06518064 0.047956588 0.015403992
## 7   0.011195438 -0.009432451 -0.0247124773 0.03118747 0.003744972 0.009879408
## 8   0.008343250  0.015789745 -0.0156475488 0.03863878 0.005650392 0.002605368
## 9   0.008236568  0.002011376 -0.0428379750 0.04790346 0.020832316 0.007710849
## 10  0.012570567  0.007893225 -0.0119779942 0.01823873 0.029259172 0.010813279
##       2019-01-09    2019-01-10    2019-01-11    2019-01-14    2019-01-15
## 1   0.0214663195  0.0070154418 -0.0051453937 -0.0001068403  0.0045448116
## 2   0.0107302444  0.0073629924  0.0013346987 -0.0029861793 -0.0046655977
## 3   0.0122910521  0.0070422778 -0.0110924280  0.0124360406  0.0722543353
## 4  -0.0046266730  0.0148200525  0.0004690556 -0.0076883127  0.0119218564
## 5  -0.0002021558  0.0072119147  0.0039341056 -0.0038065143  0.0053050647
## 6   0.0024858505 -0.0012238388  0.0196272284  0.0011025999  0.0090727502
## 7   0.0030176941  0.0080709773  0.0011915460 -0.0055073864  0.0111375049
## 8   0.0100042782 -0.0004827736  0.0030250262  0.0080313866  0.0092033433
## 9   0.0183498240  0.0081980954 -0.0008808085 -0.0139251637  0.0168026724
## 10  0.0086006183 -0.0191480851  0.0019233953 -0.0037319808  0.0008160965
##       2019-01-16  2019-01-17  2019-01-18   2019-01-22    2019-01-23
## 1   0.0006225251 0.012588750 0.020516528 -0.025881514 -0.0139925124
## 2   0.0013612867 0.013697743 0.017136792 -0.021715190 -0.0064770046
## 3   0.0022461590 0.017929158 0.017173118 -0.031168895 -0.0180964485
## 4   0.0025809220 0.005144429 0.004208833 -0.002275472  0.0052935181
## 5  -0.0015278336 0.004961335 0.013654370 -0.016821225  0.0037758715
## 6  -0.0073864628 0.021016129 0.010077461 -0.014104143 -0.0096270731
## 7   0.0002826256 0.008323762 0.014163217 -0.007444989  0.0055280671
## 8   0.0263321323 0.006417563 0.018311941 -0.009884454 -0.0002749251
## 9  -0.0022902675 0.008584956 0.019702313 -0.022857809 -0.0044027415
## 10 -0.0038397004 0.014827578 0.023542428 -0.015275991  0.0010884267
##       2019-01-24   2019-01-25    2019-01-28    2019-01-29   2019-01-30
## 1   0.0018277208  0.020269006 -0.0149593645  0.0072686284  0.020723360
## 2   0.0105346877  0.014733325 -0.0040431935  0.0112460435  0.014521073
## 3  -0.0163822749  0.015961092 -0.0211747733 -0.0111654798  0.013879088
## 4  -0.0022560009 -0.001265646  0.0037923048  0.0044858680  0.006994825
## 5  -0.0010155000  0.009823728 -0.0026636804  0.0001654900 -0.002219753
## 6   0.0101832630  0.031120219 -0.0014781764 -0.0189958555  0.002374046
## 7  -0.0004141748  0.006355271 -0.0037585270  0.0026443561  0.012590902
## 8   0.0056223661  0.010669833 -0.0010920364 -0.0070466152 -0.003415347
## 9   0.0268202084  0.021082593 -0.0141199851 -0.0137834391  0.026150552
## 10 -0.0030617864  0.014132427 -0.0009823253 -0.0003916924  0.004339525
##       2019-01-31    2019-02-01    2019-02-04   2019-02-05    2019-02-06
## 1  -0.0007308137  0.0090453019  0.0070605183 -0.001316566 -0.0078991692
## 2   0.0031348231  0.0027702668  0.0054502459  0.005835526 -0.0015139580
## 3  -0.0176333651 -0.0023617855  0.0061552554  0.029411765  0.0134857143
## 4   0.0139843722 -0.0047482641  0.0050774401  0.001355200 -0.0031949644
## 5   0.0215356972 -0.0000288866 -0.0007992307  0.004318217 -0.0037177122
## 6   0.0064726708 -0.0014458986  0.0067284910  0.012662412 -0.0148130252
## 7   0.0046001350  0.0057093904  0.0052757831  0.004965675  0.0008310123
## 8  -0.0011034762  0.0038367580  0.0056659031 -0.002006878 -0.0005345201
## 9   0.0127040999  0.0072948734  0.0066659816  0.007626970  0.0011650848
## 10  0.0016286558 -0.0061449527  0.0076770861  0.010169734  0.0009335215
##      2019-02-07    2019-02-08    2019-02-11    2019-02-12    2019-02-13
## 1  -0.032660196 -0.0072849821  0.0100890716  0.0091109263  0.0197681365
## 2  -0.012267260 -0.0004762901  0.0050359669  0.0186528870  0.0044866338
## 3  -0.011727560  0.0244180511  0.0325239259 -0.0882416435 -0.0002365831
## 4   0.006929315  0.0039295860  0.0013301623 -0.0003444053  0.0016601003
## 5  -0.006398211  0.0002490855 -0.0004883910  0.0058612013  0.0031286032
## 6  -0.023555826  0.1485982126  0.0190198510  0.0713405187  0.0095428677
## 7  -0.004580784  0.0041323408 -0.0008505548  0.0104082313  0.0043384095
## 8  -0.001276889 -0.0094885980  0.0049551951  0.0203091433  0.0020006500
## 9  -0.016372707  0.0080863740  0.0011198317  0.0198327205  0.0026417110
## 10  0.006675294 -0.0067079961  0.0064030164  0.0122311922  0.0020121244
##       2019-02-14   2019-02-15
## 1   0.0075799398  0.016975751
## 2  -0.0036946113  0.010291083
## 3   0.0087573728 -0.021820742
## 4  -0.0024342157  0.006083278
## 5   0.0003761233  0.015013138
## 6  -0.0050412271 -0.063611363
## 7  -0.0026920071  0.011096203
## 8  -0.0115933357  0.025028326
## 9   0.0026617120  0.007643313
## 10 -0.0008146489  0.008606333
## 
## Clustering vector:
##   [1]  7  2 10  9  7  5  9  7  7  9  9  5  7  2  9  4  4  4  7  5  8  4  7  7  9
##  [26]  2  9  2  7  2  9  9  9  2  8  5  8  4  9  9  9  7  7  2  1  1  2  7  2  4
##  [51]  2  4  9  4  9  2  4  8 10  2  8  7  8 10  7  8  8  1  5  8  7  8  7  7  7
##  [76]  7  2  4  8  4  5  2  7  7  2  5  4  9  2  5  5  2  8  4  7  5  7  7  4  4
## [101]  8  5  7 10  2  4  7  4  8  5  7  1  7  6  4 10  7  9  9  2  7  5  7  7  5
## [126]  1  1  4  2  2  8 10  7  2  7  5  5  5  5  4 10  2  4  7  4  4  5  1  2  7
## [151]  9  7  7  4  7  4  7  2  2  1  4  4  4  4  8  2  4  4  9  4  2  7  4  2  1
## [176]  7  9  2  1  2  4  9  7  7  8 10  7  2  2  7  2  5  5  8  4  1  9  2  7  5
## [201]  5  4  2  2  9  9  7  9 10  7  8  2  7  1  7  8 10  7  4  2  1  1  7  7  7
## [226]  2  7  7  1  9  2  7  4  7  5  7  4  7  7  7  9  7  9  9  7  9  9  2  5  9
## [251]  7  2  4  9  7  2  8  2  2  2  8  7  7  7  8 10  4  8  9  5  4  9  4  1  2
## [276]  4 10 10  2  7 10  2  2  5  7  2  7  7  7  8  4 10  9  2  7  2 10  9  4  4
## [301]  7  2  6  7  9  5  7  5  7  8  2  2  4  2  7  2  7  5  2  1  7  1  8  7  9
## [326]  7  8  7  9  9  2  1  2  7  4  7  9  4  7  3  5  7  1  7  2  9  8  2  9  2
## [351]  7  7  4  1  5  7 10  1  7  8  2  4  4  7  8  4  7  2  2  2  7  4  5  8  2
## [376]  4  2  4  5  8  4  1 10  2  1  9  2  9  2  7  4  5  8  2 10  8 10  7  2  7
## [401]  7 10  7  7  4  7  8  2  2  8  4  1  4  2  9  4  4  7  4  8  8  9  7  2  9
## [426]  8  7  7  7  5  5  2  2  7 10 10 10  8  7 10  9  8  7 10  4  7  9  9  9  2
## [451]  6  6  2  4  5 10  7  8  2  2  2  8  2  9  7 10  5  1  2  4  7  7  9  4  4
## [476]  7  5  7  9  4  4  8  2  7  7  1  5  2  7  5  2  1  4  9  1  5  2  2  7  7
## [501]  8  7
## 
## Within cluster sum of squares by cluster:
##  [1] 1.4608721 5.1714436 0.0000000 1.9768890 2.9347019 0.5638137 5.3152104
##  [8] 1.2836711 4.8632869 2.5438801
##  (between_SS / total_SS =  22.3 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"
cat(business_case_context)
## 
## In our business case, we applied k-means clustering with 10 centers to segment our customer base 
## based on their purchasing behavior

Next, we’ll combine the clusters from the k_means_obj with the umap_results_tbl.

  • Begin with the k_means_obj
  • Augment the k_means_obj with the stock_date_matrix_tbl to get the clusters added to the end of the tibble
  • Select just the symbol and .cluster columns
  • Left join the result with the umap_results_tbl by the symbol column
  • Left join the result with the result of sp_500_index_tbl %>% select(symbol, company, sector) by the symbol column.
  • Store the output as umap_kmeans_results_tbl
# Use your dplyr & broom skills to combine the k_means_obj with the umap_results_tbl
# Load necessary libraries
library(dplyr)
library(broom)
library(umap)

# Assuming `stock_date_matrix_tbl` and `umap_results_tbl` are already created and available

# Step 1: Create the k_means_obj for 10 centers
set.seed(123) # For reproducibility
k_means_obj <- kmeans(select(stock_date_matrix_tbl, -symbol), centers = 10, nstart = 20)

# Step 2: Augment the k_means_obj with the original data
k_means_augmented <- augment(k_means_obj, stock_date_matrix_tbl)

# Step 3: Select just the `symbol` and `.cluster` columns
clusters_tbl <- k_means_augmented %>%
  select(symbol, .cluster)

# Step 4: Left join the result with the `umap_results_tbl` by the `symbol` column
umap_clusters_tbl <- left_join(umap_results_tbl, clusters_tbl, by = "symbol")

# Assuming `sp_500_index_tbl` is available and contains the columns `symbol`, `company`, `sector`
# Step 5: Left join the result with `sp_500_index_tbl` to get company and sector information
sp_500_selected <- sp_500_index_tbl %>%
  select(symbol, company, sector)

umap_kmeans_results_tbl <- left_join(umap_clusters_tbl, sp_500_selected, by = "symbol")

# Step 6: Store the final output
# View the final result (optional)
print(umap_kmeans_results_tbl)
## # A tibble: 502 × 6
##         V1      V2 symbol .cluster company                       sector         
##      <dbl>   <dbl> <chr>  <fct>    <chr>                         <chr>          
##  1 -0.764   1.65   A      3        Agilent Technologies Inc.     Health Care    
##  2 -2.70    0.455  AAL    6        American Airlines Group Inc.  Industrials    
##  3  0.739  -0.0320 AAP    7        Advance Auto Parts Inc.       Consumer Discr…
##  4  0.0130  3.09   AAPL   10       Apple Inc.                    Information Te…
##  5 -0.965  -0.0193 ABBV   3        AbbVie Inc.                   Health Care    
##  6 -0.506  -0.659  ABC    3        AmerisourceBergen Corporation Health Care    
##  7  0.436   3.10   ABMD   10       ABIOMED Inc.                  Health Care    
##  8 -0.262   1.35   ABT    3        Abbott Laboratories           Health Care    
##  9  0.0598  1.63   ACN    3        Accenture Plc Class A         Information Te…
## 10  0.570   3.43   ADBE   10       Adobe Inc.                    Information Te…
## # ℹ 492 more rows
# Output: umap_kmeans_results_tbl 

Plot the K-Means and UMAP results.

  • Begin with the umap_kmeans_results_tbl
  • Use ggplot() mapping V1, V2 and color = .cluster
  • Add the geom_point() geometry with alpha = 0.5
  • Apply colors as you desire (e.g. scale_color_manual(values = palette_light() %>% rep(3)))
# Check the structure of umap_kmeans_results_tbl
str(umap_kmeans_results_tbl)
## tibble [502 × 6] (S3: tbl_df/tbl/data.frame)
##  $ V1      : num [1:502] -0.764 -2.698 0.739 0.013 -0.965 ...
##  $ V2      : num [1:502] 1.6538 0.4552 -0.032 3.0872 -0.0193 ...
##  $ symbol  : chr [1:502] "A" "AAL" "AAP" "AAPL" ...
##  $ .cluster: Factor w/ 10 levels "1","2","3","4",..: 3 6 7 10 3 3 10 3 3 10 ...
##  $ company : chr [1:502] "Agilent Technologies Inc." "American Airlines Group Inc." "Advance Auto Parts Inc." "Apple Inc." ...
##  $ sector  : chr [1:502] "Health Care" "Industrials" "Consumer Discretionary" "Information Technology" ...
# Load necessary libraries
library(ggplot2)
library(scales) # For the palette_light function

# Assuming umap_kmeans_results_tbl is already created
# Define a custom color palette
palette_light <- function() {
  c("#F8766D", "#00BA38", "#619CFF", "#F564E3", "#00C1D4", "#F28442", "#00B0F6", "#B79F00", "#00BA38", "#619CFF")
}

# Plot the UMAP results with K-Means clusters
ggplot(umap_kmeans_results_tbl, aes(x = V1, y = V2, color = factor(.cluster))) +
  geom_point(alpha = 0.5) +
  scale_color_manual(values = palette_light() %>% rep(3)) +  # Customize colors
  labs(title = "UMAP Projection with K-Means Clusters", color = "Cluster") +
  theme_minimal() +
  theme(legend.position = "right")

Congratulations! You are done with the 1st challenge!