tidyquant part 1

## Download financial data with tidyquant
## Perform various data manipulations with dplyr
## plot data with ggplot
## Athanassios Stavrakoudis 
## 29 Oct 2021

# load required libraries
library(tidyquant)
library(tidyverse)
library(writexl)

# stock index
tq_exchange_options()
# index listings
tq_exchange("NYSE")

# get options
tq_index_options()
tq_index("SP500")

# print more lines of a tibble
tq_index("SP500") %>% 
  print(n = 50)

# view all 
tq_index("SP500") %>% 
  View()
tq_index("SP500") %>% 
  print(n = Inf)


# download and save in a variable
sp500 <- tq_index("SP500")

# make some data exploration
sp500 %>% 
  str()

# details of a listing, here facebook
sp500 %>% 
  filter(symbol == 'FB') 

# count listings of a category
sp500 %>% 
  filter(sector == 'Information Technology') %>% 
  nrow()

# select some columns of the dataset
sp500 %>% 
  select(company, weight) 

# filter / select combination
sp500 %>% 
  filter(symbol == 'FB') %>% 
  select(company, weight) 

# sum a numeric column
sp500 %>% 
  select(weight) %>% 
  sum()

# group by a column and summarize, here count listings per sector
sp500 %>% 
  group_by(sector) %>% 
  summarise(cnt = n())

# find the median weight per sector
sp500 %>% 
  group_by(sector) %>% 
  summarise(m = median(weight))

# count can be done easier with count function
sp500 %>% 
  count(sector, name = "cnt")

# filter after count
sp500 %>% 
  count(sector, name = "cnt") %>%  
  filter(cnt > 50)
  
# convert weight to percentages
sp500 %>% 
  group_by(sector) %>% 
  summarise(weight = 100*sum(weight)) 


# plot weight by sector
w <- sp500 %>% 
  group_by(sector) %>% 
  summarise(weight = sum(weight))

ggplot(w, aes(x = sector, y = weight)) + 
  geom_col(fill = "dodgerblue3") +
  geom_text(aes(label = round(weight, 2)), vjust = 1.6, color = "white", size = 4) +
  theme_tq(base_size = 15, base_family = "NimbusSan") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) 

# reminder, pdf fonts
pdfFonts() %>% names()

## plot in a more elegant way with factor reorder
# mutate to create a new variable
w %>% 
  mutate(sector_ord = fct_reorder(as.factor(sector), weight)) %>% 
  ggplot(aes(x = sector_ord, y = weight)) + 
  geom_col(fill = "dodgerblue3") +
  coord_flip() +
  labs(x = "sector") +
  theme_tq(base_size = 15, base_family = "NimbusSan") 

# reorder in aes
ggplot(w, aes(x = fct_reorder(as.factor(sector), weight), y = weight)) + 
  geom_col(fill = "dodgerblue3") +
  coord_flip() +
  labs(x = "sector") +
  theme_tq(base_size = 15, base_family = "NimbusSan") 

# descending order
ggplot(w, aes(x = fct_reorder(as.factor(sector), weight, .desc = TRUE), y = weight)) + 
  geom_col(fill = "dodgerblue3") +
  coord_flip() +
  labs(x = "sector") +
  theme_tq(base_size = 15, base_family = "NimbusSan") 

# export data to excel
write_xlsx(w, "../sector_weights.xlsx")

Συνδεθείτε για περισσότερες δυνατότητες αλληλεπίδρασης,
σχολιασμοί, εξωτερικοί σύνδεσμοι, βοήθεια, ψηφοφορίες, αρχεία, κτλ.

Creative Commons License
Εκπαιδευτικό υλικό από τον Αθανάσιο Σταυρακούδη σας παρέχετε κάτω από την άδεια Creative Commons Attribution-NonCommercial-ShareAlike 4.0 License.
Σας παρακαλώ να ενημερωθείτε για κάποιους επιπλέον περιορισμούς
http://stavrakoudis.econ.uoi.gr/stavrakoudis/?iid=401.