[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.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
According to the simulation results, MicroStrategy has had a positive trend with less volatility for the last ten years compared to AMAZON, which has had a negative trend with high volatility.
Source code:
library(tidyverse)
library(tidyquant)
df_port <-
tq_get(c("AMZN","MSTR")) %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "yearly",
type = "arithmetic") %>%
mutate(date = floor_date(date, "year") %>% year(),
symbol = case_when(
symbol == "AMZN" ~ "AMAZON",
symbol == "MSTR" ~ "MicroStrategy"
)) %>%
group_by(symbol) %>%
slice_min(n = -1, order_by = date) %>%
ungroup() %>%
pivot_wider(names_from = symbol,
values_from = yearly.returns)
#Simulation
library(rsample)
set.seed(123)
port_intervals <-
reg_intervals(date ~ AMAZON + MicroStrategy,
data = df_port,
type = "percentile",
keep_reps = TRUE)
#Bootstrap confidence intervals
##https://juliasilge.com/blog/superbowl-conf-int/
port_intervals %>%
mutate(
term = str_remove(term, "TRUE"),
term = fct_reorder(term, .estimate)
) %>%
ggplot(aes(.estimate, term)) +
geom_vline(xintercept = 0, size = 1.5, lty = 2, color = "gray80") +
geom_errorbarh(aes(xmin = .lower, xmax = .upper),
size = 1.5, alpha = 0.5, color = "midnightblue"
) +
geom_point(size = 3, color = "midnightblue") +
labs(
x = "",
y = "",
subtitle = "Trends in annual returns for Amazon and MicroStrategy\n(from 2015 to 2024)"
) +
theme_minimal(base_family = "Roboto Slab",
base_size = 15) +
theme(
text = element_text(face = "bold")
)
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.
