stock.prices
Import S&P500 since 2009.economic.data
Import real U.S. GDP growth since 2010.stock.prices
## # 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
## # 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
## # 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
## # 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
stock.prices
Import S&P500 since 2009.Hint: Google tidyquant::tq_get() to find example codes.
## # 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
Hint: Calculate yearly returns; create a new variable, year; and drop date.
## # 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.Hint: Find the symbol in FRED. Select in the list of related variables, Percent Change from Preceding Period, Annual, Not Seasonally Adjusted
.
Hint: Create a new variable, year; convert price to decimal number; and drop date.
## # 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
Hint: Google dplyr::left_join() to find example codes.
## # 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
Hint: See the code in 4.2.1 Scatterplot in the textbook.
The realtionship between the stock markets and economic growth is as yearly stock market increases, so does economic growth.
Hint: Use message
, echo
and results
in the chunk options. Refer to the RMarkdown Reference Guide.