Weathering the Storm

By Carl Goodwin in R

August 2, 2020

Covid-19 began battering the financial markets in 2020 Which sectors are faring best?

I’ll compare each sector in the S&P 500 with the overall market. Baselining each at 100% as of February 19th, we’ll see which were the first to recover lost ground.

library(tidyverse)
library(wesanderson)
library(kableExtra)
library(scales, exclude = "date_format")
library(glue)
library(tidyquant)
library(clock)
theme_set(theme_bw())

(cols <- wes_palette("Moonrise2"))
symbols <-
  c(
    "SPY",
    "XLV",
    "XLK",
    "XLE",
    "XLF",
    "XLC",
    "XLI",
    "XLY",
    "XLP",
    "XLRE",
    "XLU",
    "XLB"
  )

from <- "2020-02-19"

from_formatted <- date_parse(from, format = "%Y-%m-%d") |> 
  date_format(format = "%b %d, %Y")

Note this patch if having prob lems with tq_get

eod_sectors <-
  tq_get(symbols, from = from) |>
  group_by(symbol) |>
  mutate(
    norm_close = adjusted / first(adjusted),
    type = if_else(symbol == "SPY", "Market", "Sector"),
    sector = case_when(
      symbol == "SPY"  ~ "S&P 500",
      symbol == "XLB"  ~ "Materials",
      symbol == "XLE"  ~ "Energy",
      symbol == "XLU"  ~ "Utilities",
      symbol == "XLI"  ~ "Industrical",
      symbol == "XLRE" ~ "Real Estate",
      symbol == "XLV"  ~ "Health",
      symbol == "XLK"  ~ "Technology",
      symbol == "XLF"  ~ "Financial",
      symbol == "XLC"  ~ "Communication",
      symbol == "XLY"  ~ "Consumer Discretionary",
      symbol == "XLP"  ~ "Consumer Staples",
      TRUE             ~ "Other"
    )
  ) |>
  ungroup() |> 
  drop_na()

Perhaps not too surprising to see that Tech led the way back. Energy has proven the most volatile, falling further and then recovering faster. And Comms, with all that home-working, benefited during the lockdown, but has faded since.

eod_sectors |>
  mutate(
    sector = str_wrap(sector, 12),
    sector = fct_reorder(sector, norm_close, last, .desc = TRUE)
  ) |>
  ggplot(aes(date, norm_close, colour = type)) +
  geom_rect(aes(xmin = min(date), xmax = max(date), ymin = -Inf, ymax = Inf),
    fill = if_else(eod_sectors$type == "Market", cols[1], NULL), colour = "white"
  ) +
  geom_hline(yintercept = 1, linetype = "dashed", colour = "grey80") +
  geom_line(key_glyph = "timeseries") +
  facet_wrap(~sector) +
  scale_colour_manual(values = cols[c(3, 4)]) +
  scale_y_continuous(labels = label_percent()) +
  labs(
    title = "S&P 500 Sector Impact of Covid-19",
    subtitle = glue("Relative to {from_formatted}"),
    x = NULL, y = NULL, colour = NULL
  ) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

R Toolbox

Summarising below the packages and functions used in this post enables me to separately create a toolbox visualisation summarising the usage of packages and functions across all posts.

Package Function
base c[1]; conflicts[1]; cumsum[1]; function[1]; max[1]; min[1]; search[1]; sum[1]
clock date_format[1]; date_parse[1]
dplyr filter[5]; arrange[2]; case_when[1]; desc[2]; group_by[2]; if_else[5]; mutate[6]; summarise[1]; ungroup[1]
forcats fct_reorder[1]
ggplot2 aes[2]; element_text[1]; facet_wrap[1]; geom_hline[1]; geom_line[1]; geom_rect[1]; ggplot[1]; labs[1]; scale_colour_manual[1]; scale_y_continuous[1]; theme[1]; theme_bw[1]; theme_set[1]
glue glue[1]
kableExtra kbl[1]
purrr map[1]; map2_dfr[1]; possibly[1]; set_names[1]
readr read_lines[1]
scales label_percent[1]
stringr str_c[5]; str_count[1]; str_detect[2]; str_remove[2]; str_remove_all[1]; str_starts[1]; str_wrap[1]
tibble as_tibble[1]; tibble[2]; enframe[1]
tidyquant tq_get[1]
tidyr drop_na[1]; unnest[1]
wesanderson wes_palette[1]
Posted:
August 2, 2020
Updated:
April 30, 2022
Length:
3 minute read, 440 words
Categories:
R
Tags:
quant
See Also:
The Goldilocks Principle