# Load packages
library(tidyverse)
library(tidyquant)

Step 1 Get stock prices and convert to returns

Ra <- c("TSLA", "META", "XOM", "AAPL", "PG", "AMZN") %>%
    tq_get(get  = "stock.prices",
           from = "2022-01-01") %>%
    group_by(symbol) %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Ra")
Ra
## # A tibble: 102 × 3
## # Groups:   symbol [6]
##    symbol date            Ra
##    <chr>  <date>       <dbl>
##  1 TSLA   2022-01-31 -0.219 
##  2 TSLA   2022-02-28 -0.0708
##  3 TSLA   2022-03-31  0.238 
##  4 TSLA   2022-04-29 -0.192 
##  5 TSLA   2022-05-31 -0.129 
##  6 TSLA   2022-06-30 -0.112 
##  7 TSLA   2022-07-29  0.324 
##  8 TSLA   2022-08-31 -0.0725
##  9 TSLA   2022-09-30 -0.0376
## 10 TSLA   2022-10-31 -0.142 
## # ℹ 92 more rows

Step 2 Get baseline and convert to returns

Rb <- "^IXIC" %>%
    tq_get(get  = "stock.prices",
           from = "2022-01-01") %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Rb")
Rb
## # A tibble: 17 × 2
##    date              Rb
##    <date>         <dbl>
##  1 2022-01-31 -0.101   
##  2 2022-02-28 -0.0343  
##  3 2022-03-31  0.0341  
##  4 2022-04-29 -0.133   
##  5 2022-05-31 -0.0205  
##  6 2022-06-30 -0.0871  
##  7 2022-07-29  0.123   
##  8 2022-08-31 -0.0464  
##  9 2022-09-30 -0.105   
## 10 2022-10-31  0.0390  
## 11 2022-11-30  0.0437  
## 12 2022-12-30 -0.0873  
## 13 2023-01-31  0.107   
## 14 2023-02-28 -0.0111  
## 15 2023-03-31  0.0669  
## 16 2023-04-28  0.000382
## 17 2023-05-18  0.0378

Step 3 Join the two tables

RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 102 × 4
## # Groups:   symbol [6]
##    symbol date            Ra      Rb
##    <chr>  <date>       <dbl>   <dbl>
##  1 TSLA   2022-01-31 -0.219  -0.101 
##  2 TSLA   2022-02-28 -0.0708 -0.0343
##  3 TSLA   2022-03-31  0.238   0.0341
##  4 TSLA   2022-04-29 -0.192  -0.133 
##  5 TSLA   2022-05-31 -0.129  -0.0205
##  6 TSLA   2022-06-30 -0.112  -0.0871
##  7 TSLA   2022-07-29  0.324   0.123 
##  8 TSLA   2022-08-31 -0.0725 -0.0464
##  9 TSLA   2022-09-30 -0.0376 -0.105 
## 10 TSLA   2022-10-31 -0.142   0.0390
## # ℹ 92 more rows

Step 4 Calculate CAPM

RaRb_capm <- RaRb %>%
    tq_performance(Ra = Ra, 
                   Rb = Rb, 
                   performance_fun = table.CAPM)
RaRb_capm
## # A tibble: 6 × 13
## # Groups:   symbol [6]
##   symbol ActivePremium   Alpha AnnualizedAlpha   Beta `Beta-` `Beta+`
##   <chr>          <dbl>   <dbl>           <dbl>  <dbl>   <dbl>   <dbl>
## 1 TSLA         -0.293  -0.0073         -0.0843 1.91     2.03    4.44 
## 2 META         -0.0552  0.0082          0.103  1.07     1.02    0.525
## 3 XOM           0.634   0.0416          0.631  0.314    0.178   0.236
## 4 AAPL          0.124   0.0131          0.169  1.08     0.788   1.24 
## 5 PG            0.125   0.0008          0.0092 0.0944  -0.489  -0.760
## 6 AMZN         -0.0831  0.0025          0.0307 1.53     1.38    2.43 
## # ℹ 6 more variables: Correlation <dbl>, `Correlationp-value` <dbl>,
## #   InformationRatio <dbl>, `R-squared` <dbl>, TrackingError <dbl>,
## #   TreynorRatio <dbl>

Which stock has a positively skewed distribution of returns?

The skewed distribution of returns of the stocks

RaRb_skewness <- RaRb %>%
    mutate (skewness_Ra = skewness(Ra), 
            skewness_Rb = skewness(Rb))
RaRb_skewness
## # A tibble: 102 × 6
## # Groups:   symbol [6]
##    symbol date            Ra      Rb skewness_Ra skewness_Rb
##    <chr>  <date>       <dbl>   <dbl>       <dbl>       <dbl>
##  1 TSLA   2022-01-31 -0.219  -0.101        0.644      0.0744
##  2 TSLA   2022-02-28 -0.0708 -0.0343       0.644      0.0744
##  3 TSLA   2022-03-31  0.238   0.0341       0.644      0.0744
##  4 TSLA   2022-04-29 -0.192  -0.133        0.644      0.0744
##  5 TSLA   2022-05-31 -0.129  -0.0205       0.644      0.0744
##  6 TSLA   2022-06-30 -0.112  -0.0871       0.644      0.0744
##  7 TSLA   2022-07-29  0.324   0.123        0.644      0.0744
##  8 TSLA   2022-08-31 -0.0725 -0.0464       0.644      0.0744
##  9 TSLA   2022-09-30 -0.0376 -0.105        0.644      0.0744
## 10 TSLA   2022-10-31 -0.142   0.0390       0.644      0.0744
## # ℹ 92 more rows

Which stock(s) beat the market in 2022?

So, based of the results that are showed above, in 2022, the following stocks beat the market: META, XOM, AAPL, PG, and AMZN.

A bar plot to visualize the results

# Create a bar plot of skewness
library(ggplot2)
ggplot(RaRb_skewness, aes(x = symbol, y = skewness_Ra, fill = symbol)) +
  geom_bar(stat = "identity", position = "dodge", show.legend = FALSE) +
  labs(x = "Stock", y = "Skewness", title = "Skewness of Returns") +
  theme_minimal()

Stocks that had a positively skewed distribution of returns:

# Filter stocks with positive skewness and get distinct symbols
positive_skew_stocks <- RaRb_skewness %>%
  filter(skewness_Ra > 0) %>%
  distinct(symbol)

# Print the stocks with positive skewness
positive_skew_stocks
## # A tibble: 5 × 1
## # Groups:   symbol [5]
##   symbol
##   <chr> 
## 1 TSLA  
## 2 XOM   
## 3 AAPL  
## 4 PG    
## 5 AMZN