economic data Import the U.S. Industrial Production Index since 2010.stock prices Import stock prices of NASDAQ and S&P500 since 2019.filter Select S&P500 and save it under stocks_filtered.select Select two variables (date and closing price) from stocks_filtered, and save it under stocks_selected.exchange rates Import the exchange rate between the U.S. dollar and the Chinese Yuan Renminbi, and save it under FX.left_join Merge FX and stocks_selected.Scatterplot Plot the relationship between the stock market and the exchange rate. Add the best fit line.# Load packages
library(tidyquant)
library(tidyverse)
library(dplyr)
economic data Import the U.S. Industrial Production Index since 2010.Hint: Find the symbol in FRED.
Production <- tq_get("INDPRO", get = "economic.data", from = "2010-01-01")
Production
## # A tibble: 122 x 2
## date price
## <date> <dbl>
## 1 2010-01-01 91.7
## 2 2010-02-01 92.0
## 3 2010-03-01 92.6
## 4 2010-04-01 92.9
## 5 2010-05-01 94.3
## 6 2010-06-01 94.4
## 7 2010-07-01 94.9
## 8 2010-08-01 95.1
## 9 2010-09-01 95.4
## 10 2010-10-01 95.1
## # … with 112 more rows
stock prices Import stock prices of NASDAQ and S&P500 since 2019.stocks <- tq_get(c("^GSPC","IXIC"), get = "stock.prices", from = "2019-01-01")
## Warning: x = 'IXIC', get = 'stock.prices': Error in getSymbols.yahoo(Symbols = "IXIC", env = <environment>, verbose = FALSE, : Unable to import "IXIC".
## IXIC download failed after two attempts. Error message:
## HTTP error 404.
## Removing IXIC.
## Warning: `cols` is now required.
## Please use `cols = c(stock.prices)`
stocks
## # A tibble: 310 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 ^GSPC 2019-01-02 2477. 2519. 2467. 2510. 3733160000 2510.
## 2 ^GSPC 2019-01-03 2492. 2493. 2444. 2448. 3822860000 2448.
## 3 ^GSPC 2019-01-04 2474. 2538. 2474. 2532. 4213410000 2532.
## 4 ^GSPC 2019-01-07 2536. 2566. 2525. 2550. 4104710000 2550.
## 5 ^GSPC 2019-01-08 2568. 2580. 2548. 2574. 4083030000 2574.
## 6 ^GSPC 2019-01-09 2580 2595. 2569. 2585. 4052480000 2585.
## 7 ^GSPC 2019-01-10 2574. 2598. 2562. 2597. 3704500000 2597.
## 8 ^GSPC 2019-01-11 2588. 2596. 2577. 2596. 3434490000 2596.
## 9 ^GSPC 2019-01-14 2580. 2589. 2570. 2583. 3664450000 2583.
## 10 ^GSPC 2019-01-15 2585. 2613. 2585. 2610. 3572330000 2610.
## # … with 300 more rows
filter Select S&P500 and save it under stocks_filtered.Hint: See the code in 1.2.2 Selecting observations
stocks_filtered <-filter(stocks,
symbol == "^GSPC")
stocks_filtered
## # A tibble: 310 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 ^GSPC 2019-01-02 2477. 2519. 2467. 2510. 3733160000 2510.
## 2 ^GSPC 2019-01-03 2492. 2493. 2444. 2448. 3822860000 2448.
## 3 ^GSPC 2019-01-04 2474. 2538. 2474. 2532. 4213410000 2532.
## 4 ^GSPC 2019-01-07 2536. 2566. 2525. 2550. 4104710000 2550.
## 5 ^GSPC 2019-01-08 2568. 2580. 2548. 2574. 4083030000 2574.
## 6 ^GSPC 2019-01-09 2580 2595. 2569. 2585. 4052480000 2585.
## 7 ^GSPC 2019-01-10 2574. 2598. 2562. 2597. 3704500000 2597.
## 8 ^GSPC 2019-01-11 2588. 2596. 2577. 2596. 3434490000 2596.
## 9 ^GSPC 2019-01-14 2580. 2589. 2570. 2583. 3664450000 2583.
## 10 ^GSPC 2019-01-15 2585. 2613. 2585. 2610. 3572330000 2610.
## # … with 300 more rows
select Select two variables (date and closing price) from stocks_filtered, and save it under stocks_selected.Hint: See the code in 1.2.2 Selecting observations
stocks_selected <-select(stocks_filtered, symbol, date, close )
stocks_selected
## # A tibble: 310 x 3
## symbol date close
## <chr> <date> <dbl>
## 1 ^GSPC 2019-01-02 2510.
## 2 ^GSPC 2019-01-03 2448.
## 3 ^GSPC 2019-01-04 2532.
## 4 ^GSPC 2019-01-07 2550.
## 5 ^GSPC 2019-01-08 2574.
## 6 ^GSPC 2019-01-09 2585.
## 7 ^GSPC 2019-01-10 2597.
## 8 ^GSPC 2019-01-11 2596.
## 9 ^GSPC 2019-01-14 2583.
## 10 ^GSPC 2019-01-15 2610.
## # … with 300 more rows
exchange rates Import the exchange rate between the U.S. dollar and the Chinese Yuan Renminbi, and save it under FX.Hint: Find the symbol in oanda.com.
FX <-tq_get("CNY/USD",
get = "exchange.rates")
## Warning: Oanda only provides historical data for the past 180 days. Symbol: CNY/
## USD
FX
## # A tibble: 180 x 2
## date exchange.rate
## <date> <dbl>
## 1 2019-09-28 0.140
## 2 2019-09-29 0.140
## 3 2019-09-30 0.140
## 4 2019-10-01 0.140
## 5 2019-10-02 0.140
## 6 2019-10-03 0.140
## 7 2019-10-04 0.140
## 8 2019-10-05 0.140
## 9 2019-10-06 0.140
## 10 2019-10-07 0.140
## # … with 170 more rows
left_join Merge FX and stocks_selected.Hint: For left_join, refer to our other textbook, R for Data Scince, Ch13.4 Mutating joins. Or Google dplyr::left_join() to find example codes.
data_merged <-stocks_selected %>%
left_join(FX)
data_merged
## # A tibble: 310 x 4
## symbol date close exchange.rate
## <chr> <date> <dbl> <dbl>
## 1 ^GSPC 2019-01-02 2510. NA
## 2 ^GSPC 2019-01-03 2448. NA
## 3 ^GSPC 2019-01-04 2532. NA
## 4 ^GSPC 2019-01-07 2550. NA
## 5 ^GSPC 2019-01-08 2574. NA
## 6 ^GSPC 2019-01-09 2585. NA
## 7 ^GSPC 2019-01-10 2597. NA
## 8 ^GSPC 2019-01-11 2596. NA
## 9 ^GSPC 2019-01-14 2583. NA
## 10 ^GSPC 2019-01-15 2610. NA
## # … with 300 more rows
Scatterplot Plot the relationship between the stock market and the exchange rate. Add the best fit line.Hint: See the code in 4.2.1 Scatterplot
data_merged %>%
ggplot(aes(close, exchange.rate))+
geom_point()+
geom_smooth(method = "lm")
## Warning: Removed 187 rows containing non-finite values (stat_smooth).
## Warning: Removed 187 rows containing missing values (geom_point).
Hint: See the scatterplot you created in the previous question.
The Exchange rate and stock market don’t have much correlation expect the are both rising at the moment. The exchange rate has a lot of outliers in it so it’s hard to correlate but around 3000 in the stock market there is some correlation of an upward trend.
Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.