title: “Quiz 3” author: “Garrett Macomber” output: html_document: toc: yes editor_options: chunk_output_type: console —
# Load packages
library(tidyquant)
library(tidyverse)
dividends Import dividends of General Motors and Ford since 2010.mult_stocks <- tq_get(c("F", "GM"),
get = "dividends",
from = "2010-01-01")
mult_stocks
## # A tibble: 58 x 3
## symbol date value
## <chr> <date> <dbl>
## 1 F 2012-01-27 0.05
## 2 F 2012-04-30 0.05
## 3 F 2012-08-01 0.05
## 4 F 2012-10-31 0.05
## 5 F 2013-01-28 0.1
## 6 F 2013-05-01 0.1
## 7 F 2013-07-31 0.1
## 8 F 2013-10-30 0.1
## 9 F 2014-01-29 0.125
## 10 F 2014-04-30 0.125
## # ... with 48 more rows
economic data Import New Hampshire monthly civilian unemployment rate (seasonally adjusted) since 2010.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("NASDAQCOM", "DJIA","SP500"),
get = "economic.data",
from = "2019-01-01")
mult_stocks
## # A tibble: 1,734 x 3
## symbol date price
## <chr> <date> <dbl>
## 1 NASDAQCOM 2019-01-01 NA
## 2 NASDAQCOM 2019-01-02 6666.
## 3 NASDAQCOM 2019-01-03 6464.
## 4 NASDAQCOM 2019-01-04 6739.
## 5 NASDAQCOM 2019-01-07 6823.
## 6 NASDAQCOM 2019-01-08 6897
## 7 NASDAQCOM 2019-01-09 6957.
## 8 NASDAQCOM 2019-01-10 6986.
## 9 NASDAQCOM 2019-01-11 6971.
## 10 NASDAQCOM 2019-01-14 6906.
## # ... with 1,724 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.tq_index_options()
## [1] "DOW" "DOWGLOBAL" "SP400" "SP500" "SP600"
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
mutate Convert weight in decimals to percentages.mutate(Index, weight = weight * 100)
## # 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
summarize You would think all values in weight should sum to 100%. Calculate the sum of weight to confirm.Index %>%
summarise(sum_of_weight = sum(weight))
## # A tibble: 1 x 1
## sum_of_weight
## <dbl>
## 1 1
group_by and summarize Calculate the total weight by sector. Which sector has the largest weight on the Index in sum?Index %>%
group_by(sector) %>%
summarise(total_weight_by_sector = sum(weight))
## # A tibble: 11 x 2
## sector total_weight_by_sector
## * <chr> <dbl>
## 1 Communication Services 0.111
## 2 Consumer Discretionary 0.123
## 3 Consumer Staples 0.0605
## 4 Energy 0.0284
## 5 Financials 0.117
## 6 Health Care 0.130
## 7 Industrials 0.0883
## 8 Information Technology 0.264
## 9 Materials 0.0272
## 10 Real Estate 0.0247
## 11 Utilities 0.0260
Hint: Refer to the RMarkdown Reference Guide.