# Load packages
library(tidyquant) 
library(tidyverse) 

Q1 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

Q2 economic data Import New Hampshire monthly civilian unemployment rate (seasonally adjusted) since 2010.

Hint: Find the symbol in FRED.

econ <- tq_get(c("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

Q3 stock prices Import stock prices of NASDAQ, Dow Jones Industrial Average, and S&P500 since 2019.

mult_stocks <- tq_get(c("^IXIC", "^DJI", "^GSPC" ),
                      get  = "stock.prices",
                      from = "2019-01-01")
mult_stocks
## # A tibble: 1,668 x 8
##    symbol date        open  high   low close     volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>      <dbl>    <dbl>
##  1 ^IXIC  2019-01-02 6507. 6694. 6507. 6666. 2261800000    6666.
##  2 ^IXIC  2019-01-03 6585. 6600. 6457. 6464. 2607290000    6464.
##  3 ^IXIC  2019-01-04 6567. 6761. 6554. 6739. 2579550000    6739.
##  4 ^IXIC  2019-01-07 6758. 6856. 6741. 6823. 2507550000    6823.
##  5 ^IXIC  2019-01-08 6893. 6910. 6796. 6897  2380290000    6897 
##  6 ^IXIC  2019-01-09 6923. 6985. 6900. 6957. 2422590000    6957.
##  7 ^IXIC  2019-01-10 6909. 6991. 6877. 6986. 2179080000    6986.
##  8 ^IXIC  2019-01-11 6947. 6976. 6934. 6971. 2066500000    6971.
##  9 ^IXIC  2019-01-14 6908. 6936. 6887. 6906. 1942210000    6906.
## 10 ^IXIC  2019-01-15 6931. 7026. 6928. 7024. 2038090000    7024.
## # … with 1,658 more rows

Q4 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.0594 Inform…   161345920 USD           
##  2 MSFT   Microsoft… 59491810   25881… 0.0534 Inform…    76328240 USD           
##  3 AMZN   Amazon.co… 02313510   20000… 0.0398 Consum…     4305673 USD           
##  4 FB     Facebook … 30303M10   B7TL8… 0.0203 Commun…    24269626 USD           
##  5 GOOGL  Alphabet … 02079K30   BYVY8… 0.0186 Commun…     3035238 USD           
##  6 GOOG   Alphabet … 02079K10   BYY88… 0.0181 Commun…     2930584 USD           
##  7 TSLA   Tesla Inc  88160R10   B616C… 0.0159 Consum…     7655745 USD           
##  8 BRK.B  Berkshire… 08467070   20733… 0.0146 Financ…    19647396 USD           
##  9 JPM    JPMorgan … 46625H10   21903… 0.0141 Financ…    30773720 USD           
## 10 JNJ    Johnson &… 47816010   24758… 0.0126 Health…    26577292 USD           
## # … with 495 more rows

Q5 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.

tq_index_options() tq_index(“SP500”) “SP500” select(weight) mutate( weight =weight * 100 # convert to percentages

Q6 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.

tq_index_options() tq_index(“SP500”) “SP500” %>% summarise(weight = weight(weight))

Q7 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.

tq_index_options() tq_index(“SP500”) by_weight <- “SP500” %>% group_by(weight)

Q8 Hide the messages and warnings, but display the code and its results on the webpage.

Hint: Refer to the RMarkdown Reference Guide.

Q9 Display the title and your name correctly at the top of the webpage.

Q10 Use the correct slu