K-Means Clustering Analysis of Apple, Microsoft, and Nvidia

[This article was first published on DataGeeek, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The exemption of key electronic components from tariffs is likely to positively impact Apple more, which manufactures approximately 90% of its iPhones in China, compared to other tech giants like Microsoft and Nvidia.

Furthermore, the k-means clustering chart shows that Apple has diverged year to date, specifically after the April 2 tariffs.

Source code:

library(tidyverse)
library(tidyquant)
library(timetk)

#Apple Inc. (AAPL)
df_apple <- 
  tq_get("AAPL", from = "2025-01-01") %>% 
  tq_transmute(mutate_fun = periodReturn,
               period = "daily") %>% 
  mutate(symbol = "Apple")
#NVIDIA Corporation (NVDA)
df_nvda <- 
  tq_get("NVDA", from = "2025-01-01") %>% 
  tq_transmute(mutate_fun = periodReturn,
               period = "daily") %>% 
  mutate(symbol = "NVIDIA")
#Microsoft Corporation (MSFT)
df_msft <- 
  tq_get("MSFT", from = "2025-01-01") %>% 
  tq_transmute(mutate_fun = periodReturn,
               period = "daily") %>% 
  mutate(symbol = "Microsoft")

#Merging the datasets
df_merged <- 
  df_apple %>% 
  rbind(df_nvda) %>% 
  rbind(df_msft)

#TS Features
tsfeature_tbl <- 
  df_merged %>%
  group_by(symbol) %>%
  tk_tsfeatures(
    .date_var = date,
    .value    = daily.returns,
    .features = c("frequency", 
                  "stl_features", 
                  "entropy", 
                  "acf_features",
                  "mean"),
    .scale    = TRUE,
    .prefix   = "ts_"
  ) %>%
  ungroup()

#Clustering with K-Means
set.seed(123)
cluster_tbl <- tibble(
  cluster = tsfeature_tbl %>% 
    select(-symbol) %>%
    as.matrix() %>%
    kmeans(centers = 2) %>%
    pluck("cluster")
) %>%
  bind_cols(
    tsfeature_tbl
  )


#Visualize the Cluster Assignments
cluster_tbl %>%
  select(cluster, symbol) %>%
  right_join(df_merged, by = "symbol") %>%
  group_by(symbol) %>%
  plot_time_series(
    date, 
    daily.returns, 
    .smooth = FALSE,
    .line_size = 1,
    .color_var   = cluster, 
    .facet_ncol  = 1, 
    .interactive = FALSE,
    .title = "K-Means Clustering Chart"
  ) + 
  labs(subtitle = "Year to Date-Daily Returns") +
  scale_y_continuous(labels = scales::percent) +
  theme_tq(base_family = "Roboto Slab", base_size = 16) +
  theme(plot.title = ggtext::element_markdown(face = "bold"),
        strip.text = element_text(face = "bold"),
        plot.background = element_rect(fill = "azure", color = "azure"),
        legend.position = "none")
To leave a comment for the author, please follow the link and comment on their blog: DataGeeek.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)