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.stocks <- tq_get(c("^GSPC","^DJI","^IXIC"),
get = "stock.prices",
from = "2019-01-01")
stocks
## # A tibble: 1,335 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 ^GSPC 2019-01-02 2477. 2519. 2467. 2510. 3733160000 2510.
## 2 ^GSPC 2019-01-03 2492. 2493. 2444. 2448. 3822860000 2448.
## 3 ^GSPC 2019-01-04 2474. 2538. 2474. 2532. 4213410000 2532.
## 4 ^GSPC 2019-01-07 2536. 2566. 2525. 2550. 4104710000 2550.
## 5 ^GSPC 2019-01-08 2568. 2580. 2548. 2574. 4083030000 2574.
## 6 ^GSPC 2019-01-09 2580 2595. 2569. 2585. 4052480000 2585.
## 7 ^GSPC 2019-01-10 2574. 2598. 2562. 2597. 3704500000 2597.
## 8 ^GSPC 2019-01-11 2588. 2596. 2577. 2596. 3434490000 2596.
## 9 ^GSPC 2019-01-14 2580. 2589. 2570. 2583. 3664450000 2583.
## 10 ^GSPC 2019-01-15 2585. 2613. 2585. 2610. 3572330000 2610.
## # ... with 1,325 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.0745 Health~ 5431277 USD
## 2 HD Home Depo~ 43707610 24342~ 0.0655 Consum~ 5431277 USD
## 3 AMGN Amgen Inc. 03116210 20236~ 0.0599 Health~ 5431277 USD
## 4 CRM salesforc~ 79466L30 23105~ 0.0593 Inform~ 5431277 USD
## 5 MCD McDonald'~ 58013510 25507~ 0.0531 Consum~ 5431277 USD
## 6 MSFT Microsoft~ 59491810 25881~ 0.0488 Inform~ 5431277 USD
## 7 GS Goldman S~ 38141G10 24079~ 0.0476 Financ~ 5431277 USD
## 8 V Visa Inc.~ 92826C83 B2PZN~ 0.0475 Inform~ 5431277 USD
## 9 HON Honeywell~ 43851610 20204~ 0.0395 Indust~ 5431277 USD
## 10 MMM 3M Company 88579Y10 25957~ 0.0384 Indust~ 5431277 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.45 Health~ 5431277 USD
## 2 HD Home Depo~ 43707610 24342~ 6.55 Consum~ 5431277 USD
## 3 AMGN Amgen Inc. 03116210 20236~ 5.99 Health~ 5431277 USD
## 4 CRM salesforc~ 79466L30 23105~ 5.93 Inform~ 5431277 USD
## 5 MCD McDonald'~ 58013510 25507~ 5.31 Consum~ 5431277 USD
## 6 MSFT Microsoft~ 59491810 25881~ 4.88 Inform~ 5431277 USD
## 7 GS Goldman S~ 38141G10 24079~ 4.76 Financ~ 5431277 USD
## 8 V Visa Inc.~ 92826C83 B2PZN~ 4.75 Inform~ 5431277 USD
## 9 HON Honeywell~ 43851610 20204~ 3.95 Indust~ 5431277 USD
## 10 MMM 3M Company 88579Y10 25957~ 3.84 Indust~ 5431277 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.
Consumer Discretionary has the greatest weight at 4.96.
DOW %>%
group_by(sector) %>%
summarize(mean = mean(weight))
## # A tibble: 9 x 2
## sector mean
## <chr> <dbl>
## 1 Communication Services 2.14
## 2 Consumer Discretionary 4.96
## 3 Consumer Staples 2.16
## 4 Energy 1.71
## 5 Financials 3.03
## 6 Health Care 4.70
## 7 Industrials 3.79
## 8 Information Technology 3.32
## 9 Materials 1.13
Hint: Refer to the RMarkdown Reference Guide.