# Load packages
library(tidyquant) 
library(tidyverse) 

Q1 dividends Import dividends of General Motors and Ford since 2010.

dividends <- tq_get(c("GM", "F"),
                      get  = "dividends",
                      from = "2010-01-01")
dividends
## # 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("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_index <- tq_get(c("^IXIC", "^DJI", "^GSPC"),
                      get  = "stock.prices",
                      from = "2019-01-01")
mult_index
## # A tibble: 1,671 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,661 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.

index  <- tq_index("SP500")
index 
## # 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.0582 Inform…   163588830 USD           
##  2 MSFT   Microsoft… 59491810   25881… 0.0527 Inform…    77389240 USD           
##  3 AMZN   Amazon.co… 02313510   20000… 0.0390 Consum…     4365411 USD           
##  4 FB     Facebook … 30303M10   B7TL8… 0.0203 Commun…    24607110 USD           
##  5 GOOGL  Alphabet … 02079K30   BYVY8… 0.0184 Commun…     3077364 USD           
##  6 GOOG   Alphabet … 02079K10   BYY88… 0.0179 Commun…     2971282 USD           
##  7 TSLA   Tesla Inc  88160R10   B616C… 0.0150 Consum…     7762131 USD           
##  8 BRK.B  Berkshire… 08467070   20733… 0.0149 Financ…    19920620 USD           
##  9 JPM    JPMorgan … 46625H10   21903… 0.0145 Financ…    31201408 USD           
## 10 JNJ    Johnson &… 47816010   24758… 0.0128 Health…    26946668 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.

weight <- mutate(index, 
                  weight = weight * 100)
weight
## # 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…   5.82 Inform…   163588830 USD           
##  2 MSFT   Microsoft… 59491810   25881…   5.27 Inform…    77389240 USD           
##  3 AMZN   Amazon.co… 02313510   20000…   3.90 Consum…     4365411 USD           
##  4 FB     Facebook … 30303M10   B7TL8…   2.03 Commun…    24607110 USD           
##  5 GOOGL  Alphabet … 02079K30   BYVY8…   1.84 Commun…     3077364 USD           
##  6 GOOG   Alphabet … 02079K10   BYY88…   1.79 Commun…     2971282 USD           
##  7 TSLA   Tesla Inc  88160R10   B616C…   1.50 Consum…     7762131 USD           
##  8 BRK.B  Berkshire… 08467070   20733…   1.49 Financ…    19920620 USD           
##  9 JPM    JPMorgan … 46625H10   21903…   1.45 Financ…    31201408 USD           
## 10 JNJ    Johnson &… 47816010   24758…   1.28 Health…    26946668 USD           
## # … with 495 more rows

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.

weight %>%
    summarise(weight = sum(weight))
## # A tibble: 1 x 1
##   weight
##    <dbl>
## 1    100

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.

weight %>%
group_by(sector) %>%  
summarise(weight = sum(weight))
## # A tibble: 11 x 2
##    sector                 weight
##  * <chr>                   <dbl>
##  1 Communication Services  11.1 
##  2 Consumer Discretionary  12.3 
##  3 Consumer Staples         6.05
##  4 Energy                   2.84
##  5 Financials              11.7 
##  6 Health Care             13.0 
##  7 Industrials              8.83
##  8 Information Technology  26.4 
##  9 Materials                2.72
## 10 Real Estate              2.47
## 11 Utilities                2.60

The sector with the largest weight on the S&P 500 index is the Information Technology sector with 26.4.

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