stock.prices Import S&P500 since 1999.economic.data Import real U.S. GDP growth since 2000.# Load packages
library(tidyquant)
library(tidyverse)
stock.prices
# Import NASDAQ since 2009.
stocks <- tq_get("^IXIC", get = "stock.prices", from = "2009-01-01")
stocks
## # A tibble: 2,824 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,814 more rows
# Calculate returns
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: 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.235 2021
economic.data
# Import real U.S. GDP growth since 2010.
econ <- tq_get("A191RL1A225NBEA", get = "economic.data", from = "2010-01-01")
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: 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
# Merge the two data sets.
data_merged <-
econ_decimal %>%
left_join(stock_returns)
data_merged
## # 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
# 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 1999.Hint: Google tidyquant::tq_get() to find example codes.
stocks <- tq_get("^GSPC", get = "stock.prices", from = "1999-01-01")
stocks
## # A tibble: 5,339 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,329 more rows
Hint: Calculate yearly returns; create a new variable, year; and drop date.
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: 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.
# Import real U.S. GDP growth since 2010.
econ <- tq_get("A191RL1A225NBEA", get = "economic.data", from = "2010-01-01")
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: 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
Hint: Create a new variable, year; convert price to decimal number; and drop date.
# Import real U.S. GDP growth since 2010.
econ <- tq_get("A191RL1A225NBEA", get = "economic.data", from = "2010-01-01")
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: 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
Hint: Google dplyr::left_join() to find example codes.
# Merge the two data sets.
data_merged <-
econ_decimal %>%
left_join(stock_returns)
data_merged
## # A tibble: 10 x 5
## symbol price year GDPgrowth yearly.returns
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 A191RL1A225NBEA 2.6 2010 0.026 0.235
## 2 A191RL1A225NBEA 1.6 2011 0.016 0.128
## 3 A191RL1A225NBEA 2.2 2012 0.022 -0.0000318
## 4 A191RL1A225NBEA 1.8 2013 0.018 0.134
## 5 A191RL1A225NBEA 2.5 2014 0.025 0.296
## 6 A191RL1A225NBEA 2.9 2015 0.0290 0.114
## 7 A191RL1A225NBEA 1.6 2016 0.016 -0.00727
## 8 A191RL1A225NBEA 2.4 2017 0.024 0.0954
## 9 A191RL1A225NBEA 2.9 2018 0.0290 0.194
## 10 A191RL1A225NBEA 2.3 2019 0.023 -0.0624
Hint: See the code in 4.2.1 Scatterplot in the textbook.
# 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")
As the yearly return percentage increases, gdp increases as well.
Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.