dividends Import dividends of General Motors and Ford since 2010.economic data Import New Hampshire monthly civilian unemployment rate (seasonally adjusted) since 2010.stock prices Import stock prices of NASDAQ, Dow Jones Industrial Average, and S&P500 since 2019.stock index Get all stocks in the S&P500 Index using tq_index(). Save the imported data under “Index” for the following questions.mutate Convert weight in decimals to percentages.summarize You would think all values in weight should sum to 100%. Calculate the sum of weight to confirm.group_by and summarize Calculate the total weight by sector. Which sector has the largest weight on the Index in sum?# Load packages
library(tidyquant)
library(tidyverse)
dividends Import dividends of General Motors and Ford since 2010.mult_stocks <- tq_get(c("GM", "F"),
get = "dividends",
from = "2010-01-01")
mult_stocks
## # A tibble: 58 x 3
## symbol date value
## <chr> <date> <dbl>
## 1 GM 2014-03-14 0.3
## 2 GM 2014-06-06 0.3
## 3 GM 2014-09-08 0.3
## 4 GM 2014-12-08 0.3
## 5 GM 2015-03-09 0.3
## 6 GM 2015-06-08 0.36
## 7 GM 2015-09-10 0.36
## 8 GM 2015-12-16 0.36
## 9 GM 2016-03-09 0.38
## 10 GM 2016-06-08 0.38
## # … with 48 more rows
economic data Import New Hampshire monthly civilian unemployment rate (seasonally adjusted) since 2010.Hint: Find the symbol in FRED.
econ <- tq_get("NHUR", get = "economic.data", from = "2010-01-01")
econ
## # A tibble: 133 x 3
## symbol date price
## <chr> <date> <dbl>
## 1 NHUR 2010-01-01 6.7
## 2 NHUR 2010-02-01 6.6
## 3 NHUR 2010-03-01 6.5
## 4 NHUR 2010-04-01 6.3
## 5 NHUR 2010-05-01 6.1
## 6 NHUR 2010-06-01 6
## 7 NHUR 2010-07-01 5.9
## 8 NHUR 2010-08-01 5.8
## 9 NHUR 2010-09-01 5.8
## 10 NHUR 2010-10-01 5.8
## # … with 123 more rows
stock prices Import stock prices of NASDAQ, Dow Jones Industrial Average, and S&P500 since 2019.mult_stocks <- tq_get(c("NASDAQ", "SP500", "DJIA"),
get = "stock.prices",
from = "2019-01-01")
mult_stocks
## # A tibble: 555 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 DJIA 2019-01-02 23059. 23455. 22819. 23346. 3733160000 23346.
## 2 DJIA 2019-01-03 23176. 23199. 22600. 22686. 3822860000 22686.
## 3 DJIA 2019-01-04 22895. 23551. 22895. 23433. 4213410000 23433.
## 4 DJIA 2019-01-07 23474. 23726. 23253. 23531. 4104710000 23531.
## 5 DJIA 2019-01-08 23680. 23986. 23511. 23787. 4083030000 23787.
## 6 DJIA 2019-01-09 23844. 24085. 23703. 23879. 4052480000 23879.
## 7 DJIA 2019-01-10 23811. 24068. 23613. 24002. 3704500000 24002.
## 8 DJIA 2019-01-11 23940. 24070. 23728. 23996. 3434490000 23996.
## 9 DJIA 2019-01-14 23881. 24039. 23691. 23910. 3664450000 23910.
## 10 DJIA 2019-01-15 23914. 24187. 23733. 24066. 3572330000 24066.
## # … with 545 more rows
stock index Get all stocks in the S&P500 Index using tq_index(). Save the imported data under “Index” for the following questions.Hint: Google tidyquant r package to find its manual and look for tq_index function on the manual. Alternatively, Google tq_index on the Web to find its documentation.
tq_index_options()
## [1] "DOW" "DOWGLOBAL" "SP400" "SP500" "SP600"
tq_index("SP500")
## # A tibble: 505 x 8
## symbol company identifier sedol weight sector shares_held local_currency
## <chr> <chr> <chr> <chr> <dbl> <chr> <dbl> <chr>
## 1 AAPL Apple Inc. 03783310 20462… 0.0600 Inform… 160186770 USD
## 2 MSFT Microsoft… 59491810 25881… 0.0537 Inform… 75779900 USD
## 3 AMZN Amazon.co… 02313510 20000… 0.0394 Consum… 4274800 USD
## 4 FB Facebook … 30303M10 B7TL8… 0.0201 Commun… 24095212 USD
## 5 GOOGL Alphabet … 02079K30 BYVY8… 0.0187 Commun… 3013467 USD
## 6 GOOG Alphabet … 02079K10 BYY88… 0.0181 Commun… 2909551 USD
## 7 TSLA Tesla Inc 88160R10 B616C… 0.0153 Consum… 7600764 USD
## 8 BRK.B Berkshire… 08467070 20733… 0.0146 Financ… 19506192 USD
## 9 JPM JPMorgan … 46625H10 21903… 0.0140 Financ… 30552690 USD
## 10 JNJ Johnson &… 47816010 24758… 0.0127 Health… 26386396 USD
## # … with 495 more rows
mutate Convert weight in decimals to percentages.Hint: Multiply weight by 100 and save it under the same name, weight. You may Google “dplyr::mutate” to find its documentation.
DOW <- mutate(tq_index("SP500"), weight = weight * 100)
DOW
## # A tibble: 505 x 8
## symbol company identifier sedol weight sector shares_held local_currency
## <chr> <chr> <chr> <chr> <dbl> <chr> <dbl> <chr>
## 1 AAPL Apple Inc. 03783310 20462… 6.00 Inform… 160186770 USD
## 2 MSFT Microsoft… 59491810 25881… 5.37 Inform… 75779900 USD
## 3 AMZN Amazon.co… 02313510 20000… 3.94 Consum… 4274800 USD
## 4 FB Facebook … 30303M10 B7TL8… 2.01 Commun… 24095212 USD
## 5 GOOGL Alphabet … 02079K30 BYVY8… 1.87 Commun… 3013467 USD
## 6 GOOG Alphabet … 02079K10 BYY88… 1.81 Commun… 2909551 USD
## 7 TSLA Tesla Inc 88160R10 B616C… 1.53 Consum… 7600764 USD
## 8 BRK.B Berkshire… 08467070 20733… 1.46 Financ… 19506192 USD
## 9 JPM JPMorgan … 46625H10 21903… 1.40 Financ… 30552690 USD
## 10 JNJ Johnson &… 47816010 24758… 1.27 Health… 26386396 USD
## # … with 495 more rows
summarize You would think all values in weight should sum to 100%. Calculate the sum of weight to confirm.Hint: You may Google “dplyr::summarize” to find its documentation.
DOW %>%
summarize(sum = sum(weight))
## # A tibble: 1 x 1
## sum
## <dbl>
## 1 100
group_by and summarize Calculate the total weight by sector. Which sector has the largest weight on the Index in sum?Hint: You may Google “dplyr::group_by” to find its documentation.
DOW %>%
group_by(sector) %>%
summarize(mean = mean(weight * 100))
## # A tibble: 11 x 2
## sector mean
## * <chr> <dbl>
## 1 Communication Services 42.9
## 2 Consumer Discretionary 20.2
## 3 Consumer Staples 18.8
## 4 Energy 12.7
## 5 Financials 17.6
## 6 Health Care 20.5
## 7 Industrials 11.7
## 8 Information Technology 35.9
## 9 Materials 9.56
## 10 Real Estate 8.21
## 11 Utilities 9.34
Communication Services has the largest weight on the index in sum.
Hint: Refer to the RMarkdown Reference Guide.