dividends Import dividends of Costco and Target since 2000.economic data Import the U.S. Industrial Production Index 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 Dow Jones Industrial Avarage (DOW) Index using tq_index().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 mean weight by sector. Which sector has the greatest weight on DOW on average?# Load packages
library(tidyquant)
library(tidyverse)
dividends Import dividends of Costco and Target since 2000.mult_stocks <- tq_get (c("COST" , "TGT"),
get = "dividends",
from = "2000-01-01")
mult_stocks
## # A tibble: 152 x 3
## symbol date value
## <chr> <date> <dbl>
## 1 COST 2004-05-06 0.1
## 2 COST 2004-07-21 0.1
## 3 COST 2004-11-03 0.1
## 4 COST 2005-02-04 0.1
## 5 COST 2005-05-04 0.115
## 6 COST 2005-08-03 0.115
## 7 COST 2005-11-16 0.115
## 8 COST 2006-02-07 0.115
## 9 COST 2006-05-08 0.13
## 10 COST 2006-07-28 0.13
## # ... with 142 more rows
economic data Import the U.S. Industrial Production Index since 2010.Hint: Find the symbol in FRED.
econ <- tq_get("INDPRO" , get = "economic.data", from
= "2010-01-01")
econ
## # A tibble: 128 x 3
## symbol date price
## <chr> <date> <dbl>
## 1 INDPRO 2010-01-01 91.7
## 2 INDPRO 2010-02-01 92.0
## 3 INDPRO 2010-03-01 92.6
## 4 INDPRO 2010-04-01 92.9
## 5 INDPRO 2010-05-01 94.3
## 6 INDPRO 2010-06-01 94.4
## 7 INDPRO 2010-07-01 94.9
## 8 INDPRO 2010-08-01 95.1
## 9 INDPRO 2010-09-01 95.4
## 10 INDPRO 2010-10-01 95.1
## # ... with 118 more rows
stock prices Import stock prices of NASDAQ, Dow Jones Industrial Average, and S&P500 since 2019.mult_stocks <- tq_get(c("DOW" , "SP500", "NASDAQ"),
get = "stock.prices",
from = "2019-01-01")
## Warning: Problem with `mutate()` input `data..`.
## i x = 'SP500', get = 'stock.prices': Error in getSymbols.yahoo(Symbols = "SP500", env = <environment>, verbose = FALSE, : Unable to import "SP500".
## SP500 download failed after two attempts. Error message:
## HTTP error 404.
## Removing SP500.
## i Input `data..` is `purrr::map(...)`.
## Warning: x = 'SP500', get = 'stock.prices': Error in getSymbols.yahoo(Symbols = "SP500", env = <environment>, verbose = FALSE, : Unable to import "SP500".
## SP500 download failed after two attempts. Error message:
## HTTP error 404.
## Removing SP500.
## Warning: Problem with `mutate()` input `data..`.
## i x = 'NASDAQ', get = 'stock.prices': Error in getSymbols.yahoo(Symbols = "NASDAQ", env = <environment>, verbose = FALSE, : Unable to import "NASDAQ".
## NASDAQ download failed after two attempts. Error message:
## HTTP error 404.
## Removing NASDAQ.
## i Input `data..` is `purrr::map(...)`.
## Warning: x = 'NASDAQ', get = 'stock.prices': Error in getSymbols.yahoo(Symbols = "NASDAQ", env = <environment>, verbose = FALSE, : Unable to import "NASDAQ".
## NASDAQ download failed after two attempts. Error message:
## HTTP error 404.
## Removing NASDAQ.
mult_stocks
## # A tibble: 393 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 DOW 2019-03-20 52.8 53.5 49.5 49.8 2350800 45.4
## 2 DOW 2019-03-21 50.0 50 48.2 49.0 1764700 44.6
## 3 DOW 2019-03-22 48.8 50.0 48.2 48.6 844700 44.3
## 4 DOW 2019-03-25 48.6 49.4 48 49.2 440900 44.8
## 5 DOW 2019-03-26 49 49.8 48.2 48.8 504700 44.5
## 6 DOW 2019-03-27 49 50.8 49 50.1 1788600 45.6
## 7 DOW 2019-03-28 50.9 51 49.9 50.8 585400 46.3
## 8 DOW 2019-03-29 49.5 52.5 49.2 51.6 1769000 47.0
## 9 DOW 2019-04-01 52.3 54.4 51.2 53.5 19663400 48.7
## 10 DOW 2019-04-02 55.1 57.4 54.3 56.2 17414200 51.2
## # ... with 383 more rows
stock index Get all stocks in the Dow Jones Industrial Avarage (DOW) Index using tq_index().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.
DOW <- tq_index("DOW")
DOW
## # A tibble: 30 x 8
## symbol company identifier sedol weight sector shares_held local_currency
## <chr> <chr> <chr> <chr> <dbl> <chr> <dbl> <chr>
## 1 UNH UnitedHea~ 91324P10 29177~ 0.0751 Health~ 5474034 USD
## 2 HD Home Depo~ 43707610 24342~ 0.0657 Consum~ 5474034 USD
## 3 CRM salesforc~ 79466L30 23105~ 0.0604 Inform~ 5474034 USD
## 4 AMGN Amgen Inc. 03116210 20236~ 0.0599 Health~ 5474034 USD
## 5 MCD McDonald'~ 58013510 25507~ 0.0526 Consum~ 5474034 USD
## 6 MSFT Microsoft~ 59491810 25881~ 0.0488 Inform~ 5474034 USD
## 7 GS Goldman S~ 38141G10 24079~ 0.0473 Financ~ 5474034 USD
## 8 V Visa Inc.~ 92826C83 B2PZN~ 0.0471 Inform~ 5474034 USD
## 9 HON Honeywell~ 43851610 20204~ 0.0399 Indust~ 5474034 USD
## 10 MMM 3M Company 88579Y10 25957~ 0.0387 Indust~ 5474034 USD
## # ... with 20 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 <-
DOW %>%
mutate(weight = weight * 100)
DOW
## # A tibble: 30 x 8
## symbol company identifier sedol weight sector shares_held local_currency
## <chr> <chr> <chr> <chr> <dbl> <chr> <dbl> <chr>
## 1 UNH UnitedHea~ 91324P10 29177~ 7.51 Health~ 5474034 USD
## 2 HD Home Depo~ 43707610 24342~ 6.57 Consum~ 5474034 USD
## 3 CRM salesforc~ 79466L30 23105~ 6.04 Inform~ 5474034 USD
## 4 AMGN Amgen Inc. 03116210 20236~ 5.99 Health~ 5474034 USD
## 5 MCD McDonald'~ 58013510 25507~ 5.26 Consum~ 5474034 USD
## 6 MSFT Microsoft~ 59491810 25881~ 4.88 Inform~ 5474034 USD
## 7 GS Goldman S~ 38141G10 24079~ 4.73 Financ~ 5474034 USD
## 8 V Visa Inc.~ 92826C83 B2PZN~ 4.71 Inform~ 5474034 USD
## 9 HON Honeywell~ 43851610 20204~ 3.99 Indust~ 5474034 USD
## 10 MMM 3M Company 88579Y10 25957~ 3.87 Indust~ 5474034 USD
## # ... with 20 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 mean weight by sector. Which sector has the greatest weight on DOW on average?Hint: You may Google “dplyr::group_by” to find its documentation.
DOW %>%
group_by(sector) %>%
summarize(mean = mean(weight))
## # A tibble: 9 x 2
## sector mean
## <chr> <dbl>
## 1 Communication Services 2.12
## 2 Consumer Discretionary 4.95
## 3 Consumer Staples 2.14
## 4 Energy 1.72
## 5 Financials 3.02
## 6 Health Care 4.70
## 7 Industrials 3.82
## 8 Information Technology 3.33
## 9 Materials 1.12
Hint: Refer to the RMarkdown Reference Guide.