# Load packages
# Core
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.0 ✔ stringr 1.4.1
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(tidyquant)
## Loading required package: lubridate
##
## Attaching package: 'lubridate'
##
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
##
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
##
## Attaching package: 'xts'
##
## The following objects are masked from 'package:dplyr':
##
## first, last
##
##
## Attaching package: 'PerformanceAnalytics'
##
## The following object is masked from 'package:graphics':
##
## legend
##
## Loading required package: quantmod
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
##Goal Visualize and compare skewness of your portfolio and its assets.
Choose your stocks.
from 2012-12-31 to 2017-12-31
symbol <- c("BIG", "TSLA", "AMZN", "WM", "PLUG")
prices <- tq_get(x = symbol,
get = "stock.prices",
from = "2012-12-31",
to = "2017-12-31")
asset_returns_tbl <- prices %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
type = "log") %>%
slice(-1) %>%
ungroup()
set_names(c("asset", "date", "returns"))
## asset date returns
## "asset" "date" "returns"
symbols <- asset_returns_tbl %>% distinct(symbol) %>% pull()
symbols
## [1] "AMZN" "BIG" "PLUG" "TSLA" "WM"
weight <- c(0.2,0.2,0.2,0.2,0.2)
weight
## [1] 0.2 0.2 0.2 0.2 0.2
w_tbl <- tibble(symbols, weight)
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = symbol,
returns_col = monthly.returns,
weights = w_tbl,
rebalance_on = "months",
col_rename = "returns")
## Warning: `spread_()` was deprecated in tidyr 1.2.0.
## Please use `spread()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
portfolio_skew_tidyquant_builtin_percent <- portfolio_returns_tbl %>%
tq_performance (Ra = returns,
performance_fun = table.Stats) %>%
select (Skewness)
portfolio_skew_tidyquant_builtin_percent
## # A tibble: 1 × 1
## Skewness
## <dbl>
## 1 0.332
##6 Plot Skewness Comparison
asset_skewness_tbl <- asset_returns_tbl %>%
group_by (symbol) %>%
summarise(skew = skewness(monthly.returns)) %>%
ungroup() %>%
add_row(tibble(symbol = "portfolio",
skew = skewness(portfolio_returns_tbl$returns)))
# Plot Skewness
asset_skewness_tbl %>%
ggplot(aes(x = symbol,
y = skew,
color = symbol)) +
geom_point() +
ggrepel::geom_text_repel(aes(label = symbol),
data = asset_skewness_tbl %>%
filter(symbol == "portfolio")) +
labs(y = "skewness")
#Is any asset in your portfolio more likely to return extreme positive returns than your portfolio collectively? Discuss in terms of skewness. You may also refer to the distribution of returns you plotted in Code along 4. TSLA has a higher skew than any other meaning it will vary more than any other based on returns giving extreme positives.