stock.prices Import S&P500 since 1999.economic.data Import real U.S. GDP growth since 2000.stock.prices
## # A tibble: 2,848 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 ^IXIC 2009-01-02 1579. 1636. 1572. 1632. 1438410000 1632.
## 2 ^IXIC 2009-01-05 1621. 1640. 1605. 1628. 1816580000 1628.
## 3 ^IXIC 2009-01-06 1642. 1666. 1636. 1652. 2137640000 1652.
## 4 ^IXIC 2009-01-07 1622. 1625. 1588. 1599. 2020170000 1599.
## 5 ^IXIC 2009-01-08 1590. 1617. 1584. 1617. 1968160000 1617.
## 6 ^IXIC 2009-01-09 1617. 1617. 1570. 1572. 1907390000 1572.
## 7 ^IXIC 2009-01-12 1573. 1573. 1528 1539. 1763590000 1539.
## 8 ^IXIC 2009-01-13 1537. 1558. 1527. 1546. 1965570000 1546.
## 9 ^IXIC 2009-01-14 1522. 1529. 1485. 1490. 1919980000 1490.
## 10 ^IXIC 2009-01-15 1489. 1522. 1457. 1512. 2507870000 1512.
## # … with 2,838 more rows
## # A tibble: 12 x 2
## yearly.returns year
## <dbl> <dbl>
## 1 0.390 2010
## 2 0.169 2011
## 3 -0.0180 2012
## 4 0.159 2013
## 5 0.383 2014
## 6 0.134 2015
## 7 0.0573 2016
## 8 0.0750 2017
## 9 0.282 2018
## 10 -0.0388 2019
## 11 0.352 2020
## 12 -0.0270 2021
economic.data
## # A tibble: 10 x 4
## symbol price year GDPgrowth
## <chr> <dbl> <dbl> <dbl>
## 1 A191RL1A225NBEA 2.6 2010 0.026
## 2 A191RL1A225NBEA 1.6 2011 0.016
## 3 A191RL1A225NBEA 2.2 2012 0.022
## 4 A191RL1A225NBEA 1.8 2013 0.018
## 5 A191RL1A225NBEA 2.5 2014 0.025
## 6 A191RL1A225NBEA 2.9 2015 0.0290
## 7 A191RL1A225NBEA 1.6 2016 0.016
## 8 A191RL1A225NBEA 2.4 2017 0.024
## 9 A191RL1A225NBEA 2.9 2018 0.0290
## 10 A191RL1A225NBEA 2.3 2019 0.023
Merge
## # A tibble: 10 x 5
## symbol price year GDPgrowth yearly.returns
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 A191RL1A225NBEA 2.6 2010 0.026 0.390
## 2 A191RL1A225NBEA 1.6 2011 0.016 0.169
## 3 A191RL1A225NBEA 2.2 2012 0.022 -0.0180
## 4 A191RL1A225NBEA 1.8 2013 0.018 0.159
## 5 A191RL1A225NBEA 2.5 2014 0.025 0.383
## 6 A191RL1A225NBEA 2.9 2015 0.0290 0.134
## 7 A191RL1A225NBEA 1.6 2016 0.016 0.0573
## 8 A191RL1A225NBEA 2.4 2017 0.024 0.0750
## 9 A191RL1A225NBEA 2.9 2018 0.0290 0.282
## 10 A191RL1A225NBEA 2.3 2019 0.023 -0.0388
Plot
stock.prices Import S&P500 since 1999.Hint: Google tidyquant::tq_get() to find example codes.
## # A tibble: 5,363 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 ^GSPC 1999-01-04 1229. 1249. 1219. 1228. 877000000 1228.
## 2 ^GSPC 1999-01-05 1228. 1246. 1228. 1245. 775000000 1245.
## 3 ^GSPC 1999-01-06 1245. 1272. 1245. 1272. 986900000 1272.
## 4 ^GSPC 1999-01-07 1272. 1272. 1258. 1270. 863000000 1270.
## 5 ^GSPC 1999-01-08 1270. 1278. 1262. 1275. 937800000 1275.
## 6 ^GSPC 1999-01-11 1275. 1276. 1253. 1264. 818000000 1264.
## 7 ^GSPC 1999-01-12 1264. 1264. 1238. 1240. 800200000 1240.
## 8 ^GSPC 1999-01-13 1240. 1248. 1205. 1234. 931500000 1234.
## 9 ^GSPC 1999-01-14 1234. 1237. 1210. 1212. 797200000 1212.
## 10 ^GSPC 1999-01-15 1212. 1243. 1212. 1243. 798100000 1243.
## # … with 5,353 more rows
Hint: Calculate yearly returns; create a new variable, year; and drop date.
## # A tibble: 22 x 2
## yearly.returns year
## <dbl> <dbl>
## 1 0.196 2000
## 2 -0.101 2001
## 3 -0.130 2002
## 4 -0.234 2003
## 5 0.264 2004
## 6 0.0899 2005
## 7 0.0300 2006
## 8 0.136 2007
## 9 0.0353 2008
## 10 -0.385 2009
## # … with 12 more rows
economic.data Import real U.S. GDP growth since 2000.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: 42 x 4
## symbol price year GDPgrowth
## <chr> <dbl> <dbl> <dbl>
## 1 GDPPOT 16229. 2010 162.
## 2 GDPPOT 16274. 2010 163.
## 3 GDPPOT 16321. 2010 163.
## 4 GDPPOT 16371. 2010 164.
## 5 GDPPOT 16423. 2011 164.
## 6 GDPPOT 16479. 2011 165.
## 7 GDPPOT 16536. 2011 165.
## 8 GDPPOT 16597. 2011 166.
## 9 GDPPOT 16659. 2012 167.
## 10 GDPPOT 16725. 2012 167.
## # … with 32 more rows
Hint: Google dplyr::left_join() to find example codes. Or refer to one of our textbooks: R for Data Science, Chapter 13 Relational data.
## # A tibble: 42 x 5
## symbol price year GDPgrowth yearly.returns
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 GDPPOT 16229. 2010 162. 0.235
## 2 GDPPOT 16274. 2010 163. 0.235
## 3 GDPPOT 16321. 2010 163. 0.235
## 4 GDPPOT 16371. 2010 164. 0.235
## 5 GDPPOT 16423. 2011 164. 0.128
## 6 GDPPOT 16479. 2011 165. 0.128
## 7 GDPPOT 16536. 2011 165. 0.128
## 8 GDPPOT 16597. 2011 166. 0.128
## 9 GDPPOT 16659. 2012 167. -0.0000318
## 10 GDPPOT 16725. 2012 167. -0.0000318
## # … with 32 more rows
Hint: See the code in 4.2.1 Scatterplot in the textbook.
The perfection of the stock market has brought great benefits to the economic development.
Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.