dividends Import dividends of General Motors and Ford since 2010.economic data Import New Hampshire monthly civilian unemployment rate (seasonally adjusted) 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 S&P500 Index using tq_index(). Save the imported data under “Index” for the following questions.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 total weight by sector. Which sector has the largest weight on the Index in sum?# Load packages
library(tidyquant)
## Warning: package 'tidyquant' was built under R version 4.0.4
## Warning: package 'lubridate' was built under R version 4.0.4
## Warning: package 'PerformanceAnalytics' was built under R version 4.0.3
## Warning: package 'xts' was built under R version 4.0.3
## Warning: package 'zoo' was built under R version 4.0.3
## Warning: package 'quantmod' was built under R version 4.0.3
## Warning: package 'TTR' was built under R version 4.0.3
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.3
## Warning: package 'ggplot2' was built under R version 4.0.3
## Warning: package 'tibble' was built under R version 4.0.4
## Warning: package 'tidyr' was built under R version 4.0.4
## Warning: package 'readr' was built under R version 4.0.3
## Warning: package 'purrr' was built under R version 4.0.3
## Warning: package 'dplyr' was built under R version 4.0.4
## Warning: package 'stringr' was built under R version 4.0.3
## Warning: package 'forcats' was built under R version 4.0.3
dividends Import dividends of General Motors and Ford since 2010.gm_divs <- tq_get("GM", get = "dividends", from = "2010-01-01")
gm_divs
## # A tibble: 25 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 15 more rows
ford_divs <- tq_get("F", get = "dividends", from = "2010-01-01")
ford_divs
## # A tibble: 33 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 23 more rows
economic data Import New Hampshire monthly civilian unemployment rate (seasonally adjusted) since 2010.Hint: Find the symbol in FRED.
nh_un <- tq_get("NHUR", get = "economic.data", from = "2010-01-01")
nh_un
## # 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.tq_get("^GSPC", from = "2019-01-01")
## # A tibble: 557 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 547 more rows
tq_get("DOW", from = "2019-01-01")
## # A tibble: 504 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 44.3
## 2 DOW 2019-03-21 50.0 50 48.2 49.0 1764700 43.6
## 3 DOW 2019-03-22 48.8 50.0 48.2 48.6 844700 43.2
## 4 DOW 2019-03-25 48.6 49.4 48 49.2 440900 43.7
## 5 DOW 2019-03-26 49 49.8 48.2 48.8 504700 43.5
## 6 DOW 2019-03-27 49 50.8 49 50.1 1788600 44.6
## 7 DOW 2019-03-28 50.9 51 49.9 50.8 585400 45.2
## 8 DOW 2019-03-29 49.5 52.5 49.2 51.6 1769000 45.9
## 9 DOW 2019-04-01 52.3 54.4 51.2 53.5 19663400 47.6
## 10 DOW 2019-04-02 55.1 57.4 54.3 56.2 17414200 50.1
## # ... with 494 more rows
tq_get("^IXIC", from = "2019-01-01")
## # A tibble: 557 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 547 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.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")
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 <- Index %>%
select(weight) %>%
mutate(
weight = weight * 100,
)
weight
## # A tibble: 505 x 1
## weight
## <dbl>
## 1 5.82
## 2 5.27
## 3 3.90
## 4 2.03
## 5 1.84
## 6 1.79
## 7 1.50
## 8 1.49
## 9 1.45
## 10 1.28
## # ... with 495 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.
weight %>% summarise(n = n())
## # A tibble: 1 x 1
## n
## <int>
## 1 505
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.
sector <- Index %>% group_by(sector)
sweight <- sector %>% summarise(weight = n())
sweight
## # A tibble: 11 x 2
## sector weight
## <chr> <int>
## 1 Communication Services 26
## 2 Consumer Discretionary 61
## 3 Consumer Staples 32
## 4 Energy 23
## 5 Financials 65
## 6 Health Care 63
## 7 Industrials 74
## 8 Information Technology 75
## 9 Materials 28
## 10 Real Estate 30
## 11 Utilities 28
The Information Technology sector has the largest weight on the index by weighing, 75.
Hint: Refer to the RMarkdown Reference Guide.