# Load packages
library(tidyquant) 
library(tidyverse) 

stock.prices

# Import NASDAQ since 2010.
stocks <- tq_get(c("AAPL","MSFT"), get = "stock.prices", from = "1999-01-01")
stocks
## # A tibble: 10,448 x 8
##    symbol date        open  high   low close    volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
##  1 AAPL   1999-01-04  1.50  1.51  1.43  1.47 238221200     1.29
##  2 AAPL   1999-01-05  1.50  1.57  1.48  1.55 352528400     1.35
##  3 AAPL   1999-01-06  1.58  1.58  1.46  1.49 337142400     1.30
##  4 AAPL   1999-01-07  1.51  1.61  1.50  1.61 357254800     1.40
##  5 AAPL   1999-01-08  1.66  1.67  1.57  1.61 169708000     1.40
##  6 AAPL   1999-01-11  1.63  1.65  1.60  1.64 140243600     1.43
##  7 AAPL   1999-01-12  1.65  1.67  1.58  1.65 205184000     1.44
##  8 AAPL   1999-01-13  1.53  1.69  1.51  1.66 261954000     1.45
##  9 AAPL   1999-01-14  1.62  1.64  1.47  1.48 430964800     1.29
## 10 AAPL   1999-01-15  1.49  1.50  1.43  1.48 251501600     1.29
## # … with 10,438 more rows
stock_returns <-
  stocks %>%
  group_by(symbol) %>%
  # 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: 42 x 3
## # Groups:   symbol [2]
##    symbol yearly.returns  year
##    <chr>           <dbl> <dbl>
##  1 AAPL            1.49   2000
##  2 AAPL           -0.711  2001
##  3 AAPL            0.472  2002
##  4 AAPL           -0.346  2003
##  5 AAPL            0.491  2004
##  6 AAPL            2.01   2005
##  7 AAPL            1.23   2006
##  8 AAPL            0.180  2007
##  9 AAPL            1.33   2008
## 10 AAPL           -0.569  2009
## # … with 32 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: 38 x 5
##    price  year GDPgrowth symbol yearly.returns
##    <dbl> <dbl>     <dbl> <chr>           <dbl>
##  1   4.1  2000    0.0410 AAPL           1.49  
##  2   4.1  2000    0.0410 MSFT           0.656 
##  3   1    2001    0.01   AAPL          -0.711 
##  4   1    2001    0.01   MSFT          -0.628 
##  5   1.7  2002    0.017  AAPL           0.472 
##  6   1.7  2002    0.017  MSFT           0.527 
##  7   2.9  2003    0.0290 AAPL          -0.346 
##  8   2.9  2003    0.0290 MSFT          -0.220 
##  9   3.8  2004    0.038  AAPL           0.491 
## 10   3.8  2004    0.038  MSFT           0.0682
## # … with 28 more rows

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") +
  facet_wrap(~symbol)

Q1. stock.prices Import Dow Jones Industrial Average and NASDAQ indexes since 1999.

stocks <- tq_get(c("^DJI","^IXIC"), get = "stock.prices", from = "1999-01-01")
stocks
## # A tibble: 10,448 x 8
##    symbol date        open  high   low close    volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
##  1 ^DJI   1999-01-04 9184. 9350. 9122. 9184.  89410000    9184.
##  2 ^DJI   1999-01-05 9185. 9339. 9183. 9311.  79860000    9311.
##  3 ^DJI   1999-01-06 9315. 9562. 9315. 9545. 103340000    9545.
##  4 ^DJI   1999-01-07 9542. 9542. 9426. 9538.  88290000    9538.
##  5 ^DJI   1999-01-08 9538. 9648. 9525. 9643. 103250000    9643.
##  6 ^DJI   1999-01-11 9643. 9643. 9533. 9620.  98720000    9620.
##  7 ^DJI   1999-01-12 9619. 9620. 9452. 9475.  95680000    9475.
##  8 ^DJI   1999-01-13 9471. 9471. 9213. 9350. 109060000    9350.
##  9 ^DJI   1999-01-14 9350. 9359. 9088. 9121.  90510000    9121.
## 10 ^DJI   1999-01-15 9127. 9343. 9127. 9341.  79440000    9341.
## # … with 10,438 more rows

Q2. Prepare the imported stock prices for merge.

stock_returns <-
  stocks %>%
  group_by(symbol) %>%
  # 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: 42 x 3
## # Groups:   symbol [2]
##    symbol yearly.returns  year
##    <chr>           <dbl> <dbl>
##  1 ^DJI          0.252    2000
##  2 ^DJI         -0.0617   2001
##  3 ^DJI         -0.0710   2002
##  4 ^DJI         -0.168    2003
##  5 ^DJI          0.253    2004
##  6 ^DJI          0.0315   2005
##  7 ^DJI         -0.00608  2006
##  8 ^DJI          0.163    2007
##  9 ^DJI          0.0643   2008
## 10 ^DJI         -0.338    2009
## # … with 32 more rows

Q3 economic.data Import real U.S. GDP growth since 2000.

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

Hint: Find the symbol in FRED. Select in the list of related variables, Percent Change from Preceding Period, Annual, Not Seasonally Adjusted.

Q4. Prepare the imported economic data for merge.

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

Q5. Merge the two data sets.

data_merged <-
  econ_decimal %>%
  left_join(stock_returns)
data_merged
## # A tibble: 38 x 5
##    price  year GDPgrowth symbol yearly.returns
##    <dbl> <dbl>     <dbl> <chr>           <dbl>
##  1   4.1  2000    0.0410 ^DJI           0.252 
##  2   4.1  2000    0.0410 ^IXIC          0.843 
##  3   1    2001    0.01   ^DJI          -0.0617
##  4   1    2001    0.01   ^IXIC         -0.393 
##  5   1.7  2002    0.017  ^DJI          -0.0710
##  6   1.7  2002    0.017  ^IXIC         -0.211 
##  7   2.9  2003    0.0290 ^DJI          -0.168 
##  8   2.9  2003    0.0290 ^IXIC         -0.315 
##  9   3.8  2004    0.038  ^DJI           0.253 
## 10   3.8  2004    0.038  ^IXIC          0.500 
## # … with 28 more rows

Q6. Plot the relaionship between stock returns (of both indexes) 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") +
  facet_wrap(~symbol)

Hint: See the code in 4.2.1 Scatterplot in the textbook.

Q7. Describe the relationship between the two stock markets and economic growth. Which of the two indexes seems to have greater inflence on the economy?

Q8 Hide the messages, the code and its results on the webpage.

Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.

Q9 Display the title and your name correctly at the top of the webpage.

Q10 Use the correct slug.