library(tidyverse)
library(praise)Market Indicators
Today we are measuring different market indicators using data from the Curated open data on GitHub.
Data
We collect data over time on: gold prices, natural gas prices, oil prices, the yield rate of the US 10y bond, volatility index, and the S & P 500.
Code
gold <- readr::read_csv(
'https://raw.githubusercontent.com/datasets/gold-prices/refs/heads/main/data/monthly-processed.csv'
) |>
rename(gold_price = Price)
oil <- readr::read_csv(
'https://raw.githubusercontent.com/datasets/oil-prices/refs/heads/main/data/wti-monthly.csv'
) |>
rename(oil_price = Price)
nat_gas <- readr::read_csv(
'https://raw.githubusercontent.com/datasets/natural-gas/refs/heads/main/data/monthly-processed.csv'
) |>
rename(gas_price = Price)
vix <- readr::read_csv(
'https://raw.githubusercontent.com/datasets/finance-vix/refs/heads/main/data/vix-monthly.csv'
) |> # volatility index
rename(vol_idx = Close)
bond <- readr::read_csv(
'https://raw.githubusercontent.com/datasets/bond-yields-us-10y/refs/heads/main/data/monthly.csv'
) |> # bond yields us 10y
rename(bond_rate = Rate)
temp <- readr::read_csv(
'https://raw.githubusercontent.com/datasets/global-temp/refs/heads/main/data/monthly.csv'
) |>
mutate(Date = lubridate::as_date(paste0(Year, "-01"))) |>
rename(temp_diff = Mean) |>
select(-Year, -Source)
# difference of that month from the average over all of the 20th century
sp <- readr::read_csv(
'https://raw.githubusercontent.com/datasets/s-and-p-500/refs/heads/main/data/data.csv'
) |>
select(Date, SP500)
full_data <- gold |>
full_join(oil) |>
full_join(nat_gas) |>
full_join(vix) |>
full_join(bond) |>
full_join(temp) |>
full_join(sp)Making a single plot
We start by making a single line plot over time, measuring oil prices.
full_data |>
select(Date, oil_price) |>
drop_na() |>
ggplot(aes(x = Date, y = oil_price)) +
geom_line() +
theme_bw()Write a function
The function takes a variable name as the argument and makes a line plot over time for that variable. Note the { } needed due to the non-standard evaluation.
plot_ts <- function(variable, log = TRUE) {
if (log) {
full_data |>
select(Date, {{ variable }}) |>
drop_na() |>
ggplot(aes(x = Date, y = {{ variable }})) +
geom_line() +
theme_bw() +
scale_y_log10() +
labs(x = "date")
} else {
full_data |>
select(Date, {{ variable }}) |>
drop_na() |>
ggplot(aes(x = Date, y = {{ variable }})) +
geom_line() +
theme_bw() +
labs(x = "date")
}
}
plot_ts(SP500, log = TRUE)plot_ts(SP500, log = FALSE)Mapping the function
Unforunately, the non-standard evaluation means that the function we wrote cannot be directly mapped. map() will go through the variables vector one at a time (e.g., variables[1], variables[2], etc.), and the name of the variable then needs to be translated into something that can be used in the tidy pipeline.
variables <- c("vol_idx", "gold_price", "oil_price")
map(
variables,
\(v, log = TRUE) plot_ts(!!rlang::sym(v), log = log),
log = FALSE
)[[1]]
[[2]]
[[3]]
Plotting multiple variables
Instead of looking at a single variable at a time, we might be interested in looking at multiple variables on the same plot. It is a little bit tricky because the variables are meausured on quite different scales, but some are comparable.
full_data |>
select(Date, bond_rate, temp_diff, SP500) |>
drop_na() |>
pivot_longer(
cols = -Date,
names_to = "product",
values_to = "value"
) |>
ggplot(aes(x = Date, y = value, color = product)) +
geom_line() +
theme_bw() +
scale_color_brewer(palette = "Dark2") +
scale_y_log10() +
labs(y = "", title = "Comparison over time, log10 scale")Using Shiny
We also played around with Shiny so the user could choose which variable to plot:
praise()[1] "You are swell!"