stock.prices
Import S&P500 since 2009.economic.data
Import real U.S. GDP growth since 2010.# Load packages
library(tidyquant)
library(tidyverse)
stock.prices
# Import NASDAQ since 2010.
stocks <- tq_get("^IXIC", get = "stock.prices", from = "1999-01-01")
stocks
## # A tibble: 5,221 x 7
## date open high low close volume adjusted
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1999-01-04 2208. 2234. 2193. 2208. 936660000 2208.
## 2 1999-01-05 2208. 2252. 2206. 2251. 948350000 2251.
## 3 1999-01-06 2286. 2321. 2286. 2321. 1252650000 2321.
## 4 1999-01-07 2293. 2334. 2284. 2326. 1200900000 2326.
## 5 1999-01-08 2364. 2370. 2315. 2344. 1286690000 2344.
## 6 1999-01-11 2374. 2385. 2348. 2385. 1140930000 2385.
## 7 1999-01-12 2396. 2396. 2320. 2321. 1107300000 2321.
## 8 1999-01-13 2222. 2353. 2206. 2317. 1195470000 2317.
## 9 1999-01-14 2337. 2338. 2276. 2277. 1012270000 2277.
## 10 1999-01-15 2292. 2349. 2277. 2348. 1001160000 2348.
## # … with 5,211 more rows
stock_returns <-
stocks %>%
# Calculate yearly returns
tq_transmute(select = adjusted, mutate_fun = periodReturn, period = "yearly") %>%
# Create a new variable, year
mutate(year = year(date)+1) %>%
# Drop date
select(-date)
stock_returns
## # A tibble: 21 x 2
## yearly.returns year
## <dbl> <dbl>
## 1 0.843 2000
## 2 -0.393 2001
## 3 -0.211 2002
## 4 -0.315 2003
## 5 0.500 2004
## 6 0.0859 2005
## 7 0.0137 2006
## 8 0.0952 2007
## 9 0.0981 2008
## 10 -0.405 2009
## # … with 11 more rows
economic.data
# Import real U.S. GDP growth since 2010.
econ <- tq_get("A191RL1A225NBEA", get = "economic.data", from = "2000-01-01")
econ
## # A tibble: 19 x 2
## date price
## <date> <dbl>
## 1 2000-01-01 4.1
## 2 2001-01-01 1
## 3 2002-01-01 1.7
## 4 2003-01-01 2.9
## 5 2004-01-01 3.8
## 6 2005-01-01 3.5
## 7 2006-01-01 2.9
## 8 2007-01-01 1.9
## 9 2008-01-01 -0.1
## 10 2009-01-01 -2.5
## 11 2010-01-01 2.6
## 12 2011-01-01 1.6
## 13 2012-01-01 2.2
## 14 2013-01-01 1.8
## 15 2014-01-01 2.5
## 16 2015-01-01 2.9
## 17 2016-01-01 1.6
## 18 2017-01-01 2.4
## 19 2018-01-01 2.9
econ_decimal <-
econ %>%
mutate(year = year(date), # Create a new variable, year
GDPgrowth = price/100) %>% # Convert price to decimal number
# Drop date
select(-date)
econ_decimal
## # A tibble: 19 x 3
## price year GDPgrowth
## <dbl> <dbl> <dbl>
## 1 4.1 2000 0.0410
## 2 1 2001 0.01
## 3 1.7 2002 0.017
## 4 2.9 2003 0.0290
## 5 3.8 2004 0.038
## 6 3.5 2005 0.035
## 7 2.9 2006 0.0290
## 8 1.9 2007 0.019
## 9 -0.1 2008 -0.001
## 10 -2.5 2009 -0.025
## 11 2.6 2010 0.026
## 12 1.6 2011 0.016
## 13 2.2 2012 0.022
## 14 1.8 2013 0.018
## 15 2.5 2014 0.025
## 16 2.9 2015 0.0290
## 17 1.6 2016 0.016
## 18 2.4 2017 0.024
## 19 2.9 2018 0.0290
Merge
# Merge the two data sets.
data_merged <-
econ_decimal %>%
left_join(stock_returns)
data_merged
## # A tibble: 19 x 4
## price year GDPgrowth yearly.returns
## <dbl> <dbl> <dbl> <dbl>
## 1 4.1 2000 0.0410 0.843
## 2 1 2001 0.01 -0.393
## 3 1.7 2002 0.017 -0.211
## 4 2.9 2003 0.0290 -0.315
## 5 3.8 2004 0.038 0.500
## 6 3.5 2005 0.035 0.0859
## 7 2.9 2006 0.0290 0.0137
## 8 1.9 2007 0.019 0.0952
## 9 -0.1 2008 -0.001 0.0981
## 10 -2.5 2009 -0.025 -0.405
## 11 2.6 2010 0.026 0.439
## 12 1.6 2011 0.016 0.169
## 13 2.2 2012 0.022 -0.0180
## 14 1.8 2013 0.018 0.159
## 15 2.5 2014 0.025 0.383
## 16 2.9 2015 0.0290 0.134
## 17 1.6 2016 0.016 0.0573
## 18 2.4 2017 0.024 0.0750
## 19 2.9 2018 0.0290 0.282
Plot
# Visualize the relaionship between stock returns and GDP growth
data_merged %>%
ggplot(aes(yearly.returns, GDPgrowth)) +
geom_point() +
scale_x_continuous(label = scales::percent) +
scale_y_continuous(label = scales::percent) +
geom_smooth(method = "lm")
stock.prices
Import S&P500 since 2009.newstocks <- tq_get("^GSPC", get = "stock.prices", from = "2009-01-01")
newstocks
## # A tibble: 2,706 x 7
## date open high low close volume adjusted
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2009-01-02 903. 935. 899. 932. 4048270000 932.
## 2 2009-01-05 929. 937. 920. 927. 5413910000 927.
## 3 2009-01-06 931. 944. 927. 935. 5392620000 935.
## 4 2009-01-07 927. 927. 902. 907. 4704940000 907.
## 5 2009-01-08 906. 910 897. 910. 4991550000 910.
## 6 2009-01-09 910. 912. 888. 890. 4716500000 890.
## 7 2009-01-12 890. 890. 864. 870. 4725050000 870.
## 8 2009-01-13 870. 877. 862. 872. 5567460000 872.
## 9 2009-01-14 867. 867. 837. 843. 5407880000 843.
## 10 2009-01-15 842. 852. 817. 844. 7807350000 844.
## # … with 2,696 more rows
newstock_returns <-
newstocks %>%
tq_transmute(select = adjusted, mutate_fun = periodReturn, period = "yearly") %>%
mutate(year = year(date)+1) %>%
# Drop date
select(-date)
newstock_returns
## # A tibble: 11 x 2
## yearly.returns year
## <dbl> <dbl>
## 1 0.197 2010
## 2 0.128 2011
## 3 -0.0000318 2012
## 4 0.134 2013
## 5 0.296 2014
## 6 0.114 2015
## 7 -0.00727 2016
## 8 0.0954 2017
## 9 0.194 2018
## 10 -0.0624 2019
## 11 0.152 2020
economic.data
Import real U.S. GDP growth since 2010.newecon <- tq_get("A191RL1A225NBEA", get = "economic.data", from = "2010-01-01")
newecon
## # A tibble: 9 x 2
## date price
## <date> <dbl>
## 1 2010-01-01 2.6
## 2 2011-01-01 1.6
## 3 2012-01-01 2.2
## 4 2013-01-01 1.8
## 5 2014-01-01 2.5
## 6 2015-01-01 2.9
## 7 2016-01-01 1.6
## 8 2017-01-01 2.4
## 9 2018-01-01 2.9
newecon_decimal <-
newecon %>%
mutate(year = year(date), # Create a new variable, year
GDPgrowth = price/100) %>% # Convert price to decimal number
# Drop date
select(-date)
newecon_decimal
## # A tibble: 9 x 3
## price year GDPgrowth
## <dbl> <dbl> <dbl>
## 1 2.6 2010 0.026
## 2 1.6 2011 0.016
## 3 2.2 2012 0.022
## 4 1.8 2013 0.018
## 5 2.5 2014 0.025
## 6 2.9 2015 0.0290
## 7 1.6 2016 0.016
## 8 2.4 2017 0.024
## 9 2.9 2018 0.0290
newdata_merged <-
newecon_decimal %>%
left_join(newstock_returns)
newdata_merged
## # A tibble: 9 x 4
## price year GDPgrowth yearly.returns
## <dbl> <dbl> <dbl> <dbl>
## 1 2.6 2010 0.026 0.197
## 2 1.6 2011 0.016 0.128
## 3 2.2 2012 0.022 -0.0000318
## 4 1.8 2013 0.018 0.134
## 5 2.5 2014 0.025 0.296
## 6 2.9 2015 0.0290 0.114
## 7 1.6 2016 0.016 -0.00727
## 8 2.4 2017 0.024 0.0954
## 9 2.9 2018 0.0290 0.194
newdata_merged %>%
ggplot(aes(yearly.returns, GDPgrowth)) +
geom_point() +
scale_x_continuous(label = scales::percent) +
scale_y_continuous(label = scales::percent) +
geom_smooth(method = "lm")
Stock market goes up this year ecomony next year will be doing well. ## Q8 Hide the messages, but display the code and its results on the webpage. Hint: Use message
, echo
and results
in the chunk options. Refer to the RMarkdown Reference Guide.