R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

#install.packages(c("tidyverse", "tidyquant", "lubridate"))

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## āœ” dplyr     1.2.0     āœ” readr     2.1.6
## āœ” forcats   1.0.1     āœ” stringr   1.6.0
## āœ” ggplot2   4.0.2     āœ” tibble    3.3.1
## āœ” lubridate 1.9.5     āœ” tidyr     1.3.2
## āœ” purrr     1.2.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## āœ– dplyr::filter() masks stats::filter()
## āœ– dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidyquant)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo 
## ── Attaching core tidyquant packages ─────────────────────── tidyquant 1.0.11 ──
## āœ” PerformanceAnalytics 2.0.8      āœ” TTR                  0.24.4
## āœ” quantmod             0.4.28     āœ” xts                  0.14.2── Conflicts ────────────────────────────────────────── tidyquant_conflicts() ──
## āœ– zoo::as.Date()                 masks base::as.Date()
## āœ– zoo::as.Date.numeric()         masks base::as.Date.numeric()
## āœ– dplyr::filter()                masks stats::filter()
## āœ– xts::first()                   masks dplyr::first()
## āœ– dplyr::lag()                   masks stats::lag()
## āœ– xts::last()                    masks dplyr::last()
## āœ– PerformanceAnalytics::legend() masks graphics::legend()
## āœ– quantmod::summary()            masks base::summary()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)

tickers <- c("BIMBOA.MX","CEMEXCPO.MX","MSFT","AMZN","SHELL.AS","ASML.AS")

start_date <- as.Date("2011-02-01")
end_date   <- as.Date("2024-02-01")
cut_date   <- as.Date("2020-01-01")

Including Plots

You can also embed plots, for example:

prices <- prices %>%
  mutate(period = if_else(date < cut_date, "Before 2020", "2020 onwards"))
prices %>%
  filter(period == "Before 2020") %>%
  ggplot(aes(x = symbol, y = adjusted)) +
  geom_boxplot() +
  labs(
    title = "Stock Prices (Adjusted) Before 2020",
    x = "Stock",
    y = "Adjusted Close Price"
  ) +
  theme_minimal()

prices %>%
  filter(period == "2020 onwards") %>%
  ggplot(aes(x = symbol, y = adjusted)) +
  geom_boxplot() +
  labs(
    title = "Stock Prices (Adjusted) From 2020 Onwards",
    x = "Stock",
    y = "Adjusted Close Price"
  ) +
  theme_minimal()

prices %>%
  ggplot(aes(x = symbol, y = adjusted, fill = period)) +
  geom_boxplot(position = "dodge") +
  labs(
    title = "Comparison of Stock Prices (Adjusted): Before 2020 vs 2020 Onwards",
    x = "Stock",
    y = "Adjusted Close Price",
    fill = "Period"
  ) +
  theme_minimal()

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.